ctkVTKVolumePropertyWidget.cpp 15 KB

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