File: ccSectionExtractionTool.cpp

package info (click to toggle)
cloudcompare 2.11.3-7.1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 58,224 kB
  • sloc: cpp: 229,982; ansic: 30,723; makefile: 84; sh: 20
file content (2103 lines) | stat: -rw-r--r-- 55,653 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
//##########################################################################
//#                                                                        #
//#                              CLOUDCOMPARE                              #
//#                                                                        #
//#  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 R&D / TELECOM ParisTech (ENST-TSI)             #
//#                                                                        #
//##########################################################################

#include "ccSectionExtractionTool.h"

//Local
#include "ccContourExtractor.h"
#include "ccItemSelectionDlg.h"
#include "ccOrthoSectionGenerationDlg.h"
#include "ccSectionExtractionSubDlg.h"
#include "mainwindow.h"

//qCC_db
#include <ccGenericPointCloud.h>
#include <ccHObjectCaster.h>
#include <ccLog.h>
#include <ccPointCloud.h>
#include <ccPolyline.h>
#include <ccProgressDialog.h>

//qCC_gl
#include <ccGLWindow.h>

//CCLib
#include <ReferenceCloud.h>

//Qt
#include <QCoreApplication>
#include <QInputDialog>
#include <QMdiSubWindow>
#include <QMessageBox>

//GUI
#include <ui_sectionExtractionDlg.h>

//System
#include <cassert>
#include <cmath>

//default parameters
static const ccColor::Rgb& s_defaultPolylineColor = ccColor::magenta;
static const ccColor::Rgb& s_defaultContourColor = ccColor::green;
static const ccColor::Rgb& s_defaultEditedPolylineColor = ccColor::green;
static const ccColor::Rgb& s_defaultSelectedPolylineColor = ccColor::red;

constexpr int	s_defaultPolylineWidth = 1;
constexpr int	s_defaultSelectedPolylineWidth = 3;

//default export groups
static unsigned s_polyExportGroupID = 0;
static unsigned s_profileExportGroupID = 0;
static unsigned s_cloudExportGroupID = 0;

//default arrow size
static const PointCoordinateType s_defaultArrowSize = 20;

ccSectionExtractionTool::ccSectionExtractionTool(QWidget* parent)
	: ccOverlayDialog(parent)
	, m_UI( new Ui::SectionExtractionDlg )
	, m_selectedPoly(nullptr)
	, m_state(0)
	, m_editedPoly(nullptr)
	, m_editedPolyVertices(nullptr)
{
	m_UI->setupUi(this);

	connect(m_UI->undoToolButton, &QAbstractButton::clicked, this, &ccSectionExtractionTool::undo);
	connect(m_UI->validToolButton, &QAbstractButton::clicked, this, &ccSectionExtractionTool::apply);
	connect(m_UI->cancelToolButton, &QAbstractButton::clicked, this, &ccSectionExtractionTool::cancel);
	connect(m_UI->polylineToolButton, &QAbstractButton::toggled, this, &ccSectionExtractionTool::enableSectionEditingMode);
	connect(m_UI->importFromDBToolButton, &QAbstractButton::clicked, this, &ccSectionExtractionTool::doImportPolylinesFromDB);
	connect(m_UI->vertAxisComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &ccSectionExtractionTool::setVertDimension);

	connect(m_UI->generateOrthoSectionsToolButton, &QAbstractButton::clicked, this, &ccSectionExtractionTool::generateOrthoSections);
	connect(m_UI->extractPointsToolButton, &QAbstractButton::clicked, this, &ccSectionExtractionTool::extractPoints);
	connect(m_UI->unfoldToolButton, &QAbstractButton::clicked, this, &ccSectionExtractionTool::unfoldPoints);
	connect(m_UI->exportSectionsToolButton, &QAbstractButton::clicked, this, &ccSectionExtractionTool::exportSections);

	//add shortcuts
	addOverridenShortcut(Qt::Key_Space);  //space bar for the "pause" button
	addOverridenShortcut(Qt::Key_Escape); //cancel current polyline edition
	addOverridenShortcut(Qt::Key_Delete); //delete key to delete the selected polyline

	connect(this, &ccOverlayDialog::shortcutTriggered, this, &ccSectionExtractionTool::onShortcutTriggered);
}

ccSectionExtractionTool::~ccSectionExtractionTool()
{
	if (m_editedPoly)
	{
		if (m_associatedWin)
			m_associatedWin->removeFromOwnDB(m_editedPoly);
		delete m_editedPoly;
		m_editedPoly = nullptr;
	}
	
	delete m_UI;
}

void ccSectionExtractionTool::setVertDimension(int dim)
{
	assert(dim >= 0 && dim < 3);
	if (!m_associatedWin)
		return;

	switch (dim)
	{
	case 0:
		m_associatedWin->setView(CC_RIGHT_VIEW);
		break;
	case 1:
		m_associatedWin->setView(CC_FRONT_VIEW);
		break;
	case 2:
	default:
		m_associatedWin->setView(CC_TOP_VIEW);
		break;
	}
	m_associatedWin->updateConstellationCenterAndZoom();
}

void ccSectionExtractionTool::onShortcutTriggered(int key)
{
	switch (key)
	{
	case Qt::Key_Space:
		m_UI->polylineToolButton->toggle();
		return;

	case Qt::Key_Escape:
		cancelCurrentPolyline();
		return;

	case Qt::Key_Delete:
		deleteSelectedPolyline();
		return;

	default:
		//nothing to do
		break;
	}
}

bool ccSectionExtractionTool::linkWith(ccGLWindow* win)
{
	ccGLWindow* oldWin = m_associatedWin;

	if (!ccOverlayDialog::linkWith(win))
	{
		return false;
	}

	selectPolyline(nullptr);

	if (oldWin)
	{
		//restore sections original display
		for (auto & section : m_sections)
		{
			if (section.entity)
			{
				if (!section.isInDB)
					oldWin->removeFromOwnDB(section.entity);
				section.entity->setDisplay_recursive(section.originalDisplay);
			}
		}

		//Restore clouds original display
		for (auto & cloud : m_clouds)
		{
			if (cloud.entity)
			{
				if (!cloud.isInDB)
					oldWin->removeFromOwnDB(cloud.entity);
				cloud.entity->setDisplay(cloud.originalDisplay);
			}
		}

		if (m_editedPoly)
		{
			m_editedPoly->setDisplay_recursive(nullptr);
		}

		//auto-close formerly associated window
		if (MainWindow::TheInstance())
		{
			QMdiSubWindow* subWindow = MainWindow::TheInstance()->getMDISubWindow(oldWin);
			if (subWindow)
			{
				subWindow->close();
			}
		}
	}

	if (m_associatedWin)
	{
		connect(m_associatedWin, &ccGLWindow::leftButtonClicked, this, &ccSectionExtractionTool::addPointToPolyline);
		connect(m_associatedWin, &ccGLWindow::rightButtonClicked, this, &ccSectionExtractionTool::closePolyLine);
		connect(m_associatedWin, &ccGLWindow::mouseMoved, this, &ccSectionExtractionTool::updatePolyLine);
		connect(m_associatedWin, &ccGLWindow::entitySelectionChanged, this, &ccSectionExtractionTool::entitySelected);

		//import sections in current display
		for (auto & section : m_sections)
		{
			if (section.entity)
			{
				section.originalDisplay = section.entity->getDisplay();
				section.entity->setDisplay_recursive(m_associatedWin);
				if (!section.isInDB)
					m_associatedWin->addToOwnDB(section.entity);
			}
		}

		//import clouds in current display
		for (auto & cloud : m_clouds)
		{
			if (cloud.entity)
			{
				cloud.originalDisplay = cloud.entity->getDisplay();
				cloud.entity->setDisplay(m_associatedWin);
				if (!cloud.isInDB)
				{
					m_associatedWin->addToOwnDB(cloud.entity);
				}
			}
		}

		if (m_editedPoly)
		{
			m_editedPoly->setDisplay_recursive(m_associatedWin);
		}

		//update view direction
		setVertDimension(m_UI->vertAxisComboBox->currentIndex());

		//section extraction only works in orthoraphic mode!
		m_associatedWin->setPerspectiveState(false, true);
	}

	return true;
}

