Debian Codegraph @

plugins/core/Standard/qRANSAC_SD/include/qRANSAC_SD_Commands.h

raw
1 //##########################################################################
2 //# #
3 //# CLOUDCOMPARE PLUGIN: qRANSAC_SD #
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: Chris S Brown #
15 //# #
16 //##########################################################################
17
18 #ifndef RANSAC_PLUGIN_COMMANDS_HEADER
19 #define RANSAC_PLUGIN_COMMANDS_HEADER
20
21 #include <QDir>
22
23 //CloudCompare
24 #include "ccCommandLineInterface.h"
25 #include <ccMesh.h>
26 #include <ccGenericMesh.h>
27
28 //Local
29 #include "qRANSAC_SD.h"
30
31 constexpr char COMMAND_RANSAC[] = "RANSAC";
32 constexpr char EPSILON_ABSOLUTE[] = "EPSILON_ABSOLUTE";
33 constexpr char EPSILON_PERCENTAGE_OF_SCALE[] = "EPSILON_PERCENTAGE_OF_SCALE";
34 constexpr char BITMAP_EPSILON_PERCENTAGE_OF_SCALE[] = "BITMAP_EPSILON_PERCENTAGE_OF_SCALE";
35 constexpr char BITMAP_EPSILON_ABSOLUTE[] = "BITMAP_EPSILON_ABSOLUTE";
36 constexpr char SUPPORT_POINTS[] = "SUPPORT_POINTS";
37 constexpr char MAX_NORMAL_DEV[] = "MAX_NORMAL_DEV";
38 constexpr char PROBABILITY[] = "PROBABILITY";
39 constexpr char ENABLE_PRIMITIVE[] = "ENABLE_PRIMITIVE";
40 constexpr char OUT_CLOUD_DIR[] = "OUT_CLOUD_DIR";
41 constexpr char OUT_MESH_DIR[] = "OUT_MESH_DIR";
42 constexpr char OUT_PAIR_DIR[] = "OUT_PAIR_DIR";
43 constexpr char OUT_GROUP_DIR[] = "OUT_GROUP_DIR";
44 constexpr char OUT_RANDOM_COLOR[] = "OUT_RANDOM_COLOR";
45 constexpr char OUTPUT_INDIVIDUAL_PRIMITIVES[] = "OUTPUT_INDIVIDUAL_PRIMITIVES";
46 constexpr char OUTPUT_INDIVIDUAL_SUBCLOUDS[] = "OUTPUT_INDIVIDUAL_SUBCLOUDS";
47 constexpr char OUTPUT_INDIVIDUAL_PAIRED_CLOUD_PRIMITIVE[] = "OUTPUT_INDIVIDUAL_PAIRED_CLOUD_PRIMITIVE";
48 constexpr char OUTPUT_GROUPED[] = "OUTPUT_GROUPED";
49
50 constexpr char PRIM_PLANE[] = "PLANE";
51 constexpr char PRIM_SPHERE[] = "SPHERE";
52 constexpr char PRIM_CYLINDER[] = "CYLINDER";
53 constexpr char PRIM_CONE[] = "CONE";
54 constexpr char PRIM_TORUS[] = "TORUS";
55
56
57 struct CommandRANSAC : public ccCommandLineInterface::Command
58 {
59 CommandRANSAC() : ccCommandLineInterface::Command(QObject::tr("RANSAC"), COMMAND_RANSAC) {}
60
61 virtual bool process(ccCommandLineInterface& cmd) override
62 {
63 if (cmd.clouds().empty())
64 {
65 return cmd.error(QObject::tr("No point cloud to attempt RANSAC on (be sure to open one with \"-O [cloud filename]\" before \"-%2\")").arg(COMMAND_RANSAC));
66 }
67 qRansacSD::RansacParams params;
68 QStringList paramNames = QStringList() << EPSILON_ABSOLUTE << EPSILON_PERCENTAGE_OF_SCALE <<
69 BITMAP_EPSILON_PERCENTAGE_OF_SCALE << BITMAP_EPSILON_ABSOLUTE <<
70 SUPPORT_POINTS << MAX_NORMAL_DEV << PROBABILITY << ENABLE_PRIMITIVE <<
71 OUT_CLOUD_DIR << OUT_MESH_DIR << OUT_GROUP_DIR << OUT_PAIR_DIR << OUT_RANDOM_COLOR << OUTPUT_INDIVIDUAL_PRIMITIVES <<
72 OUTPUT_INDIVIDUAL_SUBCLOUDS << OUTPUT_GROUPED << OUTPUT_INDIVIDUAL_PAIRED_CLOUD_PRIMITIVE;
73 QStringList primitiveNames = QStringList() << PRIM_PLANE << PRIM_SPHERE << PRIM_CYLINDER << PRIM_CONE << PRIM_TORUS;
74 QString outputCloudsDir;
75 QString outputMeshesDir;
76 QString outputPairDir;
77 QString outputGroupDir;
78 bool outputIndividualClouds = false;
79 bool outputIndividualPrimitives = false;
80 bool outputIndividualPairs = false;
81 bool outputGrouped = false;
82
83 float epsilonABS = -1.0f;
84 float epsilonPercentage = -1.0f;
85 float bitmapEpsilonABS = -1.0f;
86 float bitmapEpsilonPercentage = -1.0f;
87 params.epsilon = -1.0f;
88 params.bitmapEpsilon = -1.0f;
89 params.randomColor = false;
90
91 for (unsigned char k = 0; k < 5; ++k)
92 {
93 params.primEnabled[k] = false;
94 }
95
96 if (!cmd.arguments().empty())
97 {
98
99 QString param = cmd.arguments().takeFirst().toUpper();
100 while (paramNames.contains(param))
101 {
102 cmd.print(QObject::tr("\t%1 : %2").arg(COMMAND_RANSAC, param));
103 if (param == EPSILON_ABSOLUTE)
104 {
105 if (cmd.arguments().empty())
106 {
107 return cmd.error(QObject::tr("Missing parameter: number after \"-%1 %2\"").arg(COMMAND_RANSAC, EPSILON_ABSOLUTE));
108 }
109 bool ok;
110 float val = cmd.arguments().takeFirst().toFloat(&ok);
111 if (!ok)
112 {
113 return cmd.error("Invalid number for epsilon!");
114 }
115 cmd.print(QObject::tr("\tEpsilon : %1").arg(val));
116 epsilonABS = val;
117 }
118 else if (param == EPSILON_PERCENTAGE_OF_SCALE)
119 {
120 if (cmd.arguments().empty())
121 {
122 return cmd.error(QObject::tr("Missing parameter: number after \"-%1 %2\"").arg(COMMAND_RANSAC, EPSILON_PERCENTAGE_OF_SCALE));
123 }
124 bool ok;
125 float val = cmd.arguments().takeFirst().toFloat(&ok);
126 if (!ok || (val <= 0.0f || val >= 1.0f))
127 {
128 return cmd.error("Invalid number for epsilon percentage must be a float greater than 0.0 and less than 1.0!");
129 }
130 cmd.print(QObject::tr("\tEpsilon : %1").arg(val));
131 epsilonPercentage = val;
132 }
133 else if (param == BITMAP_EPSILON_ABSOLUTE)
134 {
135 if (cmd.arguments().empty())
136 {
137 return cmd.error(QObject::tr("Missing parameter: number after \"-%1 %2\"").arg(COMMAND_RANSAC, BITMAP_EPSILON_ABSOLUTE));
138 }
139 bool ok;
140 float val = cmd.arguments().takeFirst().toFloat(&ok);
141 if (!ok)
142 {
143 return cmd.error("Invalid number for Bitmap epsilon!");
144 }
145 cmd.print(QObject::tr("\tBitmap Epsilon : %1").arg(val));
146 bitmapEpsilonABS = val;
147 }
148 else if (param == BITMAP_EPSILON_PERCENTAGE_OF_SCALE)
149 {
150 if (cmd.arguments().empty())
151 {
152 return cmd.error(QObject::tr("Missing parameter: number after \"-%1 %2\"").arg(COMMAND_RANSAC, BITMAP_EPSILON_PERCENTAGE_OF_SCALE));
153 }
154 bool ok;
155 float val = cmd.arguments().takeFirst().toFloat(&ok);
156 if (!ok || (val <= 0.0f || val >= 1.0f))
157 {
158 return cmd.error("Invalid number for Bitmap epsilon must be a float greater than 0.0 and less than 1.0!!");
159 }
160 cmd.print(QObject::tr("\tBitmap Epsilon : %1").arg(val));
161 bitmapEpsilonPercentage = val;
162 }
163 else if (param == SUPPORT_POINTS)
164 {
165 if (cmd.arguments().empty())
166 {
167 return cmd.error(QObject::tr("Missing parameter: number after \"-%1 %2\"").arg(COMMAND_RANSAC, SUPPORT_POINTS));
168 }
169 bool ok;
170 unsigned count = cmd.arguments().takeFirst().toUInt(&ok);
171 if (!ok)
172 {
173 return cmd.error("Invalid number of for support points!");
174 }
175 cmd.print(QObject::tr("\tsupport points: %1").arg(count));
176 params.supportPoints = count;
177 }
178 else if (param == MAX_NORMAL_DEV)
179 {
180 if (cmd.arguments().empty())
181 {
182 return cmd.error(QObject::tr("Missing parameter: number after \"-%1 %2\"").arg(COMMAND_RANSAC, MAX_NORMAL_DEV));
183 }
184 bool ok;
185 float val = cmd.arguments().takeFirst().toFloat(&ok);
186 if (!ok)
187 {
188 return cmd.error("Invalid number for Max Normal Deviation!");
189 }
190 cmd.print(QObject::tr("\tMax Normal Deviation : %1").arg(val));
191 params.maxNormalDev_deg = val;
192 }
193 else if (param == PROBABILITY)
194 {
195 if (cmd.arguments().empty())
196 {
197 return cmd.error(QObject::tr("Missing parameter: number after \"-%1 %2\"").arg(COMMAND_RANSAC, PROBABILITY));
198 }
199 bool ok;
200 float val = cmd.arguments().takeFirst().toFloat(&ok);
201 if (!ok)
202 {
203 return cmd.error("Invalid number for Probability!");
204 }
205 cmd.print(QObject::tr("\tProbability : %1").arg(val));
206 params.probability = val;
207 }
208 else if (param == OUT_RANDOM_COLOR)
209 {
210 params.randomColor = true;
211 }
212 else if (param == OUT_CLOUD_DIR)
213 {
214 if (!makePathIfPossible(cmd, param, &outputCloudsDir, &outputIndividualClouds))
215 {
216 return false;
217 }
218 }
219 else if (param == OUT_MESH_DIR)
220 {
221 if (!makePathIfPossible(cmd, param, &outputMeshesDir, &outputIndividualPrimitives))
222 {
223 return false;
224 }
225 }
226 else if (param == OUT_GROUP_DIR)
227 {
228 if (!makePathIfPossible(cmd, param, &outputGroupDir, &outputGrouped))
229 {
230 return false;
231 }
232 }
233 else if (param == OUT_PAIR_DIR)
234 {
235 if (!makePathIfPossible(cmd, param, &outputPairDir, &outputIndividualPairs))
236 {
237 return false;
238 }
239 }
240 else if (param == OUTPUT_INDIVIDUAL_SUBCLOUDS)
241 {
242 outputIndividualClouds = true;
243 }
244 else if (param == OUTPUT_INDIVIDUAL_PRIMITIVES)
245 {
246 outputIndividualPrimitives = true;
247 }
248 else if (param == OUTPUT_INDIVIDUAL_PAIRED_CLOUD_PRIMITIVE)
249 {
250 outputIndividualPairs = true;
251 }
252 else if (param == OUTPUT_GROUPED)
253 {
254 outputGrouped = true;
255 }
256 else if (param == ENABLE_PRIMITIVE)
257 {
258 if (cmd.arguments().empty())
259 {
260 return cmd.error(QObject::tr("Missing parameter: primitive type after \"-%1 %2\"").arg(COMMAND_RANSAC, ENABLE_PRIMITIVE));
261 }
262 QString prim = cmd.arguments().takeFirst().toUpper();
263 unsigned primCount = 0;
264 while (primitiveNames.contains(prim))
265 {
266 if (prim == PRIM_PLANE)
267 {
268 params.primEnabled[qRansacSD::RPT_PLANE] = true;
269 primCount++;
270 }
271 else if (prim == PRIM_SPHERE)
272 {
273 params.primEnabled[qRansacSD::RPT_SPHERE] = true;
274 primCount++;
275 }
276 else if (prim == PRIM_CYLINDER)
277 {
278 params.primEnabled[qRansacSD::RPT_CYLINDER] = true;
279 primCount++;
280 }
281 else if (prim == PRIM_CONE)
282 {
283 params.primEnabled[qRansacSD::RPT_CONE] = true;
284 primCount++;
285 }
286 else if (prim == PRIM_TORUS)
287 {
288 params.primEnabled[qRansacSD::RPT_TORUS] = true;
289 primCount++;
290 }
291 if (cmd.arguments().empty())
292 {
293 break;
294 }
295 prim = cmd.arguments().takeFirst().toUpper();
296 }
297 if (primCount > 0)
298 {
299 if (!primitiveNames.contains(prim))
300 {
301 cmd.arguments().push_front(prim);
302 }
303 }
304 else
305 {
306 return cmd.error(QObject::tr("No valid parameter: primitive type after \"-%1 %2\"").arg(COMMAND_RANSAC, ENABLE_PRIMITIVE));
307 }
308 }
309 if (cmd.arguments().empty())
310 {
311 break;
312 }
313 param = cmd.arguments().takeFirst().toUpper();
314 }
315 if (!paramNames.contains(param))
316 {
317 cmd.arguments().push_front(param);
318 }
319 }
320 unsigned char primCount = 0;
321 for (unsigned char k = 0; k < 5; ++k)
322 {
323 primCount += static_cast<unsigned>(params.primEnabled[k]);
324 }
325 if (primCount == 0)
326 {
327 cmd.print(QObject::tr("\tDefault Shape Search == %1").arg(PRIM_PLANE));
328 params.primEnabled[qRansacSD::RPT_PLANE] = true;
329 }
330 if (!outputIndividualClouds && !outputIndividualPrimitives && !outputGrouped && !outputIndividualPairs)
331 {
332 cmd.print(QObject::tr("\tDefault output == %1").arg(OUTPUT_GROUPED));
333 outputGrouped = true;
334 }
335
336 for (CLCloudDesc clCloud : cmd.clouds())
337 {
338
339 CCVector3 bbMin, bbMax;
340 clCloud.pc->getBoundingBox(bbMin, bbMax);
341 CCVector3 diff = bbMax - bbMin;
342 float scale = std::max(std::max(diff[0], diff[1]), diff[2]);
343 if (epsilonPercentage > 0.0f)
344 {
345 params.epsilon = (epsilonPercentage * scale);
346 }
347 if (bitmapEpsilonPercentage > 0.0f)
348 {
349 params.bitmapEpsilon = (bitmapEpsilonPercentage * scale);
350 }
351 if (epsilonABS > 0.0f)
352 {
353 params.epsilon = epsilonABS;
354 }
355 if (bitmapEpsilonABS > 0.0f)
356 {
357 params.bitmapEpsilon = bitmapEpsilonABS;
358 }
359 if (params.epsilon < 0.0f)
360 {
361 params.epsilon = (0.005f * scale);
362 }
363 if (params.bitmapEpsilon < 0.0f)
364 {
365 params.bitmapEpsilon = (0.01f * scale);
366 }
367
368 ccHObject* group = qRansacSD::executeRANSAC(clCloud.pc, params, cmd.silentMode());
369
370 if (group)
371 {
372 if (outputGrouped)
373 {
374 CLGroupDesc clGroup(group, clCloud.basename + "_" + clCloud.pc->getName() + "_RANSAC_DETECTED_SHAPES", outputGroupDir != "" ? outputGroupDir : clCloud.path);
375 QString errorStr = cmd.exportEntity(clGroup, QString(), nullptr, ccCommandLineInterface::ExportOption::ForceHierarchy);
376 if (!errorStr.isEmpty())
377 {
378 cmd.warning(errorStr);
379 }
380 }
381 if (outputIndividualPrimitives || outputIndividualClouds || outputIndividualPairs)
382 {
383 ccHObject::Container meshGroup;
384 unsigned meshCount = group->filterChildren(meshGroup, true, CC_TYPES::MESH);
385 unsigned planeCount = 1;
386 unsigned sphereCount = 1;
387 unsigned cylinderCount = 1;
388 unsigned coneCount = 1;
389 unsigned torusCount = 1;
390
391 for (auto meshObj : meshGroup)
392 {
393 auto mesh = ccHObjectCaster::ToMesh(meshObj);
394 if (mesh)
395 {
396 QString suffix;
397 if (meshObj->isA(CC_TYPES::PLANE))
398 {
399 suffix = QString("_PLANE_%1").arg(planeCount, 4, 10, QChar('0'));
400 planeCount++;
401 }
402 else if (meshObj->isA(CC_TYPES::SPHERE))
403 {
404 suffix = QString("_SPHERE_%1").arg(sphereCount, 4, 10, QChar('0'));
405 sphereCount++;
406 }
407 else if (meshObj->isA(CC_TYPES::CYLINDER))
408 {
409 suffix = QString("_CYLINDER_%1").arg(cylinderCount, 4, 10, QChar('0'));
410 cylinderCount++;
411 }
412 else if (meshObj->isA(CC_TYPES::CONE))
413 {
414 suffix = QString("_CONE_%1").arg(coneCount, 4, 10, QChar('0'));
415 coneCount++;
416 }
417 else if (meshObj->isA(CC_TYPES::TORUS))
418 {
419 suffix = QString("_TORUS_%1").arg(torusCount, 4, 10, QChar('0'));
420 torusCount++;
421 }
422 auto cld = ccHObjectCaster::ToPointCloud(mesh->getParent());
423 if (outputIndividualPairs)
424 {
425 CLGroupDesc clPair(cld, clCloud.basename + "_" + clCloud.pc->getName() + suffix + QString("_pair"), outputPairDir.isEmpty() ? clCloud.path : outputPairDir);
426 QString errorStr = cmd.exportEntity(clPair, QString(), nullptr, ccCommandLineInterface::ExportOption::ForceHierarchy);
427 if (!errorStr.isEmpty())
428 {
429 cmd.warning(errorStr);
430 }
431 }
432 if (cld)
433 {
434 cld->detachChild(mesh);
435 }
436 CLCloudDesc clCloudp(cld, clCloud.basename + "_" + clCloud.pc->getName() + suffix + QString("_cloud"), outputCloudsDir.isEmpty() ? clCloud.path : outputCloudsDir);
437 cmd.clouds().push_back(clCloudp);
438 CLMeshDesc clMesh(mesh, clCloud.basename + "_" + clCloud.pc->getName() + suffix, outputMeshesDir != "" ? outputMeshesDir : clCloud.path);
439 cmd.meshes().push_back(clMesh);
440 if (outputIndividualClouds)
441 {
442 QString errorStr = cmd.exportEntity(clCloudp);
443 if (!errorStr.isEmpty())
444 {
445 cmd.warning(errorStr);
446 }
447 }
448 if (outputIndividualPrimitives)
449 {
450 QString errorStr = cmd.exportEntity(clMesh);
451 if (!errorStr.isEmpty())
452 {
453 cmd.warning(errorStr);
454 }
455 }
456 }
457 }
458 }
459 }
460 if (cmd.autoSaveMode())
461 {
462 QString errorStr = cmd.exportEntity(clCloud); // The original cloud may have had normals added
463 if (!errorStr.isEmpty())
464 {
465 cmd.warning(errorStr);
466 }
467 }
468 }
469
470
471 return true;
472 }
473
474 bool makePathIfPossible(ccCommandLineInterface& cmd, const QString &param, QString *outputPath, bool *performOutput)
475 {
476 if (cmd.arguments().empty())
477 {
478 return cmd.error(QObject::tr("\nMissing parameter: Directory after \"-%1 %2\"").arg(COMMAND_RANSAC, param));
479 }
480 QString arg = cmd.arguments().takeFirst();
481 QDir dir(arg);
482 bool pathExists = dir.exists();
483 if (!pathExists)
484 {
485 cmd.print(QObject::tr("\n%1 Does not exist\tcreating path").arg(arg));
486 pathExists = dir.mkpath(arg);
487 }
488 if (pathExists)
489 {
490 *outputPath = dir.cleanPath(arg);
491 cmd.print(QObject::tr("\t%1 : %2").arg(param, *outputPath));
492 *performOutput = true;
493 }
494 else
495 {
496 cmd.print(QObject::tr("\n%1 path could not be created, skipping %2").arg(arg, param));
497 }
498 return true;
499 }
500
501 };
502
503 #endif //RANSAC_PLUGIN_COMMANDS_HEADER