ccTracePolylineTool.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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 "ccTracePolylineTool.h"
  18. #include "ui_tracePolylineDlg.h"
  19. //Local
  20. #include "mainwindow.h"
  21. #include "ccReservedIDs.h"
  22. //common
  23. #include <ccPickingHub.h>
  24. //qCC_db
  25. #include <ccPolyline.h>
  26. #include <ccPointCloud.h>
  27. #include <ccProgressDialog.h>
  28. //System
  29. #include <cassert>
  30. ccTracePolylineTool::SegmentGLParams::SegmentGLParams(ccGenericGLDisplay* display, int x , int y)
  31. {
  32. if (display)
  33. {
  34. display->getGLCameraParameters(params);
  35. QPointF pos2D = display->toCornerGLCoordinates(x, y);
  36. clickPos = CCVector2d(pos2D.x(), pos2D.y());
  37. }
  38. }
  39. ccTracePolylineTool::ccTracePolylineTool(ccPickingHub* pickingHub, QWidget* parent)
  40. : ccOverlayDialog(parent)
  41. , m_polyTip(nullptr)
  42. , m_polyTipVertices(nullptr)
  43. , m_poly3D(nullptr)
  44. , m_poly3DVertices(nullptr)
  45. , m_done(false)
  46. , m_pickingHub(pickingHub)
  47. , m_ui( new Ui::TracePolyLineDlg )
  48. {
  49. assert(pickingHub);
  50. m_ui->setupUi(this);
  51. setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
  52. connect(m_ui->saveToolButton, &QToolButton::clicked, this, &ccTracePolylineTool::exportLine);
  53. connect(m_ui->resetToolButton, &QToolButton::clicked, this, &ccTracePolylineTool::resetLine);
  54. connect(m_ui->continueToolButton, &QToolButton::clicked, this, &ccTracePolylineTool::continueEdition);
  55. connect(m_ui->validButton, &QToolButton::clicked, this, &ccTracePolylineTool::apply);
  56. connect(m_ui->cancelButton, &QToolButton::clicked, this, &ccTracePolylineTool::cancel);
  57. connect(m_ui->widthSpinBox, qOverload<int>(&QSpinBox::valueChanged), this, &ccTracePolylineTool::onWidthSizeChanged);
  58. //add shortcuts
  59. addOverriddenShortcut(Qt::Key_Escape); //escape key for the "cancel" button
  60. addOverriddenShortcut(Qt::Key_Return); //return key for the "apply" button
  61. connect(this, &ccTracePolylineTool::shortcutTriggered, this, &ccTracePolylineTool::onShortcutTriggered);
  62. m_polyTipVertices = new ccPointCloud("Tip vertices", static_cast<unsigned>(ReservedIDs::TRACE_POLYLINE_TOOL_POLYLINE_TIP_VERTICES));
  63. m_polyTipVertices->reserve(2);
  64. m_polyTipVertices->addPoint(CCVector3(0, 0, 0));
  65. m_polyTipVertices->addPoint(CCVector3(1, 1, 1));
  66. m_polyTipVertices->setEnabled(false);
  67. m_polyTip = new ccPolyline(m_polyTipVertices, static_cast<unsigned>(ReservedIDs::TRACE_POLYLINE_TOOL_POLYLINE_TIP));
  68. m_polyTip->setForeground(true);
  69. m_polyTip->setTempColor(ccColor::green);
  70. m_polyTip->set2DMode(true);
  71. m_polyTip->reserve(2);
  72. m_polyTip->addPointIndex(0, 2);
  73. m_polyTip->setWidth(m_ui->widthSpinBox->value() < 2 ? 0 : m_ui->widthSpinBox->value()); //'1' is equivalent to the default line size
  74. m_polyTip->addChild(m_polyTipVertices);
  75. m_ui->validButton->setEnabled(false);
  76. }
  77. ccTracePolylineTool::~ccTracePolylineTool()
  78. {
  79. //m_polyTipVertices is already a child of m_polyTip
  80. if (m_polyTip)
  81. delete m_polyTip;
  82. m_polyTip = nullptr;
  83. //m_poly3DVertices is already a child of m_poly3D
  84. if (m_poly3D)
  85. delete m_poly3D;
  86. m_poly3D = nullptr;
  87. delete m_ui;
  88. m_ui = nullptr;
  89. }
  90. void ccTracePolylineTool::onShortcutTriggered(int key)
  91. {
  92. switch (key)
  93. {
  94. case Qt::Key_Return:
  95. apply();
  96. return;
  97. case Qt::Key_Escape:
  98. cancel();
  99. return;
  100. default:
  101. //nothing to do
  102. break;
  103. }
  104. }
  105. ccPolyline* ccTracePolylineTool::polylineOverSampling(unsigned steps) const
  106. {
  107. if (!m_poly3D || !m_poly3DVertices || m_segmentParams.size() != m_poly3DVertices->size())
  108. {
  109. assert(false);
  110. return nullptr;
  111. }
  112. if (steps <= 1)
  113. {
  114. //nothing to do
  115. return nullptr;
  116. }
  117. ccHObject::Container clouds;
  118. ccHObject::Container meshes;
  119. if (m_associatedWin)
  120. {
  121. m_associatedWin->getSceneDB()->filterChildren(clouds, true, CC_TYPES::POINT_CLOUD, false, m_associatedWin);
  122. m_associatedWin->getSceneDB()->filterChildren(meshes, true, CC_TYPES::MESH, false, m_associatedWin);
  123. }
  124. if (clouds.empty() && meshes.empty())
  125. {
  126. //no entity is currently displayed?!
  127. assert(false);
  128. return nullptr;
  129. }
  130. unsigned vertexCount = m_poly3DVertices->size();
  131. unsigned segmentCount = m_poly3D->size() - (m_poly3D->isClosed() ? 0 : 1);
  132. unsigned endSize = segmentCount * steps + (m_poly3D->isClosed() ? 0 : 1);
  133. ccPointCloud* newVertices = new ccPointCloud();
  134. ccPolyline* newPoly = new ccPolyline(newVertices);
  135. newPoly->addChild(newVertices);
  136. if ( !newVertices->reserve(endSize)
  137. || !newPoly->reserve(endSize) )
  138. {
  139. ccLog::Warning("[ccTracePolylineTool::PolylineOverSampling] Not enough memory");
  140. delete newPoly;
  141. return nullptr;
  142. }
  143. newVertices->importParametersFrom(m_poly3DVertices);
  144. newVertices->setName(m_poly3DVertices->getName());
  145. newVertices->setEnabled(m_poly3DVertices->isEnabled());
  146. newPoly->importParametersFrom(*m_poly3D);
  147. newPoly->setDisplay_recursive(m_poly3D->getDisplay());
  148. ccProgressDialog pDlg(true, m_associatedWin ? m_associatedWin->asWidget() : nullptr);
  149. pDlg.setWindowTitle("Oversampling");
  150. pDlg.setMethodTitle(tr("Oversampling polyline: %1 vertices").arg(endSize));
  151. pDlg.setRange(0, static_cast<int>(endSize));
  152. pDlg.show();
  153. QCoreApplication::processEvents();
  154. for (unsigned i = 0; i < segmentCount; ++i)
  155. {
  156. const CCVector3* p1 = m_poly3DVertices->getPoint(i);
  157. newVertices->addPoint(*p1);
  158. unsigned i2 = (i + 1) % vertexCount;
  159. CCVector2d clickPos1/* = m_segmentParams[i].clickPos*/;
  160. {
  161. //we actually retro-project the 3D point in the second vertex camera frame so as to get a proper behavior
  162. CCVector3d P2D;
  163. m_segmentParams[i2].params.project(*p1, P2D);
  164. clickPos1 = CCVector2d(P2D.x, P2D.y);
  165. }
  166. CCVector2d v = m_segmentParams[i2].clickPos - clickPos1;
  167. v /= steps;
  168. for (unsigned j = 1; j < steps; j++)
  169. {
  170. CCVector2d vj = clickPos1 + v * j;
  171. CCVector3 nearestPoint;
  172. double nearestElementSquareDist = -1.0;
  173. //for each cloud
  174. for (size_t c = 0; c < clouds.size(); ++c)
  175. {
  176. ccGenericPointCloud* cloud = static_cast<ccGenericPointCloud*>(clouds[c]);
  177. if (!cloud->isDisplayedIn(m_associatedWin))
  178. {
  179. continue;
  180. }
  181. int nearestPointIndex = -1;
  182. double nearestSquareDist = 0;
  183. if (cloud->pointPicking(vj,
  184. m_segmentParams[i2].params,
  185. nearestPointIndex,
  186. nearestSquareDist,
  187. m_ui->snapSizeSpinBox->value(),
  188. m_ui->snapSizeSpinBox->value(),
  189. true))
  190. {
  191. if (nearestElementSquareDist < 0 || nearestSquareDist < nearestElementSquareDist)
  192. {
  193. nearestElementSquareDist = nearestSquareDist;
  194. nearestPoint = *cloud->getPoint(nearestPointIndex);
  195. }
  196. }
  197. }
  198. //for each mesh
  199. for (size_t m = 0; m < meshes.size(); ++m)
  200. {
  201. ccGenericMesh* mesh = static_cast<ccGenericMesh*>(meshes[m]);
  202. if (!mesh->isDisplayedIn(m_associatedWin))
  203. {
  204. continue;
  205. }
  206. int nearestTriIndex = -1;
  207. double nearestSquareDist = 0;
  208. CCVector3d _nearestPoint;
  209. if (mesh->trianglePicking( vj,
  210. m_segmentParams[i2].params,
  211. nearestTriIndex,
  212. nearestSquareDist,
  213. _nearestPoint))
  214. {
  215. if (nearestElementSquareDist < 0 || nearestSquareDist < nearestElementSquareDist)
  216. {
  217. nearestElementSquareDist = nearestSquareDist;
  218. nearestPoint = _nearestPoint.toPC();
  219. }
  220. }
  221. }
  222. if (nearestElementSquareDist >= 0)
  223. {
  224. newVertices->addPoint(nearestPoint);
  225. }
  226. if (pDlg.wasCanceled())
  227. {
  228. steps = 0; //quick finish ;)
  229. break;
  230. }
  231. pDlg.setValue(pDlg.value() + 1);
  232. }
  233. }
  234. //add last point
  235. if (!m_poly3D->isClosed())
  236. {
  237. newVertices->addPoint(*m_poly3DVertices->getPoint(vertexCount - 1));
  238. }
  239. newVertices->shrinkToFit();
  240. newPoly->addPointIndex(0, newVertices->size());
  241. return newPoly;
  242. }
  243. bool ccTracePolylineTool::linkWith(ccGLWindowInterface* win)
  244. {
  245. assert(m_polyTip);
  246. assert(!m_poly3D || !win);
  247. ccGLWindowInterface* oldWin = m_associatedWin;
  248. if (!ccOverlayDialog::linkWith(win))
  249. {
  250. return false;
  251. }
  252. if (oldWin)
  253. {
  254. oldWin->removeFromOwnDB(m_polyTip);
  255. oldWin->signalEmitter()->disconnect(this);
  256. if (m_polyTip)
  257. m_polyTip->setDisplay(nullptr);
  258. }
  259. if (m_associatedWin)
  260. {
  261. connect(m_associatedWin->signalEmitter(), &ccGLWindowSignalEmitter::rightButtonClicked, this, &ccTracePolylineTool::closePolyLine);
  262. connect(m_associatedWin->signalEmitter(), &ccGLWindowSignalEmitter::mouseMoved, this, &ccTracePolylineTool::updatePolyLineTip);
  263. }
  264. return true;
  265. }
  266. static int s_defaultPickingRadius = 1;
  267. static int s_overSamplingCount = 1;
  268. bool ccTracePolylineTool::start()
  269. {
  270. assert(m_polyTip);
  271. assert(!m_poly3D);
  272. if (!m_associatedWin)
  273. {
  274. ccLog::Warning("[Trace Polyline Tool] No associated window!");
  275. return false;
  276. }
  277. m_associatedWin->setUnclosable(true);
  278. m_associatedWin->addToOwnDB(m_polyTip);
  279. if (m_pickingHub)
  280. {
  281. m_pickingHub->removeListener(this);
  282. }
  283. m_associatedWin->setPickingMode(ccGLWindowInterface::NO_PICKING);
  284. m_associatedWin->setInteractionMode( ccGLWindowInterface::MODE_TRANSFORM_CAMERA
  285. | ccGLWindowInterface::INTERACT_SIG_RB_CLICKED
  286. | ccGLWindowInterface::INTERACT_CTRL_PAN
  287. | ccGLWindowInterface::INTERACT_SIG_MOUSE_MOVED);
  288. m_associatedWin->setWindowCursor(Qt::CrossCursor);
  289. m_ui->snapSizeSpinBox->blockSignals(true);
  290. m_ui->snapSizeSpinBox->setValue(s_defaultPickingRadius);
  291. m_ui->snapSizeSpinBox->blockSignals(false);
  292. m_ui->oversampleSpinBox->blockSignals(true);
  293. m_ui->oversampleSpinBox->setValue(s_overSamplingCount);
  294. m_ui->oversampleSpinBox->blockSignals(false);
  295. resetLine(); //to reset the GUI
  296. return ccOverlayDialog::start();
  297. }
  298. void ccTracePolylineTool::stop(bool accepted)
  299. {
  300. assert(m_polyTip);
  301. if (m_pickingHub)
  302. {
  303. m_pickingHub->removeListener(this);
  304. }
  305. if (m_associatedWin)
  306. {
  307. m_associatedWin->displayNewMessage( "Polyline tracing [OFF]",
  308. ccGLWindowInterface::UPPER_CENTER_MESSAGE,
  309. false,
  310. 2,
  311. ccGLWindowInterface::MANUAL_SEGMENTATION_MESSAGE);
  312. m_associatedWin->setUnclosable(false);
  313. m_associatedWin->removeFromOwnDB(m_polyTip);
  314. m_associatedWin->setInteractionMode(ccGLWindowInterface::MODE_TRANSFORM_CAMERA);
  315. m_associatedWin->setWindowCursor(Qt::ArrowCursor);
  316. }
  317. s_defaultPickingRadius = m_ui->snapSizeSpinBox->value();
  318. s_overSamplingCount = m_ui->oversampleSpinBox->value();
  319. ccOverlayDialog::stop(accepted);
  320. }
  321. void ccTracePolylineTool::updatePolyLineTip(int x, int y, Qt::MouseButtons buttons)
  322. {
  323. if (!m_associatedWin)
  324. {
  325. assert(false);
  326. return;
  327. }
  328. if (buttons != Qt::NoButton)
  329. {
  330. //nothing to do (just hide the tip)
  331. if (m_polyTip->isEnabled())
  332. {
  333. m_polyTip->setEnabled(false);
  334. m_associatedWin->redraw(true, false);
  335. }
  336. return;
  337. }
  338. if (!m_poly3DVertices || m_poly3DVertices->size() == 0)
  339. {
  340. //there should be at least one point already picked!
  341. return;
  342. }
  343. if (m_done)
  344. {
  345. // when it is done do nothing
  346. return;
  347. }
  348. assert(m_polyTip && m_polyTipVertices && m_polyTipVertices->size() == 2);
  349. //we replace the last point by the new one
  350. {
  351. QPointF pos2D = m_associatedWin->toCenteredGLCoordinates(x, y);
  352. CCVector3 P2D( static_cast<PointCoordinateType>(pos2D.x()),
  353. static_cast<PointCoordinateType>(pos2D.y()),
  354. 0);
  355. CCVector3* lastP = const_cast<CCVector3*>(m_polyTipVertices->getPointPersistentPtr(1));
  356. *lastP = P2D;
  357. }
  358. //just in case (e.g. if the view has been rotated or zoomed)
  359. //we also update the first vertex position!
  360. {
  361. const CCVector3* P3D = m_poly3DVertices->getPoint(m_poly3DVertices->size() - 1);
  362. ccGLCameraParameters camera;
  363. m_associatedWin->getGLCameraParameters(camera);
  364. CCVector3d A2D;
  365. camera.project(*P3D, A2D);
  366. CCVector3* firstP = const_cast<CCVector3*>(m_polyTipVertices->getPointPersistentPtr(0));
  367. *firstP = CCVector3(static_cast<PointCoordinateType>(A2D.x - camera.viewport[2] / 2), //we convert A2D to centered coordinates (no need to apply high DPI scale or anything!)
  368. static_cast<PointCoordinateType>(A2D.y - camera.viewport[3] / 2),
  369. 0);
  370. }
  371. m_polyTip->setEnabled(true);
  372. m_associatedWin->redraw(true, false);
  373. }
  374. void ccTracePolylineTool::onItemPicked(const PickedItem& pi)
  375. {
  376. if (!m_associatedWin)
  377. {
  378. assert(false);
  379. return;
  380. }
  381. if (!pi.entity)
  382. {
  383. //means that the mouse has been clicked but no point was found!
  384. return;
  385. }
  386. //if the 3D polyline doesn't exist yet, we create it
  387. if (!m_poly3D || !m_poly3DVertices)
  388. {
  389. m_poly3DVertices = new ccPointCloud("Vertices", static_cast<unsigned>(ReservedIDs::TRACE_POLYLINE_TOOL_POLYLINE_VERTICES));
  390. m_poly3DVertices->setEnabled(false);
  391. m_poly3DVertices->setDisplay(m_associatedWin);
  392. m_poly3D = new ccPolyline(m_poly3DVertices, static_cast<unsigned>(ReservedIDs::TRACE_POLYLINE_TOOL_POLYLINE));
  393. m_poly3D->setTempColor(ccColor::green);
  394. m_poly3D->set2DMode(false);
  395. m_poly3D->addChild(m_poly3DVertices);
  396. m_poly3D->setWidth(m_ui->widthSpinBox->value() < 2 ? 0 : m_ui->widthSpinBox->value()); //'1' is equivalent to the default line size
  397. ccGenericPointCloud* cloud = ccHObjectCaster::ToGenericPointCloud(pi.entity);
  398. if (cloud)
  399. {
  400. //copy the first clicked entity's global shift & scale
  401. m_poly3D->copyGlobalShiftAndScale(*cloud);
  402. }
  403. m_segmentParams.resize(0); //just in case
  404. m_associatedWin->addToOwnDB(m_poly3D);
  405. }
  406. //try to add one more point
  407. if ( !m_poly3DVertices->reserve(m_poly3DVertices->size() + 1)
  408. || !m_poly3D->reserve(m_poly3DVertices->size() + 1))
  409. {
  410. ccLog::Error("Not enough memory");
  411. return;
  412. }
  413. try
  414. {
  415. m_segmentParams.reserve(m_segmentParams.size() + 1);
  416. }
  417. catch (const std::bad_alloc&)
  418. {
  419. ccLog::Error("Not enough memory");
  420. return;
  421. }
  422. m_poly3DVertices->addPoint(pi.P3D);
  423. m_poly3D->addPointIndex(m_poly3DVertices->size() - 1);
  424. m_segmentParams.emplace_back(m_associatedWin, pi.clickPoint.x(), pi.clickPoint.y());
  425. //we replace the first point of the tip by this new point
  426. {
  427. QPointF pos2D = m_associatedWin->toCenteredGLCoordinates(pi.clickPoint.x(), pi.clickPoint.y());
  428. CCVector3 P2D( static_cast<PointCoordinateType>(pos2D.x()),
  429. static_cast<PointCoordinateType>(pos2D.y()),
  430. 0);
  431. CCVector3* firstTipPoint = const_cast<CCVector3*>(m_polyTipVertices->getPointPersistentPtr(0));
  432. *firstTipPoint = P2D;
  433. m_polyTip->setEnabled(false); //don't need to display it for now
  434. }
  435. m_associatedWin->redraw(false, false);
  436. }
  437. void ccTracePolylineTool::closePolyLine(int, int)
  438. {
  439. if (!m_poly3D || (QApplication::keyboardModifiers() & Qt::ControlModifier)) //CTRL + right click = panning
  440. {
  441. return;
  442. }
  443. unsigned vertCount = m_poly3D->size();
  444. if (vertCount < 2)
  445. {
  446. //discard this polyline
  447. resetLine();
  448. }
  449. else
  450. {
  451. //hide the tip
  452. if (m_polyTip)
  453. {
  454. m_polyTip->setEnabled(false);
  455. }
  456. //update the GUI
  457. m_ui->validButton->setEnabled(true);
  458. m_ui->saveToolButton->setEnabled(true);
  459. m_ui->resetToolButton->setEnabled(true);
  460. m_ui->continueToolButton->setEnabled(true);
  461. if (m_pickingHub)
  462. {
  463. m_pickingHub->removeListener(this);
  464. }
  465. if (m_associatedWin)
  466. {
  467. m_associatedWin->setPickingMode(ccGLWindowInterface::NO_PICKING); //no more picking
  468. m_done = true;
  469. m_associatedWin->redraw(true, false);
  470. }
  471. }
  472. }
  473. void ccTracePolylineTool::restart(bool reset)
  474. {
  475. if (m_poly3D)
  476. {
  477. if (reset)
  478. {
  479. if (m_associatedWin)
  480. {
  481. //discard this polyline
  482. m_associatedWin->removeFromOwnDB(m_poly3D);
  483. }
  484. if (m_polyTip)
  485. {
  486. //hide the tip
  487. m_polyTip->setEnabled(false);
  488. }
  489. delete m_poly3D;
  490. m_segmentParams.resize(0);
  491. //delete m_poly3DVertices;
  492. m_poly3D = nullptr;
  493. m_poly3DVertices = nullptr;
  494. }
  495. else
  496. {
  497. if (m_polyTip)
  498. {
  499. //show the tip
  500. m_polyTip->setEnabled(true);
  501. }
  502. }
  503. }
  504. //enable picking
  505. if (m_pickingHub && !m_pickingHub->addListener(this, true/*, true, ccGLWindowInterface::POINT_PICKING*/))
  506. {
  507. ccLog::Error("The picking mechanism is already in use. Close the tool using it first.");
  508. }
  509. if (m_associatedWin)
  510. {
  511. m_associatedWin->redraw(false, false);
  512. }
  513. m_ui->validButton->setEnabled(false);
  514. m_ui->saveToolButton->setEnabled(false);
  515. m_ui->resetToolButton->setEnabled(false);
  516. m_ui->continueToolButton->setEnabled(false);
  517. m_done = false;
  518. }
  519. void ccTracePolylineTool::exportLine()
  520. {
  521. if (!m_poly3D)
  522. {
  523. return;
  524. }
  525. if (m_associatedWin)
  526. {
  527. m_associatedWin->removeFromOwnDB(m_poly3D);
  528. }
  529. unsigned overSampling = static_cast<unsigned>(m_ui->oversampleSpinBox->value());
  530. if (overSampling > 1)
  531. {
  532. ccPolyline* poly = polylineOverSampling(overSampling);
  533. if (poly)
  534. {
  535. delete m_poly3D;
  536. m_segmentParams.resize(0);
  537. m_poly3DVertices = nullptr;
  538. m_poly3D = poly;
  539. }
  540. else
  541. {
  542. ccLog::Error("Oversampling failed");
  543. if (m_associatedWin)
  544. {
  545. m_associatedWin->addToOwnDB(m_poly3D);
  546. }
  547. return;
  548. }
  549. }
  550. else
  551. {
  552. //update the IDs as we don't want to export an object with a reserved ID!
  553. m_poly3D->setUniqueID(ccObject::GetNextUniqueID());
  554. m_poly3DVertices->setUniqueID(ccObject::GetNextUniqueID());
  555. }
  556. m_poly3D->enableTempColor(false);
  557. m_poly3D->setDisplay(m_associatedWin); //just in case
  558. if (MainWindow::TheInstance())
  559. {
  560. MainWindow::TheInstance()->addToDB(m_poly3D);
  561. }
  562. else
  563. {
  564. assert(false);
  565. }
  566. m_poly3D = nullptr;
  567. m_segmentParams.resize(0);
  568. m_poly3DVertices = nullptr;
  569. resetLine(); //to update the GUI
  570. }
  571. void ccTracePolylineTool::apply()
  572. {
  573. exportLine();
  574. stop(true);
  575. }
  576. void ccTracePolylineTool::cancel()
  577. {
  578. resetLine();
  579. stop(false);
  580. }
  581. void ccTracePolylineTool::onWidthSizeChanged(int width)
  582. {
  583. if (m_poly3D)
  584. {
  585. m_poly3D->setWidth(width);
  586. }
  587. if (m_polyTip)
  588. {
  589. m_polyTip->setWidth(width);
  590. }
  591. if (m_associatedWin)
  592. {
  593. m_associatedWin->redraw(m_poly3D == nullptr, false);
  594. }
  595. }