void ccSectionExtractionTool::selectPolyline(Section* poly, bool autoRefreshDisplay/*=true*/)
{
	bool redraw = false;

	//deselect previously selected polyline
	if (m_selectedPoly && m_selectedPoly->entity)
	{
		m_selectedPoly->entity->showColors(true);
		m_selectedPoly->entity->setColor(s_defaultPolylineColor);
		m_selectedPoly->entity->setWidth(s_defaultPolylineWidth);
		redraw = true;
	}

	m_selectedPoly = poly;

	//select new polyline (if any)
	if (m_selectedPoly)
	{
		m_selectedPoly->entity->showColors(true);
		m_selectedPoly->entity->setColor(s_defaultSelectedPolylineColor);
		m_selectedPoly->entity->setWidth(s_defaultSelectedPolylineWidth);
		m_selectedPoly->entity->setSelected(false); //as the window selects it by default (with bounding-box, etc.) and we don't want that
		redraw = true;
	}

	if (redraw && autoRefreshDisplay && m_associatedWin)
	{
		m_associatedWin->redraw();
	}

	m_UI->generateOrthoSectionsToolButton->setEnabled(m_selectedPoly != nullptr);
	m_UI->unfoldToolButton->setEnabled(m_selectedPoly != nullptr);
}

void ccSectionExtractionTool::releasePolyline(Section* section)
{
	if (section && section->entity)
	{
		if (!section->isInDB)
		{
			//remove from display
			if (m_associatedWin)
				m_associatedWin->removeFromOwnDB(section->entity);
			//delete entity
			delete section->entity;
			section->entity = nullptr;
		}
		else
		{
			//restore original display and style
			section->entity->showColors(section->backupColorShown);
			section->entity->setColor(section->backupColor);
			section->entity->setWidth(section->backupWidth);
			section->entity->setDisplay_recursive(section->originalDisplay);
		}
	}
}

void ccSectionExtractionTool::deleteSelectedPolyline()
{
	if (!m_selectedPoly)
		return;

	Section* selectedPoly = m_selectedPoly;

	//deslect polyline before anything
	selectPolyline(nullptr, false);

	releasePolyline(selectedPoly);

	//remove the section from the list
	m_sections.removeOne(*selectedPoly);
	m_undoCount.resize(0);
	m_UI->undoToolButton->setEnabled(false);

	if (m_associatedWin)
	{
		m_associatedWin->redraw();
	}
}

void ccSectionExtractionTool::entitySelected(ccHObject* entity)
{
	if (!entity)
	{
		return;
	}

	//look if this selected entity corresponds to an active polyline
	for (auto & section : m_sections)
	{
		if (section.entity == entity)
		{
			selectPolyline(&section);
			break;
		}
	}
}

bool ccSectionExtractionTool::start()
{
	assert(!m_editedPolyVertices && !m_editedPoly);

	if (!m_associatedWin)
	{
		ccLog::Warning("[Graphical Segmentation Tool] No associated window!");
		return false;
	}

	//the user must not close this window!
	m_associatedWin->setUnclosable(true);
	m_associatedWin->updateConstellationCenterAndZoom();
	updateCloudsBox();

	enableSectionEditingMode(true);

	return ccOverlayDialog::start();
}

void ccSectionExtractionTool::removeAllEntities()
{
	reset(false);

	//and we remove the remaining clouds (if any)
	for (auto & cloud : m_clouds)
	{
		if (cloud.entity)
		{
			assert(cloud.isInDB);
			//restore original display
			cloud.entity->setDisplay(cloud.originalDisplay);
		}
	}
	
	m_clouds.clear();
	m_cloudsBox.clear();
}

