ccClippingBoxTool.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  8. //# #
  9. //# This program is distributed in the hope that it will be useful, #
  10. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  12. //# GNU General Public License for more details. #
  13. //# #
  14. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  15. //# #
  16. //##########################################################################
  17. #include "ccClippingBoxTool.h"
  18. //Local
  19. #include "ccBoundingBoxEditorDlg.h"
  20. #include "ccClippingBoxRepeatDlg.h"
  21. #include "ccContourLinesGenerator.h"
  22. #include "ccCropTool.h"
  23. #include "ccGLWindowInterface.h"
  24. #include "ccReservedIDs.h"
  25. #include "mainwindow.h"
  26. #include "db_tree/ccDBRoot.h"
  27. //qCC_db
  28. #include <ccClipBox.h>
  29. #include <ccPointCloud.h>
  30. #include <ccProgressDialog.h>
  31. #include <ccRasterGrid.h>
  32. //Qt
  33. #include <QMessageBox>
  34. namespace
  35. {
  36. //Last envelope or contour unique ID
  37. std::vector<unsigned> s_lastContourUniqueIDs;
  38. //Envelope extraction parameters (global)
  39. double s_maxEnvelopeEdgeLength = -1.0;
  40. //Meta-data key: origin entity UUID
  41. constexpr char s_originEntityUUID[] = "OriginEntityUUID";
  42. //Meta-data key: slice (unique) ID
  43. constexpr char s_sliceID[] = "SliceID";
  44. }
  45. // persistent map of the previous box used for each entity
  46. struct ccClipBoxParams
  47. {
  48. ccBBox box;
  49. ccGLMatrix trans;
  50. };
  51. static QMap< unsigned, ccClipBoxParams > s_lastBoxParams;
  52. ccClippingBoxTool::ccClippingBoxTool(QWidget* parent)
  53. : ccOverlayDialog(parent)
  54. , Ui::ClippingBoxDlg()
  55. , m_clipBox(nullptr)
  56. {
  57. setupUi(this);
  58. connect(editBoxToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::editBox);
  59. connect(extractContourToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::extractContour);
  60. connect(removeLastContourToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::removeLastContour);
  61. connect(exportButton, &QToolButton::clicked, this, &ccClippingBoxTool::exportSlice);
  62. connect(exportMultButton, &QToolButton::clicked, this, &ccClippingBoxTool::exportMultSlices);
  63. connect(resetButton, &QToolButton::clicked, this, &ccClippingBoxTool::reset);
  64. connect(restoreToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::restoreLastBox);
  65. connect(closeButton, &QToolButton::clicked, this, &ccClippingBoxTool::closeDialog);
  66. connect(showBoxToolButton, &QToolButton::toggled, this, &ccClippingBoxTool::toggleBox);
  67. connect(showInteractorsToolButton, &QToolButton::toggled, this, &ccClippingBoxTool::toggleInteractors);
  68. connect(thickXDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &ccClippingBoxTool::thicknessChanged);
  69. connect(thickYDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &ccClippingBoxTool::thicknessChanged);
  70. connect(thickZDoubleSpinBox, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &ccClippingBoxTool::thicknessChanged);
  71. connect(minusXShiftToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::shiftXMinus);
  72. connect(plusXShiftToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::shiftXPlus);
  73. connect(minusYShiftToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::shiftYMinus);
  74. connect(plusYShiftToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::shiftYPlus);
  75. connect(minusZShiftToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::shiftZMinus);
  76. connect(plusZShiftToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::shiftZPlus);
  77. viewButtonsFrame->setEnabled(true);
  78. connect(viewUpToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::setTopView);
  79. connect(viewDownToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::setBottomView);
  80. connect(viewFrontToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::setFrontView);
  81. connect(viewBackToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::setBackView);
  82. connect(viewLeftToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::setLeftView);
  83. connect(viewRightToolButton, &QToolButton::clicked, this, &ccClippingBoxTool::setRightView);
  84. s_maxEnvelopeEdgeLength = -1.0;
  85. //s_lastContourUniqueIDs.clear();
  86. removeLastContourToolButton->setEnabled(false);
  87. }
  88. ccClippingBoxTool::~ccClippingBoxTool()
  89. {
  90. if (m_clipBox)
  91. delete m_clipBox;
  92. m_clipBox = nullptr;
  93. }
  94. void ccClippingBoxTool::editBox()
  95. {
  96. if (!m_clipBox)
  97. return;
  98. ccBBox box;
  99. ccGLMatrix transformation;
  100. m_clipBox->get(box, transformation);
  101. //shift the box to its real center
  102. {
  103. CCVector3 C = box.getCenter();
  104. CCVector3 realC = transformation * C;
  105. box += (realC - C);
  106. ccGLMatrix transMat;
  107. transMat.setTranslation(-realC);
  108. transformation.clearTranslation();
  109. transMat = transformation * transMat;
  110. transMat.setTranslation(transMat.getTranslationAsVec3D() + realC);
  111. transformation = transMat;
  112. }
  113. ccBoundingBoxEditorDlg bbeDlg(true, false, this); //show the box 'axes' (orientation)
  114. bbeDlg.setBaseBBox(box, false);
  115. bbeDlg.showInclusionWarning(false);
  116. bbeDlg.setWindowTitle("Edit clipping box");
  117. //transformation.invert();
  118. bbeDlg.setBoxAxes( transformation.getColumnAsVec3D(0),
  119. transformation.getColumnAsVec3D(1),
  120. transformation.getColumnAsVec3D(2) );
  121. if (!bbeDlg.exec())
  122. return;
  123. box = bbeDlg.getBox();
  124. //construct the local box orientation matrix
  125. {
  126. CCVector3d X;
  127. CCVector3d Y;
  128. CCVector3d Z;
  129. bbeDlg.getBoxAxes(X, Y, Z);
  130. //make sure the vectors define an orthogonal basis
  131. Z = X.cross(Y);
  132. Y = Z.cross(X);
  133. X.normalize();
  134. Y.normalize();
  135. Z.normalize();
  136. ccGLMatrixd rotMat;
  137. rotMat.setColumn(0, X);
  138. rotMat.setColumn(1, Y);
  139. rotMat.setColumn(2, Z);
  140. CCVector3 C = box.getCenter();
  141. ccGLMatrixd transMat;
  142. transMat.setTranslation(-C);
  143. transMat = rotMat * transMat;
  144. transMat.setTranslation(transMat.getTranslationAsVec3D() + C);
  145. m_clipBox->setGLTransformation(ccGLMatrix(transMat.data()));
  146. }
  147. m_clipBox->setBox(box);
  148. //onBoxModified(&box); //DGM: automatically called by 'm_clipBox'
  149. if (m_associatedWin)
  150. {
  151. m_associatedWin->redraw();
  152. }
  153. }
  154. void ccClippingBoxTool::toggleInteractors(bool state)
  155. {
  156. if (m_clipBox)
  157. m_clipBox->setSelected(state);
  158. if (m_associatedWin)
  159. m_associatedWin->redraw();
  160. }
  161. void ccClippingBoxTool::toggleBox(bool state)
  162. {
  163. if (m_clipBox)
  164. m_clipBox->showBox(state);
  165. if (m_associatedWin)
  166. m_associatedWin->redraw();
  167. }
  168. bool ccClippingBoxTool::addAssociatedEntity(ccHObject* entity)
  169. {
  170. if (!entity)
  171. {
  172. assert(false);
  173. return false;
  174. }
  175. //special case
  176. if (entity->isGroup())
  177. {
  178. for (unsigned i = 0; i < entity->getChildrenNumber(); ++i)
  179. {
  180. if (!addAssociatedEntity(entity->getChild(i)))
  181. {
  182. return false;
  183. }
  184. }
  185. return true;
  186. }
  187. if (!m_associatedWin || !m_clipBox)
  188. {
  189. ccLog::Error(QString("[Clipping box] No associated 3D view or no valid clipping box!"));
  190. return false;
  191. }
  192. //we don't handle entities associated to another context
  193. if (entity->getDisplay() != m_associatedWin)
  194. {
  195. ccLog::Warning(QString("[Clipping box] Can't use entity '%1' cause it's not displayed in the active 3D view!").arg(entity->getName()));
  196. return false;
  197. }
  198. bool firstEntity = (m_clipBox && m_clipBox->getContainer().getChildrenNumber() == 0);
  199. if (firstEntity)
  200. {
  201. restoreToolButton->setEnabled(false);
  202. contourGroupBox->setEnabled(false);
  203. }
  204. if (!m_clipBox->addAssociatedEntity(entity))
  205. {
  206. //error message already issued
  207. ccLog::Error("An error occurred (see Console)");
  208. return false;
  209. }
  210. if (s_lastBoxParams.contains(entity->getUniqueID()))
  211. {
  212. restoreToolButton->setEnabled(true);
  213. }
  214. if (entity->isKindOf(CC_TYPES::POINT_CLOUD))
  215. {
  216. contourGroupBox->setEnabled(true);
  217. }
  218. //force visibility
  219. entity->setVisible(true);
  220. entity->setEnabled(true);
  221. if (m_associatedWin)
  222. {
  223. m_associatedWin->redraw();
  224. }
  225. //set proper "steps" value for slice thickness editors
  226. {
  227. CCVector3 diag = m_clipBox->getBox().getDiagVec();
  228. thickXDoubleSpinBox->setSingleStep(diag.x / 100.0);
  229. thickYDoubleSpinBox->setSingleStep(diag.y / 100.0);
  230. thickZDoubleSpinBox->setSingleStep(diag.z / 100.0);
  231. }
  232. s_maxEnvelopeEdgeLength = -1.0;
  233. s_lastContourUniqueIDs.resize(0);
  234. removeLastContourToolButton->setEnabled(false);
  235. return true;
  236. }
  237. unsigned ccClippingBoxTool::getNumberOfAssociatedEntity() const
  238. {
  239. return m_clipBox ? m_clipBox->getContainer().getChildrenNumber() : 0;
  240. }
  241. bool ccClippingBoxTool::linkWith(ccGLWindowInterface* win)
  242. {
  243. if (m_associatedWin && m_clipBox)
  244. {
  245. // restore the the clipping planes
  246. m_associatedWin->setClippingPlanesEnabled(true);
  247. //remove clipping box from previous window
  248. m_associatedWin->removeFromOwnDB(m_clipBox);
  249. m_clipBox->disconnect(this);
  250. delete m_clipBox;
  251. m_clipBox = nullptr;
  252. m_associatedWin->redraw();
  253. }
  254. if (!ccOverlayDialog::linkWith(win))
  255. {
  256. return false;
  257. }
  258. if (win)
  259. {
  260. if (!m_clipBox)
  261. {
  262. m_clipBox = new ccClipBox(QString(), static_cast<unsigned>(ReservedIDs::CLIPPING_BOX));
  263. m_clipBox->setVisible(true);
  264. m_clipBox->setEnabled(true);
  265. m_clipBox->setSelected(showInteractorsToolButton->isChecked());
  266. connect(m_clipBox, &ccClipBox::boxModified, this, &ccClippingBoxTool::onBoxModified);
  267. }
  268. // the clipping box tool doesn't tak the clipping planes into account
  269. // so we'll disable them temporarily
  270. m_associatedWin->setClippingPlanesEnabled(false);
  271. m_associatedWin->addToOwnDB(m_clipBox);
  272. }
  273. return true;
  274. }
  275. bool ccClippingBoxTool::start()
  276. {
  277. assert(!m_processing);
  278. assert(m_associatedWin);
  279. if (!m_associatedWin || !m_clipBox)
  280. return false;
  281. m_clipBox->reset();
  282. //the user must not close this window!
  283. m_associatedWin->setUnclosable(true);
  284. //m_associatedWin->displayNewMessage(QString(), ccGLWindowInterface::UPPER_CENTER_MESSAGE); //clear the area
  285. //m_associatedWin->displayNewMessage("[Rotation/Translation mode]", ccGLWindowInterface::UPPER_CENTER_MESSAGE, false, 3600, ccGLWindowInterface::MANUAL_TRANSFORMATION_MESSAGE);
  286. m_associatedWin->redraw();
  287. return ccOverlayDialog::start();
  288. }
  289. void ccClippingBoxTool::stop(bool state)
  290. {
  291. if (m_clipBox)
  292. {
  293. if (state && m_clipBox->getContainer().getChildrenNumber())
  294. {
  295. //save clip box parameters
  296. ccClipBoxParams params;
  297. m_clipBox->get(params.box, params.trans);
  298. for (unsigned ci = 0; ci != m_clipBox->getContainer().getChildrenNumber(); ++ci)
  299. {
  300. s_lastBoxParams[m_clipBox->getContainer().getChild(ci)->getUniqueID()] = params;
  301. }
  302. }
  303. m_clipBox->releaseAssociatedEntities();
  304. }
  305. if (m_associatedWin)
  306. {
  307. m_associatedWin->setUnclosable(false);
  308. //m_associatedWin->displayNewMessage("[Rotation/Translation mode OFF]", ccGLWindowInterface::UPPER_CENTER_MESSAGE, false, 2, ccGLWindowInterface::MANUAL_TRANSFORMATION_MESSAGE);
  309. m_associatedWin->redraw();
  310. }
  311. ccOverlayDialog::stop(state);
  312. }
  313. void ccClippingBoxTool::removeLastContour()
  314. {
  315. if (s_lastContourUniqueIDs.empty())
  316. return;
  317. MainWindow* mainWindow = MainWindow::TheInstance();
  318. if (mainWindow)
  319. {
  320. for (size_t i = 0; i < s_lastContourUniqueIDs.size(); ++i)
  321. {
  322. ccHObject* obj = mainWindow->db()->find(s_lastContourUniqueIDs[i]);
  323. if (obj)
  324. {
  325. //obj->prepareDisplayForRefresh();
  326. mainWindow->removeFromDB(obj);
  327. ccGLWindowInterface* win = mainWindow->getActiveGLWindow();
  328. if (win)
  329. win->redraw();
  330. }
  331. }
  332. }
  333. s_lastContourUniqueIDs.resize(0);
  334. removeLastContourToolButton->setEnabled(false);
  335. }
  336. ccHObject* GetSlice(ccHObject* obj, ccClipBox* clipBox, bool silent)
  337. {
  338. assert(clipBox);
  339. if (!obj)
  340. {
  341. assert(false);
  342. return nullptr;
  343. }
  344. if (obj->isKindOf(CC_TYPES::POINT_CLOUD))
  345. {
  346. ccGenericPointCloud* inputCloud = ccHObjectCaster::ToGenericPointCloud(obj);
  347. ccGenericPointCloud::VisibilityTableType selectionTable;
  348. try
  349. {
  350. selectionTable.resize(inputCloud->size());
  351. }
  352. catch (const std::bad_alloc&)
  353. {
  354. if (!silent)
  355. {
  356. ccLog::Error("Not enough memory!");
  357. }
  358. return nullptr;
  359. }
  360. clipBox->flagPointsInside(inputCloud, &selectionTable);
  361. ccGenericPointCloud* sliceCloud = inputCloud->createNewCloudFromVisibilitySelection(false, &selectionTable, nullptr, true);
  362. // specific case: all points were selected
  363. if (sliceCloud == inputCloud)
  364. {
  365. sliceCloud = inputCloud->clone();
  366. }
  367. if (!sliceCloud)
  368. {
  369. if (!silent)
  370. ccLog::Error("Not enough memory!");
  371. }
  372. else if (sliceCloud->size() == 0)
  373. {
  374. //ccLog::Warning("Empty slice"); // too many logs
  375. delete sliceCloud;
  376. sliceCloud = nullptr;
  377. }
  378. return sliceCloud;
  379. }
  380. else if (obj->isKindOf(CC_TYPES::MESH))
  381. {
  382. const ccGLMatrix* _transformation = nullptr;
  383. ccGLMatrix transformation;
  384. if (clipBox->isGLTransEnabled())
  385. {
  386. transformation = clipBox->getGLTransformation().inverse();
  387. _transformation = &transformation;
  388. }
  389. const ccBBox& cropBox = clipBox->getBox();
  390. ccHObject* mesh = ccCropTool::Crop(obj, cropBox, true, _transformation);
  391. if (!mesh)
  392. {
  393. if (!silent)
  394. ccLog::Error("Failed to segment the mesh!");
  395. return nullptr;
  396. }
  397. return mesh;
  398. }
  399. return nullptr;
  400. }
  401. void ccClippingBoxTool::exportSlice()
  402. {
  403. if (!m_clipBox || !MainWindow::TheInstance())
  404. return;
  405. for (unsigned ci = 0; ci != m_clipBox->getContainer().getChildrenNumber(); ++ci)
  406. {
  407. ccHObject* obj = m_clipBox->getContainer().getChild(ci);
  408. if (!obj)
  409. {
  410. assert(false);
  411. continue;
  412. }
  413. ccHObject* result = GetSlice(obj, m_clipBox, false);
  414. if (result)
  415. {
  416. result->setName(obj->getName() + QString(".section"));
  417. result->setDisplay(obj->getDisplay());
  418. result->prepareDisplayForRefresh();
  419. if (obj->getParent())
  420. obj->getParent()->addChild(result);
  421. MainWindow::TheInstance()->addToDB(result);
  422. }
  423. }
  424. }
  425. void ccClippingBoxTool::extractContour()
  426. {
  427. extractSlicesAndContours(/*singleSliceMode=*/true);
  428. }
  429. void ccClippingBoxTool::exportMultSlices()
  430. {
  431. extractSlicesAndContours(/*singleSliceMode=*/false);
  432. }
  433. static unsigned ComputeGridDimensions( const ccBBox& localBox,
  434. const bool processDim[3],
  435. int indexMins[3],
  436. int indexMaxs[3],
  437. int gridDim[3],
  438. const CCVector3& gridOrigin,
  439. const CCVector3& cellSizePlusGap)
  440. {
  441. //compute 'grid' extents in the local clipping box ref.
  442. for (int i = 0; i < 3; ++i)
  443. {
  444. indexMins[i] = 0;
  445. indexMaxs[i] = 0;
  446. gridDim[i] = 1;
  447. }
  448. unsigned cellCount = 1;
  449. for (unsigned char d = 0; d < 3; ++d)
  450. {
  451. if (processDim[d])
  452. {
  453. if (CCCoreLib::LessThanEpsilon(cellSizePlusGap.u[d]))
  454. {
  455. ccLog::Error("Box size (plus gap) is null! Can't apply repetitive process!");
  456. return 0;
  457. }
  458. PointCoordinateType a = (localBox.minCorner().u[d] - gridOrigin.u[d]) / cellSizePlusGap.u[d]; //don't forget the user defined gap between 'cells'
  459. PointCoordinateType b = (localBox.maxCorner().u[d] - gridOrigin.u[d]) / cellSizePlusGap.u[d];
  460. indexMins[d] = static_cast<int>(floor(a + static_cast<PointCoordinateType>(1.0e-6)));
  461. indexMaxs[d] = static_cast<int>(ceil(b - static_cast<PointCoordinateType>(1.0e-6))) - 1;
  462. assert(indexMaxs[d] >= indexMins[d]);
  463. gridDim[d] = std::max(indexMaxs[d] - indexMins[d] + 1, 1);
  464. cellCount *= static_cast<unsigned>(gridDim[d]);
  465. }
  466. }
  467. return cellCount;
  468. }
  469. bool ccClippingBoxTool::ExtractSlicesAndContours
  470. (
  471. const std::vector<ccGenericPointCloud*>& clouds,
  472. const std::vector<ccGenericMesh*>& meshes,
  473. ccClipBox& clipBox,
  474. bool singleSliceMode,
  475. bool repeatDimensions[3],
  476. std::vector<ccHObject*>& outputSlices,
  477. bool extractEnvelopes,
  478. PointCoordinateType maxEdgeLength,
  479. ccEnvelopeExtractor::EnvelopeType envelopeType,
  480. std::vector<ccPolyline*>& outputEnvelopes,
  481. bool extractLevelSet,
  482. double levelSetGridStep,
  483. int levelSetMinVertCount,
  484. std::vector<ccPolyline*>& levelSet,
  485. PointCoordinateType gap/*=0*/,
  486. bool multiPass/*=false*/,
  487. bool splitEnvelopes/*=false*/,
  488. bool projectOnBestFitPlane/*=false*/,
  489. bool visualDebugMode/*=false*/,
  490. bool generateRandomColors/*=false*/,
  491. ccProgressDialog* progressDialog/*=nullptr*/)
  492. {
  493. //check input
  494. if (clouds.empty() && meshes.empty())
  495. {
  496. assert(false);
  497. return false;
  498. }
  499. //repeat dimensions
  500. int repeatDimensionsSum = static_cast<int>(repeatDimensions[0])
  501. + static_cast<int>(repeatDimensions[1])
  502. + static_cast<int>(repeatDimensions[2]);
  503. if (!singleSliceMode && repeatDimensionsSum == 0)
  504. {
  505. assert(false);
  506. ccLog::Error("No dimension selected to repeat the segmentation process?!");
  507. return false;
  508. }
  509. if (extractLevelSet && repeatDimensionsSum != 1)
  510. {
  511. assert(false);
  512. ccLog::Error("Only one repeat/flat dimension should be defined for level set extraction");
  513. extractLevelSet = false;
  514. }
  515. //compute the cloud bounding box in the local clipping box ref.
  516. ccGLMatrix localTrans;
  517. {
  518. if (clipBox.isGLTransEnabled())
  519. localTrans = clipBox.getGLTransformation().inverse();
  520. else
  521. localTrans.toIdentity();
  522. }
  523. CCVector3 gridOrigin = clipBox.getOwnBB().minCorner();
  524. CCVector3 cellSize = clipBox.getOwnBB().getDiagVec();
  525. CCVector3 cellSizePlusGap = cellSize + CCVector3(gap, gap, gap);
  526. //apply process
  527. try
  528. {
  529. bool error = false;
  530. bool warningsIssued = false;
  531. size_t cloudSliceCount = 0;
  532. if (singleSliceMode)
  533. {
  534. //single slice: easy
  535. outputSlices.reserve(clouds.size());
  536. for (size_t ci = 0; ci != clouds.size(); ++ci)
  537. {
  538. ccHObject* slice = GetSlice(clouds[ci], &clipBox, false);
  539. if (slice)
  540. {
  541. slice->setName(clouds[ci]->getName() + QString(".slice"));
  542. //set meta-data
  543. slice->setMetaData(s_originEntityUUID, clouds[ci]->getUniqueID());
  544. slice->setMetaData(s_sliceID, "slice");
  545. if (slice->isKindOf(CC_TYPES::POINT_CLOUD))
  546. {
  547. slice->setMetaData("slice.origin.dim(0)", gridOrigin.x);
  548. slice->setMetaData("slice.origin.dim(1)", gridOrigin.y);
  549. slice->setMetaData("slice.origin.dim(2)", gridOrigin.z);
  550. }
  551. outputSlices.push_back(slice);
  552. }
  553. }
  554. if (outputSlices.empty())
  555. {
  556. //error message already issued
  557. return false;
  558. }
  559. cloudSliceCount = outputSlices.size();
  560. }
  561. else //repeat mode
  562. {
  563. if (!clouds.empty()) //extract sections from clouds
  564. {
  565. //compute 'grid' extents in the local clipping box ref.
  566. ccBBox localBox;
  567. for (ccGenericPointCloud* cloud : clouds)
  568. {
  569. for (unsigned i = 0; i < cloud->size(); ++i)
  570. {
  571. CCVector3 P = *cloud->getPoint(i);
  572. localTrans.apply(P);
  573. localBox.add(P);
  574. }
  575. }
  576. int indexMins[3]{ 0, 0, 0 };
  577. int indexMaxs[3]{ 0, 0, 0 };
  578. int gridDim[3]{ 0, 0, 0 };
  579. unsigned cellCount = ComputeGridDimensions(localBox, repeatDimensions, indexMins, indexMaxs, gridDim, gridOrigin, cellSizePlusGap);
  580. //we'll potentially create up to one (ref.) cloud per input loud and per cell
  581. std::vector<CCCoreLib::ReferenceCloud*> refClouds;
  582. refClouds.resize(cellCount * clouds.size(), nullptr);
  583. if (progressDialog)
  584. {
  585. progressDialog->setWindowTitle(tr("Preparing extraction"));
  586. progressDialog->start();
  587. progressDialog->show();
  588. progressDialog->setAutoClose(false);
  589. }
  590. unsigned subCloudsCount = 0;
  591. //project points into grid
  592. for (size_t ci = 0; ci != clouds.size(); ++ci)
  593. {
  594. ccGenericPointCloud* cloud = clouds[ci];
  595. unsigned pointCount = cloud->size();
  596. QString infos = tr("Cloud '%1").arg(cloud->getName());
  597. infos += tr("Points: %L1").arg( pointCount );
  598. if (progressDialog)
  599. {
  600. progressDialog->setInfo(infos);
  601. }
  602. QApplication::processEvents();
  603. CCCoreLib::NormalizedProgress nProgress(progressDialog, pointCount);
  604. for (unsigned i = 0; i < pointCount; ++i)
  605. {
  606. CCVector3 P = *cloud->getPoint(i);
  607. localTrans.apply(P);
  608. //relative coordinates (between 0 and 1)
  609. P -= gridOrigin;
  610. P.x /= cellSizePlusGap.x;
  611. P.y /= cellSizePlusGap.y;
  612. P.z /= cellSizePlusGap.z;
  613. int xi = static_cast<int>(floor(P.x));
  614. xi = std::min(std::max(xi, indexMins[0]), indexMaxs[0]);
  615. int yi = static_cast<int>(floor(P.y));
  616. yi = std::min(std::max(yi, indexMins[1]), indexMaxs[1]);
  617. int zi = static_cast<int>(floor(P.z));
  618. zi = std::min(std::max(zi, indexMins[2]), indexMaxs[2]);
  619. if (gap == 0 ||
  620. ( (P.x - static_cast<PointCoordinateType>(xi))*cellSizePlusGap.x <= cellSize.x
  621. && (P.y - static_cast<PointCoordinateType>(yi))*cellSizePlusGap.y <= cellSize.y
  622. && (P.z - static_cast<PointCoordinateType>(zi))*cellSizePlusGap.z <= cellSize.z))
  623. {
  624. int cloudIndex = ((zi - indexMins[2]) * static_cast<int>(gridDim[1]) + (yi - indexMins[1])) * static_cast<int>(gridDim[0]) + (xi - indexMins[0]);
  625. assert(cloudIndex >= 0 && static_cast<size_t>(cloudIndex)* clouds.size() + ci < refClouds.size());
  626. CCCoreLib::ReferenceCloud*& destCloud = refClouds[cloudIndex * clouds.size() + ci];
  627. if (!destCloud)
  628. {
  629. destCloud = new CCCoreLib::ReferenceCloud(cloud);
  630. ++subCloudsCount;
  631. }
  632. if (!destCloud->addPointIndex(i))
  633. {
  634. ccLog::Error("Not enough memory!");
  635. error = true;
  636. break;
  637. }
  638. }
  639. }
  640. if (!nProgress.oneStep())
  641. {
  642. // sadly, we can't cancel in the middle of this process!
  643. }
  644. } //project points into grid
  645. if (progressDialog)
  646. {
  647. progressDialog->setWindowTitle(QObject::tr("Section extraction"));
  648. progressDialog->setInfo(QObject::tr("Section(s): %L1").arg(subCloudsCount));
  649. progressDialog->setMaximum(static_cast<int>(subCloudsCount));
  650. progressDialog->setValue(0);
  651. QApplication::processEvents();
  652. }
  653. //reset count
  654. subCloudsCount = 0;
  655. //now create the real clouds
  656. for (int i = indexMins[0]; i <= indexMaxs[0]; ++i)
  657. {
  658. for (int j = indexMins[1]; j <= indexMaxs[1]; ++j)
  659. {
  660. for (int k = indexMins[2]; k <= indexMaxs[2]; ++k)
  661. {
  662. int cloudIndex = ((k - indexMins[2]) * static_cast<int>(gridDim[1]) + (j - indexMins[1])) * static_cast<int>(gridDim[0]) + (i - indexMins[0]);
  663. assert(cloudIndex >= 0 && static_cast<size_t>(cloudIndex)* clouds.size() < refClouds.size());
  664. for (size_t ci = 0; ci != clouds.size(); ++ci)
  665. {
  666. ccGenericPointCloud* cloud = clouds[ci];
  667. CCCoreLib::ReferenceCloud* destCloud = refClouds[cloudIndex * clouds.size() + ci];
  668. if (destCloud) //some slices can be empty!
  669. {
  670. //generate slice from previous selection
  671. int warnings = 0;
  672. ccPointCloud* sliceCloud = cloud->isA(CC_TYPES::POINT_CLOUD) ? static_cast<ccPointCloud*>(cloud)->partialClone(destCloud, &warnings) : ccPointCloud::From(destCloud, cloud);
  673. warningsIssued |= (warnings != 0);
  674. if (sliceCloud)
  675. {
  676. if (generateRandomColors)
  677. {
  678. ccColor::Rgb col = ccColor::Generator::Random();
  679. if (!sliceCloud->setColor(col))
  680. {
  681. ccLog::Error("Not enough memory!");
  682. error = true;
  683. i = indexMaxs[0];
  684. j = indexMaxs[1];
  685. k = indexMaxs[2];
  686. }
  687. sliceCloud->showColors(true);
  688. }
  689. sliceCloud->setEnabled(true);
  690. sliceCloud->setVisible(true);
  691. sliceCloud->setDisplay(cloud->getDisplay());
  692. CCVector3 cellOrigin( gridOrigin.x + i * cellSizePlusGap.x,
  693. gridOrigin.y + j * cellSizePlusGap.y,
  694. gridOrigin.z + k * cellSizePlusGap.z);
  695. QString slicePosStr = QString("(%1 ; %2 ; %3)").arg(cellOrigin.x).arg(cellOrigin.y).arg(cellOrigin.z);
  696. sliceCloud->setName(cloud->getName() + QString(".slice @ ") + slicePosStr);
  697. //set meta-data
  698. sliceCloud->setMetaData(s_originEntityUUID, cloud->getUniqueID());
  699. sliceCloud->setMetaData(s_sliceID, slicePosStr);
  700. sliceCloud->setMetaData("slice.origin.dim(0)", cellOrigin.x);
  701. sliceCloud->setMetaData("slice.origin.dim(1)", cellOrigin.y);
  702. sliceCloud->setMetaData("slice.origin.dim(2)", cellOrigin.z);
  703. //add slice to group
  704. outputSlices.push_back(sliceCloud);
  705. ++subCloudsCount;
  706. if (progressDialog)
  707. {
  708. progressDialog->setValue(static_cast<int>(subCloudsCount));
  709. }
  710. }
  711. if (progressDialog && progressDialog->wasCanceled())
  712. {
  713. error = true;
  714. ccLog::Warning(QString("[ExtractSlicesAndContours] Process canceled by user"));
  715. //early stop
  716. i = indexMaxs[0];
  717. j = indexMaxs[1];
  718. k = indexMaxs[2];
  719. break;
  720. }
  721. }
  722. }
  723. }
  724. }
  725. } //now create the real clouds
  726. //release memory
  727. {
  728. for (size_t i = 0; i < refClouds.size(); ++i)
  729. if (refClouds[i])
  730. delete refClouds[i];
  731. refClouds.clear();
  732. }
  733. cloudSliceCount = outputSlices.size();
  734. } //extract sections from clouds
  735. if (!meshes.empty()) //extract sections from meshes
  736. {
  737. //compute 'grid' extents in the local clipping box ref.
  738. ccBBox localBox;
  739. for (ccGenericMesh* mesh : meshes)
  740. {
  741. ccGenericPointCloud* cloud = mesh->getAssociatedCloud();
  742. for (unsigned i = 0; i < cloud->size(); ++i)
  743. {
  744. CCVector3 P = *cloud->getPoint(i);
  745. localTrans.apply(P);
  746. localBox.add(P);
  747. }
  748. }
  749. int indexMins[3]{ 0, 0, 0 };
  750. int indexMaxs[3]{ 0, 0, 0 };
  751. int gridDim[3]{ 0, 0, 0 };
  752. unsigned cellCount = ComputeGridDimensions(localBox, repeatDimensions, indexMins, indexMaxs, gridDim, gridOrigin, cellSizePlusGap);
  753. const ccGLMatrix* _transformation = nullptr;
  754. ccGLMatrix transformation;
  755. if (clipBox.isGLTransEnabled())
  756. {
  757. transformation = clipBox.getGLTransformation().inverse();
  758. _transformation = &transformation;
  759. }
  760. if (progressDialog)
  761. {
  762. progressDialog->setWindowTitle("Section extraction");
  763. progressDialog->setInfo(QObject::tr("Up to (%1 x %2 x %3) = %4 section(s)").arg(gridDim[0]).arg(gridDim[1]).arg(gridDim[2]).arg(cellCount));
  764. progressDialog->setMaximum(static_cast<int>(cellCount * meshes.size()));
  765. progressDialog->show();
  766. QApplication::processEvents();
  767. }
  768. //now extract the slices
  769. for (int i = indexMins[0]; i <= indexMaxs[0]; ++i)
  770. {
  771. for (int j = indexMins[1]; j <= indexMaxs[1]; ++j)
  772. {
  773. for (int k = indexMins[2]; k <= indexMaxs[2]; ++k)
  774. {
  775. int sliceIndex = ((k - indexMins[2]) * static_cast<int>(gridDim[1]) + (j - indexMins[1])) * static_cast<int>(gridDim[0]) + (i - indexMins[0]);
  776. CCVector3 C = gridOrigin + CCVector3(i*cellSizePlusGap.x, j*cellSizePlusGap.y, k*cellSizePlusGap.z);
  777. ccBBox cropBox(C, C + cellSize, true);
  778. for (size_t mi = 0; mi != meshes.size(); ++mi)
  779. {
  780. ccGenericMesh* mesh = meshes[mi];
  781. ccHObject* croppedEnt = ccCropTool::Crop(mesh, cropBox, true, _transformation);
  782. if (croppedEnt)
  783. {
  784. if (generateRandomColors)
  785. {
  786. ccPointCloud* croppedVertices = ccHObjectCaster::ToPointCloud(mesh->getAssociatedCloud());
  787. if (croppedVertices)
  788. {
  789. ccColor::Rgb col = ccColor::Generator::Random();
  790. if (!croppedVertices->setColor(col))
  791. {
  792. ccLog::Error("Not enough memory!");
  793. error = true;
  794. i = indexMaxs[0];
  795. j = indexMaxs[1];
  796. k = indexMaxs[2];
  797. }
  798. croppedVertices->showColors(true);
  799. mesh->showColors(true);
  800. }
  801. }
  802. croppedEnt->setEnabled(true);
  803. croppedEnt->setVisible(true);
  804. croppedEnt->setDisplay(mesh->getDisplay());
  805. QString slicePosStr = QString("(%1 ; %2 ; %3)").arg(C.x).arg(C.y).arg(C.z);
  806. croppedEnt->setName(mesh->getName() + QString(".slice @ ") + slicePosStr);
  807. //set meta-data
  808. croppedEnt->setMetaData(s_originEntityUUID, mesh->getUniqueID());
  809. croppedEnt->setMetaData(s_sliceID, slicePosStr);
  810. croppedEnt->setMetaData("slice.origin.dim(0)", C.x);
  811. croppedEnt->setMetaData("slice.origin.dim(1)", C.y);
  812. croppedEnt->setMetaData("slice.origin.dim(2)", C.z);
  813. //add slice to group
  814. outputSlices.push_back(croppedEnt);
  815. }
  816. if (progressDialog)
  817. {
  818. if (progressDialog->wasCanceled())
  819. {
  820. error = true;
  821. ccLog::Warning(QString("[ExtractSlicesAndContours] Process canceled by user"));
  822. //early stop
  823. i = indexMaxs[0];
  824. j = indexMaxs[1];
  825. k = indexMaxs[2];
  826. break;
  827. }
  828. progressDialog->setValue(sliceIndex * static_cast<int>(meshes.size()) + static_cast<int>(mi));
  829. }
  830. }
  831. }
  832. }
  833. }
  834. } //extract sections from meshes
  835. } //repeat mode
  836. //extract level set (optionaly)
  837. if (!error && extractLevelSet && cloudSliceCount != 0)
  838. {
  839. for (int iteration = 0; iteration < 1; ++iteration) //fake loop for easy break
  840. {
  841. if (progressDialog)
  842. {
  843. progressDialog->setWindowTitle("Level set extraction");
  844. progressDialog->setInfo(QObject::tr("Level(s): %L1").arg(cloudSliceCount));
  845. progressDialog->setMaximum(static_cast<int>(cloudSliceCount));
  846. progressDialog->show();
  847. QApplication::processEvents();
  848. }
  849. int Z = 2;
  850. assert(repeatDimensionsSum == 1);
  851. {
  852. for (int i = 0; i < 3; ++i)
  853. {
  854. if (repeatDimensions[i])
  855. {
  856. Z = i;
  857. break;
  858. }
  859. }
  860. }
  861. int X = (Z == 2 ? 0 : Z + 1);
  862. int Y = (X == 2 ? 0 : X + 1);
  863. CCVector3 gridOrigin = clipBox.getOwnBB().minCorner();
  864. CCVector3 gridSize = clipBox.getOwnBB().getDiagVec();
  865. ccGLMatrix globalTrans = localTrans.inverse();
  866. assert(false == CCCoreLib::LessThanEpsilon(levelSetGridStep));
  867. unsigned gridWidth = 1 + static_cast<unsigned>(gridSize.u[X] / levelSetGridStep + 0.5);
  868. unsigned gridHeight = 1 + static_cast<unsigned>(gridSize.u[Y] / levelSetGridStep + 0.5);
  869. //add a margin to avoid issues in the level set generation
  870. gridWidth += 2;
  871. gridHeight += 2;
  872. gridOrigin.u[X] -= levelSetGridStep;
  873. gridOrigin.u[Y] -= levelSetGridStep;
  874. ccRasterGrid grid;
  875. if (!grid.init(gridWidth, gridHeight, levelSetGridStep, CCVector3d(0, 0, 0)))
  876. {
  877. ccLog::Error("Not enough memory!");
  878. error = true;
  879. break;
  880. }
  881. //process all the slices originating from point clouds
  882. assert(cloudSliceCount <= outputSlices.size());
  883. for (size_t i = 0; i < cloudSliceCount; ++i)
  884. {
  885. ccPointCloud* sliceCloud = ccHObjectCaster::ToPointCloud(outputSlices[i]);
  886. assert(sliceCloud);
  887. double sliceZ = sliceCloud->getMetaData(QString("slice.origin.dim(%1)").arg(Z)).toDouble();
  888. sliceZ += gridSize.u[Z] / 2;
  889. //grid.reset();
  890. for (ccRasterGrid::Row& row : grid.rows)
  891. {
  892. for (ccRasterCell& cell : row)
  893. {
  894. cell.h = 0.0;
  895. cell.nbPoints = 0;
  896. }
  897. }
  898. //project the slice in 2D
  899. for (unsigned pi = 0; pi != sliceCloud->size(); ++pi)
  900. {
  901. CCVector3 relativePos = *sliceCloud->getPoint(pi);
  902. localTrans.apply(relativePos);
  903. relativePos -= gridOrigin;
  904. int i = static_cast<int>(relativePos.u[X] / levelSetGridStep + 0.5);
  905. int j = static_cast<int>(relativePos.u[Y] / levelSetGridStep + 0.5);
  906. //we skip points that fall outside of the grid!
  907. if ( i < 0 || i >= static_cast<int>(gridWidth)
  908. || j < 0 || j >= static_cast<int>(gridHeight))
  909. {
  910. //there shouldn't be any actually
  911. assert(false);
  912. continue;
  913. }
  914. ccRasterCell& cell = grid.rows[j][i];
  915. cell.h = 1.0;
  916. ++cell.nbPoints;
  917. }
  918. grid.updateNonEmptyCellCount();
  919. grid.updateCellStats();
  920. grid.setValid(true);
  921. //now extract the contour lines
  922. ccContourLinesGenerator::Parameters params;
  923. params.emptyCellsValue = std::numeric_limits<double>::quiet_NaN();
  924. params.minVertexCount = levelSetMinVertCount;
  925. params.parentWidget = progressDialog->parentWidget();
  926. params.startAltitude = 0.0;
  927. params.maxAltitude = 1.0;
  928. params.step = 1.0;
  929. std::vector<ccPolyline*> contours;
  930. if (ccContourLinesGenerator::GenerateContourLines(&grid, CCVector2d(gridOrigin.u[X], gridOrigin.u[Y]), params, contours))
  931. {
  932. for (size_t k = 0; k < contours.size(); ++k)
  933. {
  934. ccPolyline* poly = contours[k];
  935. CCCoreLib::GenericIndexedCloudPersist* vertices = poly->getAssociatedCloud();
  936. for (unsigned pi = 0; pi < vertices->size(); ++pi)
  937. {
  938. //convert the vertices from the local coordinate system to the global one
  939. const CCVector3* Pconst = vertices->getPoint(pi);
  940. CCVector3 P;
  941. P.u[X] = Pconst->x;
  942. P.u[Y] = Pconst->y;
  943. P.u[Z] = sliceZ;
  944. *const_cast<CCVector3*>(Pconst) = globalTrans * P;
  945. }
  946. static char s_dimNames[3] = { 'X', 'Y', 'Z' };
  947. poly->setName(QString("Contour line %1=%2 (#%3)").arg(s_dimNames[Z]).arg(sliceZ).arg(k + 1));
  948. poly->copyGlobalShiftAndScale(*sliceCloud);
  949. poly->setMetaData(ccPolyline::MetaKeyConstAltitude(), QVariant(sliceZ)); //replace the 'altitude' meta-data by the right value
  950. //set meta-data
  951. poly->setMetaData(s_originEntityUUID, sliceCloud->getMetaData(s_originEntityUUID));
  952. poly->setMetaData(s_sliceID, sliceCloud->getMetaData(s_sliceID));
  953. poly->setMetaData("slice.origin.dim(0)", sliceCloud->getMetaData("slice.origin.dim(0)"));
  954. poly->setMetaData("slice.origin.dim(1)", sliceCloud->getMetaData("slice.origin.dim(1)"));
  955. poly->setMetaData("slice.origin.dim(2)", sliceCloud->getMetaData("slice.origin.dim(2)"));
  956. levelSet.push_back(poly);
  957. }
  958. }
  959. else
  960. {
  961. ccLog::Warning(tr("Failed to generate contour lines for cloud #%1").arg(i + 1));
  962. }
  963. if (progressDialog)
  964. {
  965. if (progressDialog->wasCanceled())
  966. {
  967. error = true;
  968. ccLog::Warning(tr("[ExtractSlicesAndContours] Process canceled by user"));
  969. break;
  970. }
  971. progressDialog->setValue(static_cast<int>(i) + 1);
  972. }
  973. }
  974. }
  975. }
  976. //extract envelopes as polylines (optionaly)
  977. if (!error && extractEnvelopes && cloudSliceCount != 0)
  978. {
  979. if (progressDialog)
  980. {
  981. progressDialog->setWindowTitle(tr("Envelope extraction"));
  982. progressDialog->setInfo(tr("Envelope(s): %L1").arg(cloudSliceCount));
  983. progressDialog->setMaximum(static_cast<int>(cloudSliceCount));
  984. if (!visualDebugMode)
  985. {
  986. progressDialog->show();
  987. QApplication::processEvents();
  988. }
  989. }
  990. //preferred dimension?
  991. PointCoordinateType* preferredNormDir = nullptr;
  992. PointCoordinateType* preferredUpDir = nullptr;
  993. if (repeatDimensionsSum == 1)
  994. {
  995. for (int i = 0; i < 3; ++i)
  996. {
  997. if (repeatDimensions[i])
  998. {
  999. ccGLMatrix invLocalTrans = localTrans.inverse();
  1000. if (!projectOnBestFitPlane) //otherwise the normal will be automatically computed
  1001. preferredNormDir = invLocalTrans.getColumn(i);
  1002. preferredUpDir = invLocalTrans.getColumn(i < 2 ? 2 : 0);
  1003. break;
  1004. }
  1005. }
  1006. }
  1007. assert(cloudSliceCount <= outputSlices.size());
  1008. //process all the slices originating from point clouds
  1009. for (size_t i = 0; i < cloudSliceCount; ++i)
  1010. {
  1011. ccPointCloud* sliceCloud = ccHObjectCaster::ToPointCloud(outputSlices[i]);
  1012. assert(sliceCloud);
  1013. std::vector<ccPolyline*> polys;
  1014. if (ccEnvelopeExtractor::ExtractFlatEnvelope(sliceCloud,
  1015. multiPass,
  1016. maxEdgeLength,
  1017. polys,
  1018. envelopeType,
  1019. splitEnvelopes,
  1020. preferredNormDir,
  1021. preferredUpDir,
  1022. visualDebugMode))
  1023. {
  1024. if (!polys.empty())
  1025. {
  1026. for (size_t p = 0; p < polys.size(); ++p)
  1027. {
  1028. ccPolyline* poly = polys[p];
  1029. poly->setColor(ccColor::green);
  1030. poly->showColors(true);
  1031. poly->setGlobalScale(sliceCloud->getGlobalScale());
  1032. poly->setGlobalShift(sliceCloud->getGlobalShift());
  1033. QString envelopeName = sliceCloud->getName();
  1034. envelopeName.replace("slice", "envelope");
  1035. if (polys.size() > 1)
  1036. {
  1037. envelopeName += QString(" (part %1)").arg(p + 1);
  1038. }
  1039. poly->setName(envelopeName);
  1040. //set meta-data
  1041. poly->setMetaData(s_originEntityUUID, sliceCloud->getMetaData(s_originEntityUUID));
  1042. poly->setMetaData(s_sliceID, sliceCloud->getMetaData(s_sliceID));
  1043. poly->setMetaData("slice.origin.dim(0)", sliceCloud->getMetaData("slice.origin.dim(0)"));
  1044. poly->setMetaData("slice.origin.dim(1)", sliceCloud->getMetaData("slice.origin.dim(1)"));
  1045. poly->setMetaData("slice.origin.dim(2)", sliceCloud->getMetaData("slice.origin.dim(2)"));
  1046. outputEnvelopes.push_back(poly);
  1047. }
  1048. }
  1049. else
  1050. {
  1051. ccLog::Warning(tr("%1: points are too far from each other! Increase the max edge length").arg(sliceCloud->getName()));
  1052. warningsIssued = true;
  1053. }
  1054. }
  1055. else
  1056. {
  1057. ccLog::Warning(tr("%1: envelope extraction failed!").arg(sliceCloud->getName()));
  1058. warningsIssued = true;
  1059. }
  1060. if (progressDialog && !visualDebugMode)
  1061. {
  1062. if (progressDialog->wasCanceled())
  1063. {
  1064. error = true;
  1065. ccLog::Warning(tr("[ExtractSlicesAndContours] Process canceled by user"));
  1066. //early stop
  1067. break;
  1068. }
  1069. progressDialog->setValue(static_cast<int>(i));
  1070. }
  1071. }
  1072. } //extract envelope polylines
  1073. //release memory
  1074. if (error)
  1075. {
  1076. for (ccHObject* slice : outputSlices)
  1077. {
  1078. delete slice;
  1079. }
  1080. outputSlices.resize(0);
  1081. for (ccPolyline* poly : outputEnvelopes)
  1082. {
  1083. delete poly;
  1084. }
  1085. return false;
  1086. }
  1087. else if (warningsIssued)
  1088. {
  1089. ccLog::Warning(tr("[ExtractSlicesAndContours] Warnings were issued during the process! (result may be incomplete)"));
  1090. }
  1091. }
  1092. catch (const std::bad_alloc&)
  1093. {
  1094. ccLog::Error(tr("Not enough memory!"));
  1095. return false;
  1096. }
  1097. return true;
  1098. }
  1099. static ccHObject* FindOrCreateChildren(ccHObject* parent, QString name)
  1100. {
  1101. assert(parent && !name.isEmpty());
  1102. for (unsigned i = 0; i < parent->getChildrenNumber(); ++i)
  1103. {
  1104. ccHObject* child = parent->getChild(i);
  1105. if (child->getName() == name)
  1106. {
  1107. return child;
  1108. }
  1109. }
  1110. ccHObject* child = new ccHObject(name);
  1111. parent->addChild(child);
  1112. return child;
  1113. }
  1114. void ccClippingBoxTool::extractSlicesAndContours(bool singleSliceMode)
  1115. {
  1116. if (!m_clipBox || m_clipBox->getContainer().getChildrenNumber() == 0)
  1117. {
  1118. assert(false);
  1119. return;
  1120. }
  1121. std::vector<ccGenericPointCloud*> clouds;
  1122. std::vector<ccGenericMesh*> meshes;
  1123. try
  1124. {
  1125. for (unsigned ci = 0; ci != m_clipBox->getContainer().getChildrenNumber(); ++ci)
  1126. {
  1127. ccHObject* obj = m_clipBox->getContainer().getChild(ci);
  1128. if (obj->isKindOf(CC_TYPES::POINT_CLOUD))
  1129. {
  1130. ccGenericPointCloud* cloud = ccHObjectCaster::ToGenericPointCloud(obj);
  1131. clouds.push_back(cloud);
  1132. }
  1133. else if (obj->isKindOf(CC_TYPES::MESH))
  1134. {
  1135. ccGenericMesh* mesh = ccHObjectCaster::ToGenericMesh(obj);
  1136. meshes.push_back(mesh);
  1137. }
  1138. }
  1139. }
  1140. catch (const std::bad_alloc&)
  1141. {
  1142. ccLog::Warning("Not enough memory");
  1143. return;
  1144. }
  1145. if (clouds.empty() && meshes.empty())
  1146. {
  1147. ccLog::Warning("Only works with point clouds or meshes!");
  1148. return;
  1149. }
  1150. ccClippingBoxRepeatDlg repeatDlg(singleSliceMode, MainWindow::TheInstance());
  1151. repeatDlg.extractEnvelopesGroupBox->setEnabled(!clouds.empty());
  1152. repeatDlg.extractLevelSetGroupBox->setEnabled(!clouds.empty());
  1153. //by default we set the 'flat/repeat' dimension to the smallest box dimension
  1154. {
  1155. CCVector3 diagVec = m_clipBox->getOwnBB().getDiagVec();
  1156. unsigned char flatDim = 0;
  1157. if (diagVec.y < diagVec.x)
  1158. flatDim = 1;
  1159. if (diagVec.z < diagVec.u[flatDim])
  1160. flatDim = 2;
  1161. if (singleSliceMode)
  1162. repeatDlg.setFlatDim(flatDim);
  1163. else
  1164. repeatDlg.setRepeatDim(flatDim);
  1165. }
  1166. //random colors is only useful for mutliple slices mode
  1167. repeatDlg.randomColorCheckBox->setEnabled(!singleSliceMode);
  1168. //Semi persistent parameters
  1169. static bool s_extractSliceCloudsOrMeshes = true;
  1170. static bool s_generateRandomColors = false;
  1171. static bool s_extractEnvelopes = false;
  1172. static int s_envelopeTypeIndex = 2; //full
  1173. static bool s_multiPassEnvelope = false;
  1174. static bool s_envProjectPointsOnBestFitPlane = false;
  1175. static bool s_splitEnvelopes = false;
  1176. static bool s_envelopeDebugMode = false;
  1177. static bool s_extractLevelSet = false;
  1178. static double s_levelSetGridStep = 0.0;
  1179. static int s_levelSetMinVertCount = 3;
  1180. static double s_defaultGap = 0.0;
  1181. static int s_groupByIndex = 0;
  1182. //set default max edge length
  1183. if (s_maxEnvelopeEdgeLength < 0)
  1184. s_maxEnvelopeEdgeLength = m_clipBox->getBox().getDiagNorm() / 100.0;
  1185. if (s_levelSetGridStep <= 0.0)
  1186. s_levelSetGridStep = m_clipBox->getBox().getDiagNorm() / 300.0;
  1187. //Extract slice cloud(s) or mesh(es)
  1188. repeatDlg.extractSliceEntitiesGroupBox->setChecked(s_extractSliceCloudsOrMeshes);
  1189. repeatDlg.randomColorCheckBox->setChecked(s_generateRandomColors);
  1190. //Extract envelope(s)
  1191. repeatDlg.extractEnvelopesGroupBox->setChecked(s_extractEnvelopes);
  1192. repeatDlg.envelopeTypeComboBox->setCurrentIndex(s_envelopeTypeIndex);
  1193. repeatDlg.maxEnvelopeEdgeLengthDoubleSpinBox->setValue(s_maxEnvelopeEdgeLength);
  1194. repeatDlg.multiPassEnvelopeCheckBox->setChecked(s_multiPassEnvelope);
  1195. repeatDlg.envProjectPointsOnBestFitCheckBox->setChecked(s_envProjectPointsOnBestFitPlane);
  1196. repeatDlg.splitEnvelopeCheckBox->setChecked(s_splitEnvelopes);
  1197. repeatDlg.envDebugModeCheckBox->setChecked(s_envelopeDebugMode);
  1198. //Extract contours
  1199. repeatDlg.extractLevelSetGroupBox->setChecked(s_extractLevelSet);
  1200. repeatDlg.lsGridStepDoubleSpinBox->setValue(s_levelSetGridStep);
  1201. repeatDlg.minLSVertexCountSpinBox->setValue(s_levelSetMinVertCount);
  1202. //Other options
  1203. repeatDlg.gapDoubleSpinBox->setValue(s_defaultGap);
  1204. repeatDlg.groupByTypeComboBox->setCurrentIndex(s_groupByIndex);
  1205. if (!repeatDlg.exec())
  1206. {
  1207. //cancelled by user
  1208. return;
  1209. }
  1210. //repeat dimensions
  1211. bool processDim[3] = { repeatDlg.xRepeatCheckBox->isChecked(),
  1212. repeatDlg.yRepeatCheckBox->isChecked(),
  1213. repeatDlg.zRepeatCheckBox->isChecked() };
  1214. //whether to use random colors for (multiple) generated slices
  1215. s_extractSliceCloudsOrMeshes = repeatDlg.extractSliceEntitiesGroupBox->isChecked();
  1216. s_generateRandomColors = repeatDlg.randomColorCheckBox->isChecked();
  1217. s_extractEnvelopes = repeatDlg.extractEnvelopesGroupBox->isChecked();
  1218. s_envelopeTypeIndex = repeatDlg.envelopeTypeComboBox->currentIndex();
  1219. s_maxEnvelopeEdgeLength = repeatDlg.maxEnvelopeEdgeLengthDoubleSpinBox->value();
  1220. s_multiPassEnvelope = repeatDlg.multiPassEnvelopeCheckBox->isChecked();
  1221. s_envProjectPointsOnBestFitPlane = repeatDlg.envProjectPointsOnBestFitCheckBox->isChecked();
  1222. s_splitEnvelopes = repeatDlg.splitEnvelopeCheckBox->isChecked();
  1223. s_envelopeDebugMode = repeatDlg.envDebugModeCheckBox->isChecked();
  1224. s_extractLevelSet = repeatDlg.extractLevelSetGroupBox->isChecked();
  1225. s_levelSetGridStep = repeatDlg.lsGridStepDoubleSpinBox->value();
  1226. s_levelSetMinVertCount = repeatDlg.minLSVertexCountSpinBox->value();
  1227. s_defaultGap = repeatDlg.gapDoubleSpinBox->value();
  1228. s_groupByIndex = repeatDlg.groupByTypeComboBox->currentIndex();
  1229. ccEnvelopeExtractor::EnvelopeType envelopeType = ccEnvelopeExtractor::EnvelopeType::FULL;
  1230. switch (s_envelopeTypeIndex)
  1231. {
  1232. case 0:
  1233. envelopeType = ccEnvelopeExtractor::EnvelopeType::LOWER;
  1234. break;
  1235. case 1:
  1236. envelopeType = ccEnvelopeExtractor::EnvelopeType::UPPER;
  1237. break;
  1238. case 2:
  1239. envelopeType = ccEnvelopeExtractor::EnvelopeType::FULL;
  1240. break;
  1241. default:
  1242. assert(false);
  1243. ccLog::Warning("Internal error: unhandled envelope type");
  1244. break;
  1245. }
  1246. ccProgressDialog pDlg(false, this);
  1247. std::vector<ccHObject*> outputSlices;
  1248. std::vector<ccPolyline*> outputEnvelopes;
  1249. std::vector<ccPolyline*> outputLevelSet;
  1250. QElapsedTimer eTimer;
  1251. eTimer.start();
  1252. if (!ExtractSlicesAndContours( clouds,
  1253. meshes,
  1254. *m_clipBox,
  1255. singleSliceMode,
  1256. processDim,
  1257. outputSlices,
  1258. s_extractEnvelopes,
  1259. static_cast<PointCoordinateType>(s_maxEnvelopeEdgeLength),
  1260. envelopeType,
  1261. outputEnvelopes,
  1262. s_extractLevelSet,
  1263. s_levelSetGridStep,
  1264. s_levelSetMinVertCount,
  1265. outputLevelSet,
  1266. static_cast<PointCoordinateType>(s_defaultGap),
  1267. s_multiPassEnvelope,
  1268. s_splitEnvelopes,
  1269. s_envProjectPointsOnBestFitPlane,
  1270. s_envelopeDebugMode,
  1271. s_generateRandomColors,
  1272. &pDlg
  1273. ))
  1274. {
  1275. //process failed (error message has already been issued)
  1276. return;
  1277. }
  1278. ccLog::Print("[ccClippingBoxTool] Processed finished in %.2f s.", eTimer.elapsed() / 1.0e3);
  1279. //possible outputs
  1280. ccHObject* sliceGroup = nullptr;
  1281. ccHObject* envelopeGroup = nullptr;
  1282. ccHObject* levelSetGroup = nullptr;
  1283. QMap<QString, ccHObject*> perSliceGroups;
  1284. QMap<unsigned, ccHObject*> perEntityGroups;
  1285. QMap<unsigned, QString> perEntityGroupNames;
  1286. ccHObject* garbageGroup = new ccHObject("Extracted entities");
  1287. enum OutputFormat { BY_TYPE = 0,
  1288. BY_ENTITY = 1,
  1289. BY_SLICE = 2,
  1290. BY_ENTITY_THEN_SLICE = 3,
  1291. BY_SLICE_THEN_ENTITY = 4,
  1292. };
  1293. if ( s_groupByIndex == OutputFormat::BY_ENTITY
  1294. || s_groupByIndex == OutputFormat::BY_ENTITY_THEN_SLICE
  1295. || s_groupByIndex == OutputFormat::BY_SLICE_THEN_ENTITY
  1296. )
  1297. {
  1298. for (const ccGenericPointCloud* cloud : clouds)
  1299. {
  1300. QString name = cloud->getName() + ".slices";
  1301. perEntityGroupNames[cloud->getUniqueID()] = name;
  1302. if (s_groupByIndex != OutputFormat::BY_SLICE_THEN_ENTITY)
  1303. {
  1304. perEntityGroups[cloud->getUniqueID()] = new ccHObject(name);
  1305. }
  1306. }
  1307. for (const ccGenericMesh* mesh : meshes)
  1308. {
  1309. QString name = mesh->getName() + ".slices";
  1310. perEntityGroupNames[mesh->getUniqueID()] = name;
  1311. if (s_groupByIndex != OutputFormat::BY_SLICE_THEN_ENTITY)
  1312. {
  1313. perEntityGroups[mesh->getUniqueID()] = new ccHObject(name);
  1314. }
  1315. }
  1316. }
  1317. if (s_groupByIndex == OutputFormat::BY_SLICE)
  1318. {
  1319. for (const ccHObject* slice : outputSlices)
  1320. {
  1321. QString sliceID = slice->getMetaData(s_sliceID).toString();
  1322. perSliceGroups[sliceID] = new ccHObject(sliceID);
  1323. }
  1324. }
  1325. auto dispatchEntity = [&](ccHObject* entity, ccHObject* sliceGroup)
  1326. {
  1327. if (entity == nullptr)
  1328. {
  1329. assert(false);
  1330. return;
  1331. }
  1332. ccHObject* destGroup = nullptr;
  1333. switch (s_groupByIndex)
  1334. {
  1335. case OutputFormat::BY_TYPE:
  1336. {
  1337. destGroup = sliceGroup;
  1338. }
  1339. break;
  1340. case OutputFormat::BY_ENTITY:
  1341. case OutputFormat::BY_ENTITY_THEN_SLICE:
  1342. {
  1343. //first level: entity
  1344. unsigned entityUUID = entity->getMetaData(s_originEntityUUID).toUInt();
  1345. if (perEntityGroups.contains(entityUUID))
  1346. {
  1347. destGroup = perEntityGroups[entityUUID];
  1348. }
  1349. else
  1350. {
  1351. assert(false);
  1352. destGroup = garbageGroup;
  1353. }
  1354. if (s_groupByIndex == OutputFormat::BY_ENTITY_THEN_SLICE)
  1355. {
  1356. //second level: slice
  1357. QString sliceID = entity->getMetaData(s_sliceID).toString();
  1358. destGroup = FindOrCreateChildren(destGroup, sliceID);
  1359. }
  1360. destGroup->addChild(entity);
  1361. }
  1362. break;
  1363. case OutputFormat::BY_SLICE:
  1364. case OutputFormat::BY_SLICE_THEN_ENTITY:
  1365. {
  1366. //first level: slice
  1367. QString sliceID = entity->getMetaData(s_sliceID).toString();
  1368. if (perSliceGroups.contains(sliceID))
  1369. {
  1370. destGroup = perSliceGroups[sliceID];
  1371. }
  1372. else
  1373. {
  1374. assert(false);
  1375. destGroup = garbageGroup;
  1376. }
  1377. if (s_groupByIndex == OutputFormat::BY_SLICE_THEN_ENTITY)
  1378. {
  1379. //second level: entity
  1380. unsigned entityUUID = entity->getMetaData(s_originEntityUUID).toUInt();
  1381. if (perEntityGroupNames.contains(entityUUID))
  1382. {
  1383. destGroup = FindOrCreateChildren(destGroup, perEntityGroupNames[entityUUID]);
  1384. }
  1385. }
  1386. }
  1387. break;
  1388. default:
  1389. assert(false);
  1390. return;
  1391. }
  1392. if (destGroup)
  1393. {
  1394. destGroup->addChild(entity);
  1395. }
  1396. else
  1397. {
  1398. assert(false);
  1399. }
  1400. };
  1401. //get the default output display
  1402. ccGenericGLDisplay* defaultDisplay = nullptr;
  1403. if (m_clipBox->getContainer().getFirstChild())
  1404. {
  1405. defaultDisplay = m_clipBox->getContainer().getFirstChild()->getDisplay();
  1406. }
  1407. //base name
  1408. QString baseName;
  1409. if (m_clipBox->getContainer().getChildrenNumber() == 1)
  1410. {
  1411. baseName = m_clipBox->getContainer().getFirstChild()->getName();
  1412. }
  1413. //slices (clouds or meshes)
  1414. size_t sliceCount = outputSlices.size();
  1415. if (sliceCount)
  1416. {
  1417. if (s_extractSliceCloudsOrMeshes)
  1418. {
  1419. if (s_groupByIndex == OutputFormat::BY_TYPE)
  1420. {
  1421. //we have to create the destination group
  1422. sliceGroup = new ccHObject(baseName.isEmpty() ? QString("Slices") : baseName + QString(".slices"));
  1423. }
  1424. for (ccHObject* slice : outputSlices)
  1425. {
  1426. dispatchEntity(slice, sliceGroup);
  1427. }
  1428. }
  1429. else
  1430. {
  1431. for (ccHObject* slice : outputSlices)
  1432. {
  1433. delete slice;
  1434. }
  1435. outputSlices.clear();
  1436. }
  1437. }
  1438. if (!outputEnvelopes.empty() || !outputLevelSet.empty())
  1439. {
  1440. s_lastContourUniqueIDs.clear();
  1441. }
  1442. //envelopes
  1443. if (!outputEnvelopes.empty())
  1444. {
  1445. if (s_groupByIndex == OutputFormat::BY_TYPE)
  1446. {
  1447. //we have to create the destination group
  1448. envelopeGroup = new ccHObject(baseName.isEmpty() ? QString("Envelopes") : baseName + QString(".envelopes"));
  1449. }
  1450. for (ccPolyline* poly : outputEnvelopes)
  1451. {
  1452. dispatchEntity(poly, envelopeGroup);
  1453. s_lastContourUniqueIDs.push_back(poly->getUniqueID());
  1454. }
  1455. if (envelopeGroup)
  1456. {
  1457. s_lastContourUniqueIDs.push_back(envelopeGroup->getUniqueID());
  1458. }
  1459. }
  1460. //level set
  1461. if (!outputLevelSet.empty())
  1462. {
  1463. if (s_groupByIndex == OutputFormat::BY_TYPE)
  1464. {
  1465. //we have to create the destination group
  1466. levelSetGroup = new ccHObject(baseName.isEmpty() ? QString("Level set") : baseName + QString(".levelSet"));
  1467. }
  1468. for (ccPolyline* poly : outputLevelSet)
  1469. {
  1470. dispatchEntity(poly, levelSetGroup);
  1471. s_lastContourUniqueIDs.push_back(poly->getUniqueID());
  1472. }
  1473. if (levelSetGroup)
  1474. {
  1475. s_lastContourUniqueIDs.push_back(levelSetGroup->getUniqueID());
  1476. }
  1477. }
  1478. removeLastContourToolButton->setEnabled(!s_lastContourUniqueIDs.empty());
  1479. //now take care of the 'output' groups
  1480. {
  1481. if (sliceGroup)
  1482. {
  1483. sliceGroup->setDisplay_recursive(defaultDisplay);
  1484. MainWindow::TheInstance()->addToDB(sliceGroup);
  1485. }
  1486. if (envelopeGroup)
  1487. {
  1488. envelopeGroup->setDisplay_recursive(defaultDisplay);
  1489. MainWindow::TheInstance()->addToDB(envelopeGroup);
  1490. }
  1491. if (levelSetGroup)
  1492. {
  1493. levelSetGroup->setDisplay_recursive(defaultDisplay);
  1494. MainWindow::TheInstance()->addToDB(levelSetGroup);
  1495. }
  1496. for (ccHObject* group : perSliceGroups)
  1497. {
  1498. group->setDisplay_recursive(defaultDisplay);
  1499. MainWindow::TheInstance()->addToDB(group);
  1500. }
  1501. for (ccHObject* group : perEntityGroups)
  1502. {
  1503. group->setDisplay_recursive(defaultDisplay);
  1504. MainWindow::TheInstance()->addToDB(group);
  1505. }
  1506. //don't forget the 'garbage' group (just in case)
  1507. if (garbageGroup)
  1508. {
  1509. if (garbageGroup->getChildrenNumber() != 0)
  1510. {
  1511. garbageGroup->setDisplay_recursive(defaultDisplay);
  1512. MainWindow::TheInstance()->addToDB(garbageGroup);
  1513. }
  1514. else
  1515. {
  1516. delete garbageGroup;
  1517. garbageGroup = nullptr;
  1518. }
  1519. }
  1520. }
  1521. if (m_associatedWin)
  1522. {
  1523. m_associatedWin->redraw();
  1524. }
  1525. if (sliceCount != 0)
  1526. {
  1527. QMessageBox::warning(nullptr, tr("Process finished"), tr("%1 slices have been generated.\n(you may have to close the tool and hide the initial cloud to see them...)").arg(sliceCount));
  1528. }
  1529. else if (s_extractSliceCloudsOrMeshes)
  1530. {
  1531. QMessageBox::warning(nullptr, tr("Process finished"), tr("The process has generated no output"));
  1532. }
  1533. }
  1534. void ccClippingBoxTool::onBoxModified(const ccBBox* box)
  1535. {
  1536. if (box && box->isValid())
  1537. {
  1538. CCVector3 dd = box->maxCorner() - box->minCorner();
  1539. thickXDoubleSpinBox->setValue(dd.x);
  1540. thickYDoubleSpinBox->setValue(dd.y);
  1541. thickZDoubleSpinBox->setValue(dd.z);
  1542. thicknessGroupBox->setEnabled(true);
  1543. }
  1544. else
  1545. {
  1546. thicknessGroupBox->setEnabled(false);
  1547. }
  1548. }
  1549. void ccClippingBoxTool::thicknessChanged(double)
  1550. {
  1551. if (!m_clipBox || !m_clipBox->getBox().isValid())
  1552. return;
  1553. CCVector3 th(static_cast<PointCoordinateType>(thickXDoubleSpinBox->value()),
  1554. static_cast<PointCoordinateType>(thickYDoubleSpinBox->value()),
  1555. static_cast<PointCoordinateType>(thickZDoubleSpinBox->value()));
  1556. ccBBox box = m_clipBox->getBox();
  1557. CCVector3 boxCenter = (box.maxCorner() + box.minCorner()) / 2;
  1558. box.minCorner() = boxCenter - th/2;
  1559. box.maxCorner() = boxCenter + th/2;
  1560. m_clipBox->setBox(box);
  1561. if (m_associatedWin)
  1562. m_associatedWin->redraw();
  1563. }
  1564. void ccClippingBoxTool::shiftBox(unsigned char dim, bool minus)
  1565. {
  1566. if (!m_clipBox || !m_clipBox->getBox().isValid())
  1567. return;
  1568. assert(dim<3);
  1569. PointCoordinateType width = (m_clipBox->getBox().maxCorner() - m_clipBox->getBox().minCorner()).u[dim];
  1570. CCVector3 shiftVec(0, 0, 0);
  1571. shiftVec.u[dim] = (minus ? -width : width);
  1572. m_clipBox->shift(shiftVec);
  1573. if (m_associatedWin)
  1574. m_associatedWin->redraw();
  1575. }
  1576. void ccClippingBoxTool::reset()
  1577. {
  1578. if (m_clipBox)
  1579. m_clipBox->reset();
  1580. if (m_associatedWin)
  1581. m_associatedWin->redraw();
  1582. }
  1583. void ccClippingBoxTool::restoreLastBox()
  1584. {
  1585. if (!m_clipBox || m_clipBox->getContainer().getChildrenNumber() == 0)
  1586. {
  1587. assert(false);
  1588. return;
  1589. }
  1590. unsigned uniqueID = m_clipBox->getContainer().getFirstChild()->getUniqueID();
  1591. if (!s_lastBoxParams.contains(uniqueID))
  1592. {
  1593. assert(false);
  1594. return;
  1595. }
  1596. const ccClipBoxParams& params = s_lastBoxParams[uniqueID];
  1597. m_clipBox->set(params.box, params.trans);
  1598. }
  1599. void ccClippingBoxTool::closeDialog()
  1600. {
  1601. stop(true);
  1602. }
  1603. void ccClippingBoxTool::setTopView()
  1604. {
  1605. setView(CC_TOP_VIEW);
  1606. }
  1607. void ccClippingBoxTool::setBottomView()
  1608. {
  1609. setView(CC_BOTTOM_VIEW);
  1610. }
  1611. void ccClippingBoxTool::setFrontView()
  1612. {
  1613. setView(CC_FRONT_VIEW);
  1614. }
  1615. void ccClippingBoxTool::setBackView()
  1616. {
  1617. setView(CC_BACK_VIEW);
  1618. }
  1619. void ccClippingBoxTool::setLeftView()
  1620. {
  1621. setView(CC_LEFT_VIEW);
  1622. }
  1623. void ccClippingBoxTool::setRightView()
  1624. {
  1625. setView(CC_RIGHT_VIEW);
  1626. }
  1627. void ccClippingBoxTool::setView(CC_VIEW_ORIENTATION orientation)
  1628. {
  1629. if (!m_associatedWin)
  1630. return;
  1631. //m_associatedWin->blockSignals(true);
  1632. m_associatedWin->setView(orientation,false);
  1633. if (m_clipBox && m_clipBox->isGLTransEnabled())
  1634. {
  1635. ccViewportParameters params = m_associatedWin->getViewportParameters();
  1636. const ccGLMatrix& glMat = m_clipBox->getGLTransformation();
  1637. ccGLMatrixd rotMat(glMat.data()); rotMat.clearTranslation();
  1638. params.viewMat = params.viewMat * rotMat.inverse();
  1639. m_associatedWin->setViewportParameters(params);
  1640. }
  1641. //m_associatedWin->blockSignals(false);
  1642. m_associatedWin->redraw();
  1643. }