PointCloud.h
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4
5#include <SimoxUtility/color/ColorMap.h>
6
8
9#include <RobotAPI/interface/ArViz/Elements.h>
10
11#include "ElementOps.h"
13
14namespace armarx::viz
15{
16
17 using data::ColoredPoint;
18
19 class PointCloud : public ElementOps<PointCloud, data::ElementPointCloud>
20 {
21 public:
23
24 // Settings
25
26 /**
27 * @brief Enable or disable checking whether points are finite when adding them
28 * (disabled by default).
29 *
30 * Call this function (with no argument or `true`) before adding points to enable checking.
31 *
32 * Non-finite points can break visualization (e.g. produce a white screen),
33 * so enable this if your point cloud may contain non-finite points.
34 *
35 * @see isfinite()
36 */
38 checkFinite(bool enabled = true)
39 {
40 this->_checkFinite = enabled;
41 return *this;
42 }
43
45 transparency(float t)
46 {
47 data_->transparency = t;
48
49 return *this;
50 }
51
54 {
55 data_->pointSizeInPixels = s;
56 return *this;
57 }
58
59 // Adding points
60
63 {
64 data_->points.clear();
65 return *this;
66 }
67
69 points(std::vector<ColoredPoint> const& ps)
70 {
71 std::size_t memorySize = ps.size() * sizeof(ps[0]);
72 const Ice::Byte* const begin = reinterpret_cast<const Ice::Byte*>(ps.data());
73 const Ice::Byte* const end = begin + memorySize;
74 data_->points.assign(begin, end);
75 return *this;
76 }
77
78 // Adding single points
79
81 addPoint(ColoredPoint const& p)
82 {
83 if (isfinite(p))
84 {
86 }
87
88 return *this;
89 }
90
92 addPointUnchecked(ColoredPoint const& p)
93 {
94 const Ice::Byte* const begin = reinterpret_cast<const Ice::Byte*>(&p);
95 const Ice::Byte* const end = begin + sizeof(p);
96 data_->points.insert(data_->points.end(), begin, end);
97 return *this;
98 }
99
101 addPoint(float x, float y, float z, const data::Color& color)
102 {
103 ColoredPoint p;
104 p.x = x;
105 p.y = y;
106 p.z = z;
107 p.color = color;
108 return addPoint(p);
109 }
110
112 addPoint(float x, float y, float z, const simox::Color& color)
113 {
114 return addPoint(x, y, z, Color{color});
115 }
116
117 template <typename ColorCoeff = int>
119 addPoint(float x,
120 float y,
121 float z,
122 ColorCoeff r,
123 ColorCoeff g,
124 ColorCoeff b,
125 ColorCoeff a = 255)
126 {
127 return addPoint(x, y, z, simox::Color(r, g, b, a));
128 }
129
131 addPoint(float x, float y, float z)
132 {
133 return addPoint(x, y, z, simox::Color::black(255));
134 }
135
137 addPoint(float x, float y, float z, std::size_t id, int alpha = 255)
138 {
139 return addPoint(x, y, z, simox::color::GlasbeyLUT::at(id, alpha));
140 }
141
142 // Templated setters for usage with PCL point types (`pcl::Point*`).
143
144 /**
145 * @brief Add a point in the given color.
146 */
147 template <class PointT>
150 {
151 return addPoint(p.x, p.y, p.z, color);
152 }
153
154 /**
155 * @brief Add a point with its "natural" color.
156 *
157 * If the point has `label`, the corresponding Glasbey color is used.
158 * If the point has `r, g, b, a` (but no `label`, its RGBA is used.
159 * Otherwise, its color will be grey.
160 *
161 * @param p Point with members `x, y, z`, optionally `label`, optionally `r, g, b, a`.
162 */
163 template <class PointT>
165 addPoint(const PointT& p)
166 {
167 if constexpr (detail::has_member_label<PointT>::value)
168 {
169 return addPoint(p.x, p.y, p.z, simox::color::GlasbeyLUT::at(p.label));
170 }
172 {
173 return addPoint(p.x, p.y, p.z, simox::Color(p.r, p.g, p.b, p.a));
174 }
175 else
176 {
177 return addPoint(p.x, p.y, p.z, Color::gray());
178 }
179 }
180
181 // Label and RGBA, offer additional flag to choose which
182 /**
183 * @brief Add a colored or labeled point.
184 * @param p Point with members `x, y, z, r, g, b, a, label`.
185 * @param colorByLabel Whether to color the point by label (true) or by its RGBA (false).
186 */
187 template <class PointT>
189 addPoint(const PointT& p, bool colorByLabel)
190 {
191 if (colorByLabel)
192 {
193 return addPoint(p.x, p.y, p.z, simox::color::GlasbeyLUT::at(p.label));
194 }
195 else
196 {
197 return addPoint(p.x, p.y, p.z, simox::Color(p.r, p.g, p.b, p.a));
198 }
199 }
200
201 // Setters for point clouds (for usage with `pcl::Point*`).
202
203 /// Draw a point cloud.
204 template <class PointCloudT>
207 {
208 return this->setPointCloud(cloud, [this](const auto& p) { this->addPoint(p); });
209 }
210
211 /// Draw a point cloud with given indices.
212 template <class PointCloudT>
214 pointCloud(const PointCloudT& cloud, const std::vector<int>& indices)
215 {
216 return this->setPointCloud(cloud, indices, [this](int, const auto& p) { addPoint(p); });
217 }
218
219 /// Draw a unicolored point cloud with given color.
220 template <class PointCloudT>
223 {
224 return this->setPointCloud(cloud,
225 [this, color](const auto& p) { this->addPoint(p, color); });
226 }
227
228 /// Draw a point cloud.
229 template <class PointCloudT>
231 pointCloud(const PointCloudT& cloud, bool colorByLabel)
232 {
233 return this->setPointCloud(
234 cloud, [this, colorByLabel](const auto& p) { this->addPoint(p, colorByLabel); });
235 }
236
237 /// Draw a point cloud with given indices.
238 template <class PointCloudT>
240 pointCloud(const PointCloudT& cloud, const std::vector<int>& indices, bool colorByLabel)
241 {
242 return this->setPointCloud(cloud,
243 indices,
244 [this, colorByLabel](int, const auto& p)
245 { addPoint(p, colorByLabel); });
246 }
247
248 /// Draw a unicolored point cloud with given color and indices.
249 template <class PointCloudT>
251 pointCloud(const PointCloudT& cloud, const std::vector<int>& indices, Color color)
252 {
253 return this->setPointCloud(
254 cloud, indices, [this, color](int, const auto& p) { addPoint(p, color); });
255 return *this;
256 }
257
258 /**
259 * @brief Draw a colored point cloud with custom colors.
260 *
261 * The color of a point is specified by `colorFunc`, which must be
262 * a callable taking an element of `pointCloud` and returning its
263 * color as `viz::Color` or `simox::Color`.
264 */
265 template <class PointCloudT, class ColorFuncT>
267 pointCloud(const PointCloudT& cloud, const ColorFuncT& colorFunc)
268 {
269 return this->setPointCloud(
270 cloud, [this, &colorFunc](const auto& p) { addPoint(p, colorFunc(p)); });
271 }
272
273 /**
274 * @brief Draw a colored point cloud with custom colors and given indices.
275 */
276 template <class PointCloudT, class ColorFuncT>
279 const std::vector<int>& indices,
280 const ColorFuncT& colorFunc)
281 {
282 return this->setPointCloud(cloud,
283 indices,
284 [this, &colorFunc](int, const auto& p)
285 { addPoint(p, colorFunc(p)); });
286 }
287
288 /**
289 * @brief Draw a colored point cloud with using a color map.
290 *
291 * The color of a point is specified by `colorMap` and `scalarFunc`.
292 * `scalarFunc` must be a callable taking an element of `pointCloud` and returning
293 * a scalar value which is passed to `colorMap` to retrieve the point's color.
294 */
295 template <class PointCloudT, class ScalarFuncT>
298 const simox::ColorMap& colorMap,
299 const ScalarFuncT& scalarFunc)
300 {
301 return this->pointCloud(pointCloud,
302 [&colorMap, scalarFunc](const auto& p)
303 { return colorMap(scalarFunc(p)); });
304 }
305
306 /**
307 * @brief Draw a colored point cloud with using a color map and given indices.
308 */
309 template <class PointCloudT, class ScalarFuncT>
312 const std::vector<int>& indices,
313 const simox::ColorMap& colorMap,
314 const ScalarFuncT& scalarFunc)
315 {
316 return this->pointCloud(pointCloud,
317 indices,
318 [colorMap, scalarFunc](const auto& p)
319 { return colorMap(scalarFunc(p)); });
320 }
321
322 // Setters taking processing functions and handling iteration.
323
324 /**
325 * @brief Set the point cloud from a `pcl::PointCloud`.
326 *
327 * @param cloud The point cloud.
328 * @param pointFunc A function processing each point, taking an element of `cloud`.
329 * @param checkFinite
330 * Enable or disable checking whether points are finite (enabled by default).
331 * Points containing infinity can break visualization (e.g. produce a white screen).
332 * @param clear Whether to clear the point cloud beforehand (true by default).
333 *
334 * @return `*this`
335 */
336 template <class PointCloudT, class PointFunc>
338 setPointCloud(const PointCloudT& cloud, const PointFunc& pointFunc, bool clear = true)
339 {
340 if (clear)
341 {
342 this->clear();
343 }
344 for (const auto& p : cloud)
345 {
346 pointFunc(p);
347 }
348 return *this;
349 }
350
351 /**
352 * @brief Set the point cloud from a `pcl::PointCloud`.
353 *
354 * @param cloud The source point cloud.
355 * @param indices The indices to add.
356 * @param pointFunc
357 * A function processing each point, taking an element of each `indices` and `cloud`.
358 * @param checkFinite
359 * Enable or disable checking whether points are finite (enabled by default).
360 * Points containing infinity can break visualization (e.g. produce a white screen).
361 * @param clear Whether to clear the point cloud beforehand (true by default).
362 *
363 * @return `*this`
364 */
365 template <class PointCloudT, class PointFunc>
368 const std::vector<int>& indices,
369 const PointFunc& pointFunc,
370 bool clear = true)
371 {
372 if (clear)
373 {
374 this->clear();
375 }
376 for (int i : indices)
377 {
378 ARMARX_CHECK_FITS_SIZE(i, cloud.size());
379 const auto& p = cloud.at(size_t(i));
380 pointFunc(i, p);
381 }
382 return *this;
383 }
384
385
386 private:
387 template <class PointT>
388 bool
389 isfinite(const PointT& p) const
390 {
391 return !_checkFinite ||
392 (std::isfinite(p.x) && std::isfinite(p.y) && std::isfinite(p.z));
393 }
394
395 /// Whether to check whether points are finite when adding them.
396 bool _checkFinite = false;
397 };
398
399} // namespace armarx::viz
IceInternal::Handle< data::ElementPointCloud > data_
Definition ElementOps.h:315
ElementOps(std::string const &id)
Definition ElementOps.h:119
PointCloud & setPointCloud(const PointCloudT &cloud, const PointFunc &pointFunc, bool clear=true)
Set the point cloud from a pcl::PointCloud.
Definition PointCloud.h:338
PointCloud & addPoint(float x, float y, float z)
Definition PointCloud.h:131
PointCloud & pointSizeInPixels(float s)
Definition PointCloud.h:53
PointCloud & pointCloud(const PointCloudT &cloud, Color color)
Draw a unicolored point cloud with given color.
Definition PointCloud.h:222
PointCloud & pointCloud(const PointCloudT &cloud)
Draw a point cloud.
Definition PointCloud.h:206
PointCloud & pointCloud(const PointCloudT &cloud, const std::vector< int > &indices)
Draw a point cloud with given indices.
Definition PointCloud.h:214
PointCloud & pointCloud(const PointCloudT &pointCloud, const std::vector< int > &indices, const simox::ColorMap &colorMap, const ScalarFuncT &scalarFunc)
Draw a colored point cloud with using a color map and given indices.
Definition PointCloud.h:311
PointCloud & addPoint(float x, float y, float z, const simox::Color &color)
Definition PointCloud.h:112
PointCloud & pointCloud(const PointCloudT &cloud, const std::vector< int > &indices, const ColorFuncT &colorFunc)
Draw a colored point cloud with custom colors and given indices.
Definition PointCloud.h:278
PointCloud & addPoint(float x, float y, float z, std::size_t id, int alpha=255)
Definition PointCloud.h:137
PointCloud & clear()
Definition PointCloud.h:62
PointCloud & addPoint(const PointT &p)
Add a point with its "natural" color.
Definition PointCloud.h:165
PointCloud & pointCloud(const PointCloudT &pointCloud, const simox::ColorMap &colorMap, const ScalarFuncT &scalarFunc)
Draw a colored point cloud with using a color map.
Definition PointCloud.h:297
PointCloud & addPoint(float x, float y, float z, ColorCoeff r, ColorCoeff g, ColorCoeff b, ColorCoeff a=255)
Definition PointCloud.h:119
PointCloud & pointCloud(const PointCloudT &cloud, bool colorByLabel)
Draw a point cloud.
Definition PointCloud.h:231
PointCloud & addPoint(ColoredPoint const &p)
Definition PointCloud.h:81
PointCloud & pointCloud(const PointCloudT &cloud, const std::vector< int > &indices, bool colorByLabel)
Draw a point cloud with given indices.
Definition PointCloud.h:240
ElementOps(std::string const &id)
Definition ElementOps.h:119
PointCloud & addPoint(const PointT &p, bool colorByLabel)
Add a colored or labeled point.
Definition PointCloud.h:189
PointCloud & setPointCloud(const PointCloudT &cloud, const std::vector< int > &indices, const PointFunc &pointFunc, bool clear=true)
Set the point cloud from a pcl::PointCloud.
Definition PointCloud.h:367
PointCloud & checkFinite(bool enabled=true)
Enable or disable checking whether points are finite when adding them (disabled by default).
Definition PointCloud.h:38
PointCloud & addPoint(float x, float y, float z, const data::Color &color)
Definition PointCloud.h:101
PointCloud & transparency(float t)
Definition PointCloud.h:45
PointCloud & pointCloud(const PointCloudT &cloud, const ColorFuncT &colorFunc)
Draw a colored point cloud with custom colors.
Definition PointCloud.h:267
PointCloud & points(std::vector< ColoredPoint > const &ps)
Definition PointCloud.h:69
PointCloud & addPoint(const PointT &p, Color color)
Add a point in the given color.
Definition PointCloud.h:149
PointCloud & addPointUnchecked(ColoredPoint const &p)
Definition PointCloud.h:92
PointCloud & pointCloud(const PointCloudT &cloud, const std::vector< int > &indices, Color color)
Draw a unicolored point cloud with given color and indices.
Definition PointCloud.h:251
#define ARMARX_CHECK_FITS_SIZE(number, size)
Check whether number is nonnegative (>= 0) and less than size.
This file is part of ArmarX.
This file offers overloads of toIce() and fromIce() functions for STL container types.
pcl::PointCloud< PointT > PointCloudT
Definition Common.h:32
pcl::PointXYZRGBL PointT
Definition Common.h:30
bool isfinite(const std::vector< T, Ts... > &v)
Definition algorithm.h:366