ctkCoordinatesWidget.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. QLayoutItem* item = this->layout()->itemAt(i);
  110. ctkDoubleSpinBox* spinBox = item ? qobject_cast<ctkDoubleSpinBox*>(
  111. item->widget()) : 0;
  112. if (spinBox)
  113. {
  114. spinBox->setMinimum(min);
  115. }
  116. }
  117. this->Minimum = min;
  118. }
  119. //------------------------------------------------------------------------------
  120. double ctkCoordinatesWidget::minimum() const
  121. {
  122. return this->Minimum;
  123. }
  124. //------------------------------------------------------------------------------
  125. void ctkCoordinatesWidget::setMaximum(double max)
  126. {
  127. for (int i = 0; this->layout()->itemAt(i); ++i)
  128. {
  129. QLayoutItem* item = this->layout()->itemAt(i);
  130. ctkDoubleSpinBox* spinBox = item ? qobject_cast<ctkDoubleSpinBox*>(
  131. item->widget()) : 0;
  132. if (spinBox)
  133. {
  134. spinBox->setMaximum(max);
  135. }
  136. }
  137. this->Maximum = max;
  138. }
  139. //------------------------------------------------------------------------------
  140. double ctkCoordinatesWidget::maximum() const
  141. {
  142. return this->Maximum;
  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; this->layout()->itemAt(i); ++i)
  169. {
  170. QLayoutItem* item = this->layout()->itemAt(i);
  171. ctkDoubleSpinBox* spinBox = item ? qobject_cast<ctkDoubleSpinBox*>(
  172. item->widget()) : 0;
  173. if (spinBox)
  174. {
  175. spinBox->setDecimals(newDecimals);
  176. }
  177. }
  178. }
  179. //------------------------------------------------------------------------------
  180. void ctkCoordinatesWidget::setTemporaryDecimals(int newDecimals)
  181. {
  182. for (int i = 0; this->layout()->itemAt(i); ++i)
  183. {
  184. QLayoutItem* item = this->layout()->itemAt(i);
  185. ctkDoubleSpinBox* spinBox = item ? qobject_cast<ctkDoubleSpinBox*>(
  186. item->widget()) : 0;
  187. if (spinBox)
  188. {
  189. spinBox->spinBox()->setDecimals(newDecimals);
  190. }
  191. }
  192. }
  193. //------------------------------------------------------------------------------
  194. int ctkCoordinatesWidget::decimals() const
  195. {
  196. return this->Decimals;
  197. }
  198. // --------------------------------------------------------------------------
  199. ctkDoubleSpinBox::DecimalsOptions ctkCoordinatesWidget::decimalsOption()const
  200. {
  201. return this->DecimalsOption;
  202. }
  203. // --------------------------------------------------------------------------
  204. void ctkCoordinatesWidget
  205. ::setDecimalsOption(ctkDoubleSpinBox::DecimalsOptions newDecimalsOption)
  206. {
  207. for (int i = 0; this->layout()->itemAt(i); ++i)
  208. {
  209. QLayoutItem* item = this->layout()->itemAt(i);
  210. ctkDoubleSpinBox* spinBox = item ? qobject_cast<ctkDoubleSpinBox*>(
  211. item->widget()) : 0;
  212. if (spinBox)
  213. {
  214. spinBox->setDecimalsOption(newDecimalsOption);
  215. }
  216. }
  217. this->DecimalsOption = newDecimalsOption;
  218. }
  219. //------------------------------------------------------------------------------
  220. void ctkCoordinatesWidget::setSingleStep(double step)
  221. {
  222. for (int i = 0; this->layout()->itemAt(i); ++i)
  223. {
  224. QLayoutItem* item = this->layout()->itemAt(i);
  225. ctkDoubleSpinBox* spinBox = item ? qobject_cast<ctkDoubleSpinBox*>(
  226. item->widget()) : 0;
  227. if (spinBox)
  228. {
  229. spinBox->setSingleStep(step);
  230. }
  231. }
  232. this->SingleStep = step;
  233. }
  234. //------------------------------------------------------------------------------
  235. double ctkCoordinatesWidget::singleStep() const
  236. {
  237. return this->SingleStep;
  238. }
  239. //------------------------------------------------------------------------------
  240. void ctkCoordinatesWidget::setCoordinatesAsString(QString _pos)
  241. {
  242. QStringList posList = _pos.split(',');
  243. if (posList.count() != this->Dimension)
  244. {
  245. return;
  246. }
  247. double* newPos = new double[this->Dimension];
  248. for (int i = 0; i < this->Dimension; ++i)
  249. {
  250. newPos[i] = posList[i].toDouble();
  251. }
  252. this->setCoordinates(newPos);
  253. delete [] newPos;
  254. }
  255. //------------------------------------------------------------------------------
  256. QString ctkCoordinatesWidget::coordinatesAsString()const
  257. {
  258. QString res;
  259. for (int i = 0; i < this->Dimension; ++i)
  260. {
  261. if (i != 0)
  262. {
  263. res += ",";
  264. }
  265. res += QString::number(this->Coordinates[i]);
  266. }
  267. return res;
  268. }
  269. //------------------------------------------------------------------------------
  270. void ctkCoordinatesWidget::setCoordinates(double* coordinates)
  271. {
  272. for (int i = 0; i < this->Dimension; ++i)
  273. {
  274. this->Coordinates[i] = coordinates[i];
  275. }
  276. if (this->Normalized)
  277. {
  278. this->normalize(this->Coordinates, this->Dimension);
  279. }
  280. bool valuesModified = false;
  281. int maxDecimals = 0;
  282. bool blocked = this->blockSignals(true);
  283. for (int i = 0; i < this->Dimension; ++i)
  284. {
  285. QLayoutItem* item = this->layout()->itemAt(i);
  286. ctkDoubleSpinBox* spinBox =
  287. item ? qobject_cast<ctkDoubleSpinBox*>(item->widget()) : 0;
  288. if (spinBox)
  289. {
  290. // we don't want updateCoordinate() to be called.
  291. // it could mess with the LastUserEditedCoordinates list.
  292. bool spinBoxSignalWasBlocked = spinBox->blockSignals(true);
  293. if (spinBox->value() != this->Coordinates[i])
  294. {
  295. valuesModified = true;
  296. }
  297. // Still setValue needs to be called to recompute the number of decimals
  298. // if DecimalsByValue is set.
  299. spinBox->setValue(this->Coordinates[i]);
  300. spinBox->blockSignals(spinBoxSignalWasBlocked);
  301. maxDecimals = qMax(maxDecimals, spinBox->decimals());
  302. }
  303. }
  304. this->blockSignals(blocked);
  305. this->setTemporaryDecimals(maxDecimals);
  306. if (valuesModified)
  307. {
  308. this->updateCoordinates();
  309. }
  310. }
  311. //------------------------------------------------------------------------------
  312. void ctkCoordinatesWidget::setCoordinates(double x, double y, double z, double w)
  313. {
  314. double* coordinates = new double[this->Dimension];
  315. if (this->Dimension >= 1)
  316. {
  317. coordinates[0] = x;
  318. }
  319. if (this->Dimension >= 2)
  320. {
  321. coordinates[1] = y;
  322. }
  323. if (this->Dimension >= 3)
  324. {
  325. coordinates[2] = z;
  326. }
  327. if (this->Dimension >= 4)
  328. {
  329. coordinates[3] = w;
  330. }
  331. for (int i = 4; i < this->Dimension; ++i)
  332. {
  333. coordinates[i] = this->Coordinates[i];
  334. }
  335. this->setCoordinates(coordinates);
  336. delete [] coordinates;
  337. }
  338. //------------------------------------------------------------------------------
  339. double const * ctkCoordinatesWidget::coordinates()const
  340. {
  341. return this->Coordinates;
  342. }
  343. //------------------------------------------------------------------------------
  344. void ctkCoordinatesWidget::updateCoordinate(double coordinate)
  345. {
  346. int element = -1;
  347. for (int i = 0; i < this->Dimension; ++i)
  348. {
  349. QLayoutItem* item = this->layout()->itemAt(i);
  350. ctkDoubleSpinBox* spinBox =
  351. item ? qobject_cast<ctkDoubleSpinBox*>(item->widget()) : 0;
  352. if ( spinBox && spinBox == this->sender())
  353. {
  354. this->Coordinates[i] = coordinate;
  355. element = i;
  356. }
  357. }
  358. Q_ASSERT(element != -1);
  359. // Update the last edited history by push first the element.
  360. for (int i = this->Dimension -1; i > 0; --i)
  361. {
  362. if (this->LastUserEditedCoordinates[i] == element)
  363. {
  364. this->LastUserEditedCoordinates.swap(i,i-1);
  365. }
  366. }
  367. // What is the oldest coordinate to be edited
  368. int oldestElement = this->LastUserEditedCoordinates.last();
  369. if (this->isNormalized())
  370. {
  371. // We have to ensure the coordinates are normalized.
  372. double den = 0.;
  373. double squaredNorm = this->squaredNorm();
  374. // Old Values xx + yy + zz = 1
  375. // New values: x'x' + y'y' + z'z' = 1
  376. // Say we are changing y into y':
  377. // x'x' + z'z' = 1 - y'y'
  378. if (oldestElement != -1 &&
  379. this->Coordinates[oldestElement] != 0.0 &&
  380. squaredNorm != 0.0)
  381. {
  382. // 1) Normalize only with the oldest user edited value
  383. // The oldest element is z, that means we try to have
  384. // x = x' (so that the user doesn't loose the edit he just made on the
  385. // element (x) he edited before this one (y).
  386. // Let's pose a the coef to multiply z into z' that keeps the norm to 1
  387. // xx + z'z' = 1 - y'y' (because x = x')
  388. // xx + azaz = 1 - y'y' (because z' = az)
  389. // aa*zz = 1 - y'y' - xx
  390. // a = sqrt( (1 - y'y' - xx) / zz )
  391. den = (1. - (squaredNorm -
  392. this->Coordinates[oldestElement] *
  393. this->Coordinates[oldestElement])) /
  394. (this->Coordinates[oldestElement] *
  395. this->Coordinates[oldestElement]);
  396. if (den > 0.)
  397. {
  398. den = sqrt(den);
  399. }
  400. }
  401. // Maybe 1) failed, then give 2) a chance.
  402. if (den <= 0)
  403. {
  404. oldestElement = -1;
  405. }
  406. bool mult = true;
  407. if (oldestElement == -1)
  408. {
  409. // 2) Normalize with all the coordinates
  410. // Let's pose a the coef to multiply x into x' and z into z' that keeps
  411. // the norm to 1:
  412. // axax + azaz = 1 - y'y'
  413. // aa(xx + zz) = 1 - y'y'
  414. // a = sqrt( (1 - y'y') / (xx + zz) )
  415. squaredNorm -= coordinate * coordinate;
  416. if (squaredNorm != 0.0)
  417. {
  418. den = sqrt( (1. - coordinate * coordinate) / squaredNorm);
  419. }
  420. else if (this->Dimension > 1)
  421. {
  422. mult = false;
  423. den = sqrt((1. - coordinate*coordinate) / (this->Dimension - 1));
  424. }
  425. }
  426. // Normalize coordinates
  427. double* normalizedCoordinates = new double[this->Dimension];
  428. for (int i = 0; i < this->Dimension; ++i)
  429. {
  430. if ((i != element && oldestElement == -1) ||
  431. (i == oldestElement && oldestElement != -1))
  432. {
  433. normalizedCoordinates[i] = mult ? this->Coordinates[i] * den : den;
  434. }
  435. else
  436. {
  437. normalizedCoordinates[i] = this->Coordinates[i];
  438. }
  439. }
  440. this->setCoordinates(normalizedCoordinates);
  441. delete [] normalizedCoordinates;
  442. }
  443. else
  444. {
  445. emit coordinatesChanged(this->Coordinates);
  446. }
  447. }
  448. //------------------------------------------------------------------------------
  449. void ctkCoordinatesWidget::updateCoordinates()
  450. {
  451. for (int i = 0; i < this->Dimension; ++i)
  452. {
  453. QLayoutItem* item = this->layout()->itemAt(i);
  454. ctkDoubleSpinBox* spinBox =
  455. item ? qobject_cast<ctkDoubleSpinBox*>(item->widget()) : 0;
  456. if ( spinBox)
  457. {
  458. this->Coordinates[i] = spinBox->value();
  459. }
  460. }
  461. emit coordinatesChanged(this->Coordinates);
  462. }
  463. //------------------------------------------------------------------------------
  464. void ctkCoordinatesWidget::normalize()
  465. {
  466. double* normalizedCoordinates = new double[this->Dimension];
  467. memcpy(normalizedCoordinates, this->Coordinates,
  468. sizeof(double) * this->Dimension);
  469. ctkCoordinatesWidget::normalize(normalizedCoordinates, this->Dimension);
  470. this->setCoordinates(normalizedCoordinates);
  471. delete [] normalizedCoordinates;
  472. }
  473. //------------------------------------------------------------------------------
  474. double ctkCoordinatesWidget::normalize(double* coordinates, int dimension)
  475. {
  476. double den = ctkCoordinatesWidget::norm( coordinates, dimension );
  477. if ( den != 0.0 )
  478. {
  479. for (int i = 0; i < dimension; ++i)
  480. {
  481. coordinates[i] /= den;
  482. }
  483. }
  484. return den;
  485. }
  486. //------------------------------------------------------------------------------
  487. double ctkCoordinatesWidget::norm()const
  488. {
  489. return ctkCoordinatesWidget::norm(this->Coordinates, this->Dimension);
  490. }
  491. //------------------------------------------------------------------------------
  492. double ctkCoordinatesWidget::norm(double* coordinates, int dimension)
  493. {
  494. return sqrt(ctkCoordinatesWidget::squaredNorm(coordinates, dimension));
  495. }
  496. //------------------------------------------------------------------------------
  497. double ctkCoordinatesWidget::squaredNorm()const
  498. {
  499. return ctkCoordinatesWidget::squaredNorm(this->Coordinates, this->Dimension);
  500. }
  501. //------------------------------------------------------------------------------
  502. double ctkCoordinatesWidget::squaredNorm(double* coordinates, int dimension)
  503. {
  504. double sum = 0.;
  505. for (int i = 0; i < dimension; ++i)
  506. {
  507. sum += coordinates[i] * coordinates[i];
  508. }
  509. return sum;
  510. }
  511. //----------------------------------------------------------------------------
  512. void ctkCoordinatesWidget::setValueProxy(ctkValueProxy* proxy)
  513. {
  514. if (this->Proxy.data() == proxy)
  515. {
  516. return;
  517. }
  518. this->Proxy = proxy;
  519. for (int i = 0; i < this->Dimension; ++i)
  520. {
  521. QLayoutItem* item = this->layout()->itemAt(i);
  522. ctkDoubleSpinBox* spinBox =
  523. item ? qobject_cast<ctkDoubleSpinBox*>(item->widget()) : 0;
  524. if ( spinBox)
  525. {
  526. spinBox->setValueProxy(this->Proxy.data());
  527. }
  528. }
  529. }
  530. //----------------------------------------------------------------------------
  531. ctkValueProxy* ctkCoordinatesWidget::valueProxy() const
  532. {
  533. return this->Proxy.data();
  534. }