ctkVTKThresholdWidget.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.commontk.org/LICENSE
  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 guessThreshold(double& min, double& max, double& opacity);
  37. vtkPiecewiseFunction* PiecewiseFunction;
  38. };
  39. // ----------------------------------------------------------------------------
  40. // ctkVTKThresholdWidgetPrivate methods
  41. // ----------------------------------------------------------------------------
  42. ctkVTKThresholdWidgetPrivate::ctkVTKThresholdWidgetPrivate(
  43. ctkVTKThresholdWidget& object)
  44. : q_ptr(&object)
  45. {
  46. this->PiecewiseFunction = 0;
  47. }
  48. // ----------------------------------------------------------------------------
  49. void ctkVTKThresholdWidgetPrivate::setupUi(QWidget* widget)
  50. {
  51. Q_Q(ctkVTKThresholdWidget);
  52. Q_ASSERT(q == widget);
  53. this->Ui_ctkVTKThresholdWidget::setupUi(widget);
  54. QObject::connect(this->ThresholdSliderWidget, SIGNAL(valuesChanged(double, double)),
  55. q, SLOT(setThresholdValues(double, double)));
  56. QObject::connect(this->OpacitySliderWidget, SIGNAL(valueChanged(double)),
  57. q, SLOT(setOpacity(double)));
  58. }
  59. // ----------------------------------------------------------------------------
  60. void ctkVTKThresholdWidgetPrivate::guessThreshold(double& min, double& max, double& opacity)
  61. {
  62. min = this->ThresholdSliderWidget->minimum();
  63. max = this->ThresholdSliderWidget->maximum();
  64. opacity = 1.;
  65. if (!this->PiecewiseFunction || this->PiecewiseFunction->GetSize() == 0)
  66. {
  67. return;
  68. }
  69. int minIndex;
  70. for (minIndex=0; minIndex < this->PiecewiseFunction->GetSize(); ++minIndex)
  71. {
  72. double node[4];
  73. this->PiecewiseFunction->GetNodeValue(minIndex, node);
  74. if (node[1] > 0.)
  75. {
  76. min = node[0];
  77. opacity = node[1];
  78. break;
  79. }
  80. }
  81. if (minIndex == this->PiecewiseFunction->GetSize())
  82. {
  83. return;
  84. }
  85. int maxIndex;
  86. for (maxIndex = minIndex + 1;
  87. maxIndex < this->PiecewiseFunction->GetSize();
  88. ++maxIndex)
  89. {
  90. double node[4];
  91. this->PiecewiseFunction->GetNodeValue(maxIndex, node);
  92. // average the opacities
  93. opacity += node[1];
  94. if (node[1] == 0.)
  95. {
  96. max = node[0];
  97. opacity /= maxIndex - minIndex + 1;
  98. break;
  99. }
  100. }
  101. // couldn't find the upper threshold value, use the upper range
  102. if (maxIndex == this->PiecewiseFunction->GetSize())
  103. {
  104. opacity /= maxIndex - minIndex;
  105. }
  106. }
  107. // ----------------------------------------------------------------------------
  108. void ctkVTKThresholdWidgetPrivate::setThreshold(double min, double max, double opacity)
  109. {
  110. if (!this->PiecewiseFunction)
  111. {
  112. return;
  113. }
  114. double range[2];
  115. this->ThresholdSliderWidget->range(range);
  116. double node[4];
  117. node[0] = range[0];
  118. node[1] = 0.;
  119. node[2] = 0.5;
  120. node[3] = 0.;
  121. if (this->PiecewiseFunction->GetSize() < 1)
  122. {
  123. this->PiecewiseFunction->AddPoint(node[0], node[1], node[2], node[3]);
  124. }
  125. else
  126. {
  127. this->PiecewiseFunction->SetNodeValue(0, node);
  128. }
  129. node[0] = min;
  130. node[1] = 0.;
  131. node[2] = 0.;
  132. node[3] = 1.;
  133. if (this->PiecewiseFunction->GetSize() < 2)
  134. {
  135. this->PiecewiseFunction->AddPoint(node[0], node[1], node[2], node[3]);
  136. }
  137. else
  138. {
  139. this->PiecewiseFunction->SetNodeValue(1, node);
  140. }
  141. node[0] = max;
  142. node[1] = opacity;
  143. if (this->PiecewiseFunction->GetSize() < 3)
  144. {
  145. this->PiecewiseFunction->AddPoint(node[0], node[1], node[2], node[3]);
  146. }
  147. else
  148. {
  149. this->PiecewiseFunction->SetNodeValue(2, node);
  150. }
  151. node[0] = (max == range[1]) ? range[1] + 0.00000000000001 : range[1];
  152. node[1] = 0.;
  153. node[2] = 0.5;
  154. node[3] = 0.;
  155. if (this->PiecewiseFunction->GetSize() < 4)
  156. {
  157. this->PiecewiseFunction->AddPoint(node[0], node[1], node[2], node[3]);
  158. }
  159. else
  160. {
  161. this->PiecewiseFunction->SetNodeValue(3, node);
  162. }
  163. }
  164. // ----------------------------------------------------------------------------
  165. // ctkVTKThresholdWidget methods
  166. // ----------------------------------------------------------------------------
  167. ctkVTKThresholdWidget::ctkVTKThresholdWidget(QWidget* parentWidget)
  168. :QWidget(parentWidget)
  169. , d_ptr(new ctkVTKThresholdWidgetPrivate(*this))
  170. {
  171. Q_D(ctkVTKThresholdWidget);
  172. d->setupUi(this);
  173. }
  174. // ----------------------------------------------------------------------------
  175. ctkVTKThresholdWidget::~ctkVTKThresholdWidget()
  176. {
  177. }
  178. // ----------------------------------------------------------------------------
  179. vtkPiecewiseFunction* ctkVTKThresholdWidget::piecewiseFunction()const
  180. {
  181. Q_D(const ctkVTKThresholdWidget);
  182. return d->PiecewiseFunction;
  183. }
  184. // ----------------------------------------------------------------------------
  185. void ctkVTKThresholdWidget
  186. ::setPiecewiseFunction(vtkPiecewiseFunction* newFunction)
  187. {
  188. Q_D(ctkVTKThresholdWidget);
  189. this->qvtkReconnect(d->PiecewiseFunction, newFunction, vtkCommand::ModifiedEvent,
  190. this, SLOT(updateFromPiecewiseFunction()));
  191. d->PiecewiseFunction = newFunction;
  192. double range[2] = {0., 1.};
  193. if (d->PiecewiseFunction)
  194. {
  195. d->PiecewiseFunction->GetRange(range);
  196. }
  197. this->setRange(range[0], range[1]);
  198. if (d->PiecewiseFunction)
  199. {
  200. double min, max, value;
  201. d->guessThreshold(min, max, value);
  202. d->setThreshold(min, max, value);
  203. }
  204. }
  205. // ----------------------------------------------------------------------------
  206. void ctkVTKThresholdWidget::updateFromPiecewiseFunction()
  207. {
  208. Q_D(ctkVTKThresholdWidget);
  209. if (!d->PiecewiseFunction)
  210. {
  211. return;
  212. }
  213. double range[2];
  214. d->ThresholdSliderWidget->range(range);
  215. double minThreshold = range[0];
  216. double maxThreshold = range[1];
  217. double opacity = 1.;
  218. double node[4];
  219. if (d->PiecewiseFunction->GetSize() > 1)
  220. {
  221. d->PiecewiseFunction->GetNodeValue(1, node);
  222. minThreshold = node[0];
  223. }
  224. if (d->PiecewiseFunction->GetSize() > 2)
  225. {
  226. d->PiecewiseFunction->GetNodeValue(2, node);
  227. maxThreshold = node[0];
  228. opacity = node[1];
  229. }
  230. bool wasBlocking = d->ThresholdSliderWidget->blockSignals(true);
  231. d->ThresholdSliderWidget->setValues(minThreshold, maxThreshold);
  232. d->ThresholdSliderWidget->blockSignals(wasBlocking);
  233. d->OpacitySliderWidget->blockSignals(true);
  234. d->OpacitySliderWidget->setValue(opacity);
  235. d->OpacitySliderWidget->blockSignals(wasBlocking);
  236. }
  237. // ----------------------------------------------------------------------------
  238. void ctkVTKThresholdWidget::setThresholdValues(double min, double max)
  239. {
  240. Q_D(ctkVTKThresholdWidget);
  241. d->setThreshold(min, max, d->OpacitySliderWidget->value());
  242. }
  243. // ----------------------------------------------------------------------------
  244. void ctkVTKThresholdWidget::setOpacity(double opacity)
  245. {
  246. Q_D(ctkVTKThresholdWidget);
  247. d->setThreshold(d->ThresholdSliderWidget->minimumValue(),
  248. d->ThresholdSliderWidget->maximumValue(), opacity);
  249. }
  250. // ----------------------------------------------------------------------------
  251. void ctkVTKThresholdWidget::range(double* range)const
  252. {
  253. Q_D(const ctkVTKThresholdWidget);
  254. d->ThresholdSliderWidget->range(range);
  255. }
  256. // ----------------------------------------------------------------------------
  257. void ctkVTKThresholdWidget::setRange(double min, double max)
  258. {
  259. Q_D(ctkVTKThresholdWidget);
  260. bool wasBlocking = d->ThresholdSliderWidget->blockSignals(true);
  261. int decimals = qMax(0, -ctk::orderOfMagnitude(max - min) + 2);
  262. d->ThresholdSliderWidget->setDecimals(decimals);
  263. d->ThresholdSliderWidget->setSingleStep(pow(10., -decimals));
  264. d->ThresholdSliderWidget->setRange(min, max);
  265. d->ThresholdSliderWidget->blockSignals(wasBlocking);
  266. }