ctkVTKSliceView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. CTK_GET_CPP(ctkVTKSliceView, vtkLightBoxRendererManager*,
  124. lightBoxRendererManager, LightBoxRendererManager);
  125. //----------------------------------------------------------------------------
  126. CTK_GET_CPP(ctkVTKSliceView, vtkRenderer*, overlayRenderer, OverlayRenderer);
  127. //----------------------------------------------------------------------------
  128. CTK_SET_CPP(ctkVTKSliceView, bool, setRenderEnabled, RenderEnabled);
  129. CTK_GET_CPP(ctkVTKSliceView, bool, renderEnabled, RenderEnabled);
  130. //----------------------------------------------------------------------------
  131. vtkRenderWindowInteractor* ctkVTKSliceView::interactor() const
  132. {
  133. Q_D(const ctkVTKSliceView);
  134. return d->RenderWindow->GetInteractor();
  135. }
  136. //----------------------------------------------------------------------------
  137. void ctkVTKSliceView::setInteractor(vtkRenderWindowInteractor* newInteractor)
  138. {
  139. Q_D(const ctkVTKSliceView);
  140. d->RenderWindow->SetInteractor(newInteractor);
  141. }
  142. //----------------------------------------------------------------------------
  143. vtkInteractorObserver* ctkVTKSliceView::interactorStyle()const
  144. {
  145. Q_D(const ctkVTKSliceView);
  146. if (!d->RenderWindow->GetInteractor())
  147. {
  148. return 0;
  149. }
  150. return d->RenderWindow->GetInteractor()->GetInteractorStyle();
  151. }
  152. //----------------------------------------------------------------------------
  153. void ctkVTKSliceView::resetCamera()
  154. {
  155. Q_D(ctkVTKSliceView);
  156. d->LightBoxRendererManager->ResetCamera();
  157. }
  158. //----------------------------------------------------------------------------
  159. void ctkVTKSliceView::setImageData(vtkImageData* newImageData)
  160. {
  161. Q_D(ctkVTKSliceView);
  162. d->LightBoxRendererManager->SetImageData(newImageData);
  163. }
  164. //----------------------------------------------------------------------------
  165. QString ctkVTKSliceView::cornerAnnotationText()const
  166. {
  167. Q_D(const ctkVTKSliceView);
  168. return QString::fromStdString(d->LightBoxRendererManager->GetCornerAnnotationText());
  169. }
  170. //----------------------------------------------------------------------------
  171. vtkCornerAnnotation * ctkVTKSliceView::cornerAnnotation()const
  172. {
  173. Q_D(const ctkVTKSliceView);
  174. return d->LightBoxRendererManager->GetCornerAnnotation();
  175. }
  176. //----------------------------------------------------------------------------
  177. void ctkVTKSliceView::setCornerAnnotationText(const QString& text)
  178. {
  179. Q_D(ctkVTKSliceView);
  180. d->LightBoxRendererManager->SetCornerAnnotationText(text.toStdString());
  181. }
  182. //----------------------------------------------------------------------------
  183. vtkCornerAnnotation * ctkVTKSliceView::overlayCornerAnnotation()const
  184. {
  185. Q_D(const ctkVTKSliceView);
  186. return d->OverlayCornerAnnotation;
  187. }
  188. //----------------------------------------------------------------------------
  189. QColor ctkVTKSliceView::backgroundColor()const
  190. {
  191. Q_D(const ctkVTKSliceView);
  192. double* color = d->LightBoxRendererManager->GetBackgroundColor();
  193. QColor c;
  194. c.setRgbF(color[0], color[1], color[2]);
  195. return c;
  196. }
  197. //----------------------------------------------------------------------------
  198. void ctkVTKSliceView::setBackgroundColor(const QColor& newBackgroundColor)
  199. {
  200. Q_D(ctkVTKSliceView);
  201. double color[3];
  202. color[0] = newBackgroundColor.redF();
  203. color[1] = newBackgroundColor.greenF();
  204. color[2] = newBackgroundColor.blueF();
  205. d->LightBoxRendererManager->SetBackgroundColor(color);
  206. }
  207. //----------------------------------------------------------------------------
  208. QColor ctkVTKSliceView::highlightedBoxColor()const
  209. {
  210. Q_D(const ctkVTKSliceView);
  211. double* color = d->LightBoxRendererManager->GetHighlightedBoxColor();
  212. QColor c;
  213. c.setRgbF(color[0], color[1], color[2]);
  214. return c;
  215. }
  216. //----------------------------------------------------------------------------
  217. void ctkVTKSliceView::setHighlightedBoxColor(const QColor& newHighlightedBoxColor)
  218. {
  219. Q_D(ctkVTKSliceView);
  220. double color[3];
  221. color[0] = newHighlightedBoxColor.redF();
  222. color[1] = newHighlightedBoxColor.greenF();
  223. color[2] = newHighlightedBoxColor.blueF();
  224. d->LightBoxRendererManager->SetHighlightedBoxColor(color);
  225. }
  226. //----------------------------------------------------------------------------
  227. ctkVTKSliceView::RenderWindowLayoutType ctkVTKSliceView::renderWindowLayoutType()const
  228. {
  229. Q_D(const ctkVTKSliceView);
  230. return static_cast<ctkVTKSliceView::RenderWindowLayoutType>(
  231. d->LightBoxRendererManager->GetRenderWindowLayoutType());
  232. }
  233. //----------------------------------------------------------------------------
  234. void ctkVTKSliceView::setRenderWindowLayoutType(RenderWindowLayoutType layoutType)
  235. {
  236. Q_D(ctkVTKSliceView);
  237. d->LightBoxRendererManager->SetRenderWindowLayoutType(layoutType);
  238. }
  239. //----------------------------------------------------------------------------
  240. double ctkVTKSliceView::colorLevel()const
  241. {
  242. Q_D(const ctkVTKSliceView);
  243. return d->LightBoxRendererManager->GetColorLevel();
  244. }
  245. //----------------------------------------------------------------------------
  246. void ctkVTKSliceView::setColorLevel(double newColorLevel)
  247. {
  248. Q_D(ctkVTKSliceView);
  249. d->LightBoxRendererManager->SetColorLevel(newColorLevel);
  250. }
  251. //----------------------------------------------------------------------------
  252. double ctkVTKSliceView::colorWindow()const
  253. {
  254. Q_D(const ctkVTKSliceView);
  255. return d->LightBoxRendererManager->GetColorWindow();
  256. }
  257. //----------------------------------------------------------------------------
  258. void ctkVTKSliceView::setColorWindow(double newColorWindow)
  259. {
  260. Q_D(ctkVTKSliceView);
  261. d->LightBoxRendererManager->SetColorWindow(newColorWindow);
  262. }
  263. //----------------------------------------------------------------------------
  264. void ctkVTKSliceView::resizeEvent(QResizeEvent * event)
  265. {
  266. this->QWidget::resizeEvent(event);
  267. emit this->resized(event->size(), event->oldSize());
  268. }
  269. //----------------------------------------------------------------------------
  270. void ctkVTKSliceView::setLightBoxRendererManagerRowCount(int newRowCount)
  271. {
  272. Q_D(ctkVTKSliceView);
  273. d->LightBoxRendererManager->SetRenderWindowRowCount(newRowCount);
  274. }
  275. //----------------------------------------------------------------------------
  276. void ctkVTKSliceView::setLightBoxRendererManagerColumnCount(int newColumnCount)
  277. {
  278. Q_D(ctkVTKSliceView);
  279. d->LightBoxRendererManager->SetRenderWindowColumnCount(newColumnCount);
  280. }