Point Cloud Library (PCL)  1.9.1
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. */
72  PointCloudGeometryHandler (const PointCloudConstPtr &cloud) :
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
105  setInputCloud (const PointCloudConstPtr &cloud)
106  {
107  cloud_ = cloud;
108  }
109 
110  protected:
111  /** \brief A pointer to the input dataset. */
112  PointCloudConstPtr cloud_;
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. */
150  PointCloudGeometryHandlerXYZ (const PointCloudConstPtr &cloud);
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 displayed 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. */
198  PointCloudGeometryHandlerSurfaceNormal (const PointCloudConstPtr &cloud);
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. */
243  PointCloudGeometryHandlerCustom (const PointCloudConstPtr &cloud,
244  const std::string &x_field_name,
245  const std::string &y_field_name,
246  const std::string &z_field_name)
248  {
249  field_x_idx_ = pcl::getFieldIndex (*cloud, x_field_name, fields_);
250  if (field_x_idx_ == -1)
251  return;
252  field_y_idx_ = pcl::getFieldIndex (*cloud, y_field_name, fields_);
253  if (field_y_idx_ == -1)
254  return;
255  field_z_idx_ = pcl::getFieldIndex (*cloud, z_field_name, fields_);
256  if (field_z_idx_ == -1)
257  return;
258  field_name_ = x_field_name + y_field_name + z_field_name;
259  capable_ = true;
260  }
261 
262  /** \brief Class getName method. */
263  virtual std::string
264  getName () const { return ("PointCloudGeometryHandlerCustom"); }
265 
266  /** \brief Get the name of the field used. */
267  virtual std::string
268  getFieldName () const { return (field_name_); }
269 
270  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
271  * \param[out] points the resultant geometry
272  */
273  virtual void
275  {
276  if (!capable_)
277  return;
278 
279  if (!points)
281  points->SetDataTypeToFloat ();
282  points->SetNumberOfPoints (cloud_->points.size ());
283 
284  float data;
285  // Add all points
286  double p[3];
287  for (vtkIdType i = 0; i < static_cast<vtkIdType> (cloud_->points.size ()); ++i)
288  {
289  // Copy the value at the specified field
290  const uint8_t* pt_data = reinterpret_cast<const uint8_t*> (&cloud_->points[i]);
291  memcpy (&data, pt_data + fields_[field_x_idx_].offset, sizeof (float));
292  p[0] = data;
293 
294  memcpy (&data, pt_data + fields_[field_y_idx_].offset, sizeof (float));
295  p[1] = data;
296 
297  memcpy (&data, pt_data + fields_[field_z_idx_].offset, sizeof (float));
298  p[2] = data;
299 
300  points->SetPoint (i, p);
301  }
302  }
303 
304  private:
305  // Members derived from the base class
312 
313  /** \brief Name of the field used to create the geometry handler. */
314  std::string field_name_;
315  };
316 
317  ///////////////////////////////////////////////////////////////////////////////////////
318  /** \brief Base handler class for PointCloud geometry.
319  * \author Radu B. Rusu
320  * \ingroup visualization
321  */
322  template <>
324  {
325  public:
329 
330  typedef boost::shared_ptr<PointCloudGeometryHandler<PointCloud> > Ptr;
331  typedef boost::shared_ptr<const PointCloudGeometryHandler<PointCloud> > ConstPtr;
332 
333  /** \brief Constructor. */
334  PointCloudGeometryHandler (const PointCloudConstPtr &cloud, const Eigen::Vector4f & = Eigen::Vector4f::Zero ())
335  : cloud_ (cloud)
336  , capable_ (false)
337  , field_x_idx_ (-1)
338  , field_y_idx_ (-1)
339  , field_z_idx_ (-1)
340  , fields_ (cloud_->fields)
341  {
342  }
343 
344  /** \brief Destructor. */
346 
347  /** \brief Abstract getName method. */
348  virtual std::string
349  getName () const = 0;
350 
351  /** \brief Abstract getFieldName method. */
352  virtual std::string
353  getFieldName () const = 0;
354 
355  /** \brief Check if this handler is capable of handling the input data or not. */
356  inline bool
357  isCapable () const { return (capable_); }
358 
359  /** \brief Obtain the actual point geometry for the input dataset in VTK format.
360  * \param[out] points the resultant geometry
361  */
362  virtual void
363  getGeometry (vtkSmartPointer<vtkPoints> &points) const;
364 
365  /** \brief Set the input cloud to be used.
366  * \param[in] cloud the input cloud to be used by the handler
367  */
368  void
369  setInputCloud (const PointCloudConstPtr &cloud)
370  {
371  cloud_ = cloud;
372  }
373 
374  protected:
375  /** \brief A pointer to the input dataset. */
376  PointCloudConstPtr cloud_;
377 
378  /** \brief True if this handler is capable of handling the input data, false
379  * otherwise.
380  */
381  bool capable_;
382 
383  /** \brief The index of the field holding the X data. */
385 
386  /** \brief The index of the field holding the Y data. */
388 
389  /** \brief The index of the field holding the Z data. */
391 
392  /** \brief The list of fields available for this PointCloud. */
393  std::vector<pcl::PCLPointField> fields_;
394  };
395 
396  //////////////////////////////////////////////////////////////////////////////////////
397  /** \brief XYZ handler class for PointCloud geometry. Given an input dataset, all XYZ
398  * data present in fields "x", "y", and "z" is extracted and displayed on screen.
399  * \author Radu B. Rusu
400  * \ingroup visualization
401  */
402  template <>
403  class PCL_EXPORTS PointCloudGeometryHandlerXYZ<pcl::PCLPointCloud2> : public PointCloudGeometryHandler<pcl::PCLPointCloud2>
404  {
405  public:
409 
410  typedef boost::shared_ptr<PointCloudGeometryHandlerXYZ<PointCloud> > Ptr;
411  typedef boost::shared_ptr<const PointCloudGeometryHandlerXYZ<PointCloud> > ConstPtr;
412 
413  /** \brief Constructor. */
414  PointCloudGeometryHandlerXYZ (const PointCloudConstPtr &cloud);
415 
416  /** \brief Destructor. */
418 
419  /** \brief Class getName method. */
420  virtual std::string
421  getName () const { return ("PointCloudGeometryHandlerXYZ"); }
422 
423  /** \brief Get the name of the field used. */
424  virtual std::string
425  getFieldName () const { return ("xyz"); }
426  };
427 
428  //////////////////////////////////////////////////////////////////////////////////////
429  /** \brief Surface normal handler class for PointCloud geometry. Given an input
430  * dataset, all data present in fields "normal_x", "normal_y", and "normal_z" is
431  * extracted and displayed on screen as XYZ data.
432  * \author Radu B. Rusu
433  * \ingroup visualization
434  */
435  template <>
436  class PCL_EXPORTS PointCloudGeometryHandlerSurfaceNormal<pcl::PCLPointCloud2> : public PointCloudGeometryHandler<pcl::PCLPointCloud2>
437  {
438  public:
442 
443  typedef boost::shared_ptr<PointCloudGeometryHandlerSurfaceNormal<PointCloud> > Ptr;
444  typedef boost::shared_ptr<const PointCloudGeometryHandlerSurfaceNormal<PointCloud> > ConstPtr;
445 
446  /** \brief Constructor. */
447  PointCloudGeometryHandlerSurfaceNormal (const PointCloudConstPtr &cloud);
448 
449  /** \brief Class getName method. */
450  virtual std::string
451  getName () const { return ("PointCloudGeometryHandlerSurfaceNormal"); }
452 
453  /** \brief Get the name of the field used. */
454  virtual std::string
455  getFieldName () const { return ("normal_xyz"); }
456  };
457 
458  //////////////////////////////////////////////////////////////////////////////////////
459  /** \brief Custom handler class for PointCloud geometry. Given an input dataset and
460  * three user defined fields, all data present in them is extracted and displayed on
461  * screen as XYZ data.
462  * \author Radu B. Rusu
463  * \ingroup visualization
464  */
465  template <>
466  class PCL_EXPORTS PointCloudGeometryHandlerCustom<pcl::PCLPointCloud2> : public PointCloudGeometryHandler<pcl::PCLPointCloud2>
467  {
468  public:
472 
473  /** \brief Constructor. */
474  PointCloudGeometryHandlerCustom (const PointCloudConstPtr &cloud,
475  const std::string &x_field_name,
476  const std::string &y_field_name,
477  const std::string &z_field_name);
478 
479  /** \brief Destructor. */
481 
482  /** \brief Class getName method. */
483  virtual std::string
484  getName () const { return ("PointCloudGeometryHandlerCustom"); }
485 
486  /** \brief Get the name of the field used. */
487  virtual std::string
488  getFieldName () const { return (field_name_); }
489 
490  private:
491  /** \brief Name of the field used to create the geometry handler. */
492  std::string field_name_;
493  };
494  }
495 }
496 
497 #ifdef PCL_NO_PRECOMPILE
498 #include <pcl/visualization/impl/point_cloud_geometry_handlers.hpp>
499 #endif
500 
501 #endif // PCL_POINT_CLOUD_GEOMETRY_HANDLERS_H_
502 
boost::shared_ptr< PointCloudGeometryHandlerCustom< PointT > > Ptr
std::vector< pcl::PCLPointField > fields_
The list of fields available for this PointCloud.
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.
PointCloudConstPtr cloud_
A pointer to the input dataset.
Custom handler class for PointCloud geometry.
boost::shared_ptr< const PointCloudGeometryHandlerXYZ< PointT > > ConstPtr
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
virtual std::string getName() const
Class getName method.
PointCloudGeometryHandler< PointT >::PointCloud PointCloud
virtual std::string getFieldName() const
Get the name of the field used.
boost::shared_ptr< PointCloudGeometryHandlerSurfaceNormal< PointCloud > > Ptr
bool capable_
True if this handler is capable of handling the input data, false otherwise.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
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 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, const Eigen::Vector4f &=Eigen::Vector4f::Zero())
Constructor.
PointCloudGeometryHandler(const PointCloudConstPtr &cloud)
Constructor.
PointCloudGeometryHandler< PointT >::PointCloud PointCloud
boost::shared_ptr< const PointCloudGeometryHandlerSurfaceNormal< PointT > > ConstPtr
boost::shared_ptr< PointCloudGeometryHandlerXYZ< PointCloud > > Ptr
boost::shared_ptr< ::pcl::PCLPointCloud2 > Ptr
boost::shared_ptr< PointCloudGeometryHandlerSurfaceNormal< PointT > > Ptr
bool isCapable() const
Checl if this handler is capable of handling the input data or not.
boost::shared_ptr< const PointCloudGeometryHandlerCustom< PointT > > ConstPtr
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:428
boost::shared_ptr< const PointCloudGeometryHandlerXYZ< PointCloud > > ConstPtr
PointCloudGeometryHandler< PointT >::PointCloud PointCloud
PointCloudGeometryHandler< pcl::PCLPointCloud2 >::PointCloud PointCloud
int field_y_idx_
The index of the field holding the Y data.
boost::shared_ptr< PointCloudGeometryHandlerXYZ< PointT > > Ptr
boost::shared_ptr< PointCloudGeometryHandler< PointCloud > > Ptr
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:429
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
PointCloud represents the base class in PCL for storing collections of 3D points. ...
PointCloudGeometryHandler< pcl::PCLPointCloud2 >::PointCloud PointCloud
int field_z_idx_
The index of the field holding the Z data.
virtual std::string getName() const
Class getName method.
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 getFieldName() const
Get the name of the field used.
A point structure representing Euclidean xyz coordinates, and the RGB color.
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 std::string getFieldName() const
Get the name of the field used.
virtual std::string getFieldName() const
Get the name of the field used.