vtkLightBoxRendererManager.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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. #include "vtkLightBoxRendererManager.h"
  15. // VTK includes
  16. #include <vtkCamera.h>
  17. #include <vtkCellArray.h>
  18. #include <vtkCornerAnnotation.h>
  19. #include <vtkImageData.h>
  20. #include <vtkImageMapper.h>
  21. #include <vtkNew.h>
  22. #include <vtkObjectFactory.h>
  23. #include <vtkPoints.h>
  24. #include <vtkPolyData.h>
  25. #include <vtkPolyDataMapper2D.h>
  26. #include <vtkProperty2D.h>
  27. #include <vtkRendererCollection.h>
  28. #include <vtkRenderWindow.h>
  29. #include <vtkRenderWindowInteractor.h>
  30. #include <vtkSmartPointer.h>
  31. #include <vtkTextProperty.h>
  32. #include <vtkWeakPointer.h>
  33. // STD includes
  34. #include <vector>
  35. #include <cassert>
  36. //----------------------------------------------------------------------------
  37. vtkCxxRevisionMacro(vtkLightBoxRendererManager, "$Revision:$");
  38. vtkStandardNewMacro(vtkLightBoxRendererManager);
  39. namespace
  40. {
  41. //-----------------------------------------------------------------------------
  42. // RenderWindowItem
  43. //-----------------------------------------------------------------------------
  44. /// A RenderWindow can be split in 1x1, 2x3, ... grid view, each element of that grid
  45. /// will be identified as RenderWindowItem
  46. class RenderWindowItem
  47. {
  48. public:
  49. RenderWindowItem(const double rendererBackgroundColor[3], const double highlightedBoxColor[3],
  50. double colorWindow, double colorLevel);
  51. ~RenderWindowItem();
  52. void SetViewport(double xMin, double yMin, double viewportWidth, double viewportHeight);
  53. /// Create the actor supporing the image mapper
  54. void SetupImageMapperActor(double colorWindow, double colorLevel);
  55. /// Create a box around the renderer.
  56. void SetupHighlightedBoxActor(const double highlightedBoxColor[3], bool visible = false);
  57. /// Set HighlightedBox color
  58. void SetHighlightedBoxColor(double* newHighlightedBoxColor);
  59. vtkSmartPointer<vtkRenderer> Renderer;
  60. vtkSmartPointer<vtkImageMapper> ImageMapper;
  61. vtkSmartPointer<vtkActor2D> HighlightedBoxActor;
  62. };
  63. }
  64. // --------------------------------------------------------------------------
  65. // RenderWindowItem methods
  66. //-----------------------------------------------------------------------------
  67. RenderWindowItem::RenderWindowItem(const double rendererBackgroundColor[3],
  68. const double highlightedBoxColor[3],
  69. double colorWindow, double colorLevel)
  70. {
  71. // Instantiate a renderer
  72. this->Renderer = vtkSmartPointer<vtkRenderer>::New();
  73. this->Renderer->SetBackground(rendererBackgroundColor[0],
  74. rendererBackgroundColor[1],
  75. rendererBackgroundColor[2]);
  76. this->SetupImageMapperActor(colorWindow, colorLevel);
  77. this->SetupHighlightedBoxActor(highlightedBoxColor);
  78. }
  79. //-----------------------------------------------------------------------------
  80. RenderWindowItem::~RenderWindowItem()
  81. {
  82. this->ImageMapper->SetInput(0);
  83. }
  84. //-----------------------------------------------------------------------------
  85. void RenderWindowItem::SetViewport(double xMin, double yMin,
  86. double viewportWidth, double viewportHeight)
  87. {
  88. assert(this->Renderer);
  89. this->Renderer->SetViewport( xMin, yMin, (xMin + viewportWidth), (yMin + viewportHeight));
  90. }
  91. //---------------------------------------------------------------------------
  92. void RenderWindowItem::SetupImageMapperActor(double colorWindow, double colorLevel)
  93. {
  94. assert(this->Renderer);
  95. assert(!this->ImageMapper);
  96. // Instantiate an image mapper
  97. this->ImageMapper = vtkSmartPointer<vtkImageMapper>::New();
  98. this->ImageMapper->SetColorWindow(colorWindow);
  99. this->ImageMapper->SetColorLevel(colorLevel);
  100. // .. and its corresponding 2D actor
  101. vtkNew<vtkActor2D> actor2D;
  102. actor2D->SetMapper(this->ImageMapper);
  103. actor2D->GetProperty()->SetDisplayLocationToBackground();
  104. // .. and add it to the renderer
  105. this->Renderer->AddActor2D(actor2D.GetPointer());
  106. }
  107. //---------------------------------------------------------------------------
  108. void RenderWindowItem::SetupHighlightedBoxActor(const double highlightedBoxColor[3], bool visible)
  109. {
  110. assert(this->Renderer);
  111. assert(!this->HighlightedBoxActor);
  112. // Create a highlight actor (2D box around viewport)
  113. vtkNew<vtkPolyData> poly;
  114. vtkNew<vtkPoints> points;
  115. // Normalized Viewport means :
  116. // 0. -> 0;
  117. // 1. -> width - 1 ;
  118. // For a line of a width of 1, from (0.f,0.f) to (10.f,0.f), the line is on
  119. // 2 pixels. What pixel to draw the line on ?
  120. //
  121. // | | | | | | |
  122. // 1 | | | | | | |
  123. // | | | | | | |
  124. // +-------+-------+-------+-------+-------+-------+
  125. // | | | | | | |
  126. // 0 | What pixel |================================
  127. // | line shall |
  128. // +--be drawn---(0,0)
  129. // | above or |
  130. // -1 | below? |================================
  131. // | | | | | | |
  132. // ^ +-------+-------+-------+-------+-------+-------+
  133. // | | | | | | |
  134. // 1px | | | | | | |
  135. // | | | | | | |
  136. // V +-------+-------+-------+-------+-------+-------+
  137. // < 1px > -1 0 1 2 3
  138. // It depends of the graphic card, this is why we need to add an offset.
  139. // 0.0002 seems to work for most of the window sizes.
  140. double shift = 0.0002;
  141. points->InsertNextPoint(0. + shift, 0. + shift, 0); // bottom-left
  142. points->InsertNextPoint(1. + shift, 0. + shift, 0); // bottom-right
  143. points->InsertNextPoint(1. + shift, 1. + shift + 0.1, 0); // top-right to fill the 1,1 pixel
  144. points->InsertNextPoint(1. + shift, 1. + shift, 0); // top-right
  145. points->InsertNextPoint(0. + shift, 1. + shift, 0); // top-left
  146. points->InsertNextPoint(0. + shift, 0. + shift - 0.1, 0); // bottom-left to fill the 0,0 pixel.
  147. vtkNew<vtkCellArray> cells;
  148. cells->InsertNextCell(6);
  149. cells->InsertCellPoint(0);
  150. cells->InsertCellPoint(1);
  151. cells->InsertCellPoint(2);
  152. cells->InsertCellPoint(3);
  153. cells->InsertCellPoint(4);
  154. cells->InsertCellPoint(5);
  155. poly->SetPoints(points.GetPointer());
  156. poly->SetLines(cells.GetPointer());
  157. vtkNew<vtkCoordinate> coordinate;
  158. coordinate->SetCoordinateSystemToNormalizedViewport();
  159. coordinate->SetViewport(this->Renderer);
  160. vtkNew<vtkPolyDataMapper2D> polyDataMapper;
  161. polyDataMapper->SetInput(poly.GetPointer());
  162. polyDataMapper->SetTransformCoordinate(coordinate.GetPointer());
  163. #if ! (VTK_MAJOR_VERSION == 5 && VTK_MINOR_VERSION == 8)
  164. polyDataMapper->SetTransformCoordinateUseDouble(true);
  165. #endif
  166. this->HighlightedBoxActor = vtkSmartPointer<vtkActor2D>::New();
  167. this->HighlightedBoxActor->SetMapper(polyDataMapper.GetPointer());
  168. this->HighlightedBoxActor->GetProperty()->SetColor(highlightedBoxColor[0],
  169. highlightedBoxColor[1],
  170. highlightedBoxColor[2]);
  171. this->HighlightedBoxActor->GetProperty()->SetDisplayLocationToForeground();
  172. this->HighlightedBoxActor->GetProperty()->SetLineWidth(1.0f);
  173. this->HighlightedBoxActor->SetVisibility(visible);
  174. this->Renderer->AddActor2D(this->HighlightedBoxActor);
  175. }
  176. //-----------------------------------------------------------------------------
  177. void RenderWindowItem::SetHighlightedBoxColor(double* newHighlightedBoxColor)
  178. {
  179. this->HighlightedBoxActor->GetProperty()->SetColor(newHighlightedBoxColor);
  180. }
  181. //-----------------------------------------------------------------------------
  182. // vtkInternal
  183. //-----------------------------------------------------------------------------
  184. class vtkLightBoxRendererManager::vtkInternal
  185. {
  186. public:
  187. vtkInternal(vtkLightBoxRendererManager* external);
  188. ~vtkInternal();
  189. /// Convenient setup methods
  190. void SetupCornerAnnotation();
  191. void setupRendering();
  192. /// Update render window ImageMapper Z slice according to \a layoutType
  193. void updateRenderWindowItemsZIndex(int layoutType);
  194. vtkSmartPointer<vtkRenderWindow> RenderWindow;
  195. int RenderWindowRowCount;
  196. int RenderWindowColumnCount;
  197. int RenderWindowLayoutType;
  198. double HighlightedBoxColor[3];
  199. int RendererLayer;
  200. vtkWeakPointer<vtkRenderWindowInteractor> CurrentInteractor;
  201. vtkSmartPointer<vtkCornerAnnotation> CornerAnnotation;
  202. std::string CornerAnnotationText;
  203. vtkWeakPointer<vtkImageData> ImageData;
  204. double ColorWindow;
  205. double ColorLevel;
  206. double RendererBackgroundColor[3];
  207. /// Collection of RenderWindowItem
  208. std::vector<RenderWindowItem* > RenderWindowItemList;
  209. /// .. and its associated convenient typedef
  210. typedef std::vector<RenderWindowItem*>::iterator RenderWindowItemListIt;
  211. /// Reference to the public interface
  212. vtkLightBoxRendererManager* External;
  213. };
  214. // --------------------------------------------------------------------------
  215. // vtkInternal methods
  216. // --------------------------------------------------------------------------
  217. vtkLightBoxRendererManager::vtkInternal::vtkInternal(vtkLightBoxRendererManager* external):
  218. External(external)
  219. {
  220. this->CornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
  221. this->RenderWindowRowCount = 0;
  222. this->RenderWindowColumnCount = 0;
  223. this->RenderWindowLayoutType = vtkLightBoxRendererManager::LeftRightTopBottom;
  224. this->ColorWindow = 255;
  225. this->ColorLevel = 127.5;
  226. this->RendererLayer = 0;
  227. // Default background color: black
  228. this->RendererBackgroundColor[0] = 0.0;
  229. this->RendererBackgroundColor[1] = 0.0;
  230. this->RendererBackgroundColor[2] = 0.0;
  231. // Default highlightedBox color: green
  232. this->HighlightedBoxColor[0] = 0.0;
  233. this->HighlightedBoxColor[1] = 1.0;
  234. this->HighlightedBoxColor[2] = 0.0;
  235. this->CornerAnnotation->SetMaximumLineHeight(0.07);
  236. vtkTextProperty *tprop = this->CornerAnnotation->GetTextProperty();
  237. tprop->ShadowOn();
  238. }
  239. // --------------------------------------------------------------------------
  240. vtkLightBoxRendererManager::vtkInternal::~vtkInternal()
  241. {
  242. for(RenderWindowItemListIt it = this->RenderWindowItemList.begin();
  243. it != this->RenderWindowItemList.end();
  244. ++it)
  245. {
  246. delete *it;
  247. }
  248. this->RenderWindowItemList.clear();
  249. }
  250. // --------------------------------------------------------------------------
  251. void vtkLightBoxRendererManager::vtkInternal::SetupCornerAnnotation()
  252. {
  253. for(RenderWindowItemListIt it = this->RenderWindowItemList.begin();
  254. it != this->RenderWindowItemList.end();
  255. ++it)
  256. {
  257. if (!(*it)->Renderer->HasViewProp(this->CornerAnnotation))
  258. {
  259. (*it)->Renderer->AddViewProp(this->CornerAnnotation);
  260. }
  261. }
  262. this->CornerAnnotation->ClearAllTexts();
  263. this->CornerAnnotation->SetText(2, this->CornerAnnotationText.c_str());
  264. }
  265. //---------------------------------------------------------------------------
  266. void vtkLightBoxRendererManager::vtkInternal::setupRendering()
  267. {
  268. assert(this->RenderWindow);
  269. // Remove only renderers managed by this light box
  270. for(RenderWindowItemListIt it = this->RenderWindowItemList.begin();
  271. it != this->RenderWindowItemList.end();
  272. ++it)
  273. {
  274. this->RenderWindow->GetRenderers()->RemoveItem((*it)->Renderer);
  275. }
  276. // Compute the width and height of each RenderWindowItem
  277. double viewportWidth = 1.0 / static_cast<double>(this->RenderWindowColumnCount);
  278. double viewportHeight = 1.0 / static_cast<double>(this->RenderWindowRowCount);
  279. // Postion of the Top-Left corner of the RenderWindowItem
  280. float xMin, yMin;
  281. // Loop through RenderWindowItem
  282. for ( int rowId = 0; rowId < this->RenderWindowRowCount; ++rowId )
  283. {
  284. yMin = (this->RenderWindowRowCount - 1 - rowId) * viewportHeight;
  285. xMin = 0.0;
  286. for ( int columnId = 0; columnId < this->RenderWindowColumnCount; ++columnId )
  287. {
  288. // Get reference to the renderWindowItem
  289. RenderWindowItem * item =
  290. this->RenderWindowItemList.at(
  291. this->External->ComputeRenderWindowItemId(rowId, columnId));
  292. // Set viewport
  293. item->SetViewport(xMin, yMin, viewportWidth, viewportHeight);
  294. // Add to RenderWindow
  295. this->RenderWindow->AddRenderer(item->Renderer);
  296. xMin += viewportWidth;
  297. }
  298. }
  299. }
  300. // --------------------------------------------------------------------------
  301. void vtkLightBoxRendererManager::vtkInternal::updateRenderWindowItemsZIndex(int layoutType)
  302. {
  303. for (int rowId = 0; rowId < this->RenderWindowRowCount; ++rowId)
  304. {
  305. for (int columnId = 0; columnId < this->RenderWindowColumnCount; ++columnId)
  306. {
  307. int itemId = this->External->ComputeRenderWindowItemId(rowId, columnId);
  308. assert(itemId <= static_cast<int>(this->RenderWindowItemList.size()));
  309. RenderWindowItem * item = this->RenderWindowItemList.at(itemId);
  310. assert(item->ImageMapper->GetInput());
  311. // Default to ctkVTKSliceView::LeftRightTopBottom
  312. int zSliceIndex = rowId * this->RenderWindowColumnCount + columnId;
  313. if (layoutType == vtkLightBoxRendererManager::LeftRightBottomTop)
  314. {
  315. zSliceIndex = (this->RenderWindowRowCount - rowId - 1) *
  316. this->RenderWindowColumnCount + columnId;
  317. }
  318. item->ImageMapper->SetZSlice(zSliceIndex);
  319. }
  320. }
  321. }
  322. //---------------------------------------------------------------------------
  323. // vtkLightBoxRendererManager methods
  324. // --------------------------------------------------------------------------
  325. vtkLightBoxRendererManager::vtkLightBoxRendererManager() : Superclass()
  326. {
  327. this->Internal = new vtkInternal(this);
  328. }
  329. // --------------------------------------------------------------------------
  330. vtkLightBoxRendererManager::~vtkLightBoxRendererManager()
  331. {
  332. delete this->Internal;
  333. }
  334. //----------------------------------------------------------------------------
  335. void vtkLightBoxRendererManager::PrintSelf(ostream& os, vtkIndent indent)
  336. {
  337. this->Superclass::PrintSelf(os, indent);
  338. }
  339. //----------------------------------------------------------------------------
  340. void vtkLightBoxRendererManager::SetRendererLayer(int newLayer)
  341. {
  342. if (this->IsInitialized())
  343. {
  344. vtkErrorMacro(<< "SetRendererLayer failed - vtkLightBoxRendererManager is initialized");
  345. return;
  346. }
  347. if (newLayer == this->Internal->RendererLayer)
  348. {
  349. return;
  350. }
  351. this->Internal->RendererLayer = newLayer;
  352. this->Modified();
  353. }
  354. //----------------------------------------------------------------------------
  355. vtkRenderWindow* vtkLightBoxRendererManager::GetRenderWindow()
  356. {
  357. return this->Internal->RenderWindow;
  358. }
  359. //----------------------------------------------------------------------------
  360. void vtkLightBoxRendererManager::Initialize(vtkRenderWindow* renderWindow)
  361. {
  362. if (this->Internal->RenderWindow)
  363. {
  364. vtkWarningMacro( << "vtkLightBoxRendererManager has already been initialized");
  365. return;
  366. }
  367. if (!renderWindow)
  368. {
  369. vtkErrorMacro("Failed to initialize vtkLightBoxRendererManager with a NULL renderWindow");
  370. return;
  371. }
  372. this->Internal->RenderWindow = renderWindow;
  373. // Set default Layout
  374. this->SetRenderWindowLayout(1, 1); // Modified() is invoked by SetRenderWindowLayout
  375. }
  376. //----------------------------------------------------------------------------
  377. bool vtkLightBoxRendererManager::IsInitialized()
  378. {
  379. return this->Internal->RenderWindow;
  380. }
  381. //----------------------------------------------------------------------------
  382. void vtkLightBoxRendererManager::SetImageData(vtkImageData* newImageData)
  383. {
  384. if (!this->IsInitialized())
  385. {
  386. vtkErrorMacro(<< "SetImageData failed - vtkLightBoxRendererManager is NOT initialized");
  387. return;
  388. }
  389. vtkInternal::RenderWindowItemListIt it;
  390. for(it = this->Internal->RenderWindowItemList.begin();
  391. it != this->Internal->RenderWindowItemList.end();
  392. ++it)
  393. {
  394. (*it)->ImageMapper->SetInput(newImageData);
  395. }
  396. if (newImageData)
  397. {
  398. this->Internal->updateRenderWindowItemsZIndex(this->Internal->RenderWindowLayoutType);
  399. }
  400. this->Internal->ImageData = newImageData;
  401. this->Modified();
  402. }
  403. //----------------------------------------------------------------------------
  404. vtkCamera* vtkLightBoxRendererManager::GetActiveCamera()
  405. {
  406. if (this->Internal->RenderWindowItemList.size() == 0)
  407. {
  408. return 0;
  409. }
  410. // Obtain reference of the first renderer
  411. vtkRenderer * firstRenderer = this->Internal->RenderWindowItemList.at(0)->Renderer;
  412. if (firstRenderer->IsActiveCameraCreated())
  413. {
  414. return firstRenderer->GetActiveCamera();
  415. }
  416. else
  417. {
  418. return 0;
  419. }
  420. return 0;
  421. }
  422. //----------------------------------------------------------------------------
  423. void vtkLightBoxRendererManager::SetActiveCamera(vtkCamera* newActiveCamera)
  424. {
  425. if (!this->IsInitialized())
  426. {
  427. vtkErrorMacro(<< "SetActiveCamera failed - vtkLightBoxRendererManager is NOT initialized");
  428. return;
  429. }
  430. if (newActiveCamera == this->GetActiveCamera())
  431. {
  432. return;
  433. }
  434. newActiveCamera->ParallelProjectionOn();
  435. vtkInternal::RenderWindowItemListIt it;
  436. for(it = this->Internal->RenderWindowItemList.begin();
  437. it != this->Internal->RenderWindowItemList.end();
  438. ++it)
  439. {
  440. (*it)->Renderer->SetActiveCamera(newActiveCamera);
  441. }
  442. this->Modified();
  443. }
  444. //----------------------------------------------------------------------------
  445. void vtkLightBoxRendererManager::ResetCamera()
  446. {
  447. if (!this->IsInitialized())
  448. {
  449. vtkErrorMacro(<< "ResetCamera failed - vtkLightBoxRendererManager is NOT initialized");
  450. return;
  451. }
  452. vtkInternal::RenderWindowItemListIt it;
  453. for(it = this->Internal->RenderWindowItemList.begin();
  454. it != this->Internal->RenderWindowItemList.end();
  455. ++it)
  456. {
  457. (*it)->Renderer->ResetCamera();
  458. }
  459. this->Modified();
  460. }
  461. //----------------------------------------------------------------------------
  462. int vtkLightBoxRendererManager::GetRenderWindowItemCount()
  463. {
  464. return static_cast<int>(this->Internal->RenderWindowItemList.size());
  465. }
  466. //----------------------------------------------------------------------------
  467. vtkRenderer* vtkLightBoxRendererManager::GetRenderer(int id)
  468. {
  469. if (id < 0 || id >= static_cast<int>(this->Internal->RenderWindowItemList.size()))
  470. {
  471. return 0;
  472. }
  473. return this->Internal->RenderWindowItemList.at(id)->Renderer;
  474. }
  475. //----------------------------------------------------------------------------
  476. vtkRenderer* vtkLightBoxRendererManager::GetRenderer(int rowId, int columnId)
  477. {
  478. return this->GetRenderer(this->ComputeRenderWindowItemId(rowId, columnId));
  479. }
  480. //----------------------------------------------------------------------------
  481. void vtkLightBoxRendererManager::SetRenderWindowLayoutType(int layoutType)
  482. {
  483. if (this->Internal->RenderWindowLayoutType == layoutType)
  484. {
  485. return;
  486. }
  487. if (this->Internal->ImageData)
  488. {
  489. this->Internal->updateRenderWindowItemsZIndex(layoutType);
  490. }
  491. this->Internal->RenderWindowLayoutType = layoutType;
  492. this->Modified();
  493. }
  494. //----------------------------------------------------------------------------
  495. int vtkLightBoxRendererManager::GetRenderWindowLayoutType() const
  496. {
  497. return this->Internal->RenderWindowLayoutType;
  498. }
  499. //----------------------------------------------------------------------------
  500. void vtkLightBoxRendererManager::SetRenderWindowLayout(int rowCount, int columnCount)
  501. {
  502. if (!this->IsInitialized())
  503. {
  504. vtkErrorMacro(<< "SetRenderWindowLayout failed - "
  505. "vtkLightBoxRendererManager is NOT initialized");
  506. return;
  507. }
  508. // Sanity checks
  509. assert(rowCount >= 0 && columnCount >= 0);
  510. if(!(rowCount >= 0 && columnCount >= 0))
  511. {
  512. return;
  513. }
  514. if (this->Internal->RenderWindowRowCount == rowCount &&
  515. this->Internal->RenderWindowColumnCount == columnCount)
  516. {
  517. return;
  518. }
  519. int extraItem = (rowCount * columnCount)
  520. - static_cast<int>(this->Internal->RenderWindowItemList.size());
  521. if (extraItem > 0)
  522. {
  523. // Create extra RenderWindowItem(s)
  524. while(extraItem > 0)
  525. {
  526. RenderWindowItem * item =
  527. new RenderWindowItem(this->Internal->RendererBackgroundColor,
  528. this->Internal->HighlightedBoxColor,
  529. this->Internal->ColorWindow, this->Internal->ColorLevel);
  530. item->Renderer->SetLayer(this->Internal->RendererLayer);
  531. item->ImageMapper->SetInput(this->Internal->ImageData);
  532. this->Internal->RenderWindowItemList.push_back(item);
  533. --extraItem;
  534. }
  535. }
  536. else
  537. {
  538. // Remove extra RenderWindowItem(s)
  539. extraItem = extraItem >= 0 ? extraItem : -extraItem; // Compute Abs
  540. while(extraItem > 0)
  541. {
  542. delete this->Internal->RenderWindowItemList.back();
  543. this->Internal->RenderWindowItemList.pop_back();
  544. --extraItem;
  545. }
  546. }
  547. this->Internal->RenderWindowRowCount = rowCount;
  548. this->Internal->RenderWindowColumnCount = columnCount;
  549. this->Internal->setupRendering();
  550. this->Internal->SetupCornerAnnotation();
  551. if (this->Internal->ImageData)
  552. {
  553. this->Internal->updateRenderWindowItemsZIndex(this->Internal->RenderWindowLayoutType);
  554. }
  555. this->Modified();
  556. }
  557. //----------------------------------------------------------------------------
  558. int vtkLightBoxRendererManager::GetRenderWindowRowCount()
  559. {
  560. return this->Internal->RenderWindowRowCount;
  561. }
  562. //----------------------------------------------------------------------------
  563. void vtkLightBoxRendererManager::SetRenderWindowRowCount(int newRowCount)
  564. {
  565. this->SetRenderWindowLayout(newRowCount, this->GetRenderWindowColumnCount());
  566. }
  567. //----------------------------------------------------------------------------
  568. int vtkLightBoxRendererManager::GetRenderWindowColumnCount()
  569. {
  570. return this->Internal->RenderWindowColumnCount;
  571. }
  572. //----------------------------------------------------------------------------
  573. void vtkLightBoxRendererManager::SetRenderWindowColumnCount(int newColumnCount)
  574. {
  575. this->SetRenderWindowLayout(this->GetRenderWindowRowCount(), newColumnCount);
  576. }
  577. //----------------------------------------------------------------------------
  578. bool vtkLightBoxRendererManager::GetHighlightedById(int id)
  579. {
  580. if (!this->IsInitialized())
  581. {
  582. vtkErrorMacro(<< "SetHighlightedById failed - vtkLightBoxRendererManager is NOT initialized");
  583. return false;
  584. }
  585. if (id < 0 || id >= static_cast<int>(this->Internal->RenderWindowItemList.size()))
  586. {
  587. return false;
  588. }
  589. return this->Internal->RenderWindowItemList.at(id)->HighlightedBoxActor->GetVisibility();
  590. }
  591. //----------------------------------------------------------------------------
  592. bool vtkLightBoxRendererManager::GetHighlighted(int rowId, int columnId)
  593. {
  594. return this->GetHighlightedById(this->ComputeRenderWindowItemId(rowId, columnId));
  595. }
  596. //----------------------------------------------------------------------------
  597. void vtkLightBoxRendererManager::SetHighlightedById(int id, bool highlighted)
  598. {
  599. if (!this->IsInitialized())
  600. {
  601. vtkErrorMacro(<< "SetHighlightedById failed - vtkLightBoxRendererManager is NOT initialized");
  602. return;
  603. }
  604. if (id < 0 || id >= static_cast<int>(this->Internal->RenderWindowItemList.size()))
  605. {
  606. return;
  607. }
  608. this->Internal->RenderWindowItemList.at(id)->HighlightedBoxActor->SetVisibility(highlighted);
  609. this->Modified();
  610. }
  611. //----------------------------------------------------------------------------
  612. void vtkLightBoxRendererManager::SetHighlighted(int rowId, int columnId, bool highlighted)
  613. {
  614. this->SetHighlightedById(this->ComputeRenderWindowItemId(rowId, columnId), highlighted);
  615. }
  616. //----------------------------------------------------------------------------
  617. void vtkLightBoxRendererManager::SetHighlightedBoxColor(double newHighlightedBoxColor[3])
  618. {
  619. if (!this->IsInitialized())
  620. {
  621. vtkErrorMacro(<< "SetHighlightedById failed - vtkLightBoxRendererManager is NOT initialized");
  622. return;
  623. }
  624. if (this->Internal->HighlightedBoxColor[0] == newHighlightedBoxColor[0] &&
  625. this->Internal->HighlightedBoxColor[1] == newHighlightedBoxColor[1] &&
  626. this->Internal->HighlightedBoxColor[2] == newHighlightedBoxColor[2])
  627. {
  628. return;
  629. }
  630. this->Internal->HighlightedBoxColor[0] = newHighlightedBoxColor[0];
  631. this->Internal->HighlightedBoxColor[1] = newHighlightedBoxColor[1];
  632. this->Internal->HighlightedBoxColor[2] = newHighlightedBoxColor[2];
  633. vtkInternal::RenderWindowItemListIt it;
  634. for(it = this->Internal->RenderWindowItemList.begin();
  635. it != this->Internal->RenderWindowItemList.end();
  636. ++it)
  637. {
  638. (*it)->SetHighlightedBoxColor(newHighlightedBoxColor);
  639. }
  640. this->Modified();
  641. }
  642. //----------------------------------------------------------------------------
  643. double* vtkLightBoxRendererManager::GetHighlightedBoxColor() const
  644. {
  645. return this->Internal->HighlightedBoxColor;
  646. }
  647. //----------------------------------------------------------------------------
  648. int vtkLightBoxRendererManager::ComputeRenderWindowItemId(int rowId, int columnId)
  649. {
  650. return this->Internal->RenderWindowColumnCount * rowId + columnId;
  651. }
  652. //----------------------------------------------------------------------------
  653. void vtkLightBoxRendererManager::SetCornerAnnotationText(const std::string& text)
  654. {
  655. if (!this->IsInitialized())
  656. {
  657. vtkErrorMacro(<< "SetCornerAnnotationText failed - "
  658. "vtkLightBoxRendererManager is NOT initialized");
  659. return;
  660. }
  661. if (text.compare(this->Internal->CornerAnnotationText) == 0)
  662. {
  663. return;
  664. }
  665. this->Internal->CornerAnnotation->ClearAllTexts();
  666. this->Internal->CornerAnnotation->SetText(2, text.c_str());
  667. this->Internal->CornerAnnotationText = text;
  668. this->Modified();
  669. }
  670. //----------------------------------------------------------------------------
  671. const std::string vtkLightBoxRendererManager::GetCornerAnnotationText() const
  672. {
  673. const char * text = this->Internal->CornerAnnotation->GetText(2);
  674. return text ? text : "";
  675. }
  676. // --------------------------------------------------------------------------
  677. vtkCornerAnnotation * vtkLightBoxRendererManager::GetCornerAnnotation() const
  678. {
  679. return this->Internal->CornerAnnotation;
  680. }
  681. // --------------------------------------------------------------------------
  682. void vtkLightBoxRendererManager::SetCornerAnnotation(vtkCornerAnnotation* annotation)
  683. {
  684. // Remove current corner annotation
  685. vtkInternal::RenderWindowItemListIt it;
  686. for(it = this->Internal->RenderWindowItemList.begin();
  687. it != this->Internal->RenderWindowItemList.end();
  688. ++it)
  689. {
  690. if (!(*it)->Renderer->HasViewProp(this->Internal->CornerAnnotation))
  691. {
  692. (*it)->Renderer->RemoveViewProp(this->Internal->CornerAnnotation);
  693. }
  694. }
  695. this->Internal->CornerAnnotation = annotation;
  696. }
  697. // --------------------------------------------------------------------------
  698. void vtkLightBoxRendererManager::SetBackgroundColor(const double newBackgroundColor[3])
  699. {
  700. if (!this->IsInitialized())
  701. {
  702. vtkErrorMacro(<< "SetBackgroundColor failed - vtkLightBoxRendererManager is NOT initialized");
  703. return;
  704. }
  705. vtkInternal::RenderWindowItemListIt it;
  706. for(it = this->Internal->RenderWindowItemList.begin();
  707. it != this->Internal->RenderWindowItemList.end();
  708. ++it)
  709. {
  710. (*it)->Renderer->SetBackground(newBackgroundColor[0],
  711. newBackgroundColor[1],
  712. newBackgroundColor[2]);
  713. }
  714. this->Internal->RendererBackgroundColor[0] = newBackgroundColor[0];
  715. this->Internal->RendererBackgroundColor[1] = newBackgroundColor[1];
  716. this->Internal->RendererBackgroundColor[2] = newBackgroundColor[2];
  717. this->Modified();
  718. }
  719. //----------------------------------------------------------------------------
  720. double* vtkLightBoxRendererManager::GetBackgroundColor()const
  721. {
  722. return this->Internal->RendererBackgroundColor;
  723. }
  724. //----------------------------------------------------------------------------
  725. void vtkLightBoxRendererManager::SetColorLevel(double colorLevel)
  726. {
  727. this->SetColorWindowAndLevel(this->Internal->ColorWindow, colorLevel);
  728. }
  729. //----------------------------------------------------------------------------
  730. double vtkLightBoxRendererManager::GetColorLevel()const
  731. {
  732. return this->Internal->ColorLevel;
  733. }
  734. //----------------------------------------------------------------------------
  735. void vtkLightBoxRendererManager::SetColorWindow(double colorWindow)
  736. {
  737. this->SetColorWindowAndLevel(colorWindow, this->Internal->ColorLevel);
  738. }
  739. //----------------------------------------------------------------------------
  740. double vtkLightBoxRendererManager::GetColorWindow()const
  741. {
  742. return this->Internal->ColorWindow;
  743. }
  744. //----------------------------------------------------------------------------
  745. void vtkLightBoxRendererManager::SetColorWindowAndLevel(double colorWindow, double colorLevel)
  746. {
  747. if (this->Internal->ColorWindow == colorWindow &&
  748. this->Internal->ColorLevel == colorLevel)
  749. {
  750. return;
  751. }
  752. vtkInternal::RenderWindowItemListIt it;
  753. for(it = this->Internal->RenderWindowItemList.begin();
  754. it != this->Internal->RenderWindowItemList.end();
  755. ++it)
  756. {
  757. (*it)->ImageMapper->SetColorWindow(colorWindow);
  758. (*it)->ImageMapper->SetColorLevel(colorLevel);
  759. }
  760. this->Internal->ColorWindow = colorWindow;
  761. this->Internal->ColorLevel = colorLevel;
  762. this->Modified();
  763. }