Point Cloud Library (PCL)  1.7.2
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 #ifndef PCL_POINT_CLOUD_GEOMETRY_HANDLERS_H_
38 #define PCL_POINT_CLOUD_GEOMETRY_HANDLERS_H_
39 
40 #if defined __GNUC__
41 #pragma GCC system_header
42 #endif
43 
44 // PCL includes
45 #include <pcl/point_cloud.h>
46 #include <pcl/common/io.h>
47 // VTK includes
48 #include <vtkSmartPointer.h>
49 #include <vtkPoints.h>
50 #include <vtkFloatArray.h>
51 
52 namespace pcl
53 {
54  namespace visualization
55  {
56  /** \brief Base handler class for PointCloud geometry.
57  * \author Radu B. Rusu
58  * \ingroup visualization
59  */
60  template <typename PointT>
62  {
63  public:
65  typedef typename PointCloud::Ptr PointCloudPtr;
67 
68  typedef typename boost::shared_ptr<PointCloudGeometryHandler<PointT> > Ptr;
69  typedef typename boost::shared_ptr<const PointCloudGeometryHandler<PointT> > ConstPtr;
70 
71  /** \brief Constructor. */
73  cloud_ (cloud), capable_ (false),
74  field_x_idx_ (-1), field_y_idx_ (-1), field_z_idx_ (-1),
75  fields_ ()
76  {}
77 
78  /** \brief Destructor. */
80 
81  /** \brief Abstract getName method.
82  * \return the name of the class/object.
83  */
84  virtual std::string
85  getName () const = 0;
86 
87  /** \brief Abstract getFieldName method. */
88  virtual std::string
89  getFieldName () const = 0;
90 
91  /** \brief Checl if this handler is capable of handling the input data or not. */
92  inline bool
93  isCapable () const { return (capable_); }
94 
95  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
96  * \param[out] points the resultant geometry
97  */
98  virtual void
99  getGeometry (vtkSmartPointer<vtkPoints> &points) const = 0;
100 
101  /** \brief Set the input cloud to be used.
102  * \param[in] cloud the input cloud to be used by the handler
103  */
104  void
106  {
107  cloud_ = cloud;
108  }
109 
110  protected:
111  /** \brief A pointer to the input dataset. */
113 
114  /** \brief True if this handler is capable of handling the input data, false
115  * otherwise.
116  */
117  bool capable_;
118 
119  /** \brief The index of the field holding the X data. */
121 
122  /** \brief The index of the field holding the Y data. */
124 
125  /** \brief The index of the field holding the Z data. */
127 
128  /** \brief The list of fields available for this PointCloud. */
129  std::vector<pcl::PCLPointField> fields_;
130  };
131 
132  //////////////////////////////////////////////////////////////////////////////////////
133  /** \brief XYZ handler class for PointCloud geometry. Given an input dataset, all XYZ
134  * data present in fields "x", "y", and "z" is extracted and displayed on screen.
135  * \author Radu B. Rusu
136  * \ingroup visualization
137  */
138  template <typename PointT>
140  {
141  public:
143  typedef typename PointCloud::Ptr PointCloudPtr;
145 
146  typedef typename boost::shared_ptr<PointCloudGeometryHandlerXYZ<PointT> > Ptr;
147  typedef typename boost::shared_ptr<const PointCloudGeometryHandlerXYZ<PointT> > ConstPtr;
148 
149  /** \brief Constructor. */
151 
152  /** \brief Destructor. */
154 
155  /** \brief Class getName method. */
156  virtual std::string
157  getName () const { return ("PointCloudGeometryHandlerXYZ"); }
158 
159  /** \brief Get the name of the field used. */
160  virtual std::string
161  getFieldName () const { return ("xyz"); }
162 
163  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
164  * \param[out] points the resultant geometry
165  */
166  virtual void
167  getGeometry (vtkSmartPointer<vtkPoints> &points) const;
168 
169  private:
170  // Members derived from the base class
177  };
178 
179  //////////////////////////////////////////////////////////////////////////////////////
180  /** \brief Surface normal handler class for PointCloud geometry. Given an input
181  * dataset, all data present in fields "normal_x", "normal_y", and "normal_z" is
182  * extracted and dislayed on screen as XYZ data.
183  * \author Radu B. Rusu
184  * \ingroup visualization
185  */
186  template <typename PointT>
188  {
189  public:
191  typedef typename PointCloud::Ptr PointCloudPtr;
193 
194  typedef typename boost::shared_ptr<PointCloudGeometryHandlerSurfaceNormal<PointT> > Ptr;
195  typedef typename boost::shared_ptr<const PointCloudGeometryHandlerSurfaceNormal<PointT> > ConstPtr;
196 
197  /** \brief Constructor. */
199 
200  /** \brief Class getName method. */
201  virtual std::string
202  getName () const { return ("PointCloudGeometryHandlerSurfaceNormal"); }
203 
204  /** \brief Get the name of the field used. */
205  virtual std::string
206  getFieldName () const { return ("normal_xyz"); }
207 
208  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
209  * \param[out] points the resultant geometry
210  */
211  virtual void
212  getGeometry (vtkSmartPointer<vtkPoints> &points) const;
213 
214  private:
215  // Members derived from the base class
222  };
223 
224  //////////////////////////////////////////////////////////////////////////////////////
225  /** \brief Custom handler class for PointCloud geometry. Given an input dataset and
226  * three user defined fields, all data present in them is extracted and displayed on
227  * screen as XYZ data.
228  * \author Radu B. Rusu
229  * \ingroup visualization
230  */
231  template <typename PointT>
233  {
234  public:
236  typedef typename PointCloud::Ptr PointCloudPtr;
238 
239  typedef typename boost::shared_ptr<PointCloudGeometryHandlerCustom<PointT> > Ptr;
240  typedef typename boost::shared_ptr<const PointCloudGeometryHandlerCustom<PointT> > ConstPtr;
241 
242  /** \brief Constructor. */
244  const std::string &x_field_name,
245  const std::string &y_field_name,
246  const std::string &z_field_name)
247  {
248  field_x_idx_ = pcl::getFieldIndex (*cloud, x_field_name, fields_);
249  if (field_x_idx_ == -1)
250  return;
251  field_y_idx_ = pcl::getFieldIndex (*cloud, y_field_name, fields_);
252  if (field_y_idx_ == -1)
253  return;
254  field_z_idx_ = pcl::getFieldIndex (*cloud, z_field_name, fields_);
255  if (field_z_idx_ == -1)
256  return;
257  field_name_ = x_field_name + y_field_name + z_field_name;
258  capable_ = true;
259  }
260 
261  /** \brief Class getName method. */
262  virtual std::string
263  getName () const { return ("PointCloudGeometryHandlerCustom"); }
264 
265  /** \brief Get the name of the field used. */
266  virtual std::string
267  getFieldName () const { return (field_name_); }
268 
269  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
270  * \param[out] points the resultant geometry
271  */
272  virtual void
274  {
275  if (!capable_)
276  return;
277 
278  if (!points)
280  points->SetDataTypeToFloat ();
281  points->SetNumberOfPoints (cloud_->points.size ());
282 
283  float data;
284  // Add all points
285  double p[3];
286  for (vtkIdType i = 0; i < static_cast<vtkIdType> (cloud_->points.size ()); ++i)
287  {
288  // Copy the value at the specified field
289  const uint8_t* pt_data = reinterpret_cast<const uint8_t*> (&cloud_->points[i]);
290  memcpy (&data, pt_data + fields_[field_x_idx_].offset, sizeof (float));
291  p[0] = data;
292 
293  memcpy (&data, pt_data + fields_[field_y_idx_].offset, sizeof (float));
294  p[1] = data;
295 
296  memcpy (&data, pt_data + fields_[field_z_idx_].offset, sizeof (float));
297  p[2] = data;
298 
299  points->SetPoint (i, p);
300  }
301  }
302 
303  private:
304  // Members derived from the base class
311 
312  /** \brief Name of the field used to create the geometry handler. */
313  std::string field_name_;
314  };
315 
316  ///////////////////////////////////////////////////////////////////////////////////////
317  /** \brief Base handler class for PointCloud geometry.
318  * \author Radu B. Rusu
319  * \ingroup visualization
320  */
321  template <>
323  {
324  public:
328 
329  typedef boost::shared_ptr<PointCloudGeometryHandler<PointCloud> > Ptr;
330  typedef boost::shared_ptr<const PointCloudGeometryHandler<PointCloud> > ConstPtr;
331 
332  /** \brief Constructor. */
333  PointCloudGeometryHandler (const PointCloudConstPtr &cloud, const Eigen::Vector4f & = Eigen::Vector4f::Zero ())
334  : cloud_ (cloud)
335  , capable_ (false)
336  , field_x_idx_ (-1)
337  , field_y_idx_ (-1)
338  , field_z_idx_ (-1)
339  , fields_ (cloud_->fields)
340  {
341  }
342 
343  /** \brief Destructor. */
345 
346  /** \brief Abstract getName method. */
347  virtual std::string
348  getName () const = 0;
349 
350  /** \brief Abstract getFieldName method. */
351  virtual std::string
352  getFieldName () const = 0;
353 
354  /** \brief Check if this handler is capable of handling the input data or not. */
355  inline bool
356  isCapable () const { return (capable_); }
357 
358  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
359  * \param[out] points the resultant geometry
360  */
361  virtual void
362  getGeometry (vtkSmartPointer<vtkPoints> &points) const;
363 
364  /** \brief Set the input cloud to be used.
365  * \param[in] cloud the input cloud to be used by the handler
366  */
367  void
369  {
370  cloud_ = cloud;
371  }
372 
373  protected:
374  /** \brief A pointer to the input dataset. */
376 
377  /** \brief True if this handler is capable of handling the input data, false
378  * otherwise.
379  */
380  bool capable_;
381 
382  /** \brief The index of the field holding the X data. */
384 
385  /** \brief The index of the field holding the Y data. */
387 
388  /** \brief The index of the field holding the Z data. */
390 
391  /** \brief The list of fields available for this PointCloud. */
392  std::vector<pcl::PCLPointField> fields_;
393  };
394 
395  //////////////////////////////////////////////////////////////////////////////////////
396  /** \brief XYZ handler class for PointCloud geometry. Given an input dataset, all XYZ
397  * data present in fields "x", "y", and "z" is extracted and displayed on screen.
398  * \author Radu B. Rusu
399  * \ingroup visualization
400  */
401  template <>
402  class PCL_EXPORTS PointCloudGeometryHandlerXYZ<pcl::PCLPointCloud2> : public PointCloudGeometryHandler<pcl::PCLPointCloud2>
403  {
404  public:
408 
409  typedef boost::shared_ptr<PointCloudGeometryHandlerXYZ<PointCloud> > Ptr;
410  typedef boost::shared_ptr<const PointCloudGeometryHandlerXYZ<PointCloud> > ConstPtr;
411 
412  /** \brief Constructor. */
414 
415  /** \brief Destructor. */
417 
418  /** \brief Class getName method. */
419  virtual std::string
420  getName () const { return ("PointCloudGeometryHandlerXYZ"); }
421 
422  /** \brief Get the name of the field used. */
423  virtual std::string
424  getFieldName () const { return ("xyz"); }
425  };
426 
427  //////////////////////////////////////////////////////////////////////////////////////
428  /** \brief Surface normal handler class for PointCloud geometry. Given an input
429  * dataset, all data present in fields "normal_x", "normal_y", and "normal_z" is
430  * extracted and dislayed on screen as XYZ data.
431  * \author Radu B. Rusu
432  * \ingroup visualization
433  */
434  template <>
435  class PCL_EXPORTS PointCloudGeometryHandlerSurfaceNormal<pcl::PCLPointCloud2> : public PointCloudGeometryHandler<pcl::PCLPointCloud2>
436  {
437  public:
441 
442  typedef boost::shared_ptr<PointCloudGeometryHandlerSurfaceNormal<PointCloud> > Ptr;
443  typedef boost::shared_ptr<const PointCloudGeometryHandlerSurfaceNormal<PointCloud> > ConstPtr;
444 
445  /** \brief Constructor. */
447 
448  /** \brief Class getName method. */
449  virtual std::string
450  getName () const { return ("PointCloudGeometryHandlerSurfaceNormal"); }
451 
452  /** \brief Get the name of the field used. */
453  virtual std::string
454  getFieldName () const { return ("normal_xyz"); }
455  };
456 
457  //////////////////////////////////////////////////////////////////////////////////////
458  /** \brief Custom handler class for PointCloud geometry. Given an input dataset and
459  * three user defined fields, all data present in them is extracted and displayed on
460  * screen as XYZ data.
461  * \author Radu B. Rusu
462  * \ingroup visualization
463  */
464  template <>
465  class PCL_EXPORTS PointCloudGeometryHandlerCustom<pcl::PCLPointCloud2> : public PointCloudGeometryHandler<pcl::PCLPointCloud2>
466  {
467  public:
471 
472  /** \brief Constructor. */
474  const std::string &x_field_name,
475  const std::string &y_field_name,
476  const std::string &z_field_name);
477 
478  /** \brief Destructor. */
480 
481  /** \brief Class getName method. */
482  virtual std::string
483  getName () const { return ("PointCloudGeometryHandlerCustom"); }
484 
485  /** \brief Get the name of the field used. */
486  virtual std::string
487  getFieldName () const { return (field_name_); }
488 
489  private:
490  /** \brief Name of the field used to create the geometry handler. */
491  std::string field_name_;
492  };
493  }
494 }
495 
496 #ifdef PCL_NO_PRECOMPILE
497 #include <pcl/visualization/impl/point_cloud_geometry_handlers.hpp>
498 #endif
499 
500 #endif // PCL_POINT_CLOUD_GEOMETRY_HANDLERS_H_
501 
boost::shared_ptr< PointCloudGeometryHandlerCustom< PointT > > Ptr
std::vector< pcl::PCLPointField > fields_
The list of fields available for this PointCloud.
PointCloudGeometryHandlerXYZ(const PointCloudConstPtr &cloud)
Constructor.
PointCloudConstPtr cloud_
A pointer to the input dataset.
Custom handler class for PointCloud geometry.
boost::shared_ptr< const PointCloudGeometryHandlerXYZ< PointT > > ConstPtr
virtual std::string getFieldName() const
Get the name of the field used.
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
boost::shared_ptr< PointCloudGeometryHandler< PointT > > Ptr
int getFieldIndex(const pcl::PCLPointCloud2 &cloud, const std::string &field_name)
Get the index of a specified field (i.e., dimension/channel)
Definition: io.h:59
PointCloudGeometryHandler< PointT >::PointCloud PointCloud
boost::shared_ptr< PointCloudGeometryHandlerSurfaceNormal< PointCloud > > Ptr
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
bool capable_
True if this handler is capable of handling the input data, false otherwise.
virtual std::string getFieldName() const
Get the name of the field used.
boost::shared_ptr< const PointCloudGeometryHandlerSurfaceNormal< PointCloud > > ConstPtr
virtual void getGeometry(vtkSmartPointer< vtkPoints > &points) const =0
Obtain the actual point geometry for the input dataset in VTK format.
boost::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.
virtual std::string getFieldName() const
Get the name of the field used.
void setInputCloud(const PointCloudConstPtr &cloud)
Set the input cloud to be used.
Surface normal handler class for PointCloud geometry.
virtual std::string getName() const
Class getName method.
PointCloudGeometryHandler(const PointCloudConstPtr &cloud)
Constructor.
PointCloudGeometryHandler< PointT >::PointCloud PointCloud
boost::shared_ptr< const PointCloudGeometryHandlerSurfaceNormal< PointT > > ConstPtr
boost::shared_ptr< PointCloudGeometryHandlerXYZ< PointCloud > > Ptr
bool isCapable() const
Check if this handler is capable of handling the input data or not.
boost::shared_ptr< ::pcl::PCLPointCloud2 > Ptr
boost::shared_ptr< PointCloudGeometryHandlerSurfaceNormal< PointT > > Ptr
boost::shared_ptr< const PointCloudGeometryHandlerCustom< PointT > > ConstPtr
boost::shared_ptr< const PointCloudGeometryHandlerXYZ< PointCloud > > ConstPtr
PointCloudGeometryHandler< PointT >::PointCloud PointCloud
PointCloudGeometryHandlerSurfaceNormal(const PointCloudConstPtr &cloud)
Constructor.
PointCloudGeometryHandler< pcl::PCLPointCloud2 >::PointCloud PointCloud
int field_y_idx_
The index of the field holding the Y data.
boost::shared_ptr< PointCloudGeometryHandlerXYZ< PointT > > Ptr
virtual std::string getFieldName() const
Get the name of the field used.
virtual std::string getFieldName() const
Get the name of the field used.
boost::shared_ptr< PointCloudGeometryHandler< PointCloud > > Ptr
PointCloudGeometryHandlerCustom(const PointCloudConstPtr &cloud, const std::string &x_field_name, const std::string &y_field_name, const std::string &z_field_name)
Constructor.
void setInputCloud(const PointCloudConstPtr &cloud)
Set the input cloud to be used.
boost::shared_ptr< ::pcl::PCLPointCloud2 const > ConstPtr
PointCloudGeometryHandler< pcl::PCLPointCloud2 >::PointCloud PointCloud
int field_z_idx_
The index of the field holding the Z data.
virtual void getGeometry(vtkSmartPointer< vtkPoints > &points) const
Obtain the actual point geometry for the input dataset in VTK format.
virtual std::string getName() const
Class getName method.
bool isCapable() const
Checl if this handler is capable of handling the input data or not.
Base handler class for PointCloud geometry.
virtual std::string getFieldName() const =0
Abstract getFieldName method.
virtual std::string getName() const =0
Abstract getName method.
virtual std::string getName() const
Class getName method.
std::vector< pcl::PCLPointField > fields_
The list of fields available for this PointCloud.
int field_x_idx_
The index of the field holding the X data.
bool capable_
True if this handler is capable of handling the input data, false otherwise.
boost::shared_ptr< const PointCloudGeometryHandler< PointT > > ConstPtr
virtual void getGeometry(vtkSmartPointer< vtkPoints > &points) const
Obtain the actual point geometry for the input dataset in VTK format.