Debian Codegraph @

plugins/core/Standard/qPCL/PclUtils/utils/cc2sm.cpp

raw symbols SCIP
1 //##########################################################################
2 //# #
3 //# CLOUDCOMPARE PLUGIN: qPCL #
4 //# #
5 //# This program is free software; you can redistribute it and/or modify #
6 //# it under the terms of the GNU General Public License as published by #
7 //# the Free Software Foundation; version 2 or later of the License. #
8 //# #
9 //# This program is distributed in the hope that it will be useful, #
10 //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
11 //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
12 //# GNU General Public License for more details. #
13 //# #
14 //# COPYRIGHT: Luca Penasa #
15 //# #
16 //##########################################################################
17 //
18 #include "cc2sm.h"
19
20 //Local
21 #include "my_point_types.h"
22 #include "PCLConv.h"
23
24 //PCL
25 #include <pcl/common/io.h>
26
27 //qCC_db
28 #include <ccPointCloud.h>
29 #include <ccScalarField.h>
30
31 //system
32 #include <assert.h>
33
34 using namespace pcl;
35
36 cc2smReader::cc2smReader(ccPointCloud* cccloud)
37 : m_ccCloud(cccloud)
38 {
39 assert(m_ccCloud);
40 }
41
42 std::string cc2smReader::GetSimplifiedSFName(const QString& ccSfName)
43 {
44 QString simplified = ccSfName.simplified();
45 simplified.replace(' ', '_');
46
47 return simplified.toStdString();
48 }
49
50 pcl::PointCloud<pcl::PointXYZ>::Ptr cc2smReader::getRawXYZ() const
51 {
52 if (!m_ccCloud)
53 {
54 assert(false);
55 return {};
56 }
57
58 PointCloud<PointXYZ>::Ptr xyzCloud(new PointCloud<PointXYZ>);
59 try
60 {
61 unsigned pointCount = m_ccCloud->size();
62 xyzCloud->resize(pointCount);
63
64 for (unsigned i = 0; i < pointCount; ++i)
65 {
66 const CCVector3* P = m_ccCloud->getPoint(i);
67 xyzCloud->at(i).x = static_cast<float>(P->x);
68 xyzCloud->at(i).y = static_cast<float>(P->y);
69 xyzCloud->at(i).z = static_cast<float>(P->z);
70 }
71 }
72 catch (...)
73 {
74 //any error (memory, etc.)
75 return {};
76 }
77
78 return xyzCloud;
79 }
80
81 PCLCloud::Ptr cc2smReader::getXYZ() const
82 {
83 if (!m_ccCloud)
84 {
85 assert(false);
86 return {};
87 }
88
89 PointCloud<PointXYZ>::Ptr xyzCloud = getRawXYZ();
90 if (!xyzCloud)
91 {
92 return {};
93 }
94
95 PCLCloud::Ptr outputCloud;
96 try
97 {
98 outputCloud.reset(new PCLCloud);
99 TO_PCL_CLOUD(*xyzCloud, *outputCloud);
100 }
101 catch (...)
102 {
103 //any error (memory, etc.)
104 return {};
105 }
106
107 return outputCloud;
108 }
109
110 PCLCloud::Ptr cc2smReader::getNormals() const
111 {
112 if (!m_ccCloud || !m_ccCloud->hasNormals())
113 {
114 assert(false);
115 return {};
116 }
117
118 PCLCloud::Ptr outputCloud;
119 try
120 {
121 PointCloud<OnlyNormals> normalsCloud;
122
123 unsigned pointCount = m_ccCloud->size();
124 normalsCloud.resize(pointCount);
125
126 for (unsigned i = 0; i < pointCount; ++i)
127 {
128 const CCVector3& N = m_ccCloud->getPointNormal(i);
129 normalsCloud[i].normal_x = N.x;
130 normalsCloud[i].normal_y = N.y;
131 normalsCloud[i].normal_z = N.z;
132 }
133
134 outputCloud.reset(new PCLCloud);
135 TO_PCL_CLOUD(normalsCloud, *outputCloud);
136 }
137 catch (...)
138 {
139 //any error (memory, etc.)
140 return {};
141 }
142
143 return outputCloud;
144 }
145
146 PCLCloud::Ptr cc2smReader::getColors() const
147 {
148 if (!m_ccCloud || !m_ccCloud->hasColors())
149 {
150 assert(false);
151 return {};
152 }
153
154 PCLCloud::Ptr outputCloud;
155
156 try
157 {
158 PointCloud<OnlyRGB> rgbCloud;
159 unsigned pointCount = m_ccCloud->size();
160 rgbCloud.resize(pointCount);
161
162 for (unsigned i = 0; i < pointCount; ++i)
163 {
164 const ccColor::Rgb& rgb = m_ccCloud->getPointColor(i);
165 rgbCloud[i].r = static_cast<uint8_t>(rgb.r);
166 rgbCloud[i].g = static_cast<uint8_t>(rgb.g);
167 rgbCloud[i].b = static_cast<uint8_t>(rgb.b);
168 }
169
170 outputCloud.reset(new PCLCloud);
171 TO_PCL_CLOUD(rgbCloud, *outputCloud);
172 }
173 catch (...)
174 {
175 //any error (memory, etc.)
176 return {};
177 }
178
179 return outputCloud;
180 }
181
182 PCLCloud::Ptr cc2smReader::getFloatScalarField(const QString& fieldName) const
183 {
184 if (!m_ccCloud)
185 {
186 assert(false);
187 return {};
188 }
189
190 int sfIdx = m_ccCloud->getScalarFieldIndexByName(qPrintable(fieldName));
191 if (sfIdx < 0)
192 {
193 return {};
194 }
195
196 CCCoreLib::ScalarField* sf = m_ccCloud->getScalarField(sfIdx);
197 assert(sf);
198
199 PCLCloud::Ptr outputCloud;
200
201 try
202 {
203 PointCloud<FloatScalar> sfCloud;
204
205 unsigned pointCount = m_ccCloud->size();
206 sfCloud.resize(pointCount);
207
208 for (unsigned i = 0; i < pointCount; ++i)
209 {
210 ScalarType scalar = sf->getValue(i);
211 sfCloud[i].S5c4laR = static_cast<float>(scalar);
212 }
213
214 outputCloud.reset(new PCLCloud);
215 TO_PCL_CLOUD(sfCloud, *outputCloud);
216
217 //Now change the name of the scalar field -> we cannot have any space into the field name
218 //NOTE this is a little trick to put any number of scalar fields in a message PointCloud2 object
219 //We use a point type with a generic scalar field named scalar. we load the scalar field and
220 //then we change the name to the needed one
221 outputCloud->fields[0].name = GetSimplifiedSFName(fieldName);
222 }
223 catch (...)
224 {
225 //any error (memory, etc.)
226 return {};
227 }
228
229 return outputCloud;
230 }
231
232 PCLCloud::Ptr cc2smReader::getAsSM() const
233 {
234 if (!m_ccCloud)
235 {
236 assert(false);
237 return {};
238 }
239
240 //list of scalar fields
241 QStringList scalarFields;
242 for (unsigned i = 0; i < m_ccCloud->getNumberOfScalarFields(); ++i)
243 {
244 scalarFields << m_ccCloud->getScalarField(static_cast<int>(i))->getName();
245 }
246
247 return getAsSM(true, m_ccCloud->hasNormals(), m_ccCloud->hasColors(), scalarFields);
248 }
249
250 static PCLCloud::Ptr SetOrAdd(PCLCloud::Ptr firstCloud, PCLCloud::Ptr secondCloud)
251 {
252 if (!secondCloud)
253 {
254 assert(false);
255 return {};
256 }
257
258 if (firstCloud)
259 {
260 try
261 {
262 PCLCloud::Ptr tempCloud(new PCLCloud); //temporary cloud
263 pcl::concatenateFields(*firstCloud, *secondCloud, *tempCloud);
264 return tempCloud;
265 }
266 catch (const std::bad_alloc&)
267 {
268 ccLog::Warning("Not enough memory");
269 return nullptr;
270 }
271 }
272 else
273 {
274 return secondCloud;
275 }
276 }
277
278 PCLCloud::Ptr cc2smReader::getAsSM(bool xyz, bool normals, bool rgbColors, const QStringList& scalarFields) const
279 {
280 if (!m_ccCloud)
281 {
282 assert(false);
283 return {};
284 }
285
286 PCLCloud::Ptr outputCloud;
287
288 unsigned pointCount = m_ccCloud->size();
289
290 try
291 {
292 if (xyz)
293 {
294 PCLCloud::Ptr xyzCloud = getXYZ();
295 if (!xyzCloud)
296 {
297 return {};
298 }
299
300 outputCloud = SetOrAdd(outputCloud, xyzCloud);
301 }
302
303 if (normals && m_ccCloud->hasNormals())
304 {
305 PCLCloud::Ptr normalsCloud = getNormals();
306 if (!normalsCloud)
307 {
308 return {};
309 }
310 outputCloud = SetOrAdd(outputCloud, normalsCloud);
311 }
312
313 if (rgbColors && m_ccCloud->hasColors())
314 {
315 PCLCloud::Ptr rgbCloud = getColors();
316 if (!rgbCloud)
317 {
318 return {};
319 }
320 outputCloud = SetOrAdd(outputCloud, rgbCloud);
321 }
322
323 for (const QString& sfName : scalarFields)
324 {
325 PCLCloud::Ptr sfCloud = getFloatScalarField(sfName);
326 if (!sfCloud)
327 {
328 return {};
329 }
330 outputCloud = SetOrAdd(outputCloud, sfCloud);
331 }
332 }
333 catch (...)
334 {
335 //any error (memory, etc.)
336 outputCloud.reset();
337 }
338
339 return outputCloud;
340 }
341
342 pcl::PointCloud<pcl::PointNormal>::Ptr cc2smReader::getAsPointNormal() const
343 {
344 if (!m_ccCloud)
345 {
346 assert(false);
347 return {};
348 }
349
350 PointCloud<pcl::PointNormal>::Ptr pcl_cloud(new PointCloud<pcl::PointNormal>);
351
352 unsigned pointCount = m_ccCloud->size();
353
354 try
355 {
356 pcl_cloud->resize(pointCount);
357 }
358 catch (...)
359 {
360 //any error (memory, etc.)
361 return {};
362 }
363
364 for (unsigned i = 0; i < pointCount; ++i)
365 {
366 const CCVector3* P = m_ccCloud->getPoint(i);
367 pcl_cloud->at(i).x = static_cast<float>(P->x);
368 pcl_cloud->at(i).y = static_cast<float>(P->y);
369 pcl_cloud->at(i).z = static_cast<float>(P->z);
370 }
371
372 if (m_ccCloud->hasNormals())
373 {
374 for (unsigned i = 0; i < pointCount; ++i)
375 {
376 const CCVector3* N = m_ccCloud->getNormal(i);
377 pcl_cloud->at(i).normal_x = static_cast<float>(N->x);
378 pcl_cloud->at(i).normal_y = static_cast<float>(N->y);
379 pcl_cloud->at(i).normal_z = static_cast<float>(N->z);
380 }
381 }
382
383 return pcl_cloud;
384 }