ctkCoordinatesWidget.cpp 14 KB

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