Debian Codegraph @

plugins/core/Standard/qCanupo/src/qCanupo.cpp

raw
1 //##########################################################################
2 //# #
3 //# CLOUDCOMPARE PLUGIN: qCANUPO #
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: UEB (UNIVERSITE EUROPEENNE DE BRETAGNE) / CNRS #
15 //# #
16 //##########################################################################
17
18 #include "qCanupo.h"
19
20 //local
21 #include "qCanupoClassifDialog.h"
22 #include "qCanupoTrainingDialog.h"
23 #include "qCanupo2DViewDialog.h"
24 #include "qCanupoTools.h"
25 #include "qCanupoDisclaimerDialog.h"
26 #include "qCanupoCommands.h"
27
28 //CCCoreLib
29 #include <CloudSamplingTools.h>
30
31 //qCC_db
32 #include <ccProgressDialog.h>
33
34
35 qCanupoPlugin::qCanupoPlugin(QObject* parent/*=nullptr*/)
36 : QObject(parent)
37 , ccStdPluginInterface( ":/CC/plugin/qCanupoPlugin/info.json" )
38 , m_classifyAction(nullptr)
39 , m_trainAction(nullptr)
40 {
41 }
42
43 void qCanupoPlugin::onNewSelection(const ccHObject::Container& selectedEntities)
44 {
45 if (m_classifyAction)
46 {
47 //classification: only one point cloud
48 m_classifyAction->setEnabled(selectedEntities.size() == 1 && selectedEntities[0]->isA(CC_TYPES::POINT_CLOUD));
49 }
50
51 if (m_trainAction)
52 {
53 m_trainAction->setEnabled(m_app && m_app->dbRootObject() && m_app->dbRootObject()->getChildrenNumber() != 0); //need some loaded entities to train the classifier!
54 }
55
56 m_selectedEntities = selectedEntities;
57 }
58
59 QList<QAction*> qCanupoPlugin::getActions()
60 {
61 QList<QAction*> group;
62
63 if (!m_trainAction)
64 {
65 m_trainAction = new QAction("Train classifier", this);
66 m_trainAction->setToolTip("Train classifier");
67 m_trainAction->setIcon(QIcon(QString::fromUtf8(":/CC/plugin/qCanupoPlugin/images/iconCreate.png")));
68 connect(m_trainAction, &QAction::triggered, this, &qCanupoPlugin::doTrainAction);
69 }
70 group.push_back(m_trainAction);
71
72 if (!m_classifyAction)
73 {
74 m_classifyAction = new QAction("Classify", this);
75 m_classifyAction->setToolTip("Classify cloud");
76 m_classifyAction->setIcon(QIcon(QString::fromUtf8(":/CC/plugin/qCanupoPlugin/images/iconClassify.png")));
77 connect(m_classifyAction, &QAction::triggered, this, &qCanupoPlugin::doClassifyAction);
78 }
79 group.push_back(m_classifyAction);
80
81 return group;
82 }
83
84 void qCanupoPlugin::doClassifyAction()
85 {
86 if (!m_app)
87 {
88 assert(false);
89 return;
90 }
91
92 //disclaimer accepted?
93 if (!ShowClassifyDisclaimer(m_app))
94 {
95 return;
96 }
97
98 if (m_selectedEntities.empty() || !m_selectedEntities.front()->isA(CC_TYPES::POINT_CLOUD))
99 {
100 m_app->dispToConsole("Select one and only one point cloud!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
101 return;
102 }
103
104 ccPointCloud* cloud = static_cast<ccPointCloud*>(m_selectedEntities.front());
105
106 //display dialog
107 qCanupoClassifDialog cDlg(cloud, m_app);
108 if (!cDlg.exec())
109 {
110 //process cancelled by the user
111 return;
112 }
113 cDlg.saveParamsToPersistentSettings();
114
115 //store parameters
116 qCanupoProcess::ClassifyParams params;
117 {
118 params.confidenceThreshold = cDlg.getConfidenceTrehshold();
119 params.generateAdditionalSF = cDlg.generateAdditionalSFsCheckBox->isChecked();
120 params.generateRoughnessSF = cDlg.generateRoughnessSFsCheckBox->isChecked();
121 params.maxThreadCount = cDlg.getMaxThreadCount();
122 params.useActiveSFForConfidence = cDlg.useSF();
123 params.samplingDist = 0; //only set if we subsample the input cloud!
124
125 }
126 QString classifierFilename = cDlg.classifFileLineEdit->text();
127
128 //how should we generate the core points?
129 qCanupoClassifDialog::CORE_CLOUD_SOURCES coreSource = cDlg.getCorePointsCloudSource();
130 PointCoordinateType samplingDist = static_cast<PointCoordinateType>(cDlg.cpSubsamplingDoubleSpinBox->value());
131
132 CorePointDescSet corePointsDescriptors; //core point descriptors
133 ccPointCloud* realCorePoints = nullptr; //the core point cloud (as a real point cloud, if available)
134 CCCoreLib::GenericIndexedCloudPersist* corePoints = nullptr; //the core points, potentially as references!
135
136 switch (coreSource)
137 {
138 case qCanupoClassifDialog::ORIGINAL:
139 case qCanupoClassifDialog::OTHER:
140 {
141 realCorePoints = cDlg.getCorePointsCloud();
142 if (!realCorePoints)
143 {
144 assert(false);
145 m_app->dispToConsole("Internal error: failed to access core pointss?!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
146 return;
147 }
148 corePoints = realCorePoints;
149 }
150 break;
151
152 case qCanupoClassifDialog::SUBSAMPLED:
153 {
154 //progress dialog
155 ccProgressDialog pDlg(true, m_app->getMainWindow());
156
157 assert(samplingDist > 0);
158 CCCoreLib::CloudSamplingTools::SFModulationParams modParams(false);
159 CCCoreLib::ReferenceCloud* refCloud = CCCoreLib::CloudSamplingTools::resampleCloudSpatially(cloud,
160 samplingDist,
161 modParams,
162 nullptr,
163 &pDlg);
164 if (!refCloud)
165 {
166 m_app->dispToConsole("Failed to compute sub-sampled core points!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
167 return;
168 }
169
170 params.samplingDist = samplingDist;
171
172 //try to convert the references into a real point cloud (not mandatory though!)
173 realCorePoints = cloud->partialClone(refCloud);
174 if (realCorePoints)
175 {
176 realCorePoints->setName(cloud->getName() + QString(".core points (subsampled @ %1)").arg(samplingDist));
177 cloud->addChild(realCorePoints);
178 m_app->addToDB(realCorePoints);
179 corePoints = realCorePoints;
180 }
181 else
182 {
183 m_app->dispToConsole("Can't save subsampled cloud (not enough memory)!");
184 }
185
186 delete refCloud;
187 refCloud = nullptr;
188 }
189 break;
190
191 case qCanupoClassifDialog::MSC_FILE:
192 {
193 realCorePoints = new ccPointCloud("MSC core points");
194 QString filenmae = cDlg.getMscFilename();
195 QString error;
196 if (!corePointsDescriptors.loadFromMSC(filenmae, error, realCorePoints))
197 {
198 //failed to read the input MSC file
199 if (realCorePoints)
200 {
201 delete realCorePoints;
202 realCorePoints = nullptr;
203 }
204 m_app->dispToConsole(error, ccMainAppInterface::ERR_CONSOLE_MESSAGE);
205 return;
206 }
207 else if (!error.isNull())
208 {
209 //then it's just a warning
210 m_app->dispToConsole(QString("[qCanupo] ") + error, ccMainAppInterface::WRN_CONSOLE_MESSAGE);
211 }
212
213 assert(realCorePoints);
214 cloud->addChild(realCorePoints);
215 m_app->addToDB(realCorePoints);
216 corePoints = realCorePoints;
217 }
218 break;
219
220 default:
221 {
222 assert(false);
223 m_app->dispToConsole("Internal error: no core point source specified?!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
224 return;
225 }
226 }
227
228 assert(corePoints);
229
230 if (qCanupoProcess::Classify(classifierFilename, params, cloud, corePoints, corePointsDescriptors, realCorePoints, m_app, m_app->getMainWindow()))
231 {
232 cloud->prepareDisplayForRefresh();
233 m_app->refreshAll();
234 m_app->updateUI();
235 }
236
237 //dispose of the 'virtual' core points (if any)
238 if (corePoints != realCorePoints)
239 {
240 delete corePoints;
241 corePoints = nullptr;
242 }
243 }
244
245 void qCanupoPlugin::doTrainAction()
246 {
247 //disclaimer accepted?
248 if (!ShowTrainDisclaimer(m_app))
249 return;
250
251 //if (m_selectedEntities.size() != 2
252 // || !m_selectedEntities[0]->isA(CC_TYPES::POINT_CLOUD)
253 // || !m_selectedEntities[1]->isA(CC_TYPES::POINT_CLOUD))
254 //{
255 // m_app->dispToConsole("Select two point clouds!",ccMainAppInterface::ERR_CONSOLE_MESSAGE);
256 // return;
257 //}
258 //
259 //ccPointCloud* cloud1 = static_cast<ccPointCloud*>(m_selectedEntities[0]);
260 //ccPointCloud* cloud2 = static_cast<ccPointCloud*>(m_selectedEntities[1]);
261
262 //display dialog
263 qCanupoTrainingDialog ctDlg(/*cloud1, cloud2, */m_app);
264 if (!ctDlg.exec())
265 return;
266 ctDlg.saveParamsToPersistentSettings();
267
268 //get scales
269 std::vector<float> scales;
270 {
271 if (!ctDlg.getScales(scales))
272 {
273 m_app->dispToConsole("Invalid scale parameters!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
274 return;
275 }
276 //make sure values are in descending order!
277 std::sort(scales.begin(), scales.end(), std::greater<float>());
278 }
279
280 ccPointCloud* originCloud = ctDlg.getOriginPointCloud();
281 ccPointCloud* cloud1 = ctDlg.getClass1Cloud();
282 ccPointCloud* cloud2 = ctDlg.getClass2Cloud();
283 if (!cloud1 || !cloud2)
284 {
285 if (m_app)
286 m_app->dispToConsole("At least one cloud (class #1 or #2) was not defined!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
287 return;
288 }
289
290 assert(cloud1 != cloud2);
291 ccPointCloud* evaluationCloud = ctDlg.getEvaluationCloud();
292
293 //Descriptor ID
294 unsigned descriptorID = ctDlg.getDescriptorID();
295 //check that the selected descriptor (computer) is valid
296 {
297 assert(descriptorID != 0);
298 ScaleParamsComputer* computer = ScaleParamsComputer::GetByID(descriptorID);
299 if (!computer)
300 {
301 if (m_app)
302 m_app->dispToConsole(QString("Internal error: unhandled descriptor ID (%1)!").arg(descriptorID), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
303 return;
304 }
305 if (computer->needSF()
306 && (cloud1->getCurrentDisplayedScalarField() == nullptr
307 || cloud2->getCurrentDisplayedScalarField() == nullptr
308 || (evaluationCloud && evaluationCloud->getCurrentDisplayedScalarField() == nullptr)
309 )
310 )
311 {
312 if (m_app)
313 m_app->dispToConsole(QString("To compute this type of descriptor, all clouds must have an active scalar field!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
314 return;
315 }
316 }
317
318 //sub-sampled clouds
319 CCCoreLib::GenericIndexedCloudPersist* corePoints1 = nullptr;
320 CCCoreLib::GenericIndexedCloudPersist* corePoints2 = nullptr;
321 CCCoreLib::GenericIndexedCloudPersist* evaluationPoints = nullptr;
322
323 //progress dialog
324 ccProgressDialog pDlg(true, m_app->getMainWindow());
325
326 while (true)
327 {
328 //sub-sample clouds (if necessary)
329 {
330 assert(ctDlg.maxPointsSpinBox->value() > 0);
331 unsigned maxCorePoints = static_cast<unsigned>(ctDlg.maxPointsSpinBox->value());
332
333 //if the user has specified a third cloud for behavior representation
334 if (evaluationCloud)
335 {
336 if (evaluationCloud->size() > maxCorePoints)
337 evaluationPoints = CCCoreLib::CloudSamplingTools::subsampleCloudRandomly(evaluationCloud, maxCorePoints, &pDlg);
338 else
339 evaluationPoints = evaluationCloud;
340
341 if (!evaluationPoints)
342 {
343 m_app->dispToConsole("Failed to compute sub-sampled version of evaluation points!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
344 break;
345 }
346 }
347
348 //for class clouds, we take the smallest count (if inferior to the specified limit)
349 if (maxCorePoints > cloud1->size())
350 maxCorePoints = cloud1->size();
351 if (maxCorePoints > cloud2->size())
352 maxCorePoints = cloud2->size();
353
354 if (cloud1->size() > maxCorePoints)
355 corePoints1 = CCCoreLib::CloudSamplingTools::subsampleCloudRandomly(cloud1, maxCorePoints, &pDlg);
356 else
357 corePoints1 = cloud1;
358
359 if (!corePoints1)
360 {
361 m_app->dispToConsole("Failed to compute sub-sampled version of cloud #1!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
362 break;
363 }
364
365 if (cloud2->size() > maxCorePoints)
366 corePoints2 = CCCoreLib::CloudSamplingTools::subsampleCloudRandomly(cloud2, maxCorePoints, &pDlg);
367 else
368 corePoints2 = cloud2;
369
370 if (!corePoints2)
371 {
372 m_app->dispToConsole("Failed to compute sub-sampled version of cloud #1!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
373 break;
374 }
375 }
376
377 //compute MSC data for cloud #1
378 CorePointDescSet descriptors1;
379 {
380 bool invalidDescriptors = false;
381 QString errorStr;
382 if (!qCanupoTools::ComputeCorePointsDescriptors(corePoints1,
383 descriptors1,
384 //if the origin cloud was specified, then we'll use it as base cloud for descriptors
385 originCloud ? originCloud : cloud1,
386 scales,
387 invalidDescriptors,
388 errorStr,
389 descriptorID,
390 ctDlg.getMaxThreadCount(),
391 &pDlg/*,
392 octree*/))
393 {
394 m_app->dispToConsole(QString("Failed to compute core points descriptors: %1").arg(errorStr), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
395 break;
396 }
397 else if (invalidDescriptors)
398 {
399 m_app->dispToConsole("[qCanupo] Some descriptors couldn't be computed on cloud#1 (min scale may be too small)!", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
400 }
401 }
402
403 //compute MSC data for cloud #2
404 CorePointDescSet descriptors2;
405 {
406 bool invalidDescriptors = false;
407 QString errorStr;
408 if (!qCanupoTools::ComputeCorePointsDescriptors(corePoints2,
409 descriptors2,
410 //if the origin cloud was specified, then we'll use it as base cloud for descriptors
411 originCloud ? originCloud : cloud2,
412 scales,
413 invalidDescriptors,
414 errorStr,
415 descriptorID,
416 ctDlg.getMaxThreadCount(),
417 &pDlg/*,
418 octree*/))
419 {
420 m_app->dispToConsole(QString("Failed to compute core points descriptors: %1").arg(errorStr), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
421 break;
422 }
423 else if (invalidDescriptors)
424 {
425 m_app->dispToConsole("[qCanupo] Some descriptors couldn't be computed on cloud#2 (min scale may be too small)!", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
426 }
427 }
428
429 //if the user has specified a third cloud for behavior representation
430 //we must compute its descriptors now
431 CorePointDescSet evaluationDescriptors;
432 if (evaluationPoints)
433 {
434 //computes the 'descriptors'
435 bool invalidDescriptors = false;
436 QString errorStr;
437 if (!qCanupoTools::ComputeCorePointsDescriptors(evaluationPoints,
438 evaluationDescriptors,
439 evaluationCloud,
440 scales,
441 invalidDescriptors,
442 errorStr,
443 descriptorID,
444 ctDlg.getMaxThreadCount(),
445 &pDlg))
446 {
447 m_app->dispToConsole(QString("Failed to compute core points descriptors: %1").arg(errorStr), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
448 break;
449 }
450
451 if (invalidDescriptors)
452 {
453 m_app->dispToConsole("[qCanupo] Some descriptors couldn't be computed on evaluation cloud (min scale may be too small)!", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
454 }
455 }
456
457 //now for the Classifier training!
458 {
459 qCanupo2DViewDialog c2DDlg(&descriptors1,
460 &descriptors2,
461 cloud1->getName(),
462 cloud2->getName(),
463 ctDlg.cloud1ClassSpinBox->value(),
464 ctDlg.cloud2ClassSpinBox->value(),
465 evaluationPoints ? &evaluationDescriptors : nullptr,
466 m_app);
467
468 //we need the 3D view to be visible before updating the zoom!
469 c2DDlg.show();
470 c2DDlg.setWindowModality(Qt::ApplicationModal);
471 QApplication::processEvents();
472
473 if (!c2DDlg.trainClassifier())
474 {
475 //a real error message should have already been issued
476 m_app->dispToConsole("[qCanupo] Classifier training failed...", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
477 break;
478 }
479
480 c2DDlg.exec();
481 }
482
483 //end of the story!
484 break;
485 }
486
487 if (corePoints1 != cloud1)
488 delete corePoints1;
489 if (corePoints2 != cloud2)
490 delete corePoints2;
491 if (evaluationPoints != evaluationCloud)
492 delete evaluationPoints;
493 }
494
495 void qCanupoPlugin::registerCommands(ccCommandLineInterface* cmd)
496 {
497 if (!cmd)
498 {
499 assert(false);
500 return;
501 }
502 cmd->registerCommand(ccCommandLineInterface::Command::Shared(new CommandCanupoClassif));
503 }