ctkCoordinatesWidget.cpp 15 KB

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