ccPointPropertiesDlg.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 "ccPointPropertiesDlg.h"
  18. //Local
  19. #include "ccCommon.h"
  20. //qCC_gl
  21. #include <ccGLWindowInterface.h>
  22. #include <ccGuiParameters.h>
  23. //qCC_db
  24. #include <ccLog.h>
  25. #include <ccPointCloud.h>
  26. #include <ccGenericMesh.h>
  27. #include <cc2DViewportLabel.h>
  28. #include <cc2DLabel.h>
  29. //CCCoreLib
  30. #include <ScalarField.h>
  31. //Qt
  32. #include <QInputDialog>
  33. //System
  34. #include <assert.h>
  35. ccPointPropertiesDlg::ccPointPropertiesDlg(ccPickingHub* pickingHub, QWidget* parent)
  36. : ccPointPickingGenericInterface(pickingHub, parent)
  37. , Ui::PointPropertiesDlg()
  38. , m_pickingMode(POINT_INFO)
  39. {
  40. setupUi(this);
  41. connect(closeButton, &QToolButton::clicked, this, &ccPointPropertiesDlg::onClose);
  42. connect(pointPropertiesButton, &QToolButton::clicked, this, &ccPointPropertiesDlg::activatePointPropertiesDisplay);
  43. connect(pointPointDistanceButton, &QToolButton::clicked, this, &ccPointPropertiesDlg::activateDistanceDisplay);
  44. connect(pointsAngleButton, &QToolButton::clicked, this, &ccPointPropertiesDlg::activateAngleDisplay);
  45. connect(rectZoneToolButton, &QToolButton::clicked, this, &ccPointPropertiesDlg::activate2DZonePicking);
  46. connect(saveLabelButton, &QToolButton::clicked, this, &ccPointPropertiesDlg::exportCurrentLabel);
  47. connect(razButton, &QToolButton::clicked, this, &ccPointPropertiesDlg::initializeState);
  48. //for points picking
  49. m_label = new cc2DLabel();
  50. m_label->setSelected(true);
  51. //for 2D zone picking
  52. m_rect2DLabel = new cc2DViewportLabel();
  53. m_rect2DLabel->setVisible(false); //=invalid
  54. m_rect2DLabel->setSelected(true); //=closed
  55. }
  56. ccPointPropertiesDlg::~ccPointPropertiesDlg()
  57. {
  58. if (m_label)
  59. delete m_label;
  60. m_label = nullptr;
  61. if (m_rect2DLabel)
  62. delete m_rect2DLabel;
  63. m_rect2DLabel = nullptr;
  64. }
  65. bool ccPointPropertiesDlg::linkWith(ccGLWindowInterface* win)
  66. {
  67. assert(m_label && m_rect2DLabel);
  68. ccGLWindowInterface* oldWin = m_associatedWin;
  69. if (!ccPointPickingGenericInterface::linkWith(win))
  70. {
  71. return false;
  72. }
  73. //old window?
  74. if (oldWin)
  75. {
  76. oldWin->removeFromOwnDB(m_label);
  77. oldWin->removeFromOwnDB(m_rect2DLabel);
  78. oldWin->setInteractionMode(ccGLWindowInterface::MODE_TRANSFORM_CAMERA);
  79. oldWin->signalEmitter()->disconnect(this);
  80. }
  81. m_rect2DLabel->setVisible(false); //=invalid
  82. m_rect2DLabel->setSelected(true); //=closed
  83. m_label->clear();
  84. //new window?
  85. if (m_associatedWin)
  86. {
  87. m_associatedWin->addToOwnDB(m_label);
  88. m_associatedWin->addToOwnDB(m_rect2DLabel);
  89. connect(m_associatedWin->signalEmitter(), &ccGLWindowSignalEmitter::mouseMoved, this, &ccPointPropertiesDlg::update2DZone);
  90. connect(m_associatedWin->signalEmitter(), &ccGLWindowSignalEmitter::leftButtonClicked, this, &ccPointPropertiesDlg::processClickedPoint);
  91. connect(m_associatedWin->signalEmitter(), &ccGLWindowSignalEmitter::buttonReleased, this, &ccPointPropertiesDlg::close2DZone);
  92. }
  93. return true;
  94. }
  95. bool ccPointPropertiesDlg::start()
  96. {
  97. activatePointPropertiesDisplay();
  98. return ccPointPickingGenericInterface::start();
  99. }
  100. void ccPointPropertiesDlg::stop(bool state)
  101. {
  102. assert(m_label && m_rect2DLabel);
  103. m_label->clear();
  104. m_rect2DLabel->setVisible(false); //=invalid
  105. m_rect2DLabel->setSelected(true); //=closed
  106. if (m_associatedWin)
  107. m_associatedWin->setInteractionMode(ccGLWindowInterface::MODE_TRANSFORM_CAMERA);
  108. ccPointPickingGenericInterface::stop(state);
  109. }
  110. void ccPointPropertiesDlg::onClose()
  111. {
  112. stop(false);
  113. }
  114. void ccPointPropertiesDlg::activatePointPropertiesDisplay()
  115. {
  116. if (m_associatedWin)
  117. m_associatedWin->setInteractionMode(ccGLWindowInterface::MODE_TRANSFORM_CAMERA);
  118. m_pickingMode = POINT_INFO;
  119. pointPropertiesButton->setDown(true);
  120. pointPointDistanceButton->setDown(false);
  121. pointsAngleButton->setDown(false);
  122. rectZoneToolButton->setDown(false);
  123. m_label->setVisible(true);
  124. m_rect2DLabel->setVisible(false);
  125. }
  126. void ccPointPropertiesDlg::activateDistanceDisplay()
  127. {
  128. m_pickingMode = POINT_POINT_DISTANCE;
  129. pointPropertiesButton->setDown(false);
  130. pointPointDistanceButton->setDown(true);
  131. pointsAngleButton->setDown(false);
  132. rectZoneToolButton->setDown(false);
  133. m_label->setVisible(true);
  134. m_rect2DLabel->setVisible(false);
  135. if (m_associatedWin)
  136. {
  137. m_associatedWin->setInteractionMode(ccGLWindowInterface::MODE_TRANSFORM_CAMERA);
  138. m_associatedWin->redraw(false);
  139. }
  140. }
  141. void ccPointPropertiesDlg::activateAngleDisplay()
  142. {
  143. m_pickingMode = POINTS_ANGLE;
  144. pointPropertiesButton->setDown(false);
  145. pointPointDistanceButton->setDown(false);
  146. pointsAngleButton->setDown(true);
  147. rectZoneToolButton->setDown(false);
  148. m_label->setVisible(true);
  149. m_rect2DLabel->setVisible(false);
  150. if (m_associatedWin)
  151. {
  152. m_associatedWin->setInteractionMode(ccGLWindowInterface::MODE_TRANSFORM_CAMERA);
  153. m_associatedWin->redraw(false);
  154. }
  155. }
  156. void ccPointPropertiesDlg::activate2DZonePicking()
  157. {
  158. m_pickingMode = RECT_ZONE;
  159. pointPropertiesButton->setDown(false);
  160. pointPointDistanceButton->setDown(false);
  161. pointsAngleButton->setDown(false);
  162. rectZoneToolButton->setDown(true);
  163. m_label->setVisible(false);
  164. //m_rect2DLabel->setVisible(false);
  165. if (m_associatedWin)
  166. {
  167. m_associatedWin->setInteractionMode(ccGLWindowInterface::INTERACT_SEND_ALL_SIGNALS);
  168. m_associatedWin->redraw(false);
  169. }
  170. }
  171. void ccPointPropertiesDlg::initializeState()
  172. {
  173. assert(m_label && m_rect2DLabel);
  174. m_label->clear();
  175. m_rect2DLabel->setVisible(false); //=invalid
  176. m_rect2DLabel->setSelected(true); //=closed
  177. if (m_associatedWin)
  178. m_associatedWin->redraw(false);
  179. }
  180. void ccPointPropertiesDlg::exportCurrentLabel()
  181. {
  182. ccHObject* labelObject = nullptr;
  183. if (m_pickingMode == RECT_ZONE)
  184. labelObject = (m_rect2DLabel->isSelected() && m_rect2DLabel->isVisible() ? m_rect2DLabel : nullptr);
  185. else
  186. labelObject = (m_label && m_label->size() > 0 ? m_label : nullptr);
  187. if (!labelObject)
  188. {
  189. return;
  190. }
  191. //detach current label from window
  192. if (m_associatedWin)
  193. m_associatedWin->removeFromOwnDB(labelObject);
  194. labelObject->setSelected(false);
  195. ccHObject* newLabelObject = nullptr;
  196. if (m_pickingMode == RECT_ZONE)
  197. {
  198. //if (m_associatedWin)
  199. // m_rect2DLabel->setParameters(m_associatedWin->getViewportParameters());
  200. newLabelObject = m_rect2DLabel = new cc2DViewportLabel();
  201. m_rect2DLabel->setVisible(false); //=invalid
  202. m_rect2DLabel->setSelected(true); //=closed
  203. }
  204. else
  205. {
  206. //attach old label to first point cloud by default
  207. m_label->getPickedPoint(0).entity()->addChild(labelObject);
  208. newLabelObject = m_label = new cc2DLabel();
  209. m_label->setSelected(true);
  210. }
  211. Q_EMIT newLabel(labelObject);
  212. if (m_associatedWin)
  213. {
  214. m_associatedWin->addToOwnDB(newLabelObject);
  215. m_associatedWin->redraw(true, false);
  216. }
  217. }
  218. void ccPointPropertiesDlg::processPickedPoint(const PickedItem& picked)
  219. {
  220. assert(picked.entity);
  221. assert(m_label);
  222. assert(m_associatedWin);
  223. switch (m_pickingMode)
  224. {
  225. case POINT_INFO:
  226. m_label->clear();
  227. break;
  228. case POINT_POINT_DISTANCE:
  229. if (m_label->size() >= 2)
  230. m_label->clear();
  231. break;
  232. case POINTS_ANGLE:
  233. if (m_label->size() >= 3)
  234. m_label->clear();
  235. break;
  236. case RECT_ZONE:
  237. return; //we don't use this slot for 2D mode
  238. }
  239. if (picked.entity->isKindOf(CC_TYPES::POINT_CLOUD))
  240. {
  241. m_label->addPickedPoint(static_cast<ccGenericPointCloud*>(picked.entity), picked.itemIndex, picked.entityCenter);
  242. }
  243. else if (picked.entity->isKindOf(CC_TYPES::MESH))
  244. {
  245. m_label->addPickedPoint(static_cast<ccGenericMesh*>(picked.entity), picked.itemIndex, CCVector2d(picked.uvw.x, picked.uvw.y), picked.entityCenter);
  246. }
  247. else
  248. {
  249. assert(false);
  250. return;
  251. }
  252. m_label->setVisible(true);
  253. m_label->displayPointLegend(m_label->size() == 3); //we need to display 'A', 'B' and 'C' for 3-points labels
  254. if (m_label->size() == 1 && m_associatedWin)
  255. {
  256. m_label->setPosition(static_cast<float>(picked.clickPoint.x() + 20) / m_associatedWin->glWidth(), static_cast<float>(picked.clickPoint.y() + 20) / m_associatedWin->glHeight());
  257. }
  258. //output info to Console
  259. QStringList body = m_label->getLabelContent(ccGui::Parameters().displayedNumPrecision);
  260. ccLog::Print(QString("[Picked] ") + m_label->getName());
  261. for (QString& row : body)
  262. {
  263. ccLog::Print(QString("[Picked]\t- ") + row);
  264. }
  265. if (m_associatedWin)
  266. {
  267. m_associatedWin->redraw();
  268. }
  269. }
  270. void ccPointPropertiesDlg::processClickedPoint(int x, int y)
  271. {
  272. if (m_pickingMode != RECT_ZONE)
  273. {
  274. return;
  275. }
  276. if (!m_rect2DLabel || !m_associatedWin)
  277. {
  278. assert(false);
  279. return;
  280. }
  281. QPointF pos2D = m_associatedWin->toCenteredGLCoordinates(x, y);
  282. if (m_rect2DLabel->isSelected()) //already closed? we start a new label
  283. {
  284. cc2DViewportLabel::ROI roi{ static_cast<float>(pos2D.x()),
  285. static_cast<float>(pos2D.y()),
  286. 0.0f,
  287. 0.0f};
  288. if (m_associatedWin)
  289. {
  290. m_rect2DLabel->setParameters(m_associatedWin->getViewportParameters());
  291. }
  292. m_rect2DLabel->setVisible(false); //=invalid
  293. m_rect2DLabel->setSelected(false); //=not closed
  294. m_rect2DLabel->setRoi(roi);
  295. m_rect2DLabel->setName(QString()); //reset name before display!
  296. }
  297. else //we close the existing one
  298. {
  299. cc2DViewportLabel::ROI roi {m_rect2DLabel->roi()[0],
  300. m_rect2DLabel->roi()[1],
  301. static_cast<float>(pos2D.x()),
  302. static_cast<float>(pos2D.y()) };
  303. m_rect2DLabel->setRoi(roi);
  304. m_rect2DLabel->setVisible(true); //=valid
  305. m_rect2DLabel->setSelected(true); //=closed
  306. }
  307. if (m_associatedWin)
  308. {
  309. m_associatedWin->redraw(true, false);
  310. }
  311. }
  312. void ccPointPropertiesDlg::update2DZone(int x, int y, Qt::MouseButtons buttons)
  313. {
  314. if (m_pickingMode != RECT_ZONE)
  315. {
  316. return;
  317. }
  318. if (m_rect2DLabel->isSelected())
  319. {
  320. return;
  321. }
  322. if (!m_associatedWin)
  323. {
  324. assert(false);
  325. return;
  326. }
  327. QPointF pos2D = m_associatedWin->toCenteredGLCoordinates(x, y);
  328. cc2DViewportLabel::ROI roi {m_rect2DLabel->roi()[0],
  329. m_rect2DLabel->roi()[1],
  330. static_cast<float>(pos2D.x()),
  331. static_cast<float>(pos2D.y()) };
  332. m_rect2DLabel->setRoi(roi);
  333. m_rect2DLabel->setVisible(true);
  334. m_associatedWin->redraw(true, false);
  335. }
  336. static QString s_last2DLabelComment;
  337. void ccPointPropertiesDlg::close2DZone()
  338. {
  339. if (m_pickingMode != RECT_ZONE)
  340. return;
  341. if (m_rect2DLabel->isSelected() || !m_rect2DLabel->isVisible())
  342. return;
  343. m_rect2DLabel->setSelected(true);
  344. bool ok;
  345. QString title = QInputDialog::getText(this, "Set area label title", "Title:", QLineEdit::Normal, s_last2DLabelComment, &ok);
  346. if (!ok)
  347. {
  348. m_rect2DLabel->setVisible(false);
  349. }
  350. else
  351. {
  352. m_rect2DLabel->setName(title);
  353. s_last2DLabelComment = title;
  354. }
  355. if (m_associatedWin)
  356. m_associatedWin->redraw(true, false);
  357. }