ctkCoordinatesWidget.cpp 12 KB

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