ctkCoordinatesWidget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. }
  70. }
  71. else
  72. {
  73. memcpy(newPos, this->Coordinates, dim * sizeof(double));
  74. for (int i = this->Dimension - 1 ; i >= dim; --i)
  75. {
  76. QLayoutItem* item = this->layout()->takeAt(i);
  77. QWidget* widget = item ? item->widget() : 0;
  78. delete item;
  79. delete widget;
  80. }
  81. }
  82. delete [] this->Coordinates;
  83. this->Coordinates = newPos;
  84. this->Dimension = dim;
  85. this->updateGeometry();
  86. this->updateCoordinates();
  87. }
  88. //------------------------------------------------------------------------------
  89. int ctkCoordinatesWidget::dimension() const
  90. {
  91. return this->Dimension;
  92. }
  93. //------------------------------------------------------------------------------
  94. void ctkCoordinatesWidget::setMinimum(double min)
  95. {
  96. for (int i = 0; this->layout()->itemAt(i); ++i)
  97. {
  98. QLayoutItem* item = this->layout()->itemAt(i);
  99. QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
  100. item->widget()) : 0;
  101. if (spinBox)
  102. {
  103. spinBox->setMinimum(min);
  104. }
  105. }
  106. this->Minimum = min;
  107. }
  108. //------------------------------------------------------------------------------
  109. double ctkCoordinatesWidget::minimum() const
  110. {
  111. return this->Minimum;
  112. }
  113. //------------------------------------------------------------------------------
  114. void ctkCoordinatesWidget::setMaximum(double max)
  115. {
  116. for (int i = 0; this->layout()->itemAt(i); ++i)
  117. {
  118. QLayoutItem* item = this->layout()->itemAt(i);
  119. QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
  120. item->widget()) : 0;
  121. if (spinBox)
  122. {
  123. spinBox->setMaximum(max);
  124. }
  125. }
  126. this->Maximum = max;
  127. }
  128. //------------------------------------------------------------------------------
  129. double ctkCoordinatesWidget::maximum() const
  130. {
  131. return this->Maximum;
  132. }
  133. //------------------------------------------------------------------------------
  134. void ctkCoordinatesWidget::setNormalized(bool normalized)
  135. {
  136. this->Normalized = normalized;
  137. if (this->Normalized)
  138. {
  139. double* normalizedCoordinates = new double[this->Dimension];
  140. memcpy(normalizedCoordinates, this->Coordinates, sizeof(double)*this->Dimension);
  141. ctkCoordinatesWidget::normalize(normalizedCoordinates, this->Dimension);
  142. this->setMinimum(-1.);
  143. this->setMaximum(1.);
  144. this->setCoordinates(normalizedCoordinates);
  145. delete [] normalizedCoordinates;
  146. }
  147. }
  148. //------------------------------------------------------------------------------
  149. bool ctkCoordinatesWidget::isNormalized() const
  150. {
  151. return this->Normalized;
  152. }
  153. //------------------------------------------------------------------------------
  154. void ctkCoordinatesWidget::setDecimals(int newDecimals)
  155. {
  156. this->Decimals = newDecimals;
  157. for (int i = 0; this->layout()->itemAt(i); ++i)
  158. {
  159. QLayoutItem* item = this->layout()->itemAt(i);
  160. QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
  161. item->widget()) : 0;
  162. if (spinBox)
  163. {
  164. spinBox->setDecimals(newDecimals);
  165. }
  166. }
  167. }
  168. //------------------------------------------------------------------------------
  169. int ctkCoordinatesWidget::decimals() const
  170. {
  171. return this->Decimals;
  172. }
  173. //------------------------------------------------------------------------------
  174. void ctkCoordinatesWidget::setSingleStep(double step)
  175. {
  176. for (int i = 0; this->layout()->itemAt(i); ++i)
  177. {
  178. QLayoutItem* item = this->layout()->itemAt(i);
  179. QDoubleSpinBox* spinBox = item ? dynamic_cast<QDoubleSpinBox*>(
  180. item->widget()) : 0;
  181. if (spinBox)
  182. {
  183. spinBox->setSingleStep(step);
  184. }
  185. }
  186. this->SingleStep = step;
  187. }
  188. //------------------------------------------------------------------------------
  189. double ctkCoordinatesWidget::singleStep() const
  190. {
  191. return this->SingleStep;
  192. }
  193. //------------------------------------------------------------------------------
  194. void ctkCoordinatesWidget::setCoordinatesAsString(QString _pos)
  195. {
  196. QStringList posList = _pos.split(',');
  197. if (posList.count() != this->Dimension)
  198. {
  199. return;
  200. }
  201. double* newPos = new double[this->Dimension];
  202. for (int i = 0; i < this->Dimension; ++i)
  203. {
  204. newPos[i] = posList[i].toDouble();
  205. }
  206. this->setCoordinates(newPos);
  207. delete [] newPos;
  208. }
  209. //------------------------------------------------------------------------------
  210. QString ctkCoordinatesWidget::coordinatesAsString()const
  211. {
  212. QString res;
  213. for (int i = 0; i < this->Dimension; ++i)
  214. {
  215. if (i != 0)
  216. {
  217. res += ",";
  218. }
  219. res += QString::number(this->Coordinates[i]);
  220. }
  221. return res;
  222. }
  223. //------------------------------------------------------------------------------
  224. void ctkCoordinatesWidget::setCoordinates(double* coordinates)
  225. {
  226. for (int i = 0; i < this->Dimension; ++i)
  227. {
  228. this->Coordinates[i] = coordinates[i];
  229. }
  230. if (this->Normalized)
  231. {
  232. this->normalize(this->Coordinates, this->Dimension);
  233. }
  234. bool blocked = this->blockSignals(true);
  235. for (int i = 0; i < this->Dimension; ++i)
  236. {
  237. QLayoutItem* item = this->layout()->itemAt(i);
  238. QDoubleSpinBox* spinBox =
  239. item ? dynamic_cast<QDoubleSpinBox*>(item->widget()) : 0;
  240. if (spinBox)
  241. {
  242. spinBox->setValue(this->Coordinates[i]);
  243. }
  244. }
  245. this->blockSignals(blocked);
  246. this->updateCoordinates();
  247. }
  248. //------------------------------------------------------------------------------
  249. void ctkCoordinatesWidget::setCoordinates(double x, double y, double z, double w)
  250. {
  251. double* coordinates = new double[this->Dimension];
  252. if (this->Dimension >= 1)
  253. {
  254. coordinates[0] = x;
  255. }
  256. if (this->Dimension >= 2)
  257. {
  258. coordinates[1] = y;
  259. }
  260. if (this->Dimension >= 3)
  261. {
  262. coordinates[2] = z;
  263. }
  264. if (this->Dimension >= 4)
  265. {
  266. coordinates[3] = w;
  267. }
  268. for (int i = 4; i < this->Dimension; ++i)
  269. {
  270. coordinates[i] = this->Coordinates[i];
  271. }
  272. this->setCoordinates(coordinates);
  273. delete [] coordinates;
  274. }
  275. //------------------------------------------------------------------------------
  276. double const * ctkCoordinatesWidget::coordinates()const
  277. {
  278. return this->Coordinates;
  279. }
  280. //------------------------------------------------------------------------------
  281. void ctkCoordinatesWidget::updateCoordinate(double coordinate)
  282. {
  283. double den = 0.;
  284. int element = -1;
  285. for (int i = 0; i < this->Dimension; ++i)
  286. {
  287. QLayoutItem* item = this->layout()->itemAt(i);
  288. QDoubleSpinBox* spinBox =
  289. item ? qobject_cast<QDoubleSpinBox*>(item->widget()) : 0;
  290. if ( spinBox && spinBox == this->sender())
  291. {
  292. this->Coordinates[i] = coordinate;
  293. element = i;
  294. }
  295. else
  296. {
  297. den += this->Coordinates[i]*this->Coordinates[i];
  298. }
  299. }
  300. Q_ASSERT(element != -1);
  301. if (this->isNormalized())
  302. {
  303. // Old Values xx + yy + zz = 1
  304. // New values: x'x' + y'y' + z'z' = 1
  305. // Say we are changing y into y':
  306. // x'x' + z'z' = 1 - y'y'
  307. // Let's pose a the coef to multiply x into x' that keeps the norm to 1:
  308. // axax + azaz = 1 - y'y'
  309. // aa(xx + zz) = 1 - y'y'
  310. // a = sqrt( (1 - y'y') / (xx + zz) )
  311. bool mult = true;
  312. if (den != 0.0)
  313. {
  314. mult = true;
  315. den = sqrt( (1. - coordinate * coordinate) / den);
  316. }
  317. else if (this->Dimension > 1)
  318. {
  319. mult = false;
  320. den = sqrt((1. - coordinate*coordinate) / (this->Dimension - 1));
  321. }
  322. // Normalize coordinates
  323. double* normalizedCoordinates = new double[this->Dimension];
  324. for (int i = 0; i < this->Dimension; ++i)
  325. {
  326. if (i != element)
  327. {
  328. normalizedCoordinates[i] = mult ? this->Coordinates[i] * den : den;
  329. }
  330. else
  331. {
  332. normalizedCoordinates[i] = this->Coordinates[i];
  333. }
  334. }
  335. this->setCoordinates(normalizedCoordinates);
  336. delete [] normalizedCoordinates;
  337. }
  338. else
  339. {
  340. emit coordinatesChanged(this->Coordinates);
  341. }
  342. }
  343. //------------------------------------------------------------------------------
  344. void ctkCoordinatesWidget::updateCoordinates()
  345. {
  346. for (int i = 0; i < this->Dimension; ++i)
  347. {
  348. QLayoutItem* item = this->layout()->itemAt(i);
  349. QDoubleSpinBox* spinBox =
  350. item ? qobject_cast<QDoubleSpinBox*>(item->widget()) : 0;
  351. if ( spinBox)
  352. {
  353. this->Coordinates[i] = spinBox->value();
  354. }
  355. }
  356. emit coordinatesChanged(this->Coordinates);
  357. }
  358. //------------------------------------------------------------------------------
  359. void ctkCoordinatesWidget::normalize()
  360. {
  361. double* normalizedCoordinates = new double[this->Dimension];
  362. memcpy(normalizedCoordinates, this->Coordinates,
  363. sizeof(double) * this->Dimension);
  364. ctkCoordinatesWidget::normalize(normalizedCoordinates, this->Dimension);
  365. this->setCoordinates(normalizedCoordinates);
  366. delete [] normalizedCoordinates;
  367. }
  368. //------------------------------------------------------------------------------
  369. double ctkCoordinatesWidget::normalize(double* coordinates, int dimension)
  370. {
  371. double den = ctkCoordinatesWidget::norm( coordinates, dimension );
  372. if ( den != 0.0 )
  373. {
  374. for (int i = 0; i < dimension; ++i)
  375. {
  376. coordinates[i] /= den;
  377. }
  378. }
  379. return den;
  380. }
  381. //------------------------------------------------------------------------------
  382. double ctkCoordinatesWidget::norm()const
  383. {
  384. return ctkCoordinatesWidget::norm(this->Coordinates, this->Dimension);
  385. }
  386. //------------------------------------------------------------------------------
  387. double ctkCoordinatesWidget::norm(double* coordinates, int dimension)
  388. {
  389. return sqrt(ctkCoordinatesWidget::squaredNorm(coordinates, dimension));
  390. }
  391. //------------------------------------------------------------------------------
  392. double ctkCoordinatesWidget::squaredNorm()const
  393. {
  394. return ctkCoordinatesWidget::squaredNorm(this->Coordinates, this->Dimension);
  395. }
  396. //------------------------------------------------------------------------------
  397. double ctkCoordinatesWidget::squaredNorm(double* coordinates, int dimension)
  398. {
  399. double sum = 0.;
  400. for (int i = 0; i < dimension; ++i)
  401. {
  402. sum += coordinates[i] * coordinates[i];
  403. }
  404. return sum;
  405. }