Point Cloud Library (PCL)  1.7.1
point_cloud_color_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_COLOR_HANDLERS_H_
38 #define PCL_POINT_CLOUD_COLOR_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 #include <pcl/visualization/common/common.h>
48 // VTK includes
49 #include <vtkSmartPointer.h>
50 #include <vtkDataArray.h>
51 #include <vtkFloatArray.h>
52 #include <vtkUnsignedCharArray.h>
53 
54 namespace pcl
55 {
56  namespace visualization
57  {
58  //////////////////////////////////////////////////////////////////////////////////////
59  /** \brief Base Handler class for PointCloud colors.
60  * \author Radu B. Rusu
61  * \ingroup visualization
62  */
63  template <typename PointT>
65  {
66  public:
68  typedef typename PointCloud::Ptr PointCloudPtr;
70 
71  typedef boost::shared_ptr<PointCloudColorHandler<PointT> > Ptr;
72  typedef boost::shared_ptr<const PointCloudColorHandler<PointT> > ConstPtr;
73 
74  /** \brief Constructor. */
76  cloud_ (), capable_ (false), field_idx_ (-1), fields_ ()
77  {}
78 
79  /** \brief Constructor. */
81  cloud_ (cloud), capable_ (false), field_idx_ (-1), fields_ ()
82  {}
83 
84  /** \brief Destructor. */
86 
87  /** \brief Check if this handler is capable of handling the input data or not. */
88  inline bool
89  isCapable () const { return (capable_); }
90 
91  /** \brief Abstract getName method. */
92  virtual std::string
93  getName () const = 0;
94 
95  /** \brief Abstract getFieldName method. */
96  virtual std::string
97  getFieldName () const = 0;
98 
99  /** \brief Obtain the actual color for the input dataset as vtk scalars.
100  * \param[out] scalars the output scalars containing the color for the dataset
101  * \return true if the operation was successful (the handler is capable and
102  * the input cloud was given as a valid pointer), false otherwise
103  */
104  virtual bool
105  getColor (vtkSmartPointer<vtkDataArray> &scalars) const = 0;
106 
107  /** \brief Set the input cloud to be used.
108  * \param[in] cloud the input cloud to be used by the handler
109  */
110  virtual void
112  {
113  cloud_ = cloud;
114  }
115 
116  protected:
117  /** \brief A pointer to the input dataset. */
119 
120  /** \brief True if this handler is capable of handling the input data, false
121  * otherwise.
122  */
123  bool capable_;
124 
125  /** \brief The index of the field holding the data that represents the color. */
127 
128  /** \brief The list of fields available for this PointCloud. */
129  std::vector<pcl::PCLPointField> fields_;
130  };
131 
132  //////////////////////////////////////////////////////////////////////////////////////
133  /** \brief Handler for random PointCloud colors (i.e., R, G, B will be randomly chosen)
134  * \author Radu B. Rusu
135  * \ingroup visualization
136  */
137  template <typename PointT>
139  {
141  typedef typename PointCloud::Ptr PointCloudPtr;
142  typedef typename PointCloud::ConstPtr PointCloudConstPtr;
143 
144  public:
145  typedef boost::shared_ptr<PointCloudColorHandlerRandom<PointT> > Ptr;
146  typedef boost::shared_ptr<const PointCloudColorHandlerRandom<PointT> > ConstPtr;
147 
148  /** \brief Constructor. */
151  {
152  capable_ = true;
153  }
154 
155  /** \brief Constructor. */
158  {
159  capable_ = true;
160  }
161 
162  /** \brief Abstract getName method. */
163  virtual std::string
164  getName () const { return ("PointCloudColorHandlerRandom"); }
165 
166  /** \brief Get the name of the field used. */
167  virtual std::string
168  getFieldName () const { return ("[random]"); }
169 
170  /** \brief Obtain the actual color for the input dataset as vtk scalars.
171  * \param[out] scalars the output scalars containing the color for the dataset
172  * \return true if the operation was successful (the handler is capable and
173  * the input cloud was given as a valid pointer), false otherwise
174  */
175  virtual bool
176  getColor (vtkSmartPointer<vtkDataArray> &scalars) const;
177 
178  protected:
179  // Members derived from the base class
182  };
183 
184  //////////////////////////////////////////////////////////////////////////////////////
185  /** \brief Handler for predefined user colors. The color at each point will be drawn
186  * as the use given R, G, B values.
187  * \author Radu B. Rusu
188  * \ingroup visualization
189  */
190  template <typename PointT>
192  {
194  typedef typename PointCloud::Ptr PointCloudPtr;
195  typedef typename PointCloud::ConstPtr PointCloudConstPtr;
196 
197  public:
198  typedef boost::shared_ptr<PointCloudColorHandlerCustom<PointT> > Ptr;
199  typedef boost::shared_ptr<const PointCloudColorHandlerCustom<PointT> > ConstPtr;
200 
201  /** \brief Constructor. */
202  PointCloudColorHandlerCustom (double r, double g, double b)
204  , r_ (r)
205  , g_ (g)
206  , b_ (b)
207  {
208  capable_ = true;
209  }
210 
211  /** \brief Constructor. */
213  double r, double g, double b)
214  : PointCloudColorHandler<PointT> (cloud)
215  , r_ (r)
216  , g_ (g)
217  , b_ (b)
218  {
219  capable_ = true;
220  }
221 
222  /** \brief Destructor. */
224 
225  /** \brief Abstract getName method. */
226  virtual std::string
227  getName () const { return ("PointCloudColorHandlerCustom"); }
228 
229  /** \brief Get the name of the field used. */
230  virtual std::string
231  getFieldName () const { return (""); }
232 
233  /** \brief Obtain the actual color for the input dataset as vtk scalars.
234  * \param[out] scalars the output scalars containing the color for the dataset
235  * \return true if the operation was successful (the handler is capable and
236  * the input cloud was given as a valid pointer), false otherwise
237  */
238  virtual bool
239  getColor (vtkSmartPointer<vtkDataArray> &scalars) const;
240 
241  protected:
242  // Members derived from the base class
245 
246  /** \brief Internal R, G, B holding the values given by the user. */
247  double r_, g_, b_;
248  };
249 
250  //////////////////////////////////////////////////////////////////////////////////////
251  /** \brief RGB handler class for colors. Uses the data present in the "rgb" or "rgba"
252  * fields as the color at each point.
253  * \author Radu B. Rusu
254  * \ingroup visualization
255  */
256  template <typename PointT>
258  {
260  typedef typename PointCloud::Ptr PointCloudPtr;
261  typedef typename PointCloud::ConstPtr PointCloudConstPtr;
262 
263  public:
264  typedef boost::shared_ptr<PointCloudColorHandlerRGBField<PointT> > Ptr;
265  typedef boost::shared_ptr<const PointCloudColorHandlerRGBField<PointT> > ConstPtr;
266 
267  /** \brief Constructor. */
269  {
270  capable_ = false;
271  }
272 
273  /** \brief Constructor. */
275  : PointCloudColorHandler<PointT> (cloud)
276  {
277  setInputCloud (cloud);
278  }
279 
280  /** \brief Destructor. */
282 
283  /** \brief Get the name of the field used. */
284  virtual std::string
285  getFieldName () const { return ("rgb"); }
286 
287  /** \brief Obtain the actual color for the input dataset as vtk scalars.
288  * \param[out] scalars the output scalars containing the color for the dataset
289  * \return true if the operation was successful (the handler is capable and
290  * the input cloud was given as a valid pointer), false otherwise
291  */
292  virtual bool
293  getColor (vtkSmartPointer<vtkDataArray> &scalars) const;
294 
295  /** \brief Set the input cloud to be used.
296  * \param[in] cloud the input cloud to be used by the handler
297  */
298  virtual void
299  setInputCloud (const PointCloudConstPtr &cloud);
300 
301  protected:
302  /** \brief Class getName method. */
303  virtual std::string
304  getName () const { return ("PointCloudColorHandlerRGBField"); }
305 
306  private:
307  // Members derived from the base class
312  };
313 
314  //////////////////////////////////////////////////////////////////////////////////////
315  /** \brief HSV handler class for colors. Uses the data present in the "h", "s", "v"
316  * fields as the color at each point.
317  * \ingroup visualization
318  */
319  template <typename PointT>
321  {
323  typedef typename PointCloud::Ptr PointCloudPtr;
324  typedef typename PointCloud::ConstPtr PointCloudConstPtr;
325 
326  public:
327  typedef boost::shared_ptr<PointCloudColorHandlerHSVField<PointT> > Ptr;
328  typedef boost::shared_ptr<const PointCloudColorHandlerHSVField<PointT> > ConstPtr;
329 
330  /** \brief Constructor. */
332 
333  /** \brief Empty destructor */
335 
336  /** \brief Get the name of the field used. */
337  virtual std::string
338  getFieldName () const { return ("hsv"); }
339 
340  /** \brief Obtain the actual color for the input dataset as vtk scalars.
341  * \param[out] scalars the output scalars containing the color for the dataset
342  * \return true if the operation was successful (the handler is capable and
343  * the input cloud was given as a valid pointer), false otherwise
344  */
345  virtual bool
346  getColor (vtkSmartPointer<vtkDataArray> &scalars) const;
347 
348  protected:
349  /** \brief Class getName method. */
350  virtual std::string
351  getName () const { return ("PointCloudColorHandlerHSVField"); }
352 
353  /** \brief The field index for "S". */
355 
356  /** \brief The field index for "V". */
358  private:
359  // Members derived from the base class
364  };
365 
366  //////////////////////////////////////////////////////////////////////////////////////
367  /** \brief Generic field handler class for colors. Uses an user given field to extract
368  * 1D data and display the color at each point using a min-max lookup table.
369  * \author Radu B. Rusu
370  * \ingroup visualization
371  */
372  template <typename PointT>
374  {
376  typedef typename PointCloud::Ptr PointCloudPtr;
377  typedef typename PointCloud::ConstPtr PointCloudConstPtr;
378 
379  public:
380  typedef boost::shared_ptr<PointCloudColorHandlerGenericField<PointT> > Ptr;
381  typedef boost::shared_ptr<const PointCloudColorHandlerGenericField<PointT> > ConstPtr;
382 
383  /** \brief Constructor. */
384  PointCloudColorHandlerGenericField (const std::string &field_name)
385  : field_name_ (field_name)
386  {
387  capable_ = false;
388  }
389 
390  /** \brief Constructor. */
392  const std::string &field_name)
393  : PointCloudColorHandler<PointT> (cloud)
394  , field_name_ (field_name)
395  {
396  setInputCloud (cloud);
397  }
398 
399  /** \brief Destructor. */
401 
402  /** \brief Get the name of the field used. */
403  virtual std::string getFieldName () const { return (field_name_); }
404 
405  /** \brief Obtain the actual color for the input dataset as vtk scalars.
406  * \param[out] scalars the output scalars containing the color for the dataset
407  * \return true if the operation was successful (the handler is capable and
408  * the input cloud was given as a valid pointer), false otherwise
409  */
410  virtual bool
411  getColor (vtkSmartPointer<vtkDataArray> &scalars) const;
412 
413  /** \brief Set the input cloud to be used.
414  * \param[in] cloud the input cloud to be used by the handler
415  */
416  virtual void
417  setInputCloud (const PointCloudConstPtr &cloud);
418 
419  protected:
420  /** \brief Class getName method. */
421  virtual std::string
422  getName () const { return ("PointCloudColorHandlerGenericField"); }
423 
424  private:
429 
430  /** \brief Name of the field used to create the color handler. */
431  std::string field_name_;
432  };
433 
434  //////////////////////////////////////////////////////////////////////////////////////
435  /** \brief Base Handler class for PointCloud colors.
436  * \author Radu B. Rusu
437  * \ingroup visualization
438  */
439  template <>
440  class PCL_EXPORTS PointCloudColorHandler<pcl::PCLPointCloud2>
441  {
442  public:
446 
447  typedef boost::shared_ptr<PointCloudColorHandler<PointCloud> > Ptr;
448  typedef boost::shared_ptr<const PointCloudColorHandler<PointCloud> > ConstPtr;
449 
450  /** \brief Constructor. */
452  cloud_ (cloud), capable_ (false), field_idx_ ()
453  {}
454 
455  /** \brief Destructor. */
457 
458  /** \brief Return whether this handler is capable of handling the input data or not. */
459  inline bool
460  isCapable () const { return (capable_); }
461 
462  /** \brief Abstract getName method. */
463  virtual std::string
464  getName () const = 0;
465 
466  /** \brief Abstract getFieldName method. */
467  virtual std::string
468  getFieldName () const = 0;
469 
470  /** \brief Obtain the actual color for the input dataset as vtk scalars.
471  * \param[out] scalars the output scalars containing the color for the dataset
472  * \return true if the operation was successful (the handler is capable and
473  * the input cloud was given as a valid pointer), false otherwise
474  */
475  virtual bool
476  getColor (vtkSmartPointer<vtkDataArray> &scalars) const = 0;
477 
478  /** \brief Set the input cloud to be used.
479  * \param[in] cloud the input cloud to be used by the handler
480  */
481  void
483  {
484  cloud_ = cloud;
485  }
486 
487  protected:
488  /** \brief A pointer to the input dataset. */
490 
491  /** \brief True if this handler is capable of handling the input data, false
492  * otherwise.
493  */
494  bool capable_;
495 
496  /** \brief The index of the field holding the data that represents the color. */
498  };
499 
500  //////////////////////////////////////////////////////////////////////////////////////
501  /** \brief Handler for random PointCloud colors (i.e., R, G, B will be randomly chosen)
502  * \author Radu B. Rusu
503  * \ingroup visualization
504  */
505  template <>
506  class PCL_EXPORTS PointCloudColorHandlerRandom<pcl::PCLPointCloud2> : public PointCloudColorHandler<pcl::PCLPointCloud2>
507  {
511 
512  public:
513  typedef boost::shared_ptr<PointCloudColorHandlerRandom<PointCloud> > Ptr;
514  typedef boost::shared_ptr<const PointCloudColorHandlerRandom<PointCloud> > ConstPtr;
515 
516  /** \brief Constructor. */
519  {
520  capable_ = true;
521  }
522 
523  /** \brief Empty destructor */
525 
526  /** \brief Get the name of the class. */
527  virtual std::string
528  getName () const { return ("PointCloudColorHandlerRandom"); }
529 
530  /** \brief Get the name of the field used. */
531  virtual std::string
532  getFieldName () const { return ("[random]"); }
533 
534  /** \brief Obtain the actual color for the input dataset as vtk scalars.
535  * \param[out] scalars the output scalars containing the color for the dataset
536  * \return true if the operation was successful (the handler is capable and
537  * the input cloud was given as a valid pointer), false otherwise
538  */
539  virtual bool
540  getColor (vtkSmartPointer<vtkDataArray> &scalars) const;
541  };
542 
543  //////////////////////////////////////////////////////////////////////////////////////
544  /** \brief Handler for predefined user colors. The color at each point will be drawn
545  * as the use given R, G, B values.
546  * \author Radu B. Rusu
547  * \ingroup visualization
548  */
549  template <>
550  class PCL_EXPORTS PointCloudColorHandlerCustom<pcl::PCLPointCloud2> : public PointCloudColorHandler<pcl::PCLPointCloud2>
551  {
555 
556  public:
557  /** \brief Constructor. */
559  double r, double g, double b) :
561  r_ (r), g_ (g), b_ (b)
562  {
563  capable_ = true;
564  }
565 
566  /** \brief Empty destructor */
568 
569  /** \brief Get the name of the class. */
570  virtual std::string
571  getName () const { return ("PointCloudColorHandlerCustom"); }
572 
573  /** \brief Get the name of the field used. */
574  virtual std::string
575  getFieldName () const { return (""); }
576 
577  /** \brief Obtain the actual color for the input dataset as vtk scalars.
578  * \param[out] scalars the output scalars containing the color for the dataset
579  * \return true if the operation was successful (the handler is capable and
580  * the input cloud was given as a valid pointer), false otherwise
581  */
582  virtual bool
583  getColor (vtkSmartPointer<vtkDataArray> &scalars) const;
584 
585  protected:
586  /** \brief Internal R, G, B holding the values given by the user. */
587  double r_, g_, b_;
588  };
589 
590  //////////////////////////////////////////////////////////////////////////////////////
591  /** \brief RGB handler class for colors. Uses the data present in the "rgb" or "rgba"
592  * fields as the color at each point.
593  * \author Radu B. Rusu
594  * \ingroup visualization
595  */
596  template <>
597  class PCL_EXPORTS PointCloudColorHandlerRGBField<pcl::PCLPointCloud2> : public PointCloudColorHandler<pcl::PCLPointCloud2>
598  {
602 
603  public:
604  typedef boost::shared_ptr<PointCloudColorHandlerRGBField<PointCloud> > Ptr;
605  typedef boost::shared_ptr<const PointCloudColorHandlerRGBField<PointCloud> > ConstPtr;
606 
607  /** \brief Constructor. */
609 
610  /** \brief Empty destructor */
612 
613  /** \brief Obtain the actual color for the input dataset as vtk scalars.
614  * \param[out] scalars the output scalars containing the color for the dataset
615  * \return true if the operation was successful (the handler is capable and
616  * the input cloud was given as a valid pointer), false otherwise
617  */
618  virtual bool
619  getColor (vtkSmartPointer<vtkDataArray> &scalars) const;
620 
621  protected:
622  /** \brief Get the name of the class. */
623  virtual std::string
624  getName () const { return ("PointCloudColorHandlerRGBField"); }
625 
626  /** \brief Get the name of the field used. */
627  virtual std::string
628  getFieldName () const { return ("rgb"); }
629  };
630 
631  //////////////////////////////////////////////////////////////////////////////////////
632  /** \brief HSV handler class for colors. Uses the data present in the "h", "s", "v"
633  * fields as the color at each point.
634  * \ingroup visualization
635  */
636  template <>
637  class PCL_EXPORTS PointCloudColorHandlerHSVField<pcl::PCLPointCloud2> : public PointCloudColorHandler<pcl::PCLPointCloud2>
638  {
642 
643  public:
644  typedef boost::shared_ptr<PointCloudColorHandlerHSVField<PointCloud> > Ptr;
645  typedef boost::shared_ptr<const PointCloudColorHandlerHSVField<PointCloud> > ConstPtr;
646 
647  /** \brief Constructor. */
649 
650  /** \brief Empty destructor */
652 
653  /** \brief Obtain the actual color for the input dataset as vtk scalars.
654  * \param[out] scalars the output scalars containing the color for the dataset
655  * \return true if the operation was successful (the handler is capable and
656  * the input cloud was given as a valid pointer), false otherwise
657  */
658  virtual bool
659  getColor (vtkSmartPointer<vtkDataArray> &scalars) const;
660 
661  protected:
662  /** \brief Get the name of the class. */
663  virtual std::string
664  getName () const { return ("PointCloudColorHandlerHSVField"); }
665 
666  /** \brief Get the name of the field used. */
667  virtual std::string
668  getFieldName () const { return ("hsv"); }
669 
670  /** \brief The field index for "S". */
672 
673  /** \brief The field index for "V". */
675  };
676 
677  //////////////////////////////////////////////////////////////////////////////////////
678  /** \brief Generic field handler class for colors. Uses an user given field to extract
679  * 1D data and display the color at each point using a min-max lookup table.
680  * \author Radu B. Rusu
681  * \ingroup visualization
682  */
683  template <>
684  class PCL_EXPORTS PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> : public PointCloudColorHandler<pcl::PCLPointCloud2>
685  {
689 
690  public:
691  typedef boost::shared_ptr<PointCloudColorHandlerGenericField<PointCloud> > Ptr;
692  typedef boost::shared_ptr<const PointCloudColorHandlerGenericField<PointCloud> > ConstPtr;
693 
694  /** \brief Constructor. */
696  const std::string &field_name);
697 
698  /** \brief Empty destructor */
700 
701  /** \brief Obtain the actual color for the input dataset as vtk scalars.
702  * \param[out] scalars the output scalars containing the color for the dataset
703  * \return true if the operation was successful (the handler is capable and
704  * the input cloud was given as a valid pointer), false otherwise
705  */
706  virtual bool
707  getColor (vtkSmartPointer<vtkDataArray> &scalars) const;
708 
709  protected:
710  /** \brief Get the name of the class. */
711  virtual std::string
712  getName () const { return ("PointCloudColorHandlerGenericField"); }
713 
714  /** \brief Get the name of the field used. */
715  virtual std::string
716  getFieldName () const { return (field_name_); }
717 
718  private:
719  /** \brief Name of the field used to create the color handler. */
720  std::string field_name_;
721  };
722  }
723 }
724 
725 #include <pcl/visualization/impl/point_cloud_color_handlers.hpp>
726 
727 #endif // PCL_POINT_CLOUD_COLOR_HANDLERS_H_
728