ctkCoordinatesWidget.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QDoubleSpinBox>
  17. #include <QHBoxLayout>
  18. // CTK includes
  19. #include "ctkCoordinatesWidget.h"
  20. #include "ctkDoubleSpinBox.h"
  21. #include "ctkValueProxy.h"
  22. // STD includes
  23. #include <cmath>
  24. //------------------------------------------------------------------------------
  25. ctkCoordinatesWidget::ctkCoordinatesWidget(QWidget* _parent) :QWidget(_parent)
  26. {
  27. this->Decimals = 3;
  28. ctkDoubleSpinBox temp;
  29. this->DecimalsOption = temp.decimalsOption();
  30. this->SingleStep = 1.;
  31. this->Minimum = -100000.;
  32. this->Maximum = 100000.;
  33. this->Normalized = false;
  34. this->Dimension = 0;
  35. this->Coordinates = 0;
  36. QHBoxLayout* hboxLayout = new QHBoxLayout(this);
  37. hboxLayout->setContentsMargins(0, 0, 0, 0);
  38. this->setLayout(hboxLayout);
  39. this->setDimension(3);
  40. }
  41. //------------------------------------------------------------------------------
  42. ctkCoordinatesWidget::~ctkCoordinatesWidget()
  43. {
  44. delete [] this->Coordinates;
  45. }
  46. //------------------------------------------------------------------------------
  47. void ctkCoordinatesWidget::addSpinBox()
  48. {
  49. ctkDoubleSpinBox* spinBox = new ctkDoubleSpinBox(this);
  50. spinBox->setDecimals(this->Decimals);
  51. spinBox->setDecimalsOption(this->DecimalsOption);
  52. spinBox->setSingleStep(this->SingleStep);
  53. spinBox->setMinimum(this->Minimum);
  54. spinBox->setMaximum(this->Maximum);
  55. spinBox->setValueProxy(this->Proxy.data());
  56. connect( spinBox, SIGNAL(valueChanged(double)),
  57. this, SLOT(updateCoordinate(double)));
  58. // Same number of decimals within the spinboxes.
  59. connect( spinBox, SIGNAL(decimalsChanged(int)),
  60. this, SLOT(setTemporaryDecimals(int)));
  61. this->layout()->addWidget(spinBox);
  62. }
  63. //------------------------------------------------------------------------------
  64. void ctkCoordinatesWidget::setDimension(int dim)
  65. {
  66. if (dim < 1)
  67. {
  68. return;
  69. }
  70. double* newPos = new double[dim];
  71. if (dim > this->Dimension)
  72. {
  73. memcpy(newPos, this->Coordinates, this->Dimension * sizeof(double));
  74. for (int i = this->Dimension; i < dim; ++i)
  75. {
  76. newPos[i] = 0.;
  77. this->addSpinBox();
  78. this->LastUserEditedCoordinates.push_back(i);
  79. }
  80. }
  81. else
  82. {
  83. memcpy(newPos, this->Coordinates, dim * sizeof(double));
  84. for (int i = this->Dimension - 1 ; i >= dim; --i)
  85. {
  86. QLayoutItem* item = this->layout()->takeAt(i);
  87. QWidget* widget = item ? item->widget() : 0;
  88. delete item;
  89. delete widget;
  90. this->LastUserEditedCoordinates.pop_back();
  91. }
  92. }
  93. delete [] this->Coordinates;
  94. this->Coordinates = newPos;
  95. this->Dimension = dim;
  96. this->updateGeometry();
  97. this->updateCoordinates();
  98. }
  99. //------------------------------------------------------------------------------
  100. int ctkCoordinatesWidget::dimension() const
  101. {
  102. return this->Dimension;
  103. }
  104. //------------------------------------------------------------------------------
  105. void ctkCoordinatesWidget::setMinimum(double min)
  106. {
  107. for (int i = 0; this->layout()->itemAt(i); ++i)
  108. {
  109. this->spinBox(i)->setMinimum(min);
  110. }
  111. this->Minimum = min;
  112. }
  113. //------------------------------------------------------------------------------
  114. double ctkCoordinatesWidget::minimum() const
  115. {
  116. return this->Minimum;
  117. }
  118. //------------------------------------------------------------------------------
  119. void ctkCoordinatesWidget::setMaximum(double max)
  120. {
  121. for (int i = 0; this->layout()->itemAt(i); ++i)
  122. {
  123. this->spinBox(i)->setMaximum(max);
  124. }
  125. this->Maximum = max;
  126. }
  127. //------------------------------------------------------------------------------
  128. double ctkCoordinatesWidget::maximum() const
  129. {
  130. return this->Maximum;
  131. }
  132. //------------------------------------------------------------------------------
  133. void ctkCoordinatesWidget::setRange(double min, double max)
  134. {
  135. for (int i = 0; this->layout()->itemAt(i); ++i)
  136. {
  137. this->spinBox(i)->setRange(min, max);
  138. }
  139. this->Minimum = min;
  140. this->Maximum = max;
  141. }
  142. //------------------------------------------------------------------------------
  143. void ctkCoordinatesWidget::setNormalized(bool normalized)
  144. {
  145. this->Normalized = normalized;
  146. if (this->Normalized)
  147. {
  148. double* normalizedCoordinates = new double[this->Dimension];
  149. memcpy(normalizedCoordinates, this->Coordinates, sizeof(double)*this->Dimension);
  150. ctkCoordinatesWidget::normalize(normalizedCoordinates, this->Dimension);
  151. this->setMinimum(-1.);
  152. this->setMaximum(1.);
  153. this->setCoordinates(normalizedCoordinates);
  154. delete [] normalizedCoordinates;
  155. }
  156. }
  157. //------------------------------------------------------------------------------
  158. bool ctkCoordinatesWidget::isNormalized() const
  159. {
  160. return this->Normalized;
  161. }
  162. //------------------------------------------------------------------------------
  163. void ctkCoordinatesWidget::setDecimals(int newDecimals)
  164. {
  165. this->Decimals = newDecimals;
  166. for (int i = 0; this->layout()->itemAt(i); ++i)
  167. {
  168. this->spinBox(i)->setDecimals(newDecimals);
  169. }
  170. }
  171. //------------------------------------------------------------------------------
  172. void ctkCoordinatesWidget::updateDecimals()
  173. {
  174. int maxDecimals = 0;
  175. for (int i = 0; this->layout()->itemAt(i); ++i)
  176. {
  177. maxDecimals = qMax(maxDecimals, this->spinBox(i)->decimals());
  178. }
  179. this->setTemporaryDecimals(maxDecimals);
  180. }
  181. //------------------------------------------------------------------------------
  182. void ctkCoordinatesWidget::setTemporaryDecimals(int newDecimals)
  183. {
  184. for (int i = 0; this->layout()->itemAt(i); ++i)
  185. {
  186. this->spinBox(i)->spinBox()->setDecimals(newDecimals);
  187. }
  188. }
  189. //------------------------------------------------------------------------------
  190. int ctkCoordinatesWidget::decimals() const
  191. {
  192. return this->Decimals;
  193. }
  194. // --------------------------------------------------------------------------
  195. ctkDoubleSpinBox::DecimalsOptions ctkCoordinatesWidget::decimalsOption()const
  196. {
  197. return this->DecimalsOption;
  198. }
  199. // --------------------------------------------------------------------------
  200. void ctkCoordinatesWidget
  201. ::setDecimalsOption(ctkDoubleSpinBox::DecimalsOptions newDecimalsOption)
  202. {
  203. for (int i = 0; this->layout()->itemAt(i); ++i)
  204. {
  205. this->spinBox(i)->setDecimalsOption(newDecimalsOption);
  206. }
  207. this->DecimalsOption = newDecimalsOption;
  208. }
  209. //------------------------------------------------------------------------------
  210. void ctkCoordinatesWidget::setSingleStep(double step)
  211. {
  212. for (int i = 0; this->layout()->itemAt(i); ++i)
  213. {
  214. this->spinBox(i)->setSingleStep(step);
  215. }
  216. this->SingleStep = step;
  217. }
  218. //------------------------------------------------------------------------------
  219. double ctkCoordinatesWidget::singleStep() const
  220. {
  221. return this->SingleStep;
  222. }
  223. //------------------------------------------------------------------------------
  224. void ctkCoordinatesWidget::setCoordinatesAsString(QString _pos)
  225. {
  226. QStringList posList = _pos.split(',');
  227. if (posList.count() != this->Dimension)
  228. {
  229. return;
  230. }
  231. double* newPos = new double[this->Dimension];
  232. for (int i = 0; i < this->Dimension; ++i)
  233. {
  234. newPos[i] = posList[i].toDouble();
  235. }
  236. this->setCoordinates(newPos);
  237. delete [] newPos;
  238. }
  239. //------------------------------------------------------------------------------
  240. QString ctkCoordinatesWidget::coordinatesAsString()const
  241. {
  242. QString res;
  243. for (int i = 0; i < this->Dimension; ++i)
  244. {
  245. if (i != 0)
  246. {
  247. res += ",";
  248. }
  249. res += QString::number(this->Coordinates[i]);
  250. }
  251. return res;
  252. }
  253. //------------------------------------------------------------------------------
  254. void ctkCoordinatesWidget::setCoordinates(double* coordinates)
  255. {
  256. for (int i = 0; i < this->Dimension; ++i)
  257. {
  258. this->Coordinates[i] = coordinates[i];
  259. }
  260. if (this->Normalized)
  261. {
  262. this->normalize(this->Coordinates, this->Dimension);
  263. }
  264. bool valuesModified = false;
  265. bool blocked = this->blockSignals(true);
  266. for (int i = 0; i < this->Dimension; ++i)
  267. {
  268. ctkDoubleSpinBox* spinbox = this->spinBox(i);
  269. if (spinbox)
  270. {
  271. // we don't want updateCoordinate() to be called.
  272. // it could mess with the LastUserEditedCoordinates list.
  273. bool spinBoxSignalWasBlocked = spinbox->blockSignals(true);
  274. if (spinbox->value() != this->Coordinates[i])
  275. {
  276. valuesModified = true;
  277. }
  278. // Still setValue needs to be called to recompute the number of decimals
  279. // if DecimalsByValue is set.
  280. spinbox->setValue(this->Coordinates[i]);
  281. spinbox->blockSignals(spinBoxSignalWasBlocked);
  282. }
  283. }
  284. this->blockSignals(blocked);
  285. this->updateDecimals();
  286. if (valuesModified)
  287. {
  288. this->updateCoordinates();
  289. }
  290. }
  291. //------------------------------------------------------------------------------
  292. void ctkCoordinatesWidget::setCoordinates(double x, double y, double z, double w)
  293. {
  294. double* coordinates = new double[this->Dimension];
  295. if (this->Dimension >= 1)
  296. {
  297. coordinates[0] = x;
  298. }
  299. if (this->Dimension >= 2)
  300. {
  301. coordinates[1] = y;
  302. }
  303. if (this->Dimension >= 3)
  304. {
  305. coordinates[2] = z;
  306. }
  307. if (this->Dimension >= 4)
  308. {
  309. coordinates[3] = w;
  310. }
  311. for (int i = 4; i < this->Dimension; ++i)
  312. {
  313. coordinates[i] = this->Coordinates[i];
  314. }
  315. this->setCoordinates(coordinates);
  316. delete [] coordinates;
  317. }
  318. //------------------------------------------------------------------------------
  319. double const * ctkCoordinatesWidget::coordinates()const
  320. {
  321. return this->Coordinates;
  322. }
  323. //------------------------------------------------------------------------------
  324. void ctkCoordinatesWidget::updateCoordinate(double coordinate)
  325. {
  326. int element = -1;
  327. for (int i = 0; i < this->Dimension; ++i)
  328. {
  329. if ( this->spinBox(i) && this->spinBox(i) == this->sender())
  330. {
  331. this->Coordinates[i] = coordinate;
  332. element = i;
  333. }
  334. }
  335. Q_ASSERT(element != -1);
  336. // Update the last edited history by push first the element.
  337. for (int i = this->Dimension -1; i > 0; --i)
  338. {
  339. if (this->LastUserEditedCoordinates[i] == element)
  340. {
  341. this->LastUserEditedCoordinates.swap(i,i-1);
  342. }
  343. }
  344. // What is the oldest coordinate to be edited
  345. int oldestElement = this->LastUserEditedCoordinates.last();
  346. if (this->isNormalized())
  347. {
  348. // We have to ensure the coordinates are normalized.
  349. double den = 0.;
  350. double squaredNorm = this->squaredNorm();
  351. // Old Values xx + yy + zz = 1
  352. // New values: x'x' + y'y' + z'z' = 1
  353. // Say we are changing y into y':
  354. // x'x' + z'z' = 1 - y'y'
  355. if (oldestElement != -1 &&
  356. this->Coordinates[oldestElement] != 0.0 &&
  357. squaredNorm != 0.0)
  358. {
  359. // 1) Normalize only with the oldest user edited value
  360. // The oldest element is z, that means we try to have
  361. // x = x' (so that the user doesn't loose the edit he just made on the
  362. // element (x) he edited before this one (y).
  363. // Let's pose a the coef to multiply z into z' that keeps the norm to 1
  364. // xx + z'z' = 1 - y'y' (because x = x')
  365. // xx + azaz = 1 - y'y' (because z' = az)
  366. // aa*zz = 1 - y'y' - xx
  367. // a = sqrt( (1 - y'y' - xx) / zz )
  368. den = (1. - (squaredNorm -
  369. this->Coordinates[oldestElement] *
  370. this->Coordinates[oldestElement])) /
  371. (this->Coordinates[oldestElement] *
  372. this->Coordinates[oldestElement]);
  373. if (den > 0.)
  374. {
  375. den = sqrt(den);
  376. }
  377. }
  378. // Maybe 1) failed, then give 2) a chance.
  379. if (den <= 0)
  380. {
  381. oldestElement = -1;
  382. }
  383. bool mult = true;
  384. if (oldestElement == -1)
  385. {
  386. // 2) Normalize with all the coordinates
  387. // Let's pose a the coef to multiply x into x' and z into z' that keeps
  388. // the norm to 1:
  389. // axax + azaz = 1 - y'y'
  390. // aa(xx + zz) = 1 - y'y'
  391. // a = sqrt( (1 - y'y') / (xx + zz) )
  392. squaredNorm -= coordinate * coordinate;
  393. if (squaredNorm != 0.0)
  394. {
  395. den = sqrt( (1. - coordinate * coordinate) / squaredNorm);
  396. }
  397. else if (this->Dimension > 1)
  398. {
  399. mult = false;
  400. den = sqrt((1. - coordinate*coordinate) / (this->Dimension - 1));
  401. }
  402. }
  403. // Normalize coordinates
  404. double* normalizedCoordinates = new double[this->Dimension];
  405. for (int i = 0; i < this->Dimension; ++i)
  406. {
  407. if ((i != element && oldestElement == -1) ||
  408. (i == oldestElement && oldestElement != -1))
  409. {
  410. normalizedCoordinates[i] = mult ? this->Coordinates[i] * den : den;
  411. }
  412. else
  413. {
  414. normalizedCoordinates[i] = this->Coordinates[i];
  415. }
  416. }
  417. this->setCoordinates(normalizedCoordinates);
  418. delete [] normalizedCoordinates;
  419. }
  420. else
  421. {
  422. emit coordinatesChanged(this->Coordinates);
  423. }
  424. }
  425. //------------------------------------------------------------------------------
  426. void ctkCoordinatesWidget::updateCoordinates()
  427. {
  428. for (int i = 0; i < this->Dimension; ++i)
  429. {
  430. this->Coordinates[i] = this->spinBox(i)->value();
  431. }
  432. emit coordinatesChanged(this->Coordinates);
  433. }
  434. //------------------------------------------------------------------------------
  435. void ctkCoordinatesWidget::normalize()
  436. {
  437. double* normalizedCoordinates = new double[this->Dimension];
  438. memcpy(normalizedCoordinates, this->Coordinates,
  439. sizeof(double) * this->Dimension);
  440. ctkCoordinatesWidget::normalize(normalizedCoordinates, this->Dimension);
  441. this->setCoordinates(normalizedCoordinates);
  442. delete [] normalizedCoordinates;
  443. }
  444. //------------------------------------------------------------------------------
  445. double ctkCoordinatesWidget::normalize(double* coordinates, int dimension)
  446. {
  447. double den = ctkCoordinatesWidget::norm( coordinates, dimension );
  448. if ( den != 0.0 )
  449. {
  450. for (int i = 0; i < dimension; ++i)
  451. {
  452. coordinates[i] /= den;
  453. }
  454. }
  455. return den;
  456. }
  457. //------------------------------------------------------------------------------
  458. double ctkCoordinatesWidget::norm()const
  459. {
  460. return ctkCoordinatesWidget::norm(this->Coordinates, this->Dimension);
  461. }
  462. //------------------------------------------------------------------------------
  463. double ctkCoordinatesWidget::norm(double* coordinates, int dimension)
  464. {
  465. return sqrt(ctkCoordinatesWidget::squaredNorm(coordinates, dimension));
  466. }
  467. //------------------------------------------------------------------------------
  468. double ctkCoordinatesWidget::squaredNorm()const
  469. {
  470. return ctkCoordinatesWidget::squaredNorm(this->Coordinates, this->Dimension);
  471. }
  472. //------------------------------------------------------------------------------
  473. double ctkCoordinatesWidget::squaredNorm(double* coordinates, int dimension)
  474. {
  475. double sum = 0.;
  476. for (int i = 0; i < dimension; ++i)
  477. {
  478. sum += coordinates[i] * coordinates[i];
  479. }
  480. return sum;
  481. }
  482. //----------------------------------------------------------------------------
  483. ctkDoubleSpinBox* ctkCoordinatesWidget::spinBox(int i)
  484. {
  485. QLayoutItem* item = this->layout()->itemAt(i);
  486. ctkDoubleSpinBox* spinBox =
  487. item ? qobject_cast<ctkDoubleSpinBox*>(item->widget()) : 0;
  488. return spinBox;
  489. }
  490. //----------------------------------------------------------------------------
  491. void ctkCoordinatesWidget::setValueProxy(ctkValueProxy* proxy)
  492. {
  493. if (this->Proxy.data() == proxy)
  494. {
  495. return;
  496. }
  497. this->onValueProxyAboutToBeModified();
  498. if (this->Proxy)
  499. {
  500. disconnect(this->Proxy.data(), SIGNAL(proxyAboutToBeModified()),
  501. this, SLOT(onValueProxyAboutToBeModified()));
  502. disconnect(this->Proxy.data(), SIGNAL(proxyModified()),
  503. this, SLOT(onValueProxyModified()));
  504. }
  505. this->Proxy = proxy;
  506. if (this->Proxy)
  507. {
  508. connect(this->Proxy.data(), SIGNAL(proxyAboutToBeModified()),
  509. this, SLOT(onValueProxyAboutToBeModified()));
  510. }
  511. for (int i = 0; i < this->Dimension; ++i)
  512. {
  513. this->spinBox(i)->setValueProxy(this->Proxy.data());
  514. }
  515. if (this->Proxy)
  516. {
  517. connect(this->Proxy.data(), SIGNAL(proxyModified()),
  518. this, SLOT(onValueProxyModified()));
  519. }
  520. this->onValueProxyModified();
  521. }
  522. //----------------------------------------------------------------------------
  523. ctkValueProxy* ctkCoordinatesWidget::valueProxy() const
  524. {
  525. return this->Proxy.data();
  526. }
  527. //----------------------------------------------------------------------------
  528. void ctkCoordinatesWidget::onValueProxyAboutToBeModified()
  529. {
  530. for (int i = 0; i < this->Dimension; ++i)
  531. {
  532. this->spinBox(i)->blockSignals(true);
  533. }
  534. }
  535. //----------------------------------------------------------------------------
  536. void ctkCoordinatesWidget::onValueProxyModified()
  537. {
  538. for (int i = 0; i < this->Dimension; ++i)
  539. {
  540. this->spinBox(i)->blockSignals(false);
  541. }
  542. // Only decimals (not range/nor value) may have change during a proxy
  543. // modification.
  544. this->updateDecimals();
  545. }