void ccSectionExtractionTool::undo()
{
	if (m_undoCount.empty())
		return;

	size_t count = 0;
	do
	{
		count = m_undoCount.back();
		m_undoCount.pop_back();
	} while (static_cast<int>(count) >= m_sections.size() && !m_undoCount.empty());

	//ask for a confirmation
	if (QMessageBox::question(MainWindow::TheInstance(), "Undo", QString("Remove %1 polylines?").arg(m_sections.size() - count), QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
	{
		//restore undo stack!
		m_undoCount.push_back(count);
		return;
	}

	selectPolyline(nullptr);

	//we remove all polylines after a given point
	{
		while (m_sections.size() > static_cast<int>(count))
		{
			Section& section = m_sections.back();
			releasePolyline(&section);
			m_sections.pop_back();
		}
	}

	//update GUI
	m_UI->exportSectionsToolButton->setEnabled(count != 0);
	m_UI->extractPointsToolButton->setEnabled(count != 0);
	m_UI->undoToolButton->setEnabled(!m_undoCount.empty());

	if (m_associatedWin)
		m_associatedWin->redraw();
}

bool ccSectionExtractionTool::reset(bool askForConfirmation/*=true*/)
{
	if (m_sections.empty() && m_clouds.empty())
	{
		//nothing to do
		return true;
	}

	if (askForConfirmation)
	{
		//if we found at least one temporary polyline, we display a confirmation message
		for (auto & section : m_sections)
		{
			if (section.entity && !section.isInDB)
			{
				if (QMessageBox::question(MainWindow::TheInstance(), "Reset", "You'll lose all manually defined polylines: are you sure?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
					return false;
				else
					break;
			}
		}
	}

	selectPolyline(nullptr);

	//we remove all polylines
	for (auto & section : m_sections)
	{
		releasePolyline(&section);
	}
	
	m_sections.clear();
	m_undoCount.resize(0);
	m_UI->undoToolButton->setEnabled(false);
	m_UI->exportSectionsToolButton->setEnabled(false);
	m_UI->extractPointsToolButton->setEnabled(false);

	//and we remove only temporary clouds
	for (int i = 0; i < m_clouds.size();)
	{
		Cloud& cloud = m_clouds[i];
		if (cloud.entity && !cloud.isInDB)
		{
			if (m_associatedWin)
				m_associatedWin->removeFromOwnDB(cloud.entity);
			delete cloud.entity;
			cloud.entity = nullptr;

			m_clouds.removeAt(i);
		}
		else
		{
			++i;
		}
	}
	
	updateCloudsBox();

	if (m_associatedWin)
		m_associatedWin->redraw();

	return true;
}

void ccSectionExtractionTool::stop(bool accepted)
{
	if (m_editedPoly)
	{
		if (m_associatedWin)
			m_associatedWin->removeFromOwnDB(m_editedPoly);
		delete m_editedPoly;
		m_editedPoly = nullptr;
	}
	m_editedPolyVertices = nullptr;

	enableSectionEditingMode(false);
	reset(true);

	if (m_associatedWin)
	{
		m_associatedWin->setInteractionMode(ccGLWindow::TRANSFORM_CAMERA());
		m_associatedWin->setPickingMode(ccGLWindow::DEFAULT_PICKING);
		m_associatedWin->setUnclosable(false);
	}

	ccOverlayDialog::stop(accepted);
}

void ccSectionExtractionTool::updateCloudsBox()
{
	m_cloudsBox.clear();

	for (auto & cloud : m_clouds)
	{
		if (cloud.entity)
			m_cloudsBox += cloud.entity->getOwnBB();
	}
}

bool ccSectionExtractionTool::addPolyline(ccPolyline* inputPoly, bool alreadyInDB/*=true*/)
{
	if (!inputPoly)
	{
		assert(false);
		return false;
	}

	for (auto & section : m_sections)
	{
		if (section.entity == inputPoly)
		{
			//cloud already in DB
			return false;
		}
	}

	//convert the polyline to 3D mode if necessary
	if (inputPoly->is2DMode())
	{
		//viewing parameters (for conversion from 2D to 3D)
		ccGLCameraParameters camera;
		m_associatedWin->getGLCameraParameters(camera);
		const double half_w = camera.viewport[2] / 2.0;
		const double half_h = camera.viewport[3] / 2.0;

		//working dimension
		int vertDim = m_UI->vertAxisComboBox->currentIndex();
		assert(vertDim >= 0 && vertDim < 3);

		//get default altitude from the cloud(s) bouding-box
		PointCoordinateType defaultZ = 0;
		if (m_cloudsBox.isValid())
		{
			defaultZ = m_cloudsBox.maxCorner()[vertDim];
		}

		//duplicate polyline
		ccPolyline* duplicatePoly = new ccPolyline(nullptr);
		ccPointCloud* duplicateVertices = nullptr;
		if (duplicatePoly->initWith(duplicateVertices, *inputPoly))
		{
			assert(duplicateVertices);
			for (unsigned i = 0; i < duplicateVertices->size(); ++i)
			{
				CCVector3& P = const_cast<CCVector3&>(*duplicateVertices->getPoint(i));
				CCVector3d Pd(half_w + P.x, half_h + P.y, 0/*P.z*/);
				CCVector3d Q3D;
				camera.unproject(Pd, Q3D);
				P = CCVector3::fromArray(Q3D.u);
				P.u[vertDim] = defaultZ;
			}

			duplicateVertices->invalidateBoundingBox();
			duplicateVertices->setEnabled(false);
			duplicatePoly->set2DMode(false);
			duplicatePoly->setDisplay_recursive(inputPoly->getDisplay());
			duplicatePoly->setName(inputPoly->getName());
			duplicatePoly->setGlobalScale(inputPoly->getGlobalScale());
			duplicatePoly->setGlobalShift(inputPoly->getGlobalShift());

			if (!alreadyInDB)
				delete inputPoly;
			else
				alreadyInDB = false;
			inputPoly = duplicatePoly;
		}
		else
		{
			delete duplicatePoly;
			duplicatePoly = nullptr;

			ccLog::Error("Not enough memory to import polyline!");
			return false;
		}
	}

	//add polyline to the 'sections' set
	//(all its parameters will be backuped!)
	m_sections.push_back(Section(inputPoly, alreadyInDB));
	m_UI->exportSectionsToolButton->setEnabled(true);
	m_UI->extractPointsToolButton->setEnabled(true);

	//apply default look
	inputPoly->setEnabled(true);
	inputPoly->setVisible(true);
	inputPoly->showColors(true);
	inputPoly->setColor(s_defaultPolylineColor);
	inputPoly->setWidth(s_defaultPolylineWidth);

	//add to display
	if (m_associatedWin)
	{
		inputPoly->setDisplay_recursive(m_associatedWin);
		if (!alreadyInDB)
			m_associatedWin->addToOwnDB(inputPoly);
	}

	return true;
}

static bool s_mixedShiftAndScaleInfo = false;

bool ccSectionExtractionTool::addCloud(ccGenericPointCloud* inputCloud, bool alreadyInDB/*=true*/)
{
	assert(inputCloud);

	if (m_clouds.empty())
		s_mixedShiftAndScaleInfo = false;

	for (CloudPool::iterator it = m_clouds.begin(); it != m_clouds.end(); ++it)
	{
		Cloud& cloud = *it;
		if (cloud.entity == inputCloud)
		{
			//cloud already in DB
			return false;
		}

		//test (on the first cloud) that the global shift & scale info is the same
		if (!s_mixedShiftAndScaleInfo && it == m_clouds.begin())
		{
			if (cloud.entity->getGlobalScale() != inputCloud->getGlobalScale()
				|| (cloud.entity->getGlobalShift() - inputCloud->getGlobalShift()).norm() < ZERO_TOLERANCE)
			{
				ccLog::Warning("[ccSectionExtractionTool] Clouds have different shift & scale information! Only the first one will be used");
				s_mixedShiftAndScaleInfo = true;
			}
		}
	}

	m_clouds.push_back(Cloud(inputCloud, alreadyInDB));
	if (m_associatedWin)
	{
		inputCloud->setDisplay(m_associatedWin);
		if (!alreadyInDB)
			m_associatedWin->addToOwnDB(inputCloud);
	}

	return true;
}

void ccSectionExtractionTool::updatePolyLine(int x, int y, Qt::MouseButtons buttons)
{
	Q_UNUSED( buttons );
	
	if (!m_associatedWin)
	{
		assert(false);
		return;
	}

	//process not started yet?
	if ((m_state & RUNNING) == 0)
		return;

	if (!m_editedPoly)
		return;

	unsigned vertCount = m_editedPolyVertices->size();
	if (vertCount < 2)
		return;

	QPointF pos2D = m_associatedWin->toCenteredGLCoordinates(x, y);
	CCVector3 P(static_cast<PointCoordinateType>(pos2D.x()),
		static_cast<PointCoordinateType>(pos2D.y()),
		0);

	//we replace last point by the current one
	CCVector3* lastP = const_cast<CCVector3*>(m_editedPolyVertices->getPointPersistentPtr(vertCount - 1));
	*lastP = P;

	m_associatedWin->redraw(true, false);
}

void ccSectionExtractionTool::addPointToPolyline(int x, int y)
{
	if ((m_state & STARTED) == 0)
	{
		return;
	}
	if (!m_associatedWin)
	{
		assert(false);
		return;
	}

	if (!m_editedPoly)
	{
		assert(!m_editedPolyVertices);
		m_editedPolyVertices = new ccPointCloud("vertices");
		m_editedPoly = new ccPolyline(m_editedPolyVertices);
		m_editedPoly->setForeground(true);
		m_editedPoly->setColor(s_defaultEditedPolylineColor);
		m_editedPoly->showColors(true);
		m_editedPoly->set2DMode(true);
		//copy (first) cloud shift & scale info!
		if (!m_clouds.empty() && m_clouds.front().entity)
		{
			ccGenericPointCloud* cloud = m_clouds.front().entity;
			m_editedPoly->setGlobalScale(cloud->getGlobalScale());
			m_editedPoly->setGlobalShift(cloud->getGlobalShift());
		}
		m_editedPoly->addChild(m_editedPolyVertices);
		m_associatedWin->addToOwnDB(m_editedPoly);
	}

	unsigned vertCount = m_editedPolyVertices->size();

	//clicked point (2D)
	QPointF pos2D = m_associatedWin->toCenteredGLCoordinates(x, y);
	CCVector3 P(static_cast<PointCoordinateType>(pos2D.x()),
		static_cast<PointCoordinateType>(pos2D.y()),
		0);

	//start new polyline?
	if (((m_state & RUNNING) == 0) || vertCount == 0)
	{
		//reset state
		m_state = (STARTED | RUNNING);
		//reset polyline
		m_editedPolyVertices->clear();
		if (!m_editedPolyVertices->reserve(2))
		{
			ccLog::Error("Out of memory!");
			return;
		}
		//we add the same point twice (the last point will be used for display only)
		m_editedPolyVertices->addPoint(P);
		m_editedPolyVertices->addPoint(P);
		m_editedPoly->clear();
		if (!m_editedPoly->addPointIndex(0, 2))
		{
			ccLog::Error("Out of memory!");
			return;
		}
	}
	else //next points
	{
		if (!m_editedPolyVertices->reserve(vertCount + 1))
		{
			ccLog::Error("Out of memory!");
			return;
		}

		//we replace last point by the current one
		assert(vertCount >= 2);
		CCVector3* lastP = const_cast<CCVector3*>(m_editedPolyVertices->getPointPersistentPtr(vertCount - 1));
		CCVector3* lastQ = const_cast<CCVector3*>(m_editedPolyVertices->getPointPersistentPtr(vertCount - 2));
		PointCoordinateType tipLength = (*lastQ - *lastP).norm();
		*lastP = P;
		//and add a new (equivalent) one
		m_editedPolyVertices->addPoint(P);
		if (!m_editedPoly->addPointIndex(vertCount))
		{
			ccLog::Error("Out of memory!");
			return;
		}
		PointCoordinateType defaultArrowSize = std::min(s_defaultArrowSize, tipLength / 2);
		m_editedPoly->showArrow(true, vertCount - 1, defaultArrowSize);
	}

	m_associatedWin->redraw(true, false);
}

void ccSectionExtractionTool::closePolyLine(int, int)
{
	//only in RUNNING mode
	if ((m_state & RUNNING) == 0 || !m_editedPoly)
		return;

	assert(m_editedPoly);
	unsigned vertCount = m_editedPoly->size();
	if (vertCount < 3)
	{
		m_editedPoly->clear();
		m_editedPolyVertices->clear();
	}
	else
	{
		//remove last point!
		m_editedPoly->resize(vertCount - 1); //can't fail --> smaller

		//remove polyline from the 'temporary' world
		if (m_associatedWin)
			m_associatedWin->removeFromOwnDB(m_editedPoly);
		//set default display style
		m_editedPoly->showColors(true);
		m_editedPoly->setColor(s_defaultPolylineColor);
		m_editedPoly->setWidth(s_defaultPolylineWidth);
		if (!m_clouds.isEmpty())
			m_editedPoly->setDisplay_recursive(m_clouds.front().originalDisplay); //set the same 'default' display as the cloud
		m_editedPoly->setName(QString("Polyline #%1").arg(m_sections.size() + 1));
		//save polyline
		if (!addPolyline(m_editedPoly, false))
		{
			//if something went wrong, we have to remove the polyline manually
			delete m_editedPoly;
		}
		m_editedPoly = nullptr;
		m_editedPolyVertices = nullptr;
	}

	//stop
	m_state &= (~RUNNING);

	if (m_associatedWin)
	{
		m_associatedWin->redraw(true, false);
	}
}

void ccSectionExtractionTool::cancelCurrentPolyline()
{
	if ((m_state & STARTED) == 0
		|| !m_editedPoly)
	{
		return;
	}

	assert(m_editedPolyVertices);
	m_editedPoly->clear();
	m_editedPolyVertices->clear();

	//stop
	m_state &= (~RUNNING);

	if (m_associatedWin)
		m_associatedWin->redraw();
}

void ccSectionExtractionTool::enableSectionEditingMode(bool state)
{
	if (!m_associatedWin)
		return;

	if (!state/*=activate pause mode*/)
	{
		//select the last polyline by default (if any)
		if (!m_sections.empty() && !m_sections.back().isInDB)
			selectPolyline(&m_sections.back());

		m_state = PAUSED;

		if (m_editedPoly && m_editedPolyVertices)
		{
			m_editedPoly->clear();
			m_editedPolyVertices->clear();
		}
		m_associatedWin->setInteractionMode(ccGLWindow::PAN_ONLY());
		m_associatedWin->displayNewMessage(QString(), ccGLWindow::UPPER_CENTER_MESSAGE, false, 0, ccGLWindow::MANUAL_SEGMENTATION_MESSAGE);
		m_associatedWin->setPickingMode(ccGLWindow::ENTITY_PICKING); //to be able to select polylines!
	}
	else
	{
		//deselect all currently selected polylines
		selectPolyline(nullptr);

		//set new 'undo' step
		addUndoStep();

		m_state = STARTED;

		m_associatedWin->setPickingMode(ccGLWindow::NO_PICKING);
		m_associatedWin->setInteractionMode(ccGLWindow::INTERACT_SEND_ALL_SIGNALS);
		m_associatedWin->displayNewMessage("Section edition mode", ccGLWindow::UPPER_CENTER_MESSAGE, false, 3600, ccGLWindow::MANUAL_SEGMENTATION_MESSAGE);
		m_associatedWin->displayNewMessage("Left click: add section points / Right click: stop", ccGLWindow::UPPER_CENTER_MESSAGE, true, 3600, ccGLWindow::MANUAL_SEGMENTATION_MESSAGE);
	}

	//update mini-GUI
	m_UI->polylineToolButton->blockSignals(true);
	m_UI->polylineToolButton->setChecked(state);
	m_UI->frame->setEnabled(!state);
	m_UI->polylineToolButton->blockSignals(false);

	m_associatedWin->redraw();
}

void ccSectionExtractionTool::addUndoStep()
{
	if (m_undoCount.empty() || (static_cast<int>(m_undoCount.back()) < m_sections.size()))
	{
		m_undoCount.push_back(m_sections.size());
		m_UI->undoToolButton->setEnabled(true);
	}
}

void ccSectionExtractionTool::doImportPolylinesFromDB()
{
	MainWindow* mainWindow = MainWindow::TheInstance();
	if (!mainWindow)
		return;

	ccHObject* root = mainWindow->dbRootObject();
	ccHObject::Container polylines;
	if (root)
	{
		root->filterChildren(polylines, true, CC_TYPES::POLY_LINE);
	}

	if (!polylines.empty())
	{
		std::vector<int> indexes;
		if (!ccItemSelectionDlg::SelectEntities(polylines, indexes, this))
		{
			return;
		}

		//set new 'undo' step
		addUndoStep();

		enableSectionEditingMode(false);
		
		for (int index : indexes)
		{
			assert(index >= 0 && index < static_cast<int>(polylines.size()));
			assert(polylines[index]->isA(CC_TYPES::POLY_LINE));
			
			ccPolyline* poly = static_cast<ccPolyline*>(polylines[index]);
			addPolyline(poly, true);
		}
		
		//auto-select the last one
		if (!m_sections.empty())
			selectPolyline(&(m_sections.back()));
		if (m_associatedWin)
			m_associatedWin->redraw();
	}
	else
	{
		ccLog::Error("No polyline in DB!");
	}
}

void ccSectionExtractionTool::cancel()
{
	reset(false);

	stop(false);
}

void ccSectionExtractionTool::apply()
{
	if (!reset(true))
		return;

	stop(true);
}

static double s_orthoSectionWidth = -1.0;
static double s_orthoSectionStep = -1.0;
static bool s_autoSaveAndRemoveGeneratrix = true;
void ccSectionExtractionTool::generateOrthoSections()
{
	if (!m_selectedPoly)
	{
		ccLog::Warning("[ccSectionExtractionTool] No polyline selected");
		return;
	}

	//compute poyline length
	ccPolyline* poly = m_selectedPoly->entity;
	unsigned vertCount = (poly ? poly->size() : 0);
	if (vertCount < 2)
	{
		ccLog::Warning("[ccSectionExtractionTool] Invalid polyline");
		return;
	}

	PointCoordinateType length = poly->computeLength();

	//show arrow
	{
		assert(vertCount >= 2);
		const CCVector3* lastQ = poly->getPoint(vertCount - 2);
		const CCVector3* lastP = poly->getPoint(vertCount - 1);
		PointCoordinateType tipLength = (*lastQ - *lastP).norm();
		PointCoordinateType defaultArrowSize = m_associatedWin->computeActualPixelSize() * s_defaultArrowSize;
		defaultArrowSize = std::min(defaultArrowSize, tipLength / 2);
		poly->showArrow(true, poly->size() - 1, defaultArrowSize);
		m_associatedWin->redraw();
	}

	//display dialog
	ccOrthoSectionGenerationDlg osgDlg(MainWindow::TheInstance());
	osgDlg.setPathLength(length);
	if (s_orthoSectionWidth > 0.0)
		osgDlg.setSectionsWidth(s_orthoSectionWidth);
	if (s_orthoSectionStep > 0.0)
		osgDlg.setGenerationStep(s_orthoSectionStep);
	osgDlg.setAutoSaveAndRemove(s_autoSaveAndRemoveGeneratrix);

	if (osgDlg.exec())
	{
		//now generate the orthogonal sections
		s_orthoSectionStep = osgDlg.getGenerationStep();
		s_orthoSectionWidth = osgDlg.getSectionsWidth();
		s_autoSaveAndRemoveGeneratrix = osgDlg.autoSaveAndRemove();

		if (s_autoSaveAndRemoveGeneratrix)
		{
			//save
			if (!m_selectedPoly->isInDB)
			{
				ccHObject* destEntity = getExportGroup(s_polyExportGroupID, "Exported sections");
				assert(destEntity);
				destEntity->addChild(m_selectedPoly->entity);
				m_selectedPoly->isInDB = true;
				m_selectedPoly->entity->setDisplay_recursive(destEntity->getDisplay());
				MainWindow::TheInstance()->addToDB(m_selectedPoly->entity, false, false);
			}
			//and remove
			deleteSelectedPolyline();
		}

		//set new 'undo' step
		addUndoStep();

		//normal to the plane
		CCVector3 N(0, 0, 0);
		int vertDim = m_UI->vertAxisComboBox->currentIndex();
		assert(vertDim >= 0 && vertDim < 3);
		{
			N.u[vertDim] = 1.0;
		}

		//curvilinear position
		double s = 0;
		//current length
		double l = 0;
		unsigned maxCount = vertCount;
		if (!poly->isClosed())
			maxCount--;
		unsigned polyIndex = 0;
		for (unsigned i = 0; i < maxCount; ++i)
		{
			const CCVector3* A = poly->getPoint(i);
			const CCVector3* B = poly->getPoint((i + 1) % vertCount);
			CCVector3 AB = (*B - *A);
			AB.u[vertDim] = 0;
			CCVector3 nAB = AB.cross(N);
			nAB.normalize();

			double lAB = (*B - *A).norm();
			while (s < l + lAB)
			{
				double s_local = s - l;
				assert(s_local < lAB);

				//create orhogonal polyline
				ccPointCloud* vertices = new ccPointCloud("vertices");
				ccPolyline* orthoPoly = new ccPolyline(vertices);
				orthoPoly->addChild(vertices);
				if (vertices->reserve(2) && orthoPoly->reserve(2))
				{
					//intersection point
					CCVector3 I = *A + AB * (s_local / lAB);
					CCVector3 I1 = I + nAB * static_cast<PointCoordinateType>(s_orthoSectionWidth / 2);
					CCVector3 I2 = I - nAB * static_cast<PointCoordinateType>(s_orthoSectionWidth / 2);

					vertices->addPoint(I1);
					orthoPoly->addPointIndex(0);
					vertices->addPoint(I2);
					orthoPoly->addPointIndex(1);

					orthoPoly->setClosed(false);
					orthoPoly->set2DMode(false);
					orthoPoly->setGlobalScale(poly->getGlobalScale());
					orthoPoly->setGlobalShift(poly->getGlobalShift());

					//set default display style
					vertices->setEnabled(false);
					orthoPoly->showColors(true);
					orthoPoly->setColor(s_defaultPolylineColor);
					orthoPoly->setWidth(s_defaultPolylineWidth);
					if (!m_clouds.isEmpty())
						orthoPoly->setDisplay_recursive(m_clouds.front().originalDisplay); //set the same 'default' display as the cloud
					orthoPoly->setName(QString("%1.%2").arg(poly->getName()).arg(++polyIndex));

					//add meta data (for Mascaret export)
					{
						orthoPoly->setMetaData(ccPolyline::MetaKeyUpDir(), QVariant(vertDim));
						orthoPoly->setMetaData(ccPolyline::MetaKeyAbscissa(), QVariant(s));
						orthoPoly->setMetaData(ccPolyline::MetaKeyPrefixCenter() + ".x", QVariant(static_cast<double>(I.x)));
						orthoPoly->setMetaData(ccPolyline::MetaKeyPrefixCenter() + ".y", QVariant(static_cast<double>(I.y)));
						orthoPoly->setMetaData(ccPolyline::MetaKeyPrefixCenter() + ".z", QVariant(static_cast<double>(I.z)));
						orthoPoly->setMetaData(ccPolyline::MetaKeyPrefixDirection() + ".x", QVariant(static_cast<double>(nAB.x)));
						orthoPoly->setMetaData(ccPolyline::MetaKeyPrefixDirection() + ".y", QVariant(static_cast<double>(nAB.y)));
						orthoPoly->setMetaData(ccPolyline::MetaKeyPrefixDirection() + ".z", QVariant(static_cast<double>(nAB.z)));
					}

					if (!addPolyline(orthoPoly, false))
					{
						delete orthoPoly;
						orthoPoly = nullptr;
					}
				}
				else
				{
					delete orthoPoly;
					orthoPoly = nullptr;
					ccLog::Error("Not enough memory!");
					//early stop
					i = maxCount;
					break;
				}

				s += s_orthoSectionStep;
			}

			l += lAB;
		}
	}

	poly->showArrow(false, 0, 0);
	if (m_associatedWin)
		m_associatedWin->redraw();
}

ccHObject* ccSectionExtractionTool::getExportGroup(unsigned& defaultGroupID, const QString& defaultName)
{
	MainWindow* mainWin = MainWindow::TheInstance();
	ccHObject* root = mainWin ? mainWin->dbRootObject() : nullptr;
	if (!root)
	{
		ccLog::Warning("Internal error (no MainWindow or DB?!)");
		assert(false);
		return nullptr;
	}

	ccHObject* destEntity = (defaultGroupID != 0 ? root->find(defaultGroupID) : nullptr);
	if (!destEntity)
	{
		destEntity = new ccHObject(defaultName);
		//assign default display
		for (auto & cloud : m_clouds)
		{
			if (cloud.entity)
			{
				destEntity->setDisplay_recursive(cloud.originalDisplay);
				break;
			}
		}
		mainWin->addToDB(destEntity);
		defaultGroupID = destEntity->getUniqueID();
	}
	return destEntity;
}

void ccSectionExtractionTool::exportSections()
{
	if (m_sections.empty())
		return;

	//we only export 'temporary' objects
	unsigned exportCount = 0;

	for (auto & section : m_sections)
	{
		if (section.entity && !section.isInDB)
			++exportCount;
	}

	if (!exportCount)
	{
		//nothing to do
		ccLog::Warning("[ccSectionExtractionTool] All active sections are already in DB");
		return;
	}

	ccHObject* destEntity = getExportGroup(s_polyExportGroupID, "Exported sections");
	assert(destEntity);

	MainWindow* mainWin = MainWindow::TheInstance();

	//export entites
	for (auto & section : m_sections)
	{
		if (section.entity && !section.isInDB)
		{
			destEntity->addChild(section.entity);
			section.isInDB = true;
			section.entity->setDisplay_recursive(destEntity->getDisplay());
			mainWin->addToDB(section.entity, false, false);
		}
	}

	ccLog::Print(QString("[ccSectionExtractionTool] %1 sections exported").arg(exportCount));
}

bool ccSectionExtractionTool::extractSectionContour(const ccPolyline* originalSection,
	const ccPointCloud* originalSectionCloud,
	ccPointCloud* unrolledSectionCloud,
	unsigned sectionIndex,
	ccContourExtractor::ContourType contourType,
	PointCoordinateType maxEdgeLength,
	bool multiPass,
	bool splitContour,
	bool& contourGenerated,
	bool visualDebugMode/*=false*/)
{
	contourGenerated = false;

	if (!originalSectionCloud || !unrolledSectionCloud)
	{
		ccLog::Warning("[ccSectionExtractionTool][extract contour] Internal error: invalid input parameter(s)");
		return false;
	}

	if (originalSectionCloud->size() < 2)
	{
		//nothing to do
		ccLog::Warning(QString("[ccSectionExtractionTool][extract contour] Section #%1 contains less than 2 points and will be ignored").arg(sectionIndex));
		return true;
	}

	//by default, the points in 'unrolledSectionCloud' are 2D (X = curvilinear coordinate, Y = height, Z = 0)
	CCVector3 N(0, 0, 1);
	CCVector3 Y(0, 1, 0);

	std::vector<unsigned> vertIndexes;
	ccPolyline* contour = ccContourExtractor::ExtractFlatContour(unrolledSectionCloud,
		multiPass,
		maxEdgeLength,
		N.u,
		Y.u,
		contourType,
		&vertIndexes,
		visualDebugMode);
	if (contour)
	{
		//update vertices (to replace 'unrolled' points by 'original' ones
		{
			CCLib::GenericIndexedCloud* vertices = contour->getAssociatedCloud();
			if (vertIndexes.size() == static_cast<size_t>(vertices->size()))
			{
				for (unsigned i = 0; i < vertices->size(); ++i)
				{
					const CCVector3* P = vertices->getPoint(i);
					assert(vertIndexes[i] < originalSectionCloud->size());
					*const_cast<CCVector3*>(P) = *originalSectionCloud->getPoint(vertIndexes[i]);
				}

				ccPointCloud* verticesAsPC = dynamic_cast<ccPointCloud*>(vertices);
				if (verticesAsPC)
					verticesAsPC->refreshBB();
			}
			else
			{
				ccLog::Warning("[ccSectionExtractionTool][extract contour] Internal error (couldn't fetch original points indexes?!)");
				delete contour;
				return false;
			}
		}

		std::vector<ccPolyline*> parts;
		if (splitContour)
		{
#ifdef QT_DEBUG
			//compute some stats on the contour
			{
				double minLength = 0;
				double maxLength = 0;
				double sumLength = 0;
				unsigned count = contour->size();
				if (!contour->isClosed())
					--count;
				for (unsigned i=0; i<count; ++i)
				{
					const CCVector3* A = contour->getPoint(i);
					const CCVector3* B = contour->getPoint((i+1) % contour->size());
					CCVector3 e = *B - *A;
					double l = e.norm();
					if (i != 0)
					{
						minLength = std::min(minLength,l);
						maxLength = std::max(maxLength,l);
						sumLength += l;
					}
					else
					{
						minLength = maxLength = sumLength = l;
					}
				}
				ccLog::PrintDebug(QString("Contour: min = %1 / avg = %2 / max = %3").arg(minLength).arg(sumLength/count).arg(maxLength));
			}
#endif

			/*bool success = */contour->split(maxEdgeLength, parts);
			delete contour;
			contour = nullptr;
		}
		else
		{
			parts.push_back(contour);
		}

		//create output group if necessary
		ccHObject* destEntity = getExportGroup(s_profileExportGroupID, "Extracted profiles");
		assert(destEntity);

		for (size_t p = 0; p < parts.size(); ++p)
		{
			ccPolyline* contourPart = parts[p];
			QString name = QString("Section contour #%1").arg(sectionIndex);
			if (parts.size() > 1)
				name += QString("(part %1/%2)").arg(p + 1).arg(parts.size());
			contourPart->setName(name);
			contourPart->setGlobalScale(originalSectionCloud->getGlobalScale());
			contourPart->setGlobalShift(originalSectionCloud->getGlobalShift());
			contourPart->setColor(s_defaultContourColor);
			contourPart->showColors(true);
			//copy meta-data (import for Mascaret export!)
			{
				const QVariantMap& metaData = originalSection->metaData();
				for (QVariantMap::const_iterator it = metaData.begin(); it != metaData.end(); ++it)
				{
					contourPart->setMetaData(it.key(), it.value());
				}
			}

			//add to main DB
			destEntity->addChild(contourPart);
			contourPart->setDisplay_recursive(destEntity->getDisplay());
			MainWindow::TheInstance()->addToDB(contourPart, false, false);
		}

		contourGenerated = true;
	}

	return true;
}

bool ccSectionExtractionTool::extractSectionCloud(const std::vector<CCLib::ReferenceCloud*>& refClouds,
	unsigned sectionIndex,
	bool& cloudGenerated)
{
	cloudGenerated = false;

	ccPointCloud* sectionCloud = nullptr;
	for (int i = 0; i < static_cast<int>(refClouds.size()); ++i)
	{
		if (!refClouds[i])
			continue;
		assert(m_clouds[i].entity); //a valid ref. cloud must have a valid counterpart!

		//extract part/section from each cloud
		ccPointCloud* part = nullptr;

		//if the cloud is a ccPointCloud, we can keep a lot more information
		//when extracting the section cloud
		ccPointCloud* pc = dynamic_cast<ccPointCloud*>(m_clouds[i].entity);
		if (pc)
		{
			part = pc->partialClone(refClouds[i]);
		}
		else
		{
			part = ccPointCloud::From(refClouds[i], m_clouds[i].entity);
		}

		if (part)
		{
			if (i == 0)
			{
				//we simply use this 'part' cloud as the section cloud
				sectionCloud = part;
			}
			else
			{
				//fuse it with the global cloud
				unsigned cloudSizeBefore = sectionCloud->size();
				unsigned partSize = part->size();
				sectionCloud->append(part, cloudSizeBefore, true);

				//don't need it anymore
				delete part;
				part = nullptr;
				//check that it actually worked!
				if (sectionCloud->size() != cloudSizeBefore + partSize)
				{
					//not enough memory
					ccLog::Warning("[ccSectionExtractionTool][extract cloud] Not enough memory");
					delete sectionCloud;
					return false;
				}
			}
		}
		else
		{
			//not enough memory
			ccLog::Warning("[ccSectionExtractionTool][extract cloud] Not enough memory");
			delete sectionCloud;
			return false;
		}
	}

	if (sectionCloud)
	{
		//create output group if necessary
		ccHObject* destEntity = getExportGroup(s_cloudExportGroupID, "Extracted section clouds");
		assert(destEntity);

		sectionCloud->setName(QString("Section cloud #%1").arg(sectionIndex));
		sectionCloud->setDisplay(destEntity->getDisplay());

		//add to main DB
		destEntity->addChild(sectionCloud);
		MainWindow::TheInstance()->addToDB(sectionCloud, false, false);

		cloudGenerated = true;
	}

	return true;
}

struct Segment
{
	Segment()
		: A(0, 0)
		, B(0, 0)
		, u(0, 0)
		, d(0)
		, curvPos(0)
	{}

	CCVector2 A, B, u;
	PointCoordinateType d, curvPos;
};

void ccSectionExtractionTool::unfoldPoints()
{
	if (!m_selectedPoly || !m_selectedPoly->entity)
	{
		assert(false);
		return;
	}

	ccPolyline* poly = m_selectedPoly->entity;
	unsigned polyVertCount = poly->size();
	if (polyVertCount < 2)
	{
		ccLog::Error("Invalid polyline?!");
		assert(false);
		return;
	}

	//compute loaded clouds bounding-box
	ccBBox box;
	unsigned totalPointCount = 0;
	{
		for (auto & cloud : m_clouds)
		{
			if (cloud.entity)
			{
				box += cloud.entity->getOwnBB();
				totalPointCount += cloud.entity->size();
			}
		}
	}

	static double s_defaultThickness = -1.0;
	if (s_defaultThickness <= 0)
	{
		s_defaultThickness = box.getMaxBoxDim() / 10.0;
	}

	bool ok;
	double thickness = QInputDialog::getDouble(MainWindow::TheInstance(), "Thickness", "Distance to polyline:", s_defaultThickness, 1.0e-6, 1.0e6, 6, &ok);
	if (!ok)
		return;
	s_defaultThickness = thickness;

	//projection direction
	int vertDim = m_UI->vertAxisComboBox->currentIndex();
	int xDim = (vertDim < 2 ? vertDim + 1 : 0);
	int yDim = (xDim < 2 ? xDim + 1 : 0);

	//we consider half of the total thickness as points can be on both sides!
	double maxSquareDistToPolyline = (thickness / 2) * (thickness / 2);

	//prepare the computation of 2D distances
	std::vector<Segment> segments;
	unsigned polySegmentCount = poly->isClosed() ? polyVertCount : polyVertCount - 1;
	{
		try
		{
			segments.reserve(polySegmentCount);
		}
		catch (const std::bad_alloc&)
		{
			//not enough memory
			ccLog::Error("Not enough memory");
			return;
		}

		PointCoordinateType curvPos = 0;
		for (unsigned j = 0; j < polySegmentCount; ++j)
		{
			//current polyline segment
			const CCVector3* A = poly->getPoint(j);
			const CCVector3* B = poly->getPoint((j + 1) % polyVertCount);

			Segment s;
			{
				s.A = CCVector2(A->u[xDim], A->u[yDim]);
				s.B = CCVector2(B->u[xDim], B->u[yDim]);
				s.u = s.B - s.A;
				s.d = s.u.norm();
				if (s.d > ZERO_TOLERANCE)
				{
					s.curvPos = curvPos;
					s.u /= s.d;
					segments.push_back(s);
				}
			}

			//update curvilinear pos
			curvPos += (*B - *A).norm();
		}
	}

	ccProgressDialog pdlg(true);
	CCLib::NormalizedProgress nprogress(&pdlg, totalPointCount);
	pdlg.setMethodTitle(tr("Unfold cloud(s)"));
	pdlg.setInfo(tr("Number of segments: %1\nNumber of points: %2").arg(polySegmentCount).arg(totalPointCount));
	pdlg.start();
	QCoreApplication::processEvents();

	unsigned exportedClouds = 0;

	//for each cloud
	for (auto & pc : m_clouds)
	{
		ccGenericPointCloud* cloud = pc.entity;
		if (!cloud)
		{
			assert(false);
			continue;
		}

		CCLib::ReferenceCloud unfoldedIndexes(cloud);
		if (!unfoldedIndexes.reserve(cloud->size()))
		{
			ccLog::Error("Not enough memory");
			return;
		}
		std::vector<CCVector3> unfoldedPoints;
		try
		{
			unfoldedPoints.reserve(cloud->size());
		}
		catch (const std::bad_alloc&)
		{
			ccLog::Error("Not enough memory");
			return;
		}

		//now test each point and see if it's close to the current polyline (in 2D)
		for (unsigned i = 0; i < cloud->size(); ++i)
		{
			const CCVector3* P = cloud->getPoint(i);
			CCVector2 P2D(P->u[xDim], P->u[yDim]);

			//test each segment
			int closestSegment = -1;
			PointCoordinateType minSquareDist = -PC_ONE;
			for (unsigned j = 0; j < polySegmentCount; ++j)
			{
				const Segment& s = segments[j];
				CCVector2 AP2D = P2D - s.A;

				//longitudinal 'distance'
				PointCoordinateType dotprod = s.u.dot(AP2D);

				PointCoordinateType squareDist = 0;
				if (dotprod < 0.0f)
				{
					//dist to nearest vertex
					squareDist = AP2D.norm2();
				}
				else if (dotprod > s.d)
				{
					//dist to nearest vertex
					squareDist = (P2D - s.B).norm2();
				}
				else
				{
					//orthogonal distance
					squareDist = (AP2D - s.u*dotprod).norm2();
				}

				if (squareDist <= maxSquareDistToPolyline)
				{
					if (closestSegment < 0 || squareDist < minSquareDist)
					{
						minSquareDist = squareDist;
						closestSegment = static_cast<int>(j);
					}
				}
			}

			if (closestSegment >= 0)
			{
				const Segment& s = segments[closestSegment];

				//we use the curvilinear position of the point in the X dimension (and Y is 0)
				CCVector3 Q;
				{
					CCVector2 AP2D = P2D - s.A;
					PointCoordinateType dotprod = s.u.dot(AP2D);
					PointCoordinateType d = (AP2D - s.u*dotprod).norm();

					//compute the sign of 'minDist'
					PointCoordinateType crossprod = AP2D.y * s.u.x - AP2D.x * s.u.y;

					Q.u[xDim] = s.curvPos + dotprod;
					Q.u[yDim] = crossprod < 0 ? -d : d; //signed orthogonal distance to the polyline
					Q.u[vertDim] = P->u[vertDim];
				}

				unfoldedIndexes.addPointIndex(i);
				unfoldedPoints.push_back(Q);
			}

			if (!nprogress.oneStep())
			{
				ccLog::Warning("[Unfold] Process cancelled by the user");
				return;
			}

		} //for each point

		if (unfoldedIndexes.size() != 0)
		{
			//assign the default global shift & scale info
			ccPointCloud* unfoldedCloud = nullptr;
			{
				if (cloud->isA(CC_TYPES::POINT_CLOUD))
					unfoldedCloud = static_cast<ccPointCloud*>(cloud)->partialClone(&unfoldedIndexes);
				else
					unfoldedCloud = ccPointCloud::From(&unfoldedIndexes, cloud);
			}
			if (!unfoldedCloud)
			{
				ccLog::Error("Not enough memory");
				return;
			}

			assert(unfoldedCloud->size() == unfoldedPoints.size());
			CCVector3 C = box.minCorner();
			C.u[vertDim] = 0;
			C.u[xDim] = box.minCorner().u[xDim]; //we start at the bounding-box limit
			for (unsigned i = 0; i < unfoldedCloud->size(); ++i)
			{
				//update the points positions
				*const_cast<CCVector3*>(unfoldedCloud->getPoint(i)) = unfoldedPoints[i] + C;
			}
			unfoldedCloud->invalidateBoundingBox();

			unfoldedCloud->setName(cloud->getName() + ".unfolded");
			unfoldedCloud->setGlobalShift(cloud->getGlobalShift());
			unfoldedCloud->setGlobalScale(cloud->getGlobalScale());

			unfoldedCloud->shrinkToFit();
			unfoldedCloud->setDisplay(pc.originalDisplay);
			MainWindow::TheInstance()->addToDB(unfoldedCloud);

			++exportedClouds;
		}
		else
		{
			ccLog::Warning(QString("[Unfold] No point of the cloud '%1' were unfolded (check parameters)").arg(cloud->getName()));
		}

	} //for each cloud

	ccLog::Print(QString("[Unfold] %1 cloud(s) exported").arg(exportedClouds));
}

struct Segment2D
{
	Segment2D() : s(0) {}

	CCVector2 A, B, uAB;
	PointCoordinateType lAB;
	PointCoordinateType s; //curvilinear coordinate
};

void ccSectionExtractionTool::extractPoints()
{
	static double s_defaultSectionThickness = -1.0;
	static double s_contourMaxEdgeLength = 0;
	static bool s_extractSectionsAsClouds = false;
	static bool s_extractSectionsAsContours = true;
	static bool s_multiPass = false;
	static bool s_splitContour = false;
	static ccContourExtractor::ContourType s_extractSectionsType = ccContourExtractor::LOWER;

	//number of eligible sections
	unsigned sectionCount = 0;
	{
		for (auto & section : m_sections)
		{
			if (section.entity && section.entity->size() > 1)
				++sectionCount;
		}
	}
	if (sectionCount == 0)
	{
		ccLog::Error("No (valid) section!");
		return;
	}

	//compute loaded clouds bounding-box
	ccBBox box;
	unsigned pointCount = 0;

	for (auto & cloud : m_clouds)
	{
		if (cloud.entity)
		{
			box += cloud.entity->getOwnBB();
			pointCount += cloud.entity->size();
		}
	}

	if (s_defaultSectionThickness <= 0)
	{
		s_defaultSectionThickness = box.getMaxBoxDim() / 500.0;
	}
	if (s_contourMaxEdgeLength <= 0)
	{
		s_contourMaxEdgeLength = box.getMaxBoxDim() / 500.0;
	}

	//show dialog
	ccSectionExtractionSubDlg sesDlg(MainWindow::TheInstance());
	sesDlg.setActiveSectionCount(sectionCount);
	sesDlg.setSectionThickness(s_defaultSectionThickness);
	sesDlg.setMaxEdgeLength(s_contourMaxEdgeLength);
	sesDlg.doExtractClouds(s_extractSectionsAsClouds);
	sesDlg.doExtractContours(s_extractSectionsAsContours, s_extractSectionsType);
	sesDlg.doUseMultiPass(s_multiPass);
	sesDlg.doSplitContours(s_splitContour);

	if (!sesDlg.exec())
		return;

	s_defaultSectionThickness = sesDlg.getSectionThickness();
	s_contourMaxEdgeLength = sesDlg.getMaxEdgeLength();
	s_extractSectionsAsClouds = sesDlg.extractClouds();
	s_extractSectionsAsContours = sesDlg.extractContours();
	s_extractSectionsType = sesDlg.getContourType();
	s_multiPass = sesDlg.useMultiPass();
	s_splitContour = sesDlg.splitContours();
	bool visualDebugMode = sesDlg.visualDebugMode();

	//progress dialog
	ccProgressDialog pdlg(true);
	CCLib::NormalizedProgress nprogress(&pdlg, static_cast<unsigned>(sectionCount));
	if (!visualDebugMode)
	{
		pdlg.setMethodTitle(tr("Extract sections"));
		pdlg.setInfo(tr("Number of sections: %1\nNumber of points: %2").arg(sectionCount).arg(pointCount));
		pdlg.start();
		QCoreApplication::processEvents();
	}

	int vertDim = m_UI->vertAxisComboBox->currentIndex();
	int xDim = (vertDim < 2 ? vertDim + 1 : 0);
	int yDim = (xDim < 2 ? xDim + 1 : 0);

	//we consider half of the total thickness as points can be on both sides!
	double sectionThicknessSq = std::pow(s_defaultSectionThickness / 2.0, 2.0);
	bool error = false;

	unsigned generatedContours = 0;
	unsigned generatedClouds = 0;

	try
	{
		//for each slice
		for (int s = 0; s < m_sections.size(); ++s)
		{
			ccPolyline* poly = m_sections[s].entity;
			if (poly)
			{
				unsigned polyVertCount = poly->size();
				if (polyVertCount < 2)
				{
					assert(false);
					continue;
				}
				unsigned polySegmentCount = poly->isClosed() ? polyVertCount : polyVertCount - 1;

				//project the section in '2D'
				std::vector<Segment2D> polySegments2D;
				{
					polySegments2D.reserve(polySegmentCount);
					PointCoordinateType s = 0;
					for (unsigned j = 0; j < polySegmentCount; ++j)
					{
						Segment2D seg2D;
						const CCVector3* A = poly->getPoint(j);
						const CCVector3* B = poly->getPoint((j + 1) % polyVertCount);
						seg2D.A = CCVector2(A->u[xDim], A->u[yDim]);
						seg2D.B = CCVector2(B->u[xDim], B->u[yDim]);
						seg2D.uAB = seg2D.B - seg2D.A; //(unit) direction
						seg2D.lAB = seg2D.uAB.norm(); //length
						seg2D.s = s;
						s += seg2D.lAB;

						if (seg2D.lAB < ZERO_TOLERANCE)
						{
							//ignore too small segments
							continue;
						}
						
						seg2D.uAB /= seg2D.lAB;
						polySegments2D.push_back(seg2D);
					}

					if (polySegments2D.empty())
					{
						assert(false);
						continue;
					}
					polySegments2D.shrink_to_fit();
				}

				int cloudCount = m_clouds.size();
				std::vector<CCLib::ReferenceCloud*> refClouds;
				if (s_extractSectionsAsClouds)
				{
					refClouds.resize(cloudCount, nullptr);
				}

				//for contour extraction as a polyline
				ccPointCloud* originalSlicePoints = nullptr;
				ccPointCloud* unrolledSlicePoints = nullptr;
				if (s_extractSectionsAsContours)
				{
					originalSlicePoints = new ccPointCloud("section.orig");
					unrolledSlicePoints = new ccPointCloud("section.unroll");

					//assign them the default (first!) global shift & scale info
					assert(!m_clouds.empty());
					ccGenericPointCloud* cloud = m_clouds.front().entity;
					originalSlicePoints->setGlobalScale(cloud->getGlobalScale());
					originalSlicePoints->setGlobalShift(cloud->getGlobalShift());
				}

				//for each cloud
				for (int c = 0; c < cloudCount; ++c)
				{
					ccGenericPointCloud* cloud = m_clouds[c].entity;
					if (cloud)
					{
						//for contour extraction as a cloud
						CCLib::ReferenceCloud* refCloud = nullptr;
						if (s_extractSectionsAsClouds)
						{
							refCloud = new CCLib::ReferenceCloud(cloud);
						}

						//compute the distance of each point to the current polyline segment
						for (unsigned i = 0; i < cloud->size(); ++i)
						{
							const CCVector3* P = cloud->getPoint(i);
							CCVector2 P2D(P->u[xDim], P->u[yDim]);

							//for each vertex
							PointCoordinateType minSquareDist = -PC_ONE;
							PointCoordinateType curvilinearPos = 0.0;
							size_t minIndex = 0;
							for (size_t j = 0; j < polySegments2D.size(); ++j)
							{
								const Segment2D& seg2D = polySegments2D[j];
								CCVector2 AP2D = P2D - seg2D.A;

								//square distance to the polyline
								PointCoordinateType squareDist = 0;

								//longitudinal 'distance'
								double dotprod = seg2D.uAB.dot(AP2D);
								if (dotprod < 0)
								{
									if (j == 0 && !poly->isClosed())
										continue;
									squareDist = AP2D.norm2();
								}
								else if (dotprod > seg2D.lAB)
								{
									if (j + 1 == polySegments2D.size() && !poly->isClosed())
										continue;
									squareDist = (P2D - seg2D.B).norm2();
								}
								else
								{
									//orthogonal distance
									squareDist = (AP2D - seg2D.uAB * dotprod).norm2();
								}

								if (minSquareDist < 0 || squareDist < minSquareDist)
								{
									minSquareDist = squareDist;
									curvilinearPos = dotprod;
									minIndex = j;
								}
							}

							//elligible point?
							if (minSquareDist >= 0 && minSquareDist < sectionThicknessSq)
							{
								//if we extract the section as cloud(s), we add the point to the (current) ref. cloud
								if (s_extractSectionsAsClouds)
								{
									assert(refCloud);
									unsigned refCloudSize = refCloud->size();
									if (refCloudSize == refCloud->capacity())
									{
										refCloudSize += (refCloudSize / 2 + 1);
										if (!refCloud->reserve(refCloudSize))
										{
											//not enough memory
											ccLog::Warning("[ccSectionExtractionTool] Not enough memory");
											error = true;
											break;
										}
									}
									refCloud->addPointIndex(i);
								}

								//if we extract the section as contour(s), we add it to the 2D points set
								if (s_extractSectionsAsContours)
								{
									assert(originalSlicePoints && unrolledSlicePoints);
									assert(originalSlicePoints->size() == unrolledSlicePoints->size());

									unsigned cloudSize = originalSlicePoints->size();
									if (cloudSize == originalSlicePoints->capacity())
									{
										cloudSize += (cloudSize / 2 + 1);
										if (!originalSlicePoints->reserve(cloudSize)
											|| !unrolledSlicePoints->reserve(cloudSize))
										{
											//not enough memory
											ccLog::Warning("[ccSectionExtractionTool] Not enough memory");
											error = true;
											break;
										}
									}

									const Segment2D& seg2D = polySegments2D[minIndex];

									//we project the 'real' 3D point in the section plane
									CCVector3 Pproj3D;
									{
										Pproj3D.u[xDim] = seg2D.A.x + seg2D.uAB.x * curvilinearPos;
										Pproj3D.u[yDim] = seg2D.A.y + seg2D.uAB.y * curvilinearPos;
										Pproj3D.u[vertDim] = P->u[vertDim];
									}
									originalSlicePoints->addPoint(Pproj3D);
									unrolledSlicePoints->addPoint(CCVector3(seg2D.s + curvilinearPos, P->u[vertDim], 0));
								}
							}

							if (error)
							{
								break;
							}

						} //for each point

						if (refCloud)
						{
							assert(s_extractSectionsAsClouds);
							if (error || refCloud->size() == 0)
							{
								delete refCloud;
								refCloud = nullptr;
							}
							else
							{
								refClouds[c] = refCloud;
							}
						}

					}

					if (error)
					{
						break;
					}

				} //for each cloud

				if (!error)
				{
					//Extract sections as (polyline) contours
					if (/*!error && */s_extractSectionsAsContours)
					{
						assert(originalSlicePoints && unrolledSlicePoints);
						bool contourGenerated = false;
						error = !extractSectionContour(	poly,
														originalSlicePoints,
														unrolledSlicePoints,
														s + 1,
														s_extractSectionsType,
														s_contourMaxEdgeLength,
														s_multiPass,
														s_splitContour,
														contourGenerated,
														visualDebugMode);

						if (contourGenerated)
							++generatedContours;
					}

					//Extract sections as clouds
					if (!error && s_extractSectionsAsClouds)
					{
						assert(static_cast<int>(refClouds.size()) == cloudCount);
						bool cloudGenerated = false;
						error = !extractSectionCloud(refClouds, s + 1, cloudGenerated);
						if (cloudGenerated)
							++generatedClouds;
					}
				}

				//release memory
				for (auto & refCloud : refClouds)
				{
					delete refCloud;
					refCloud = nullptr;
				}

				delete originalSlicePoints;
				originalSlicePoints = nullptr;

				delete unrolledSlicePoints;
				unrolledSlicePoints = nullptr;
			} //if (poly)

			if (!nprogress.oneStep())
			{
				ccLog::Warning("[ccSectionExtractionTool] Canceled by user");
				error = true;
			}

			if (error)
				break;
		} //for (int s=0; s<m_sections.size(); ++s)
	}
	catch (const std::bad_alloc&)
	{
		error = true;
	}

	if (error)
	{
		ccLog::Error("An error occurred (see console)");
	}
	else
	{
		ccLog::Print(QString("[ccSectionExtractionTool] Job done (%1 contour(s) and %2 cloud(s) were generated)").arg(generatedContours).arg(generatedClouds));
	}
}