ctkCoordinatesWidget.cpp 14 KB

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