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 <iostream>
27#include <map>
28#include <tuple>
29#include <type_traits>
30
31#include <pcl/common/colors.h>
32#include <pcl/common/io.h>
33#include <pcl/io/pcd_io.h>
34#include <pcl/point_cloud.h>
35#include <pcl/point_types.h>
36
37#include <VisionX/interface/core/DataTypes.h>
38
39// Some code was moved to: (do not include here)
40// #include <VisionX/libraries/PointCloudTools/segments.h>
41
42
43namespace visionx::tools
44{
45
46 template <typename PointCloudPtrT>
47 std::tuple<uint8_t, uint8_t, uint8_t>
48 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 inline void
65 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} // namespace visionx::tools
86
88{
89 // Need a seperate method to be callable from pcl::PointCloud<>::Ptr versions.
90 /**
91 * @brief Fill `labelIndicesMap` with indices of each segment's points.
92 * @param labeledCloud A labeled point cloud.
93 * @param labeledPointMap The map to fill (label -> point indicies).
94 * @param excludeZero If true, points with label == 0 are ignored.
95 */
96 template <class PointCloudT>
97 void
98 fillLabelMap(const PointCloudT& labeledCloud,
99 std::map<uint32_t, pcl::PointIndices>& labelIndicesMap,
100 bool excludeZero)
101 {
102 for (size_t i = 0; i < labeledCloud.points.size(); i++)
103 {
104 uint32_t currentLabel = labeledCloud.points[i].label;
105
106 if (excludeZero && currentLabel == 0)
107 {
108 continue;
109 }
110
111 // With [] operator, std::map default-constructs a value if it is not already
112 // present, which is exactly what we need here (i.e. no need to explicitly check
113 // key presence).
114 labelIndicesMap[currentLabel].indices.push_back(static_cast<int>(i));
115 }
116 }
117} // namespace visionx::tools::detail
118
119namespace visionx::tools
120{
121 template <class T>
122 struct is_shared_ptr : std::false_type
123 {
124 };
125
126 template <class T>
127 struct is_shared_ptr<boost::shared_ptr<T>> : std::true_type
128 {
129 };
130
131 template <class T>
132 struct is_shared_ptr<std::shared_ptr<T>> : std::true_type
133 {
134 };
135
136 // Legacy: Support pcl::PointCloud<>::Ptr as template argument.
137 /// @see detail::fillLabelMap()
138 template <class LabeledPointCloudPtrT>
139 typename std::enable_if<is_shared_ptr<LabeledPointCloudPtrT>::value, void>::type
140 fillLabelMap(LabeledPointCloudPtrT labeledCloudPtr,
141 std::map<uint32_t, pcl::PointIndices>& labelIndicesMap,
142 bool excludeZero = true)
143 {
144 detail::fillLabelMap(*labeledCloudPtr, labelIndicesMap, excludeZero);
145 }
146
147 // Version for const pcl::PointCloud<>& .
148 /// @see detail::fillLabelMap()
149 template <class PointCloudT>
150 typename std::enable_if<!is_shared_ptr<PointCloudT>::value, void>::type
151 fillLabelMap(const PointCloudT& labeledCloud,
152 std::map<uint32_t, pcl::PointIndices>& labelIndicesMap,
153 bool excludeZero = true)
154 {
155 detail::fillLabelMap(labeledCloud, labelIndicesMap, excludeZero);
156 }
157
158 /// @see detail::fillLabelMap()
159 template <class PointCloudT>
160 std::map<uint32_t, pcl::PointIndices>
161 getLabelMap(const PointCloudT& labeledCloud, bool excludeZero = true)
162 {
163 std::map<uint32_t, pcl::PointIndices> labelIndicesMap;
164 detail::fillLabelMap(labeledCloud, labelIndicesMap, excludeZero);
165 return labelIndicesMap;
166 }
167} // namespace visionx::tools
constexpr T c
void fillLabelMap(const PointCloudT &labeledCloud, std::map< uint32_t, pcl::PointIndices > &labelIndicesMap, bool excludeZero)
Fill labelIndicesMap with indices of each segment's points.
std::map< uint32_t, pcl::PointIndices > getLabelMap(const PointCloudT &labeledCloud, bool excludeZero=true)
void colorizeLabeledPointCloud(pcl::PointCloud< pcl::PointXYZL >::Ptr sourceCloudPtr, pcl::PointCloud< pcl::PointXYZRGBA >::Ptr targetCloudPtr)
std::tuple< uint8_t, uint8_t, uint8_t > colorizeSegment(PointCloudPtrT &segment)
std::enable_if< is_shared_ptr< LabeledPointCloudPtrT >::value, void >::type fillLabelMap(LabeledPointCloudPtrT labeledCloudPtr, std::map< uint32_t, pcl::PointIndices > &labelIndicesMap, bool excludeZero=true)