ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
PclPointCloudColorHandlerLUT.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #ifdef _MSC_VER
11 #pragma warning(disable : 4996) // Use of [[deprecated]] feature
12 #endif
13 
14 #include <pcl/point_cloud.h>
15 #include <pcl/point_types.h>
16 #include <pcl/visualization/point_cloud_color_handlers.h>
17 #include <vtkDataArray.h>
18 #include <vtkSmartPointer.h>
19 #include <vtkUnsignedCharArray.h>
20 
21 #include "PclCloudLUT.h"
22 
23 using pcl::visualization::PointCloudColorHandler;
24 
25 template <typename PointT>
26 class PclPointCloudColorHandlerLUT : public PointCloudColorHandler<PointT> {
27 public:
28  typedef pcl::PointCloud<PointT> PointCloud;
29  typedef typename PointCloud::Ptr PointCloudPtr;
30  typedef typename PointCloud::ConstPtr PointCloudConstPtr;
31 
32  typedef std::shared_ptr<PclPointCloudColorHandlerLUT<PointT>> Ptr;
33  typedef std::shared_ptr<const PclPointCloudColorHandlerLUT<PointT>>
35 
37  PclPointCloudColorHandlerLUT() : PointCloudColorHandler<PointT>() {
38  capable_ = false;
39  }
40 
43  : PointCloudColorHandler<PointT>(cloud) {
44  setInputCloud(cloud);
45  }
46 
49 
52  inline bool isCapable() const { return (capable_); }
53 
55  virtual std::string getName() const { return ""; };
56 
58  virtual std::string getFieldName() const { return ""; };
59 
66  virtual bool getColor(vtkSmartPointer<vtkDataArray>& scalars) const {
67  if (!capable_ || !cloud_) return (false);
68 
69  if (!scalars) scalars = vtkSmartPointer<vtkUnsignedCharArray>::New();
70  scalars->SetNumberOfComponents(3);
71 
72  vtkIdType nr_points = cloud_->points.size();
73  reinterpret_cast<vtkUnsignedCharArray*>(&(*scalars))
74  ->SetNumberOfTuples(nr_points);
75  unsigned char* colors =
76  reinterpret_cast<vtkUnsignedCharArray*>(&(*scalars))
77  ->GetPointer(0);
78 
79  int j = 0;
80  for (vtkIdType cp = 0; cp < nr_points; ++cp) {
81  if (pcl::isFinite(cloud_->points[cp])) {
82  // const pcl::RGB& color = PclCloudLUT::at (label[cp] %
83  // PclCloudLUT::size ());
84  const pcl::RGB& color = PclCloudLUT::at(label[cp]);
85  colors[j] = color.r;
86  colors[j + 1] = color.g;
87  colors[j + 2] = color.b;
88  j += 3;
89  }
90  }
91 
92  return (true);
93  };
94 
96  if (!capable_ || !cloud_) return nullptr;
97 
100  scalars->SetNumberOfComponents(3);
101 
102  vtkIdType nr_points = cloud_->points.size();
103  reinterpret_cast<vtkUnsignedCharArray*>(&(*scalars))
104  ->SetNumberOfTuples(nr_points);
105  unsigned char* colors =
106  reinterpret_cast<vtkUnsignedCharArray*>(&(*scalars))
107  ->GetPointer(0);
108 
109  int j = 0;
110  for (vtkIdType cp = 0; cp < nr_points; ++cp) {
111  if (pcl::isFinite(cloud_->points[cp])) {
112  // const pcl::RGB& color = PclCloudLUT::at (label[cp] %
113  // PclCloudLUT::size ());
114  const pcl::RGB& color = PclCloudLUT::at(label[cp]);
115  colors[j] = color.r;
116  colors[j + 1] = color.g;
117  colors[j + 2] = color.b;
118  j += 3;
119  }
120  }
121  return scalars;
122  }
123 
127  virtual void setInputCloud(const PointCloudConstPtr& cloud) {
128  cloud_ = cloud;
129  }
130 
131  void setLabel(int* value) {
132  label = value;
133  capable_ = true;
134  };
135 
136 private:
140  int* label;
141 
142  // Members derived from the base class
143  using PointCloudColorHandler<PointT>::cloud_;
144  using PointCloudColorHandler<PointT>::capable_;
145  using PointCloudColorHandler<PointT>::field_idx_;
146  using PointCloudColorHandler<PointT>::fields_;
147 };
math::float4 color
pcl::PointXYZ PointT
Definition: PCLCloud.h:16
static pcl::RGB at(int color_id)
Definition: PclCloudLUT.cpp:24
virtual std::string getName() const
Abstract getName method.
virtual vtkSmartPointer< vtkDataArray > getColor() const
PclPointCloudColorHandlerLUT(const PointCloudConstPtr &cloud)
Constructor.
std::shared_ptr< PclPointCloudColorHandlerLUT< PointT > > Ptr
virtual bool getColor(vtkSmartPointer< vtkDataArray > &scalars) const
Obtain the actual color for the input dataset as vtk scalars.
virtual ~PclPointCloudColorHandlerLUT()
Destructor.
bool isCapable() const
Check if this handler is capable of handling the input data or not.
std::shared_ptr< const PclPointCloudColorHandlerLUT< PointT > > ConstPtr
virtual std::string getFieldName() const
Abstract getFieldName method.
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Set the input cloud to be used.
double colors[3]