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