ctkVTKSliceView.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #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::preRender()
  110. {
  111. Q_D(ctkVTKSliceView);
  112. vtkImageData* imageData = d->LightBoxRendererManager->GetImageData();
  113. if (imageData)
  114. {
  115. /// FIXME: Shouldn't be needed to update the pipeline here. During the
  116. /// rendering, the actors should update the pipeline via the mappers.
  117. imageData->Update();
  118. }
  119. }
  120. //----------------------------------------------------------------------------
  121. void ctkVTKSliceView::setImageData(vtkImageData* newImageData)
  122. {
  123. Q_D(ctkVTKSliceView);
  124. d->LightBoxRendererManager->SetImageData(newImageData);
  125. }
  126. //----------------------------------------------------------------------------
  127. vtkCornerAnnotation * ctkVTKSliceView::overlayCornerAnnotation()const
  128. {
  129. Q_D(const ctkVTKSliceView);
  130. return d->OverlayCornerAnnotation;
  131. }
  132. //----------------------------------------------------------------------------
  133. QColor ctkVTKSliceView::backgroundColor()const
  134. {
  135. Q_D(const ctkVTKSliceView);
  136. double* color = d->LightBoxRendererManager->GetBackgroundColor();
  137. QColor c;
  138. c.setRgbF(color[0], color[1], color[2]);
  139. return c;
  140. }
  141. //----------------------------------------------------------------------------
  142. void ctkVTKSliceView::setBackgroundColor(const QColor& newBackgroundColor)
  143. {
  144. Q_D(ctkVTKSliceView);
  145. double color[3];
  146. color[0] = newBackgroundColor.redF();
  147. color[1] = newBackgroundColor.greenF();
  148. color[2] = newBackgroundColor.blueF();
  149. d->LightBoxRendererManager->SetBackgroundColor(color);
  150. }
  151. //----------------------------------------------------------------------------
  152. QColor ctkVTKSliceView::highlightedBoxColor()const
  153. {
  154. Q_D(const ctkVTKSliceView);
  155. double* color = d->LightBoxRendererManager->GetHighlightedBoxColor();
  156. QColor c;
  157. c.setRgbF(color[0], color[1], color[2]);
  158. return c;
  159. }
  160. //----------------------------------------------------------------------------
  161. void ctkVTKSliceView::setHighlightedBoxColor(const QColor& newHighlightedBoxColor)
  162. {
  163. Q_D(ctkVTKSliceView);
  164. double color[3];
  165. color[0] = newHighlightedBoxColor.redF();
  166. color[1] = newHighlightedBoxColor.greenF();
  167. color[2] = newHighlightedBoxColor.blueF();
  168. d->LightBoxRendererManager->SetHighlightedBoxColor(color);
  169. }
  170. //----------------------------------------------------------------------------
  171. ctkVTKSliceView::RenderWindowLayoutType ctkVTKSliceView::renderWindowLayoutType()const
  172. {
  173. Q_D(const ctkVTKSliceView);
  174. return static_cast<ctkVTKSliceView::RenderWindowLayoutType>(
  175. d->LightBoxRendererManager->GetRenderWindowLayoutType());
  176. }
  177. //----------------------------------------------------------------------------
  178. void ctkVTKSliceView::setRenderWindowLayoutType(RenderWindowLayoutType layoutType)
  179. {
  180. Q_D(ctkVTKSliceView);
  181. d->LightBoxRendererManager->SetRenderWindowLayoutType(layoutType);
  182. }
  183. //----------------------------------------------------------------------------
  184. double ctkVTKSliceView::colorLevel()const
  185. {
  186. Q_D(const ctkVTKSliceView);
  187. return d->LightBoxRendererManager->GetColorLevel();
  188. }
  189. //----------------------------------------------------------------------------
  190. void ctkVTKSliceView::setColorLevel(double newColorLevel)
  191. {
  192. Q_D(ctkVTKSliceView);
  193. d->LightBoxRendererManager->SetColorLevel(newColorLevel);
  194. }
  195. //----------------------------------------------------------------------------
  196. double ctkVTKSliceView::colorWindow()const
  197. {
  198. Q_D(const ctkVTKSliceView);
  199. return d->LightBoxRendererManager->GetColorWindow();
  200. }
  201. //----------------------------------------------------------------------------
  202. void ctkVTKSliceView::setColorWindow(double newColorWindow)
  203. {
  204. Q_D(ctkVTKSliceView);
  205. d->LightBoxRendererManager->SetColorWindow(newColorWindow);
  206. }
  207. //----------------------------------------------------------------------------
  208. void ctkVTKSliceView::resizeEvent(QResizeEvent * event)
  209. {
  210. this->QWidget::resizeEvent(event);
  211. emit this->resized(event->size());
  212. }
  213. //----------------------------------------------------------------------------
  214. void ctkVTKSliceView::setLightBoxRendererManagerRowCount(int newRowCount)
  215. {
  216. Q_D(ctkVTKSliceView);
  217. d->LightBoxRendererManager->SetRenderWindowRowCount(newRowCount);
  218. }
  219. //----------------------------------------------------------------------------
  220. void ctkVTKSliceView::setLightBoxRendererManagerColumnCount(int newColumnCount)
  221. {
  222. Q_D(ctkVTKSliceView);
  223. d->LightBoxRendererManager->SetRenderWindowColumnCount(newColumnCount);
  224. }