ctkVTKSliceView.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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. // CTK includes
  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. // Convenient macro
  33. #define VTK_CREATE(type, name) \
  34. vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
  35. // --------------------------------------------------------------------------
  36. // RenderWindowItem methods
  37. //-----------------------------------------------------------------------------
  38. RenderWindowItem::RenderWindowItem(const QColor& rendererBackgroundColor,
  39. double colorWindow, double colorLevel)
  40. {
  41. // Instanciate a renderer
  42. this->Renderer = vtkSmartPointer<vtkRenderer>::New();
  43. this->Renderer->SetBackground(rendererBackgroundColor.redF(),
  44. rendererBackgroundColor.greenF(),
  45. rendererBackgroundColor.blueF());
  46. this->setupImageMapperActor(colorWindow, colorLevel);
  47. this->setupHighlightBoxActor();
  48. }
  49. //-----------------------------------------------------------------------------
  50. void RenderWindowItem::setViewport(double xMin, double yMin,
  51. double viewportWidth, double viewportHeight)
  52. {
  53. Q_ASSERT(this->Renderer);
  54. this->Renderer->SetViewport( xMin, yMin, (xMin + viewportWidth), (yMin + viewportHeight));
  55. }
  56. //---------------------------------------------------------------------------
  57. void RenderWindowItem::setupImageMapperActor(double colorWindow, double colorLevel)
  58. {
  59. Q_ASSERT(this->Renderer);
  60. Q_ASSERT(!this->ImageMapper);
  61. // Instanciate an image mapper
  62. this->ImageMapper = vtkSmartPointer<vtkImageMapper>::New();
  63. this->ImageMapper->SetColorWindow(colorWindow);
  64. this->ImageMapper->SetColorLevel(colorLevel);
  65. // .. and its corresponding 2D actor
  66. VTK_CREATE(vtkActor2D, actor2D);
  67. actor2D->SetMapper(this->ImageMapper);
  68. actor2D->GetProperty()->SetDisplayLocationToBackground();
  69. // .. and add it to the renderer
  70. this->Renderer->AddActor2D(actor2D);
  71. }
  72. //---------------------------------------------------------------------------
  73. void RenderWindowItem::setupHighlightBoxActor(bool visible)
  74. {
  75. Q_ASSERT(this->Renderer);
  76. Q_ASSERT(!this->HighlightBoxActor);
  77. // Create a highlight actor (2D box around viewport)
  78. VTK_CREATE(vtkPolyData, poly);
  79. VTK_CREATE(vtkPoints, points);
  80. double eps = 0.0;
  81. points->InsertNextPoint(eps, eps, 0);
  82. points->InsertNextPoint(1, eps, 0);
  83. points->InsertNextPoint(1, 1, 0);
  84. points->InsertNextPoint(eps, 1, 0);
  85. VTK_CREATE(vtkCellArray, cells);
  86. cells->InsertNextCell(5);
  87. cells->InsertCellPoint(0);
  88. cells->InsertCellPoint(1);
  89. cells->InsertCellPoint(2);
  90. cells->InsertCellPoint(3);
  91. cells->InsertCellPoint(0);
  92. poly->SetPoints(points);
  93. poly->SetLines(cells);
  94. VTK_CREATE(vtkCoordinate, coordinate);
  95. coordinate->SetCoordinateSystemToNormalizedViewport();
  96. coordinate->SetViewport(this->Renderer);
  97. VTK_CREATE(vtkPolyDataMapper2D, polyDataMapper);
  98. polyDataMapper->SetInput(poly);
  99. polyDataMapper->SetTransformCoordinate(coordinate);
  100. this->HighlightBoxActor = vtkSmartPointer<vtkActor2D>::New();
  101. this->HighlightBoxActor->SetMapper(polyDataMapper);
  102. this->HighlightBoxActor->GetProperty()->SetColor(1, 1, 0);
  103. this->HighlightBoxActor->GetProperty()->SetDisplayLocationToForeground();
  104. this->HighlightBoxActor->GetProperty()->SetLineWidth(3); // wide enough so not clipped
  105. this->HighlightBoxActor->SetVisibility(visible);
  106. this->Renderer->AddActor2D(this->HighlightBoxActor);
  107. }
  108. // --------------------------------------------------------------------------
  109. // ctkVTKSliceViewPrivate methods
  110. // --------------------------------------------------------------------------
  111. ctkVTKSliceViewPrivate::ctkVTKSliceViewPrivate()
  112. {
  113. this->RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  114. this->Axes = vtkSmartPointer<vtkAxesActor>::New();
  115. this->Orientation = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
  116. this->CornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
  117. this->RenderPending = false;
  118. this->RenderEnabled = false;
  119. this->RenderWindowRowCount = 0;
  120. this->RenderWindowColumnCount = 0;
  121. this->RenderWindowLayoutType = ctkVTKSliceView::LeftRightTopBottom;
  122. this->ColorWindow = 255;
  123. this->ColorLevel = 127.5;
  124. this->RendererBackgroundColor = QColor(Qt::black);
  125. }
  126. // --------------------------------------------------------------------------
  127. void ctkVTKSliceViewPrivate::setupCornerAnnotation()
  128. {
  129. foreach(const QSharedPointer<RenderWindowItem>& item, this->RenderWindowItemList)
  130. {
  131. if (!item->Renderer->HasViewProp(this->CornerAnnotation))
  132. {
  133. item->Renderer->AddViewProp(this->CornerAnnotation);
  134. this->CornerAnnotation->SetMaximumLineHeight(0.07);
  135. vtkTextProperty *tprop = this->CornerAnnotation->GetTextProperty();
  136. tprop->ShadowOn();
  137. }
  138. this->CornerAnnotation->ClearAllTexts();
  139. }
  140. }
  141. //---------------------------------------------------------------------------
  142. void ctkVTKSliceViewPrivate::setupRendering()
  143. {
  144. CTK_P(ctkVTKSliceView);
  145. Q_ASSERT(this->RenderWindow);
  146. this->RenderWindow->SetAlphaBitPlanes(1);
  147. this->RenderWindow->SetMultiSamples(0);
  148. this->RenderWindow->StereoCapableWindowOn();
  149. this->RenderWindow->GetRenderers()->RemoveAllItems();
  150. // Compute the width and height of each RenderWindowItem
  151. double viewportWidth = 1.0 / static_cast<double>(this->RenderWindowColumnCount);
  152. double viewportHeight = 1.0 / static_cast<double>(this->RenderWindowRowCount);
  153. // Postion of the Top-Left corner of the RenderWindowItem
  154. float xMin, yMin;
  155. // Loop through RenderWindowItem
  156. for ( int rowId = 0; rowId < this->RenderWindowRowCount; ++rowId )
  157. {
  158. yMin = (this->RenderWindowRowCount - 1 - rowId) * viewportHeight;
  159. xMin = 0.0;
  160. for ( int columnId = 0; columnId < this->RenderWindowColumnCount; ++columnId )
  161. {
  162. // Get reference to the renderWindowItem
  163. RenderWindowItem * item =
  164. this->RenderWindowItemList.at(p->renderWindowItemId(rowId, columnId)).data();
  165. // Set viewport
  166. item->setViewport(xMin, yMin, viewportWidth, viewportHeight);
  167. // Add to RenderWindow
  168. this->RenderWindow->AddRenderer(item->Renderer);
  169. xMin += viewportWidth;
  170. }
  171. }
  172. this->VTKWidget->SetRenderWindow(this->RenderWindow);
  173. }
  174. //---------------------------------------------------------------------------
  175. void ctkVTKSliceViewPrivate::setupDefaultInteractor()
  176. {
  177. CTK_P(ctkVTKSliceView);
  178. p->setInteractor(this->RenderWindow->GetInteractor());
  179. }
  180. // --------------------------------------------------------------------------
  181. void ctkVTKSliceViewPrivate::updateRenderWindowItemsZIndex(
  182. ctkVTKSliceView::RenderWindowLayoutType layoutType)
  183. {
  184. CTK_P(ctkVTKSliceView);
  185. for (int rowId = 0; rowId < this->RenderWindowRowCount; ++rowId)
  186. {
  187. for (int columnId = 0; columnId < this->RenderWindowColumnCount; ++columnId)
  188. {
  189. int itemId = p->renderWindowItemId(rowId, columnId);
  190. Q_ASSERT(itemId <= this->RenderWindowItemList.size());
  191. RenderWindowItem * item = this->RenderWindowItemList.at(itemId).data();
  192. Q_ASSERT(item->ImageMapper->GetInput());
  193. // Default to ctkVTKSliceView::LeftRightTopBottom
  194. int zSliceIndex = rowId * this->RenderWindowColumnCount + columnId;
  195. if (layoutType == ctkVTKSliceView::LeftRightBottomTop)
  196. {
  197. zSliceIndex = (this->RenderWindowRowCount - rowId - 1) *
  198. this->RenderWindowColumnCount + columnId;
  199. }
  200. item->ImageMapper->SetZSlice(zSliceIndex);
  201. }
  202. }
  203. }
  204. //---------------------------------------------------------------------------
  205. // ctkVTKSliceView methods
  206. // --------------------------------------------------------------------------
  207. ctkVTKSliceView::ctkVTKSliceView(QWidget* _parent) : Superclass(_parent)
  208. {
  209. CTK_INIT_PRIVATE(ctkVTKSliceView);
  210. CTK_D(ctkVTKSliceView);
  211. d->VTKWidget = new QVTKWidget(this);
  212. this->setLayout(new QVBoxLayout);
  213. this->layout()->setMargin(0);
  214. this->layout()->setSpacing(0);
  215. this->layout()->addWidget(d->VTKWidget);
  216. this->setRenderWindowLayout(1, 1);
  217. d->setupDefaultInteractor();
  218. }
  219. // --------------------------------------------------------------------------
  220. ctkVTKSliceView::~ctkVTKSliceView()
  221. {
  222. }
  223. //----------------------------------------------------------------------------
  224. void ctkVTKSliceView::scheduleRender()
  225. {
  226. CTK_D(ctkVTKSliceView);
  227. if (!d->RenderEnabled)
  228. {
  229. return;
  230. }
  231. if (!d->RenderPending)
  232. {
  233. d->RenderPending = true;
  234. QTimer::singleShot(0, this, SLOT(forceRender()));
  235. }
  236. }
  237. //----------------------------------------------------------------------------
  238. void ctkVTKSliceView::forceRender()
  239. {
  240. CTK_D(ctkVTKSliceView);
  241. if (!d->RenderEnabled)
  242. {
  243. return;
  244. }
  245. d->RenderWindow->Render();
  246. d->RenderPending = false;
  247. }
  248. //----------------------------------------------------------------------------
  249. CTK_GET_CXX(ctkVTKSliceView, vtkRenderWindow*, renderWindow, RenderWindow);
  250. //----------------------------------------------------------------------------
  251. CTK_GET_CXX(ctkVTKSliceView, vtkRenderWindowInteractor*, interactor, CurrentInteractor);
  252. //----------------------------------------------------------------------------
  253. void ctkVTKSliceView::setInteractor(vtkRenderWindowInteractor* newInteractor)
  254. {
  255. CTK_D(ctkVTKSliceView);
  256. d->RenderWindow->SetInteractor(newInteractor);
  257. //d->Orientation->SetOrientationMarker(d->Axes);
  258. //d->Orientation->SetInteractor(newInteractor);
  259. //d->Orientation->SetEnabled(1);
  260. //d->Orientation->InteractiveOff();
  261. d->CurrentInteractor = newInteractor;
  262. }
  263. //----------------------------------------------------------------------------
  264. vtkInteractorObserver* ctkVTKSliceView::interactorStyle()
  265. {
  266. CTK_D(ctkVTKSliceView);
  267. if (d->CurrentInteractor)
  268. {
  269. return d->CurrentInteractor->GetInteractorStyle();
  270. }
  271. else
  272. {
  273. return 0;
  274. }
  275. }
  276. //----------------------------------------------------------------------------
  277. void ctkVTKSliceView::setCornerAnnotationText(const QString& text)
  278. {
  279. CTK_D(ctkVTKSliceView);
  280. d->CornerAnnotation->ClearAllTexts();
  281. d->CornerAnnotation->SetText(2, text.toLatin1());
  282. }
  283. //----------------------------------------------------------------------------
  284. QString ctkVTKSliceView::cornerAnnotationText() const
  285. {
  286. CTK_D(const ctkVTKSliceView);
  287. return QLatin1String(d->CornerAnnotation->GetText(2));
  288. }
  289. // --------------------------------------------------------------------------
  290. void ctkVTKSliceView::setBackgroundColor(const QColor& newBackgroundColor)
  291. {
  292. CTK_D(ctkVTKSliceView);
  293. foreach(const QSharedPointer<RenderWindowItem>& item, d->RenderWindowItemList)
  294. {
  295. item->Renderer->SetBackground(newBackgroundColor.redF(),
  296. newBackgroundColor.greenF(),
  297. newBackgroundColor.blueF());
  298. }
  299. d->RendererBackgroundColor = newBackgroundColor;
  300. }
  301. //----------------------------------------------------------------------------
  302. CTK_GET_CXX(ctkVTKSliceView, QColor, backgroundColor, RendererBackgroundColor);
  303. //----------------------------------------------------------------------------
  304. vtkCamera* ctkVTKSliceView::activeCamera()
  305. {
  306. CTK_D(ctkVTKSliceView);
  307. if (d->RenderWindowItemList.size() == 0)
  308. {
  309. return 0;
  310. }
  311. // Obtain reference of the first renderer
  312. vtkRenderer * firstRenderer = d->RenderWindowItemList.at(0)->Renderer;
  313. if (firstRenderer->IsActiveCameraCreated())
  314. {
  315. return firstRenderer->GetActiveCamera();
  316. }
  317. else
  318. {
  319. return 0;
  320. }
  321. return 0;
  322. }
  323. //----------------------------------------------------------------------------
  324. void ctkVTKSliceView::setActiveCamera(vtkCamera* newActiveCamera)
  325. {
  326. CTK_D(ctkVTKSliceView);
  327. if (newActiveCamera == this->activeCamera())
  328. {
  329. return;
  330. }
  331. newActiveCamera->ParallelProjectionOn();
  332. foreach(const QSharedPointer<RenderWindowItem>& item, d->RenderWindowItemList)
  333. {
  334. item->Renderer->SetActiveCamera(newActiveCamera);
  335. }
  336. }
  337. //----------------------------------------------------------------------------
  338. void ctkVTKSliceView::resetCamera()
  339. {
  340. CTK_D(ctkVTKSliceView);
  341. foreach(const QSharedPointer<RenderWindowItem>& item, d->RenderWindowItemList)
  342. {
  343. item->Renderer->ResetCamera();
  344. }
  345. }
  346. //----------------------------------------------------------------------------
  347. int ctkVTKSliceView::rendererCount()
  348. {
  349. CTK_D(ctkVTKSliceView);
  350. return d->RenderWindowItemList.size();
  351. }
  352. //----------------------------------------------------------------------------
  353. vtkRenderer* ctkVTKSliceView::renderer(int id)
  354. {
  355. CTK_D(ctkVTKSliceView);
  356. if (id < 0 || id >= d->RenderWindowItemList.size())
  357. {
  358. return 0;
  359. }
  360. return d->RenderWindowItemList.at(id)->Renderer;
  361. }
  362. //----------------------------------------------------------------------------
  363. vtkRenderer* ctkVTKSliceView::renderer(int rowId, int columnId)
  364. {
  365. return this->renderer(this->renderWindowItemId(rowId, columnId));
  366. }
  367. //----------------------------------------------------------------------------
  368. CTK_SET_CXX(ctkVTKSliceView, bool, setRenderEnabled, RenderEnabled);
  369. CTK_GET_CXX(ctkVTKSliceView, bool, renderEnabled, RenderEnabled);
  370. //----------------------------------------------------------------------------
  371. int ctkVTKSliceView::renderWindowItemId(int rowId, int columnId)
  372. {
  373. CTK_D(ctkVTKSliceView);
  374. return d->RenderWindowColumnCount * rowId + columnId;
  375. }
  376. //----------------------------------------------------------------------------
  377. void ctkVTKSliceView::setRenderWindowLayoutType(ctkVTKSliceView::RenderWindowLayoutType layoutType)
  378. {
  379. CTK_D(ctkVTKSliceView);
  380. if (d->RenderWindowLayoutType == layoutType)
  381. {
  382. return;
  383. }
  384. if (d->ImageData)
  385. {
  386. d->updateRenderWindowItemsZIndex(layoutType);
  387. }
  388. d->RenderWindowLayoutType = layoutType;
  389. }
  390. //----------------------------------------------------------------------------
  391. CTK_GET_CXX(ctkVTKSliceView, ctkVTKSliceView::RenderWindowLayoutType,
  392. renderWindowLayoutType, RenderWindowLayoutType);
  393. //----------------------------------------------------------------------------
  394. void ctkVTKSliceView::setRenderWindowLayout(int rowCount, int columnCount)
  395. {
  396. CTK_D(ctkVTKSliceView);
  397. //qDebug() << "setRenderWindowLayout" << rowCount << columnCount;
  398. // Sanity checks
  399. Q_ASSERT(rowCount >= 0 && columnCount >= 0);
  400. if(!(rowCount >= 0 && columnCount >= 0))
  401. {
  402. return;
  403. }
  404. if (d->RenderWindowRowCount == rowCount && d->RenderWindowColumnCount == columnCount)
  405. {
  406. return;
  407. }
  408. int extraItem = (rowCount * columnCount) - d->RenderWindowItemList.size();
  409. if (extraItem > 0)
  410. {
  411. //logger.debug(QString("Creating %1 RenderWindowItem").arg(extraItem));
  412. // Create extra RenderWindowItem(s)
  413. while(extraItem > 0)
  414. {
  415. RenderWindowItem * item =
  416. new RenderWindowItem(d->RendererBackgroundColor, d->ColorWindow, d->ColorLevel);
  417. item->ImageMapper->SetInput(d->ImageData);
  418. d->RenderWindowItemList.append(QSharedPointer<RenderWindowItem>(item));
  419. --extraItem;
  420. }
  421. }
  422. else
  423. {
  424. //logger.debug(QString("Removing %1 RenderWindowItem").arg(extraItem));
  425. // Remove extra RenderWindowItem(s)
  426. extraItem = qAbs(extraItem);
  427. while(extraItem > 0)
  428. {
  429. d->RenderWindowItemList.removeLast();
  430. --extraItem;
  431. }
  432. }
  433. d->RenderWindowRowCount = rowCount;
  434. d->RenderWindowColumnCount = columnCount;
  435. d->setupRendering();
  436. if (d->ImageData)
  437. {
  438. d->updateRenderWindowItemsZIndex(d->RenderWindowLayoutType);
  439. }
  440. }
  441. //----------------------------------------------------------------------------
  442. void ctkVTKSliceView::setHighlightedById(int id, bool value)
  443. {
  444. CTK_D(ctkVTKSliceView);
  445. if (id < 0 || id >= d->RenderWindowItemList.size())
  446. {
  447. return;
  448. }
  449. d->RenderWindowItemList.at(id)->HighlightBoxActor->SetVisibility(value);
  450. }
  451. //----------------------------------------------------------------------------
  452. void ctkVTKSliceView::setHighlighted(int rowId, int columnId, bool value)
  453. {
  454. this->setHighlightedById(this->renderWindowItemId(rowId, columnId), value);
  455. }
  456. //----------------------------------------------------------------------------
  457. CTK_GET_CXX(ctkVTKSliceView, double, colorWindow, ColorWindow);
  458. CTK_GET_CXX(ctkVTKSliceView, double, colorLevel, ColorLevel);
  459. //----------------------------------------------------------------------------
  460. void ctkVTKSliceView::setImageData(vtkImageData* imageDataToSet)
  461. {
  462. CTK_D(ctkVTKSliceView);
  463. foreach(const QSharedPointer<RenderWindowItem>& item, d->RenderWindowItemList)
  464. {
  465. item->ImageMapper->SetInput(imageDataToSet);
  466. }
  467. if (imageDataToSet)
  468. {
  469. d->updateRenderWindowItemsZIndex(d->RenderWindowLayoutType);
  470. }
  471. d->ImageData = imageDataToSet;
  472. }