ctkVTKSliceView.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "ctkPimpl.h"
  19. #include "ctkVTKSliceView.h"
  20. #include "ctkVTKSliceView_p.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. // --------------------------------------------------------------------------
  33. // ctkVTKSliceViewPrivate methods
  34. // --------------------------------------------------------------------------
  35. ctkVTKSliceViewPrivate::ctkVTKSliceViewPrivate(ctkVTKSliceView& object)
  36. : ctkVTKAbstractViewPrivate(object)
  37. {
  38. this->LightBoxRendererManager = vtkSmartPointer<vtkLightBoxRendererManager>::New();
  39. this->OverlayRenderer = vtkSmartPointer<vtkRenderer>::New();
  40. }
  41. // --------------------------------------------------------------------------
  42. void ctkVTKSliceViewPrivate::setupCornerAnnotation()
  43. {
  44. this->ctkVTKAbstractViewPrivate::setupCornerAnnotation();
  45. this->LightBoxRendererManager->SetCornerAnnotation(this->CornerAnnotation);
  46. }
  47. //---------------------------------------------------------------------------
  48. void ctkVTKSliceViewPrivate::setupRendering()
  49. {
  50. Q_ASSERT(this->RenderWindow);
  51. this->RenderWindow->SetNumberOfLayers(2);
  52. // Initialize light box
  53. this->LightBoxRendererManager->Initialize(this->RenderWindow);
  54. this->LightBoxRendererManager->SetRenderWindowLayout(1, 1);
  55. // Setup overlay renderer
  56. this->OverlayRenderer->SetLayer(1);
  57. this->RenderWindow->AddRenderer(this->OverlayRenderer);
  58. // Parallel projection is needed to prevent actors from warping/tilting
  59. // when they are near the edge of the window.
  60. vtkCamera* camera = this->OverlayRenderer->GetActiveCamera();
  61. if (camera)
  62. {
  63. camera->ParallelProjectionOn();
  64. }
  65. // Create cornerAnnotation and set its default property
  66. this->OverlayCornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
  67. this->OverlayCornerAnnotation->SetMaximumLineHeight(0.07);
  68. vtkTextProperty *tprop = this->OverlayCornerAnnotation->GetTextProperty();
  69. tprop->ShadowOn();
  70. this->OverlayCornerAnnotation->ClearAllTexts();
  71. // Add corner annotation to overlay renderer
  72. this->OverlayRenderer->AddViewProp(this->OverlayCornerAnnotation);
  73. this->ctkVTKAbstractViewPrivate::setupRendering();
  74. }
  75. //---------------------------------------------------------------------------
  76. // ctkVTKSliceView methods
  77. // --------------------------------------------------------------------------
  78. ctkVTKSliceView::ctkVTKSliceView(QWidget* parentWidget)
  79. : Superclass(new ctkVTKSliceViewPrivate(*this), parentWidget)
  80. {
  81. Q_D(ctkVTKSliceView);
  82. d->init();
  83. this->VTKWidget()->installEventFilter(this);
  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. #if (VTK_MAJOR_VERSION <= 5)
  110. void ctkVTKSliceView::setImageData(vtkImageData* newImageData)
  111. {
  112. Q_D(ctkVTKSliceView);
  113. d->LightBoxRendererManager->SetImageData(newImageData);
  114. }
  115. #else
  116. void ctkVTKSliceView::setImageDataConnection(vtkAlgorithmOutput* newImageDataPort)
  117. {
  118. Q_D(ctkVTKSliceView);
  119. d->LightBoxRendererManager->SetImageDataConnection(newImageDataPort);
  120. }
  121. #endif
  122. //----------------------------------------------------------------------------
  123. vtkCornerAnnotation * ctkVTKSliceView::overlayCornerAnnotation()const
  124. {
  125. Q_D(const ctkVTKSliceView);
  126. return d->OverlayCornerAnnotation;
  127. }
  128. //----------------------------------------------------------------------------
  129. QColor ctkVTKSliceView::backgroundColor()const
  130. {
  131. Q_D(const ctkVTKSliceView);
  132. double* color = d->LightBoxRendererManager->GetBackgroundColor();
  133. QColor c;
  134. c.setRgbF(color[0], color[1], color[2]);
  135. return c;
  136. }
  137. //----------------------------------------------------------------------------
  138. void ctkVTKSliceView::setBackgroundColor(const QColor& newBackgroundColor)
  139. {
  140. Q_D(ctkVTKSliceView);
  141. double color[3];
  142. color[0] = newBackgroundColor.redF();
  143. color[1] = newBackgroundColor.greenF();
  144. color[2] = newBackgroundColor.blueF();
  145. d->LightBoxRendererManager->SetBackgroundColor(color);
  146. }
  147. //----------------------------------------------------------------------------
  148. QColor ctkVTKSliceView::highlightedBoxColor()const
  149. {
  150. Q_D(const ctkVTKSliceView);
  151. double* color = d->LightBoxRendererManager->GetHighlightedBoxColor();
  152. QColor c;
  153. c.setRgbF(color[0], color[1], color[2]);
  154. return c;
  155. }
  156. //----------------------------------------------------------------------------
  157. void ctkVTKSliceView::setHighlightedBoxColor(const QColor& newHighlightedBoxColor)
  158. {
  159. Q_D(ctkVTKSliceView);
  160. double color[3];
  161. color[0] = newHighlightedBoxColor.redF();
  162. color[1] = newHighlightedBoxColor.greenF();
  163. color[2] = newHighlightedBoxColor.blueF();
  164. d->LightBoxRendererManager->SetHighlightedBoxColor(color);
  165. }
  166. //----------------------------------------------------------------------------
  167. ctkVTKSliceView::RenderWindowLayoutType ctkVTKSliceView::renderWindowLayoutType()const
  168. {
  169. Q_D(const ctkVTKSliceView);
  170. return static_cast<ctkVTKSliceView::RenderWindowLayoutType>(
  171. d->LightBoxRendererManager->GetRenderWindowLayoutType());
  172. }
  173. //----------------------------------------------------------------------------
  174. void ctkVTKSliceView::setRenderWindowLayoutType(RenderWindowLayoutType layoutType)
  175. {
  176. Q_D(ctkVTKSliceView);
  177. d->LightBoxRendererManager->SetRenderWindowLayoutType(layoutType);
  178. }
  179. //----------------------------------------------------------------------------
  180. double ctkVTKSliceView::colorLevel()const
  181. {
  182. Q_D(const ctkVTKSliceView);
  183. return d->LightBoxRendererManager->GetColorLevel();
  184. }
  185. //----------------------------------------------------------------------------
  186. void ctkVTKSliceView::setColorLevel(double newColorLevel)
  187. {
  188. Q_D(ctkVTKSliceView);
  189. d->LightBoxRendererManager->SetColorLevel(newColorLevel);
  190. }
  191. //----------------------------------------------------------------------------
  192. double ctkVTKSliceView::colorWindow()const
  193. {
  194. Q_D(const ctkVTKSliceView);
  195. return d->LightBoxRendererManager->GetColorWindow();
  196. }
  197. //----------------------------------------------------------------------------
  198. void ctkVTKSliceView::setColorWindow(double newColorWindow)
  199. {
  200. Q_D(ctkVTKSliceView);
  201. d->LightBoxRendererManager->SetColorWindow(newColorWindow);
  202. }
  203. //----------------------------------------------------------------------------
  204. bool ctkVTKSliceView::eventFilter(QObject *object, QEvent *event)
  205. {
  206. if (object == this->VTKWidget())
  207. {
  208. if (event->type() == QEvent::Resize)
  209. {
  210. QResizeEvent * resizeEvent = dynamic_cast<QResizeEvent*>(event);
  211. object->event(event);
  212. emit this->resized(resizeEvent->size());
  213. return true;
  214. }
  215. return false;
  216. }
  217. else
  218. {
  219. return this->Superclass::eventFilter(object, event);
  220. }
  221. }
  222. //----------------------------------------------------------------------------
  223. void ctkVTKSliceView::setLightBoxRendererManagerRowCount(int newRowCount)
  224. {
  225. Q_D(ctkVTKSliceView);
  226. d->LightBoxRendererManager->SetRenderWindowRowCount(newRowCount);
  227. }
  228. //----------------------------------------------------------------------------
  229. void ctkVTKSliceView::setLightBoxRendererManagerColumnCount(int newColumnCount)
  230. {
  231. Q_D(ctkVTKSliceView);
  232. d->LightBoxRendererManager->SetRenderWindowColumnCount(newColumnCount);
  233. }