ctkCoordinatesWidget.cpp 16 KB

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