ctkCoordinatesWidget.cpp 18 KB

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