Debian Codegraph @

plugins/core/Standard/qSRA/src/distanceMapGenerationDlg.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 "distanceMapGenerationDlg.h"
19
20 //local
21 #include "ccSymbolCloud.h"
22 #include "ccMapWindow.h"
23 #include "dxfProfilesExporter.h"
24 #include "dxfProfilesExportDlg.h"
25
26 //qCC_plugins
27 #include <ccMainAppInterface.h>
28
29 //qCC
30 #include <ccColorScaleSelector.h>
31 #include <ccColorScalesManager.h>
32 #include <ccColorScaleEditorDlg.h>
33 #include <ccRenderToFileDlg.h>
34
35 //common
36 #include <ccQtHelpers.h>
37
38 //qCC_db
39 #include <ccFileUtils.h>
40 #include <ccPointCloud.h>
41 #include <ccPlane.h>
42 #include <ccScalarField.h>
43 #include <ccPolyline.h>
44 #include <ccMaterialSet.h>
45
46 //Qt
47 #include <QColorDialog>
48 #include <QCloseEvent>
49 #include <QFile>
50 #include <QFileDialog>
51 #include <QFileInfo>
52 #include <QFontMetrics>
53 #include <QHBoxLayout>
54 #include <QLocale>
55 #include <QMainWindow>
56 #include <QProgressDialog>
57 #include <QSettings>
58 #include <QTextStream>
59
60 //system
61 #include <assert.h>
62
63 //default names
64 static const char XLABEL_CLOUD_NAME[] = "X_Labels";
65 static const char YLABEL_CLOUD_NAME[] = "Y_Labels";
66 static const double DEFAULT_LABEL_MARGIN = 20.0; //half of this in fact (we use the 'symbol' size)
67
68 static double ConvertAngleFromRad(double angle_rad, DistanceMapGenerationDlg::ANGULAR_UNIT destUnit)
69 {
70 switch (destUnit)
71 {
72 case DistanceMapGenerationDlg::ANG_DEG: //degrees
73 return CCCoreLib::RadiansToDegrees(angle_rad);
74 case DistanceMapGenerationDlg::ANG_RAD: //radians
75 return angle_rad;
76 case DistanceMapGenerationDlg::ANG_GRAD: //grades
77 return angle_rad / M_PI * 200.0;
78 default:
79 assert(false);
80 }
81
82 return 0.0;
83 }
84
85 static double ConvertAngleToRad(double angle, DistanceMapGenerationDlg::ANGULAR_UNIT srcUnit)
86 {
87 switch (srcUnit)
88 {
89 case DistanceMapGenerationDlg::ANG_DEG: //degrees
90 return CCCoreLib::DegreesToRadians(angle);
91 case DistanceMapGenerationDlg::ANG_RAD: //radians
92 return angle;
93 case DistanceMapGenerationDlg::ANG_GRAD: //grades
94 return angle / 200.0 * M_PI;
95 default:
96 assert(false);
97 }
98
99 return 0.0;
100 }
101
102 DistanceMapGenerationDlg::DistanceMapGenerationDlg(ccPointCloud* cloud, ccScalarField* sf, ccPolyline* polyline, ccMainAppInterface* app/*=nullptr*/)
103 : QDialog(app ? app->getMainWindow() : nullptr)
104 , m_app(app)
105 , m_cloud(cloud)
106 , m_profile(polyline)
107 , m_sf(sf)
108 , m_map(nullptr)
109 , m_angularUnits(ANG_GRAD)
110 , m_window(nullptr)
111 , m_colorScaleSelector(nullptr)
112 , m_xLabels(nullptr)
113 , m_yLabels(nullptr)
114 , m_gridColor(Qt::gray)
115 , m_symbolColor(Qt::black)
116 {
117 setupUi(this);
118
119 assert(m_cloud && m_sf && m_profile);
120
121 //add color ramp selector widget (before calling initFromPersistentSettings!)
122 if (m_sf)
123 {
124 //create selector widget
125 m_colorScaleSelector = new ccColorScaleSelector(m_app->getColorScalesManager(), this, QString::fromUtf8(":/CC/plugin/qSRA/images/gearIcon.png"));
126 m_colorScaleSelector->init();
127 m_colorScaleSelector->setSelectedScale(ccColorScalesManager::GetDefaultScale()->getUuid());
128 connect(m_colorScaleSelector, &ccColorScaleSelector::colorScaleSelected, this, &DistanceMapGenerationDlg::colorScaleChanged);
129 connect(m_colorScaleSelector, &ccColorScaleSelector::colorScaleEditorSummoned, this, &DistanceMapGenerationDlg::spawnColorScaleEditor);
130 //add selector to group's layout
131 if (!colorRampGroupBox->layout())
132 colorRampGroupBox->setLayout(new QHBoxLayout());
133 colorRampGroupBox->layout()->addWidget(m_colorScaleSelector);
134 colorScaleStepsSpinBox->setRange(ccColorScale::MIN_STEPS, ccColorScale::MAX_STEPS);
135 }
136
137 //init parameters from persistent settings
138 initFromPersistentSettings();
139
140 if (m_sf)
141 {
142 //we apply the cloud current color scale ONLY if it is not a default one
143 //(otherwise we keep the default dialog's one)
144 const ccColorScale::Shared& scale = m_sf->getColorScale();
145 if (scale && !scale->isLocked())
146 m_colorScaleSelector->setSelectedScale(scale->getUuid());
147 }
148
149 //profile meta-data
150 DistanceMapGenerationTool::ProfileMetaData profileDesc;
151 bool validProfile = false;
152
153 //set default dialog values with polyline & cloud information
154 if (m_profile)
155 {
156 validProfile = DistanceMapGenerationTool::GetPoylineMetaData(m_profile, profileDesc);
157 if (validProfile)
158 {
159 //update the 'Generatrix' tab
160 {
161 axisDimComboBox->setCurrentIndex(profileDesc.revolDim);
162
163 xOriginDoubleSpinBox->setValue(profileDesc.origin.x);
164 yOriginDoubleSpinBox->setValue(profileDesc.origin.y);
165 zOriginDoubleSpinBox->setValue(profileDesc.origin.z);
166 }
167
168 updateMinAndMaxLimits();
169
170 //check that the vertical step (for the grid) is not bigger than the map
171 double yMin = 0.0;
172 double yMax = 0.0;
173 double yStep = 0.0;
174 getGridYValues(yMin, yMax, yStep, ANG_RAD);
175 double dY = yMax - yMin;
176 if (dY < scaleHStepDoubleSpinBox->value())
177 {
178 scaleHStepDoubleSpinBox->setValue(dY);
179 ccLog::Warning("[qSRA] The vertical step of the grid has been automatically reduced to match the map height (you can change it if required)");
180 }
181 }
182 else
183 {
184 if (m_app)
185 m_app->dispToConsole(QString("Invalid profile: can't generate a proper map!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
186 }
187 }
188
189 //compute min and max height of the points
190 //we will 'lock" the max height value with that information
191 if (m_cloud)
192 {
193 ccBBox bbox = m_cloud->getOwnBB();
194 PointCoordinateType hMin = 0;
195 PointCoordinateType hMax = 0;
196 if (bbox.isValid())
197 {
198 hMin = bbox.minCorner().u[profileDesc.revolDim];
199 hMax = bbox.maxCorner().u[profileDesc.revolDim];
200 }
201
202 if (hMax - hMin <= 0)
203 {
204 if (m_app)
205 m_app->dispToConsole(QString("Cloud is flat: can't generate a proper map!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
206 }
207 else
208 {
209 hStepDoubleSpinBox->setMaximum(hMax - hMin);
210 }
211 }
212
213 //add window
214 {
215 m_window = new ccMapWindow();
216 ccGui::ParamStruct params = m_window->getDisplayParameters();
217 params.backgroundCol = ccColor::white;
218 params.textDefaultCol = ccColor::black;
219 params.drawBackgroundGradient = false;
220 params.colorScaleShowHistogram = false;
221 params.colorScaleRampWidth = 30;
222 params.decimateMeshOnMove = false;
223 params.displayCross = false;
224 params.colorScaleUseShader = false;
225 m_window->setDisplayParameters(params, true);
226 m_window->setPerspectiveState(false, true);
227 m_window->setInteractionMode(ccGLWindowInterface::INTERACT_PAN | ccGLWindowInterface::INTERACT_CLICKABLE_ITEMS | ccGLWindowInterface::INTERACT_ZOOM_CAMERA);
228 m_window->displayOverlayEntities(false, false);
229 m_window->showSF(displayColorScaleCheckBox->isChecked());
230 m_window->setSunLight(true);
231 m_window->setCustomLight(false);
232 //add window to the right side layout
233 mapFrame->setLayout(new QHBoxLayout());
234 mapFrame->layout()->addWidget(m_window);
235 precisionSpinBox->setValue(params.displayedNumPrecision);
236 }
237
238 //create labels "clouds" (empty)
239 {
240 m_xLabels = new ccSymbolCloud(XLABEL_CLOUD_NAME);
241 m_xLabels->showSymbols(false);
242 m_xLabels->setSymbolSize(DEFAULT_LABEL_MARGIN);
243 m_xLabels->setLabelAlignmentFlags(ccGenericGLDisplay::ALIGN_HMIDDLE | ccGenericGLDisplay::ALIGN_VBOTTOM);
244 m_window->addToOwnDB(m_xLabels, false);
245 m_yLabels = new ccSymbolCloud(YLABEL_CLOUD_NAME);
246 m_yLabels->showSymbols(false);
247 m_yLabels->setSymbolSize(DEFAULT_LABEL_MARGIN);
248 m_yLabels->setLabelAlignmentFlags(ccGenericGLDisplay::ALIGN_HRIGHT | ccGenericGLDisplay::ALIGN_VMIDDLE);
249 m_window->addToOwnDB(m_yLabels, false);
250 }
251
252 connect(projectionComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &DistanceMapGenerationDlg::projectionModeChanged);
253 connect(angularUnitComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &DistanceMapGenerationDlg::angularUnitChanged);
254 connect(xStepDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateGridSteps);
255 connect(hStepDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateGridSteps);
256 connect(latStepDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateGridSteps);
257 connect(xMinDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateGridSteps);
258 connect(xMaxDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateGridSteps);
259 connect(hMinDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateGridSteps);
260 connect(hMaxDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateGridSteps);
261 connect(latMinDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateGridSteps);
262 connect(latMaxDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateGridSteps);
263 connect(axisDimComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &DistanceMapGenerationDlg::updateProfileRevolDim);
264 connect(xOriginDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateProfileOrigin);
265 connect(yOriginDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateProfileOrigin);
266 connect(zOriginDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::updateProfileOrigin);
267 connect(baseRadiusDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &DistanceMapGenerationDlg::baseRadiusChanged);
268
269 connect(heightUnitLineEdit, &QLineEdit::editingFinished, this, &DistanceMapGenerationDlg::updateHeightUnits);
270
271 connect(exportCloudPushButton, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::exportMapAsCloud);
272 connect(exportMeshPushButton, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::exportMapAsMesh);
273 connect(exportMatrixPushButton, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::exportMapAsGrid);
274 connect(exportImagePushButton, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::exportMapAsImage);
275 connect(loadLabelsPushButton, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::loadOverlaySymbols);
276 connect(clearLabelsPushButton, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::clearOverlaySymbols);
277
278 connect(symbolSizeSpinBox, qOverload<int>(&QSpinBox::valueChanged), this, &DistanceMapGenerationDlg::overlaySymbolsSizeChanged);
279 connect(fontSizeSpinBox, qOverload<int>(&QSpinBox::valueChanged), this, &DistanceMapGenerationDlg::labelFontSizeChanged);
280 connect(precisionSpinBox, qOverload<int>(&QSpinBox::valueChanged), this, &DistanceMapGenerationDlg::labelPrecisionChanged);
281 connect(colorScaleStepsSpinBox, qOverload<int>(&QSpinBox::valueChanged), this, &DistanceMapGenerationDlg::colorRampStepsChanged);
282
283 connect(overlayGridGroupBox, &QGroupBox::toggled, this, &DistanceMapGenerationDlg::toggleOverlayGrid);
284 connect(scaleXStepDoubleSpinBox, &QAbstractSpinBox::editingFinished, this, &DistanceMapGenerationDlg::updateOverlayGrid);
285 connect(scaleHStepDoubleSpinBox, &QAbstractSpinBox::editingFinished, this, &DistanceMapGenerationDlg::updateOverlayGrid);
286 connect(scaleLatStepDoubleSpinBox, &QAbstractSpinBox::editingFinished, this, &DistanceMapGenerationDlg::updateOverlayGrid);
287 connect(xScaleCheckBox, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::updateOverlayGrid);
288 connect(yScaleCheckBox, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::updateOverlayGrid);
289 connect(gridColorButton, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::changeGridColor);
290 connect(symbolColorButton, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::changeSymbolColor);
291 connect(displayColorScaleCheckBox, &QAbstractButton::toggled, this, &DistanceMapGenerationDlg::toggleColorScaleDisplay);
292 connect(updateVolumesPushButton, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::updateVolumes);
293
294 //DXF profiles export button is only visible/connected if DXF support is enabled!
295 if (DxfProfilesExporter::IsEnabled())
296 {
297 connect(exportImageDXFButton, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::exportProfilesAsDXF);
298 }
299 else
300 {
301 exportImageDXFButton->hide();
302 }
303
304 //button box
305 {
306 QPushButton* applyButton = buttonBox->button(QDialogButtonBox::Apply);
307 QPushButton* closeButton = buttonBox->button(QDialogButtonBox::Close);
308 connect(applyButton, &QAbstractButton::clicked, this, &DistanceMapGenerationDlg::update);
309 connect(closeButton, &QAbstractButton::clicked, this, &QDialog::accept);
310 connect(closeButton, &QAbstractButton::clicked, [&] { closeEvent(nullptr); });
311 }
312
313 angularUnitChanged(m_angularUnits); //just to be sure
314 baseRadiusChanged(0);
315 overlaySymbolsColorChanged();
316 overlayGridColorChanged();
317 labelFontSizeChanged(-1);
318 projectionModeChanged(-1);
319 }
320
321 void DistanceMapGenerationDlg::closeEvent(QCloseEvent* e)
322 {
323 if (m_window && m_window->getOwnDB())
324 {
325 //remove the mesh otherwise it may not be removed before CC quits
326 //and the OpenGL context won't be valid anymore to unload the map texture
327 m_window->getOwnDB()->removeAllChildren();
328 }
329
330 QDialog::closeEvent(e);
331 }
332
333 void DistanceMapGenerationDlg::updateMinAndMaxLimits()
334 {
335 if (m_cloud && m_profile)
336 {
337 DistanceMapGenerationTool::ProfileMetaData profileDesc;
338 if (DistanceMapGenerationTool::GetPoylineMetaData(m_profile, profileDesc))
339 {
340 //compute mean 'radius'
341 //as well as min and max 'height'
342 double baseRadius = 0.0;
343 double minHeight = 0.0;
344 double maxHeight = 0.0;
345 for (unsigned i = 0; i < m_profile->size(); ++i)
346 {
347 const CCVector3* P = m_profile->getPoint(i);
348 double radius = P->x;
349 double height = P->y + profileDesc.heightShift;
350 baseRadius += radius;
351
352 if (i != 0)
353 {
354 if (height < minHeight)
355 minHeight = height;
356 else if (height > maxHeight)
357 maxHeight = height;
358 }
359 else
360 {
361 minHeight = maxHeight = height;
362 }
363 }
364
365 //set default 'base radius'
366 if (m_profile->size() != 0)
367 baseRadius /= m_profile->size();
368 if (baseRadius == 0.0)
369 baseRadius = 1.0;
370
371 baseRadiusDoubleSpinBox->blockSignals(true);
372 baseRadiusDoubleSpinBox->setValue(baseRadius);
373 baseRadiusDoubleSpinBox->blockSignals(false);
374
375 //set default min and max height
376 hMinDoubleSpinBox->blockSignals(true);
377 hMinDoubleSpinBox->setValue(minHeight);
378 hMinDoubleSpinBox->blockSignals(false);
379
380 hMaxDoubleSpinBox->blockSignals(true);
381 hMaxDoubleSpinBox->setValue(maxHeight);
382 hMaxDoubleSpinBox->blockSignals(false);
383
384 //do the same for the latitude
385
386 //compute transformation from the cloud to the surface (of revolution)
387 ccGLMatrix cloudToSurfaceOrigin = profileDesc.computeCloudToSurfaceOriginTrans();
388
389 double minLat_rad = 0.0;
390 double maxLat_rad = 0.0;
391 if (DistanceMapGenerationTool::ComputeMinAndMaxLatitude_rad(m_cloud,
392 minLat_rad,
393 maxLat_rad,
394 cloudToSurfaceOrigin,
395 static_cast<unsigned char>(profileDesc.revolDim)))
396 {
397 latMinDoubleSpinBox->blockSignals(true);
398 latMinDoubleSpinBox->setValue(ConvertAngleFromRad(minLat_rad, m_angularUnits));
399 latMinDoubleSpinBox->blockSignals(false);
400
401 latMaxDoubleSpinBox->blockSignals(true);
402 latMaxDoubleSpinBox->setValue(ConvertAngleFromRad(maxLat_rad, m_angularUnits));
403 latMaxDoubleSpinBox->blockSignals(false);
404 }
405 }
406 }
407 }
408
409 void DistanceMapGenerationDlg::projectionModeChanged(int)
410 {
411 //reset eveything, etc.
412 ProjectionMode mode = getProjectionMode();
413
414 clearView();
415
416 //conical mode only
417 latLabel->setVisible(mode == PROJ_CONICAL);
418 latMinDoubleSpinBox->setVisible(mode == PROJ_CONICAL);
419 latMaxDoubleSpinBox->setVisible(mode == PROJ_CONICAL);
420 latStepDoubleSpinBox->setVisible(mode == PROJ_CONICAL);
421 latStepLabel->setVisible(mode == PROJ_CONICAL);
422 scaleLatStepLabel->setVisible(mode == PROJ_CONICAL);
423 scaleLatStepDoubleSpinBox->setVisible(mode == PROJ_CONICAL);
424 spanRatioFrame->setVisible(mode == PROJ_CONICAL);
425
426 //cylindrical mode only
427 yLabel->setVisible(mode == PROJ_CYLINDRICAL);
428 hMinDoubleSpinBox->setVisible(mode == PROJ_CYLINDRICAL);
429 hMaxDoubleSpinBox->setVisible(mode == PROJ_CYLINDRICAL);
430 exportImageDXFButton->setVisible(mode == PROJ_CYLINDRICAL);
431 heightStepLabel->setVisible(mode == PROJ_CYLINDRICAL);
432 hStepDoubleSpinBox->setVisible(mode == PROJ_CYLINDRICAL);
433 scaleHeightStepLabel->setVisible(mode == PROJ_CYLINDRICAL);
434 scaleHStepDoubleSpinBox->setVisible(mode == PROJ_CYLINDRICAL);
435 heightUnitLineEdit->setVisible(mode == PROJ_CYLINDRICAL);
436 yScaleCheckBox->setVisible(mode == PROJ_CYLINDRICAL);
437
438 baseRadiusChanged(0);
439 updateGridSteps();
440
441 if (m_map)
442 update();
443 }
444
445 DistanceMapGenerationDlg::ProjectionMode DistanceMapGenerationDlg::getProjectionMode() const
446 {
447 switch (projectionComboBox->currentIndex())
448 {
449 case 0:
450 return PROJ_CYLINDRICAL;
451 case 1:
452 return PROJ_CONICAL;
453 default:
454 assert(false);
455 }
456
457 return PROJ_CYLINDRICAL;
458 }
459
460 DistanceMapGenerationDlg::ANGULAR_UNIT DistanceMapGenerationDlg::getAngularUnit() const
461 {
462 switch (m_angularUnits)
463 {
464 case 0: //degrees
465 return ANG_DEG;
466 case 1: //radians
467 return ANG_RAD;
468 case 2: //grades
469 return ANG_GRAD;
470 default:
471 assert(false);
472 }
473
474 return ANG_DEG;
475 }
476
477 QString DistanceMapGenerationDlg::getAngularUnitString() const
478 {
479 switch (m_angularUnits)
480 {
481 case 0: //degrees
482 return "deg";
483 case 1: //radians
484 return "rad";
485 case 2: //grades
486 return "grad";
487 default:
488 assert(false);
489 }
490
491 return "none";
492 }
493
494 QString DistanceMapGenerationDlg::getCondensedAngularUnitString() const
495 {
496 switch (m_angularUnits)
497 {
498 case 0: //degrees
499 return QChar(0x00B0);
500 case 1: //radians
501 return "rd";
502 case 2: //grades
503 return "gr";
504 default:
505 assert(false);
506 }
507
508 return "none";
509 }
510
511 double DistanceMapGenerationDlg::getSpinboxAngularValue(QDoubleSpinBox* spinBox,
512 DistanceMapGenerationDlg::ANGULAR_UNIT destUnit) const
513 {
514 //no conversion necessary?
515 if (m_angularUnits == destUnit)
516 return spinBox->value();
517
518 //otherwise we convert to radians first
519 double angle_rad = ConvertAngleToRad(spinBox->value(), m_angularUnits);
520 //then to the destination value
521 return ConvertAngleFromRad(angle_rad, destUnit);
522 }
523
524 void DistanceMapGenerationDlg::updateZoom(ccBBox& box)
525 {
526 if (!m_window || !box.isValid())
527 return;
528
529 //equivalent to 'ccGLWindowInterface::updateConstellationCenterAndZoom' but we take aspect ratio into account
530
531 //we get the bounding-box diagonal length
532 PointCoordinateType bbDiag = box.getDiagNorm();
533 if (CCCoreLib::GreaterThanEpsilon(bbDiag))
534 {
535 bool sfDisplayed = m_window->getAssociatedScalarField() && m_window->sfShown();
536 bool yLabelDisplayed = m_yLabels && m_yLabels->isVisible() && m_yLabels->size();
537 float centerPos = 0.5f;
538
539 ccViewportParameters params = m_window->getViewportParameters();
540
541 //we compute the focal distance so that the map appears at the right 'scale'
542 {
543 int screenWidth = m_window->glWidth();
544 int scaleWidth = 0;
545 int labelsWidth = 0;
546
547 QFont labelFont = m_window->getTextDisplayFont();
548 labelFont.setPointSize(fontSizeSpinBox->value());
549 QFontMetrics fm(labelFont);
550
551 if (sfDisplayed)
552 {
553 scaleWidth = m_window->getDisplayParameters().colorScaleRampWidth + fm.width("123.456789");
554 scaleWidth += scaleWidth / 10; // add 10% of margin
555 }
556 if (yLabelDisplayed)
557 {
558 //find the largest label width
559 int maxWidth = 0;
560 for (unsigned i = 0; i < m_yLabels->size(); ++i)
561 {
562 QString label = m_yLabels->getLabel(i);
563 if (!label.isNull())
564 {
565 int width = fm.width(label);
566 maxWidth = std::max(maxWidth, width);
567 }
568 }
569 labelsWidth = maxWidth;
570 labelsWidth += labelsWidth / 10; // add 10% of margin
571 }
572
573 //available room for the map
574 int mapWidth = std::max(1, screenWidth - scaleWidth - labelsWidth);
575
576 //we zoom so that the map takes all the room left
577 float mapPart = static_cast<float>(mapWidth) / static_cast<float>(screenWidth);
578
579 //we must also center the camera on the right position so that the map
580 //appears in between the scale and the color ramp
581 float mapStart = static_cast<float>(labelsWidth) / static_cast<float>(screenWidth);
582 centerPos = (0.5f - mapStart) / mapPart;
583
584 //update pixel size accordingly
585 float screenHeight = m_window->glHeight();
586 double pixelSize = std::max(box.getDiagVec().x * m_window->getDisplayScale().x / mapWidth, box.getDiagVec().y * m_window->getDisplayScale().y / screenHeight);
587 double distanceToWidthRatio = params.computeDistanceToWidthRatio();
588 double focalDistance = (pixelSize * screenWidth) / (distanceToWidthRatio);
589 params.setFocalDistance(focalDistance);
590 }
591
592 //we set the pivot point on the box center
593 CCVector3 P = box.getCenter();
594 if (centerPos != 0.5f) //if we don't look exactly at the center of the map
595 {
596 P.x = box.minCorner().x * (1.0f - centerPos) + box.maxCorner().x * centerPos;
597 }
598
599 CCVector3d pivotPoint = P;
600 CCVector3d cameraCenter = pivotPoint;
601 cameraCenter.z += params.getFocalDistance();
602 params.setPivotPoint(pivotPoint, false);
603 params.setCameraCenter(cameraCenter, false);
604
605 m_window->setViewportParameters(params);
606
607 m_window->invalidateViewport();
608 m_window->invalidateVisualization();
609 m_window->deprecate3DLayer();
610 }
611
612 m_window->redraw();
613 }
614
615 void DistanceMapGenerationDlg::clearView()
616 {
617 if (!m_window)
618 return;
619
620 //remove existing sf
621 m_window->setAssociatedScalarField(nullptr);
622
623 //remove existing map (or maps?)
624 ccHObject::Container maps;
625 m_window->getOwnDB()->filterChildren(maps, false, CC_TYPES::MESH);
626 for (size_t i = 0; i < maps.size(); ++i)
627 {
628 m_window->removeFromOwnDB(maps[i]);
629 }
630
631 //remove any polylines
632 {
633 ccHObject::Container polylines;
634 m_window->getOwnDB()->filterChildren(polylines, false, CC_TYPES::POLY_LINE);
635 for (size_t i = 0; i < polylines.size(); ++i)
636 {
637 m_window->removeFromOwnDB(polylines[i]);
638 }
639 }
640 m_xLabels->setVisible(false);
641 m_yLabels->setVisible(false);
642 }
643
644 void DistanceMapGenerationDlg::update()
645 {
646 if (m_map)
647 {
648 if (getProjectionMode() == PROJ_CONICAL)
649 {
650 //we must check that the projection parameter have not changed!
651 //Otherwise the symbols will be misplaced...
652 double yMin = 0.0;
653 double yMax = 0.0;
654 double yStep = 0.0;
655 getGridYValues(yMin, yMax, yStep, ANG_RAD);
656
657 if (!m_map->conical
658 || m_map->yMin != yMin
659 || m_map->yMax != yMax
660 || m_map->conicalSpanRatio != conicSpanRatioDoubleSpinBox->value())
661 {
662 clearOverlaySymbols();
663 }
664 }
665 else if (m_map->conical)
666 {
667 //we can't keep the symbols when switching the projection mode
668 clearOverlaySymbols();
669 }
670 }
671
672 //release memory
673 m_map.clear();
674
675 //clear 3D view
676 clearView();
677
678 //update map
679 m_map = updateMap();
680 //and GUI
681 exportGroupBox->setEnabled(m_map != nullptr);
682
683 //auto update volumes
684 updateVolumes();
685
686 if (m_map && m_window)
687 {
688 ccMesh* mapMesh = nullptr;
689
690 ProjectionMode mode = getProjectionMode();
691 if (mode == PROJ_CYLINDRICAL)
692 {
693 //by default we map an image on a plane
694 double dx = static_cast<double>(m_map->xSteps) * m_map->xStep;
695 double dy = static_cast<double>(m_map->ySteps) * m_map->yStep;
696 ccGLMatrix transMat;
697 transMat.setTranslation(CCVector3( static_cast<PointCoordinateType>(dx / 2 + m_map->xMin),
698 static_cast<PointCoordinateType>(dy / 2 + m_map->yMin),
699 0));
700 ccPlane* mapPlane = new ccPlane( static_cast<PointCoordinateType>(dx),
701 static_cast<PointCoordinateType>(dy),
702 &transMat,
703 "map");
704 mapMesh = static_cast<ccMesh*>(mapPlane);
705 }
706 else //if (mode == PROJ_CONICAL) //conical projection
707 {
708 //no choice, we create a mesh
709 bool ccw = ccwCheckBox->isChecked();
710 m_map->conicalSpanRatio = conicSpanRatioDoubleSpinBox->value();
711 mapMesh = DistanceMapGenerationTool::ConvertConicalMapToMesh(m_map, ccw);
712 }
713
714 if (mapMesh)
715 {
716 mapMesh->setVisible(true);
717 mapMesh->showNormals(false);
718 m_window->addToOwnDB(mapMesh, false);
719
720 updateMapTexture();
721
722 //add a virtual scalar field for color ramp display
723 ccScalarField* sf = new ccScalarField();
724 {
725 sf->reserve(2);
726 ScalarType smin = static_cast<ScalarType>(m_map->minVal);
727 ScalarType smax = static_cast<ScalarType>(m_map->maxVal);
728 sf->addElement(smin);
729 sf->addElement(smax);
730 sf->computeMinAndMax();
731 }
732 //selected color scale
733 ccColorScale::Shared colorScale = ccColorScalesManager::GetDefaultScale();
734 if (m_colorScaleSelector)
735 colorScale = m_colorScaleSelector->getSelectedScale();
736 sf->setColorScale(colorScale);
737 sf->setColorRampSteps(colorScaleStepsSpinBox->value());
738 m_window->setAssociatedScalarField(sf);
739 }
740 else
741 {
742 m_app->dispToConsole(QString("Not enough memory to display the map!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
743 }
744 }
745
746 //update sf names, etc.
747 updateHeightUnits(); //already call 'updateOverlayGrid'!
748 //force grid update if necessary
749 //updateOverlayGrid();
750 //update zoom
751 ccBBox box = m_window ? m_window->getOwnDB()->getDisplayBB_recursive(false, m_window) : ccBBox();
752 updateZoom(box);
753
754 saveToPersistentSettings();
755 }
756
757 void DistanceMapGenerationDlg::updateHeightUnits()
758 {
759 scaleHStepDoubleSpinBox->setSuffix(QString(" ") + heightUnitLineEdit->text());
760
761 if (m_window && m_window->getAssociatedScalarField())
762 {
763 m_window->getAssociatedScalarField()->setName(qPrintable(QString("Distance (%1)").arg(getHeightUnitString())));
764 }
765
766 updateOverlayGrid();
767 }
768
769 void DistanceMapGenerationDlg::updateMapTexture()
770 {
771 if (!m_map || !m_colorScaleSelector || !m_window)
772 return;
773
774 ccHObject::Container texturedEntities;
775
776 ProjectionMode mode = getProjectionMode();
777 if (mode == PROJ_CYLINDRICAL)
778 {
779 //cylindrical projection: look for a plane
780 if (m_window->getOwnDB()->filterChildren(texturedEntities, false, CC_TYPES::PLANE) == 0)
781 return;
782 }
783 else if (mode == PROJ_CONICAL)
784 {
785 //conical projection: look for a standard mesh
786 if (m_window->getOwnDB()->filterChildren(texturedEntities, false, CC_TYPES::MESH) == 0)
787 return;
788 }
789
790 //spawn "update" dialog
791 QProgressDialog progressDlg(tr("Updating..."), QString(), 0, 0, nullptr, Qt::Popup);
792 progressDlg.setMinimumDuration(0);
793 progressDlg.setModal(true);
794 progressDlg.show();
795 QApplication::processEvents();
796
797 //current color scale
798 ccColorScale::Shared colorScale = m_colorScaleSelector->getSelectedScale();
799 if (!colorScale)
800 {
801 if (m_app)
802 m_app->dispToConsole(QString("No color scale chosen!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
803 return;
804 }
805
806 //create new texture QImage
807 QImage mapImage = DistanceMapGenerationTool::ConvertMapToImage(m_map, colorScale, colorScaleStepsSpinBox->value());
808 if (mapImage.isNull())
809 {
810 if (m_app)
811 m_app->dispToConsole(QString("Failed to create map texture! Not enough memory?"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
812 return;
813 }
814
815 for (ccHObject* texturedEntity : texturedEntities)
816 {
817 //we release the old texture!
818 texturedEntity->setDisplay(nullptr);
819 texturedEntity->setDisplay(m_window);
820
821 //set new image as texture
822 if (mode == PROJ_CYLINDRICAL && texturedEntity->isA(CC_TYPES::PLANE))
823 {
824 if (!static_cast<ccPlane*>(texturedEntity)->setAsTexture(mapImage))
825 {
826 if (m_app)
827 m_app->dispToConsole(QString("Not enough memory to update the map!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
828 return;
829 }
830 }
831 if (mode == PROJ_CONICAL && texturedEntity->isA(CC_TYPES::MESH))
832 {
833 ccMesh* mesh = static_cast<ccMesh*>(texturedEntity);
834 //set material
835 ccMaterialSet* materialSet = const_cast<ccMaterialSet*>(mesh->getMaterialSet());
836 assert(materialSet);
837 //remove old material (if any)
838 materialSet->clear();
839 //add new material
840 {
841 ccMaterial::Shared material(new ccMaterial("texture"));
842 material->setTexture(mapImage, QString(), false);
843 materialSet->addMaterial(material);
844 }
845 }
846 }
847
848 m_window->redraw();
849 }
850
851 void DistanceMapGenerationDlg::colorScaleChanged(int)
852 {
853 if (!m_window || !m_colorScaleSelector)
854 return;
855
856 ccScalarField* sf = m_window->getAssociatedScalarField();
857 if (sf)
858 {
859 ccColorScale::Shared colorScale = m_colorScaleSelector->getSelectedScale();
860 unsigned steps = static_cast<unsigned>(colorScaleStepsSpinBox->value());
861
862 sf->setColorScale(colorScale);
863 sf->setColorRampSteps(steps);
864
865 m_window->redraw();
866 }
867
868 //same thing with textures
869 updateMapTexture();
870 }
871
872 void DistanceMapGenerationDlg::spawnColorScaleEditor()
873 {
874 if (!m_app || !m_app->getColorScalesManager())
875 return;
876
877 ccColorScale::Shared colorScale = (m_colorScaleSelector ? m_colorScaleSelector->getSelectedScale() : m_app->getColorScalesManager()->getDefaultScale(ccColorScalesManager::BGYR));
878 ccColorScaleEditorDialog cseDlg(m_app->getColorScalesManager(), m_app, colorScale, m_app->getMainWindow());
879 if (cseDlg.exec())
880 {
881 colorScale = cseDlg.getActiveScale();
882 if (colorScale && m_colorScaleSelector)
883 {
884 m_colorScaleSelector->init(); //in fact it's a 're-init'
885 m_colorScaleSelector->setSelectedScale(colorScale->getUuid());
886 }
887
888 //save current scale manager state to persistent settings
889 m_app->getColorScalesManager()->toPersistentSettings();
890 }
891 }
892
893 //helper
894 static void SetSpinBoxValues( QDoubleSpinBox* spinBox,
895 int decimals,
896 double minVal,
897 double maxVal,
898 double step,
899 double value)
900 {
901 if (spinBox)
902 {
903 spinBox->setDecimals(decimals);
904 spinBox->setRange(minVal, maxVal);
905 spinBox->setSingleStep(step);
906 spinBox->setValue(value);
907 }
908 }
909
910 void DistanceMapGenerationDlg::angularUnitChanged(int index)
911 {
912 //backup previous value
913 double xStep_rad = getSpinboxAngularValue(xStepDoubleSpinBox, ANG_RAD);
914 double xMin_rad = getSpinboxAngularValue(xMinDoubleSpinBox, ANG_RAD);
915 double xMax_rad = getSpinboxAngularValue(xMaxDoubleSpinBox, ANG_RAD);
916 double scaleXStep_rad = getSpinboxAngularValue(scaleXStepDoubleSpinBox, ANG_RAD);
917
918 //same for latitude-related spinboxes
919 double latStep_rad = getSpinboxAngularValue(latStepDoubleSpinBox, ANG_RAD);
920 double latMin_rad = getSpinboxAngularValue(latMinDoubleSpinBox, ANG_RAD);
921 double latMax_rad = getSpinboxAngularValue(latMaxDoubleSpinBox, ANG_RAD);
922 double scaleLatStep_rad = getSpinboxAngularValue(scaleLatStepDoubleSpinBox, ANG_RAD);
923
924 switch(index)
925 {
926 case 0: //degrees
927 {
928 m_angularUnits = ANG_DEG;
929
930 SetSpinBoxValues(xStepDoubleSpinBox, 2, 0.01, 360.0, 0.1, CCCoreLib::RadiansToDegrees( xStep_rad ) );
931 SetSpinBoxValues(scaleXStepDoubleSpinBox, 2, 0.01, 360.0, 5.0, CCCoreLib::RadiansToDegrees( scaleXStep_rad ) );
932 SetSpinBoxValues(xMinDoubleSpinBox, 2, 0.0, 360.0, 5.0, CCCoreLib::RadiansToDegrees( xMin_rad ) );
933 SetSpinBoxValues(xMaxDoubleSpinBox, 2, 0.0, 360.0, 5.0, CCCoreLib::RadiansToDegrees( xMax_rad ) );
934
935 SetSpinBoxValues(latStepDoubleSpinBox, 2, 0.01, 89.99, 1.0, CCCoreLib::RadiansToDegrees( latStep_rad ) );
936 SetSpinBoxValues(scaleLatStepDoubleSpinBox, 2, 0.01, 89.99, 1.0, CCCoreLib::RadiansToDegrees( scaleLatStep_rad ) );
937 SetSpinBoxValues(latMinDoubleSpinBox, 2, -89.99, 89.99, 1.0, CCCoreLib::RadiansToDegrees( latMin_rad ) );
938 SetSpinBoxValues(latMaxDoubleSpinBox, 2, -89.99, 89.99, 1.0, CCCoreLib::RadiansToDegrees( latMax_rad) );
939
940 xMaxDoubleSpinBox->setMaximum(360.0);
941 xMaxDoubleSpinBox->setValue(360.0);
942 break;
943 }
944
945 case 1: //radians
946 {
947 m_angularUnits = ANG_RAD;
948
949 double PIx2 = 2.0*M_PI;
950 SetSpinBoxValues(xStepDoubleSpinBox, 4, 0.0001, PIx2, 0.1, xStep_rad);
951 SetSpinBoxValues(scaleXStepDoubleSpinBox, 4, 0.0001, PIx2, 0.5, scaleXStep_rad);
952 SetSpinBoxValues(xMinDoubleSpinBox, 4, 0.0, PIx2, 0.5, xMin_rad);
953 SetSpinBoxValues(xMaxDoubleSpinBox, 4, 0.0, PIx2, 0.5, xMax_rad);
954
955 double PIdiv2 = M_PI/2.0-0.0001;
956 SetSpinBoxValues(scaleLatStepDoubleSpinBox, 4, 0.0001, PIdiv2, 0.3, scaleLatStep_rad);
957 SetSpinBoxValues(latStepDoubleSpinBox, 4, 0.0001, PIdiv2, 0.3, latStep_rad);
958 SetSpinBoxValues(latMinDoubleSpinBox, 4, -PIdiv2, PIdiv2, 0.3, latMin_rad);
959 SetSpinBoxValues(latMaxDoubleSpinBox, 4, -PIdiv2, PIdiv2, 0.3, latMax_rad);
960
961 xMaxDoubleSpinBox->setMaximum(PIx2);
962 xMaxDoubleSpinBox->setValue(PIx2);
963 break;
964 }
965
966 case 2: //grades
967 {
968 m_angularUnits = ANG_GRAD;
969
970 SetSpinBoxValues(xStepDoubleSpinBox, 2, 0.01, 400.0, 0.1, xStep_rad * 200.0 / M_PI);
971 SetSpinBoxValues(scaleXStepDoubleSpinBox, 2, 0.01, 400.0, 5.0, scaleXStep_rad * 200.0 / M_PI);
972 SetSpinBoxValues(xMinDoubleSpinBox, 2, 0.0, 400.0, 5.0, xMin_rad * 200.0 / M_PI);
973 SetSpinBoxValues(xMaxDoubleSpinBox, 2, 0.0, 400.0, 5.0, xMax_rad * 200.0 / M_PI);
974
975 SetSpinBoxValues(scaleLatStepDoubleSpinBox, 2, 0.01, 99.99, 1.0, scaleLatStep_rad * 200.0 / M_PI);
976 SetSpinBoxValues(latStepDoubleSpinBox, 2, 0.01, 99.99, 1.0, latStep_rad * 200.0 / M_PI);
977 SetSpinBoxValues(latMinDoubleSpinBox, 2, -99.99, 99.99, 1.0, latMin_rad * 200.0 / M_PI);
978 SetSpinBoxValues(latMaxDoubleSpinBox, 2, -99.99, 99.99, 1.0, latMax_rad * 200.0 / M_PI);
979
980 xMaxDoubleSpinBox->setMaximum(400.0);
981 xMaxDoubleSpinBox->setValue(400.0);
982 break;
983 }
984
985 default: //shouldn't happen!
986 assert(false);
987 }
988
989 //update spinboxes suffix
990 {
991 QString suffix = QString(" ") + getAngularUnitString();
992 scaleXStepDoubleSpinBox->setSuffix(suffix);
993 latMinDoubleSpinBox->setSuffix(suffix);
994 latMaxDoubleSpinBox->setSuffix(suffix);
995 scaleLatStepDoubleSpinBox->setSuffix(suffix);
996 }
997
998 updateOverlayGrid();
999 }
1000
1001 void DistanceMapGenerationDlg::getGridXValues(double& minX, double& maxX, double& step, ANGULAR_UNIT unit/*=ANG_RAD*/) const
1002 {
1003 minX = getSpinboxAngularValue(xMinDoubleSpinBox, unit);
1004 maxX = getSpinboxAngularValue(xMaxDoubleSpinBox, unit);
1005 step = getSpinboxAngularValue(xStepDoubleSpinBox, unit);
1006 }
1007
1008 void DistanceMapGenerationDlg::getGridYValues(double& minY, double& maxY, double& step, ANGULAR_UNIT unit/*=ANG_RAD*/) const
1009 {
1010 switch (getProjectionMode())
1011 {
1012 case PROJ_CYLINDRICAL:
1013 minY = hMinDoubleSpinBox->value();
1014 maxY = hMaxDoubleSpinBox->value();
1015 step = hStepDoubleSpinBox->value();
1016 break;
1017 case PROJ_CONICAL:
1018 minY = getSpinboxAngularValue(latMinDoubleSpinBox, unit);
1019 maxY = getSpinboxAngularValue(latMaxDoubleSpinBox, unit);
1020 step = getSpinboxAngularValue(latStepDoubleSpinBox, unit);
1021 break;
1022 default:
1023 assert(false);
1024 break;
1025 }
1026 }
1027
1028 double DistanceMapGenerationDlg::getScaleYStep(ANGULAR_UNIT unit/*=ANG_RAD*/) const
1029 {
1030 if (getProjectionMode() == PROJ_CYLINDRICAL)
1031 return scaleHStepDoubleSpinBox->value();
1032 else
1033 return getSpinboxAngularValue(scaleLatStepDoubleSpinBox, unit);
1034 }
1035
1036 void DistanceMapGenerationDlg::updateProfileRevolDim(int dim)
1037 {
1038 if (!m_profile)
1039 {
1040 assert(false);
1041 return;
1042 }
1043
1044 //update projection dimension
1045 assert(dim >= 0 && dim < 3);
1046 DistanceMapGenerationTool::SetPoylineRevolDim(m_profile, dim);
1047 }
1048
1049 void DistanceMapGenerationDlg::updateProfileOrigin()
1050 {
1051 if (!m_profile)
1052 {
1053 assert(false);
1054 return;
1055 }
1056
1057 DistanceMapGenerationTool::ProfileMetaData profileDesc;
1058 if (!DistanceMapGenerationTool::GetPoylineMetaData(m_profile, profileDesc))
1059 {
1060 assert(false);
1061 return;
1062 }
1063
1064 //update origin
1065 CCVector3 origin( static_cast<PointCoordinateType>(xOriginDoubleSpinBox->value()),
1066 static_cast<PointCoordinateType>(yOriginDoubleSpinBox->value()),
1067 static_cast<PointCoordinateType>(zOriginDoubleSpinBox->value()) );
1068
1069 //DGM: we must compensate for the change of shift along the revolution axis!
1070 double dShift = origin.u[profileDesc.revolDim] - profileDesc.origin.u[profileDesc.revolDim];
1071 profileDesc.heightShift -= dShift;
1072
1073 DistanceMapGenerationTool::SetPoylineOrigin(m_profile, origin);
1074 DistanceMapGenerationTool::SetPolylineHeightShift(m_profile, profileDesc.heightShift);
1075
1076 if (dShift != 0)
1077 {
1078 clearOverlaySymbols(); //symbols placement depend on the origin position along the revolution axis
1079 }
1080 updateMinAndMaxLimits();
1081 }
1082
1083 void DistanceMapGenerationDlg::updateGridSteps()
1084 {
1085 //angular step
1086 QString xStepsStr;
1087 {
1088 double minX = 0.0;
1089 double maxX = 0.0;
1090 double step = 0.0;
1091 getGridXValues(minX, maxX, step, m_angularUnits);
1092 xStepsStr = (step > 0 ? QString::number(ceil(std::max(maxX - minX, 0.0) / step)) : "inf");
1093 }
1094
1095 //Y step
1096 QString yStepsStr;
1097 {
1098 double minY = 0.0;
1099 double maxY = 0.0;
1100 double step = 0.0;
1101 getGridYValues(minY, maxY, step, m_angularUnits);
1102 yStepsStr = (step > 0 ? QString::number(ceil(std::max(maxY - minY, 0.0) / step)) : "inf");
1103 }
1104
1105 gridSizeLabel->setText(QString("%1 x %2").arg(xStepsStr).arg(yStepsStr));
1106 }
1107
1108 double DistanceMapGenerationDlg::getBaseRadius() const
1109 {
1110 return getProjectionMode() == PROJ_CONICAL ? 1.0 : baseRadiusDoubleSpinBox->value();
1111 }
1112
1113 void DistanceMapGenerationDlg::baseRadiusChanged(double)
1114 {
1115 if (!m_window)
1116 return;
1117
1118 CCVector2d displayScale(getBaseRadius(), 1.0);
1119 m_window->setDisplayScale(displayScale);
1120 m_window->redraw();
1121 }
1122
1123 QString DistanceMapGenerationDlg::getHeightUnitString() const
1124 {
1125 return heightUnitLineEdit->text();
1126 }
1127
1128 DistanceMapGenerationTool::FillStrategyType DistanceMapGenerationDlg::getFillingStrategy() const
1129 {
1130 switch (fillingStrategyComboxBox->currentIndex())
1131 {
1132 case 0:
1133 return DistanceMapGenerationTool::FILL_STRAT_MIN_DIST;
1134 case 1:
1135 return DistanceMapGenerationTool::FILL_STRAT_AVG_DIST;
1136 case 2:
1137 return DistanceMapGenerationTool::FILL_STRAT_MAX_DIST;
1138 default:
1139 return DistanceMapGenerationTool::INVALID_STRATEGY_TYPE;
1140 }
1141 return DistanceMapGenerationTool::INVALID_STRATEGY_TYPE;
1142 }
1143
1144 DistanceMapGenerationTool::EmptyCellFillOption DistanceMapGenerationDlg::getEmptyCellFillingOption() const
1145 {
1146 switch (emptyCellsComboBox->currentIndex())
1147 {
1148 case 0:
1149 return DistanceMapGenerationTool::LEAVE_EMPTY;
1150 case 1:
1151 return DistanceMapGenerationTool::FILL_WITH_ZERO;
1152 case 2:
1153 return DistanceMapGenerationTool::FILL_INTERPOLATE;
1154 default:
1155 return DistanceMapGenerationTool::LEAVE_EMPTY;
1156 }
1157 return DistanceMapGenerationTool::LEAVE_EMPTY;
1158 }
1159
1160 QSharedPointer<DistanceMapGenerationTool::Map> DistanceMapGenerationDlg::updateMap()
1161 {
1162 if (!m_cloud || !m_sf || !m_profile)
1163 {
1164 assert(false);
1165 return QSharedPointer<DistanceMapGenerationTool::Map>(nullptr);
1166 }
1167
1168 //profile parameters
1169 DistanceMapGenerationTool::ProfileMetaData profileDesc;
1170 if (!DistanceMapGenerationTool::GetPoylineMetaData(m_profile, profileDesc))
1171 {
1172 assert(false);
1173 return QSharedPointer<DistanceMapGenerationTool::Map>(nullptr);
1174 }
1175
1176 //compute transformation from cloud to the surface (of revolution)
1177 ccGLMatrix cloudToSurface = profileDesc.computeCloudToSurfaceOriginTrans();
1178
1179 //steps
1180 double angStep_rad = getSpinboxAngularValue(xStepDoubleSpinBox, ANG_RAD);
1181 //CW (clockwise) or CCW (counterclockwise)
1182 bool ccw = ccwCheckBox->isChecked();
1183
1184 //Y values
1185 double yMin = 0.0;
1186 double yMax = 0.0;
1187 double yStep = 0.0;
1188 getGridYValues(yMin, yMax, yStep, ANG_RAD);
1189
1190 //generate map
1191 return DistanceMapGenerationTool::CreateMap(m_cloud,
1192 m_sf,
1193 cloudToSurface,
1194 profileDesc.revolDim,
1195 angStep_rad,
1196 yStep,
1197 yMin,
1198 yMax,
1199 getProjectionMode() == PROJ_CONICAL,
1200 ccw,
1201 getFillingStrategy(),
1202 getEmptyCellFillingOption(),
1203 m_app);
1204 }
1205
1206 void DistanceMapGenerationDlg::exportMapAsCloud()
1207 {
1208 if (!m_map)
1209 {
1210 if (m_app)
1211 m_app->dispToConsole(QString("Invalid map! Try to refresh it?"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1212 return;
1213 }
1214 if (!m_profile)
1215 {
1216 if (m_app)
1217 m_app->dispToConsole(QString("Invalid profile?!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1218 return;
1219 }
1220
1221 double baseRadius = getBaseRadius();
1222
1223 ccPointCloud* cloud = DistanceMapGenerationTool::ConvertMapToCloud(m_map, m_profile, baseRadius);
1224 if (cloud)
1225 {
1226 if (m_colorScaleSelector && cloud->getCurrentDisplayedScalarField())
1227 {
1228 cloud->getCurrentDisplayedScalarField()->setColorScale(m_colorScaleSelector->getSelectedScale());
1229 }
1230 cloud->setName(m_cloud->getName() + QString(".map(%1,%2)").arg(m_map->xSteps).arg(m_map->ySteps));
1231
1232 if (m_app)
1233 {
1234 m_app->addToDB(cloud);
1235 }
1236 }
1237 }
1238
1239 void DistanceMapGenerationDlg::exportMapAsMesh()
1240 {
1241 if (!m_profile || !m_colorScaleSelector)
1242 {
1243 assert(false);
1244 return;
1245 }
1246
1247 if (!m_map)
1248 {
1249 if (m_app)
1250 m_app->dispToConsole(QString("Invalid map! Try to refresh it?"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1251 return;
1252 }
1253
1254 //profile parameters
1255 DistanceMapGenerationTool::ProfileMetaData profileDesc;
1256 if (!DistanceMapGenerationTool::GetPoylineMetaData(m_profile, profileDesc))
1257 {
1258 assert(false);
1259 return;
1260 }
1261
1262 ccColorScale::Shared colorScale = m_colorScaleSelector->getSelectedScale();
1263
1264 //create new texture QImage
1265 QImage mapImage = DistanceMapGenerationTool::ConvertMapToImage(m_map, colorScale, colorScaleStepsSpinBox->value());
1266 if (mapImage.isNull())
1267 {
1268 if (m_app)
1269 m_app->dispToConsole(QString("Failed to generate mesh texture! Not enough memory?"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1270 return;
1271 }
1272
1273 //compute transformation from cloud to the profile (origin)
1274 ccGLMatrix cloudToProfile = profileDesc.computeCloudToProfileOriginTrans();
1275 ccMesh* mesh = DistanceMapGenerationTool::ConvertProfileToMesh(m_profile, cloudToProfile, m_map->counterclockwise, m_map->xSteps, mapImage);
1276 if (mesh)
1277 {
1278 mesh->setDisplay_recursive(m_cloud->getDisplay());
1279 mesh->setName(m_cloud->getName() + QString(".map(%1,%2)").arg(m_map->xSteps).arg(m_map->ySteps));
1280 if (m_app)
1281 m_app->addToDB(mesh);
1282 }
1283 else
1284 {
1285 if (m_app)
1286 m_app->dispToConsole(QString("Failed to generate mesh! Not enough memory?"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1287 }
1288 }
1289
1290 void DistanceMapGenerationDlg::exportMapAsGrid()
1291 {
1292 if (!m_map)
1293 {
1294 if (m_app)
1295 m_app->dispToConsole(QString("Invalid map! Try to refresh it?"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1296 return;
1297 }
1298
1299 //persistent settings (default export path)
1300 QSettings settings;
1301 settings.beginGroup("qSRA");
1302 QString path = settings.value("exportPath", ccFileUtils::defaultDocPath()).toString();
1303
1304 QString filter("Grid file (*.csv)");
1305
1306 //open file saving dialog
1307 QString filename = QFileDialog::getSaveFileName(nullptr, "Select output file", path, filter);
1308 if (filename.isEmpty())
1309 return;
1310
1311 //save current export path to persistent settings
1312 settings.setValue("exportPath", QFileInfo(filename).absolutePath());
1313
1314 QString xUnit = getAngularUnitString();
1315 double xConversionFactor = ConvertAngleFromRad(1.0, m_angularUnits);
1316 QString yUnit = getHeightUnitString();
1317 double yConversionFactor = 1.0;
1318
1319 if (DistanceMapGenerationTool::SaveMapAsCSVMatrix(m_map, filename, xUnit, yUnit, xConversionFactor, yConversionFactor, m_app))
1320 {
1321 if (m_app)
1322 m_app->dispToConsole(QString("File '%1' saved successfully").arg(filename), ccMainAppInterface::STD_CONSOLE_MESSAGE);
1323 }
1324 }
1325
1326 void DistanceMapGenerationDlg::exportMapAsImage()
1327 {
1328 if (!m_window)
1329 return;
1330
1331 ccRenderToFileDlg rtfDlg(m_window->glWidth(), m_window->glHeight(), m_app->getMainWindow());
1332
1333 rtfDlg.hideOptions();
1334
1335 if (rtfDlg.exec())
1336 {
1337 QApplication::processEvents();
1338 m_window->renderToFile(rtfDlg.getFilename(), rtfDlg.getZoom(), rtfDlg.dontScalePoints(), rtfDlg.renderOverlayItems());
1339 }
1340 }
1341
1342 void DistanceMapGenerationDlg::exportProfilesAsDXF()
1343 {
1344 if (!m_map || !m_profile)
1345 return;
1346
1347 DxfProfilesExportDlg dpeDlg(this);
1348
1349 if (!dpeDlg.exec())
1350 return;
1351
1352 //profile meta-data (we only need the height shift)
1353 PointCoordinateType heightShift = 0;
1354 DistanceMapGenerationTool::GetPolylineHeightShift(m_profile, heightShift);
1355
1356 DxfProfilesExporter::Parameters params;
1357 params.legendTheoProfileTitle = dpeDlg.theoNameLineEdit->text();
1358 params.legendRealProfileTitle = dpeDlg.realNameLineEdit->text();
1359 params.scaledDevUnits = dpeDlg.scaledDevUnitsLineEdit->text();
1360 params.devLabelMultCoef = dpeDlg.devValuesScaleDoubleSpinBox->value();
1361 params.devMagnifyCoef = dpeDlg.magnifyCoefSpinBox->value();
1362 params.precision = dpeDlg.precisionSpinBox->value();
1363
1364 /*** vertical profiles ***/
1365
1366 int angularStepCount = dpeDlg.angularStepsSpinBox->value();
1367 assert(angularStepCount >= 1);
1368 //we take the same steps as the overlay grid for labels
1369 QString vertFilename = dpeDlg.getVertFilename();
1370 if (!vertFilename.isNull())
1371 {
1372 //generate profiles titles
1373 params.profileTitles.clear();
1374 QString vertProfileBaseTitle = dpeDlg.vertTitleLineEdit->text();
1375 for (int i = 0; i < angularStepCount; ++i)
1376 {
1377 double angle_rad = static_cast<double>(i) * 2.0 * M_PI / angularStepCount;
1378 double angle_cur = ConvertAngleFromRad(angle_rad, m_angularUnits);
1379 params.profileTitles << QString("%1 - %2 %3").arg(vertProfileBaseTitle).arg(angle_cur).arg(getAngularUnitString());
1380 }
1381
1382 double heightStep = getScaleYStep();
1383 if (!DxfProfilesExporter::SaveVerticalProfiles( m_map,
1384 m_profile,
1385 vertFilename,
1386 angularStepCount,
1387 heightStep,
1388 heightShift,
1389 params,
1390 m_app))
1391 {
1392 if (m_app)
1393 m_app->dispToConsole(QString("Failed to save file '%1'!").arg(vertFilename), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1394 return;
1395 }
1396 else
1397 {
1398 if (m_app)
1399 m_app->dispToConsole(QString("File '%1' saved successfully").arg(vertFilename), ccMainAppInterface::STD_CONSOLE_MESSAGE);
1400 }
1401 }
1402
1403 /*** horizontal profiles ***/
1404
1405 QString horizFilename = dpeDlg.getHorizFilename();
1406 int heightStepCount = dpeDlg.heightStepsSpinBox->value();
1407 assert(heightStepCount >= 1);
1408 //we take the same steps as the overlay grid for labels
1409 if (!horizFilename.isNull())
1410 {
1411 //generate profiles titles
1412 params.profileTitles.clear();
1413 QString horizProfileBaseTitle = dpeDlg.horizTitleLineEdit->text();
1414 params.profileTitles << QString("%1 - %2 ").arg(horizProfileBaseTitle).arg("%1") + getHeightUnitString();
1415
1416 double angleStep_rad = getSpinboxAngularValue(scaleXStepDoubleSpinBox, ANG_RAD);
1417 if (!DxfProfilesExporter::SaveHorizontalProfiles( m_map,
1418 m_profile,
1419 horizFilename,
1420 heightStepCount,
1421 heightShift,
1422 angleStep_rad,
1423 ConvertAngleFromRad(1.0,m_angularUnits),
1424 getCondensedAngularUnitString(),
1425 params,
1426 m_app))
1427 {
1428 if (m_app)
1429 m_app->dispToConsole(QString("Failed to save file '%1'!").arg(horizFilename), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1430 return;
1431 }
1432 else
1433 {
1434 if (m_app)
1435 m_app->dispToConsole(QString("File '%1' saved successfully").arg(horizFilename), ccMainAppInterface::STD_CONSOLE_MESSAGE);
1436 }
1437 }
1438 }
1439
1440 void DistanceMapGenerationDlg::loadOverlaySymbols()
1441 {
1442 //need a valid map
1443 if (!m_map)
1444 {
1445 if (m_app)
1446 m_app->dispToConsole(QString("Generate a valid map first!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1447 return;
1448 }
1449
1450 //profile parameters
1451 DistanceMapGenerationTool::ProfileMetaData profileDesc;
1452 if (!DistanceMapGenerationTool::GetPoylineMetaData(m_profile, profileDesc))
1453 {
1454 assert(false);
1455 return;
1456 }
1457
1458 //persistent settings (default import path)
1459 QSettings settings;
1460 settings.beginGroup("qSRA");
1461 QString path = settings.value("importPath", ccFileUtils::defaultDocPath()).toString();
1462
1463 QString filter("Symbols (*.txt)");
1464
1465 //open file loading dialog
1466 QString filename = QFileDialog::getOpenFileName(nullptr, "Select symbols file", path, filter);
1467 if (filename.isEmpty())
1468 return;
1469
1470 QFileInfo fileInfo(filename);
1471 if (!fileInfo.exists()) //?!
1472 {
1473 if (m_app)
1474 m_app->dispToConsole(QString("Failed to find symbol file '%1'?!").arg(filename), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1475 return;
1476 }
1477
1478 //save current impoort path to persistent settings
1479 settings.setValue("importPath", fileInfo.absolutePath());
1480
1481 ccSymbolCloud* symbolCloud = nullptr;
1482 //try to load the file (as a "symbol" point cloud)
1483 {
1484 QFile file(filename);
1485 assert(file.exists());
1486 if (!file.open(QFile::ReadOnly))
1487 {
1488 if (m_app)
1489 m_app->dispToConsole(QString("Failed to open symbol file '%1'!").arg(filename), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1490 return;
1491 }
1492
1493 symbolCloud = new ccSymbolCloud(fileInfo.baseName());
1494
1495 QTextStream stream(&file);
1496 QString currentLine = stream.readLine();
1497 bool error = false;
1498 while (!currentLine.isNull())
1499 {
1500 QStringList tokens = currentLine.simplified().split(QChar(' '), QString::SkipEmptyParts);
1501 if (tokens.size() == 4)
1502 {
1503 bool okX = false;
1504 bool okY = false;
1505 bool okZ = false;
1506 CCVector3 P(static_cast<PointCoordinateType>(tokens[1].toDouble(&okX)),
1507 static_cast<PointCoordinateType>(tokens[2].toDouble(&okY)),
1508 static_cast<PointCoordinateType>(tokens[3].toDouble(&okZ)));
1509
1510 if (!okX || !okY || !okZ)
1511 {
1512 error = true;
1513 break;
1514 }
1515
1516 QString label = tokens[0];
1517 if (symbolCloud->size() == symbolCloud->capacity())
1518 {
1519 if (!symbolCloud->reserveThePointsTable(symbolCloud->size() + 64) || !symbolCloud->reserveLabelArray(symbolCloud->size() + 64))
1520 {
1521 if (m_app)
1522 m_app->dispToConsole(QString("Not enough memory!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1523 error = true;
1524 break;
1525 }
1526 }
1527
1528 //DGM: warning, for historical reasons height values are expressed relative to the profile origin!
1529 P.u[profileDesc.revolDim] += profileDesc.origin.u[profileDesc.revolDim];
1530
1531 symbolCloud->addPoint(P);
1532 symbolCloud->addLabel(label);
1533 }
1534
1535 //next line
1536 currentLine = stream.readLine();
1537 }
1538
1539 if (symbolCloud->size() == 0)
1540 {
1541 delete symbolCloud;
1542 symbolCloud = nullptr;
1543 }
1544 else
1545 {
1546 symbolCloud->shrinkToFit();
1547 }
1548
1549 if (error)
1550 {
1551 delete symbolCloud;
1552 symbolCloud = nullptr;
1553
1554 if (m_app)
1555 m_app->dispToConsole(QString("An error occurred while loading the file! Result may be incomplete"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1556 return;
1557 }
1558 }
1559
1560 if (symbolCloud)
1561 {
1562 //unroll the symbol cloud the same way as the input cloud
1563 if (m_window)
1564 {
1565 //compute transformation from cloud to the surface (of revolution)
1566 ccGLMatrix cloudToSurface = profileDesc.computeCloudToSurfaceOriginTrans();
1567 //CW (clockwise) or CCW (counterclockwise)
1568 bool ccw = ccwCheckBox->isChecked();
1569
1570 if (getProjectionMode() == PROJ_CYLINDRICAL)
1571 {
1572 DistanceMapGenerationTool::ConvertCloudToCylindrical( symbolCloud,
1573 cloudToSurface,
1574 profileDesc.revolDim,
1575 ccw);
1576 }
1577 else /*if (getProjectionMode() == PROJ_CONICAL)*/
1578 {
1579 double conicalSpanRatio = conicSpanRatioDoubleSpinBox->value();
1580 DistanceMapGenerationTool::ConvertCloudToConical( symbolCloud,
1581 cloudToSurface,
1582 profileDesc.revolDim,
1583 m_map->yMin,
1584 m_map->yMax,
1585 conicalSpanRatio,
1586 ccw);
1587 }
1588 }
1589 symbolCloud->setSymbolSize(static_cast<double>(symbolSizeSpinBox->value()));
1590 symbolCloud->setFontSize(fontSizeSpinBox->value());
1591 symbolCloud->setVisible(true);
1592 symbolCloud->setDisplay(m_window);
1593 ccColor::Rgb rgb( static_cast<ColorCompType>(m_symbolColor.red()),
1594 static_cast<ColorCompType>(m_symbolColor.green()),
1595 static_cast<ColorCompType>(m_symbolColor.blue()) );
1596 symbolCloud->setTempColor(rgb, true);
1597 if (m_window != nullptr)
1598 {
1599 m_window->addToOwnDB(symbolCloud, false);
1600 m_window->redraw();
1601 }
1602
1603 clearLabelsPushButton->setEnabled(true);
1604 clearLabelsPushButton->setText(QString("Clear (%1)").arg(symbolCloud->size()));
1605 }
1606 else
1607 {
1608 assert(false);
1609 delete symbolCloud;
1610 symbolCloud = nullptr;
1611 }
1612 }
1613
1614 void DistanceMapGenerationDlg::clearOverlaySymbols()
1615 {
1616 if (!m_window)
1617 return;
1618
1619 ccHObject::Container clouds;
1620 m_window->getOwnDB()->filterChildren(clouds, false, CC_TYPES::POINT_CLOUD);
1621
1622 for (size_t i = 0; i < clouds.size(); ++i)
1623 if (clouds[i] != m_xLabels && clouds[i] != m_yLabels)
1624 m_window->removeFromOwnDB(clouds[i]);
1625
1626 clearLabelsPushButton->setEnabled(false);
1627 clearLabelsPushButton->setText("Clear");
1628 m_window->redraw();
1629 }
1630
1631 void DistanceMapGenerationDlg::overlaySymbolsSizeChanged(int size)
1632 {
1633 if (!m_window)
1634 return;
1635
1636 double symbolSize = (double)symbolSizeSpinBox->value();
1637
1638 ccHObject* db = m_window->getOwnDB();
1639 for (unsigned i = 0; i < db->getChildrenNumber(); ++i)
1640 {
1641 ccHObject* child = db->getChild(i);
1642 if (child->isA(CC_TYPES::POINT_CLOUD)
1643 && child != m_xLabels && child != m_yLabels) //don't modify the X an Y label clouds!
1644 {
1645 static_cast<ccSymbolCloud*>(child)->setSymbolSize(symbolSize);
1646 }
1647 }
1648 m_window->redraw();
1649 }
1650
1651 void DistanceMapGenerationDlg::overlaySymbolsColorChanged()
1652 {
1653 ccQtHelpers::SetButtonColor(symbolColorButton, m_symbolColor);
1654
1655 if (!m_window)
1656 return;
1657
1658 ccColor::Rgb rgb( static_cast<ColorCompType>(m_symbolColor.red()),
1659 static_cast<ColorCompType>(m_symbolColor.green()),
1660 static_cast<ColorCompType>(m_symbolColor.blue()) );
1661
1662 ccHObject* db = m_window->getOwnDB();
1663 for (unsigned i = 0; i < db->getChildrenNumber(); ++i)
1664 {
1665 ccHObject* child = db->getChild(i);
1666 if (child->isA(CC_TYPES::POINT_CLOUD)
1667 && child != m_xLabels && child != m_yLabels) //don't modify the X an Y label clouds!
1668 {
1669 child->setTempColor(rgb, true);
1670 }
1671 }
1672
1673 m_window->redraw();
1674 }
1675
1676 void DistanceMapGenerationDlg::overlayGridColorChanged()
1677 {
1678 ccQtHelpers::SetButtonColor(gridColorButton, m_gridColor);
1679
1680 if (!m_window)
1681 return;
1682
1683 ccColor::Rgb rgb( static_cast<ColorCompType>(m_gridColor.red()),
1684 static_cast<ColorCompType>(m_gridColor.green()),
1685 static_cast<ColorCompType>(m_gridColor.blue()) );
1686
1687 ccHObject* db = m_window->getOwnDB();
1688 for (unsigned i = 0; i < db->getChildrenNumber(); ++i)
1689 {
1690 ccHObject* child = db->getChild(i);
1691 if (child->isA(CC_TYPES::POLY_LINE))
1692 {
1693 static_cast<ccPolyline*>(child)->setColor(rgb);
1694 }
1695 }
1696
1697 m_xLabels->setTempColor(rgb, true);
1698 m_yLabels->setTempColor(rgb, true);
1699
1700 m_window->redraw();
1701 }
1702
1703 void DistanceMapGenerationDlg::labelFontSizeChanged(int)
1704 {
1705 if (!m_window)
1706 return;
1707
1708 int fontSize = fontSizeSpinBox->value();
1709
1710 ccHObject* db = m_window->getOwnDB();
1711 for (unsigned i = 0; i < db->getChildrenNumber(); ++i)
1712 {
1713 ccHObject* child = db->getChild(i);
1714 if (child->isA(CC_TYPES::POINT_CLOUD))
1715 {
1716 static_cast<ccSymbolCloud*>(child)->setFontSize(fontSize);
1717 }
1718 }
1719
1720 //update window font-size
1721 ccGui::ParamStruct params = m_window->getDisplayParameters();
1722 params.defaultFontSize = fontSize;
1723 m_window->setDisplayParameters(params, true);
1724
1725 m_window->redraw();
1726 }
1727
1728 void DistanceMapGenerationDlg::labelPrecisionChanged(int prec)
1729 {
1730 if (!m_window)
1731 return;
1732
1733 //update numerical precision
1734 ccGui::ParamStruct params = m_window->getDisplayParameters();
1735 params.displayedNumPrecision = prec;
1736 m_window->setDisplayParameters(params, true);
1737
1738 m_window->redraw();
1739 }
1740
1741 void DistanceMapGenerationDlg::colorRampStepsChanged(int)
1742 {
1743 colorScaleChanged(-1); //dummy index, not used
1744 }
1745
1746 void DistanceMapGenerationDlg::updateOverlayGrid()
1747 {
1748 toggleOverlayGrid(overlayGridGroupBox->isChecked());
1749 }
1750
1751 void DistanceMapGenerationDlg::toggleOverlayGrid(bool state)
1752 {
1753 if (!m_window)
1754 return;
1755
1756 assert(m_xLabels && m_yLabels);
1757 if (!m_xLabels || !m_yLabels)
1758 return;
1759
1760 //remove any polylines
1761 {
1762 ccHObject::Container polylines;
1763 m_window->getOwnDB()->filterChildren(polylines, false, CC_TYPES::POLY_LINE);
1764 for (size_t i = 0; i < polylines.size(); ++i)
1765 m_window->removeFromOwnDB(polylines[i]);
1766 }
1767 //and labels
1768 m_xLabels->clear();
1769 m_yLabels->clear();
1770 m_xLabels->setVisible(state && xScaleCheckBox->isChecked());
1771 m_yLabels->setVisible(state && yScaleCheckBox->isChecked());
1772
1773 if (state && m_map) //on
1774 {
1775 ccColor::Rgb rgb( static_cast<ColorCompType>(m_gridColor.red()),
1776 static_cast<ColorCompType>(m_gridColor.green()),
1777 static_cast<ColorCompType>(m_gridColor.blue()) );
1778
1779 //we reconstruct the grid and the corresponding labels
1780 double xMin_rad = 0.0;
1781 double xMax_rad = 0.0;
1782 double xStep_rad = 0.0;
1783 getGridXValues(xMin_rad, xMax_rad, xStep_rad, ANG_RAD);
1784 double scaleXStep_rad = getSpinboxAngularValue(scaleXStepDoubleSpinBox, ANG_RAD);
1785
1786 double yMin = 0.0;
1787 double yMax = 0.0;
1788 double yStep = 0.0;
1789 getGridYValues(yMin, yMax, yStep, ANG_RAD);
1790 double scaleYStep = getScaleYStep(ANG_RAD);
1791
1792 if (scaleXStep_rad == 0 || scaleYStep == 0)
1793 {
1794 if (m_app)
1795 m_app->dispToConsole(QString("Internal error: invalid step values?!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1796 m_window->redraw();
1797 return;
1798 }
1799
1800 unsigned xStepCount = static_cast<unsigned>(ceil(std::max(xMax_rad - xMin_rad, 0.0) / scaleXStep_rad));
1801 unsigned yStepCount = static_cast<unsigned>(ceil(std::max(yMax - yMin, 0.0) / scaleYStep));
1802
1803 //correct 'xMax' and 'yMax'
1804 xMax_rad = xMin_rad + static_cast<double>(xStepCount) * scaleXStep_rad;
1805 yMax = yMin + static_cast<double>(yStepCount) * scaleYStep;
1806
1807 //projection mode
1808 ProjectionMode mode = getProjectionMode();
1809 double nProj = 1.0;
1810 if (mode == PROJ_CONICAL)
1811 {
1812 double conicalSpanRatio = conicSpanRatioDoubleSpinBox->value();
1813 nProj = DistanceMapGenerationTool::ConicalProjectN(m_map->yMin, m_map->yMax) * conicalSpanRatio;
1814 }
1815 bool ccw = ccwCheckBox->isChecked();
1816
1817 //create vertical polylines
1818 {
1819 QString angularUnitsStr = getCondensedAngularUnitString();
1820 if (m_xLabels->isVisible())
1821 {
1822 if (!m_xLabels->reserve(xStepCount + 1) || !m_xLabels->reserveLabelArray(xStepCount + 1))
1823 {
1824 if (m_app)
1825 m_app->dispToConsole(QString("Not engouh memory to display the 'X' scale?!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1826 m_xLabels->clear();
1827 m_xLabels->setVisible(false);
1828 }
1829 m_xLabels->setTempColor(rgb, true);
1830 }
1831 for (unsigned i = 0; i <= xStepCount; ++i)
1832 {
1833 double angle_rad = xMin_rad + static_cast<double>(i) * scaleXStep_rad;
1834
1835 CCVector3 Pbottom( static_cast<PointCoordinateType>(angle_rad),
1836 static_cast<PointCoordinateType>(yMin),
1837 0);
1838 CCVector3 Pup( static_cast<PointCoordinateType>(angle_rad),
1839 static_cast<PointCoordinateType>(yMax),
1840 0);
1841
1842 if (mode == PROJ_CONICAL)
1843 {
1844 //vertical lines remain "straight" lines after Conical projection
1845 Pbottom = DistanceMapGenerationTool::ProjectPointOnCone(Pbottom.x, Pbottom.y, m_map->yMin, nProj, ccw);
1846 Pup = DistanceMapGenerationTool::ProjectPointOnCone(Pup.x, Pup.y, m_map->yMin, nProj, ccw);
1847 }
1848 Pbottom.z = 1.0;
1849 Pup.z = 1.0;
1850
1851 //polyline
1852 ccPointCloud* vertices = new ccPointCloud(/*QString("Angle %1").arg(static_cast<int>(CCCoreLib::RadiansToDegrees(angle_rad)))*/);
1853 vertices->reserve(2);
1854 vertices->addPoint(Pbottom);
1855 vertices->addPoint(Pup);
1856 ccPolyline* poly = new ccPolyline(vertices);
1857 poly->addPointIndex(0, 2);
1858 poly->addChild(vertices);
1859 vertices->setEnabled(false);
1860 poly->setColor(rgb);
1861 poly->showColors(true);
1862 poly->setVisible(true);
1863 poly->set2DMode(false);
1864 m_window->addToOwnDB(poly, false);
1865
1866 if (m_xLabels->isVisible())
1867 {
1868 m_xLabels->addPoint(Pbottom);
1869 m_xLabels->addLabel(QString("%1%2").arg(ConvertAngleFromRad(angle_rad, m_angularUnits), 0, 'f', m_angularUnits == ANG_RAD ? 2 : 0).arg(angularUnitsStr));
1870 }
1871 }
1872 }
1873
1874 //create horizontal polylines
1875 {
1876 if (m_yLabels->isVisible())
1877 {
1878 if (!m_yLabels->reserve(yStepCount + 1) || !m_yLabels->reserveLabelArray(yStepCount + 1))
1879 {
1880 if (m_app)
1881 m_app->dispToConsole(QString("Not enough memory to display the 'Y' scale?!"), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
1882 m_yLabels->clear();
1883 m_yLabels->setVisible(false);
1884 }
1885 m_yLabels->setTempColor(rgb, true);
1886 }
1887 for (unsigned i = 0; i <= yStepCount; ++i)
1888 {
1889 double y = yMin + static_cast<double>(i) * scaleYStep;
1890
1891 //polyline
1892 ccPointCloud* vertices = new ccPointCloud(/*QString("Height %1").arg(height)*/);
1893
1894 if (mode == PROJ_CONICAL)
1895 {
1896 //horizontal lines become "curved" lines after Conical projection!
1897 const unsigned polySteps = 100;
1898 if (vertices->reserve(polySteps + 1))
1899 for (unsigned j = 0; j <= polySteps; ++j)
1900 {
1901 double angle_rad = xMin_rad + static_cast<double>(j) / (polySteps * (xMax_rad - xMin_rad));
1902 CCVector3 P = DistanceMapGenerationTool::ProjectPointOnCone(angle_rad, y, m_map->yMin, nProj, ccw);
1903 P.z = 1.0;
1904 vertices->addPoint(P);
1905 }
1906 }
1907 else
1908 {
1909 CCVector3 Pleft( static_cast<PointCoordinateType>(xMin_rad),
1910 static_cast<PointCoordinateType>(y),
1911 CCCoreLib::PC_ONE);
1912 CCVector3 Pright( static_cast<PointCoordinateType>(xMax_rad),
1913 static_cast<PointCoordinateType>(y),
1914 CCCoreLib::PC_ONE);
1915 vertices->reserve(2);
1916 vertices->addPoint(Pleft);
1917 vertices->addPoint(Pright);
1918 }
1919
1920 unsigned vertCount = vertices->size();
1921 if (vertCount)
1922 {
1923 ccPolyline* poly = new ccPolyline(vertices);
1924 poly->addPointIndex(0, vertices->size());
1925 poly->addChild(vertices);
1926 vertices->setEnabled(false);
1927 poly->setColor(rgb);
1928 poly->showColors(true);
1929 poly->setVisible(true);
1930 poly->set2DMode(false);
1931 m_window->addToOwnDB(poly, false);
1932 }
1933 else
1934 {
1935 delete vertices;
1936 vertices = nullptr;
1937 }
1938
1939 if (mode != PROJ_CONICAL && m_yLabels->isVisible())
1940 {
1941 //cylindrical 'mode' labels
1942 CCVector3 Pleft(static_cast<PointCoordinateType>(xMin_rad),
1943 static_cast<PointCoordinateType>(y),
1944 CCCoreLib::PC_ONE);
1945 m_yLabels->addPoint(Pleft);
1946 m_yLabels->addLabel(QString("%1 %2").arg(y).arg(getHeightUnitString()));
1947 }
1948 }
1949 }
1950 }
1951
1952 m_window->redraw();
1953 }
1954
1955 void DistanceMapGenerationDlg::changeGridColor()
1956 {
1957 QColor newCol = QColorDialog::getColor(m_gridColor, this);
1958 if (!newCol.isValid())
1959 return;
1960
1961 m_gridColor = newCol;
1962
1963 overlayGridColorChanged();
1964 }
1965
1966 void DistanceMapGenerationDlg::changeSymbolColor()
1967 {
1968 QColor newCol = QColorDialog::getColor(m_symbolColor, this);
1969 if (!newCol.isValid())
1970 return;
1971
1972 m_symbolColor = newCol;
1973
1974 overlaySymbolsColorChanged();
1975 }
1976
1977 void DistanceMapGenerationDlg::toggleColorScaleDisplay(bool state)
1978 {
1979 if (m_window)
1980 {
1981 m_window->showSF(state);
1982 m_window->redraw();
1983 }
1984 }
1985
1986 void DistanceMapGenerationDlg::updateVolumes()
1987 {
1988 if (getProjectionMode() == PROJ_CONICAL)
1989 {
1990 volumeTextEdit->setText("Cylindrical projection mode only!");
1991 return;
1992 }
1993
1994 if (m_map && m_profile)
1995 {
1996 DistanceMapGenerationTool::Measures surfaces;
1997 DistanceMapGenerationTool::Measures volumes;
1998 if (DistanceMapGenerationTool::ComputeSurfacesAndVolumes(m_map, m_profile, surfaces, volumes))
1999 {
2000 QLocale locale(QLocale::English);
2001 QString text;
2002 text.append(QString("[Theoretical]\n"));
2003 text.append(QString("surface = %1\n").arg(locale.toString(surfaces.theoretical)));
2004 text.append(QString("volume = %1\n").arg(locale.toString(volumes.theoretical)));
2005 text.append(QString("\n"));
2006 text.append(QString("[Actual]\n"));
2007 text.append(QString("Surface: %1\n").arg(locale.toString(surfaces.total)));
2008 text.append(QString("Volume: %1\n").arg(locale.toString(volumes.total)));
2009 text.append(QString("\n"));
2010 text.append(QString("Positive (deviations) surface:\n%1\n").arg(locale.toString(surfaces.positive)));
2011 text.append(QString("Negative (deviations) surface:\n%1\n").arg(locale.toString(surfaces.negative)));
2012 text.append(QString("\n"));
2013 text.append(QString("Positive volume (gain of matter):\n%1\n").arg(locale.toString(volumes.positive)));
2014 text.append(QString("Negative volume (loss of matter):\n%1\n").arg(locale.toString(volumes.negative)));
2015 text.append(QString("Sum:\n%1\n").arg(locale.toString(volumes.positive + volumes.negative)));
2016 volumeTextEdit->setText(text);
2017 }
2018 else
2019 {
2020 volumeTextEdit->setText("Volume(s) computation failed!");
2021 }
2022 }
2023 else
2024 {
2025 if (!m_map)
2026 volumeTextEdit->setText("No map!");
2027 else
2028 volumeTextEdit->setText("No profile defined!");
2029 }
2030 }
2031
2032 void DistanceMapGenerationDlg::initFromPersistentSettings()
2033 {
2034 QSettings settings;
2035 settings.beginGroup("DistanceMapGenerationDialog");
2036
2037 //read parameters
2038 double conicSpanRatio = settings.value("conicSpanRatio", conicSpanRatioDoubleSpinBox->value()).toDouble();
2039 int angularUnit = settings.value("angularUnit", angularUnitComboBox->currentIndex()).toInt();
2040 QString heightUnit = settings.value("heightUnit", heightUnitLineEdit->text()).toString();
2041 double angularStep = settings.value("angularStep", xStepDoubleSpinBox->value()).toDouble();
2042 double heightStep = settings.value("heightStep", hStepDoubleSpinBox->value()).toDouble();
2043 double latitudeStep = settings.value("latitudeStep", latStepDoubleSpinBox->value()).toDouble();
2044 double scaleAngularStep = settings.value("scaleAngularStep", scaleXStepDoubleSpinBox->value()).toDouble();
2045 double scaleHeightStep = settings.value("scaleHeightStep", scaleHStepDoubleSpinBox->value()).toDouble();
2046 double scaleLatitudeStep = settings.value("scaleLatitudeStep", scaleLatStepDoubleSpinBox->value()).toDouble();
2047 bool ccw = settings.value("CCW", ccwCheckBox->isChecked()).toBool();
2048 int fillStrategy = settings.value("fillStrategy", fillingStrategyComboxBox->currentIndex()).toBool();
2049 int emptyCells = settings.value("emptyCells", emptyCellsComboBox->currentIndex()).toInt();
2050 bool showOverlayGrid = settings.value("showOverlayGrid", overlayGridGroupBox->isChecked()).toBool();
2051 bool showXScale = settings.value("showXScale", xScaleCheckBox->isChecked()).toBool();
2052 bool showYScale = settings.value("showYScale", yScaleCheckBox->isChecked()).toBool();
2053 bool showColorScale = settings.value("showColorScale", displayColorScaleCheckBox->isChecked()).toBool();
2054 QString uuid = settings.value("colorScale", QString()).toString();
2055 int colorScaleSteps = settings.value("colorScaleSteps", colorScaleStepsSpinBox->value()).toInt();
2056 int symbolSize = settings.value("symbolSize", symbolSizeSpinBox->value()).toInt();
2057 int fontSize = settings.value("fontSize", fontSizeSpinBox->value()).toInt();
2058
2059 //apply parameters
2060 conicSpanRatioDoubleSpinBox->setValue(conicSpanRatio);
2061 angularUnitComboBox->setCurrentIndex(angularUnit);
2062 angularUnitChanged(angularUnit); //force update
2063 heightUnitLineEdit->setText(heightUnit);
2064 updateHeightUnits(); //force update
2065 xStepDoubleSpinBox->setValue(angularStep);
2066 hStepDoubleSpinBox->setValue(heightStep);
2067 latStepDoubleSpinBox->setValue(latitudeStep);
2068 scaleXStepDoubleSpinBox->setValue(scaleAngularStep);
2069 scaleHStepDoubleSpinBox->setValue(scaleHeightStep);
2070 scaleLatStepDoubleSpinBox->setValue(scaleLatitudeStep);
2071 ccwCheckBox->setChecked(ccw);
2072 fillingStrategyComboxBox->setCurrentIndex(fillStrategy);
2073 emptyCellsComboBox->setCurrentIndex(emptyCells);
2074 overlayGridGroupBox->setChecked(showOverlayGrid);
2075 xScaleCheckBox->setChecked(showXScale);
2076 yScaleCheckBox->setChecked(showYScale);
2077 displayColorScaleCheckBox->setChecked(showColorScale);
2078 if (m_colorScaleSelector && !uuid.isNull())
2079 m_colorScaleSelector->setSelectedScale(uuid);
2080 colorScaleStepsSpinBox->setValue(colorScaleSteps);
2081 symbolSizeSpinBox->setValue(symbolSize);
2082 fontSizeSpinBox->setValue(fontSize);
2083
2084 settings.endGroup();
2085 }
2086
2087 void DistanceMapGenerationDlg::saveToPersistentSettings()
2088 {
2089 QSettings settings;
2090 settings.beginGroup("DistanceMapGenerationDialog");
2091
2092 //write parameters
2093 settings.setValue("conicSpanRatio", conicSpanRatioDoubleSpinBox->value());
2094 settings.setValue("angularUnit", angularUnitComboBox->currentIndex());
2095 settings.setValue("heightUnit", heightUnitLineEdit->text());
2096 settings.setValue("angularStep", xStepDoubleSpinBox->value());
2097 settings.setValue("heightStep", hStepDoubleSpinBox->value());
2098 settings.setValue("latitudeStep", latStepDoubleSpinBox->value());
2099 settings.setValue("scaleAngularStep", scaleXStepDoubleSpinBox->value());
2100 settings.setValue("scaleHeightStep", scaleHStepDoubleSpinBox->value());
2101 settings.setValue("scaleLatitudeStep", scaleLatStepDoubleSpinBox->value());
2102 settings.setValue("CCW", ccwCheckBox->isChecked());
2103 settings.setValue("fillStrategy", fillingStrategyComboxBox->currentIndex());
2104 settings.setValue("emptyCells", emptyCellsComboBox->currentIndex());
2105 settings.setValue("showOverlayGrid", overlayGridGroupBox->isChecked());
2106 settings.setValue("showXScale", xScaleCheckBox->isChecked());
2107 settings.setValue("showYScale", yScaleCheckBox->isChecked());
2108 settings.setValue("showColorScale", displayColorScaleCheckBox->isChecked());
2109 if (m_colorScaleSelector)
2110 {
2111 ccColorScale::Shared colorScale = m_colorScaleSelector->getSelectedScale();
2112 if (colorScale)
2113 settings.setValue("colorScale", colorScale->getUuid());
2114 }
2115 settings.setValue("colorScaleSteps", colorScaleStepsSpinBox->value());
2116 settings.setValue("symbolSize", symbolSizeSpinBox->value());
2117 settings.setValue("fontSize", fontSizeSpinBox->value());
2118
2119
2120 settings.endGroup();
2121 }