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)
176 memcpy (&rgb, (
reinterpret_cast<const char *
> (&(*cloud_)[cp])) + rgba_offset,
sizeof (
pcl::RGB));
178 colors[j + 1] = rgb.g;
179 colors[j + 2] = rgb.b;
186 for (vtkIdType cp = 0; cp < nr_points; ++cp)
188 int idx =
static_cast<int> (cp) * 3;
189 memcpy (&rgb, (
reinterpret_cast<const char *
> (&(*cloud_)[cp])) + rgba_offset,
sizeof (
pcl::RGB));
190 colors[idx ] = rgb.r;
191 colors[idx + 1] = rgb.g;
192 colors[idx + 2] = rgb.b;
199 template <
typename Po
intT>
233 if (!capable_ || !cloud_)
237 scalars->SetNumberOfComponents (3);
239 vtkIdType nr_points = cloud_->size ();
240 scalars->SetNumberOfTuples (nr_points);
241 unsigned char* colors = scalars->GetPointer (0);
247 for (std::size_t d = 0; d < fields_.size (); ++d)
248 if (fields_[d].name ==
"x")
249 x_idx =
static_cast<int> (d);
254 for (vtkIdType cp = 0; cp < nr_points; ++cp)
262 float h = (*cloud_)[cp].h;
263 float v = (*cloud_)[cp].v;
264 float s = (*cloud_)[cp].s;
268 h = h < 0.0f ? h - (((int)h)/360 - 1)*360 : h - (((
int)h)/360)*360;
271 if (s > 1.0f) s = 1.0f;
272 if (s < 0.0f) s = 0.0f;
273 if (v > 1.0f) v = 1.0f;
274 if (v < 0.0f) v = 0.0f;
278 colors[idx] = colors[idx+1] = colors[idx+2] = v*255;
284 int i = std::floor (a);
286 float p = v * (1 - s);
287 float q = v * (1 - s * f);
288 float t = v * (1 - s * (1 - f));
293 colors[idx] = v*255; colors[idx+1] = t*255; colors[idx+2] = p*255;
break;
295 colors[idx] = q*255; colors[idx+1] = v*255; colors[idx+2] = p*255;
break;
297 colors[idx] = p*255; colors[idx+1] = v*255; colors[idx+2] = t*255;
break;
299 colors[idx] = p*255; colors[idx+1] = q*255; colors[idx+2] = v*255;
break;
301 colors[idx] = t*255; colors[idx+1] = p*255; colors[idx+2] = v*255;
break;
303 colors[idx] = v*255; colors[idx+1] = p*255; colors[idx+2] = q*255;
break;
312 for (vtkIdType cp = 0; cp < nr_points; ++cp)
314 float h = (*cloud_)[cp].h;
315 float v = (*cloud_)[cp].v;
316 float s = (*cloud_)[cp].s;
320 h = h < 0.0f ? h - (((int)h)/360 - 1)*360 : h - (((
int)h)/360)*360;
323 if (s > 1.0f) s = 1.0f;
324 if (s < 0.0f) s = 0.0f;
325 if (v > 1.0f) v = 1.0f;
326 if (v < 0.0f) v = 0.0f;
330 colors[idx] = colors[idx+1] = colors[idx+2] = v*255;
336 int i = std::floor (a);
338 float p = v * (1 - s);
339 float q = v * (1 - s * f);
340 float t = v * (1 - s * (1 - f));
345 colors[idx] = v*255; colors[idx+1] = t*255; colors[idx+2] = p*255;
break;
347 colors[idx] = q*255; colors[idx+1] = v*255; colors[idx+2] = p*255;
break;
349 colors[idx] = p*255; colors[idx+1] = v*255; colors[idx+2] = t*255;
break;
351 colors[idx] = p*255; colors[idx+1] = q*255; colors[idx+2] = v*255;
break;
353 colors[idx] = t*255; colors[idx+1] = p*255; colors[idx+2] = v*255;
break;
355 colors[idx] = v*255; colors[idx+1] = p*255; colors[idx+2] = q*255;
break;
365 template <
typename Po
intT>
void
370 field_idx_ = pcl::getFieldIndex<PointT> (field_name_, fields_);
371 if (field_idx_ == -1) {
375 if (fields_[field_idx_].datatype != pcl::PCLPointField::PointFieldTypes::FLOAT32) {
377 PCL_ERROR(
"[pcl::PointCloudColorHandlerGenericField::setInputCloud] This currently only works with float32 fields, but field %s has a different type.\n", field_name_.c_str());
387 if (!capable_ || !cloud_)
391 scalars->SetNumberOfComponents (1);
393 vtkIdType nr_points = cloud_->size ();
394 scalars->SetNumberOfTuples (nr_points);
396 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
398 float* colors =
new float[nr_points];
404 for (std::size_t d = 0; d < fields_.size (); ++d)
405 if (fields_[d].name ==
"x")
406 x_idx =
static_cast<int> (d);
411 for (vtkIdType cp = 0; cp < nr_points; ++cp)
417 const std::uint8_t* pt_data =
reinterpret_cast<const std::uint8_t*
> (&(*cloud_)[cp]);
418 memcpy (&field_data, pt_data + fields_[field_idx_].offset,
pcl::getFieldSize (fields_[field_idx_].datatype));
420 colors[j] = field_data;
427 for (vtkIdType cp = 0; cp < nr_points; ++cp)
429 const std::uint8_t* pt_data =
reinterpret_cast<const std::uint8_t*
> (&(*cloud_)[cp]);
430 memcpy (&field_data, pt_data + fields_[field_idx_].offset,
pcl::getFieldSize (fields_[field_idx_].datatype));
432 if (!std::isfinite (field_data))
435 colors[j] = field_data;
439 scalars->SetArray (colors, j, 0, vtkFloatArray::VTK_DATA_ARRAY_DELETE);
444 template <
typename Po
intT>
void
450 field_idx_ = pcl::getFieldIndex<PointT> (
"rgba", fields_);
451 if (field_idx_ != -1)
461 if (!capable_ || !cloud_)
465 scalars->SetNumberOfComponents (4);
467 vtkIdType nr_points = cloud_->size ();
468 scalars->SetNumberOfTuples (nr_points);
469 unsigned char* colors = scalars->GetPointer (0);
473 for (std::size_t d = 0; d < fields_.size (); ++d)
474 if (fields_[d].name ==
"x")
475 x_idx =
static_cast<int> (d);
481 for (vtkIdType cp = 0; cp < nr_points; ++cp)
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...
constexpr bool isXYZFinite(const PointT &) noexcept
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.