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