ctkCoordinatesWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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[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. }
  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. spinBox->setValue(this->Coordinates[i]);
  245. }
  246. }
  247. this->blockSignals(blocked);
  248. this->updateCoordinates();
  249. }
  250. //------------------------------------------------------------------------------
  251. void ctkCoordinatesWidget::setCoordinates(double x, double y, double z, double w)
  252. {
  253. double coordinates[this->Dimension];
  254. if (this->Dimension >= 1)
  255. {
  256. coordinates[0] = x;
  257. }
  258. if (this->Dimension >= 2)
  259. {
  260. coordinates[1] = y;
  261. }
  262. if (this->Dimension >= 3)
  263. {
  264. coordinates[2] = z;
  265. }
  266. if (this->Dimension >= 4)
  267. {
  268. coordinates[3] = w;
  269. }
  270. for (int i = 4; i < this->Dimension; ++i)
  271. {
  272. coordinates[i] = this->Coordinates[i];
  273. }
  274. this->setCoordinates(coordinates);
  275. }
  276. //------------------------------------------------------------------------------
  277. double const * ctkCoordinatesWidget::coordinates()const
  278. {
  279. return this->Coordinates;
  280. }
  281. //------------------------------------------------------------------------------
  282. void ctkCoordinatesWidget::updateCoordinate(double coordinate)
  283. {
  284. double den = 0.;
  285. int element = -1;
  286. for (int i = 0; i < this->Dimension; ++i)
  287. {
  288. QLayoutItem* item = this->layout()->itemAt(i);
  289. QDoubleSpinBox* spinBox =
  290. item ? qobject_cast<QDoubleSpinBox*>(item->widget()) : 0;
  291. if ( spinBox && spinBox == this->sender())
  292. {
  293. this->Coordinates[i] = coordinate;
  294. element = i;
  295. }
  296. else
  297. {
  298. den += this->Coordinates[i]*this->Coordinates[i];
  299. }
  300. }
  301. Q_ASSERT(element != -1);
  302. if (this->isNormalized())
  303. {
  304. // Old Values xx + yy + zz = 1
  305. // New values: x'x' + y'y' + z'z' = 1
  306. // Say we are changing y into y':
  307. // x'x' + z'z' = 1 - y'y'
  308. // Let's pose a the coef to multiply x into x' that keeps the norm to 1:
  309. // axax + azaz = 1 - y'y'
  310. // aa(xx + zz) = 1 - y'y'
  311. // a = sqrt( (1 - y'y') / (xx + zz) )
  312. bool mult = true;
  313. if (den != 0.0)
  314. {
  315. mult = true;
  316. den = sqrt( (1. - coordinate * coordinate) / den);
  317. }
  318. else if (this->Dimension > 1)
  319. {
  320. mult = false;
  321. den = sqrt((1. - coordinate*coordinate) / (this->Dimension - 1));
  322. }
  323. double normalizedCoordinates[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. }
  337. else
  338. {
  339. emit coordinatesChanged(this->Coordinates);
  340. }
  341. }
  342. //------------------------------------------------------------------------------
  343. void ctkCoordinatesWidget::updateCoordinates()
  344. {
  345. for (int i = 0; i < this->Dimension; ++i)
  346. {
  347. QLayoutItem* item = this->layout()->itemAt(i);
  348. QDoubleSpinBox* spinBox =
  349. item ? qobject_cast<QDoubleSpinBox*>(item->widget()) : 0;
  350. if ( spinBox)
  351. {
  352. this->Coordinates[i] = spinBox->value();
  353. }
  354. }
  355. emit coordinatesChanged(this->Coordinates);
  356. }
  357. //------------------------------------------------------------------------------
  358. void ctkCoordinatesWidget::normalize()
  359. {
  360. double normalizedCoordinates[this->Dimension];
  361. memcpy(normalizedCoordinates, this->Coordinates,
  362. sizeof(double) * this->Dimension);
  363. ctkCoordinatesWidget::normalize(normalizedCoordinates, this->Dimension);
  364. this->setCoordinates(normalizedCoordinates);
  365. }
  366. //------------------------------------------------------------------------------
  367. double ctkCoordinatesWidget::normalize(double* coordinates, int dimension)
  368. {
  369. double den = ctkCoordinatesWidget::norm( coordinates, dimension );
  370. if ( den != 0.0 )
  371. {
  372. for (int i = 0; i < dimension; ++i)
  373. {
  374. coordinates[i] /= den;
  375. }
  376. }
  377. return den;
  378. }
  379. //------------------------------------------------------------------------------
  380. double ctkCoordinatesWidget::norm(double* coordinates, int dimension)
  381. {
  382. double sum = 0.;
  383. for (int i = 0; i < dimension; ++i)
  384. {
  385. sum += coordinates[i] * coordinates[i];
  386. }
  387. return sqrt(sum);
  388. }