ctkVTKThresholdWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. protected:
  41. void setNodes(double nodeValues[4][4]);
  42. void setNodeValue(int index, double* nodeValue);
  43. };
  44. // ----------------------------------------------------------------------------
  45. // ctkVTKThresholdWidgetPrivate methods
  46. // ----------------------------------------------------------------------------
  47. ctkVTKThresholdWidgetPrivate::ctkVTKThresholdWidgetPrivate(
  48. ctkVTKThresholdWidget& object)
  49. : q_ptr(&object)
  50. {
  51. this->PiecewiseFunction = 0;
  52. this->UserRange = false;
  53. }
  54. // ----------------------------------------------------------------------------
  55. void ctkVTKThresholdWidgetPrivate::setupUi(QWidget* widget)
  56. {
  57. Q_Q(ctkVTKThresholdWidget);
  58. Q_ASSERT(q == widget);
  59. this->Ui_ctkVTKThresholdWidget::setupUi(widget);
  60. QObject::connect(this->ThresholdSliderWidget, SIGNAL(valuesChanged(double,double)),
  61. q, SLOT(setThresholdValues(double,double)));
  62. QObject::connect(this->OpacitySliderWidget, SIGNAL(valueChanged(double)),
  63. q, SLOT(setOpacity(double)));
  64. }
  65. // ----------------------------------------------------------------------------
  66. void ctkVTKThresholdWidgetPrivate::guessThreshold(double& min, double& max, double& opacity)const
  67. {
  68. min = this->ThresholdSliderWidget->minimum();
  69. max = this->ThresholdSliderWidget->maximum();
  70. opacity = 1.;
  71. if (!this->PiecewiseFunction || this->PiecewiseFunction->GetSize() == 0)
  72. {
  73. return;
  74. }
  75. int minIndex;
  76. // The min index is the point before the opacity is >0
  77. for (minIndex=0; minIndex < this->PiecewiseFunction->GetSize(); ++minIndex)
  78. {
  79. double node[4];
  80. this->PiecewiseFunction->GetNodeValue(minIndex, node);
  81. if (node[1] > 0.)
  82. {
  83. opacity = node[1];
  84. max = node[0];
  85. break;
  86. }
  87. min = node[0];
  88. }
  89. if (minIndex == this->PiecewiseFunction->GetSize())
  90. {
  91. return;
  92. }
  93. int maxIndex = minIndex + 1;
  94. for (;maxIndex < this->PiecewiseFunction->GetSize(); ++maxIndex)
  95. {
  96. double node[4];
  97. this->PiecewiseFunction->GetNodeValue(maxIndex, node);
  98. // use the max opacity
  99. opacity = std::max(opacity, node[1]);
  100. if (node[1] < opacity)
  101. {
  102. break;
  103. }
  104. max = node[0];
  105. }
  106. }
  107. // ----------------------------------------------------------------------------
  108. void ctkVTKThresholdWidgetPrivate::setRange(double min, double max)
  109. {
  110. bool wasBlocking = this->ThresholdSliderWidget->blockSignals(true);
  111. int decimals = qMax(0, -ctk::orderOfMagnitude(max - min) + 2);
  112. this->ThresholdSliderWidget->setDecimals(decimals);
  113. this->ThresholdSliderWidget->setSingleStep(pow(10., -decimals));
  114. this->ThresholdSliderWidget->setRange(min, max);
  115. this->ThresholdSliderWidget->blockSignals(wasBlocking);
  116. }
  117. // ----------------------------------------------------------------------------
  118. void ctkVTKThresholdWidgetPrivate::setThreshold(double min, double max, double opacity)
  119. {
  120. Q_Q(ctkVTKThresholdWidget);
  121. if (!this->PiecewiseFunction)
  122. {
  123. return;
  124. }
  125. double range[2];
  126. this->ThresholdSliderWidget->range(range);
  127. // Start of the curve, always y=0
  128. double nodes[4][4];
  129. nodes[0][0] = range[0];
  130. nodes[0][1] = 0.;
  131. nodes[0][2] = 0.5; // midpoint
  132. nodes[0][3] = 0.; // sharpness
  133. // Starting threshold point with a sharp slope that jumps to the opacity
  134. // which is set by the next point
  135. nodes[1][0] = min +
  136. ((min == nodes[0][0] && !this->PiecewiseFunction->GetAllowDuplicateScalars()) ?
  137. 0.00000000000001 : 0.);
  138. nodes[1][1] = 0.;
  139. nodes[1][2] = 0.; // jumps directly
  140. nodes[1][3] = 1.; // sharp
  141. // Ending threshold point with a sharp slope that jumps back to a 0 opacity
  142. nodes[2][0] = max +
  143. ((max == nodes[1][0] && !this->PiecewiseFunction->GetAllowDuplicateScalars()) ?
  144. 0.00000000000001 : 0.);
  145. nodes[2][1] = opacity;
  146. nodes[2][2] = 0.;
  147. nodes[2][3] = 1.;
  148. // End of the curve, always y = 0
  149. nodes[3][0] = range[1] +
  150. ((range[1] == nodes[2][0] && !this->PiecewiseFunction->GetAllowDuplicateScalars()) ?
  151. 0.00000000000001 : 0.);
  152. nodes[3][1] = 0.;
  153. nodes[3][2] = 0.5;
  154. nodes[3][3] = 0.;
  155. q->qvtkBlock(this->PiecewiseFunction, vtkCommand::ModifiedEvent, q);
  156. this->setNodes(nodes);
  157. q->qvtkUnblock(this->PiecewiseFunction, vtkCommand::ModifiedEvent, q);
  158. q->updateFromPiecewiseFunction();
  159. }
  160. // ----------------------------------------------------------------------------
  161. void ctkVTKThresholdWidgetPrivate::setNodes(double nodeValues[4][4])
  162. {
  163. double lastX = VTK_DOUBLE_MIN;
  164. double minX = nodeValues[0][0];
  165. for(int i = 0; i < 4; ++i)
  166. {
  167. int index = this->PiecewiseFunction->GetSize();
  168. bool alreadyEqual = false;
  169. // search the node index to modify
  170. for (int j = 0; j < this->PiecewiseFunction->GetSize(); ++j)
  171. {
  172. double node[4];
  173. this->PiecewiseFunction->GetNodeValue(j, node);
  174. if (node[0] < minX || node[0] > lastX)
  175. {
  176. index = j;
  177. break;
  178. }
  179. else if (node[0] == lastX)
  180. {
  181. if (alreadyEqual)
  182. {
  183. index = j;
  184. break;
  185. }
  186. alreadyEqual = true;
  187. }
  188. }
  189. this->setNodeValue(index, nodeValues[i]);
  190. lastX = nodeValues[i][0];
  191. }
  192. }
  193. // ----------------------------------------------------------------------------
  194. void ctkVTKThresholdWidgetPrivate::setNodeValue(int index, double* nodeValues)
  195. {
  196. if (this->PiecewiseFunction->GetSize() <= index)
  197. {
  198. this->PiecewiseFunction->AddPoint(
  199. nodeValues[0], nodeValues[1], nodeValues[2], nodeValues[3]);
  200. }
  201. else
  202. {
  203. this->PiecewiseFunction->SetNodeValue(index, nodeValues);
  204. }
  205. }
  206. // ----------------------------------------------------------------------------
  207. // ctkVTKThresholdWidget methods
  208. // ----------------------------------------------------------------------------
  209. ctkVTKThresholdWidget::ctkVTKThresholdWidget(QWidget* parentWidget)
  210. :QWidget(parentWidget)
  211. , d_ptr(new ctkVTKThresholdWidgetPrivate(*this))
  212. {
  213. Q_D(ctkVTKThresholdWidget);
  214. d->setupUi(this);
  215. }
  216. // ----------------------------------------------------------------------------
  217. ctkVTKThresholdWidget::~ctkVTKThresholdWidget()
  218. {
  219. }
  220. // ----------------------------------------------------------------------------
  221. vtkPiecewiseFunction* ctkVTKThresholdWidget::piecewiseFunction()const
  222. {
  223. Q_D(const ctkVTKThresholdWidget);
  224. return d->PiecewiseFunction;
  225. }
  226. // ----------------------------------------------------------------------------
  227. void ctkVTKThresholdWidget
  228. ::setPiecewiseFunction(vtkPiecewiseFunction* newFunction)
  229. {
  230. Q_D(ctkVTKThresholdWidget);
  231. if (d->PiecewiseFunction == newFunction)
  232. {
  233. return;
  234. }
  235. this->qvtkReconnect(d->PiecewiseFunction, newFunction, vtkCommand::ModifiedEvent,
  236. this, SLOT(updateFromPiecewiseFunction()));
  237. d->PiecewiseFunction = newFunction;
  238. if (!d->UserRange)
  239. {
  240. double range[2] = {0., 1.};
  241. if (d->PiecewiseFunction)
  242. {
  243. d->PiecewiseFunction->GetRange(range);
  244. }
  245. d->setRange(range[0], range[1]);
  246. }
  247. if (d->PiecewiseFunction)
  248. {
  249. double min, max, value;
  250. d->guessThreshold(min, max, value);
  251. d->setThreshold(min, max, value);
  252. }
  253. }
  254. // ----------------------------------------------------------------------------
  255. void ctkVTKThresholdWidget::updateFromPiecewiseFunction()
  256. {
  257. Q_D(ctkVTKThresholdWidget);
  258. if (!d->PiecewiseFunction)
  259. {
  260. return;
  261. }
  262. double range[2];
  263. d->ThresholdSliderWidget->range(range);
  264. double minThreshold = range[0];
  265. double maxThreshold = range[1];
  266. double opacity = d->OpacitySliderWidget->value();
  267. double node[4];
  268. if (d->PiecewiseFunction->GetSize() > 1)
  269. {
  270. d->PiecewiseFunction->GetNodeValue(1, node);
  271. minThreshold = node[0];
  272. }
  273. if (d->PiecewiseFunction->GetSize() > 2)
  274. {
  275. d->PiecewiseFunction->GetNodeValue(2, node);
  276. maxThreshold = node[0];
  277. opacity = node[1];
  278. }
  279. if (d->PiecewiseFunction->GetSize() > 3)
  280. {
  281. d->PiecewiseFunction->GetNodeValue(3, node);
  282. }
  283. bool wasBlocking = d->ThresholdSliderWidget->blockSignals(true);
  284. d->ThresholdSliderWidget->setValues(minThreshold, maxThreshold);
  285. d->ThresholdSliderWidget->blockSignals(wasBlocking);
  286. d->OpacitySliderWidget->blockSignals(true);
  287. d->OpacitySliderWidget->setValue(opacity);
  288. d->OpacitySliderWidget->blockSignals(wasBlocking);
  289. }
  290. // ----------------------------------------------------------------------------
  291. void ctkVTKThresholdWidget::setThresholdValues(double min, double max)
  292. {
  293. Q_D(ctkVTKThresholdWidget);
  294. d->setThreshold(min, max, d->OpacitySliderWidget->value());
  295. }
  296. // ----------------------------------------------------------------------------
  297. void ctkVTKThresholdWidget::thresholdValues(double* values)const
  298. {
  299. Q_D(const ctkVTKThresholdWidget);
  300. values[0] = d->ThresholdSliderWidget->minimumValue();
  301. values[1] = d->ThresholdSliderWidget->maximumValue();
  302. }
  303. // ----------------------------------------------------------------------------
  304. void ctkVTKThresholdWidget::setOpacity(double opacity)
  305. {
  306. Q_D(ctkVTKThresholdWidget);
  307. d->setThreshold(d->ThresholdSliderWidget->minimumValue(),
  308. d->ThresholdSliderWidget->maximumValue(), opacity);
  309. }
  310. // ----------------------------------------------------------------------------
  311. double ctkVTKThresholdWidget::opacity()const
  312. {
  313. Q_D(const ctkVTKThresholdWidget);
  314. return d->OpacitySliderWidget->value();
  315. }
  316. // ----------------------------------------------------------------------------
  317. void ctkVTKThresholdWidget::range(double* range)const
  318. {
  319. Q_D(const ctkVTKThresholdWidget);
  320. d->ThresholdSliderWidget->range(range);
  321. }
  322. // ----------------------------------------------------------------------------
  323. void ctkVTKThresholdWidget::setRange(double min, double max)
  324. {
  325. Q_D(ctkVTKThresholdWidget);
  326. d->UserRange = true;
  327. d->setRange(min, max);
  328. }