ctkVTKSliceView.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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->LightBoxRendererManager->Initialize(this->RenderWindow);
  58. this->VTKWidget->SetRenderWindow(this->RenderWindow);
  59. }
  60. //---------------------------------------------------------------------------
  61. // ctkVTKSliceView methods
  62. // --------------------------------------------------------------------------
  63. ctkVTKSliceView::ctkVTKSliceView(QWidget* _parent) : Superclass(_parent)
  64. , d_ptr(new ctkVTKSliceViewPrivate)
  65. {
  66. Q_D(ctkVTKSliceView);
  67. d->VTKWidget = new QVTKWidget(this);
  68. this->setLayout(new QVBoxLayout);
  69. this->layout()->setMargin(0);
  70. this->layout()->setSpacing(0);
  71. this->layout()->addWidget(d->VTKWidget);
  72. d->setupRendering();
  73. d->LightBoxRendererManager->SetRenderWindowLayout(1, 1);
  74. }
  75. // --------------------------------------------------------------------------
  76. ctkVTKSliceView::~ctkVTKSliceView()
  77. {
  78. }
  79. //----------------------------------------------------------------------------
  80. void ctkVTKSliceView::scheduleRender()
  81. {
  82. Q_D(ctkVTKSliceView);
  83. logger.trace("scheduleRender");
  84. if (!d->RenderEnabled)
  85. {
  86. return;
  87. }
  88. if (!d->RenderPending)
  89. {
  90. d->RenderPending = true;
  91. QTimer::singleShot(0, this, SLOT(forceRender()));
  92. }
  93. }
  94. //----------------------------------------------------------------------------
  95. void ctkVTKSliceView::forceRender()
  96. {
  97. Q_D(ctkVTKSliceView);
  98. if (!d->RenderEnabled || !this->isVisible())
  99. {
  100. return;
  101. }
  102. logger.trace("forceRender");
  103. d->RenderWindow->Render();
  104. d->RenderPending = false;
  105. }
  106. //----------------------------------------------------------------------------
  107. CTK_GET_CPP(ctkVTKSliceView, vtkRenderWindow*, renderWindow, RenderWindow);
  108. //----------------------------------------------------------------------------
  109. CTK_GET_CPP(ctkVTKSliceView, vtkLightBoxRendererManager*,
  110. lightBoxRendererManager, LightBoxRendererManager);
  111. //----------------------------------------------------------------------------
  112. CTK_SET_CPP(ctkVTKSliceView, bool, setRenderEnabled, RenderEnabled);
  113. CTK_GET_CPP(ctkVTKSliceView, bool, renderEnabled, RenderEnabled);
  114. //----------------------------------------------------------------------------
  115. vtkRenderWindowInteractor* ctkVTKSliceView::interactor() const
  116. {
  117. Q_D(const ctkVTKSliceView);
  118. return d->RenderWindow->GetInteractor();
  119. }
  120. //----------------------------------------------------------------------------
  121. void ctkVTKSliceView::setInteractor(vtkRenderWindowInteractor* newInteractor)
  122. {
  123. Q_D(const ctkVTKSliceView);
  124. d->RenderWindow->SetInteractor(newInteractor);
  125. }
  126. //----------------------------------------------------------------------------
  127. vtkInteractorObserver* ctkVTKSliceView::interactorStyle()const
  128. {
  129. Q_D(const ctkVTKSliceView);
  130. if (!d->RenderWindow->GetInteractor())
  131. {
  132. return 0;
  133. }
  134. return d->RenderWindow->GetInteractor()->GetInteractorStyle();
  135. }
  136. //----------------------------------------------------------------------------
  137. void ctkVTKSliceView::resetCamera()
  138. {
  139. Q_D(ctkVTKSliceView);
  140. d->LightBoxRendererManager->ResetCamera();
  141. }
  142. //----------------------------------------------------------------------------
  143. void ctkVTKSliceView::setImageData(vtkImageData* newImageData)
  144. {
  145. Q_D(ctkVTKSliceView);
  146. d->LightBoxRendererManager->SetImageData(newImageData);
  147. }
  148. //----------------------------------------------------------------------------
  149. QString ctkVTKSliceView::cornerAnnotationText()const
  150. {
  151. Q_D(const ctkVTKSliceView);
  152. return QString::fromStdString(d->LightBoxRendererManager->GetCornerAnnotationText());
  153. }
  154. //----------------------------------------------------------------------------
  155. void ctkVTKSliceView::setCornerAnnotationText(const QString& text)
  156. {
  157. Q_D(ctkVTKSliceView);
  158. d->LightBoxRendererManager->SetCornerAnnotationText(text.toStdString());
  159. }
  160. //----------------------------------------------------------------------------
  161. QColor ctkVTKSliceView::backgroundColor()const
  162. {
  163. Q_D(const ctkVTKSliceView);
  164. double* color = d->LightBoxRendererManager->GetBackgroundColor();
  165. QColor c;
  166. c.setRgbF(color[0], color[1], color[2]);
  167. return c;
  168. }
  169. //----------------------------------------------------------------------------
  170. void ctkVTKSliceView::setBackgroundColor(const QColor& newBackgroundColor)
  171. {
  172. Q_D(ctkVTKSliceView);
  173. double color[3];
  174. color[0] = newBackgroundColor.redF();
  175. color[1] = newBackgroundColor.greenF();
  176. color[2] = newBackgroundColor.blueF();
  177. d->LightBoxRendererManager->SetBackgroundColor(color);
  178. }
  179. //----------------------------------------------------------------------------
  180. QColor ctkVTKSliceView::highlightedBoxColor()const
  181. {
  182. Q_D(const ctkVTKSliceView);
  183. double* color = d->LightBoxRendererManager->GetHighlightedBoxColor();
  184. QColor c;
  185. c.setRgbF(color[0], color[1], color[2]);
  186. return c;
  187. }
  188. //----------------------------------------------------------------------------
  189. void ctkVTKSliceView::setHighlightedBoxColor(const QColor& newHighlightedBoxColor)
  190. {
  191. Q_D(ctkVTKSliceView);
  192. double color[3];
  193. color[0] = newHighlightedBoxColor.redF();
  194. color[1] = newHighlightedBoxColor.greenF();
  195. color[2] = newHighlightedBoxColor.blueF();
  196. d->LightBoxRendererManager->SetHighlightedBoxColor(color);
  197. }
  198. //----------------------------------------------------------------------------
  199. ctkVTKSliceView::RenderWindowLayoutType ctkVTKSliceView::renderWindowLayoutType()const
  200. {
  201. Q_D(const ctkVTKSliceView);
  202. return static_cast<ctkVTKSliceView::RenderWindowLayoutType>(
  203. d->LightBoxRendererManager->GetRenderWindowLayoutType());
  204. }
  205. //----------------------------------------------------------------------------
  206. void ctkVTKSliceView::setRenderWindowLayoutType(RenderWindowLayoutType layoutType)
  207. {
  208. Q_D(ctkVTKSliceView);
  209. d->LightBoxRendererManager->SetRenderWindowLayoutType(layoutType);
  210. }
  211. //----------------------------------------------------------------------------
  212. double ctkVTKSliceView::colorLevel()const
  213. {
  214. Q_D(const ctkVTKSliceView);
  215. return d->LightBoxRendererManager->GetColorLevel();
  216. }
  217. //----------------------------------------------------------------------------
  218. void ctkVTKSliceView::setColorLevel(double newColorLevel)
  219. {
  220. Q_D(ctkVTKSliceView);
  221. d->LightBoxRendererManager->SetColorLevel(newColorLevel);
  222. }
  223. //----------------------------------------------------------------------------
  224. double ctkVTKSliceView::colorWindow()const
  225. {
  226. Q_D(const ctkVTKSliceView);
  227. return d->LightBoxRendererManager->GetColorWindow();
  228. }
  229. //----------------------------------------------------------------------------
  230. void ctkVTKSliceView::setColorWindow(double newColorWindow)
  231. {
  232. Q_D(ctkVTKSliceView);
  233. d->LightBoxRendererManager->SetColorWindow(newColorWindow);
  234. }
  235. //----------------------------------------------------------------------------
  236. void ctkVTKSliceView::resizeEvent(QResizeEvent * event)
  237. {
  238. this->QWidget::resizeEvent(event);
  239. emit this->resized(event->size(), event->oldSize());
  240. }
  241. //----------------------------------------------------------------------------
  242. void ctkVTKSliceView::setLightBoxRendererManagerRowCount(int newRowCount)
  243. {
  244. Q_D(ctkVTKSliceView);
  245. d->LightBoxRendererManager->SetRenderWindowRowCount(newRowCount);
  246. }
  247. //----------------------------------------------------------------------------
  248. void ctkVTKSliceView::setLightBoxRendererManagerColumnCount(int newColumnCount)
  249. {
  250. Q_D(ctkVTKSliceView);
  251. d->LightBoxRendererManager->SetRenderWindowColumnCount(newColumnCount);
  252. }