-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
distanceMapGenerationDlg.cpp
2054 lines (1787 loc) · 64.6 KB
/
distanceMapGenerationDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//##########################################################################
//# #
//# CLOUDCOMPARE PLUGIN: qSRA #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 or later of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: EDF #
//# #
//##########################################################################
#include "distanceMapGenerationDlg.h"
//local
#include "ccSymbolCloud.h"
#include "ccMapWindow.h"
#include "dxfProfilesExporter.h"
#include "dxfProfilesExportDlg.h"
//qCC_plugins
#include <ccMainAppInterface.h>
//qCC
#include <ccColorScaleSelector.h>
#include <ccColorScalesManager.h>
#include <ccColorScaleEditorDlg.h>
#include <ccCommon.h>
#include <ccQtHelpers.h>
#include <ccRenderToFileDlg.h>
//qCC_db
#include <ccFileUtils.h>
#include <ccPointCloud.h>
#include <ccPlane.h>
#include <ccScalarField.h>
#include <ccPolyline.h>
#include <ccMaterialSet.h>
//Qt
#include <QMainWindow>
#include <QHBoxLayout>
#include <QSettings>
#include <QFileDialog>
#include <QFileInfo>
#include <QFile>
#include <QTextStream>
#include <QProgressDialog>
#include <QColorDialog>
#include <QFontMetrics>
#include <QLocale>
//system
#include <assert.h>
//default names
static const char XLABEL_CLOUD_NAME[] = "X_Labels";
static const char YLABEL_CLOUD_NAME[] = "Y_Labels";
static const double DEFAULT_LABEL_MARGIN = 20.0; //half of this in fact (we use the 'symbol' size)
static double ConvertAngleFromRad(double angle_rad, DistanceMapGenerationDlg::ANGULAR_UNIT destUnit)
{
switch(destUnit)
{
case DistanceMapGenerationDlg::ANG_DEG: //degrees
return angle_rad * CC_RAD_TO_DEG;
case DistanceMapGenerationDlg::ANG_RAD: //radians
return angle_rad;
case DistanceMapGenerationDlg::ANG_GRAD: //grades
return angle_rad / M_PI * 200.0;
default:
assert(false);
}
return 0.0;
}
static double ConvertAngleToRad(double angle, DistanceMapGenerationDlg::ANGULAR_UNIT srcUnit)
{
switch(srcUnit)
{
case DistanceMapGenerationDlg::ANG_DEG: //degrees
return angle * CC_DEG_TO_RAD;
case DistanceMapGenerationDlg::ANG_RAD: //radians
return angle;
case DistanceMapGenerationDlg::ANG_GRAD: //grades
return angle / 200.0 * M_PI;
default:
assert(false);
}
return 0.0;
}
DistanceMapGenerationDlg::DistanceMapGenerationDlg(ccPointCloud* cloud, ccScalarField* sf, ccPolyline* polyline, ccMainAppInterface* app/*=0*/)
: QDialog(app ? app->getMainWindow() : 0)
, m_app(app)
, m_cloud(cloud)
, m_profile(polyline)
, m_sf(sf)
, m_map(0)
, m_angularUnits(ANG_GRAD)
, m_window(0)
, m_colorScaleSelector(0)
, m_xLabels(0)
, m_yLabels(0)
, m_gridColor(Qt::gray)
, m_symbolColor(Qt::black)
{
setupUi(this);
assert(m_cloud && m_sf && m_profile);
//add color ramp selector widget (before calling initFromPersistentSettings!)
if (m_sf)
{
//create selector widget
m_colorScaleSelector = new ccColorScaleSelector(m_app->getColorScalesManager(),this,QString::fromUtf8(":/CC/plugin/qSRA/gearIcon.png"));
m_colorScaleSelector->init();
m_colorScaleSelector->setSelectedScale(ccColorScalesManager::GetDefaultScale()->getUuid());
connect(m_colorScaleSelector, SIGNAL(colorScaleSelected(int)), this, SLOT(colorScaleChanged(int)));
connect(m_colorScaleSelector, SIGNAL(colorScaleEditorSummoned()), this, SLOT(spawnColorScaleEditor()));
//add selector to group's layout
if (!colorRampGroupBox->layout())
colorRampGroupBox->setLayout(new QHBoxLayout());
colorRampGroupBox->layout()->addWidget(m_colorScaleSelector);
colorScaleStepsSpinBox->setRange(ccColorScale::MIN_STEPS,ccColorScale::MAX_STEPS);
}
//init parameters from persistent settings
initFromPersistentSettings();
if (m_sf)
{
//we apply the cloud current color scale ONLY if it is not a default one
//(otherwise we keep the default dialog's one)
const ccColorScale::Shared& scale = m_sf->getColorScale();
if (scale && !scale->isLocked())
m_colorScaleSelector->setSelectedScale(scale->getUuid());
}
//profile meta-data
DistanceMapGenerationTool::ProfileMetaData profileDesc;
bool validProfile = false;
//set default dialog values with polyline & cloud information
if (m_profile)
{
validProfile = DistanceMapGenerationTool::GetPoylineMetaData(m_profile, profileDesc);
if (validProfile)
{
//update the 'Generatrix' tab
{
axisDimComboBox->setCurrentIndex(profileDesc.revolDim);
xOriginDoubleSpinBox->setValue(profileDesc.origin.x);
yOriginDoubleSpinBox->setValue(profileDesc.origin.y);
zOriginDoubleSpinBox->setValue(profileDesc.origin.z);
}
updateMinAndMaxLimits();
}
else
{
if (m_app)
m_app->dispToConsole(QString("Invalid profile: can't generate a proper map!"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
}
}
//compute min and max height of the points
//we will 'lock" the max height value with that information
if (m_cloud)
{
ccBBox bbox = m_cloud->getOwnBB();
PointCoordinateType hMin = 0, hMax = 0;
if (bbox.isValid())
{
hMin = bbox.minCorner().u[profileDesc.revolDim];
hMax = bbox.maxCorner().u[profileDesc.revolDim];
}
if (hMax - hMin <= 0)
{
if (m_app)
m_app->dispToConsole(QString("Cloud is flat: can't generate a proper map!"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
}
else
{
hStepDoubleSpinBox->setMaximum(hMax-hMin);
}
}
//add window
{
m_window = new ccMapWindow();
ccGui::ParamStruct params = m_window->getDisplayParameters();
params.backgroundCol = ccColor::white;
params.textDefaultCol = ccColor::black;
params.drawBackgroundGradient = false;
params.colorScaleShowHistogram = false;
params.colorScaleRampWidth = 30;
params.decimateMeshOnMove = false;
params.displayCross = false;
params.colorScaleUseShader = false;
m_window->setDisplayParameters(params,true);
m_window->setPerspectiveState(false,true);
m_window->setInteractionMode(ccGLWindow::INTERACT_PAN | ccGLWindow::INTERACT_CLICKABLE_ITEMS | ccGLWindow::INTERACT_ZOOM_CAMERA);
m_window->displayOverlayEntities(false);
m_window->showSF(displayColorScaleCheckBox->isChecked());
//add window to the right side layout
mapFrame->setLayout(new QHBoxLayout());
#ifdef CC_GL_WINDOW_USE_QWINDOW
mapFrame->layout()->addWidget(QWidget::createWindowContainer(m_window));
#else
mapFrame->layout()->addWidget(m_window);
#endif
precisionSpinBox->setValue(params.displayedNumPrecision);
}
//create labels "clouds" (empty)
{
m_xLabels = new ccSymbolCloud(XLABEL_CLOUD_NAME);
m_xLabels->showSymbols(false);
m_xLabels->setSymbolSize(DEFAULT_LABEL_MARGIN);
m_xLabels->setLabelAlignmentFlags(ccGenericGLDisplay::ALIGN_HMIDDLE | ccGenericGLDisplay::ALIGN_VBOTTOM);
m_window->addToOwnDB(m_xLabels,false);
m_yLabels = new ccSymbolCloud(YLABEL_CLOUD_NAME);
m_yLabels->showSymbols(false);
m_yLabels->setSymbolSize(DEFAULT_LABEL_MARGIN);
m_yLabels->setLabelAlignmentFlags(ccGenericGLDisplay::ALIGN_HRIGHT | ccGenericGLDisplay::ALIGN_VMIDDLE);
m_window->addToOwnDB(m_yLabels,false);
}
connect(projectionComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(projectionModeChanged(int)));
connect(angularUnitComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(angularUnitChanged(int)));
connect(xStepDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateGridSteps()));
connect(hStepDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateGridSteps()));
connect(latStepDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateGridSteps()));
connect(xMinDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateGridSteps()));
connect(xMaxDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateGridSteps()));
connect(hMinDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateGridSteps()));
connect(hMaxDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateGridSteps()));
connect(latMinDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateGridSteps()));
connect(latMaxDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateGridSteps()));
connect(axisDimComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateProfileRevolDim(int)));
connect(xOriginDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateProfileOrigin()));
connect(yOriginDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateProfileOrigin()));
connect(zOriginDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateProfileOrigin()));
connect(baseRadiusDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(baseRadiusChanged(double)));
connect(heightUnitLineEdit, SIGNAL(editingFinished()), this, SLOT(updateHeightUnits()));
connect(exportCloudPushButton, SIGNAL(clicked()), this, SLOT(exportMapAsCloud()));
connect(exportMeshPushButton, SIGNAL(clicked()), this, SLOT(exportMapAsMesh()));
connect(exportMatrixPushButton, SIGNAL(clicked()), this, SLOT(exportMapAsGrid()));
connect(exportImagePushButton, SIGNAL(clicked()), this, SLOT(exportMapAsImage()));
connect(loadLabelsPushButton, SIGNAL(clicked()), this, SLOT(loadOverlaySymbols()));
connect(clearLabelsPushButton, SIGNAL(clicked()), this, SLOT(clearOverlaySymbols()));
connect(symbolSizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(overlaySymbolsSizeChanged(int)));
connect(fontSizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(labelFontSizeChanged(int)));
connect(precisionSpinBox, SIGNAL(valueChanged(int)), this, SLOT(labelPrecisionChanged(int)));
connect(colorScaleStepsSpinBox, SIGNAL(valueChanged(int)), this, SLOT(colorRampStepsChanged(int)));
connect(overlayGridGroupBox, SIGNAL(toggled(bool)), this, SLOT(toggleOverlayGrid(bool)));
connect(scaleXStepDoubleSpinBox, SIGNAL(editingFinished()), this, SLOT(updateOverlayGrid()));
connect(scaleHStepDoubleSpinBox, SIGNAL(editingFinished()), this, SLOT(updateOverlayGrid()));
connect(scaleLatStepDoubleSpinBox, SIGNAL(editingFinished()), this, SLOT(updateOverlayGrid()));
connect(xScaleCheckBox, SIGNAL(clicked()), this, SLOT(updateOverlayGrid()));
connect(yScaleCheckBox, SIGNAL(clicked()), this, SLOT(updateOverlayGrid()));
connect(gridColorButton, SIGNAL(clicked()), this, SLOT(changeGridColor()));
connect(symbolColorButton, SIGNAL(clicked()), this, SLOT(changeSymbolColor()));
connect(displayColorScaleCheckBox, SIGNAL(toggled(bool)), this, SLOT(toggleColorScaleDisplay(bool)));
connect(updateVolumesPushButton, SIGNAL(clicked()), this, SLOT(updateVolumes()));
//DXF profiles export button is only visible/connected if DXF support is enabled!
if (DxfProfilesExporter::IsEnabled())
{
connect(exportImageDXFButton, SIGNAL(clicked()), this, SLOT(exportProfilesAsDXF()));
}
else
{
exportImageDXFButton->hide();
}
//button box
{
QPushButton* applyButton = buttonBox->button(QDialogButtonBox::Apply);
QPushButton* closeButton = buttonBox->button(QDialogButtonBox::Close);
connect(applyButton, SIGNAL(clicked()), this, SLOT(update()));
connect(closeButton, SIGNAL(clicked()), this, SLOT(accept()));
}
angularUnitChanged(m_angularUnits); //just to be sure
baseRadiusChanged(0);
overlaySymbolsColorChanged();
overlayGridColorChanged();
labelFontSizeChanged(-1);
projectionModeChanged(-1);
}
DistanceMapGenerationDlg::~DistanceMapGenerationDlg()
{
}
void DistanceMapGenerationDlg::updateMinAndMaxLimits()
{
if (m_cloud && m_profile)
{
DistanceMapGenerationTool::ProfileMetaData profileDesc;
if (DistanceMapGenerationTool::GetPoylineMetaData(m_profile, profileDesc))
{
//compute mean 'radius'
//as well as min and max 'height'
double baseRadius = 0.0;
double minHeight = 0.0;
double maxHeight = 0.0;
for (unsigned i = 0; i<m_profile->size(); ++i)
{
const CCVector3* P = m_profile->getPoint(i);
double radius = P->x;
double height = P->y + profileDesc.heightShift;
baseRadius += radius;
if (i != 0)
{
if (height < minHeight)
minHeight = height;
else if (height > maxHeight)
maxHeight = height;
}
else
{
minHeight = maxHeight = height;
}
}
//set default 'base radius'
if (m_profile->size() != 0)
baseRadius /= m_profile->size();
if (baseRadius == 0.0)
baseRadius = 1.0;
baseRadiusDoubleSpinBox->blockSignals(true);
baseRadiusDoubleSpinBox->setValue(baseRadius);
baseRadiusDoubleSpinBox->blockSignals(false);
//set default min and max height
hMinDoubleSpinBox->blockSignals(true);
hMinDoubleSpinBox->setValue(minHeight);
hMinDoubleSpinBox->blockSignals(false);
hMaxDoubleSpinBox->blockSignals(true);
hMaxDoubleSpinBox->setValue(maxHeight);
hMaxDoubleSpinBox->blockSignals(false);
//do the same for the latitude
//compute transformation from the cloud to the surface (of revolution)
ccGLMatrix cloudToSurfaceOrigin = profileDesc.computeCloudToSurfaceOriginTrans();
double minLat_rad = 0.0, maxLat_rad = 0.0;
if (DistanceMapGenerationTool::ComputeMinAndMaxLatitude_rad(m_cloud,
minLat_rad,
maxLat_rad,
cloudToSurfaceOrigin,
static_cast<unsigned char>(profileDesc.revolDim)))
{
latMinDoubleSpinBox->blockSignals(true);
latMinDoubleSpinBox->setValue(ConvertAngleFromRad(minLat_rad, m_angularUnits));
latMinDoubleSpinBox->blockSignals(false);
latMaxDoubleSpinBox->blockSignals(true);
latMaxDoubleSpinBox->setValue(ConvertAngleFromRad(maxLat_rad, m_angularUnits));
latMaxDoubleSpinBox->blockSignals(false);
}
}
}
}
void DistanceMapGenerationDlg::projectionModeChanged(int)
{
//reset eveything, etc.
ProjectionMode mode = getProjectionMode();
clearView();
//conical mode only
latLabel->setVisible(mode == PROJ_CONICAL);
latMinDoubleSpinBox->setVisible(mode == PROJ_CONICAL);
latMaxDoubleSpinBox->setVisible(mode == PROJ_CONICAL);
latStepDoubleSpinBox->setVisible(mode == PROJ_CONICAL);
latStepLabel->setVisible(mode == PROJ_CONICAL);
scaleLatStepLabel->setVisible(mode == PROJ_CONICAL);
scaleLatStepDoubleSpinBox->setVisible(mode == PROJ_CONICAL);
spanRatioFrame->setVisible(mode == PROJ_CONICAL);
//cylindrical mode only
yLabel->setVisible(mode == PROJ_CYLINDRICAL);
hMinDoubleSpinBox->setVisible(mode == PROJ_CYLINDRICAL);
hMaxDoubleSpinBox->setVisible(mode == PROJ_CYLINDRICAL);
exportImageDXFButton->setVisible(mode == PROJ_CYLINDRICAL);
heightStepLabel->setVisible(mode == PROJ_CYLINDRICAL);
hStepDoubleSpinBox->setVisible(mode == PROJ_CYLINDRICAL);
scaleHeightStepLabel->setVisible(mode == PROJ_CYLINDRICAL);
scaleHStepDoubleSpinBox->setVisible(mode == PROJ_CYLINDRICAL);
heightUnitLineEdit->setVisible(mode == PROJ_CYLINDRICAL);
yScaleCheckBox->setVisible(mode == PROJ_CYLINDRICAL);
baseRadiusChanged(0);
updateGridSteps();
if (m_map)
update();
}
DistanceMapGenerationDlg::ProjectionMode DistanceMapGenerationDlg::getProjectionMode() const
{
switch(projectionComboBox->currentIndex())
{
case 0:
return PROJ_CYLINDRICAL;
case 1:
return PROJ_CONICAL;
default:
assert(false);
}
return PROJ_CYLINDRICAL;
}
DistanceMapGenerationDlg::ANGULAR_UNIT DistanceMapGenerationDlg::getAngularUnit() const
{
switch(m_angularUnits)
{
case 0: //degrees
return ANG_DEG;
case 1: //radians
return ANG_RAD;
case 2: //grades
return ANG_GRAD;
default:
assert(false);
}
return ANG_DEG;
}
QString DistanceMapGenerationDlg::getAngularUnitString() const
{
switch(m_angularUnits)
{
case 0: //degrees
return "deg";
case 1: //radians
return "rad";
case 2: //grades
return "grad";
default:
assert(false);
}
return "none";
}
QString DistanceMapGenerationDlg::getCondensedAngularUnitString() const
{
switch(m_angularUnits)
{
case 0: //degrees
return QChar(0x00B0);
case 1: //radians
return "rd";
case 2: //grades
return "gr";
default:
assert(false);
}
return "none";
}
double DistanceMapGenerationDlg::getSpinboxAngularValue(QDoubleSpinBox* spinBox,
DistanceMapGenerationDlg::ANGULAR_UNIT destUnit) const
{
//no conversion necessary?
if (m_angularUnits == destUnit)
return spinBox->value();
//otherwise we convert to radians first
double angle_rad = ConvertAngleToRad(spinBox->value(), m_angularUnits);
//then to the destination value
return ConvertAngleFromRad(angle_rad, destUnit);
}
void DistanceMapGenerationDlg::updateZoom(ccBBox& box)
{
if (!m_window || !box.isValid())
return;
//equivalent to 'ccGLWindow::updateConstellationCenterAndZoom' but we take aspect ratio into account
//we get the bounding-box diagonal length
PointCoordinateType bbDiag = box.getDiagNorm();
if (bbDiag > ZERO_TOLERANCE)
{
bool sfDisplayed = m_window->getAssociatedScalarField() && m_window->sfShown();
bool yLabelDisplayed = m_yLabels && m_yLabels->isVisible() && m_yLabels->size();
float centerPos = 0.5f;
//we compute the pixel size (in world coordinates)
{
ccViewportParameters params = m_window->getViewportParameters();
params.zoom = 1.0f;
int screenWidth = m_window->width();
int scaleWidth = 0;
int labelsWidth = 0;
if (sfDisplayed)
{
scaleWidth = m_window->getDisplayParameters().colorScaleRampWidth + QFontMetrics(m_window->getTextDisplayFont()).width("123.456789");
}
if (yLabelDisplayed)
{
//find the largest label width
QFont labelFont = m_window->getTextDisplayFont();
labelFont.setPointSize(m_yLabels->getFontSize());
QFontMetrics fm(labelFont);
int maxWidth = 0;
for (unsigned i=0; i<m_yLabels->size(); ++i)
{
QString label = m_yLabels->getLabel(i);
if (!label.isNull())
{
int width = fm.width(label);
maxWidth = std::max(maxWidth,width);
}
}
labelsWidth = maxWidth;
}
//available room for the map
int mapWidth = std::max(1,screenWidth-scaleWidth-labelsWidth);
//we zoom so that the map takes all the room left
float mapPart = static_cast<float>(mapWidth)/static_cast<float>(screenWidth);
params.zoom *= mapPart;
//we must also center the camera on the right position so that the map
//appears inbetween the scale and the color ramp
float mapStart = static_cast<float>(labelsWidth)/static_cast<float>(screenWidth);
centerPos = (0.5f - mapStart) / mapPart;
//update pixel size accordingly
float screenHeight = static_cast<float>(m_window->height()) * params.orthoAspectRatio;
params.pixelSize = static_cast<float>(std::max(box.getDiagVec().x/static_cast<PointCoordinateType>(mapWidth), box.getDiagVec().y/static_cast<PointCoordinateType>(screenHeight)));
m_window->setViewportParameters(params);
}
//we set the pivot point on the box center
CCVector3 P = box.getCenter();
if (centerPos != 0.5f) //if we don't look exactly at the center of the map
P.x = box.minCorner().x * (1.0f - centerPos) + box.maxCorner().x * centerPos;
m_window->setPivotPoint(CCVector3d::fromArray(P.u));
m_window->setCameraPos(CCVector3d::fromArray(P.u));
m_window->invalidateViewport();
m_window->invalidateVisualization();
}
m_window->redraw();
}
void DistanceMapGenerationDlg::clearView()
{
if (!m_window)
return;
//remove existing sf
m_window->setAssociatedScalarField(0);
//remove existing map (or maps?)
ccHObject::Container maps;
m_window->getOwnDB()->filterChildren(maps, false, CC_TYPES::MESH);
for (size_t i = 0; i < maps.size(); ++i)
{
m_window->removeFromOwnDB(maps[i]);
}
//remove any polylines
{
ccHObject::Container polylines;
m_window->getOwnDB()->filterChildren(polylines, false, CC_TYPES::POLY_LINE);
for (size_t i = 0; i < polylines.size(); ++i)
{
m_window->removeFromOwnDB(polylines[i]);
}
}
m_xLabels->setVisible(false);
m_yLabels->setVisible(false);
}
void DistanceMapGenerationDlg::update()
{
if (m_map)
{
if (getProjectionMode() == PROJ_CONICAL)
{
//we must check that the projection parameter have not changed!
//Otherwise the symbols will be misplaced...
double yMin, yMax, yStep;
getGridYValues(yMin, yMax, yStep, ANG_RAD);
if ( !m_map->conical
|| m_map->yMin != yMin
|| m_map->yMax != yMax
|| m_map->conicalSpanRatio != conicSpanRatioDoubleSpinBox->value())
{
clearOverlaySymbols();
}
}
else if (m_map->conical)
{
//we can't keep the symbols when switching the projection mode
clearOverlaySymbols();
}
}
//release memory
m_map.clear();
//clear 3D view
clearView();
//update map
m_map = updateMap();
//and GUI
exportGroupBox->setEnabled(m_map != 0);
//auto update volumes
updateVolumes();
if (m_map && m_window)
{
ccMesh* mapMesh = 0;
ProjectionMode mode = getProjectionMode();
if (mode == PROJ_CYLINDRICAL)
{
//by default we map an image on a plane
double dx = static_cast<double>(m_map->xSteps) * m_map->xStep;
double dy = static_cast<double>(m_map->ySteps) * m_map->yStep;
ccGLMatrix transMat;
transMat.setTranslation(CCVector3( static_cast<PointCoordinateType>(dx / 2 + m_map->xMin),
static_cast<PointCoordinateType>(dy / 2 + m_map->yMin),
0));
ccPlane* mapPlane = new ccPlane( static_cast<PointCoordinateType>(dx),
static_cast<PointCoordinateType>(dy),
&transMat,
"map");
mapMesh = static_cast<ccMesh*>(mapPlane);
}
else //if (mode == PROJ_CONICAL) //conical projection
{
//no choice, we create a mesh
bool ccw = ccwCheckBox->isChecked();
m_map->conicalSpanRatio = conicSpanRatioDoubleSpinBox->value();
mapMesh = DistanceMapGenerationTool::ConvertConicalMapToMesh(m_map, ccw);
}
if (mapMesh)
{
mapMesh->setVisible(true);
mapMesh->showNormals(false);
m_window->addToOwnDB(mapMesh,false);
updateMapTexture();
//add a virtual scalar field for color ramp display
ccScalarField* sf = new ccScalarField();
{
sf->reserve(2);
ScalarType smin = static_cast<ScalarType>(m_map->minVal);
ScalarType smax = static_cast<ScalarType>(m_map->maxVal);
sf->addElement(smin);
sf->addElement(smax);
sf->computeMinAndMax();
}
//selected color scale
ccColorScale::Shared colorScale = ccColorScalesManager::GetDefaultScale();
if (m_colorScaleSelector)
colorScale = m_colorScaleSelector->getSelectedScale();
sf->setColorScale(colorScale);
sf->setColorRampSteps(colorScaleStepsSpinBox->value());
m_window->setAssociatedScalarField(sf);
}
else
{
m_app->dispToConsole(QString("Not enough memory to display the map!"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
}
}
//update sf names, etc.
updateHeightUnits(); //already call 'updateOverlayGrid'!
//force grid update if necessary
//updateOverlayGrid();
//update zoom
ccBBox box = m_window ? m_window->getOwnDB()->getDisplayBB_recursive(false, m_window) : ccBBox();
updateZoom(box);
saveToPersistentSettings();
}
void DistanceMapGenerationDlg::updateHeightUnits()
{
scaleHStepDoubleSpinBox->setSuffix(QString(" ")+heightUnitLineEdit->text());
if (m_window && m_window->getAssociatedScalarField())
{
m_window->getAssociatedScalarField()->setName(qPrintable(QString("Distance (%1)").arg(getHeightUnitString())));
}
updateOverlayGrid();
}
void DistanceMapGenerationDlg::updateMapTexture()
{
if (!m_map || !m_colorScaleSelector || !m_window)
return;
ccHObject::Container texturedEntities;
ProjectionMode mode = getProjectionMode();
if (mode == PROJ_CYLINDRICAL)
{
//cylindrical projection: look for a plane
if (m_window->getOwnDB()->filterChildren(texturedEntities,false,CC_TYPES::PLANE) == 0)
return;
}
else if (mode == PROJ_CONICAL)
{
//conical projection: look for a standard mesh
if (m_window->getOwnDB()->filterChildren(texturedEntities,false,CC_TYPES::MESH) == 0)
return;
}
//spawn "update" dialog
QProgressDialog progressDlg(QString("Updating..."),0,0,0,0,Qt::Popup);
progressDlg.setMinimumDuration(0);
progressDlg.setModal(true);
progressDlg.show();
QApplication::processEvents();
//current color scale
ccColorScale::Shared colorScale = m_colorScaleSelector->getSelectedScale();
if (!colorScale)
{
if (m_app)
m_app->dispToConsole(QString("No color scale chosen!"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
return;
}
//create new texture QImage
QImage mapImage = DistanceMapGenerationTool::ConvertMapToImage(m_map, colorScale, colorScaleStepsSpinBox->value());
if (mapImage.isNull())
{
if (m_app)
m_app->dispToConsole(QString("Failed to create map texture! Not enough memory?"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
return;
}
for (size_t i=0; i<texturedEntities.size(); ++i)
{
//we release the old texture!
texturedEntities[i]->setDisplay(0);
texturedEntities[i]->setDisplay(m_window);
//set new image as texture
if (mode == PROJ_CYLINDRICAL && texturedEntities[i]->isA(CC_TYPES::PLANE))
{
if (!static_cast<ccPlane*>(texturedEntities[i])->setAsTexture(mapImage))
{
if (m_app)
m_app->dispToConsole(QString("Not enough memory to update the map!"),ccMainAppInterface::ERR_CONSOLE_MESSAGE);
return;
}
}
if (mode == PROJ_CONICAL && texturedEntities[i]->isA(CC_TYPES::MESH))
{
ccMesh* mesh = static_cast<ccMesh*>(texturedEntities[i]);
//set material
ccMaterialSet* materialSet = const_cast<ccMaterialSet*>(mesh->getMaterialSet());
assert(materialSet);
//remove old material (if any)
materialSet->clear();
//add new material
{
ccMaterial::Shared material(new ccMaterial("texture"));
material->setTexture(mapImage,QString(),false);
materialSet->addMaterial(material);
}
}
}
m_window->redraw();
}
void DistanceMapGenerationDlg::colorScaleChanged(int)
{
if (!m_window || !m_colorScaleSelector)
return;
ccScalarField* sf = m_window->getAssociatedScalarField();
if (sf)
{
ccColorScale::Shared colorScale = m_colorScaleSelector->getSelectedScale();
unsigned steps = static_cast<unsigned>(colorScaleStepsSpinBox->value());
sf->setColorScale(colorScale);
sf->setColorRampSteps(steps);
m_window->redraw();
}
//same thing with textures
updateMapTexture();
}
void DistanceMapGenerationDlg::spawnColorScaleEditor()
{
if (!m_app || !m_app->getColorScalesManager())
return;
ccColorScale::Shared colorScale = (m_colorScaleSelector ? m_colorScaleSelector->getSelectedScale() : m_app->getColorScalesManager()->getDefaultScale(ccColorScalesManager::BGYR));
ccColorScaleEditorDialog cseDlg(m_app->getColorScalesManager(),m_app,colorScale,m_app->getMainWindow());
if (cseDlg.exec())
{
colorScale = cseDlg.getActiveScale();
if (colorScale)
{
m_colorScaleSelector->init(); //in fact it's a 're-init'
m_colorScaleSelector->setSelectedScale(colorScale->getUuid());
}
//save current scale manager state to persistent settings
m_app->getColorScalesManager()->toPersistentSettings();
}
}
//helper
static void SetSpinBoxValues( QDoubleSpinBox* spinBox,
int decimals,
double minVal,
double maxVal,
double step,
double value)
{
if (spinBox)
{
spinBox->setDecimals(decimals);
spinBox->setRange(minVal,maxVal);
spinBox->setSingleStep(step);
spinBox->setValue(value);
}
}
void DistanceMapGenerationDlg::angularUnitChanged(int index)
{
//backup previous value
double xStep_rad = getSpinboxAngularValue(xStepDoubleSpinBox, ANG_RAD);
double xMin_rad = getSpinboxAngularValue(xMinDoubleSpinBox, ANG_RAD);
double xMax_rad = getSpinboxAngularValue(xMaxDoubleSpinBox, ANG_RAD);
double scaleXStep_rad = getSpinboxAngularValue(scaleXStepDoubleSpinBox, ANG_RAD);
//same for latitude-related spinboxes
double latStep_rad = getSpinboxAngularValue(latStepDoubleSpinBox, ANG_RAD);
double latMin_rad = getSpinboxAngularValue(latMinDoubleSpinBox, ANG_RAD);
double latMax_rad = getSpinboxAngularValue(latMaxDoubleSpinBox, ANG_RAD);
double scaleLatStep_rad = getSpinboxAngularValue(scaleLatStepDoubleSpinBox, ANG_RAD);
switch(index)
{
case 0: //degrees
{
m_angularUnits = ANG_DEG;
SetSpinBoxValues(xStepDoubleSpinBox, 2, 0.01, 360.0, 0.1, xStep_rad * CC_RAD_TO_DEG);
SetSpinBoxValues(scaleXStepDoubleSpinBox, 2, 0.01, 360.0, 5.0, scaleXStep_rad * CC_RAD_TO_DEG);
SetSpinBoxValues(xMinDoubleSpinBox, 2, 0.0, 360.0, 5.0, xMin_rad * CC_RAD_TO_DEG);
SetSpinBoxValues(xMaxDoubleSpinBox, 2, 0.0, 360.0, 5.0, xMax_rad * CC_RAD_TO_DEG);
SetSpinBoxValues(latStepDoubleSpinBox, 2, 0.01, 89.99, 1.0, latStep_rad * CC_RAD_TO_DEG);
SetSpinBoxValues(scaleLatStepDoubleSpinBox, 2, 0.01, 89.99, 1.0, scaleLatStep_rad * CC_RAD_TO_DEG);
SetSpinBoxValues(latMinDoubleSpinBox, 2, -89.99, 89.99, 1.0, latMin_rad * CC_RAD_TO_DEG);
SetSpinBoxValues(latMaxDoubleSpinBox, 2, -89.99, 89.99, 1.0, latMax_rad * CC_RAD_TO_DEG);
xMaxDoubleSpinBox->setMaximum(360.0);
xMaxDoubleSpinBox->setValue(360.0);
break;
}
case 1: //radians
{
m_angularUnits = ANG_RAD;
double PIx2 = 2.0*M_PI;
SetSpinBoxValues(xStepDoubleSpinBox, 4, 0.0001, PIx2, 0.1, xStep_rad);
SetSpinBoxValues(scaleXStepDoubleSpinBox, 4, 0.0001, PIx2, 0.5, scaleXStep_rad);
SetSpinBoxValues(xMinDoubleSpinBox, 4, 0.0, PIx2, 0.5, xMin_rad);
SetSpinBoxValues(xMaxDoubleSpinBox, 4, 0.0, PIx2, 0.5, xMax_rad);
double PIdiv2 = M_PI/2.0-0.0001;
SetSpinBoxValues(scaleLatStepDoubleSpinBox, 4, 0.0001, PIdiv2, 0.3, scaleLatStep_rad);
SetSpinBoxValues(latStepDoubleSpinBox, 4, 0.0001, PIdiv2, 0.3, latStep_rad);
SetSpinBoxValues(latMinDoubleSpinBox, 4, -PIdiv2, PIdiv2, 0.3, latMin_rad);
SetSpinBoxValues(latMaxDoubleSpinBox, 4, -PIdiv2, PIdiv2, 0.3, latMax_rad);
xMaxDoubleSpinBox->setMaximum(PIx2);
xMaxDoubleSpinBox->setValue(PIx2);
break;
}
case 2: //grades
{
m_angularUnits = ANG_GRAD;
SetSpinBoxValues(xStepDoubleSpinBox, 2, 0.01, 400.0, 0.1, xStep_rad * 200.0 / M_PI);
SetSpinBoxValues(scaleXStepDoubleSpinBox, 2, 0.01, 400.0, 5.0, scaleXStep_rad * 200.0 / M_PI);
SetSpinBoxValues(xMinDoubleSpinBox, 2, 0.0, 400.0, 5.0, xMin_rad * 200.0 / M_PI);
SetSpinBoxValues(xMaxDoubleSpinBox, 2, 0.0, 400.0, 5.0, xMax_rad * 200.0 / M_PI);
SetSpinBoxValues(scaleLatStepDoubleSpinBox, 2, 0.01, 99.99, 1.0, scaleLatStep_rad * 200.0 / M_PI);
SetSpinBoxValues(latStepDoubleSpinBox, 2, 0.01, 99.99, 1.0, latStep_rad * 200.0 / M_PI);
SetSpinBoxValues(latMinDoubleSpinBox, 2, -99.99, 99.99, 1.0, latMin_rad * 200.0 / M_PI);
SetSpinBoxValues(latMaxDoubleSpinBox, 2, -99.99, 99.99, 1.0, latMax_rad * 200.0 / M_PI);
xMaxDoubleSpinBox->setMaximum(400.0);
xMaxDoubleSpinBox->setValue(400.0);
break;
}
default: //shouldn't happen!
assert(false);
}
//update spinboxes suffix
{
QString suffix = QString(" ")+getAngularUnitString();
scaleXStepDoubleSpinBox->setSuffix(suffix);
latMinDoubleSpinBox->setSuffix(suffix);
latMaxDoubleSpinBox->setSuffix(suffix);
scaleLatStepDoubleSpinBox->setSuffix(suffix);
}
updateOverlayGrid();
}
void DistanceMapGenerationDlg::getGridXValues(double& minX, double& maxX, double& step, ANGULAR_UNIT unit/*=ANG_RAD*/) const
{
minX = getSpinboxAngularValue(xMinDoubleSpinBox, unit);
maxX = getSpinboxAngularValue(xMaxDoubleSpinBox, unit);
step = getSpinboxAngularValue(xStepDoubleSpinBox, unit);
}
void DistanceMapGenerationDlg::getGridYValues(double& minY, double& maxY, double& step, ANGULAR_UNIT unit/*=ANG_RAD*/) const
{
switch (getProjectionMode())
{
case PROJ_CYLINDRICAL:
minY = hMinDoubleSpinBox->value();
maxY = hMaxDoubleSpinBox->value();
step = hStepDoubleSpinBox->value();
break;
case PROJ_CONICAL:
minY = getSpinboxAngularValue(latMinDoubleSpinBox, unit);
maxY = getSpinboxAngularValue(latMaxDoubleSpinBox, unit);
step = getSpinboxAngularValue(latStepDoubleSpinBox, unit);
break;
default:
assert(false);
break;
}
}
double DistanceMapGenerationDlg::getScaleYStep(ANGULAR_UNIT unit/*=ANG_RAD*/) const
{
if (getProjectionMode() == PROJ_CYLINDRICAL)
return scaleHStepDoubleSpinBox->value();
else
return getSpinboxAngularValue(scaleLatStepDoubleSpinBox, unit);
}
void DistanceMapGenerationDlg::updateProfileRevolDim(int dim)
{
if (!m_profile)
{
assert(false);
return;