segments.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <vector>
5 
6 #include <pcl/point_cloud.h>
7 #include <pcl/PointIndices.h>
8 
9 #include <VisionX/interface/core/DataTypes.h>
10 
11 #include <SimoxUtility/shapes/AxisAlignedBoundingBox.h>
12 
13 #include "AABB.h"
14 
15 
16 namespace visionx::tools::detail
17 {
18  // Version for pcl::PointIndices
19  void addSegmentIndex(std::map<uint32_t, pcl::PointIndices>& segmentIndices,
20  uint32_t label, std::size_t index);
21  // Version for pcl::PointIndices::Ptr
22  void addSegmentIndex(std::map<uint32_t, pcl::PointIndices::Ptr>& segmentIndices,
23  uint32_t label, std::size_t index);
24 
25  template <class LabeledPointT, class MapValueT>
26  void addSegmentIndices(const pcl::PointCloud<LabeledPointT>& labeledCloud,
27  std::map<uint32_t, MapValueT>& segmentIndices,
28  bool excludeZero = false)
29  {
30  for (std::size_t i = 0; i < labeledCloud.points.size(); i++)
31  {
32  const uint32_t currentLabel = labeledCloud.points[i].label;
33  if (!(excludeZero && currentLabel == 0))
34  {
35  addSegmentIndex(segmentIndices, currentLabel, i);
36  }
37  }
38  }
39 }
40 
41 namespace visionx::tools
42 {
43  // Segment Indices
44 
45  /**
46  * @brief Add indices of each segment's points to `segmentIndices`.
47  * @param segmentIndices The map to fill (label -> point indicies).
48  * @param labeledCloud A labeled point cloud.
49  * @param excludeZero If true, points with label == 0 are ignored.
50  */
51  template <class LabeledPointT>
52  void addSegmentIndices(std::map<uint32_t, pcl::PointIndices>& segmentIndices,
53  const pcl::PointCloud<LabeledPointT>& labeledCloud,
54  bool excludeZero = false)
55  {
56  detail::addSegmentIndices(labeledCloud, segmentIndices, excludeZero);
57  }
58 
59  template <class LabeledPointT>
60  void addSegmentIndices(std::map<uint32_t, pcl::PointIndices::Ptr>& segmentIndices,
61  const pcl::PointCloud<LabeledPointT>& labeledCloud,
62  bool excludeZero = false)
63  {
64  detail::addSegmentIndices(labeledCloud, segmentIndices, excludeZero);
65  }
66 
67  /**
68  * @brief Get a map from segment labels to indices of segments' points.
69  * @param labeledCloud A labeled point cloud.
70  * @param excludeZero If true, points with label == 0 are ignored.
71  * @return Map from segment labels to segment indices.
72  */
73  template <class LabeledPointT>
74  std::map<uint32_t, pcl::PointIndices> getSegmentIndices(
75  const pcl::PointCloud<LabeledPointT>& labeledCloud, bool excludeZero = false)
76  {
77  std::map<uint32_t, pcl::PointIndices> segmentIndices;
78  addSegmentIndices(segmentIndices, labeledCloud, excludeZero);
79  return segmentIndices;
80  }
81 
82  /// Version of `addSegmentIndices()` containing `pcl::PointIndices::Ptr`.
83  /// @see addSegmentIndices()
84  template <class LabeledPointT>
85  std::map<uint32_t, pcl::PointIndices::Ptr> getSegmentIndicesPtr(
86  const pcl::PointCloud<LabeledPointT>& labeledCloud, bool excludeZero = false)
87  {
88  std::map<uint32_t, pcl::PointIndices::Ptr> segmentIndices;
89  addSegmentIndices(segmentIndices, labeledCloud, excludeZero);
90  return segmentIndices;
91  }
92 
93 
94  /// Return the set of labels in `pointCloud`.
95  template <typename LabeledPointT>
96  std::set<uint32_t> getLabelSet(const pcl::PointCloud<LabeledPointT>& pointCloud)
97  {
98  std::set<uint32_t> labels;
99  for (const auto& point : pointCloud)
100  {
101  labels.insert(point.label);
102  }
103  return labels;
104  }
105 
106  /// Return the labels in `segmentIndices` as `std::set`.
107  template <class ValueT>
108  std::set<uint32_t> getLabelSet(const std::map<uint32_t, ValueT>& segmentIndices)
109  {
110  std::set<uint32_t> labels;
111  for (const auto& [label, _] : segmentIndices)
112  {
113  labels.insert(label);
114  }
115  return labels;
116  }
117 
118  /// Return the labels in `segmentIndices` as `std::vector`.
119  template <class ValueT>
120  std::vector<uint32_t> getUniqueLabels(const std::map<uint32_t, ValueT>& segmentIndices)
121  {
122  std::vector<uint32_t> labels;
123  labels.reserve(segmentIndices.size());
124  for (const auto& [label, _] : segmentIndices)
125  {
126  labels.push_back(label);
127  }
128  return labels;
129  }
130 
131 
132  template <class PointT>
133  void relabel(pcl::PointCloud<PointT>& pointCloud,
134  const std::map<uint32_t, pcl::PointIndices>& segmentIndices)
135  {
136  for (const auto& [label, indices] : segmentIndices)
137  {
138  for (int index : indices.indices)
139  {
140  pointCloud.at(index).label = label;
141  }
142  }
143  }
144 
145  template <class PointT>
146  void relabel(pcl::PointCloud<PointT>& pointCloud,
147  const std::map<uint32_t, pcl::PointIndices::Ptr>& segmentIndices)
148  {
149  for (const auto& [label, indices] : segmentIndices)
150  {
151  for (int index : indices->indices)
152  {
153  pointCloud.at(index).label = label;
154  }
155  }
156  }
157 
158 
159  template <class PointT, class IndicesT = pcl::PointIndices>
160  std::vector<simox::AxisAlignedBoundingBox>
161  getSegmentAABBs(const pcl::PointCloud<PointT>& pointCloud,
162  const std::vector<IndicesT>& segmentIndices)
163  {
164  std::vector<simox::AxisAlignedBoundingBox> aabbs;
165  aabbs.reserve(segmentIndices.size());
166  for (const auto& indices : segmentIndices)
167  {
168  aabbs.push_back(getAABB(pointCloud, indices));
169  }
170  return aabbs;
171  }
172 
173  template <class PointT, class IndicesT = pcl::PointIndices>
174  std::map<uint32_t, simox::AxisAlignedBoundingBox>
175  getSegmentAABBs(const pcl::PointCloud<PointT>& pointCloud,
176  const std::map<uint32_t, IndicesT>& segmentIndices)
177  {
178  std::map<uint32_t, simox::AxisAlignedBoundingBox> aabbs;
179  for (const auto& [label, indices] : segmentIndices)
180  {
181  aabbs[label] = getAABB(pointCloud, indices);
182  }
183  return aabbs;
184  }
185 
186 }
visionx::tools
Definition: PCLUtilities.cpp:4
index
uint8_t index
Definition: EtherCATFrame.h:59
visionx::tools::getSegmentIndices
std::map< uint32_t, pcl::PointIndices > getSegmentIndices(const pcl::PointCloud< LabeledPointT > &labeledCloud, bool excludeZero=false)
Get a map from segment labels to indices of segments' points.
Definition: segments.h:74
visionx::tools::detail::addSegmentIndex
void addSegmentIndex(std::map< uint32_t, pcl::PointIndices > &segmentIndices, uint32_t label, std::size_t index)
Definition: segments.cpp:6
visionx::tools::getUniqueLabels
std::vector< uint32_t > getUniqueLabels(const std::map< uint32_t, ValueT > &segmentIndices)
Return the labels in segmentIndices as std::vector.
Definition: segments.h:120
visionx::tools::getLabelSet
std::set< uint32_t > getLabelSet(const pcl::PointCloud< LabeledPointT > &pointCloud)
Return the set of labels in pointCloud.
Definition: segments.h:96
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
visionx::tools::getSegmentAABBs
std::vector< simox::AxisAlignedBoundingBox > getSegmentAABBs(const pcl::PointCloud< PointT > &pointCloud, const std::vector< IndicesT > &segmentIndices)
Definition: segments.h:161
visionx::tools::relabel
void relabel(pcl::PointCloud< PointT > &pointCloud, const std::map< uint32_t, pcl::PointIndices > &segmentIndices)
Definition: segments.h:133
visionx::tools::getAABB
simox::AxisAlignedBoundingBox getAABB(const PointCloudT &pointCloud, const pcl::PointIndices &indices={})
Get the axis-aligned bounding-box of the given point cloud.
Definition: AABB.h:27
visionx::tools::addSegmentIndices
void addSegmentIndices(std::map< uint32_t, pcl::PointIndices > &segmentIndices, const pcl::PointCloud< LabeledPointT > &labeledCloud, bool excludeZero=false)
Add indices of each segment's points to segmentIndices.
Definition: segments.h:52
visionx::tools::detail::addSegmentIndices
void addSegmentIndices(const pcl::PointCloud< LabeledPointT > &labeledCloud, std::map< uint32_t, MapValueT > &segmentIndices, bool excludeZero=false)
Definition: segments.h:26
visionx::tools::getSegmentIndicesPtr
std::map< uint32_t, pcl::PointIndices::Ptr > getSegmentIndicesPtr(const pcl::PointCloud< LabeledPointT > &labeledCloud, bool excludeZero=false)
Version of addSegmentIndices() containing pcl::PointIndices::Ptr.
Definition: segments.h:85
AABB.h
visionx::tools::detail
Definition: PCLUtilities.h:86