vision
Loading...
Searching...
No Matches
YarpCloudUtils-pcl.hpp
1// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
3#ifndef __YARP_CLOUD_UTILS_PCL_HPP__
4#define __YARP_CLOUD_UTILS_PCL_HPP__
5
6#include <stdexcept>
7#include <type_traits>
8
9#include <pcl/point_cloud.h>
10#include <pcl/point_types.h>
11#include <pcl/PolygonMesh.h>
12#include <pcl/common/io.h>
13
14#include "YarpCloudUtils-pcl-traits.hpp"
15
16namespace
17{
18
19class cloud_container
20{
21public:
22 template <typename T>
23 typename pcl::PointCloud<T>::ConstPtr getCloud() const;
24
25 template <typename T>
26 typename pcl::PointCloud<T>::Ptr & setCloud();
27
28 template <typename T>
29 const typename pcl::PointCloud<T>::Ptr & useCloud() const;
30
31 pcl::PolygonMesh::ConstPtr getMesh() const;
32
33 pcl::PolygonMesh::Ptr & setMesh();
34
35private:
36 template <typename T1, typename T2>
37 static auto initializeCloudPointer(const typename pcl::PointCloud<T1>::ConstPtr & in);
38
39 pcl::PointCloud<pcl::PointXYZ>::Ptr xyz;
40 pcl::PointCloud<pcl::PointXYZRGB>::Ptr xyz_rgb;
41 pcl::PointCloud<pcl::PointXYZI>::Ptr xyzi;
42 pcl::PointCloud<pcl::InterestPoint>::Ptr xyz_interest;
43 pcl::PointCloud<pcl::PointNormal>::Ptr xyz_normal;
44 pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr xyz_rgb_normal;
45 pcl::PointCloud<pcl::PointXYZINormal>::Ptr xyzi_normal;
46 pcl::PolygonMesh::Ptr mesh;
47};
48
49// helpers
50
51template <typename T1, typename T2>
52inline auto cloud_container::initializeCloudPointer(const typename pcl::PointCloud<T1>::ConstPtr & in)
53{
54 if constexpr (!std::is_same_v<T1, T2>)
55 {
56 if constexpr (!pcl_is_convertible<T1, T2>::value)
57 {
58 std::string name_lhs = pcl_descriptor<T1>::name;
59 std::string name_rhs = pcl_descriptor<T2>::name;
60 throw std::runtime_error("illegal conversion from " + name_lhs + " to " + name_rhs);
61 }
62
63 auto out = pcl::make_shared<pcl::PointCloud<T2>>();
64 pcl::copyPointCloud(*in, *out);
65 return out;
66 }
67 else
68 {
69 return in;
70 }
71}
72
73// cloud_container::getCloud
74
75template <typename T>
76typename pcl::PointCloud<T>::ConstPtr cloud_container::getCloud() const
77{
78 if (xyz)
79 {
80 return initializeCloudPointer<pcl::PointXYZ, T>(xyz);
81 }
82 else if (xyz_rgb)
83 {
84 return initializeCloudPointer<pcl::PointXYZRGB, T>(xyz_rgb);
85 }
86 else if (xyzi)
87 {
88 return initializeCloudPointer<pcl::PointXYZI, T>(xyzi);
89 }
90 else if (xyz_interest)
91 {
92 return initializeCloudPointer<pcl::InterestPoint, T>(xyz_interest);
93 }
94 else if (xyz_normal)
95 {
96 return initializeCloudPointer<pcl::PointNormal, T>(xyz_normal);
97 }
98 else if (xyz_rgb_normal)
99 {
100 return initializeCloudPointer<pcl::PointXYZRGBNormal, T>(xyz_rgb_normal);
101 }
102 else if (xyzi_normal)
103 {
104 return initializeCloudPointer<pcl::PointXYZINormal, T>(xyzi_normal);
105 }
106 else
107 {
108 throw std::runtime_error("cloud pointer was not initialized");
109 }
110}
111
112// cloud_container::setCloud
113
114template <>
115pcl::PointCloud<pcl::PointXYZ>::Ptr & cloud_container::setCloud<pcl::PointXYZ>()
116{
117 xyz = pcl::make_shared<pcl::PointCloud<pcl::PointXYZ>>();
118 return xyz;
119}
120
121template <>
122pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud_container::setCloud<pcl::PointXYZRGB>()
123{
124 xyz_rgb = pcl::make_shared<pcl::PointCloud<pcl::PointXYZRGB>>();
125 return xyz_rgb;
126}
127
128template <>
129pcl::PointCloud<pcl::PointXYZI>::Ptr & cloud_container::setCloud<pcl::PointXYZI>()
130{
131 xyzi = pcl::make_shared<pcl::PointCloud<pcl::PointXYZI>>();
132 return xyzi;
133}
134
135template <>
136pcl::PointCloud<pcl::InterestPoint>::Ptr & cloud_container::setCloud<pcl::InterestPoint>()
137{
138 xyz_interest = pcl::make_shared<pcl::PointCloud<pcl::InterestPoint>>();
139 return xyz_interest;
140}
141
142template <>
143pcl::PointCloud<pcl::PointNormal>::Ptr & cloud_container::setCloud<pcl::PointNormal>()
144{
145 xyz_normal = pcl::make_shared<pcl::PointCloud<pcl::PointNormal>>();
146 return xyz_normal;
147}
148
149template <>
150pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr & cloud_container::setCloud<pcl::PointXYZRGBNormal>()
151{
152 xyz_rgb_normal = pcl::make_shared<pcl::PointCloud<pcl::PointXYZRGBNormal>>();
153 return xyz_rgb_normal;
154}
155
156template <>
157pcl::PointCloud<pcl::PointXYZINormal>::Ptr & cloud_container::setCloud<pcl::PointXYZINormal>()
158{
159 xyzi_normal = pcl::make_shared<pcl::PointCloud<pcl::PointXYZINormal>>();
160 return xyzi_normal;
161}
162
163// cloud_container::useCloud
164
165template <>
166const pcl::PointCloud<pcl::PointXYZ>::Ptr & cloud_container::useCloud<pcl::PointXYZ>() const
167{
168 return xyz;
169}
170
171template <>
172const pcl::PointCloud<pcl::PointXYZRGB>::Ptr & cloud_container::useCloud<pcl::PointXYZRGB>() const
173{
174 return xyz_rgb;
175}
176
177template <>
178const pcl::PointCloud<pcl::PointXYZI>::Ptr & cloud_container::useCloud<pcl::PointXYZI>() const
179{
180 return xyzi;
181}
182
183template <>
184const pcl::PointCloud<pcl::InterestPoint>::Ptr & cloud_container::useCloud<pcl::InterestPoint>() const
185{
186 return xyz_interest;
187}
188
189template <>
190const pcl::PointCloud<pcl::PointNormal>::Ptr & cloud_container::useCloud<pcl::PointNormal>() const
191{
192 return xyz_normal;
193}
194
195template <>
196const pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr & cloud_container::useCloud<pcl::PointXYZRGBNormal>() const
197{
198 return xyz_rgb_normal;
199}
200
201template <>
202const pcl::PointCloud<pcl::PointXYZINormal>::Ptr & cloud_container::useCloud<pcl::PointXYZINormal>() const
203{
204 return xyzi_normal;
205}
206
207// cloud_container::(get|set)Mesh
208
209inline pcl::PolygonMesh::ConstPtr cloud_container::getMesh() const
210{
211 if (mesh)
212 {
213 return mesh; // a ctor is called here (Ptr is turned into ConstPtr)
214 }
215 else
216 {
217 throw std::runtime_error("mesh pointer was not initialized");
218 }
219}
220
221inline pcl::PolygonMesh::Ptr & cloud_container::setMesh()
222{
223 mesh = pcl::make_shared<pcl::PolygonMesh>();
224 return mesh;
225}
226
227} // namespace
228
229#endif // __YARP_CLOUD_UTILS_PCL_HPP__