Point Cloud Library (PCL)  1.11.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 
38 #pragma once
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  using PointCloudPtr = typename PointCloud::Ptr;
67 
68  using Ptr = shared_ptr<PointCloudGeometryHandler<PointT> >;
69  using ConstPtr = shared_ptr<const PointCloudGeometryHandler<PointT> >;
70 
71  /** \brief Constructor. */
73  cloud_ (cloud), capable_ (false),
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
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  using PointCloudPtr = typename PointCloud::Ptr;
145 
146  using Ptr = shared_ptr<PointCloudGeometryHandlerXYZ<PointT> >;
147  using ConstPtr = shared_ptr<const PointCloudGeometryHandlerXYZ<PointT> >;
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 displayed on screen as XYZ data.
183  * \author Radu B. Rusu
184  * \ingroup visualization
185  */
186  template <typename PointT>
188  {
189  public:
191  using PointCloudPtr = typename PointCloud::Ptr;
193 
194  using Ptr = shared_ptr<PointCloudGeometryHandlerSurfaceNormal<PointT> >;
195  using ConstPtr = shared_ptr<const PointCloudGeometryHandlerSurfaceNormal<PointT> >;
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  using PointCloudPtr = typename PointCloud::Ptr;
238 
239  using Ptr = shared_ptr<PointCloudGeometryHandlerCustom<PointT> >;
240  using ConstPtr = shared_ptr<const PointCloudGeometryHandlerCustom<PointT> >;
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)
248  {
249  field_x_idx_ = pcl::getFieldIndex<PointT> (x_field_name, fields_);
250  if (field_x_idx_ == UNAVAILABLE)
251  return;
252  field_y_idx_ = pcl::getFieldIndex<PointT> (y_field_name, fields_);
253  if (field_y_idx_ == UNAVAILABLE)
254  return;
255  field_z_idx_ = pcl::getFieldIndex<PointT> (z_field_name, fields_);
256  if (field_z_idx_ == UNAVAILABLE)
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_->size ());
283 
284  float data;
285  // Add all points
286  double p[3];
287  for (vtkIdType i = 0; i < static_cast<vtkIdType> (cloud_->size ()); ++i)
288  {
289  // Copy the value at the specified field
290  const std::uint8_t* pt_data = reinterpret_cast<const std::uint8_t*> (&(*cloud_)[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  using Ptr = shared_ptr<PointCloudGeometryHandler<PointCloud> >;
331  using ConstPtr = shared_ptr<const PointCloudGeometryHandler<PointCloud> >;
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_ (UNAVAILABLE)
338  , field_y_idx_ (UNAVAILABLE)
339  , field_z_idx_ (UNAVAILABLE)
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
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
370  {
371  cloud_ = cloud;
372  }
373 
374  protected:
375  /** \brief A pointer to the input dataset. */
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 <>
404  {
405  public:
409 
410  using Ptr = shared_ptr<PointCloudGeometryHandlerXYZ<PointCloud> >;
411  using ConstPtr = shared_ptr<const PointCloudGeometryHandlerXYZ<PointCloud> >;
412 
413  /** \brief Constructor. */
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 <>
437  {
438  public:
442 
443  using Ptr = shared_ptr<PointCloudGeometryHandlerSurfaceNormal<PointCloud> >;
444  using ConstPtr = shared_ptr<const PointCloudGeometryHandlerSurfaceNormal<PointCloud> >;
445 
446  /** \brief Constructor. */
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 <>
467  {
468  public:
472 
473  /** \brief Constructor. */
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
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:181
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:429
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:430
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:65
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
Definition: types.h:120
std::uint8_t uint8_t
Definition: types.h:54
#define PCL_EXPORTS
Definition: pcl_macros.h:328
A point structure representing Euclidean xyz coordinates, and the RGB color.