Debian Codegraph @

plugins/core/Standard/qCanupo/src/qCanupo2DViewDialog.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 "qCanupo2DViewDialog.h"
19
20 //local
21 #include "qCanupoTools.h"
22
23 //qCC_plugins
24 #include "ccMainAppInterface.h"
25
26 //qCC_db
27 #include <ccPointCloud.h>
28 #include <ccPolyline.h>
29 #include <ccSphere.h>
30
31 //qCC_gl
32 #include <ccGLWindowInterface.h>
33
34 //Qt
35 #include <QSettings>
36 #include <QMainWindow>
37 #include <QFileInfo>
38 #include <QFileDialog>
39 #include <QPushButton>
40 #include <QApplication>
41 #include <QMessageBox>
42
43 //system
44 #include <limits>
45 #include <assert.h>
46
47 static bool s_firstDisplay = true;
48
49 qCanupo2DViewDialog::qCanupo2DViewDialog( const CorePointDescSet* descriptors1,
50 const CorePointDescSet* descriptors2,
51 QString cloud1Name,
52 QString cloud2Name,
53 int class1/*=1*/,
54 int class2/*=2*/,
55 const CorePointDescSet* evaluationDescriptors/*=nullptr*/,
56 ccMainAppInterface* app/*=nullptr*/)
57 : QDialog(app ? app->getMainWindow() : nullptr)
58 , Ui::Canupo2DViewDialog()
59 , m_app(app)
60 , m_glWindow(nullptr)
61 , m_classifierSaved(false)
62 , m_descriptors1(descriptors1)
63 , m_descriptors2(descriptors2)
64 , m_evaluationDescriptors(evaluationDescriptors)
65 , m_class1(class1)
66 , m_cloud1Name(cloud1Name)
67 , m_cloud2Name(cloud2Name)
68 , m_class2(class2)
69 , m_cloud(nullptr)
70 , m_poly(nullptr)
71 , m_polyVertices(nullptr)
72 , m_selectedPointIndex(-1)
73 , m_pickingRadius(5)
74 {
75 setupUi(this);
76
77 //update legend
78 cloud1NameLabel->setText(QString("class %1: ").arg(m_class1) + m_cloud1Name); //blue points
79 cloud2NameLabel->setText(QString("class %1: ").arg(m_class2) + m_cloud2Name); //red points
80
81 s_firstDisplay = true;
82
83 //setup 2D view
84 {
85 QWidget* glWidget = nullptr;
86 m_app->createGLWindow(m_glWindow, glWidget);
87 assert(m_glWindow && glWidget);
88
89 ccGui::ParamStruct params = m_glWindow->getDisplayParameters();
90 params.backgroundCol = ccColor::white;
91 params.textDefaultCol = ccColor::black;
92 params.pointsDefaultCol = ccColor::black;
93 params.drawBackgroundGradient = false;
94 params.displayCross = false;
95 m_glWindow->setDisplayParameters(params, true);
96 m_glWindow->setPerspectiveState(false, true);
97 m_glWindow->setInteractionMode(ccGLWindowInterface::MODE_PAN_ONLY | ccGLWindowInterface::INTERACT_SEND_ALL_SIGNALS);
98 m_glWindow->setPickingMode(ccGLWindowInterface::NO_PICKING);
99 m_glWindow->displayOverlayEntities(false, false);
100 m_glWindow->setSunLight(true);
101 m_glWindow->setCustomLight(false);
102 //add window to the dedicated layout
103 viewFrame->setLayout(new QHBoxLayout());
104 viewFrame->layout()->addWidget(glWidget);
105
106 connect(m_glWindow->signalEmitter(), &ccGLWindowSignalEmitter::leftButtonClicked, this, &qCanupo2DViewDialog::addOrSelectPoint);
107 connect(m_glWindow->signalEmitter(), &ccGLWindowSignalEmitter::rightButtonClicked, this, &qCanupo2DViewDialog::removePoint);
108 connect(m_glWindow->signalEmitter(), &ccGLWindowSignalEmitter::mouseMoved, this, &qCanupo2DViewDialog::moveSelectedPoint);
109 connect(m_glWindow->signalEmitter(), &ccGLWindowSignalEmitter::buttonReleased, this, &qCanupo2DViewDialog::deselectPoint);
110 }
111
112 updateScalesList(true);
113
114 //loadParamsFromPersistentSettings();
115 connect(resetToolButton, &QAbstractButton::clicked, this, &qCanupo2DViewDialog::resetBoundary);
116 connect(statisticsToolButton, &QAbstractButton::clicked, this, &qCanupo2DViewDialog::computeStatistics);
117 connect(savePushButton, &QAbstractButton::clicked, this, &qCanupo2DViewDialog::saveClassifier);
118 connect(donePushButton, &QAbstractButton::clicked, this, &qCanupo2DViewDialog::checkBeforeAccept);
119
120 connect(pointSizeSpinBox, qOverload<int>(&QSpinBox::valueChanged), this, &qCanupo2DViewDialog::setPointSize);
121 connect(scalesCountSpinBox, qOverload<int>(&QSpinBox::valueChanged), this, &qCanupo2DViewDialog::onScalesCountSpinBoxChanged);
122 }
123
124 qCanupo2DViewDialog::~qCanupo2DViewDialog()
125 {
126 reset();
127
128 if (m_app && m_glWindow)
129 {
130 m_app->destroyGLWindow(m_glWindow);
131 }
132 }
133
134 void qCanupo2DViewDialog::updateScalesList(bool firstTime)
135 {
136 //update list of scales
137 if (!m_descriptors1 || !m_descriptors2)
138 {
139 scalesListLineEdit->setText("Invalid descriptors!");
140 scalesCountSpinBox->setEnabled(false);
141 }
142 else
143 {
144 const std::vector<float>& allScales = m_descriptors1->scales();
145 int maxScaleCount = static_cast<int>(allScales.size());
146 scalesCountSpinBox->setRange(1,maxScaleCount);
147 if (firstTime)
148 scalesCountSpinBox->setValue(maxScaleCount);
149
150 int currentScaleCount = scalesCountSpinBox->value();
151 QStringList scalesList;
152 for (int i=0; i<currentScaleCount; ++i)
153 scalesList << QString::number(allScales[maxScaleCount - currentScaleCount + i]);
154
155 scalesListLineEdit->setText(scalesList.join(" "));
156 scalesCountSpinBox->setEnabled(true);
157 }
158 }
159
160 void qCanupo2DViewDialog::reset()
161 {
162 if (m_glWindow)
163 m_glWindow->getOwnDB()->removeAllChildren();
164
165 if (m_poly)
166 delete m_poly;
167 m_poly = nullptr;
168 m_polyVertices = nullptr; //m_polyVertices is a child of m_poly
169
170 if (m_cloud)
171 delete m_cloud;
172 m_cloud = nullptr;
173 }
174
175 void qCanupo2DViewDialog::getActiveScales(std::vector<float>& scales) const
176 {
177 scales.clear();
178
179 if (!m_descriptors1)
180 return;
181
182 //take only the right number (among the smallest)
183 const std::vector<float>& allScales = m_descriptors1->scales();
184 int maxScaleCount = static_cast<int>(allScales.size());
185 int currentScaleCount = scalesCountSpinBox->value();
186 assert(currentScaleCount >= 1 && currentScaleCount <= maxScaleCount);
187 currentScaleCount = std::min<int>(currentScaleCount, maxScaleCount);
188 scales.resize(currentScaleCount);
189 for (int i=0; i<currentScaleCount; ++i)
190 {
191 //take only the smallest values (scales are sorted from the biggest to the smallest)
192 scales[i] = allScales[maxScaleCount - currentScaleCount + i];
193 }
194 }
195
196
197 //Training in progress
198 static bool s_training = false;
199 static int s_trainingValue = 0;
200
201 bool qCanupo2DViewDialog::trainClassifier()
202 {
203 if (!m_descriptors1 || !m_descriptors2)
204 return false;
205
206 s_training = true;
207 s_trainingValue = scalesCountSpinBox->value();
208 statisticsToolButton->setEnabled(false);
209 setEnabled(false);
210 QApplication::processEvents();
211
212 //take only the right number (among the smallest)
213 std::vector<float> scales;
214 getActiveScales(scales);
215
216 //reset display
217 reset();
218
219 //Reset classifier and re-train it!
220 m_classifier = Classifier();
221 m_classifier.class1 = m_class1;
222 m_classifier.class2 = m_class2;
223
224 m_cloud = new ccPointCloud("CANUPO projections");
225 if (!qCanupoTools::TrainClassifier( m_classifier,
226 *m_descriptors1,
227 *m_descriptors2,
228 scales,
229 m_cloud,
230 m_evaluationDescriptors,
231 m_app))
232 {
233 delete m_cloud;
234 m_cloud = nullptr;
235
236 s_training = false;
237 setEnabled(true);
238
239 return false;
240 }
241
242 addObject(m_cloud);
243
244 //show reference points as spheres
245 {
246 PointCoordinateType l = m_cloud->getOwnBB().getMaxBoxDim() / 100;
247 ccGLMatrix matPos;
248 matPos.setTranslation(CCVector3(m_classifier.refPointPos.x,m_classifier.refPointPos.y,0));
249 ccSphere* spherePos = new ccSphere(l,&matPos);
250 spherePos->setColor(ccColor::red);
251 spherePos->showColors(true);
252 spherePos->enableStippling(true);
253 m_cloud->addChild(spherePos);
254 addObject(spherePos);
255
256 ccGLMatrix matNeg;
257 matNeg.setTranslation(CCVector3(m_classifier.refPointNeg.x,m_classifier.refPointNeg.y,0));
258 ccSphere* sphereNeg = new ccSphere(l,&matNeg);
259 sphereNeg->setColor(ccColor::blue);
260 sphereNeg->showColors(true);
261 sphereNeg->enableStippling(true);
262 m_cloud->addChild(sphereNeg);
263 addObject(sphereNeg);
264 }
265
266 //update/create boundary representation
267 resetBoundary();
268
269 if (s_firstDisplay)
270 {
271 updateZoom();
272 s_firstDisplay = false;
273 }
274
275 s_training = false;
276 setEnabled(true);
277
278 //we need a valid classifier to compute statistics!
279 statisticsToolButton->setEnabled(true);
280
281 return true;
282 }
283
284 void qCanupo2DViewDialog::onScalesCountSpinBoxChanged(int value)
285 {
286 if (s_training)
287 {
288 scalesCountSpinBox->blockSignals(true);
289 scalesCountSpinBox->setValue(s_trainingValue);
290 scalesCountSpinBox->blockSignals(false);
291 }
292 else
293 {
294 //update list of scales
295 updateScalesList(false);
296
297 trainClassifier();
298 }
299 }
300
301 void qCanupo2DViewDialog::computeStatistics()
302 {
303 qCanupoTools::EvalParameters params;
304 std::vector<float> scales;
305 getActiveScales(scales);
306
307 Classifier classifier = m_classifier;
308 updateClassifierPath(classifier);
309
310 if (qCanupoTools::EvaluateClassifier( classifier,
311 *m_descriptors1,
312 *m_descriptors2,
313 scales,
314 params))
315 {
316 QStringList info;
317 info << QString("Class %1 (%2)").arg(m_class1).arg(m_cloud1Name);
318 info << QString("\tTotal: %1").arg(params.true1+params.false1);
319 info << QString("\tTruly classified: %1").arg(params.true1);
320 info << QString("\tFalsely classified: %1").arg(params.false1);
321 info << QString("\tDist. to boundary: %1 +/- %2").arg(params.mu1).arg(sqrt(params.var1));
322 info << QString("");
323 info << QString("Class %1 (%2)").arg(m_class2).arg(m_cloud2Name);
324 info << QString("\tTotal: %1").arg(params.true2+params.false2);
325 info << QString("\tTruly classified: %1").arg(params.true2);
326 info << QString("\tFalsely classified: %1").arg(params.false2);
327 info << QString("\tDist. to boundary: %1 +/- %2").arg(params.mu2).arg(sqrt(params.var2));
328 info << QString("");
329 info << QString("Balanced accuracy (ba) = %1").arg(params.ba());
330 info << QString("Fisher Discriminant Ratio (fdr) = %1").arg(params.fdr());
331
332 QMessageBox::information(this,"Statistics",info.join("\n"),QMessageBox::Ok);
333 }
334 }
335
336 void qCanupo2DViewDialog::setPointSize(int value)
337 {
338 if (m_glWindow)
339 {
340 m_glWindow->setPointSize(static_cast<float>(value));
341 m_glWindow->redraw();
342 }
343 }
344
345 void qCanupo2DViewDialog::checkBeforeAccept()
346 {
347 if (!m_classifierSaved)
348 {
349 if (QMessageBox::warning(this, "Classifier has not been saved!", "Do you really want to close the dialog before saving the classifier?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
350 return;
351 }
352
353 accept();
354 }
355
356 void qCanupo2DViewDialog::resetBoundary()
357 {
358 assert(m_glWindow);
359
360 if (!m_poly)
361 {
362 assert(!m_polyVertices);
363 m_polyVertices = new ccPointCloud("vertices");
364 m_poly = new ccPolyline(m_polyVertices);
365 m_poly->addChild(m_polyVertices);
366 m_poly->setColor(ccColor::magenta);
367 m_poly->showColors(true);
368 m_poly->setWidth(2);
369 m_poly->showVertices(true);
370 m_poly->setVertexMarkerWidth(4);
371 addObject(m_poly);
372 }
373
374 m_poly->clear();
375 m_polyVertices->clear();
376
377 unsigned pathLength = static_cast<unsigned>(m_classifier.path.size());
378 if (pathLength > 1)
379 {
380 if ( !m_poly->reserve(pathLength)
381 || !m_polyVertices->reserve(pathLength))
382 return;
383
384 for (unsigned i=0; i<pathLength; ++i)
385 m_polyVertices->addPoint(CCVector3(m_classifier.path[i].x, m_classifier.path[i].y, -std::numeric_limits<PointCoordinateType>::epsilon()));
386
387 m_poly->addPointIndex(0,pathLength);
388 }
389
390 m_glWindow->redraw();
391 }
392
393 void qCanupo2DViewDialog::saveClassifier()
394 {
395 //select file to save
396 QSettings settings("qCanupo");
397 settings.beginGroup("Classif");
398 QString currentPath = settings.value("MscCurrentPath",QApplication::applicationDirPath()).toString();
399
400 QString filename = QFileDialog::getSaveFileName(this,"Save Classifier",currentPath,"*.prm");
401 if (filename.isEmpty())
402 return;
403
404 //update the classifier's path
405 Classifier classifier = m_classifier;
406 updateClassifierPath(classifier);
407
408 QString error;
409 if (classifier.save(filename, error))
410 {
411 m_classifierSaved = true;
412 if (m_app)
413 m_app->dispToConsole(QString("Classifier file saved: '%1'").arg(filename));
414 }
415 else
416 {
417 if (m_app)
418 m_app->dispToConsole(error,ccMainAppInterface::ERR_CONSOLE_MESSAGE);
419 }
420
421 //we update current file path
422 currentPath = QFileInfo(filename).absolutePath();
423 settings.setValue("MscCurrentPath",currentPath);
424 }
425
426 void qCanupo2DViewDialog::updateClassifierPath(Classifier& classifier) const
427 {
428 //update the classifier's path
429 if (m_poly)
430 {
431 classifier.path.resize(m_poly->size());
432 for (unsigned i=0; i<m_poly->size(); ++i)
433 {
434 const CCVector3* P = m_poly->getPoint(i);
435 classifier.path[i] = Classifier::Point2D(P->x,P->y);
436 }
437 }
438 }
439
440 void qCanupo2DViewDialog::addObject(ccHObject* obj)
441 {
442 if (!obj)
443 return;
444 obj->setVisible(true);
445 m_glWindow->addToOwnDB(obj);
446 }
447
448 void qCanupo2DViewDialog::updateZoom()
449 {
450 ccBBox box = m_glWindow->getOwnDB()->getDisplayBB_recursive(false,m_glWindow);
451 m_glWindow->updateConstellationCenterAndZoom(&box);
452 m_glWindow->redraw();
453 }
454
455 void qCanupo2DViewDialog::setPickingRadius(int radius)
456 {
457 m_pickingRadius = std::max(radius,1);
458 }
459
460 CCVector3 qCanupo2DViewDialog::getClickPos(int x, int y) const
461 {
462 if (!m_glWindow)
463 {
464 assert(false);
465 return CCVector3(0, 0, 0);
466 }
467
468 //we must convert the mouse click to the polyline coordinate system
469 ccGLCameraParameters camera;
470 m_glWindow->getGLCameraParameters(camera);
471
472 QPointF pos2D = m_glWindow->toCornerGLCoordinates(x, y);
473 CCVector3 P2D(pos2D.x(), pos2D.y(), 0);
474
475 CCVector3d P3D;
476 camera.unproject(P2D, P3D);
477
478 //DGM: the Z value is meaningless!
479 P3D.z = 0;
480
481 return P3D.toPC();
482 }
483
484 int qCanupo2DViewDialog::getClosestVertex(int x, int y, CCVector3& P) const
485 {
486 if (!m_poly || !m_glWindow)
487 return -1;
488
489 P = getClickPos(x, y);
490
491 //look for the closest vertex
492 int closeIndex = -1;
493 float closestSquareDist = 0;
494 for (unsigned i=0; i<m_poly->size(); ++i)
495 {
496 float squareDist = (*m_poly->getPoint(i) - P).norm2();
497 if (closeIndex < 0 || squareDist < closestSquareDist)
498 {
499 closestSquareDist = squareDist;
500 closeIndex = static_cast<int>(i);
501 }
502 }
503
504 return closeIndex;
505 }
506
507 void qCanupo2DViewDialog::addOrSelectPoint(int x, int y)
508 {
509 if (!m_poly || !m_glWindow)
510 return;
511
512 CCVector3 P;
513 int closeIndex = getClosestVertex(x, y, P);
514
515 //B = closest vertex
516 const CCVector3* B = (closeIndex >= 0 ? m_poly->getPoint(closeIndex) : nullptr);
517
518 //picking radius
519 double maxPickingDist = static_cast<double>(m_pickingRadius) * m_glWindow->computeActualPixelSize();
520
521 //to allow 'mouse move" tracking event
522 m_glWindow->setInteractionMode(ccGLWindowInterface::INTERACT_SEND_ALL_SIGNALS);
523
524 //is the closest point close enough?
525 if (closeIndex >= 0)
526 {
527 assert(B);
528 if ((P - *B).norm() <= maxPickingDist)
529 {
530 //we select the closest vertex
531 m_selectedPointIndex = closeIndex;
532 return;
533 }
534 }
535
536 //let's look if the click fall 'inside' a segment
537 double nearestProjectionDist = maxPickingDist;
538 CCVector3 nearestProj = P;
539 int nearestSegIndex = -1;
540 for (unsigned i=0; i+1<m_poly->size(); ++i)
541 {
542 const CCVector3* A = m_poly->getPoint(i);
543 const CCVector3* B = m_poly->getPoint(i+1);
544
545 CCVector3 AB = (*B-*A);
546 CCVector3 AP = ( P-*A);
547
548 PointCoordinateType dot = AB.dot(AP);
549
550 //projection falls inside the segment?
551 dot /= AB.norm2();
552 if (dot > 0 && dot < CCCoreLib::PC_ONE)
553 {
554 CCVector3 AH = AB*dot;
555 CCVector3 HP = AP - AH;
556 double dist = HP.norm();
557
558 if (dist < nearestProjectionDist)
559 {
560 nearestProjectionDist = dist;
561 nearestProj = AH + *A;
562 nearestSegIndex = static_cast<int>(i);
563 }
564 }
565 }
566
567 if (nearestSegIndex >= 0)
568 {
569 //if we have a candidate
570 closeIndex = nearestSegIndex;
571 P = nearestProj; //shall we really replace P by its projection on the
572 }
573 else
574 {
575 //we'll add the point to the nearest end (i.e. front or back)
576 const CCVector3* A = m_poly->getPoint(0);
577 const CCVector3* B = m_poly->getPoint(m_poly->size()-1);
578 if ((P-*A).norm2() < (P-*B).norm2())
579 {
580 closeIndex = -1;
581 }
582 else
583 {
584 closeIndex = static_cast<int>(m_poly->size()-1);
585 }
586 }
587
588 //add the point to the vertices cloud
589 ccPointCloud* vertices = dynamic_cast<ccPointCloud*>(m_poly->getAssociatedCloud());
590 if (!vertices)
591 {
592 assert(false);
593 return;
594 }
595 unsigned newIndexInCloud = vertices->size();
596 if (!vertices->reserve(newIndexInCloud + 1))
597 {
598 delete vertices;
599 return;
600 }
601 vertices->addPoint(P);
602
603 //add the corresponding point index at the end of the polyline
604 //(whatever the case to avoid complicated tests later)
605 m_poly->reserve(m_poly->size()+1);
606 m_poly->addPointIndex(newIndexInCloud);
607
608 m_selectedPointIndex = closeIndex+1;
609
610 //eventually we must shift the indexes starting from newIndexInPoly
611 unsigned newIndexInPoly = static_cast<unsigned>(m_selectedPointIndex);
612 while (newIndexInPoly < m_poly->size())
613 {
614 unsigned previousIndexInCloud = m_poly->getPointGlobalIndex(newIndexInPoly);
615 m_poly->setPointIndex(newIndexInPoly, newIndexInCloud);
616 newIndexInCloud = previousIndexInCloud;
617 ++newIndexInPoly;
618 }
619
620 m_glWindow->redraw();
621 }
622
623 void qCanupo2DViewDialog::removePoint(int x, int y)
624 {
625 if (!m_poly || !m_glWindow)
626 return;
627
628 //we can't accept less than 2 vertices!
629 unsigned polySize = m_poly->size();
630 if (polySize < 3)
631 return;
632
633 CCVector3 P;
634 int closeIndex = getClosestVertex(x, y, P);
635
636 //nothing to do
637 if (closeIndex < 0)
638 return;
639
640 //picking radius
641 double maxPickingDist = m_pickingRadius * m_glWindow->computeActualPixelSize();
642
643 //B = closest vertex
644 const CCVector3* B = m_poly->getPoint(closeIndex);
645 if ((P-*B).norm() > maxPickingDist)
646 {
647 //too far
648 return;
649 }
650
651 //shift all indexes on the polyline only (we don't bother with the vertices cloud!)
652 for (unsigned i = static_cast<unsigned>(closeIndex); i < polySize - 1; ++i)
653 {
654 m_poly->setPointIndex(i, m_poly->getPointGlobalIndex(i + 1));
655 }
656 m_poly->resize(polySize - 1);
657
658 m_glWindow->redraw();
659 }
660
661 void qCanupo2DViewDialog::moveSelectedPoint(int x, int y, Qt::MouseButtons buttons)
662 {
663 if (buttons != Qt::LeftButton)
664 return;
665 if (m_selectedPointIndex < 0)
666 return;
667 if (!m_poly || !m_glWindow)
668 return;
669
670 CCVector3 newP = getClickPos(x, y);
671
672 assert(static_cast<int>(m_poly->size()) > m_selectedPointIndex);
673 CCVector3* P = const_cast<CCVector3*>(m_poly->getPoint(m_selectedPointIndex));
674 *P = newP;
675
676 //TODO: in theory we should update the vertices cloud bounding-box
677 //(but we as never use it...)
678
679 m_glWindow->redraw();
680 }
681
682 void qCanupo2DViewDialog::deselectPoint()
683 {
684 //to disable 'mouse move' event tracking
685 m_glWindow->setInteractionMode(ccGLWindowInterface::MODE_PAN_ONLY | ccGLWindowInterface::INTERACT_SEND_ALL_SIGNALS);
686
687 m_selectedPointIndex = -1;
688 }