ctkVTKThresholdWidget.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. // CTK includes
  17. #include "ctkLogger.h"
  18. #include "ctkVTKThresholdWidget.h"
  19. #include "ctkUtils.h"
  20. #include "ui_ctkVTKThresholdWidget.h"
  21. // VTK includes
  22. #include <vtkPiecewiseFunction.h>
  23. //----------------------------------------------------------------------------
  24. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKThresholdWidget");
  25. //----------------------------------------------------------------------------
  26. class ctkVTKThresholdWidgetPrivate:
  27. public Ui_ctkVTKThresholdWidget
  28. {
  29. Q_DECLARE_PUBLIC(ctkVTKThresholdWidget);
  30. protected:
  31. ctkVTKThresholdWidget* const q_ptr;
  32. public:
  33. ctkVTKThresholdWidgetPrivate(ctkVTKThresholdWidget& object);
  34. void setupUi(QWidget* widget);
  35. void setThreshold(double min, double max, double opacity);
  36. void setRange(double min, double max);
  37. void guessThreshold(double& min, double& max, double& opacity)const;
  38. vtkPiecewiseFunction* PiecewiseFunction;
  39. bool UserRange;
  40. bool UseSharpness;
  41. protected:
  42. void setNodes(double nodeValues[4][4]);
  43. void setNodes(double nodeValues[][4], const int nodeCount);
  44. void setNodeValue(int index, double* nodeValue);
  45. double safeX(double value, double& previous)const;
  46. static double nextHigher(double value);
  47. };
  48. // ----------------------------------------------------------------------------
  49. // ctkVTKThresholdWidgetPrivate methods
  50. // ----------------------------------------------------------------------------
  51. ctkVTKThresholdWidgetPrivate::ctkVTKThresholdWidgetPrivate(
  52. ctkVTKThresholdWidget& object)
  53. : q_ptr(&object)
  54. {
  55. this->PiecewiseFunction = 0;
  56. this->UserRange = false;
  57. this->UseSharpness = false;
  58. }
  59. // ----------------------------------------------------------------------------
  60. void ctkVTKThresholdWidgetPrivate::setupUi(QWidget* widget)
  61. {
  62. Q_Q(ctkVTKThresholdWidget);
  63. Q_ASSERT(q == widget);
  64. this->Ui_ctkVTKThresholdWidget::setupUi(widget);
  65. QObject::connect(this->ThresholdSliderWidget, SIGNAL(valuesChanged(double,double)),
  66. q, SLOT(setThresholdValues(double,double)));
  67. QObject::connect(this->OpacitySliderWidget, SIGNAL(valueChanged(double)),
  68. q, SLOT(setOpacity(double)));
  69. }
  70. // ----------------------------------------------------------------------------
  71. void ctkVTKThresholdWidgetPrivate::guessThreshold(double& min, double& max, double& opacity)const
  72. {
  73. min = this->ThresholdSliderWidget->minimum();
  74. max = this->ThresholdSliderWidget->maximum();
  75. opacity = 1.;
  76. if (!this->PiecewiseFunction || this->PiecewiseFunction->GetSize() == 0)
  77. {
  78. return;
  79. }
  80. int minIndex;
  81. // The min index is the point before the opacity is >0
  82. for (minIndex=0; minIndex < this->PiecewiseFunction->GetSize(); ++minIndex)
  83. {
  84. double node[4];
  85. this->PiecewiseFunction->GetNodeValue(minIndex, node);
  86. if (node[1] > 0.)
  87. {
  88. opacity = node[1];
  89. max = node[0];
  90. break;
  91. }
  92. min = node[0];
  93. }
  94. if (minIndex == this->PiecewiseFunction->GetSize())
  95. {
  96. return;
  97. }
  98. int maxIndex = minIndex + 1;
  99. for (;maxIndex < this->PiecewiseFunction->GetSize(); ++maxIndex)
  100. {
  101. double node[4];
  102. this->PiecewiseFunction->GetNodeValue(maxIndex, node);
  103. // use the max opacity
  104. opacity = std::max(opacity, node[1]);
  105. if (node[1] < opacity)
  106. {
  107. break;
  108. }
  109. max = node[0];
  110. }
  111. }
  112. // ----------------------------------------------------------------------------
  113. void ctkVTKThresholdWidgetPrivate::setRange(double min, double max)
  114. {
  115. bool wasBlocking = this->ThresholdSliderWidget->blockSignals(true);
  116. int decimals = qMax(0, -ctk::orderOfMagnitude(max - min) + 2);
  117. this->ThresholdSliderWidget->setDecimals(decimals);
  118. this->ThresholdSliderWidget->setSingleStep(pow(10., -decimals));
  119. this->ThresholdSliderWidget->setRange(min, max);
  120. this->ThresholdSliderWidget->blockSignals(wasBlocking);
  121. }
  122. //----------------------------------------------------------------------------
  123. double ctkVTKThresholdWidgetPrivate::nextHigher(double value)
  124. {
  125. // Increment the value by the smallest offset possible
  126. typedef union {
  127. long long i64;
  128. double d64;
  129. } dbl_64;
  130. dbl_64 d;
  131. d.d64 = value;
  132. d.i64 += (value >= 0) ? 1 : -1;
  133. return d.d64;
  134. }
  135. // ----------------------------------------------------------------------------
  136. double ctkVTKThresholdWidgetPrivate::safeX(double value, double& previous)const
  137. {
  138. if (value < previous)
  139. {
  140. value = previous;
  141. }
  142. if (value == previous && !this->PiecewiseFunction->GetAllowDuplicateScalars())
  143. {
  144. value = this->nextHigher(value);
  145. }
  146. previous = value;
  147. return value;
  148. }
  149. // ----------------------------------------------------------------------------
  150. void ctkVTKThresholdWidgetPrivate::setThreshold(double min, double max, double opacity)
  151. {
  152. Q_Q(ctkVTKThresholdWidget);
  153. if (!this->PiecewiseFunction)
  154. {
  155. return;
  156. }
  157. double range[2];
  158. this->ThresholdSliderWidget->range(range);
  159. int nodeCount = 0;
  160. if (this->UseSharpness)
  161. {
  162. // +------2
  163. // | |
  164. // 0------1 +----3
  165. nodeCount = 4;
  166. }
  167. else
  168. {
  169. // 2------3
  170. // | |
  171. // 0------1 4----5
  172. nodeCount = 6;
  173. }
  174. double (*nodes)[4] = new double[nodeCount][4];
  175. double previous = VTK_DOUBLE_MIN;
  176. // Start of the curve, always y=0
  177. int index = 0;
  178. nodes[index][0] = this->safeX(range[0], previous);
  179. nodes[index][1] = 0.;
  180. nodes[index][2] = 0.5; // midpoint
  181. nodes[index][3] = 0.; // sharpness
  182. ++index;
  183. // Starting threshold point with a sharp slope that jumps to the opacity
  184. // which is set by the next point
  185. nodes[index][0] = this->safeX(min, previous);
  186. nodes[index][1] = 0.;
  187. nodes[index][2] = this->UseSharpness ? 0. : 0.5;
  188. nodes[index][3] = this->UseSharpness ? 1. : 0.;
  189. ++index;
  190. if (!this->UseSharpness)
  191. {
  192. nodes[index][0] = this->safeX(min, previous);
  193. nodes[index][1] = opacity;
  194. nodes[index][2] = 0.5;
  195. nodes[index][3] = 0.;
  196. ++index;
  197. }
  198. // Ending threshold point with a sharp slope that jumps back to a 0 opacity
  199. nodes[index][0] = this->safeX(max, previous);;
  200. nodes[index][1] = opacity;
  201. nodes[index][2] = this->UseSharpness ? 0. : 0.5;
  202. nodes[index][3] = this->UseSharpness ? 1. : 0.;
  203. ++index;
  204. if (!this->UseSharpness)
  205. {
  206. nodes[index][0] = this->safeX(max, previous);
  207. nodes[index][1] = 0.;
  208. nodes[index][2] = 0.5;
  209. nodes[index][3] = 0.;
  210. ++index;
  211. }
  212. // End of the curve, always y = 0
  213. nodes[index][0] = this->safeX(range[1], previous);
  214. nodes[index][1] = 0.;
  215. nodes[index][2] = 0.5;
  216. nodes[index][3] = 0.;
  217. ++index;
  218. q->qvtkBlock(this->PiecewiseFunction, vtkCommand::ModifiedEvent, q);
  219. this->setNodes(nodes, nodeCount);
  220. q->qvtkUnblock(this->PiecewiseFunction, vtkCommand::ModifiedEvent, q);
  221. q->updateFromPiecewiseFunction();
  222. delete []nodes;
  223. }
  224. // ----------------------------------------------------------------------------
  225. void ctkVTKThresholdWidgetPrivate
  226. ::setNodes(double nodeValues[][4], const int nodeCount)
  227. {
  228. for(int i = 0; i < nodeCount; ++i)
  229. {
  230. int index = i;
  231. double node[4];
  232. // Search the index where to write the node
  233. for (int j = 0; j < i; ++j)
  234. {
  235. this->PiecewiseFunction->GetNodeValue(j, node);
  236. bool different = node[0] != nodeValues[j][0] ||
  237. node[1] != nodeValues[j][1] ||
  238. node[2] != nodeValues[j][2] ||
  239. node[3] != nodeValues[j][3];
  240. if (different)
  241. {
  242. index = j;
  243. break;
  244. }
  245. }
  246. if (index >= this->PiecewiseFunction->GetSize())
  247. {
  248. node[0] = VTK_DOUBLE_MAX;
  249. }
  250. else if (index == i)
  251. {
  252. this->PiecewiseFunction->GetNodeValue(index, node);
  253. }
  254. // else we already have node correctly set
  255. if (!this->UseSharpness)
  256. {
  257. // be smart in order to reduce "jumps"
  258. // Here is the intermediate steps we try to avoid:
  259. // Start
  260. // 2---3
  261. // | |
  262. // 0---1 4--5
  263. // After node1 is set:
  264. // 1 _3
  265. // |\ / |
  266. // 0---| 2 4--5
  267. // After node1 is set:
  268. // 2--3
  269. // | |
  270. // 0-----1 4--5
  271. if ((i == 1 && node[0] < nodeValues[i][0]) ||
  272. (i == 3 && node[0] < nodeValues[i][0]))
  273. {
  274. this->setNodeValue(index + 1, nodeValues[i+1]);
  275. }
  276. }
  277. this->setNodeValue(index, nodeValues[i]);
  278. }
  279. }
  280. // ----------------------------------------------------------------------------
  281. void ctkVTKThresholdWidgetPrivate::setNodeValue(int index, double* nodeValues)
  282. {
  283. if (index >= this->PiecewiseFunction->GetSize())
  284. {
  285. this->PiecewiseFunction->AddPoint(
  286. nodeValues[0], nodeValues[1], nodeValues[2], nodeValues[3]);
  287. }
  288. else
  289. {
  290. double values[4];
  291. this->PiecewiseFunction->GetNodeValue(index, values);
  292. // Updating the node will fire a modified event which can be costly
  293. // We change the node values only if there is a change.
  294. if (values[0] != nodeValues[0] ||
  295. values[1] != nodeValues[1] ||
  296. values[2] != nodeValues[2] ||
  297. values[3] != nodeValues[3])
  298. {
  299. this->PiecewiseFunction->SetNodeValue(index, nodeValues);
  300. }
  301. }
  302. }
  303. // ----------------------------------------------------------------------------
  304. // ctkVTKThresholdWidget methods
  305. // ----------------------------------------------------------------------------
  306. ctkVTKThresholdWidget::ctkVTKThresholdWidget(QWidget* parentWidget)
  307. :QWidget(parentWidget)
  308. , d_ptr(new ctkVTKThresholdWidgetPrivate(*this))
  309. {
  310. Q_D(ctkVTKThresholdWidget);
  311. d->setupUi(this);
  312. }
  313. // ----------------------------------------------------------------------------
  314. ctkVTKThresholdWidget::~ctkVTKThresholdWidget()
  315. {
  316. }
  317. // ----------------------------------------------------------------------------
  318. vtkPiecewiseFunction* ctkVTKThresholdWidget::piecewiseFunction()const
  319. {
  320. Q_D(const ctkVTKThresholdWidget);
  321. return d->PiecewiseFunction;
  322. }
  323. // ----------------------------------------------------------------------------
  324. void ctkVTKThresholdWidget
  325. ::setPiecewiseFunction(vtkPiecewiseFunction* newFunction)
  326. {
  327. Q_D(ctkVTKThresholdWidget);
  328. if (d->PiecewiseFunction == newFunction)
  329. {
  330. return;
  331. }
  332. this->qvtkReconnect(d->PiecewiseFunction, newFunction, vtkCommand::ModifiedEvent,
  333. this, SLOT(updateFromPiecewiseFunction()));
  334. d->PiecewiseFunction = newFunction;
  335. if (!d->UserRange)
  336. {
  337. double range[2] = {0., 1.};
  338. if (d->PiecewiseFunction)
  339. {
  340. d->PiecewiseFunction->GetRange(range);
  341. }
  342. d->setRange(range[0], range[1]);
  343. }
  344. if (d->PiecewiseFunction)
  345. {
  346. double min, max, value;
  347. d->guessThreshold(min, max, value);
  348. d->setThreshold(min, max, value);
  349. }
  350. }
  351. // ----------------------------------------------------------------------------
  352. void ctkVTKThresholdWidget::updateFromPiecewiseFunction()
  353. {
  354. Q_D(ctkVTKThresholdWidget);
  355. if (!d->PiecewiseFunction)
  356. {
  357. return;
  358. }
  359. double range[2];
  360. d->ThresholdSliderWidget->range(range);
  361. double minThreshold = range[0];
  362. double maxThreshold = range[1];
  363. double opacity = d->OpacitySliderWidget->value();
  364. double node[4];
  365. int minIndex = 1;
  366. if (d->PiecewiseFunction->GetSize() > minIndex)
  367. {
  368. d->PiecewiseFunction->GetNodeValue(minIndex, node);
  369. minThreshold = node[0];
  370. }
  371. int maxIndex = d->UseSharpness ? 2 : 3;
  372. if (d->PiecewiseFunction->GetSize() > maxIndex)
  373. {
  374. d->PiecewiseFunction->GetNodeValue(maxIndex, node);
  375. maxThreshold = node[0];
  376. opacity = node[1];
  377. }
  378. if (d->PiecewiseFunction->GetSize() > 3)
  379. {
  380. d->PiecewiseFunction->GetNodeValue(3, node);
  381. }
  382. bool wasBlocking = d->ThresholdSliderWidget->blockSignals(true);
  383. d->ThresholdSliderWidget->setValues(minThreshold, maxThreshold);
  384. d->ThresholdSliderWidget->blockSignals(wasBlocking);
  385. d->OpacitySliderWidget->blockSignals(true);
  386. d->OpacitySliderWidget->setValue(opacity);
  387. d->OpacitySliderWidget->blockSignals(wasBlocking);
  388. }
  389. // ----------------------------------------------------------------------------
  390. void ctkVTKThresholdWidget::setThresholdValues(double min, double max)
  391. {
  392. Q_D(ctkVTKThresholdWidget);
  393. d->setThreshold(min, max, d->OpacitySliderWidget->value());
  394. }
  395. // ----------------------------------------------------------------------------
  396. void ctkVTKThresholdWidget::thresholdValues(double* values)const
  397. {
  398. Q_D(const ctkVTKThresholdWidget);
  399. values[0] = d->ThresholdSliderWidget->minimumValue();
  400. values[1] = d->ThresholdSliderWidget->maximumValue();
  401. }
  402. // ----------------------------------------------------------------------------
  403. void ctkVTKThresholdWidget::setOpacity(double opacity)
  404. {
  405. Q_D(ctkVTKThresholdWidget);
  406. d->setThreshold(d->ThresholdSliderWidget->minimumValue(),
  407. d->ThresholdSliderWidget->maximumValue(), opacity);
  408. }
  409. // ----------------------------------------------------------------------------
  410. double ctkVTKThresholdWidget::opacity()const
  411. {
  412. Q_D(const ctkVTKThresholdWidget);
  413. return d->OpacitySliderWidget->value();
  414. }
  415. // ----------------------------------------------------------------------------
  416. void ctkVTKThresholdWidget::range(double* range)const
  417. {
  418. Q_D(const ctkVTKThresholdWidget);
  419. d->ThresholdSliderWidget->range(range);
  420. }
  421. // ----------------------------------------------------------------------------
  422. void ctkVTKThresholdWidget::setRange(double min, double max)
  423. {
  424. Q_D(ctkVTKThresholdWidget);
  425. d->UserRange = true;
  426. d->setRange(min, max);
  427. }
  428. // ----------------------------------------------------------------------------
  429. bool ctkVTKThresholdWidget::useSharpness()const
  430. {
  431. Q_D(const ctkVTKThresholdWidget);
  432. return d->UseSharpness;
  433. }
  434. // ----------------------------------------------------------------------------
  435. void ctkVTKThresholdWidget::setUseSharpness(bool use)
  436. {
  437. Q_D(ctkVTKThresholdWidget);
  438. if (use == d->UseSharpness)
  439. {
  440. return;
  441. }
  442. d->UseSharpness = use;
  443. double min, max, value;
  444. d->guessThreshold(min, max, value);
  445. d->setThreshold(min, max, value);
  446. }