ctkVTKVolumePropertyWidget.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "ui_ctkVTKVolumePropertyWidget.h"
  21. // VTK includes
  22. #include <vtkColorTransferControlPointsItem.h>
  23. #include <vtkColorTransferFunction.h>
  24. #include <vtkColorTransferFunctionItem.h>
  25. #include <vtkCompositeControlPointsItem.h>
  26. #include <vtkCompositeTransferFunctionItem.h>
  27. #include <vtkContextScene.h>
  28. #include <vtkLookupTable.h>
  29. #include <vtkLookupTableItem.h>
  30. #include <vtkPiecewiseControlPointsItem.h>
  31. #include <vtkPiecewiseFunction.h>
  32. #include <vtkPiecewiseFunctionItem.h>
  33. #include <vtkVolumeProperty.h>
  34. //----------------------------------------------------------------------------
  35. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKVolumePropertyWidget");
  36. //----------------------------------------------------------------------------
  37. class ctkVTKVolumePropertyWidgetPrivate:
  38. public Ui_ctkVTKVolumePropertyWidget
  39. {
  40. Q_DECLARE_PUBLIC(ctkVTKVolumePropertyWidget);
  41. protected:
  42. ctkVTKVolumePropertyWidget* const q_ptr;
  43. public:
  44. ctkVTKVolumePropertyWidgetPrivate(ctkVTKVolumePropertyWidget& object);
  45. void setupUi(QWidget* widget);
  46. vtkVolumeProperty* VolumeProperty;
  47. int CurrentComponent;
  48. };
  49. // ----------------------------------------------------------------------------
  50. // ctkVTKVolumePropertyWidgetPrivate methods
  51. // ----------------------------------------------------------------------------
  52. ctkVTKVolumePropertyWidgetPrivate::ctkVTKVolumePropertyWidgetPrivate(
  53. ctkVTKVolumePropertyWidget& object)
  54. : q_ptr(&object)
  55. {
  56. this->VolumeProperty = 0;
  57. this->CurrentComponent = 0;
  58. }
  59. // ----------------------------------------------------------------------------
  60. void ctkVTKVolumePropertyWidgetPrivate::setupUi(QWidget* widget)
  61. {
  62. Q_Q(ctkVTKVolumePropertyWidget);
  63. Q_ASSERT(q == widget);
  64. this->Ui_ctkVTKVolumePropertyWidget::setupUi(widget);
  65. this->ScalarOpacityWidget->view()->addCompositeFunction(0, 0, false, true);
  66. this->ScalarColorWidget->view()->addColorTransferFunction(0);
  67. this->GradientWidget->view()->addPiecewiseFunction(0);
  68. this->GradientGroupBox->setCollapsed(true);
  69. this->AdvancedGroupBox->setCollapsed(true);
  70. QObject::connect(this->InterpolationComboBox, SIGNAL(currentIndexChanged(int)),
  71. q, SLOT(setInterpolationMode(int)));
  72. QObject::connect(this->ShadeCheckBox, SIGNAL(toggled(bool)),
  73. q, SLOT(setShade(bool)));
  74. QObject::connect(this->MaterialPropertyWidget, SIGNAL(ambientChanged(double)),
  75. q, SLOT(setAmbient(double)));
  76. QObject::connect(this->MaterialPropertyWidget, SIGNAL(diffuseChanged(double)),
  77. q, SLOT(setDiffuse(double)));
  78. QObject::connect(this->MaterialPropertyWidget, SIGNAL(specularChanged(double)),
  79. q, SLOT(setSpecular(double)));
  80. QObject::connect(this->MaterialPropertyWidget, SIGNAL(specularPowerChanged(double)),
  81. q, SLOT(setSpecularPower(double)));
  82. }
  83. // ----------------------------------------------------------------------------
  84. // ctkVTKVolumePropertyWidget methods
  85. // ----------------------------------------------------------------------------
  86. ctkVTKVolumePropertyWidget::ctkVTKVolumePropertyWidget(QWidget* parentWidget)
  87. :QWidget(parentWidget)
  88. , d_ptr(new ctkVTKVolumePropertyWidgetPrivate(*this))
  89. {
  90. Q_D(ctkVTKVolumePropertyWidget);
  91. d->setupUi(this);
  92. }
  93. // ----------------------------------------------------------------------------
  94. ctkVTKVolumePropertyWidget::~ctkVTKVolumePropertyWidget()
  95. {
  96. }
  97. // ----------------------------------------------------------------------------
  98. vtkVolumeProperty* ctkVTKVolumePropertyWidget::volumeProperty()const
  99. {
  100. Q_D(const ctkVTKVolumePropertyWidget);
  101. return d->VolumeProperty;
  102. }
  103. // ----------------------------------------------------------------------------
  104. void ctkVTKVolumePropertyWidget
  105. ::setVolumeProperty(vtkVolumeProperty* newVolumeProperty)
  106. {
  107. Q_D(ctkVTKVolumePropertyWidget);
  108. this->qvtkReconnect(d->VolumeProperty, newVolumeProperty, vtkCommand::ModifiedEvent,
  109. this, SLOT(updateFromVolumeProperty()));
  110. d->VolumeProperty = newVolumeProperty;
  111. this->updateFromVolumeProperty();
  112. }
  113. // ----------------------------------------------------------------------------
  114. void ctkVTKVolumePropertyWidget::updateFromVolumeProperty()
  115. {
  116. Q_D(ctkVTKVolumePropertyWidget);
  117. vtkColorTransferFunction* colorTransferFunction = 0;
  118. vtkPiecewiseFunction* opacityFunction = 0;
  119. vtkPiecewiseFunction* gradientFunction = 0;
  120. if (d->VolumeProperty)
  121. {
  122. colorTransferFunction =
  123. d->VolumeProperty->GetRGBTransferFunction()->GetSize() ?
  124. d->VolumeProperty->GetRGBTransferFunction() : 0;
  125. opacityFunction =
  126. d->VolumeProperty->GetScalarOpacity()->GetSize() ?
  127. d->VolumeProperty->GetScalarOpacity() : 0;
  128. gradientFunction =
  129. d->VolumeProperty->GetGradientOpacity()->GetSize() ?
  130. d->VolumeProperty->GetGradientOpacity() : 0;
  131. }
  132. d->ScalarOpacityWidget->view()->setOpacityFunctionToPlots(opacityFunction);
  133. d->ScalarOpacityWidget->view()->setColorTransferFunctionToPlots(colorTransferFunction);
  134. d->ScalarColorWidget->view()->setColorTransferFunctionToPlots(colorTransferFunction);
  135. d->GradientWidget->view()->setPiecewiseFunctionToPlots(gradientFunction);
  136. if (d->VolumeProperty)
  137. {
  138. d->InterpolationComboBox->setCurrentIndex(
  139. d->VolumeProperty->GetInterpolationType() == VTK_NEAREST_INTERPOLATION ? 0 : 1);
  140. d->ShadeCheckBox->setChecked(
  141. d->VolumeProperty->GetShade(d->CurrentComponent));
  142. d->MaterialPropertyWidget->setAmbient(
  143. d->VolumeProperty->GetAmbient(d->CurrentComponent));
  144. d->MaterialPropertyWidget->setDiffuse(
  145. d->VolumeProperty->GetDiffuse(d->CurrentComponent));
  146. d->MaterialPropertyWidget->setSpecular(
  147. d->VolumeProperty->GetSpecular(d->CurrentComponent));
  148. d->MaterialPropertyWidget->setSpecularPower(
  149. d->VolumeProperty->GetSpecularPower(d->CurrentComponent));
  150. }
  151. }
  152. // ----------------------------------------------------------------------------
  153. void ctkVTKVolumePropertyWidget::setInterpolationMode(int mode)
  154. {
  155. Q_D(ctkVTKVolumePropertyWidget);
  156. if (!d->VolumeProperty)
  157. {
  158. return;
  159. }
  160. d->VolumeProperty->SetInterpolationType(mode);
  161. }
  162. // ----------------------------------------------------------------------------
  163. void ctkVTKVolumePropertyWidget::setShade(bool enable)
  164. {
  165. Q_D(ctkVTKVolumePropertyWidget);
  166. if (!d->VolumeProperty)
  167. {
  168. return;
  169. }
  170. d->VolumeProperty->SetShade(d->CurrentComponent, enable);
  171. }
  172. // ----------------------------------------------------------------------------
  173. void ctkVTKVolumePropertyWidget::setAmbient(double value)
  174. {
  175. Q_D(ctkVTKVolumePropertyWidget);
  176. if (!d->VolumeProperty)
  177. {
  178. return;
  179. }
  180. d->VolumeProperty->SetAmbient(d->CurrentComponent, value);
  181. }
  182. // ----------------------------------------------------------------------------
  183. void ctkVTKVolumePropertyWidget::setDiffuse(double value)
  184. {
  185. Q_D(ctkVTKVolumePropertyWidget);
  186. if (!d->VolumeProperty)
  187. {
  188. return;
  189. }
  190. d->VolumeProperty->SetDiffuse(d->CurrentComponent, value);
  191. }
  192. // ----------------------------------------------------------------------------
  193. void ctkVTKVolumePropertyWidget::setSpecular(double value)
  194. {
  195. Q_D(ctkVTKVolumePropertyWidget);
  196. if (!d->VolumeProperty)
  197. {
  198. return;
  199. }
  200. d->VolumeProperty->SetSpecular(d->CurrentComponent, value);
  201. }
  202. // ----------------------------------------------------------------------------
  203. void ctkVTKVolumePropertyWidget::setSpecularPower(double value)
  204. {
  205. Q_D(ctkVTKVolumePropertyWidget);
  206. if (!d->VolumeProperty)
  207. {
  208. return;
  209. }
  210. d->VolumeProperty->SetSpecularPower(d->CurrentComponent, value);
  211. }