ctkVTKSliceView.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 <QResizeEvent>
  17. // CTK includes
  18. #include "ctkVTKSliceView.h"
  19. #include "ctkVTKSliceView_p.h"
  20. #include "ctkLogger.h"
  21. // VTK includes
  22. #include <vtkRendererCollection.h>
  23. #include <vtkRenderWindowInteractor.h>
  24. #include <vtkTextProperty.h>
  25. #include <vtkProperty2D.h>
  26. #include <vtkCamera.h>
  27. #include <vtkImageData.h>
  28. #include <vtkCellArray.h>
  29. #include <vtkPoints.h>
  30. #include <vtkPolyData.h>
  31. #include <vtkPolyDataMapper2D.h>
  32. // Convenient macro
  33. #define VTK_CREATE(type, name) \
  34. vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
  35. //--------------------------------------------------------------------------
  36. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKSliceView");
  37. //--------------------------------------------------------------------------
  38. // --------------------------------------------------------------------------
  39. // ctkVTKSliceViewPrivate methods
  40. // --------------------------------------------------------------------------
  41. ctkVTKSliceViewPrivate::ctkVTKSliceViewPrivate(ctkVTKSliceView& object)
  42. : ctkVTKAbstractViewPrivate(object)
  43. {
  44. this->LightBoxRendererManager = vtkSmartPointer<vtkLightBoxRendererManager>::New();
  45. this->OverlayRenderer = vtkSmartPointer<vtkRenderer>::New();
  46. }
  47. // --------------------------------------------------------------------------
  48. void ctkVTKSliceViewPrivate::setupCornerAnnotation()
  49. {
  50. logger.trace("setupCornerAnnotation");
  51. // whatever is done in ctkVTKAbsrtactViewPrivate would be overriden anyway
  52. this->ctkVTKAbstractViewPrivate::setupCornerAnnotation();
  53. this->LightBoxRendererManager->SetCornerAnnotation(this->CornerAnnotation);
  54. }
  55. //---------------------------------------------------------------------------
  56. void ctkVTKSliceViewPrivate::setupRendering()
  57. {
  58. Q_ASSERT(this->RenderWindow);
  59. this->RenderWindow->SetNumberOfLayers(2);
  60. // Initialize light box
  61. this->LightBoxRendererManager->Initialize(this->RenderWindow);
  62. this->LightBoxRendererManager->SetRenderWindowLayout(1, 1);
  63. // Setup overlay renderer
  64. this->OverlayRenderer->SetLayer(1);
  65. this->RenderWindow->AddRenderer(this->OverlayRenderer);
  66. // Create cornerAnnotation and set its default property
  67. this->OverlayCornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
  68. this->OverlayCornerAnnotation->SetMaximumLineHeight(0.07);
  69. vtkTextProperty *tprop = this->OverlayCornerAnnotation->GetTextProperty();
  70. tprop->ShadowOn();
  71. this->OverlayCornerAnnotation->ClearAllTexts();
  72. // Add corner annotation to overlay renderer
  73. this->OverlayRenderer->AddViewProp(this->OverlayCornerAnnotation);
  74. this->ctkVTKAbstractViewPrivate::setupRendering();
  75. }
  76. //---------------------------------------------------------------------------
  77. // ctkVTKSliceView methods
  78. // --------------------------------------------------------------------------
  79. ctkVTKSliceView::ctkVTKSliceView(QWidget* parentWidget)
  80. : Superclass(new ctkVTKSliceViewPrivate(*this), parentWidget)
  81. {
  82. Q_D(ctkVTKSliceView);
  83. d->init();
  84. }
  85. // --------------------------------------------------------------------------
  86. ctkVTKSliceView::~ctkVTKSliceView()
  87. {
  88. }
  89. //----------------------------------------------------------------------------
  90. void ctkVTKSliceView::setActiveCamera(vtkCamera * newActiveCamera)
  91. {
  92. Q_D(ctkVTKSliceView);
  93. d->LightBoxRendererManager->SetActiveCamera(newActiveCamera);
  94. d->OverlayRenderer->SetActiveCamera(newActiveCamera);
  95. }
  96. //----------------------------------------------------------------------------
  97. CTK_GET_CPP(ctkVTKSliceView, vtkLightBoxRendererManager*,
  98. lightBoxRendererManager, LightBoxRendererManager);
  99. //----------------------------------------------------------------------------
  100. CTK_GET_CPP(ctkVTKSliceView, vtkRenderer*, overlayRenderer, OverlayRenderer);
  101. //----------------------------------------------------------------------------
  102. void ctkVTKSliceView::resetCamera()
  103. {
  104. Q_D(ctkVTKSliceView);
  105. d->OverlayRenderer->ResetCamera();
  106. d->LightBoxRendererManager->ResetCamera();
  107. }
  108. //----------------------------------------------------------------------------
  109. void ctkVTKSliceView::setImageData(vtkImageData* newImageData)
  110. {
  111. Q_D(ctkVTKSliceView);
  112. d->LightBoxRendererManager->SetImageData(newImageData);
  113. }
  114. //----------------------------------------------------------------------------
  115. vtkCornerAnnotation * ctkVTKSliceView::overlayCornerAnnotation()const
  116. {
  117. Q_D(const ctkVTKSliceView);
  118. return d->OverlayCornerAnnotation;
  119. }
  120. //----------------------------------------------------------------------------
  121. QColor ctkVTKSliceView::backgroundColor()const
  122. {
  123. Q_D(const ctkVTKSliceView);
  124. double* color = d->LightBoxRendererManager->GetBackgroundColor();
  125. QColor c;
  126. c.setRgbF(color[0], color[1], color[2]);
  127. return c;
  128. }
  129. //----------------------------------------------------------------------------
  130. void ctkVTKSliceView::setBackgroundColor(const QColor& newBackgroundColor)
  131. {
  132. Q_D(ctkVTKSliceView);
  133. double color[3];
  134. color[0] = newBackgroundColor.redF();
  135. color[1] = newBackgroundColor.greenF();
  136. color[2] = newBackgroundColor.blueF();
  137. d->LightBoxRendererManager->SetBackgroundColor(color);
  138. }
  139. //----------------------------------------------------------------------------
  140. QColor ctkVTKSliceView::highlightedBoxColor()const
  141. {
  142. Q_D(const ctkVTKSliceView);
  143. double* color = d->LightBoxRendererManager->GetHighlightedBoxColor();
  144. QColor c;
  145. c.setRgbF(color[0], color[1], color[2]);
  146. return c;
  147. }
  148. //----------------------------------------------------------------------------
  149. void ctkVTKSliceView::setHighlightedBoxColor(const QColor& newHighlightedBoxColor)
  150. {
  151. Q_D(ctkVTKSliceView);
  152. double color[3];
  153. color[0] = newHighlightedBoxColor.redF();
  154. color[1] = newHighlightedBoxColor.greenF();
  155. color[2] = newHighlightedBoxColor.blueF();
  156. d->LightBoxRendererManager->SetHighlightedBoxColor(color);
  157. }
  158. //----------------------------------------------------------------------------
  159. ctkVTKSliceView::RenderWindowLayoutType ctkVTKSliceView::renderWindowLayoutType()const
  160. {
  161. Q_D(const ctkVTKSliceView);
  162. return static_cast<ctkVTKSliceView::RenderWindowLayoutType>(
  163. d->LightBoxRendererManager->GetRenderWindowLayoutType());
  164. }
  165. //----------------------------------------------------------------------------
  166. void ctkVTKSliceView::setRenderWindowLayoutType(RenderWindowLayoutType layoutType)
  167. {
  168. Q_D(ctkVTKSliceView);
  169. d->LightBoxRendererManager->SetRenderWindowLayoutType(layoutType);
  170. }
  171. //----------------------------------------------------------------------------
  172. double ctkVTKSliceView::colorLevel()const
  173. {
  174. Q_D(const ctkVTKSliceView);
  175. return d->LightBoxRendererManager->GetColorLevel();
  176. }
  177. //----------------------------------------------------------------------------
  178. void ctkVTKSliceView::setColorLevel(double newColorLevel)
  179. {
  180. Q_D(ctkVTKSliceView);
  181. d->LightBoxRendererManager->SetColorLevel(newColorLevel);
  182. }
  183. //----------------------------------------------------------------------------
  184. double ctkVTKSliceView::colorWindow()const
  185. {
  186. Q_D(const ctkVTKSliceView);
  187. return d->LightBoxRendererManager->GetColorWindow();
  188. }
  189. //----------------------------------------------------------------------------
  190. void ctkVTKSliceView::setColorWindow(double newColorWindow)
  191. {
  192. Q_D(ctkVTKSliceView);
  193. d->LightBoxRendererManager->SetColorWindow(newColorWindow);
  194. }
  195. //----------------------------------------------------------------------------
  196. void ctkVTKSliceView::resizeEvent(QResizeEvent * event)
  197. {
  198. this->QWidget::resizeEvent(event);
  199. emit this->resized(event->size());
  200. }
  201. //----------------------------------------------------------------------------
  202. void ctkVTKSliceView::setLightBoxRendererManagerRowCount(int newRowCount)
  203. {
  204. Q_D(ctkVTKSliceView);
  205. d->LightBoxRendererManager->SetRenderWindowRowCount(newRowCount);
  206. }
  207. //----------------------------------------------------------------------------
  208. void ctkVTKSliceView::setLightBoxRendererManagerColumnCount(int newColumnCount)
  209. {
  210. Q_D(ctkVTKSliceView);
  211. d->LightBoxRendererManager->SetRenderWindowColumnCount(newColumnCount);
  212. }