Debian Codegraph @

plugins/core/Standard/qCanupo/src/qCanupoProcess.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 "qCanupoProcess.h"
19
20 //local
21 #include "qCanupoTools.h"
22
23 //CloudCompare
24 #include <ccMainAppInterface.h>
25
26 //CCCoreLib
27 #include <CloudSamplingTools.h>
28 #include <ReferenceCloud.h>
29
30 //qCC_db
31 #include <ccOctree.h>
32 #include <ccOctreeProxy.h>
33 #include <ccPointCloud.h>
34 #include <ccProgressDialog.h>
35 #include <ccScalarField.h>
36
37 //Qt
38 #include <QApplication>
39 #include <QMessageBox>
40 #include <QStringList>
41
42 // Default SF names
43 #ifdef COMPILE_PRIVATE_CANUPO
44 static const char CANUPO_PER_LEVEL_ROUGHNESS_SF_NAME[] = "CANUPO.roughness";
45 #endif
46 static const char CANUPO_PER_LEVEL_ADDITIONAL_SF_NAME[] = "CANUPO.(x-y)";
47
48 //Reserved name for CANUPO 'MSC' meta-data
49 static const char s_canupoMSCMetaData[] = "CanupoMSCData";
50
51 //Tries to refine the classification (returns the new confidence if successful)
52 static float RefinePointClassif(const Classifier& classifier,
53 const float confidence,
54 float& distToBoundary,
55 ccPointCloud* cloud,
56 ccOctree* octree,
57 unsigned char octreeLevel,
58 CCCoreLib::GenericIndexedCloudPersist* corePoints,
59 CCCoreLib::DgmOctree* corePointsOctree,
60 unsigned char coreOctreeLevel,
61 unsigned coreIndex,
62 PointCoordinateType largestRadius,
63 const std::vector<int>& corePointClasses
64 )
65 {
66 CCCoreLib::ScalarField* sf = cloud->getCurrentDisplayedScalarField();
67 if (!sf)
68 {
69 assert(false);
70 return confidence;
71 }
72
73 try
74 {
75 // find all scene data around that core point
76 CCCoreLib::DgmOctree::NeighboursSet neighbors;
77 int n = octree->getPointsInSphericalNeighbourhood(*corePoints->getPoint(coreIndex),
78 largestRadius, //we use the biggest neighborhood
79 neighbors,
80 octreeLevel);
81
82 // for each scene data point, find the corresponding core point and check if it is reliable
83 std::vector<ScalarType> class1SFValues;
84 std::vector<ScalarType> class2SFValues;
85 for (int j = 0; j < n; ++j)
86 {
87 unsigned currentPointIndex = neighbors[j].pointIndex;
88 //look for the nearest 'core point' neighbor
89 unsigned nearestCoreIndex = 0;
90 if (corePoints == cloud)
91 {
92 //if we used the whole cloud as core points, then each point is a core point!
93 nearestCoreIndex = currentPointIndex;
94 }
95 else
96 {
97 double maxSquareDist = 0;
98 CCCoreLib::ReferenceCloud Yk(corePoints);
99 if (corePointsOctree->findPointNeighbourhood(cloud->getPoint( currentPointIndex),
100 &Yk,
101 1,
102 coreOctreeLevel,
103 maxSquareDist) == 1)
104 {
105 nearestCoreIndex = Yk.getPointGlobalIndex(0);
106 }
107 else
108 {
109 assert(false);
110 continue;
111 }
112 }
113
114 int corePointClass = corePointClasses[nearestCoreIndex];
115 if (corePointClass != -1)
116 {
117 const ScalarType& sfValue = sf->getValue(currentPointIndex);
118 if (corePointClass == classifier.class1)
119 class1SFValues.push_back(sfValue);
120 else if (corePointClass == classifier.class2)
121 class2SFValues.push_back(sfValue);
122 // else the extra info is irrelevant for this classifier pair
123 }
124 }
125
126 // some local info? TODO: min size for considering this information is reliable?
127 size_t nsamples = class1SFValues.size() + class2SFValues.size();
128 if (nsamples == 0)
129 {
130 //nothing to do
131 return confidence;
132 }
133
134 // only one class?
135 if (class1SFValues.empty())
136 {
137 distToBoundary = static_cast<float>(class2SFValues.size()) / n;
138 if (distToBoundary < 0.5f)
139 {
140 //too close
141 return confidence;
142 }
143 }
144 else if (class2SFValues.empty())
145 {
146 distToBoundary = -static_cast<float>(class1SFValues.size()) / n; //dist(class 1) < 0
147 if (distToBoundary > -0.5f)
148 {
149 //too close
150 return confidence;
151 }
152 }
153 else
154 {
155 std::sort(class1SFValues.begin(), class1SFValues.end());
156 std::sort(class2SFValues.begin(), class2SFValues.end());
157
158 std::vector<ScalarType>* smallestSet = &class1SFValues;
159 std::vector<ScalarType>* largestSet = &class2SFValues;
160
161 if (class1SFValues.size() >= class2SFValues.size())
162 {
163 std::swap(smallestSet, largestSet);
164 }
165
166 std::vector<ScalarType> bestSplit;
167 std::vector<int> bestSplitDir;
168 float bestConfidence = -1.0f;
169
170 for (size_t k = 0; k < smallestSet->size(); ++k)
171 {
172 size_t nsbelow = 0;
173 size_t nlabove = 0;
174
175 // dichomed is the first index so that largestSet[dichomed] >= smallestSet[k]...
176 size_t dichomed = std::upper_bound(largestSet->begin(), largestSet->end(), smallestSet->at(k)) - largestSet->begin();
177 if (dichomed == 0)
178 {
179 nlabove = largestSet->size();
180 nsbelow = k + 1;
181 }
182 else
183 {
184 nlabove = largestSet->size() - dichomed;
185 nsbelow = k;
186 }
187
188 // classification on either side, take largest and reverse roles if necessary
189 float c1 = static_cast<float>(nlabove) / largestSet->size() + static_cast<float>(nsbelow) / smallestSet->size();
190 float c2 = static_cast<float>(largestSet->size() - nlabove) / largestSet->size() + static_cast<float>(smallestSet->size() - nsbelow) / smallestSet->size();
191 float conf = std::max(c1, c2);
192 // no need to average for comparison purpose
193 if (bestConfidence < conf)
194 {
195 bestSplit.clear();
196 bestSplitDir.clear();
197 bestConfidence = conf;
198 }
199
200 if (qCanupoTools::Fpeq<float>(bestConfidence, conf))
201 {
202 bestSplit.push_back((smallestSet->at(k) + largestSet->at(std::min(dichomed, largestSet->size() - 1))) / 2);
203 bestSplitDir.push_back(c1 <= c2 ? 1 : 0);
204 }
205 }
206
207 bestConfidence /= 2;
208 // see if we're improving estimated probability or not
209 // TODO: sometimes (rarely) there are mistakes in the reference core points and we're dealing with similar classes
210 // => put back these core points in the unreliable pool
211 if (/*old*/confidence < bestConfidence)
212 {
213 // take median best split
214 size_t bsi = bestSplit.size() / 2;
215 distToBoundary = sf->getValue(coreIndex) - bestSplit[bsi];
216 // reverse if necessary
217 if (bestSplitDir[bsi] == 1)
218 distToBoundary = -distToBoundary;
219 // back to original vectors
220 if (class1SFValues.size() >= class2SFValues.size())
221 distToBoundary = -distToBoundary;
222 }
223 else
224 {
225 //no improvement
226 return confidence;
227 }
228 }
229
230 //update confidence
231 float newConfidence = 1.0f / (exp(-std::abs(distToBoundary)) + 1.0f); //in [0.5 ; 1]
232 newConfidence = 2 * (newConfidence - 0.5f); //map to [0;1]
233
234 return newConfidence;
235 }
236 catch (const std::bad_alloc&)
237 {
238 //not enough memory
239 return -1.0f;
240 }
241
242 assert(false); //we shouldn't arrive here!
243 return confidence;
244 }
245
246 bool qCanupoProcess::Classify( QString classifierFilename,
247 const ClassifyParams& params,
248 ccPointCloud* cloud,
249 CCCoreLib::GenericIndexedCloudPersist* corePoints,
250 CorePointDescSet& corePointsDescriptors,
251 ccPointCloud* realCorePoints/*=nullptr*/,
252 ccMainAppInterface* app/*=nullptr*/,
253 QWidget* parentWidget/*=nullptr*/,
254 bool silent/*=false*/)
255 {
256 //core points are mandatory
257 if (!cloud || !corePoints)
258 {
259 assert(false);
260 return false;
261 }
262
263 //load the classifier file
264 std::vector<Classifier> classifiers;
265 std::vector<float> scales;
266 unsigned descriptorID = DESC_DIMENSIONALITY;
267 {
268 QString error;
269 if (!Classifier::Load(classifierFilename, classifiers, scales, error))
270 {
271 if (app)
272 app->dispToConsole(QString("An error occured: ") + error, ccMainAppInterface::ERR_CONSOLE_MESSAGE);
273 return false;
274 }
275 if (classifiers.empty())
276 {
277 if (app)
278 app->dispToConsole(QString("Invalid classifier file!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
279 return false;
280 }
281
282 //check that the descriptor ID of every classifier is handled by this version!
283 for (size_t i = 0; i < classifiers.size(); ++i)
284 {
285 if (!ScaleParamsComputer::GetByID(classifiers[i].descriptorID))
286 {
287 if (app)
288 app->dispToConsole(QString("Unhandled descriptor type! (ID = %1)").arg(classifiers[i].descriptorID), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
289 return false;
290 }
291 if (i != 0 && classifiers[i].descriptorID != classifiers[0].descriptorID)
292 {
293 if (app)
294 app->dispToConsole(QString("Can't handle mixed descriptor types!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
295 return false;
296 }
297 }
298 descriptorID = classifiers[0].descriptorID;
299 }
300
301 //progress dialog
302 ccProgressDialog pDlg(true, parentWidget);
303
304 //does the core point cloud has associated meta-data?
305 QVariant mscMetaData;
306 bool useExistingMetaData = false;
307 if (realCorePoints)
308 {
309 mscMetaData = realCorePoints->getMetaData(s_canupoMSCMetaData);
310 if (mscMetaData.isValid())
311 {
312 bool validMetaData = ( mscMetaData.type() == QVariant::ByteArray
313 && corePointsDescriptors.fromByteArray(mscMetaData.toByteArray()));
314
315 if (validMetaData)
316 {
317 useExistingMetaData = true;
318 }
319 else
320 {
321 if (app)
322 app->dispToConsole("[qCanupo] Failed to read core point cloud associated MSC meta data?!", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
323 mscMetaData.clear();
324 }
325 }
326 }
327
328 #ifdef COMPILE_PRIVATE_CANUPO
329 //whether to compute per scale roughness for each core point!
330 bool generateRoughnessSF = params.generateRoughnessSF;
331 std::vector<ccScalarField*> coreRoughnessSFs;
332 #endif
333
334 try
335 {
336 for (int step = 0; step < 1; ++step) //fake loop for easy break
337 {
338 //check descriptors' scales (if already loaded)
339 bool computeDescriptors = true;
340 if (!corePointsDescriptors.scales().empty())
341 {
342 if (qCanupoTools::TestVectorsOverlap(scales, corePointsDescriptors.scales()) < scales.size())
343 {
344 if (!silent && QMessageBox::question( parentWidget,
345 "Scales mismatch",
346 "Available descriptors/scales data mismatch with classifier's! Compute new descriptors or cancel?",
347 QMessageBox::Yes,
348 QMessageBox::Cancel) == QMessageBox::Cancel)
349 {
350 //cancel process
351 break;
352 }
353 }
354 else
355 {
356 computeDescriptors = false; //no need to compute the descriptors as we already have them!
357 }
358
359 //check descriptor type!
360 if (!computeDescriptors && corePointsDescriptors.descriptorID() != descriptorID)
361 {
362 if (!silent && QMessageBox::question(parentWidget,
363 "Descriptor type mismatch",
364 "Available descriptors have been computed with another descriptor type! Compute new descriptors or cancel?",
365 QMessageBox::Yes,
366 QMessageBox::Cancel) == QMessageBox::Cancel)
367 {
368 //cancel process
369 break;
370 }
371 else
372 {
373 //force new computation
374 computeDescriptors = true;
375 }
376 }
377 }
378
379 //compute the original cloud octree
380 ccOctree::Shared octree = cloud->getOctree();
381 if (!octree)
382 {
383 octree = cloud->computeOctree(&pDlg);
384 if (!octree)
385 {
386 if (app)
387 app->dispToConsole("Failed to compute input cloud octree!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
388 break;
389 }
390 else if (app && cloud->getParent())
391 {
392 app->addToDB(cloud->getOctreeProxy());
393 }
394 }
395
396 #ifdef COMPILE_PRIVATE_CANUPO
397 computeDescriptors |= generateRoughnessSF;
398 #endif
399
400 //let's compute the descriptors
401 if (computeDescriptors)
402 {
403 //check that the selected descriptor (computer) is valid
404 {
405 assert(descriptorID != 0);
406 ScaleParamsComputer* computer = ScaleParamsComputer::GetByID(descriptorID);
407 if (!computer)
408 {
409 if (app)
410 app->dispToConsole(QString("Internal error: unhandled descriptor ID (%1)!").arg(descriptorID), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
411 break;
412 }
413
414 if (computer->needSF()
415 && ((realCorePoints && realCorePoints->getCurrentDisplayedScalarField() == nullptr)
416 || (!realCorePoints && cloud->getCurrentDisplayedScalarField() == nullptr) //if realCorePoints == nullptr, it means that the subsampled cloud couldn't be converted to a real cloud!
417 )
418 )
419 {
420 if (app)
421 app->dispToConsole(QString("To compute this type of descriptor, the core points cloud must have an active scalar field!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
422 break;
423 }
424 }
425
426 #ifdef COMPILE_PRIVATE_CANUPO
427 //test: create 1 sf per scale with roughness
428 if (generateRoughnessSF)
429 {
430 size_t scaleCount = scales.size();
431 coreRoughnessSFs.resize(scaleCount,0);
432 //for each scale
433 for (size_t s = 0; s < scaleCount; ++s)
434 {
435 QString sfName = QString(CANUPO_PER_LEVEL_ROUGHNESS_SF_NAME) + QString(" @ scale %1").arg(scales[s]);
436
437 coreRoughnessSFs[s] = new ccScalarField(qPrintable(sfName));
438 if (!coreRoughnessSFs[s]->resizeSafe(corePoints->size(), CCCoreLib::NAN_VALUE))
439 {
440 if (app)
441 app->dispToConsole("Not enough memory to store per-level roughness!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
442 generateRoughnessSF = false;
443 break;
444 }
445 }
446 }
447 #endif
448 if (app)
449 app->dispToConsole(QString("[Canupo] Will use %1 threads").arg(params.maxThreadCount == 0 ? "the max number of" : QString::number(params.maxThreadCount)), ccMainAppInterface::STD_CONSOLE_MESSAGE);
450
451 //computes the 'descriptors'
452 bool invalidDescriptors = false;
453 QString errorStr;
454 if (!qCanupoTools::ComputeCorePointsDescriptors(corePoints,
455 corePointsDescriptors,
456 cloud,
457 scales,
458 invalidDescriptors,
459 errorStr,
460 descriptorID,
461 params.maxThreadCount,
462 &pDlg,
463 octree.data()
464 #ifdef COMPILE_PRIVATE_CANUPO
465 , generateRoughnessSF ? &coreRoughnessSFs : nullptr
466 #endif
467 ))
468 {
469 if (app)
470 app->dispToConsole(QString("Failed to compute core points descriptors: %1").arg(errorStr), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
471 break;
472 }
473 else if (invalidDescriptors)
474 {
475 if (app)
476 app->dispToConsole("[qCanupo] Some descriptors couldn't be computed (min scale may be too small)!", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
477 }
478 }
479
480 //main classification process
481 {
482 //advanced options
483 assert(!params.useActiveSFForConfidence || cloud->getCurrentDisplayedScalarField() != nullptr);
484
485 // core points octree
486 CCCoreLib::DgmOctree* corePointsOctree = nullptr;
487 if (corePoints == cloud)
488 {
489 corePointsOctree = octree.data();
490 }
491 else
492 {
493 corePointsOctree = new CCCoreLib::DgmOctree(corePoints);
494 if (!corePointsOctree->build(&pDlg))
495 {
496 if (app)
497 app->dispToConsole("Failed to compute core points octree! (not enough memory?)", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
498 delete corePointsOctree;
499 corePointsOctree = nullptr;
500 break;
501 }
502 }
503 assert(corePointsOctree);
504
505 const PointCoordinateType& largestRadius = scales.front() / 2; //largest scale is the first by construction in canupo
506 unsigned char coreOctreeLevel = corePointsOctree->findBestLevelForAGivenNeighbourhoodSizeExtraction(params.samplingDist != 0 ? params.samplingDist : largestRadius);
507 unsigned char octreeLevel = octree->findBestLevelForAGivenNeighbourhoodSizeExtraction(largestRadius);
508
509 //core points class labels
510 std::vector<int> corePointClasses;
511 //core points confidence values
512 std::vector<float> corePointConfidences;
513
514 // set the class of all core points that are far enough from hyperplane
515 bool processCanceled = false;
516
517 size_t corePointCount = corePointsDescriptors.size();
518
519 corePointClasses.resize(corePointCount, -1);
520 corePointConfidences.resize(corePointCount, 0.0f);
521
522 //number of points that couldn't be classified
523 std::vector<unsigned> pendingPoints(corePointCount);
524 {
525 for (size_t i = 0; i < corePointCount; ++i)
526 pendingPoints[i] = static_cast<unsigned>(i);
527 }
528 std::vector<unsigned> unreliablePointIndexes;
529
530 CCCoreLib::ScalarField* sf = cloud->getCurrentDisplayedScalarField();
531 assert(!params.useActiveSFForConfidence || sf);
532
533 //while unreliable points remain
534 while (!pendingPoints.empty())
535 {
536 //progress notification
537 pDlg.reset();
538 pDlg.setInfo(QObject::tr("Remaining points to classify: %1\nSource points: %2").arg(pendingPoints.size()).arg(cloud->size()));
539 pDlg.setMethodTitle(QObject::tr("Classification"));
540 CCCoreLib::NormalizedProgress nProgress(&pDlg, corePoints->size());
541 pDlg.start();
542
543 for (size_t i = 0; i < pendingPoints.size(); ++i)
544 {
545 unsigned coreIndex = pendingPoints[i];
546 CorePointDesc& coreDesc = corePointsDescriptors[coreIndex];
547
548 //most common case
549 if (classifiers.size() == 1)
550 {
551 const Classifier& classifier = classifiers.front();
552 float distToBoundary = classifier.classify(coreDesc);
553
554 float confidence = 1.0f / (exp(-std::abs(distToBoundary)) + 1.0f); //in [0.5 ; 1]
555 confidence = 2 * (confidence - 0.5f); //map to [0;1]
556
557 //unreliable point
558 bool unreliable = false;
559 if (confidence < params.confidenceThreshold)
560 {
561 unreliable = true;
562 if (params.useActiveSFForConfidence)
563 {
564 //use the scalar field to refine the classification
565 float newConfidence = RefinePointClassif( classifier,
566 confidence,
567 distToBoundary,
568 cloud,
569 octree.data(),
570 octreeLevel,
571 corePoints,
572 corePointsOctree,
573 coreOctreeLevel,
574 coreIndex,
575 largestRadius,
576 corePointClasses);
577
578 if (newConfidence < 0)
579 {
580 if (app)
581 app->dispToConsole("Internal error: failed to refine the classification on the boundary (not enough memory)", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
582 return false;
583 }
584 else if (newConfidence > confidence)
585 {
586 unreliable = false;
587 confidence = newConfidence;
588 }
589 }
590 }
591
592 if (!unreliable)
593 {
594 int theClass = (distToBoundary >= 0 ? classifier.class2 : classifier.class1);
595 corePointClasses[i] = theClass;
596 corePointConfidences[i] = confidence;
597 }
598 else if (params.useActiveSFForConfidence)
599 {
600 //this point can't be classified this way
601 unreliablePointIndexes.push_back(static_cast<unsigned>(i));
602 }
603 }
604 else //more than one classifier
605 {
606 std::map< int, int > votes;
607 std::map< int, float > minConfidences;
608
609 // apply all classifiers and look for the most represented class
610 for (std::vector<Classifier>::const_iterator classifierIt = classifiers.begin(); classifierIt != classifiers.end(); ++classifierIt)
611 {
612 const Classifier& classifier = *classifierIt;
613
614 // uniformize the order, distToBoundary>0 selects the larger class of both
615 float distToBoundary = classifier.classify(coreDesc); //DGM: the descriptors may have more values than the number of scales!
616 //if (classifier.class1 > classifier.class2)
617 // distToBoundary = -distToBoundary;
618
619 //int minclass = std::min(classifier.class1, classifier.class2);
620 //int maxclass = std::max(classifier.class1, classifier.class2);
621
622 //int minclass = classifier.class1;
623 //int maxclass = classifier.class2;
624
625 float confidence = 1.0f / (exp(-std::abs(distToBoundary)) + 1.0f); //in [0.5 ; 1]
626 confidence = 2 * (confidence - 0.5f); //map to [0;1]
627
628 //unreliable point
629 if (confidence < params.confidenceThreshold)
630 {
631 bool unreliable = true;
632 if (params.useActiveSFForConfidence)
633 {
634 //use the scalar field to refine the classification
635 float newConfidence = RefinePointClassif(classifier,
636 confidence,
637 distToBoundary,
638 cloud,
639 octree.data(),
640 octreeLevel,
641 corePoints,
642 corePointsOctree,
643 coreOctreeLevel,
644 coreIndex,
645 largestRadius,
646 corePointClasses);
647
648 if (newConfidence < 0)
649 {
650 if (app)
651 app->dispToConsole("Internal error: failed to refine the classification on the boundary (not enough memory)", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
652 return false;
653 }
654 else if (newConfidence > confidence)
655 {
656 unreliable = false;
657 confidence = newConfidence;
658 }
659 }
660
661 if (unreliable)
662 {
663 votes.clear();
664 break;
665 }
666 }
667
668 int theClass = (distToBoundary >= 0 ? classifier.class2 : classifier.class1);
669 ++votes[theClass];
670
671 // maintain the min confidence for each class
672 if (minConfidences.find(theClass) == minConfidences.end())
673 {
674 minConfidences[theClass] = confidence;
675 }
676 else
677 {
678 if (confidence < minConfidences[theClass])
679 minConfidences[theClass] = confidence;
680 }
681 }
682
683 if (!votes.empty())
684 {
685 // search for max vote
686 std::vector<int> bestClasses;
687 int maxVoteCount = -1;
688 for (auto &vote : votes)
689 {
690 int voteCount = vote.second;
691 if (maxVoteCount < voteCount)
692 {
693 bestClasses.clear();
694 bestClasses.push_back(vote.first);
695 maxVoteCount = voteCount;
696 }
697 else if (maxVoteCount == voteCount)
698 {
699 bestClasses.push_back(vote.first);
700 }
701 }
702
703 // in case of equality, use the distances to the decision boundary
704 int bestClassLabel = bestClasses.front();
705 if (bestClasses.size() > 1)
706 {
707 for (size_t j = 1; j<bestClasses.size(); ++j)
708 {
709 if (minConfidences[bestClasses[j]] > minConfidences[bestClassLabel])
710 bestClassLabel = bestClasses[j];
711 }
712 }
713
714 corePointClasses[i] = bestClassLabel;
715 corePointConfidences[i] = minConfidences[bestClassLabel];
716 }
717 else if (params.useActiveSFForConfidence)
718 {
719 //this point can't be classified this way
720 unreliablePointIndexes.push_back(static_cast<unsigned>(i));
721 }
722 }
723
724 //progress notification
725 if (!nProgress.oneStep())
726 {
727 processCanceled = true;
728 break;
729 }
730 }
731
732 //nothing has changed?
733 if (pendingPoints.size() == unreliablePointIndexes.size())
734 {
735 break;
736 }
737
738 pendingPoints = unreliablePointIndexes;
739 unreliablePointIndexes.clear();
740 }
741
742 if (processCanceled)
743 {
744 if (app)
745 app->dispToConsole("[qCanupo] Process cancelled by user!", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
746 break;
747 }
748
749 // eventually label the points
750 {
751 // instantiate the scalar fields
752 CCCoreLib::ScalarField* classLabelSF = nullptr;
753 int classLabelSFIdx = -1;
754 {
755 classLabelSFIdx = cloud->getScalarFieldIndexByName("CANUPO.class");
756 if (classLabelSFIdx < 0)
757 classLabelSFIdx = cloud->addScalarField("CANUPO.class");
758 if (classLabelSFIdx >= 0)
759 {
760 classLabelSF = cloud->getScalarField(classLabelSFIdx);
761 classLabelSF->fill(CCCoreLib::NAN_VALUE);
762 }
763 else
764 {
765 if (app)
766 app->dispToConsole("Not enough memory (class label can't be created)!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
767 break;
768 }
769 }
770
771 CCCoreLib::ScalarField* confidenceSF = nullptr;
772 int confidenceSFIdx = -1;
773 {
774 confidenceSFIdx = cloud->getScalarFieldIndexByName("CANUPO.confidence");
775 if (confidenceSFIdx < 0)
776 confidenceSFIdx = cloud->addScalarField("CANUPO.confidence");
777 if (confidenceSFIdx >= 0)
778 {
779 confidenceSF = cloud->getScalarField(confidenceSFIdx);
780 confidenceSF->fill(CCCoreLib::NAN_VALUE);
781 }
782 else if (app)
783 {
784 app->dispToConsole("[qCanupo] Not enough memory to store the confidence values!", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
785 }
786 }
787
788 //optional: create 1 SF per scale with 'x-y'
789 std::vector<ccScalarField*> scaleSFs;
790 bool generateAdditionalSF = params.generateAdditionalSF;
791 if (generateAdditionalSF && corePointsDescriptors.dimPerScale() != 2)
792 {
793 if (app)
794 app->dispToConsole("[qCanupo] Per-level 'x-y' values can only be extracted from descriptor with 2 dimensions per scale!", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
795 generateAdditionalSF = false;
796 }
797
798 if (generateAdditionalSF)
799 {
800 //remove any previously generated scalar field starting by CANUPO_PER_LEVEL_ADDITIONAL_SF_NAME
801 {
802 QStringList toDelete;
803 for (unsigned i = 0; i < cloud->getNumberOfScalarFields(); ++i)
804 {
805 QString sfName(cloud->getScalarField(i)->getName());
806 if (sfName.startsWith(CANUPO_PER_LEVEL_ADDITIONAL_SF_NAME))
807 toDelete << sfName;
808 }
809
810 for (int j = 0; j < toDelete.size(); ++j)
811 cloud->deleteScalarField(cloud->getScalarFieldIndexByName(qPrintable(toDelete[j])));
812 }
813
814 size_t scaleCount = scales.size();
815 scaleSFs.resize(scaleCount, nullptr);
816 //for each scale
817 for (size_t s = 0; s < scaleCount; ++s)
818 {
819 QString sfName = QString(CANUPO_PER_LEVEL_ADDITIONAL_SF_NAME) + QString(" @ scale %1").arg(scales[s]);
820
821 //SF with same name (if any) should have already been removed!
822 assert(cloud->getScalarFieldIndexByName(qPrintable(sfName)) < 0);
823
824 scaleSFs[s] = new ccScalarField(qPrintable(sfName));
825 if (!scaleSFs[s]->resizeSafe(cloud->size(), true, CCCoreLib::NAN_VALUE))
826 {
827 if (app)
828 app->dispToConsole("Not enough memory to store per-level 'x-y' values!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
829 while (!scaleSFs.empty())
830 {
831 scaleSFs.back()->release();
832 scaleSFs.pop_back();
833 }
834 generateAdditionalSF = false;
835 break;
836 }
837 }
838 }
839
840 #ifdef COMPILE_PRIVATE_CANUPO
841 //optional: create 1 sf per scale with roughness
842 std::vector<ccScalarField*> roughnessSFs;
843 if (generateRoughnessSF)
844 {
845 //remove any previously generated scalar field starting by CANUPO_PER_LEVEL_ROUGHNESS_SF_NAME
846 {
847 QStringList toDelete;
848 for (unsigned i = 0; i < cloud->getNumberOfScalarFields(); ++i)
849 {
850 QString sfName(cloud->getScalarField(i)->getName());
851 if (sfName.startsWith(CANUPO_PER_LEVEL_ROUGHNESS_SF_NAME))
852 toDelete << sfName;
853 }
854
855 for (int j = 0; j < toDelete.size(); ++j)
856 cloud->deleteScalarField(cloud->getScalarFieldIndexByName(qPrintable(toDelete[j])));
857 }
858
859 //if the output cloud has the same number of points as the core points cloud, no need to duplicate the scalar fields!
860 if (corePoints->size() == cloud->size())
861 {
862 for (size_t s = 0; s < coreRoughnessSFs.size(); ++s)
863 {
864 //SF with same name (if any) should have already been removed!
865 assert(cloud->getScalarFieldIndexByName(coreRoughnessSFs[s]->getName()) < 0);
866
867 coreRoughnessSFs[s]->computeMinAndMax();
868 cloud->addScalarField(coreRoughnessSFs[s]);
869 }
870 coreRoughnessSFs.clear(); //don't want to release them anymore!
871 generateRoughnessSF = false; //no need to bother anymore
872 }
873 else
874 {
875 size_t scaleCount = scales.size();
876 roughnessSFs.resize(scaleCount, 0);
877 assert(coreRoughnessSFs.size() == roughnessSFs.size());
878 //for each scale
879 for (size_t s = 0; s < scaleCount; ++s)
880 {
881 //same name as the per-core points version
882 roughnessSFs[s] = new ccScalarField(coreRoughnessSFs[s]->getName());
883
884 //SF with same name (if any) should have already been removed!
885 assert(cloud->getScalarFieldIndexByName(roughnessSFs[s]->getName()) < 0);
886
887 if (!roughnessSFs[s]->resizeSafe(cloud->size(), CCCoreLib::NAN_VALUE))
888 {
889 if (app)
890 app->dispToConsole("Not enough memory to store per-level roughness!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
891 while (!roughnessSFs.empty())
892 {
893 roughnessSFs.back()->release();
894 roughnessSFs.pop_back();
895 }
896 generateRoughnessSF = false;
897 break;
898 }
899 }
900 }
901 }
902 #endif
903
904 //progress notification
905 pDlg.reset();
906 pDlg.setInfo(QObject::tr("Core points: %1\nSource points: %2").arg(corePoints->size()).arg(cloud->size()));
907 pDlg.setMethodTitle(QObject::tr("Labelling"));
908 CCCoreLib::NormalizedProgress nProgress(&pDlg, cloud->size());
909 pDlg.start();
910
911 bool error = false;
912 CCCoreLib::ReferenceCloud Yk(corePoints);
913 for (unsigned i = 0; i < cloud->size(); ++i)
914 {
915 const CCVector3* P = cloud->getPoint(i);
916 // process this point
917 // first look for the nearest neighbor in core points
918 unsigned nearestCorePointIndex = 0;
919 if (corePoints == cloud)
920 {
921 //if we used the whole cloud as core points, then each point is a core point
922 nearestCorePointIndex = i;
923 }
924 else
925 {
926 double maxSquareDist = 0;
927 Yk.clear(false);
928
929 assert(corePointsOctree);
930 if (corePointsOctree->findPointNeighbourhood(P,
931 &Yk,
932 1,
933 coreOctreeLevel,
934 maxSquareDist) == 1)
935 {
936 nearestCorePointIndex = Yk.getPointGlobalIndex(0);
937 }
938 else
939 {
940 assert(false);
941 error = true;
942 break;
943 }
944 }
945
946 assert(classLabelSF);
947 ScalarType classVal = static_cast<ScalarType>(corePointClasses[nearestCorePointIndex]);
948 if (classVal >= 0)
949 classLabelSF->setValue(i, classVal);
950 // otherwise, it is already defaulted to NaN
951
952 if (confidenceSF)
953 {
954 ScalarType confVal = static_cast<ScalarType>(corePointConfidences[nearestCorePointIndex]);
955 confidenceSF->setValue(i, confVal);
956 }
957
958 //save 'x-y' values
959 if (generateAdditionalSF)
960 {
961 unsigned dimPerScale = corePointsDescriptors.dimPerScale();
962 assert(dimPerScale == 2);
963 const CorePointDesc& desc = corePointsDescriptors[nearestCorePointIndex];
964 assert(desc.params.size() == scaleSFs.size() * dimPerScale);
965 for (size_t s = 0; s < scaleSFs.size(); ++s)
966 {
967 ScalarType val = (desc.params[s*dimPerScale] - desc.params[s*dimPerScale + 1]);
968 scaleSFs[s]->setValue(i, val);
969 }
970 }
971
972 #ifdef COMPILE_PRIVATE_CANUPO
973 //save roughness values
974 if (generateRoughnessSF)
975 {
976 const CorePointDesc& desc = corePointsDescriptors[nearestCorePointIndex];
977 assert(coreRoughnessSFs.size() == roughnessSFs.size());
978 for (size_t s = 0; s < roughnessSFs.size(); ++s)
979 {
980 const ScalarType& val = coreRoughnessSFs[s]->getValue(nearestCorePointIndex);
981 roughnessSFs[s]->setValue(i, val);
982 }
983 }
984 #endif
985
986 //progress notification
987 if (!nProgress.oneStep())
988 {
989 processCanceled = true;
990 break;
991 }
992 }
993
994 if (error)
995 {
996 if (app)
997 app->dispToConsole("Internal error: failed to get nearest core points?!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
998 cloud->deleteScalarField(classLabelSFIdx);
999 if (confidenceSFIdx >= 0)
1000 cloud->deleteScalarField(confidenceSFIdx);
1001 while (!scaleSFs.empty())
1002 {
1003 scaleSFs.back()->release();
1004 scaleSFs.pop_back();
1005 }
1006 }
1007 else
1008 {
1009 classLabelSF->computeMinAndMax();
1010 cloud->setCurrentDisplayedScalarField(classLabelSFIdx);
1011
1012 if (confidenceSF)
1013 {
1014 confidenceSF->computeMinAndMax();
1015 }
1016
1017 if (generateAdditionalSF)
1018 {
1019 for (auto &scaleSF : scaleSFs)
1020 {
1021 scaleSF->computeMinAndMax();
1022 scaleSF->setSymmetricalScale(true);
1023 cloud->addScalarField(scaleSF);
1024 }
1025 }
1026
1027 #ifdef COMPILE_PRIVATE_CANUPO
1028 if (generateRoughnessSF)
1029 {
1030 for (size_t s = 0; s < roughnessSFs.size(); ++s)
1031 {
1032 roughnessSFs[s]->computeMinAndMax();
1033 cloud->addScalarField(roughnessSFs[s]);
1034 }
1035 }
1036 #endif
1037 }
1038 cloud->showSF(cloud->getCurrentDisplayedScalarField() != nullptr);
1039 }
1040
1041 //dispose of octree
1042 if (corePointsOctree != octree)
1043 {
1044 delete corePointsOctree;
1045 corePointsOctree = nullptr;
1046 }
1047
1048 //save MSC data as meta-data on the core point cloud
1049 if (realCorePoints && !useExistingMetaData)
1050 {
1051 bool proceed = true;
1052 if (mscMetaData.isValid())
1053 {
1054 proceed = (silent || QMessageBox::question( parentWidget,
1055 "Overwrite MSC meta-data?",
1056 "Core points cloud already has associated MSC meta-data, should we overwrite them?",
1057 QMessageBox::Yes,
1058 QMessageBox::No) == QMessageBox::Yes
1059 );
1060 }
1061
1062 if (proceed)
1063 {
1064 mscMetaData = corePointsDescriptors.toByteArray();
1065 if (mscMetaData.isValid())
1066 {
1067 realCorePoints->setMetaData(s_canupoMSCMetaData, mscMetaData);
1068 if (app)
1069 app->dispToConsole(QString("[qCanupo] MSC descriptors have been saved as meta-data (cloud '%1')").arg(realCorePoints->getName()));
1070 }
1071 else
1072 {
1073 if (app)
1074 app->dispToConsole("[qCanupo] Failed to save MSC meta-data (not enough memory?)", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
1075 }
1076 }
1077 }
1078 }
1079
1080 break;
1081 }
1082 }
1083 catch (const std::bad_alloc&)
1084 {
1085 //not enough memory
1086 if (app)
1087 app->dispToConsole("Not enough memory", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1088 return false;
1089 }
1090
1091 #ifdef COMPILE_PRIVATE_CANUPO
1092 //release roughness SFs (if any)
1093 while (!coreRoughnessSFs.empty())
1094 {
1095 coreRoughnessSFs.back()->release();
1096 coreRoughnessSFs.pop_back();
1097 }
1098 #endif
1099
1100 return true;
1101 }