Debian Codegraph @

plugins/core/Standard/qSRA/src/qSRA.cpp

raw
1 //##########################################################################
2 //# #
3 //# CLOUDCOMPARE PLUGIN: qSRA #
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: EDF #
15 //# #
16 //##########################################################################
17
18 #include "qSRA.h"
19
20 //Local
21 #include "profileImportDlg.h"
22 #include "distanceMapGenerationDlg.h"
23 #include "profileLoader.h"
24
25 //Qt
26 #include <QtGui>
27 #include <QSettings>
28 #include <QFileInfo>
29 #include <QFile>
30 #include <QMessageBox>
31 #include <QTextStream>
32 #include <QMainWindow>
33
34 //qCC_db
35 #include <ccFileUtils.h>
36 #include <ccHObjectCaster.h>
37 #include <ccPointCloud.h>
38 #include <ccProgressDialog.h>
39 #include <ccMesh.h>
40 #include <ccPolyline.h>
41 #include <ccCone.h>
42 #include <ccScalarField.h>
43
44 //System
45 #include <string.h>
46 #include <algorithm>
47 #include <vector>
48
49 qSRA::qSRA(QObject* parent/*=nullptr*/)
50 : QObject(parent)
51 , ccStdPluginInterface(":/CC/plugin/qSRA/info.json")
52 , m_doLoadProfile(nullptr)
53 , m_doCompareCloudToProfile(nullptr)
54 , m_doProjectCloudDists(nullptr)
55 {
56 }
57
58 QList<QAction *> qSRA::getActions()
59 {
60 //actions
61 if (!m_doLoadProfile)
62 {
63 m_doLoadProfile = new QAction("Load profile",this);
64 m_doLoadProfile->setToolTip("Loads the 2D profile of a Surface of Revolution (from a dedicated ASCII file)");
65 m_doLoadProfile->setIcon(QIcon(QString::fromUtf8(":/CC/plugin/qSRA/images/loadProfileIcon.png")));
66 //connect signal
67 connect(m_doLoadProfile, &QAction::triggered, this, &qSRA::loadProfile);
68 }
69
70 if (!m_doCompareCloudToProfile)
71 {
72 m_doCompareCloudToProfile = new QAction("Cloud-SurfRev radial distance",this);
73 m_doCompareCloudToProfile->setToolTip("Computes the radial distances between a cloud and a Surface of Revolution (polyline/profile, cone or cylinder)");
74 m_doCompareCloudToProfile->setIcon(QIcon(QString::fromUtf8(":/CC/plugin/qSRA/images/distToProfileIcon.png")));
75 //connect signal
76 connect(m_doCompareCloudToProfile, &QAction::triggered, this, &qSRA::computeCloud2ProfileRadialDist);
77 }
78
79 if (!m_doProjectCloudDists)
80 {
81 m_doProjectCloudDists = new QAction("2D distance map",this);
82 m_doProjectCloudDists->setToolTip("Creates the 2D deviation map (radial distances) from a Surface or Revolution (unroll)");
83 m_doProjectCloudDists->setIcon(QIcon(QString::fromUtf8(":/CC/plugin/qSRA/images/createMapIcon.png")));
84 //connect signal
85 connect(m_doProjectCloudDists, &QAction::triggered, this, &qSRA::projectCloudDistsInGrid);
86 }
87
88 return QList<QAction *>{
89 m_doLoadProfile,
90 m_doCompareCloudToProfile,
91 m_doProjectCloudDists,
92 };
93 }
94
95 void qSRA::onNewSelection(const ccHObject::Container& selectedEntities)
96 {
97 if (m_doLoadProfile)
98 {
99 //always active
100 }
101
102 bool validSelection = false;
103 if (selectedEntities.size() == 2)
104 {
105 //we expect a cloud...
106 int cloudIndex = selectedEntities[0]->isA(CC_TYPES::POINT_CLOUD) ? 0 : selectedEntities[1]->isA(CC_TYPES::POINT_CLOUD) ? 1 : -1;
107 if (cloudIndex != -1)
108 {
109 //... and either a polyline or a cone/cylinder
110 validSelection = (selectedEntities[1 - cloudIndex]->isA(CC_TYPES::POLY_LINE) || selectedEntities[1 - cloudIndex]->isKindOf(CC_TYPES::CONE));
111 }
112 }
113
114 if (m_doCompareCloudToProfile)
115 {
116 m_doCompareCloudToProfile->setEnabled(validSelection);
117 }
118
119 if (m_doProjectCloudDists)
120 {
121 m_doProjectCloudDists->setEnabled(validSelection);
122 }
123 }
124
125 //return (and create if necessary) the plugin default destination container
126 const QString QSRA_DEFAULT_CONTAINER_NAME("Profile(s)");
127 ccHObject* GetDefaultContainer(ccMainAppInterface* app)
128 {
129 if (!app || !app->dbRootObject())
130 {
131 assert(false);
132 return nullptr;
133 }
134
135 //we look in qCC database for a group with the right name (i.e. if it has already been created)
136 ccHObject::Container groups;
137 app->dbRootObject()->filterChildren(groups,true,CC_TYPES::HIERARCHY_OBJECT);
138 for (size_t j = 0; j < groups.size(); ++j)
139 {
140 if (groups[j]->getName() == QSRA_DEFAULT_CONTAINER_NAME)
141 return groups[j];
142 }
143
144 //otherwise we create it
145 ccHObject* defaultContainer = new ccHObject(QSRA_DEFAULT_CONTAINER_NAME);
146 app->addToDB(defaultContainer);
147
148 return defaultContainer;
149 }
150
151 void qSRA::loadProfile() const
152 {
153 if (!m_app)
154 {
155 assert(false);
156 return;
157 }
158
159 //persistent settings (default import path)
160 QSettings settings;
161 settings.beginGroup("qSRA");
162 QString path = settings.value("importPath", ccFileUtils::defaultDocPath()).toString();
163
164 ProfileImportDlg piDlg(m_app->getMainWindow());
165 piDlg.setDefaultFilename(path);
166
167 if (!piDlg.exec())
168 return;
169
170 QString filename = piDlg.getFilename();
171 if (filename.isEmpty())
172 return;
173
174 //save current import path to persistent settings
175 settings.setValue("importPath", QFileInfo(filename).absolutePath());
176
177 //get the user defined global axis
178 int axisDim = piDlg.getAxisDimension();
179 assert(axisDim >= 0 && axisDim <= 2);
180
181 //load profile as a (2D) polyline
182 CCVector3 origin(0, 0, 0);
183 ccPolyline* polyline = ProfileLoader::Load(filename, origin, m_app);
184 if (!polyline)
185 {
186 if (m_app)
187 m_app->dispToConsole(QString("Failed to load file '%1'!").arg(filename), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
188 return;
189 }
190
191 //DGM: the following works only because the axis is colinear with X, Y or Z!
192 PointCoordinateType heightShift = 0;
193 if (piDlg.absoluteHeightValues())
194 {
195 heightShift = -origin.u[axisDim];
196 }
197
198 //apply a visual transformation to see the polyline in the right place
199 {
200 ccGLMatrix trans;
201 CCVector3 T = origin;
202 T.u[axisDim] += heightShift;
203 trans.setTranslation(T);
204 float* mat = trans.data();
205 switch (axisDim)
206 {
207 case 0: //X
208 //invert X and Y
209 mat[0] = 0; mat[1] = 1;
210 mat[4] = 1; mat[5] = 0;
211 break;
212 //case 1: //Y
213 case 2: //Z
214 //invert Z and Y
215 mat[5] = 0; mat[6] = 1;
216 mat[9] = 1; mat[10] = 0;
217 break;
218 default:
219 //nothing to do
220 break;
221 }
222 polyline->set2DMode(false);
223 polyline->setGLTransformation(trans);
224 }
225
226 //set meta-data
227 DistanceMapGenerationTool::SetPoylineOrigin(polyline, origin);
228 DistanceMapGenerationTool::SetPoylineRevolDim(polyline, axisDim);
229 DistanceMapGenerationTool::SetPolylineHeightShift(polyline, heightShift);
230
231 //default destination container
232 ccHObject* defaultContainer = GetDefaultContainer(m_app);
233 if (defaultContainer)
234 {
235 defaultContainer->addChild(polyline);
236 }
237
238 m_app->addToDB(polyline, true, false, true);
239
240 m_app->dispToConsole(QString("[qSRA] File '%1' successfully loaded").arg(filename), ccMainAppInterface::STD_CONSOLE_MESSAGE);
241 }
242
243 //helper
244 static ccPolyline* GetConeProfile(ccCone* cone)
245 {
246 if (!cone)
247 {
248 assert(false);
249 return nullptr;
250 }
251
252 //we deduce the profile orientation and position from the cone 4x4 transformation
253 ccGLMatrix& coneTrans = cone->getTransformation();
254
255 CCVector3 axis = coneTrans.getColumnAsVec3D(2);
256 CCVector3 origin = coneTrans.getTranslationAsVec3D();
257 PointCoordinateType height = cone->getHeight();
258 //we'll use the 'largest' axis dimension as 'revolution dimension'
259 int revolDim = 0;
260 for (int i = 1; i<3; ++i)
261 if (std::abs(axis.u[i]) > std::abs(axis.u[revolDim]))
262 revolDim = i;
263
264 //the profile has only one segment
265 ccPointCloud* vertices = new ccPointCloud("vertices");
266 {
267 if (!vertices->reserve(2))
268 {
269 delete vertices;
270 ccLog::Error("Not enough memory");
271 return nullptr;
272 }
273
274 vertices->addPoint(CCVector3(cone->getBottomRadius(), -height/2, 0));
275 vertices->addPoint(CCVector3(cone->getTopRadius(), height/2, 0));
276 }
277
278 ccPolyline* polyline = new ccPolyline(vertices);
279 {
280 polyline->addChild(vertices);
281 if (!polyline->reserve(2))
282 {
283 delete polyline;
284 ccLog::Error("Not enough memory");
285 return nullptr;
286 }
287 polyline->addPointIndex(0, 2);
288 polyline->setClosed(false);
289 }
290
291 //apply a visual transformation to see the polyline in the right place
292 {
293 CCVector3 y(0, 1, 0);
294 CCVector3 Z(0, 0, 0);
295 Z.u[revolDim] = CCCoreLib::PC_ONE;
296 ccGLMatrix axisTrans = ccGLMatrix::FromToRotation(y, Z);
297 assert(CCCoreLib::LessThanSquareEpsilon(((axisTrans * y) - Z).norm2d()));
298 ccGLMatrix polyMat = coneTrans * axisTrans;
299 polyline->setGLTransformation(polyMat);
300 }
301
302 //set meta-data
303 DistanceMapGenerationTool::SetPoylineOrigin(polyline, origin);
304 DistanceMapGenerationTool::SetPoylineAxis(polyline, axis);
305 DistanceMapGenerationTool::SetPolylineHeightShift(polyline, 0/*height / 2*/);
306 DistanceMapGenerationTool::SetPoylineRevolDim(polyline, revolDim);
307
308 return polyline;
309 }
310
311 void qSRA::computeCloud2ProfileRadialDist() const
312 {
313 if (!m_app)
314 {
315 assert(false);
316 return;
317 }
318
319 const ccHObject::Container& selectedEntities = m_app->getSelectedEntities();
320 if (selectedEntities.size() != 2)
321 {
322 assert(false);
323 return;
324 }
325
326 //retrieve input cloud and polyline
327 ccPointCloud* cloud = nullptr;
328 ccPolyline* polyline = nullptr;
329 bool tempPolyline = false;
330 {
331 for (unsigned i = 0; i < 2; ++i)
332 {
333 if (selectedEntities[i]->isA(CC_TYPES::POINT_CLOUD))
334 {
335 cloud = static_cast<ccPointCloud*>(selectedEntities[i]);
336 }
337 else if (selectedEntities[i]->isA(CC_TYPES::POLY_LINE))
338 {
339 polyline = static_cast<ccPolyline*>(selectedEntities[i]);
340 }
341 else if (!polyline && selectedEntities[i]->isKindOf(CC_TYPES::CONE))
342 {
343 //special case: we can deduce the polyline from the cone/cylinder parameters
344 ccCone* cone = static_cast<ccCone*>(selectedEntities[i]);
345 polyline = GetConeProfile(cone);
346 if (!polyline)
347 {
348 //the conversion failed?!
349 return;
350 }
351 tempPolyline = true;
352
353 #ifdef _DEBUG
354 //test: apply a visual transformation to see the polyline in the right place
355 {
356 polyline->set2DMode(false);
357 polyline->setWidth(2);
358 polyline->setColor(ccColor::green);
359 polyline->showColors(true);
360 cone->setVisible(false);
361 cone->addChild(polyline);
362 m_app->addToDB(polyline);
363 tempPolyline = false;
364 }
365 #endif
366 }
367 }
368 }
369
370 if (cloud && polyline)
371 {
372 if (doComputeRadialDists(cloud, polyline))
373 {
374 //automatically ask the user if he wants to generate a 2D map
375 if (QMessageBox::question( m_app ? m_app->getMainWindow() : nullptr,
376 "Generate map",
377 "Do you want to generate a 2D deviation map?",
378 QMessageBox::Yes,
379 QMessageBox::No) == QMessageBox::Yes)
380 {
381 doProjectCloudDistsInGrid(cloud, polyline);
382 }
383 }
384 }
385 else
386 {
387 if (m_app)
388 m_app->dispToConsole(QString("Select exactly one cloud and one Surface of Revolution (polyline/profile, cone or cylinder)"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
389 }
390
391 if (polyline && tempPolyline)
392 {
393 delete polyline;
394 polyline = nullptr;
395 }
396 }
397
398 bool qSRA::doComputeRadialDists(ccPointCloud* cloud, ccPolyline* polyline) const
399 {
400 if (!cloud || !polyline)
401 {
402 assert(false);
403 return false;
404 }
405
406 if (DistanceMapGenerationTool::ComputeRadialDist(cloud, polyline, false, m_app))
407 {
408 cloud->prepareDisplayForRefresh();
409 if (m_app)
410 {
411 m_app->updateUI();
412 m_app->refreshAll();
413 }
414 return true;
415 }
416 else
417 {
418 if (m_app)
419 m_app->dispToConsole("An error occurred while computing radial distances!",ccMainAppInterface::WRN_CONSOLE_MESSAGE);
420 return false;
421 }
422 }
423
424 void qSRA::projectCloudDistsInGrid() const
425 {
426 if (!m_app)
427 {
428 assert(false);
429 return;
430 }
431
432 const ccHObject::Container& selectedEntities = m_app->getSelectedEntities();
433 size_t selectCount = selectedEntities.size();
434 if (selectCount != 1 && selectCount != 2)
435 {
436 assert(false);
437 return;
438 }
439
440 //retrieve input cloud and polyline
441 ccPointCloud* cloud = nullptr;
442 ccPolyline* polyline = nullptr;
443 bool tempPolyline = false;
444 {
445 for (size_t i = 0; i < selectCount; ++i)
446 {
447 if (selectedEntities[i]->isA(CC_TYPES::POINT_CLOUD))
448 {
449 cloud = static_cast<ccPointCloud*>(selectedEntities[i]);
450 }
451 else if (selectedEntities[i]->isA(CC_TYPES::POLY_LINE))
452 {
453 polyline = static_cast<ccPolyline*>(selectedEntities[i]);
454 }
455 else if (!polyline && selectedEntities[i]->isKindOf(CC_TYPES::CONE))
456 {
457 //special case: we can deduce the polyline from the cone/cylinder parameters
458 ccCone* cone = static_cast<ccCone*>(selectedEntities[i]);
459 polyline = GetConeProfile(cone);
460 if (!polyline)
461 {
462 //the conversion failed?!
463 return;
464 }
465 tempPolyline = true;
466 }
467 }
468 }
469
470 if (cloud && polyline)
471 {
472 doProjectCloudDistsInGrid(cloud,polyline);
473 }
474
475 if (polyline && tempPolyline)
476 {
477 delete polyline;
478 polyline = nullptr;
479 }
480 }
481
482 void qSRA::doProjectCloudDistsInGrid(ccPointCloud* cloud, ccPolyline* polyline) const
483 {
484 assert(cloud && m_app);
485 if (!cloud)
486 return;
487
488 //get the scalar field to map
489 ccScalarField* sf = nullptr;
490 {
491 int sfIdx = cloud->getScalarFieldIndexByName(RADIAL_DIST_SF_NAME);
492 if (sfIdx < 0)
493 {
494 sf = cloud->getCurrentDisplayedScalarField();
495 if (sf)
496 {
497 if (QMessageBox::question( m_app ? m_app->getMainWindow() : nullptr,
498 "Distance field",
499 QString("Cloud has no '%1' field. Do you want to use the active scalar field instead?").arg(RADIAL_DIST_SF_NAME),
500 QMessageBox::Yes,
501 QMessageBox::No) == QMessageBox::No )
502 {
503 //we can stop already
504 return;
505 }
506 }
507 else
508 {
509 QString message = QString("Cloud has no '%1' field and no active scalar field!").arg(RADIAL_DIST_SF_NAME);
510 ccLog::Error(message);
511
512 //additional indications
513 if (m_doCompareCloudToProfile)
514 {
515 ccLog::Warning(QString("You can compute the radial distances with the '%1' method").arg(m_doCompareCloudToProfile->text()));
516 }
517 return;
518 }
519 }
520 else
521 {
522 sf = static_cast<ccScalarField*>(cloud->getScalarField(sfIdx));
523 }
524 }
525 assert(sf);
526
527 DistanceMapGenerationDlg dmgDlg(cloud, sf, polyline, m_app);
528
529 dmgDlg.exec();
530 }