PCLUtilities.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX::Tools
19  * @author Markus Grotz ( markus dot grotz at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <map>
27 #include <tuple>
28 #include <iostream>
29 #include <type_traits>
30 
31 #include <pcl/point_types.h>
32 #include <pcl/point_cloud.h>
33 #include <pcl/common/io.h>
34 #include <pcl/io/pcd_io.h>
35 
36 #include <pcl/common/colors.h>
37 
38 #include <VisionX/interface/core/DataTypes.h>
39 
40 // Some code was moved to: (do not include here)
41 // #include <VisionX/libraries/PointCloudTools/segments.h>
42 
43 
44 namespace visionx::tools
45 {
46 
47  template <typename PointCloudPtrT>
48  std::tuple<uint8_t, uint8_t, uint8_t> colorizeSegment(PointCloudPtrT& segment)
49  {
50  const uint8_t r = rand() % 255;
51  const uint8_t g = rand() % 255;
52  const uint8_t b = rand() % 255;
53 
54  for (auto& p : segment->points)
55  {
56  p.r = r;
57  p.g = g;
58  p.b = b;
59  }
60 
61  return std::make_tuple(r, g, b);
62  }
63 
64 
65  inline void colorizeLabeledPointCloud(pcl::PointCloud<pcl::PointXYZL>::Ptr sourceCloudPtr,
66  pcl::PointCloud<pcl::PointXYZRGBA>::Ptr targetCloudPtr)
67  {
68  pcl::copyPointCloud(*sourceCloudPtr, *targetCloudPtr);
69 
70  std::map<uint32_t, float> colorMap;
71 
72  for (size_t i = 0; i < sourceCloudPtr->points.size(); i++)
73  {
74  pcl::PointXYZL p = sourceCloudPtr->points[i];
75 
76  if (!colorMap.count(p.label))
77  {
78  pcl::RGB c = pcl::GlasbeyLUT::at(p.label % pcl::GlasbeyLUT::size());
79  colorMap.insert(std::make_pair(p.label, c.rgb));
80  }
81 
82  targetCloudPtr->points[i].rgb = colorMap[p.label];
83  }
84  }
85 }
87 {
88  // Need a seperate method to be callable from pcl::PointCloud<>::Ptr versions.
89  /**
90  * @brief Fill `labelIndicesMap` with indices of each segment's points.
91  * @param labeledCloud A labeled point cloud.
92  * @param labeledPointMap The map to fill (label -> point indicies).
93  * @param excludeZero If true, points with label == 0 are ignored.
94  */
95  template <class PointCloudT>
96  void fillLabelMap(const PointCloudT& labeledCloud, std::map<uint32_t, pcl::PointIndices>& labelIndicesMap, bool excludeZero)
97  {
98  for (size_t i = 0; i < labeledCloud.points.size(); i++)
99  {
100  uint32_t currentLabel = labeledCloud.points[i].label;
101 
102  if (excludeZero && currentLabel == 0)
103  {
104  continue;
105  }
106 
107  // With [] operator, std::map default-constructs a value if it is not already
108  // present, which is exactly what we need here (i.e. no need to explicitly check
109  // key presence).
110  labelIndicesMap[currentLabel].indices.push_back(static_cast<int>(i));
111  }
112  }
113 }
114 namespace visionx::tools
115 {
116  template <class T> struct is_shared_ptr : std::false_type {};
117  template <class T> struct is_shared_ptr<boost::shared_ptr<T>> : std::true_type {};
118  template <class T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
119 
120  // Legacy: Support pcl::PointCloud<>::Ptr as template argument.
121  /// @see detail::fillLabelMap()
122  template <class LabeledPointCloudPtrT>
124  fillLabelMap(LabeledPointCloudPtrT labeledCloudPtr,
125  std::map<uint32_t, pcl::PointIndices>& labelIndicesMap,
126  bool excludeZero = true)
127  {
128  detail::fillLabelMap(*labeledCloudPtr, labelIndicesMap, excludeZero);
129  }
130 
131  // Version for const pcl::PointCloud<>& .
132  /// @see detail::fillLabelMap()
133  template <class PointCloudT>
135  fillLabelMap(const PointCloudT& labeledCloud,
136  std::map<uint32_t, pcl::PointIndices>& labelIndicesMap,
137  bool excludeZero = true)
138  {
139  detail::fillLabelMap(labeledCloud, labelIndicesMap, excludeZero);
140  }
141 
142  /// @see detail::fillLabelMap()
143  template <class PointCloudT>
144  std::map<uint32_t, pcl::PointIndices> getLabelMap(
145  const PointCloudT& labeledCloud, bool excludeZero = true)
146  {
147  std::map<uint32_t, pcl::PointIndices> labelIndicesMap;
148  detail::fillLabelMap(labeledCloud, labelIndicesMap, excludeZero);
149  return labelIndicesMap;
150  }
151 }
152 
visionx::tools::colorizeSegment
std::tuple< uint8_t, uint8_t, uint8_t > colorizeSegment(PointCloudPtrT &segment)
Definition: PCLUtilities.h:48
visionx::tools
Definition: PCLUtilities.cpp:4
visionx::tools::colorizeLabeledPointCloud
void colorizeLabeledPointCloud(pcl::PointCloud< pcl::PointXYZL >::Ptr sourceCloudPtr, pcl::PointCloud< pcl::PointXYZRGBA >::Ptr targetCloudPtr)
Definition: PCLUtilities.h:65
visionx::tools::detail::fillLabelMap
void fillLabelMap(const PointCloudT &labeledCloud, std::map< uint32_t, pcl::PointIndices > &labelIndicesMap, bool excludeZero)
Fill labelIndicesMap with indices of each segment's points.
Definition: PCLUtilities.h:96
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
boost
Definition: ApplicationOptions.h:37
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::PointCloudT
pcl::PointCloud< PointT > PointCloudT
Definition: Common.h:30
visionx::tools::getLabelMap
std::map< uint32_t, pcl::PointIndices > getLabelMap(const PointCloudT &labeledCloud, bool excludeZero=true)
Definition: PCLUtilities.h:144
std
Definition: Application.h:66
visionx::tools::is_shared_ptr
Definition: PCLUtilities.h:116
visionx::tools::fillLabelMap
std::enable_if< is_shared_ptr< LabeledPointCloudPtrT >::value, void >::type fillLabelMap(LabeledPointCloudPtrT labeledCloudPtr, std::map< uint32_t, pcl::PointIndices > &labelIndicesMap, bool excludeZero=true)
Definition: PCLUtilities.h:124
visionx::tools::detail
Definition: PCLUtilities.h:86