Point Cloud Library (PCL)  1.12.0
point_cloud_geometry_handlers.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #pragma once
39 
40 #if defined __GNUC__
41 #pragma GCC system_header
42 #endif
43 
44 // PCL includes
45 #include <pcl/pcl_base.h> // for UNAVAILABLE
46 #include <pcl/point_cloud.h>
47 #include <pcl/common/io.h>
48 // VTK includes
49 #include <vtkSmartPointer.h>
50 #include <vtkPoints.h>
51 #include <vtkFloatArray.h>
52 
53 namespace pcl
54 {
55  namespace visualization
56  {
57  /** \brief Base handler class for PointCloud geometry.
58  * \author Radu B. Rusu
59  * \ingroup visualization
60  */
61  template <typename PointT>
63  {
64  public:
66  using PointCloudPtr = typename PointCloud::Ptr;
68 
69  using Ptr = shared_ptr<PointCloudGeometryHandler<PointT> >;
70  using ConstPtr = shared_ptr<const PointCloudGeometryHandler<PointT> >;
71 
72  /** \brief Constructor. */
74  cloud_ (cloud), capable_ (false),
76  fields_ ()
77  {}
78 
79  /** \brief Destructor. */
81 
82  /** \brief Abstract getName method.
83  * \return the name of the class/object.
84  */
85  virtual std::string
86  getName () const = 0;
87 
88  /** \brief Abstract getFieldName method. */
89  virtual std::string
90  getFieldName () const = 0;
91 
92  /** \brief Checl if this handler is capable of handling the input data or not. */
93  inline bool
94  isCapable () const { return (capable_); }
95 
96  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
97  * \param[out] points the resultant geometry
98  */
99  virtual void
101 
102  /** \brief Set the input cloud to be used.
103  * \param[in] cloud the input cloud to be used by the handler
104  */
105  void
107  {
108  cloud_ = cloud;
109  }
110 
111  protected:
112  /** \brief A pointer to the input dataset. */
114 
115  /** \brief True if this handler is capable of handling the input data, false
116  * otherwise.
117  */
118  bool capable_;
119 
120  /** \brief The index of the field holding the X data. */
122 
123  /** \brief The index of the field holding the Y data. */
125 
126  /** \brief The index of the field holding the Z data. */
128 
129  /** \brief The list of fields available for this PointCloud. */
130  std::vector<pcl::PCLPointField> fields_;
131  };
132 
133  //////////////////////////////////////////////////////////////////////////////////////
134  /** \brief XYZ handler class for PointCloud geometry. Given an input dataset, all XYZ
135  * data present in fields "x", "y", and "z" is extracted and displayed on screen.
136  * \author Radu B. Rusu
137  * \ingroup visualization
138  */
139  template <typename PointT>
141  {
142  public:
144  using PointCloudPtr = typename PointCloud::Ptr;
146 
147  using Ptr = shared_ptr<PointCloudGeometryHandlerXYZ<PointT> >;
148  using ConstPtr = shared_ptr<const PointCloudGeometryHandlerXYZ<PointT> >;
149 
150  /** \brief Constructor. */
152 
153  /** \brief Destructor. */
155 
156  /** \brief Class getName method. */
157  virtual std::string
158  getName () const { return ("PointCloudGeometryHandlerXYZ"); }
159 
160  /** \brief Get the name of the field used. */
161  virtual std::string
162  getFieldName () const { return ("xyz"); }
163 
164  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
165  * \param[out] points the resultant geometry
166  */
167  virtual void
168  getGeometry (vtkSmartPointer<vtkPoints> &points) const;
169 
170  private:
171  // Members derived from the base class
178  };
179 
180  //////////////////////////////////////////////////////////////////////////////////////
181  /** \brief Surface normal handler class for PointCloud geometry. Given an input
182  * dataset, all data present in fields "normal_x", "normal_y", and "normal_z" is
183  * extracted and displayed on screen as XYZ data.
184  * \author Radu B. Rusu
185  * \ingroup visualization
186  */
187  template <typename PointT>
189  {
190  public:
192  using PointCloudPtr = typename PointCloud::Ptr;
194 
195  using Ptr = shared_ptr<PointCloudGeometryHandlerSurfaceNormal<PointT> >;
196  using ConstPtr = shared_ptr<const PointCloudGeometryHandlerSurfaceNormal<PointT> >;
197 
198  /** \brief Constructor. */
200 
201  /** \brief Class getName method. */
202  virtual std::string
203  getName () const { return ("PointCloudGeometryHandlerSurfaceNormal"); }
204 
205  /** \brief Get the name of the field used. */
206  virtual std::string
207  getFieldName () const { return ("normal_xyz"); }
208 
209  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
210  * \param[out] points the resultant geometry
211  */
212  virtual void
213  getGeometry (vtkSmartPointer<vtkPoints> &points) const;
214 
215  private:
216  // Members derived from the base class
223  };
224 
225  //////////////////////////////////////////////////////////////////////////////////////
226  /** \brief Custom handler class for PointCloud geometry. Given an input dataset and
227  * three user defined fields, all data present in them is extracted and displayed on
228  * screen as XYZ data.
229  * \author Radu B. Rusu
230  * \ingroup visualization
231  */
232  template <typename PointT>
234  {
235  public:
237  using PointCloudPtr = typename PointCloud::Ptr;
239 
240  using Ptr = shared_ptr<PointCloudGeometryHandlerCustom<PointT> >;
241  using ConstPtr = shared_ptr<const PointCloudGeometryHandlerCustom<PointT> >;
242 
243  /** \brief Constructor. */
245  const std::string &x_field_name,
246  const std::string &y_field_name,
247  const std::string &z_field_name)
249  {
250  field_x_idx_ = pcl::getFieldIndex<PointT> (x_field_name, fields_);
251  if (field_x_idx_ == UNAVAILABLE)
252  return;
253  field_y_idx_ = pcl::getFieldIndex<PointT> (y_field_name, fields_);
254  if (field_y_idx_ == UNAVAILABLE)
255  return;
256  field_z_idx_ = pcl::getFieldIndex<PointT> (z_field_name, fields_);
257  if (field_z_idx_ == UNAVAILABLE)
258  return;
259  field_name_ = x_field_name + y_field_name + z_field_name;
260  capable_ = true;
261  }
262 
263  /** \brief Class getName method. */
264  virtual std::string
265  getName () const { return ("PointCloudGeometryHandlerCustom"); }
266 
267  /** \brief Get the name of the field used. */
268  virtual std::string
269  getFieldName () const { return (field_name_); }
270 
271  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
272  * \param[out] points the resultant geometry
273  */
274  virtual void
276  {
277  if (!capable_)
278  return;
279 
280  if (!points)
282  points->SetDataTypeToFloat ();
283  points->SetNumberOfPoints (cloud_->size ());
284 
285  float data;
286  // Add all points
287  double p[3];
288  for (vtkIdType i = 0; i < static_cast<vtkIdType> (cloud_->size ()); ++i)
289  {
290  // Copy the value at the specified field
291  const std::uint8_t* pt_data = reinterpret_cast<const std::uint8_t*> (&(*cloud_)[i]);
292  memcpy (&data, pt_data + fields_[field_x_idx_].offset, sizeof (float));
293  p[0] = data;
294 
295  memcpy (&data, pt_data + fields_[field_y_idx_].offset, sizeof (float));
296  p[1] = data;
297 
298  memcpy (&data, pt_data + fields_[field_z_idx_].offset, sizeof (float));
299  p[2] = data;
300 
301  points->SetPoint (i, p);
302  }
303  }
304 
305  private:
306  // Members derived from the base class
313 
314  /** \brief Name of the field used to create the geometry handler. */
315  std::string field_name_;
316  };
317 
318  ///////////////////////////////////////////////////////////////////////////////////////
319  /** \brief Base handler class for PointCloud geometry.
320  * \author Radu B. Rusu
321  * \ingroup visualization
322  */
323  template <>
325  {
326  public:
330 
331  using Ptr = shared_ptr<PointCloudGeometryHandler<PointCloud> >;
332  using ConstPtr = shared_ptr<const PointCloudGeometryHandler<PointCloud> >;
333 
334  /** \brief Constructor. */
335  PointCloudGeometryHandler (const PointCloudConstPtr &cloud, const Eigen::Vector4f & = Eigen::Vector4f::Zero ())
336  : cloud_ (cloud)
337  , capable_ (false)
338  , field_x_idx_ (UNAVAILABLE)
339  , field_y_idx_ (UNAVAILABLE)
340  , field_z_idx_ (UNAVAILABLE)
341  , fields_ (cloud_->fields)
342  {
343  }
344 
345  /** \brief Destructor. */
347 
348  /** \brief Abstract getName method. */
349  virtual std::string
350  getName () const = 0;
351 
352  /** \brief Abstract getFieldName method. */
353  virtual std::string
354  getFieldName () const = 0;
355 
356  /** \brief Check if this handler is capable of handling the input data or not. */
357  inline bool
358  isCapable () const { return (capable_); }
359 
360  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
361  * \param[out] points the resultant geometry
362  */
363  virtual void
365 
366  /** \brief Set the input cloud to be used.
367  * \param[in] cloud the input cloud to be used by the handler
368  */
369  void
371  {
372  cloud_ = cloud;
373  }
374 
375  protected:
376  /** \brief A pointer to the input dataset. */
378 
379  /** \brief True if this handler is capable of handling the input data, false
380  * otherwise.
381  */
382  bool capable_;
383 
384  /** \brief The index of the field holding the X data. */
386 
387  /** \brief The index of the field holding the Y data. */
389 
390  /** \brief The index of the field holding the Z data. */
392 
393  /** \brief The list of fields available for this PointCloud. */
394  std::vector<pcl::PCLPointField> fields_;
395  };
396 
397  //////////////////////////////////////////////////////////////////////////////////////
398  /** \brief XYZ handler class for PointCloud geometry. Given an input dataset, all XYZ
399  * data present in fields "x", "y", and "z" is extracted and displayed on screen.
400  * \author Radu B. Rusu
401  * \ingroup visualization
402  */
403  template <>
405  {
406  public:
410 
411  using Ptr = shared_ptr<PointCloudGeometryHandlerXYZ<PointCloud> >;
412  using ConstPtr = shared_ptr<const PointCloudGeometryHandlerXYZ<PointCloud> >;
413 
414  /** \brief Constructor. */
416 
417  /** \brief Destructor. */
419 
420  /** \brief Class getName method. */
421  virtual std::string
422  getName () const { return ("PointCloudGeometryHandlerXYZ"); }
423 
424  /** \brief Get the name of the field used. */
425  virtual std::string
426  getFieldName () const { return ("xyz"); }
427  };
428 
429  //////////////////////////////////////////////////////////////////////////////////////
430  /** \brief Surface normal handler class for PointCloud geometry. Given an input
431  * dataset, all data present in fields "normal_x", "normal_y", and "normal_z" is
432  * extracted and displayed on screen as XYZ data.
433  * \author Radu B. Rusu
434  * \ingroup visualization
435  */
436  template <>
438  {
439  public:
443 
444  using Ptr = shared_ptr<PointCloudGeometryHandlerSurfaceNormal<PointCloud> >;
445  using ConstPtr = shared_ptr<const PointCloudGeometryHandlerSurfaceNormal<PointCloud> >;
446 
447  /** \brief Constructor. */
449 
450  /** \brief Class getName method. */
451  virtual std::string
452  getName () const { return ("PointCloudGeometryHandlerSurfaceNormal"); }
453 
454  /** \brief Get the name of the field used. */
455  virtual std::string
456  getFieldName () const { return ("normal_xyz"); }
457  };
458 
459  //////////////////////////////////////////////////////////////////////////////////////
460  /** \brief Custom handler class for PointCloud geometry. Given an input dataset and
461  * three user defined fields, all data present in them is extracted and displayed on
462  * screen as XYZ data.
463  * \author Radu B. Rusu
464  * \ingroup visualization
465  */
466  template <>
468  {
469  public:
473 
474  /** \brief Constructor. */
476  const std::string &x_field_name,
477  const std::string &y_field_name,
478  const std::string &z_field_name);
479 
480  /** \brief Destructor. */
482 
483  /** \brief Class getName method. */
484  virtual std::string
485  getName () const { return ("PointCloudGeometryHandlerCustom"); }
486 
487  /** \brief Get the name of the field used. */
488  virtual std::string
489  getFieldName () const { return (field_name_); }
490 
491  private:
492  /** \brief Name of the field used to create the geometry handler. */
493  std::string field_name_;
494  };
495  }
496 }
497 
498 #ifdef PCL_NO_PRECOMPILE
499 #include <pcl/visualization/impl/point_cloud_geometry_handlers.hpp>
500 #endif
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
virtual std::string getName() const =0
Abstract getName method.
void setInputCloud(const PointCloudConstPtr &cloud)
Set the input cloud to be used.
shared_ptr< const PointCloudGeometryHandler< PointCloud > > ConstPtr
virtual void getGeometry(vtkSmartPointer< vtkPoints > &points) const
Obtain the actual point geometry for the input dataset in VTK format.
bool isCapable() const
Check if this handler is capable of handling the input data or not.
bool capable_
True if this handler is capable of handling the input data, false otherwise.
std::vector< pcl::PCLPointField > fields_
The list of fields available for this PointCloud.
PointCloudGeometryHandler(const PointCloudConstPtr &cloud, const Eigen::Vector4f &=Eigen::Vector4f::Zero())
Constructor.
virtual std::string getFieldName() const =0
Abstract getFieldName method.
virtual std::string getFieldName() const
Get the name of the field used.
PointCloudGeometryHandlerCustom(const PointCloudConstPtr &cloud, const std::string &x_field_name, const std::string &y_field_name, const std::string &z_field_name)
Constructor.
virtual std::string getFieldName() const
Get the name of the field used.
virtual std::string getName() const
Class getName method.
virtual void getGeometry(vtkSmartPointer< vtkPoints > &points) const
Obtain the actual point geometry for the input dataset in VTK format.
PointCloudGeometryHandlerCustom(const PointCloudConstPtr &cloud, const std::string &x_field_name, const std::string &y_field_name, const std::string &z_field_name)
Constructor.
Base handler class for PointCloud geometry.
std::vector< pcl::PCLPointField > fields_
The list of fields available for this PointCloud.
index_t field_y_idx_
The index of the field holding the Y data.
virtual std::string getName() const =0
Abstract getName method.
bool isCapable() const
Checl if this handler is capable of handling the input data or not.
index_t field_z_idx_
The index of the field holding the Z data.
shared_ptr< const PointCloudGeometryHandler< PointT > > ConstPtr
PointCloudGeometryHandler(const PointCloudConstPtr &cloud)
Constructor.
bool capable_
True if this handler is capable of handling the input data, false otherwise.
void setInputCloud(const PointCloudConstPtr &cloud)
Set the input cloud to be used.
PointCloudConstPtr cloud_
A pointer to the input dataset.
virtual std::string getFieldName() const =0
Abstract getFieldName method.
virtual void getGeometry(vtkSmartPointer< vtkPoints > &points) const =0
Obtain the actual point geometry for the input dataset in VTK format.
shared_ptr< PointCloudGeometryHandler< PointT > > Ptr
index_t field_x_idx_
The index of the field holding the X data.
PointCloudGeometryHandlerSurfaceNormal(const PointCloudConstPtr &cloud)
Constructor.
Surface normal handler class for PointCloud geometry.
virtual std::string getFieldName() const
Get the name of the field used.
virtual void getGeometry(vtkSmartPointer< vtkPoints > &points) const
Obtain the actual point geometry for the input dataset in VTK format.
PointCloudGeometryHandlerSurfaceNormal(const PointCloudConstPtr &cloud)
Constructor.
virtual std::string getName() const
Class getName method.
virtual std::string getFieldName() const
Get the name of the field used.
PointCloudGeometryHandlerXYZ(const PointCloudConstPtr &cloud)
Constructor.
virtual void getGeometry(vtkSmartPointer< vtkPoints > &points) const
Obtain the actual point geometry for the input dataset in VTK format.
PointCloudGeometryHandlerXYZ(const PointCloudConstPtr &cloud)
Constructor.
virtual std::string getFieldName() const
Get the name of the field used.
virtual std::string getName() const
Class getName method.
static constexpr index_t UNAVAILABLE
Definition: pcl_base.h:62
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
Definition: types.h:112
#define PCL_EXPORTS
Definition: pcl_macros.h:323
A point structure representing Euclidean xyz coordinates, and the RGB color.