ctkVTKSliceView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 <QTimer>
  16. #include <QVBoxLayout>
  17. #include <QDebug>
  18. #include <QResizeEvent>
  19. // CTK includes
  20. #include "ctkVTKSliceView.h"
  21. #include "ctkVTKSliceView_p.h"
  22. #include "ctkLogger.h"
  23. // VTK includes
  24. #include <vtkRendererCollection.h>
  25. #include <vtkRenderWindowInteractor.h>
  26. #include <vtkTextProperty.h>
  27. #include <vtkProperty2D.h>
  28. #include <vtkCamera.h>
  29. #include <vtkImageData.h>
  30. #include <vtkCellArray.h>
  31. #include <vtkPoints.h>
  32. #include <vtkPolyData.h>
  33. #include <vtkPolyDataMapper2D.h>
  34. // Convenient macro
  35. #define VTK_CREATE(type, name) \
  36. vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
  37. //--------------------------------------------------------------------------
  38. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKSliceView");
  39. //--------------------------------------------------------------------------
  40. // --------------------------------------------------------------------------
  41. // ctkVTKSliceViewPrivate methods
  42. // --------------------------------------------------------------------------
  43. ctkVTKSliceViewPrivate::ctkVTKSliceViewPrivate()
  44. {
  45. this->RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  46. this->LightBoxRendererManager = vtkSmartPointer<vtkLightBoxRendererManager>::New();
  47. this->RenderPending = false;
  48. this->RenderEnabled = false;
  49. }
  50. //---------------------------------------------------------------------------
  51. void ctkVTKSliceViewPrivate::setupRendering()
  52. {
  53. Q_ASSERT(this->RenderWindow);
  54. this->RenderWindow->SetAlphaBitPlanes(1);
  55. this->RenderWindow->SetMultiSamples(0);
  56. this->RenderWindow->StereoCapableWindowOn();
  57. this->RenderWindow->SetNumberOfLayers(2);
  58. // Initialize light box
  59. this->LightBoxRendererManager->Initialize(this->RenderWindow);
  60. // Setup overlay renderer
  61. this->OverlayRenderer = vtkSmartPointer<vtkRenderer>::New();
  62. this->OverlayRenderer->SetLayer(1);
  63. this->RenderWindow->AddRenderer(this->OverlayRenderer);
  64. // Create cornerAnnotation and set its default property
  65. this->OverlayCornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
  66. this->OverlayCornerAnnotation->SetMaximumLineHeight(0.07);
  67. vtkTextProperty *tprop = this->OverlayCornerAnnotation->GetTextProperty();
  68. tprop->ShadowOn();
  69. this->OverlayCornerAnnotation->ClearAllTexts();
  70. // Add corner annotation to overlay renderer
  71. this->OverlayRenderer->AddViewProp(this->OverlayCornerAnnotation);
  72. this->VTKWidget->SetRenderWindow(this->RenderWindow);
  73. }
  74. //---------------------------------------------------------------------------
  75. // ctkVTKSliceView methods
  76. // --------------------------------------------------------------------------
  77. ctkVTKSliceView::ctkVTKSliceView(QWidget* _parent) : Superclass(_parent)
  78. , d_ptr(new ctkVTKSliceViewPrivate)
  79. {
  80. Q_D(ctkVTKSliceView);
  81. d->VTKWidget = new QVTKWidget(this);
  82. this->setLayout(new QVBoxLayout);
  83. this->layout()->setMargin(0);
  84. this->layout()->setSpacing(0);
  85. this->layout()->addWidget(d->VTKWidget);
  86. d->setupRendering();
  87. d->LightBoxRendererManager->SetRenderWindowLayout(1, 1);
  88. }
  89. // --------------------------------------------------------------------------
  90. ctkVTKSliceView::~ctkVTKSliceView()
  91. {
  92. }
  93. //----------------------------------------------------------------------------
  94. void ctkVTKSliceView::scheduleRender()
  95. {
  96. Q_D(ctkVTKSliceView);
  97. logger.trace("scheduleRender");
  98. if (!d->RenderEnabled)
  99. {
  100. return;
  101. }
  102. if (!d->RenderPending)
  103. {
  104. d->RenderPending = true;
  105. QTimer::singleShot(0, this, SLOT(forceRender()));
  106. }
  107. }
  108. //----------------------------------------------------------------------------
  109. void ctkVTKSliceView::forceRender()
  110. {
  111. Q_D(ctkVTKSliceView);
  112. if (!d->RenderEnabled || !this->isVisible())
  113. {
  114. return;
  115. }
  116. logger.trace("forceRender");
  117. d->RenderWindow->Render();
  118. d->RenderPending = false;
  119. }
  120. //----------------------------------------------------------------------------
  121. CTK_GET_CPP(ctkVTKSliceView, vtkRenderWindow*, renderWindow, RenderWindow);
  122. //----------------------------------------------------------------------------
  123. void ctkVTKSliceView::setActiveCamera(vtkCamera * newActiveCamera)
  124. {
  125. Q_D(ctkVTKSliceView);
  126. d->LightBoxRendererManager->SetActiveCamera(newActiveCamera);
  127. d->OverlayRenderer->SetActiveCamera(newActiveCamera);
  128. }
  129. //----------------------------------------------------------------------------
  130. CTK_GET_CPP(ctkVTKSliceView, vtkLightBoxRendererManager*,
  131. lightBoxRendererManager, LightBoxRendererManager);
  132. //----------------------------------------------------------------------------
  133. CTK_GET_CPP(ctkVTKSliceView, vtkRenderer*, overlayRenderer, OverlayRenderer);
  134. //----------------------------------------------------------------------------
  135. CTK_SET_CPP(ctkVTKSliceView, bool, setRenderEnabled, RenderEnabled);
  136. CTK_GET_CPP(ctkVTKSliceView, bool, renderEnabled, RenderEnabled);
  137. //----------------------------------------------------------------------------
  138. vtkRenderWindowInteractor* ctkVTKSliceView::interactor() const
  139. {
  140. Q_D(const ctkVTKSliceView);
  141. return d->RenderWindow->GetInteractor();
  142. }
  143. //----------------------------------------------------------------------------
  144. void ctkVTKSliceView::setInteractor(vtkRenderWindowInteractor* newInteractor)
  145. {
  146. Q_D(const ctkVTKSliceView);
  147. d->RenderWindow->SetInteractor(newInteractor);
  148. }
  149. //----------------------------------------------------------------------------
  150. vtkInteractorObserver* ctkVTKSliceView::interactorStyle()const
  151. {
  152. Q_D(const ctkVTKSliceView);
  153. if (!d->RenderWindow->GetInteractor())
  154. {
  155. return 0;
  156. }
  157. return d->RenderWindow->GetInteractor()->GetInteractorStyle();
  158. }
  159. //----------------------------------------------------------------------------
  160. void ctkVTKSliceView::resetCamera()
  161. {
  162. Q_D(ctkVTKSliceView);
  163. d->OverlayRenderer->ResetCamera();
  164. d->LightBoxRendererManager->ResetCamera();
  165. }
  166. //----------------------------------------------------------------------------
  167. void ctkVTKSliceView::setImageData(vtkImageData* newImageData)
  168. {
  169. Q_D(ctkVTKSliceView);
  170. d->LightBoxRendererManager->SetImageData(newImageData);
  171. }
  172. //----------------------------------------------------------------------------
  173. QString ctkVTKSliceView::cornerAnnotationText()const
  174. {
  175. Q_D(const ctkVTKSliceView);
  176. return QString::fromStdString(d->LightBoxRendererManager->GetCornerAnnotationText());
  177. }
  178. //----------------------------------------------------------------------------
  179. vtkCornerAnnotation * ctkVTKSliceView::cornerAnnotation()const
  180. {
  181. Q_D(const ctkVTKSliceView);
  182. return d->LightBoxRendererManager->GetCornerAnnotation();
  183. }
  184. //----------------------------------------------------------------------------
  185. void ctkVTKSliceView::setCornerAnnotationText(const QString& text)
  186. {
  187. Q_D(ctkVTKSliceView);
  188. d->LightBoxRendererManager->SetCornerAnnotationText(text.toStdString());
  189. }
  190. //----------------------------------------------------------------------------
  191. vtkCornerAnnotation * ctkVTKSliceView::overlayCornerAnnotation()const
  192. {
  193. Q_D(const ctkVTKSliceView);
  194. return d->OverlayCornerAnnotation;
  195. }
  196. //----------------------------------------------------------------------------
  197. QColor ctkVTKSliceView::backgroundColor()const
  198. {
  199. Q_D(const ctkVTKSliceView);
  200. double* color = d->LightBoxRendererManager->GetBackgroundColor();
  201. QColor c;
  202. c.setRgbF(color[0], color[1], color[2]);
  203. return c;
  204. }
  205. //----------------------------------------------------------------------------
  206. void ctkVTKSliceView::setBackgroundColor(const QColor& newBackgroundColor)
  207. {
  208. Q_D(ctkVTKSliceView);
  209. double color[3];
  210. color[0] = newBackgroundColor.redF();
  211. color[1] = newBackgroundColor.greenF();
  212. color[2] = newBackgroundColor.blueF();
  213. d->LightBoxRendererManager->SetBackgroundColor(color);
  214. }
  215. //----------------------------------------------------------------------------
  216. QColor ctkVTKSliceView::highlightedBoxColor()const
  217. {
  218. Q_D(const ctkVTKSliceView);
  219. double* color = d->LightBoxRendererManager->GetHighlightedBoxColor();
  220. QColor c;
  221. c.setRgbF(color[0], color[1], color[2]);
  222. return c;
  223. }
  224. //----------------------------------------------------------------------------
  225. void ctkVTKSliceView::setHighlightedBoxColor(const QColor& newHighlightedBoxColor)
  226. {
  227. Q_D(ctkVTKSliceView);
  228. double color[3];
  229. color[0] = newHighlightedBoxColor.redF();
  230. color[1] = newHighlightedBoxColor.greenF();
  231. color[2] = newHighlightedBoxColor.blueF();
  232. d->LightBoxRendererManager->SetHighlightedBoxColor(color);
  233. }
  234. //----------------------------------------------------------------------------
  235. ctkVTKSliceView::RenderWindowLayoutType ctkVTKSliceView::renderWindowLayoutType()const
  236. {
  237. Q_D(const ctkVTKSliceView);
  238. return static_cast<ctkVTKSliceView::RenderWindowLayoutType>(
  239. d->LightBoxRendererManager->GetRenderWindowLayoutType());
  240. }
  241. //----------------------------------------------------------------------------
  242. void ctkVTKSliceView::setRenderWindowLayoutType(RenderWindowLayoutType layoutType)
  243. {
  244. Q_D(ctkVTKSliceView);
  245. d->LightBoxRendererManager->SetRenderWindowLayoutType(layoutType);
  246. }
  247. //----------------------------------------------------------------------------
  248. double ctkVTKSliceView::colorLevel()const
  249. {
  250. Q_D(const ctkVTKSliceView);
  251. return d->LightBoxRendererManager->GetColorLevel();
  252. }
  253. //----------------------------------------------------------------------------
  254. void ctkVTKSliceView::setColorLevel(double newColorLevel)
  255. {
  256. Q_D(ctkVTKSliceView);
  257. d->LightBoxRendererManager->SetColorLevel(newColorLevel);
  258. }
  259. //----------------------------------------------------------------------------
  260. double ctkVTKSliceView::colorWindow()const
  261. {
  262. Q_D(const ctkVTKSliceView);
  263. return d->LightBoxRendererManager->GetColorWindow();
  264. }
  265. //----------------------------------------------------------------------------
  266. void ctkVTKSliceView::setColorWindow(double newColorWindow)
  267. {
  268. Q_D(ctkVTKSliceView);
  269. d->LightBoxRendererManager->SetColorWindow(newColorWindow);
  270. }
  271. //----------------------------------------------------------------------------
  272. void ctkVTKSliceView::resizeEvent(QResizeEvent * event)
  273. {
  274. this->QWidget::resizeEvent(event);
  275. emit this->resized(event->size(), event->oldSize());
  276. }
  277. //----------------------------------------------------------------------------
  278. void ctkVTKSliceView::setLightBoxRendererManagerRowCount(int newRowCount)
  279. {
  280. Q_D(ctkVTKSliceView);
  281. d->LightBoxRendererManager->SetRenderWindowRowCount(newRowCount);
  282. }
  283. //----------------------------------------------------------------------------
  284. void ctkVTKSliceView::setLightBoxRendererManagerColumnCount(int newColumnCount)
  285. {
  286. Q_D(ctkVTKSliceView);
  287. d->LightBoxRendererManager->SetRenderWindowColumnCount(newColumnCount);
  288. }