vtkLightBoxRendererManager.cpp 30 KB

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