ctkVTKVolumePropertyWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 "ctkVTKScalarsToColorsView.h"
  19. #include "ctkVTKVolumePropertyWidget.h"
  20. #include "ctkUtils.h"
  21. #include "ui_ctkVTKVolumePropertyWidget.h"
  22. // VTK includes
  23. #include <vtkColorTransferControlPointsItem.h>
  24. #include <vtkColorTransferFunction.h>
  25. #include <vtkColorTransferFunctionItem.h>
  26. #include <vtkCompositeControlPointsItem.h>
  27. #include <vtkCompositeTransferFunctionItem.h>
  28. #include <vtkContextScene.h>
  29. #include <vtkLookupTable.h>
  30. #include <vtkLookupTableItem.h>
  31. #include <vtkPiecewiseControlPointsItem.h>
  32. #include <vtkPiecewiseFunction.h>
  33. #include <vtkPiecewiseFunctionItem.h>
  34. #include <vtkVolumeProperty.h>
  35. //----------------------------------------------------------------------------
  36. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKVolumePropertyWidget");
  37. //----------------------------------------------------------------------------
  38. class ctkVTKVolumePropertyWidgetPrivate:
  39. public Ui_ctkVTKVolumePropertyWidget
  40. {
  41. Q_DECLARE_PUBLIC(ctkVTKVolumePropertyWidget);
  42. protected:
  43. ctkVTKVolumePropertyWidget* const q_ptr;
  44. public:
  45. ctkVTKVolumePropertyWidgetPrivate(ctkVTKVolumePropertyWidget& object);
  46. void setupUi(QWidget* widget);
  47. void computeRange(double* range);
  48. void updateThresholdSlider(vtkPiecewiseFunction* opacityFunction);
  49. void setThreshold(double min, double max, double opacity);
  50. vtkVolumeProperty* VolumeProperty;
  51. int CurrentComponent;
  52. };
  53. // ----------------------------------------------------------------------------
  54. // ctkVTKVolumePropertyWidgetPrivate methods
  55. // ----------------------------------------------------------------------------
  56. ctkVTKVolumePropertyWidgetPrivate::ctkVTKVolumePropertyWidgetPrivate(
  57. ctkVTKVolumePropertyWidget& object)
  58. : q_ptr(&object)
  59. {
  60. this->VolumeProperty = 0;
  61. this->CurrentComponent = 0;
  62. }
  63. // ----------------------------------------------------------------------------
  64. void ctkVTKVolumePropertyWidgetPrivate::setupUi(QWidget* widget)
  65. {
  66. Q_Q(ctkVTKVolumePropertyWidget);
  67. Q_ASSERT(q == widget);
  68. this->Ui_ctkVTKVolumePropertyWidget::setupUi(widget);
  69. this->ScalarOpacityWidget->view()->addCompositeFunction(0, 0, true, true);
  70. vtkCompositeControlPointsItem* composite =
  71. vtkCompositeControlPointsItem::SafeDownCast(
  72. this->ScalarOpacityWidget->view()->opacityFunctionPlots()[1]);
  73. composite->SetColorFill(true);
  74. composite->SetPointsFunction(vtkCompositeControlPointsItem::OpacityPointsFunction);
  75. this->ScalarColorWidget->view()->addColorTransferFunction(0);
  76. this->GradientWidget->view()->addPiecewiseFunction(0);
  77. this->ScalarOpacityThresholdWidget->setVisible(false);
  78. QObject::connect(this->ScalarOpacityWidget, SIGNAL(axesModified()),
  79. q, SLOT(onAxesModified()));//, Qt::QueuedConnection);
  80. QObject::connect(this->ScalarColorWidget, SIGNAL(axesModified()),
  81. q, SLOT(onAxesModified()));//, Qt::QueuedConnection);
  82. QObject::connect(this->GradientWidget, SIGNAL(axesModified()),
  83. q, SLOT(onAxesModified()));//, Qt::QueuedConnection);
  84. this->GradientGroupBox->setCollapsed(true);
  85. this->AdvancedGroupBox->setCollapsed(true);
  86. QObject::connect(this->InterpolationComboBox, SIGNAL(currentIndexChanged(int)),
  87. q, SLOT(setInterpolationMode(int)));
  88. QObject::connect(this->ShadeCheckBox, SIGNAL(toggled(bool)),
  89. q, SLOT(setShade(bool)));
  90. QObject::connect(this->MaterialPropertyWidget, SIGNAL(ambientChanged(double)),
  91. q, SLOT(setAmbient(double)));
  92. QObject::connect(this->MaterialPropertyWidget, SIGNAL(diffuseChanged(double)),
  93. q, SLOT(setDiffuse(double)));
  94. QObject::connect(this->MaterialPropertyWidget, SIGNAL(specularChanged(double)),
  95. q, SLOT(setSpecular(double)));
  96. QObject::connect(this->MaterialPropertyWidget, SIGNAL(specularPowerChanged(double)),
  97. q, SLOT(setSpecularPower(double)));
  98. }
  99. // ----------------------------------------------------------------------------
  100. void ctkVTKVolumePropertyWidgetPrivate::computeRange(double* range)
  101. {
  102. range[0] = 0.;
  103. range[1] = 1.;
  104. if (!this->VolumeProperty)
  105. {
  106. return;
  107. }
  108. Q_ASSERT(this->VolumeProperty->GetRGBTransferFunction(this->CurrentComponent));
  109. Q_ASSERT(this->VolumeProperty->GetScalarOpacity(this->CurrentComponent));
  110. Q_ASSERT(this->VolumeProperty->GetGradientOpacity(this->CurrentComponent));
  111. double colorRange[2] = {0., 1.};
  112. this->VolumeProperty->GetRGBTransferFunction(this->CurrentComponent)->GetRange(colorRange);
  113. range[0] = qMin(range[0], colorRange[0]);
  114. range[1] = qMax(range[1], colorRange[1]);
  115. double opacityRange[2] = {0., 1.};
  116. this->VolumeProperty->GetScalarOpacity(this->CurrentComponent)->GetRange(opacityRange);
  117. range[0] = qMin(range[0], opacityRange[0]);
  118. range[1] = qMax(range[1], opacityRange[1]);
  119. double gradientRange[2] = {0., 1.};
  120. this->VolumeProperty->GetGradientOpacity(this->CurrentComponent)->GetRange(gradientRange);
  121. range[0] = qMin(range[0], gradientRange[0]);
  122. range[1] = qMax(range[1], gradientRange[1]);
  123. }
  124. // ----------------------------------------------------------------------------
  125. // ctkVTKVolumePropertyWidget methods
  126. // ----------------------------------------------------------------------------
  127. ctkVTKVolumePropertyWidget::ctkVTKVolumePropertyWidget(QWidget* parentWidget)
  128. :QWidget(parentWidget)
  129. , d_ptr(new ctkVTKVolumePropertyWidgetPrivate(*this))
  130. {
  131. Q_D(ctkVTKVolumePropertyWidget);
  132. d->setupUi(this);
  133. }
  134. // ----------------------------------------------------------------------------
  135. ctkVTKVolumePropertyWidget::~ctkVTKVolumePropertyWidget()
  136. {
  137. }
  138. // ----------------------------------------------------------------------------
  139. vtkVolumeProperty* ctkVTKVolumePropertyWidget::volumeProperty()const
  140. {
  141. Q_D(const ctkVTKVolumePropertyWidget);
  142. return d->VolumeProperty;
  143. }
  144. // ----------------------------------------------------------------------------
  145. void ctkVTKVolumePropertyWidget
  146. ::setVolumeProperty(vtkVolumeProperty* newVolumeProperty)
  147. {
  148. Q_D(ctkVTKVolumePropertyWidget);
  149. this->qvtkReconnect(d->VolumeProperty, newVolumeProperty, vtkCommand::ModifiedEvent,
  150. this, SLOT(updateFromVolumeProperty()));
  151. d->VolumeProperty = newVolumeProperty;
  152. this->updateFromVolumeProperty();
  153. double range[2];
  154. d->computeRange(range);
  155. d->ScalarOpacityThresholdWidget->setRange(range[0], range[1]);
  156. double chartBounds[8];
  157. d->ScalarOpacityWidget->view()->chartBounds(chartBounds);
  158. chartBounds[2] = range[0];
  159. chartBounds[3] = range[1];
  160. d->ScalarOpacityWidget->view()->setChartUserBounds(chartBounds);
  161. d->ScalarColorWidget->view()->chartBounds(chartBounds);
  162. chartBounds[2] = range[0];
  163. chartBounds[3] = range[1];
  164. d->ScalarColorWidget->view()->setChartUserBounds(chartBounds);
  165. d->GradientWidget->view()->chartBounds(chartBounds);
  166. chartBounds[2] = range[0];
  167. chartBounds[3] = range[1];
  168. d->GradientWidget->view()->setChartUserBounds(chartBounds);
  169. }
  170. // ----------------------------------------------------------------------------
  171. void ctkVTKVolumePropertyWidget::updateFromVolumeProperty()
  172. {
  173. Q_D(ctkVTKVolumePropertyWidget);
  174. vtkColorTransferFunction* colorTransferFunction = 0;
  175. vtkPiecewiseFunction* opacityFunction = 0;
  176. vtkPiecewiseFunction* gradientFunction = 0;
  177. if (d->VolumeProperty)
  178. {
  179. colorTransferFunction =
  180. d->VolumeProperty->GetRGBTransferFunction()->GetSize() ?
  181. d->VolumeProperty->GetRGBTransferFunction() : 0;
  182. opacityFunction =
  183. d->VolumeProperty->GetScalarOpacity()->GetSize() ?
  184. d->VolumeProperty->GetScalarOpacity() : 0;
  185. gradientFunction =
  186. d->VolumeProperty->GetGradientOpacity()->GetSize() ?
  187. d->VolumeProperty->GetGradientOpacity() : 0;
  188. }
  189. d->ScalarOpacityThresholdWidget->setPiecewiseFunction(this->useThresholdSlider() ? opacityFunction : 0);
  190. d->ScalarOpacityWidget->view()->setOpacityFunctionToPlots(opacityFunction);
  191. d->ScalarOpacityWidget->view()->setColorTransferFunctionToPlots(colorTransferFunction);
  192. d->ScalarColorWidget->view()->setColorTransferFunctionToPlots(colorTransferFunction);
  193. d->GradientWidget->view()->setPiecewiseFunctionToPlots(gradientFunction);
  194. if (d->VolumeProperty)
  195. {
  196. d->InterpolationComboBox->setCurrentIndex(
  197. d->VolumeProperty->GetInterpolationType() == VTK_NEAREST_INTERPOLATION ? 0 : 1);
  198. d->ShadeCheckBox->setChecked(
  199. d->VolumeProperty->GetShade(d->CurrentComponent));
  200. d->MaterialPropertyWidget->setAmbient(
  201. d->VolumeProperty->GetAmbient(d->CurrentComponent));
  202. d->MaterialPropertyWidget->setDiffuse(
  203. d->VolumeProperty->GetDiffuse(d->CurrentComponent));
  204. d->MaterialPropertyWidget->setSpecular(
  205. d->VolumeProperty->GetSpecular(d->CurrentComponent));
  206. d->MaterialPropertyWidget->setSpecularPower(
  207. d->VolumeProperty->GetSpecularPower(d->CurrentComponent));
  208. }
  209. }
  210. // ----------------------------------------------------------------------------
  211. void ctkVTKVolumePropertyWidget::setInterpolationMode(int mode)
  212. {
  213. Q_D(ctkVTKVolumePropertyWidget);
  214. if (!d->VolumeProperty)
  215. {
  216. return;
  217. }
  218. d->VolumeProperty->SetInterpolationType(mode);
  219. }
  220. // ----------------------------------------------------------------------------
  221. void ctkVTKVolumePropertyWidget::setShade(bool enable)
  222. {
  223. Q_D(ctkVTKVolumePropertyWidget);
  224. if (!d->VolumeProperty)
  225. {
  226. return;
  227. }
  228. d->VolumeProperty->SetShade(d->CurrentComponent, enable);
  229. }
  230. // ----------------------------------------------------------------------------
  231. void ctkVTKVolumePropertyWidget::setAmbient(double value)
  232. {
  233. Q_D(ctkVTKVolumePropertyWidget);
  234. if (!d->VolumeProperty)
  235. {
  236. return;
  237. }
  238. d->VolumeProperty->SetAmbient(d->CurrentComponent, value);
  239. }
  240. // ----------------------------------------------------------------------------
  241. void ctkVTKVolumePropertyWidget::setDiffuse(double value)
  242. {
  243. Q_D(ctkVTKVolumePropertyWidget);
  244. if (!d->VolumeProperty)
  245. {
  246. return;
  247. }
  248. d->VolumeProperty->SetDiffuse(d->CurrentComponent, value);
  249. }
  250. // ----------------------------------------------------------------------------
  251. void ctkVTKVolumePropertyWidget::setSpecular(double value)
  252. {
  253. Q_D(ctkVTKVolumePropertyWidget);
  254. if (!d->VolumeProperty)
  255. {
  256. return;
  257. }
  258. d->VolumeProperty->SetSpecular(d->CurrentComponent, value);
  259. }
  260. // ----------------------------------------------------------------------------
  261. void ctkVTKVolumePropertyWidget::setSpecularPower(double value)
  262. {
  263. Q_D(ctkVTKVolumePropertyWidget);
  264. if (!d->VolumeProperty)
  265. {
  266. return;
  267. }
  268. d->VolumeProperty->SetSpecularPower(d->CurrentComponent, value);
  269. }
  270. // ----------------------------------------------------------------------------
  271. bool ctkVTKVolumePropertyWidget::useThresholdSlider()const
  272. {
  273. Q_D(const ctkVTKVolumePropertyWidget);
  274. return d->ScalarOpacityThresholdWidget->isVisibleTo(
  275. const_cast<ctkVTKVolumePropertyWidget*>(this));
  276. }
  277. // ----------------------------------------------------------------------------
  278. void ctkVTKVolumePropertyWidget::setUseThresholdSlider(bool enable)
  279. {
  280. Q_D(ctkVTKVolumePropertyWidget);
  281. d->ScalarOpacityThresholdWidget->setVisible(enable);
  282. d->ScalarOpacityWidget->setVisible(!enable);
  283. this->updateFromVolumeProperty();
  284. }
  285. // ----------------------------------------------------------------------------
  286. void ctkVTKVolumePropertyWidget::onAxesModified()
  287. {
  288. Q_D(ctkVTKVolumePropertyWidget);
  289. //return;
  290. ctkVTKScalarsToColorsWidget* senderWidget =
  291. qobject_cast<ctkVTKScalarsToColorsWidget*>(this->sender());
  292. double xRange[2] = {0.,0.};
  293. senderWidget->xRange(xRange);
  294. if (senderWidget != d->ScalarOpacityWidget)
  295. {
  296. bool wasBlocking = d->ScalarOpacityWidget->blockSignals(true);
  297. d->ScalarOpacityWidget->setXRange(xRange[0], xRange[1]);
  298. d->ScalarOpacityWidget->blockSignals(wasBlocking);
  299. }
  300. if (senderWidget != d->ScalarColorWidget)
  301. {
  302. bool wasBlocking = d->ScalarColorWidget->blockSignals(true);
  303. d->ScalarColorWidget->setXRange(xRange[0], xRange[1]);
  304. d->ScalarColorWidget->blockSignals(wasBlocking);
  305. }
  306. if (senderWidget != d->GradientWidget)
  307. {
  308. bool wasBlocking = d->GradientWidget->blockSignals(true);
  309. d->GradientWidget->setXRange(xRange[0], xRange[1]);
  310. d->GradientWidget->blockSignals(wasBlocking);
  311. }
  312. }