44 #include <pcl/common/colors.h>
45 #include <pcl/common/io.h>
46 #include <pcl/common/point_tests.h>
52 namespace visualization
58 if (!capable_ || !cloud_)
62 scalars->SetNumberOfComponents (3);
64 vtkIdType nr_points = cloud_->size ();
65 scalars->SetNumberOfTuples (nr_points);
68 unsigned char* colors =
new unsigned char[nr_points * 3];
71 for (vtkIdType cp = 0; cp < nr_points; ++cp)
73 colors[cp * 3 + 0] =
static_cast<unsigned char> (r_);
74 colors[cp * 3 + 1] =
static_cast<unsigned char> (g_);
75 colors[cp * 3 + 2] =
static_cast<unsigned char> (b_);
77 scalars->SetArray (colors, 3 * nr_points, 0, vtkUnsignedCharArray::VTK_DATA_ARRAY_DELETE);
85 if (!capable_ || !cloud_)
89 scalars->SetNumberOfComponents (3);
91 vtkIdType nr_points = cloud_->size ();
92 scalars->SetNumberOfTuples (nr_points);
95 unsigned char* colors =
new unsigned char[nr_points * 3];
99 int r_ =
static_cast<int> (
pcl_lrint (r * 255.0)),
100 g_ =
static_cast<int> (
pcl_lrint (g * 255.0)),
101 b_ =
static_cast<int> (
pcl_lrint (b * 255.0));
104 for (vtkIdType cp = 0; cp < nr_points; ++cp)
106 colors[cp * 3 + 0] =
static_cast<unsigned char> (r_);
107 colors[cp * 3 + 1] =
static_cast<unsigned char> (g_);
108 colors[cp * 3 + 2] =
static_cast<unsigned char> (b_);
110 scalars->SetArray (colors, 3 * nr_points, 0, vtkUnsignedCharArray::VTK_DATA_ARRAY_DELETE);
115 template <
typename Po
intT>
void
121 field_idx_ = pcl::getFieldIndex<PointT> (
"rgb", fields_);
122 if (field_idx_ != -1)
129 field_idx_ = pcl::getFieldIndex<PointT> (
"rgba", fields_);
130 if (field_idx_ != -1)
141 if (!capable_ || !cloud_)
145 std::vector<pcl::PCLPointField> fields;
147 rgba_index = pcl::getFieldIndex<PointT> (
"rgb", fields);
148 if (rgba_index == -1)
149 rgba_index = pcl::getFieldIndex<PointT> (
"rgba", fields);
151 int rgba_offset = fields[rgba_index].offset;
154 scalars->SetNumberOfComponents (3);
156 vtkIdType nr_points = cloud_->size ();
157 scalars->SetNumberOfTuples (nr_points);
158 unsigned char* colors = scalars->GetPointer (0);
162 for (std::size_t d = 0; d < fields_.size (); ++d)
163 if (fields_[d].name ==
"x")
164 x_idx =
static_cast<int> (d);
171 for (vtkIdType cp = 0; cp < nr_points; ++cp)
174 if (!std::isfinite ((*cloud_)[cp].x) ||
175 !std::isfinite ((*cloud_)[cp].y) ||
176 !std::isfinite ((*cloud_)[cp].z))
178 memcpy (&rgb, (
reinterpret_cast<const char *
> (&(*cloud_)[cp])) + rgba_offset,
sizeof (
pcl::RGB));
180 colors[j + 1] = rgb.g;
181 colors[j + 2] = rgb.b;
188 for (vtkIdType cp = 0; cp < nr_points; ++cp)
190 int idx =
static_cast<int> (cp) * 3;
191 memcpy (&rgb, (
reinterpret_cast<const char *
> (&(*cloud_)[cp])) + rgba_offset,
sizeof (
pcl::RGB));
192 colors[idx ] = rgb.r;
193 colors[idx + 1] = rgb.g;
194 colors[idx + 2] = rgb.b;
201 template <
typename Po
intT>
235 if (!capable_ || !cloud_)
239 scalars->SetNumberOfComponents (3);
241 vtkIdType nr_points = cloud_->size ();
242 scalars->SetNumberOfTuples (nr_points);
243 unsigned char* colors = scalars->GetPointer (0);
249 for (std::size_t d = 0; d < fields_.size (); ++d)
250 if (fields_[d].name ==
"x")
251 x_idx =
static_cast<int> (d);
256 for (vtkIdType cp = 0; cp < nr_points; ++cp)
259 if (!std::isfinite ((*cloud_)[cp].x) ||
260 !std::isfinite ((*cloud_)[cp].y) ||
261 !std::isfinite ((*cloud_)[cp].z))
266 float h = (*cloud_)[cp].h;
267 float v = (*cloud_)[cp].v;
268 float s = (*cloud_)[cp].s;
272 h = h < 0.0f ? h - (((int)h)/360 - 1)*360 : h - (((
int)h)/360)*360;
275 if (s > 1.0f) s = 1.0f;
276 if (s < 0.0f) s = 0.0f;
277 if (v > 1.0f) v = 1.0f;
278 if (v < 0.0f) v = 0.0f;
282 colors[idx] = colors[idx+1] = colors[idx+2] = v*255;
288 int i = std::floor (a);
290 float p = v * (1 - s);
291 float q = v * (1 - s * f);
292 float t = v * (1 - s * (1 - f));
297 colors[idx] = v*255; colors[idx+1] = t*255; colors[idx+2] = p*255;
break;
299 colors[idx] = q*255; colors[idx+1] = v*255; colors[idx+2] = p*255;
break;
301 colors[idx] = p*255; colors[idx+1] = v*255; colors[idx+2] = t*255;
break;
303 colors[idx] = p*255; colors[idx+1] = q*255; colors[idx+2] = v*255;
break;
305 colors[idx] = t*255; colors[idx+1] = p*255; colors[idx+2] = v*255;
break;
307 colors[idx] = v*255; colors[idx+1] = p*255; colors[idx+2] = q*255;
break;
316 for (vtkIdType cp = 0; cp < nr_points; ++cp)
318 float h = (*cloud_)[cp].h;
319 float v = (*cloud_)[cp].v;
320 float s = (*cloud_)[cp].s;
324 h = h < 0.0f ? h - (((int)h)/360 - 1)*360 : h - (((
int)h)/360)*360;
327 if (s > 1.0f) s = 1.0f;
328 if (s < 0.0f) s = 0.0f;
329 if (v > 1.0f) v = 1.0f;
330 if (v < 0.0f) v = 0.0f;
334 colors[idx] = colors[idx+1] = colors[idx+2] = v*255;
340 int i = std::floor (a);
342 float p = v * (1 - s);
343 float q = v * (1 - s * f);
344 float t = v * (1 - s * (1 - f));
349 colors[idx] = v*255; colors[idx+1] = t*255; colors[idx+2] = p*255;
break;
351 colors[idx] = q*255; colors[idx+1] = v*255; colors[idx+2] = p*255;
break;
353 colors[idx] = p*255; colors[idx+1] = v*255; colors[idx+2] = t*255;
break;
355 colors[idx] = p*255; colors[idx+1] = q*255; colors[idx+2] = v*255;
break;
357 colors[idx] = t*255; colors[idx+1] = p*255; colors[idx+2] = v*255;
break;
359 colors[idx] = v*255; colors[idx+1] = p*255; colors[idx+2] = q*255;
break;
369 template <
typename Po
intT>
void
374 field_idx_ = pcl::getFieldIndex<PointT> (field_name_, fields_);
375 if (field_idx_ != -1)
385 if (!capable_ || !cloud_)
389 scalars->SetNumberOfComponents (1);
391 vtkIdType nr_points = cloud_->size ();
392 scalars->SetNumberOfTuples (nr_points);
394 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
396 float* colors =
new float[nr_points];
402 for (std::size_t d = 0; d < fields_.size (); ++d)
403 if (fields_[d].name ==
"x")
404 x_idx =
static_cast<int> (d);
409 for (vtkIdType cp = 0; cp < nr_points; ++cp)
412 if (!std::isfinite ((*cloud_)[cp].x) || !std::isfinite ((*cloud_)[cp].y) || !std::isfinite ((*cloud_)[cp].z))
415 const std::uint8_t* pt_data =
reinterpret_cast<const std::uint8_t*
> (&(*cloud_)[cp]);
416 memcpy (&field_data, pt_data + fields_[field_idx_].offset,
pcl::getFieldSize (fields_[field_idx_].datatype));
418 colors[j] = field_data;
425 for (vtkIdType cp = 0; cp < nr_points; ++cp)
427 const std::uint8_t* pt_data =
reinterpret_cast<const std::uint8_t*
> (&(*cloud_)[cp]);
428 memcpy (&field_data, pt_data + fields_[field_idx_].offset,
pcl::getFieldSize (fields_[field_idx_].datatype));
430 if (!std::isfinite (field_data))
433 colors[j] = field_data;
437 scalars->SetArray (colors, j, 0, vtkFloatArray::VTK_DATA_ARRAY_DELETE);
442 template <
typename Po
intT>
void
448 field_idx_ = pcl::getFieldIndex<PointT> (
"rgba", fields_);
449 if (field_idx_ != -1)
459 if (!capable_ || !cloud_)
463 scalars->SetNumberOfComponents (4);
465 vtkIdType nr_points = cloud_->size ();
466 scalars->SetNumberOfTuples (nr_points);
467 unsigned char* colors = scalars->GetPointer (0);
471 for (std::size_t d = 0; d < fields_.size (); ++d)
472 if (fields_[d].name ==
"x")
473 x_idx =
static_cast<int> (d);
479 for (vtkIdType cp = 0; cp < nr_points; ++cp)
482 if (!std::isfinite ((*cloud_)[cp].x) ||
483 !std::isfinite ((*cloud_)[cp].y) ||
484 !std::isfinite ((*cloud_)[cp].z))
487 colors[j ] = (*cloud_)[cp].r;
488 colors[j + 1] = (*cloud_)[cp].g;
489 colors[j + 2] = (*cloud_)[cp].b;
490 colors[j + 3] = (*cloud_)[cp].a;
497 for (vtkIdType cp = 0; cp < nr_points; ++cp)
499 int idx =
static_cast<int> (cp) * 4;
500 colors[idx ] = (*cloud_)[cp].r;
501 colors[idx + 1] = (*cloud_)[cp].g;
502 colors[idx + 2] = (*cloud_)[cp].b;
503 colors[idx + 3] = (*cloud_)[cp].a;
510 template <
typename Po
intT>
void
514 field_idx_ = pcl::getFieldIndex<PointT> (
"label", fields_);
515 if (field_idx_ != -1)
526 if (!capable_ || !cloud_)
530 scalars->SetNumberOfComponents (3);
532 vtkIdType nr_points = cloud_->size ();
533 scalars->SetNumberOfTuples (nr_points);
534 unsigned char* colors = scalars->GetPointer (0);
537 std::map<std::uint32_t, pcl::RGB> colormap;
538 if (!static_mapping_)
540 std::set<std::uint32_t> labels;
542 for (vtkIdType i = 0; i < nr_points; ++i)
543 labels.insert ((*cloud_)[i].label);
546 std::size_t color = 0;
547 for (std::set<std::uint32_t>::iterator iter = labels.begin (); iter != labels.end (); ++iter, ++color)
552 for (vtkIdType cp = 0; cp < nr_points; ++cp)
557 colors[j ] = color.r;
558 colors[j + 1] = color.g;
559 colors[j + 2] = color.b;
static std::size_t size()
Get the number of colors in the lookup table.
static RGB at(std::size_t color_id)
Get a color from the lookup table with a given id.
vtkSmartPointer< vtkDataArray > getColor() const override
Obtain the actual color for the input dataset as a VTK data array.
vtkSmartPointer< vtkDataArray > getColor() const override
Obtain the actual color for the input dataset as a VTK data array.
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Set the input cloud to be used.
PointCloudColorHandlerHSVField(const PointCloudConstPtr &cloud)
Constructor.
vtkSmartPointer< vtkDataArray > getColor() const override
Obtain the actual color for the input dataset as a VTK data array.
int v_field_idx_
The field index for "V".
int s_field_idx_
The field index for "S".
Base Handler class for PointCloud colors.
bool capable_
True if this handler is capable of handling the input data, false otherwise.
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Set the input cloud to be used.
typename PointCloud::ConstPtr PointCloudConstPtr
std::vector< pcl::PCLPointField > fields_
The list of fields available for this PointCloud.
int field_idx_
The index of the field holding the data that represents the color.
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Set the input cloud to be used.
vtkSmartPointer< vtkDataArray > getColor() const override
Obtain the actual color for the input dataset as a VTK data array.
vtkSmartPointer< vtkDataArray > getColor() const override
Obtain the actual color for the input dataset as a VTK data array.
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Set the input cloud to be used.
vtkSmartPointer< vtkDataArray > getColor() const override
Obtain the actual color for the input dataset as a VTK data array.
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Set the input cloud to be used.
vtkSmartPointer< vtkDataArray > getColor() const override
Obtain the actual color for the input dataset as a VTK data array.
int getFieldSize(const int datatype)
Obtains the size of a specific field data type in bytes.
PCL_EXPORTS void getRandomColors(double &r, double &g, double &b, double min=0.2, double max=2.8)
Get (good) random values for R/G/B.
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Defines all the PCL and non-PCL macros used.
A point structure representing Euclidean xyz coordinates, and the RGB color.
A structure representing RGB color information.