PointCloudConversions.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 <cassert>
27 #include <iostream>
28 
29 #include <pcl/point_types.h>
30 #include <pcl/point_cloud.h>
31 
32 #include <RobotAPI/interface/visualization/DebugDrawerInterface.h>
33 
34 #include <VisionX/interface/core/DataTypes.h>
35 
36 
37 namespace visionx::tools
38 {
39 
40  template <typename PointT>
41  visionx::PointContentType getPointContentType()
42  {
43  return visionx::ePoints;
44  }
45 
46  template <> visionx::PointContentType getPointContentType<pcl::PointXYZ>();
47  template <> visionx::PointContentType getPointContentType<pcl::PointXYZI>();
48  template <> visionx::PointContentType getPointContentType<pcl::PointXYZL>();
49  template <> visionx::PointContentType getPointContentType<pcl::PointNormal>();
50  template <> visionx::PointContentType getPointContentType<pcl::PointXYZRGBA>();
51  template <> visionx::PointContentType getPointContentType<pcl::PointXYZRGBL>();
52  template <> visionx::PointContentType getPointContentType<pcl::PointXYZRGBNormal>();
53 
54 
55  size_t getBytesPerPoint(visionx::PointContentType pointContent);
56  std::string toString(visionx::PointContentType pointContent);
57 
58 
59  /// Indicate whether this point type contains colors.
60  bool isColored(visionx::PointContentType type);
61  /// Indicate whether this point type contains labels.
62  bool isLabeled(visionx::PointContentType type);
63 
64 
65  /**
66  * @param sourcePtr point to the source pcl point cloud
67  */
68  //template<typename PointT>
69  // void convertFromPCL(typename pcl::PointCloud<PointT>::Ptr sourcePtr, void **target)
70  void convertFromPCL(pcl::PointCloud<pcl::PointXYZ>::Ptr sourcePtr, void** target);
71 
72 
73  /**
74  * @param targetPtr point to the pcl point cloud
75  */
76  //template<typename PointT>
77  //void convertToPCL(void **source, visionx::MetaPointCloudFormatPtr pointCloudFormat, typename pcl::PointCloud<PointT>::Ptr targetPtr)
78  void convertToPCL(void** source, visionx::MetaPointCloudFormatPtr pointCloudFormat, pcl::PointCloud<pcl::PointXYZ>::Ptr targetPtr);
79 
80  void convertToPCL(const visionx::ColoredPointCloud& source, pcl::PointCloud<pcl::PointXYZRGBA>::Ptr targetPtr);
81  void convertFromPCL(const pcl::PointCloud<pcl::PointXYZRGBA>::Ptr sourcePtr, visionx::ColoredPointCloud& target);
82 
83  void convertFromPCL(pcl::PointCloud<pcl::PointXYZRGBA>::Ptr sourcePtr, void** target);
84  void convertToPCL(void** source, visionx::MetaPointCloudFormatPtr pointCloudFormat, pcl::PointCloud<pcl::PointXYZRGBA>::Ptr targetPtr);
85 
86  void convertFromPCL(pcl::PointCloud<pcl::PointXYZL>::Ptr sourcePtr, void** target);
87  void convertToPCL(void** source, visionx::MetaPointCloudFormatPtr pointCloudFormat, pcl::PointCloud<pcl::PointXYZL>::Ptr targetPtr);
88 
89  void convertFromPCL(pcl::PointCloud<pcl::PointXYZI>::Ptr sourcePtr, void** target);
90  void convertToPCL(void** source, visionx::MetaPointCloudFormatPtr pointCloudFormat, pcl::PointCloud<pcl::PointXYZI>::Ptr targetPtr);
91 
92 
93  void convertFromPCL(pcl::PointCloud<pcl::PointXYZRGBL>::Ptr sourcePtr, void** target);
94  void convertToPCL(void** source, visionx::MetaPointCloudFormatPtr pointCloudFormat, pcl::PointCloud<pcl::PointXYZRGBL>::Ptr targetPtr);
95 
96  void convertFromPCL(pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr& sourcePtr, void** target);
97  void convertToPCL(void** source, visionx::MetaPointCloudFormatPtr pointCloudFormat, pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr targetPtr);
98 
99  void convertFromPCL(pcl::PointCloud<pcl::PointXYZRGB>::Ptr sourcePtr, void** target);
100  void convertToPCL(void** source, visionx::MetaPointCloudFormatPtr pointCloudFormat, pcl::PointCloud<pcl::PointXYZRGB>::Ptr targetPtr);
101 
103  {
107  };
108 
109  void convertFromPCLToDebugDrawer(const pcl::PointCloud<pcl::PointXYZRGBA>::Ptr& sourcePtr, armarx::DebugDrawer24BitColoredPointCloud& target, PCLToDebugDrawerConversionMode mode = eConvertAsPoints, float pointSize = 3);
110  void convertFromPCLToDebugDrawer(const pcl::PointCloud<pcl::PointXYZL>::Ptr& sourcePtr, armarx::DebugDrawer24BitColoredPointCloud& target, PCLToDebugDrawerConversionMode mode = eConvertAsPoints, float pointSize = 3);
111  void convertFromPCLToDebugDrawer(const pcl::PointCloud<pcl::PointXYZ>::Ptr& sourcePtr, armarx::DebugDrawer24BitColoredPointCloud& target, PCLToDebugDrawerConversionMode mode = eConvertAsPoints, float pointSize = 3);
112  void convertFromPCLToDebugDrawer(const pcl::PointCloud<pcl::PointXYZRGBL>::Ptr& sourcePtr, armarx::DebugDrawer24BitColoredPointCloud& target, PCLToDebugDrawerConversionMode mode = eConvertAsPoints, float pointSize = 3);
113 
114 
115 
116  // TEMPLATE BASED POINT CLOUD CONVERSION between PCL and VisionX (Ice) types.
117  // (without copying code over and over)
118 
119  /// Convert a PCL point cloud to a VisionX point cloud.
120  template <typename VisionXPointT, typename PclPointT>
121  void convertFromPCL(const pcl::PointCloud<PclPointT>& source,
122  std::vector<VisionXPointT>& target);
123 
124  /**
125  * @brief Convert a PCL point cloud to a VisionX point cloud (as return value).
126  *
127  * Pass the desired point type as first template argument, for example:
128  * @code
129  * pcl::PointCloud<pcl::PointXYZRGBA> source = ...;
130  * ColoredPointCloud target = visionx::tools::convertFromPCL<ColoredPoint3D>(source);
131  * ColoredPointCloud target = visionx::tools::convertFromPCL<ColoredPointCloud::value_type>(source);
132  * @endcode
133  */
134  template <typename VisionXPointT, typename PclPointT>
135  std::vector<VisionXPointT> convertFromPCL(const pcl::PointCloud<PclPointT>& source);
136 
137 
138  /// Convert a VisionX point cloud to a PCL point cloud.
139  template <typename PclPointT, typename VisionXPointT>
140  void convertToPCL(const std::vector<VisionXPointT>& source,
141  pcl::PointCloud<PclPointT>& target);
142 
143  /**
144  * @brief Convert a VisionX point cloud to a PCL point cloud (as return value).
145  *
146  * Pass the desired point type as first template argument, for example:
147  * @code
148  * using PointCloudT = pcl::PointCloud<pcl::PointXYZRGBA>;
149  * ColoredPointCloud source = ...;
150  * pcl::PointCloud<pcl::PointXYZRGBA>::Ptr target = visionx::tools::convertToPCL<pcl::PointXYZRGBA>(source);
151  * pcl::PointCloud<pcl::PointXYZRGBA> target = *visionx::tools::convertToPCL<PointCloudT::PointType>(source);
152  * @endcode
153  */
154  template <typename PclPointT, typename VisionXPointT>
155  typename pcl::PointCloud<PclPointT>::Ptr convertToPCL(const std::vector<VisionXPointT>& source);
156 
157 
158  // POINT CONVERSION
159 
160  /// Convert a PCL point type to a VisionX point type.
161  template <typename PclPointT, typename VisionXPointT>
162  void convertPointFromPCL(const PclPointT& p, VisionXPointT& v)
163  {
164  p.CONVERSION_BETWEEN_THESE_POINT_TYPES_IS_NOT_IMPLEMENTED(v);
165  }
166 
167  /// Convert a VisionX point type to a PCL point type.
168  template <typename PclPointT, typename VisionXPointT>
169  void convertPointToPCL(const VisionXPointT& v, PclPointT& p)
170  {
171  p.CONVERSION_BETWEEN_THESE_POINT_TYPES_IS_NOT_IMPLEMENTED(v);
172  }
173 
174  // POINT CONVERSION HELPERS
175 }
176 
177 namespace visionx::tools::detail
178 {
179  /// Convert x, y, z attributes. Use with visionx::Point3D and PCL type with XYZ.
180  template <typename SourceT, typename TargetT>
181  void convertXYZ(const SourceT& source, TargetT& target)
182  {
183  target.x = source.x;
184  target.y = source.y;
185  target.z = source.z;
186  }
187 
188  /// Convert r, g, b attributes. Use with visionx::RGBA and PCL type with RGBA.
189  template <typename SourceT, typename TargetT>
190  void convertRGBA(const SourceT& source, TargetT& target)
191  {
192  target.r = source.r;
193  target.g = source.g;
194  target.b = source.b;
195  target.a = source.a;
196  }
197 
198  /// Convert label attribute. Use with visionx point type and PCL type with labels.
199  template <typename SourceT, typename TargetT>
200  void convertLabel(const SourceT& source, TargetT& target)
201  {
202  target.label = source.label;
203  }
204 }
205 
206 namespace visionx::tools
207 {
208 
209  // POINT CONVERSION SPECIALIZATIONS
210 
211  // pcl::PointXYZ <-> visionx::Point3D
212  template <> void convertPointFromPCL(const pcl::PointXYZ& source, visionx::Point3D& target);
213  template <> void convertPointToPCL(const visionx::Point3D& source, pcl::PointXYZ& target);
214 
215  // pcl::PointXYZRGBA <-> visionx::ColoredPoint3D
216  template <> void convertPointFromPCL(const pcl::PointXYZRGBA& source, visionx::ColoredPoint3D& target);
217  template <> void convertPointToPCL(const visionx::ColoredPoint3D& source, pcl::PointXYZRGBA& target);
218 
219  // pcl::PointXYZL <-> visionx::LabeledPoint3D
220  template <> void convertPointFromPCL(const pcl::PointXYZL& source, visionx::LabeledPoint3D& target);
221  template <> void convertPointToPCL(const visionx::LabeledPoint3D& source, pcl::PointXYZL& target);
222 
223  // pcl::PointXYZRGBL <-> visionx::ColoredLabeledPoint3D
224  template <> void convertPointFromPCL(const pcl::PointXYZRGBL& source, visionx::ColoredLabeledPoint3D& target);
225  template <> void convertPointToPCL(const visionx::ColoredLabeledPoint3D& source, pcl::PointXYZRGBL& target);
226 
227 
228 
229  // IMPLEMENTATION (depends on convertPointFromPcl)
230 
231  // Convert a PCL point cloud to a VisionX point cloud.
232  template <typename VisionXPointT, typename PclPointT>
233  void convertFromPCL(const pcl::PointCloud<PclPointT>& source,
234  std::vector<VisionXPointT>& target)
235  {
236  target.resize(source.size());
237  for (std::size_t i = 0; i < source.size(); ++i)
238  {
240  }
241  }
242 
243  // Convert a PCL point cloud to a VisionX point cloud.
244  template <typename VisionXPointT, typename PclPointT>
245  std::vector<VisionXPointT> convertFromPCL(const pcl::PointCloud<PclPointT>& source)
246  {
247  std::vector<VisionXPointT> target;
249  return target;
250  }
251 
252  // Convert a VisionX point cloud to a PCL point cloud.
253  template <typename PclPointT, typename VisionXPointT>
254  void convertToPCL(const std::vector<VisionXPointT>& source,
255  pcl::PointCloud<PclPointT>& target)
256  {
257  target.resize(source.size());
258  for (std::size_t i = 0; i < source.size(); ++i)
259  {
261  }
262  }
263 
264  /// Convert a VisionX point cloud to a PCL point cloud.
265  template <typename PclPointT, typename VisionXPointT>
266  typename pcl::PointCloud<PclPointT>::Ptr convertToPCL(const std::vector<VisionXPointT>& source)
267  {
268  typename pcl::PointCloud<PclPointT>::Ptr target(new pcl::PointCloud<PclPointT>);
270  return target;
271  }
272 
273 }
274 
275 
visionx::tools::PCLToDebugDrawerConversionMode
PCLToDebugDrawerConversionMode
Definition: PointCloudConversions.h:102
visionx::tools
Definition: PCLUtilities.cpp:4
visionx::tools::detail::convertXYZ
void convertXYZ(const SourceT &source, TargetT &target)
Convert x, y, z attributes. Use with visionx::Point3D and PCL type with XYZ.
Definition: PointCloudConversions.h:181
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
visionx::tools::eConvertAsColors
@ eConvertAsColors
Definition: PointCloudConversions.h:105
visionx::tools::isLabeled
bool isLabeled(PointContentType type)
Definition: PointCloudConversions.cpp:108
visionx::tools::detail::convertRGBA
void convertRGBA(const SourceT &source, TargetT &target)
Convert r, g, b attributes. Use with visionx::RGBA and PCL type with RGBA.
Definition: PointCloudConversions.h:190
visionx::tools::detail::convertLabel
void convertLabel(const SourceT &source, TargetT &target)
Convert label attribute. Use with visionx point type and PCL type with labels.
Definition: PointCloudConversions.h:200
visionx::tools::convertFromPCL
void convertFromPCL(pcl::PointCloud< pcl::PointXYZRGBA >::Ptr sourcePtr, void **target)
Definition: PointCloudConversions.cpp:121
visionx::tools::convertPointFromPCL
void convertPointFromPCL(const pcl::PointXYZ &source, visionx::Point3D &target)
Definition: PointCloudConversions.cpp:697
visionx::tools::convertPointToPCL
void convertPointToPCL(const visionx::Point3D &source, pcl::PointXYZ &target)
Definition: PointCloudConversions.cpp:702
visionx::tools::isColored
bool isColored(PointContentType type)
Definition: PointCloudConversions.cpp:95
visionx::tools::eConvertAsPoints
@ eConvertAsPoints
Definition: PointCloudConversions.h:104
visionx::tools::convertFromPCLToDebugDrawer
void convertFromPCLToDebugDrawer(const pcl::PointCloud< pcl::PointXYZRGBA >::Ptr &sourcePtr, armarx::DebugDrawer24BitColoredPointCloud &target, PCLToDebugDrawerConversionMode mode, float pointSize)
Definition: PointCloudConversions.cpp:530
visionx::tools::toString
std::string toString(PointContentType pointContent)
Definition: PointCloudConversions.cpp:765
visionx::tools::eConvertAsLabels
@ eConvertAsLabels
Definition: PointCloudConversions.h:106
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
visionx::tools::convertToPCL
void convertToPCL(void **source, visionx::MetaPointCloudFormatPtr pointCloudFormat, pcl::PointCloud< pcl::PointXYZRGBA >::Ptr targetPtr)
Definition: PointCloudConversions.cpp:146
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
visionx::tools::getBytesPerPoint
size_t getBytesPerPoint(visionx::PointContentType pointContent)
Definition: PointCloudConversions.cpp:64
visionx::tools::getPointContentType
visionx::PointContentType getPointContentType()
Definition: PointCloudConversions.h:41
visionx::tools::detail
Definition: PCLUtilities.h:86