vtkLightBoxRendererManager.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. polyDataMapper->SetTransformCoordinateUseDouble(true);
  164. this->HighlightedBoxActor = vtkSmartPointer<vtkActor2D>::New();
  165. this->HighlightedBoxActor->SetMapper(polyDataMapper.GetPointer());
  166. this->HighlightedBoxActor->GetProperty()->SetColor(highlightedBoxColor[0],
  167. highlightedBoxColor[1],
  168. highlightedBoxColor[2]);
  169. this->HighlightedBoxActor->GetProperty()->SetDisplayLocationToForeground();
  170. this->HighlightedBoxActor->GetProperty()->SetLineWidth(1.0f);
  171. this->HighlightedBoxActor->SetVisibility(visible);
  172. this->Renderer->AddActor2D(this->HighlightedBoxActor);
  173. }
  174. //-----------------------------------------------------------------------------
  175. void RenderWindowItem::SetHighlightedBoxColor(double* newHighlightedBoxColor)
  176. {
  177. this->HighlightedBoxActor->GetProperty()->SetColor(newHighlightedBoxColor);
  178. }
  179. //-----------------------------------------------------------------------------
  180. // vtkInternal
  181. //-----------------------------------------------------------------------------
  182. class vtkLightBoxRendererManager::vtkInternal
  183. {
  184. public:
  185. vtkInternal(vtkLightBoxRendererManager* external);
  186. ~vtkInternal();
  187. /// Convenient setup methods
  188. void SetupCornerAnnotation();
  189. void setupRendering();
  190. /// Update render window ImageMapper Z slice according to \a layoutType
  191. void updateRenderWindowItemsZIndex(int layoutType);
  192. vtkSmartPointer<vtkRenderWindow> RenderWindow;
  193. int RenderWindowRowCount;
  194. int RenderWindowColumnCount;
  195. int RenderWindowLayoutType;
  196. double HighlightedBoxColor[3];
  197. int RendererLayer;
  198. vtkWeakPointer<vtkRenderWindowInteractor> CurrentInteractor;
  199. vtkSmartPointer<vtkCornerAnnotation> CornerAnnotation;
  200. std::string CornerAnnotationText;
  201. vtkWeakPointer<vtkImageData> ImageData;
  202. double ColorWindow;
  203. double ColorLevel;
  204. double RendererBackgroundColor[3];
  205. /// Collection of RenderWindowItem
  206. std::vector<RenderWindowItem* > RenderWindowItemList;
  207. /// .. and its associated convenient typedef
  208. typedef std::vector<RenderWindowItem*>::iterator RenderWindowItemListIt;
  209. /// Reference to the public interface
  210. vtkLightBoxRendererManager* External;
  211. };
  212. // --------------------------------------------------------------------------
  213. // vtkInternal methods
  214. // --------------------------------------------------------------------------
  215. vtkLightBoxRendererManager::vtkInternal::vtkInternal(vtkLightBoxRendererManager* external):
  216. External(external)
  217. {
  218. this->CornerAnnotation = vtkSmartPointer<vtkCornerAnnotation>::New();
  219. this->RenderWindowRowCount = 0;
  220. this->RenderWindowColumnCount = 0;
  221. this->RenderWindowLayoutType = vtkLightBoxRendererManager::LeftRightTopBottom;
  222. this->ColorWindow = 255;
  223. this->ColorLevel = 127.5;
  224. this->RendererLayer = 0;
  225. // Default background color: black
  226. this->RendererBackgroundColor[0] = 0.0;
  227. this->RendererBackgroundColor[1] = 0.0;
  228. this->RendererBackgroundColor[2] = 0.0;
  229. // Default highlightedBox color: green
  230. this->HighlightedBoxColor[0] = 0.0;
  231. this->HighlightedBoxColor[1] = 1.0;
  232. this->HighlightedBoxColor[2] = 0.0;
  233. this->CornerAnnotation->SetMaximumLineHeight(0.07);
  234. vtkTextProperty *tprop = this->CornerAnnotation->GetTextProperty();
  235. tprop->ShadowOn();
  236. }
  237. // --------------------------------------------------------------------------
  238. vtkLightBoxRendererManager::vtkInternal::~vtkInternal()
  239. {
  240. for(RenderWindowItemListIt it = this->RenderWindowItemList.begin();
  241. it != this->RenderWindowItemList.end();
  242. ++it)
  243. {
  244. delete *it;
  245. }
  246. this->RenderWindowItemList.clear();
  247. }
  248. // --------------------------------------------------------------------------
  249. void vtkLightBoxRendererManager::vtkInternal::SetupCornerAnnotation()
  250. {
  251. for(RenderWindowItemListIt it = this->RenderWindowItemList.begin();
  252. it != this->RenderWindowItemList.end();
  253. ++it)
  254. {
  255. if (!(*it)->Renderer->HasViewProp(this->CornerAnnotation))
  256. {
  257. (*it)->Renderer->AddViewProp(this->CornerAnnotation);
  258. }
  259. }
  260. this->CornerAnnotation->ClearAllTexts();
  261. this->CornerAnnotation->SetText(2, this->CornerAnnotationText.c_str());
  262. }
  263. //---------------------------------------------------------------------------
  264. void vtkLightBoxRendererManager::vtkInternal::setupRendering()
  265. {
  266. assert(this->RenderWindow);
  267. // Remove only renderers managed by this light box
  268. for(RenderWindowItemListIt it = this->RenderWindowItemList.begin();
  269. it != this->RenderWindowItemList.end();
  270. ++it)
  271. {
  272. this->RenderWindow->GetRenderers()->RemoveItem((*it)->Renderer);
  273. }
  274. // Compute the width and height of each RenderWindowItem
  275. double viewportWidth = 1.0 / static_cast<double>(this->RenderWindowColumnCount);
  276. double viewportHeight = 1.0 / static_cast<double>(this->RenderWindowRowCount);
  277. // Postion of the Top-Left corner of the RenderWindowItem
  278. float xMin, yMin;
  279. // Loop through RenderWindowItem
  280. for ( int rowId = 0; rowId < this->RenderWindowRowCount; ++rowId )
  281. {
  282. yMin = (this->RenderWindowRowCount - 1 - rowId) * viewportHeight;
  283. xMin = 0.0;
  284. for ( int columnId = 0; columnId < this->RenderWindowColumnCount; ++columnId )
  285. {
  286. // Get reference to the renderWindowItem
  287. RenderWindowItem * item =
  288. this->RenderWindowItemList.at(
  289. this->External->ComputeRenderWindowItemId(rowId, columnId));
  290. // Set viewport
  291. item->SetViewport(xMin, yMin, viewportWidth, viewportHeight);
  292. // Add to RenderWindow
  293. this->RenderWindow->AddRenderer(item->Renderer);
  294. xMin += viewportWidth;
  295. }
  296. }
  297. }
  298. // --------------------------------------------------------------------------
  299. void vtkLightBoxRendererManager::vtkInternal::updateRenderWindowItemsZIndex(int layoutType)
  300. {
  301. for (int rowId = 0; rowId < this->RenderWindowRowCount; ++rowId)
  302. {
  303. for (int columnId = 0; columnId < this->RenderWindowColumnCount; ++columnId)
  304. {
  305. int itemId = this->External->ComputeRenderWindowItemId(rowId, columnId);
  306. assert(itemId <= static_cast<int>(this->RenderWindowItemList.size()));
  307. RenderWindowItem * item = this->RenderWindowItemList.at(itemId);
  308. assert(item->ImageMapper->GetInput());
  309. // Default to ctkVTKSliceView::LeftRightTopBottom
  310. int zSliceIndex = rowId * this->RenderWindowColumnCount + columnId;
  311. if (layoutType == vtkLightBoxRendererManager::LeftRightBottomTop)
  312. {
  313. zSliceIndex = (this->RenderWindowRowCount - rowId - 1) *
  314. this->RenderWindowColumnCount + columnId;
  315. }
  316. item->ImageMapper->SetZSlice(zSliceIndex);
  317. }
  318. }
  319. }
  320. //---------------------------------------------------------------------------
  321. // vtkLightBoxRendererManager methods
  322. // --------------------------------------------------------------------------
  323. vtkLightBoxRendererManager::vtkLightBoxRendererManager() : Superclass()
  324. {
  325. this->Internal = new vtkInternal(this);
  326. }
  327. // --------------------------------------------------------------------------
  328. vtkLightBoxRendererManager::~vtkLightBoxRendererManager()
  329. {
  330. delete this->Internal;
  331. }
  332. //----------------------------------------------------------------------------
  333. void vtkLightBoxRendererManager::PrintSelf(ostream& os, vtkIndent indent)
  334. {
  335. this->Superclass::PrintSelf(os, indent);
  336. }
  337. //----------------------------------------------------------------------------
  338. void vtkLightBoxRendererManager::SetRendererLayer(int newLayer)
  339. {
  340. if (this->IsInitialized())
  341. {
  342. vtkErrorMacro(<< "SetRendererLayer failed - vtkLightBoxRendererManager is initialized");
  343. return;
  344. }
  345. if (newLayer == this->Internal->RendererLayer)
  346. {
  347. return;
  348. }
  349. this->Internal->RendererLayer = newLayer;
  350. this->Modified();
  351. }
  352. //----------------------------------------------------------------------------
  353. vtkRenderWindow* vtkLightBoxRendererManager::GetRenderWindow()
  354. {
  355. return this->Internal->RenderWindow;
  356. }
  357. //----------------------------------------------------------------------------
  358. void vtkLightBoxRendererManager::Initialize(vtkRenderWindow* renderWindow)
  359. {
  360. if (this->Internal->RenderWindow)
  361. {
  362. vtkWarningMacro( << "vtkLightBoxRendererManager has already been initialized");
  363. return;
  364. }
  365. if (!renderWindow)
  366. {
  367. vtkErrorMacro("Failed to initialize vtkLightBoxRendererManager with a NULL renderWindow");
  368. return;
  369. }
  370. this->Internal->RenderWindow = renderWindow;
  371. // Set default Layout
  372. this->SetRenderWindowLayout(1, 1); // Modified() is invoked by SetRenderWindowLayout
  373. }
  374. //----------------------------------------------------------------------------
  375. bool vtkLightBoxRendererManager::IsInitialized()
  376. {
  377. return this->Internal->RenderWindow;
  378. }
  379. //----------------------------------------------------------------------------
  380. void vtkLightBoxRendererManager::SetImageData(vtkImageData* newImageData)
  381. {
  382. if (!this->IsInitialized())
  383. {
  384. vtkErrorMacro(<< "SetImageData failed - vtkLightBoxRendererManager is NOT initialized");
  385. return;
  386. }
  387. vtkInternal::RenderWindowItemListIt it;
  388. for(it = this->Internal->RenderWindowItemList.begin();
  389. it != this->Internal->RenderWindowItemList.end();
  390. ++it)
  391. {
  392. (*it)->ImageMapper->SetInput(newImageData);
  393. }
  394. if (newImageData)
  395. {
  396. this->Internal->updateRenderWindowItemsZIndex(this->Internal->RenderWindowLayoutType);
  397. }
  398. this->Internal->ImageData = newImageData;
  399. this->Modified();
  400. }
  401. //----------------------------------------------------------------------------
  402. vtkCamera* vtkLightBoxRendererManager::GetActiveCamera()
  403. {
  404. if (this->Internal->RenderWindowItemList.size() == 0)
  405. {
  406. return 0;
  407. }
  408. // Obtain reference of the first renderer
  409. vtkRenderer * firstRenderer = this->Internal->RenderWindowItemList.at(0)->Renderer;
  410. if (firstRenderer->IsActiveCameraCreated())
  411. {
  412. return firstRenderer->GetActiveCamera();
  413. }
  414. else
  415. {
  416. return 0;
  417. }
  418. return 0;
  419. }
  420. //----------------------------------------------------------------------------
  421. void vtkLightBoxRendererManager::SetActiveCamera(vtkCamera* newActiveCamera)
  422. {
  423. if (!this->IsInitialized())
  424. {
  425. vtkErrorMacro(<< "SetActiveCamera failed - vtkLightBoxRendererManager is NOT initialized");
  426. return;
  427. }
  428. if (newActiveCamera == this->GetActiveCamera())
  429. {
  430. return;
  431. }
  432. newActiveCamera->ParallelProjectionOn();
  433. vtkInternal::RenderWindowItemListIt it;
  434. for(it = this->Internal->RenderWindowItemList.begin();
  435. it != this->Internal->RenderWindowItemList.end();
  436. ++it)
  437. {
  438. (*it)->Renderer->SetActiveCamera(newActiveCamera);
  439. }
  440. this->Modified();
  441. }
  442. //----------------------------------------------------------------------------
  443. void vtkLightBoxRendererManager::ResetCamera()
  444. {
  445. if (!this->IsInitialized())
  446. {
  447. vtkErrorMacro(<< "ResetCamera failed - vtkLightBoxRendererManager is NOT initialized");
  448. return;
  449. }
  450. vtkInternal::RenderWindowItemListIt it;
  451. for(it = this->Internal->RenderWindowItemList.begin();
  452. it != this->Internal->RenderWindowItemList.end();
  453. ++it)
  454. {
  455. (*it)->Renderer->ResetCamera();
  456. }
  457. this->Modified();
  458. }
  459. //----------------------------------------------------------------------------
  460. int vtkLightBoxRendererManager::GetRenderWindowItemCount()
  461. {
  462. return static_cast<int>(this->Internal->RenderWindowItemList.size());
  463. }
  464. //----------------------------------------------------------------------------
  465. vtkRenderer* vtkLightBoxRendererManager::GetRenderer(int id)
  466. {
  467. if (id < 0 || id >= static_cast<int>(this->Internal->RenderWindowItemList.size()))
  468. {
  469. return 0;
  470. }
  471. return this->Internal->RenderWindowItemList.at(id)->Renderer;
  472. }
  473. //----------------------------------------------------------------------------
  474. vtkRenderer* vtkLightBoxRendererManager::GetRenderer(int rowId, int columnId)
  475. {
  476. return this->GetRenderer(this->ComputeRenderWindowItemId(rowId, columnId));
  477. }
  478. //----------------------------------------------------------------------------
  479. void vtkLightBoxRendererManager::SetRenderWindowLayoutType(int layoutType)
  480. {
  481. if (this->Internal->RenderWindowLayoutType == layoutType)
  482. {
  483. return;
  484. }
  485. if (this->Internal->ImageData)
  486. {
  487. this->Internal->updateRenderWindowItemsZIndex(layoutType);
  488. }
  489. this->Internal->RenderWindowLayoutType = layoutType;
  490. this->Modified();
  491. }
  492. //----------------------------------------------------------------------------
  493. int vtkLightBoxRendererManager::GetRenderWindowLayoutType() const
  494. {
  495. return this->Internal->RenderWindowLayoutType;
  496. }
  497. //----------------------------------------------------------------------------
  498. void vtkLightBoxRendererManager::SetRenderWindowLayout(int rowCount, int columnCount)
  499. {
  500. if (!this->IsInitialized())
  501. {
  502. vtkErrorMacro(<< "SetRenderWindowLayout failed - "
  503. "vtkLightBoxRendererManager is NOT initialized");
  504. return;
  505. }
  506. // Sanity checks
  507. assert(rowCount >= 0 && columnCount >= 0);
  508. if(!(rowCount >= 0 && columnCount >= 0))
  509. {
  510. return;
  511. }
  512. if (this->Internal->RenderWindowRowCount == rowCount &&
  513. this->Internal->RenderWindowColumnCount == columnCount)
  514. {
  515. return;
  516. }
  517. int extraItem = (rowCount * columnCount)
  518. - static_cast<int>(this->Internal->RenderWindowItemList.size());
  519. if (extraItem > 0)
  520. {
  521. // Create extra RenderWindowItem(s)
  522. while(extraItem > 0)
  523. {
  524. RenderWindowItem * item =
  525. new RenderWindowItem(this->Internal->RendererBackgroundColor,
  526. this->Internal->HighlightedBoxColor,
  527. this->Internal->ColorWindow, this->Internal->ColorLevel);
  528. item->Renderer->SetLayer(this->Internal->RendererLayer);
  529. item->ImageMapper->SetInput(this->Internal->ImageData);
  530. this->Internal->RenderWindowItemList.push_back(item);
  531. --extraItem;
  532. }
  533. }
  534. else
  535. {
  536. // Remove extra RenderWindowItem(s)
  537. extraItem = extraItem >= 0 ? extraItem : -extraItem; // Compute Abs
  538. while(extraItem > 0)
  539. {
  540. delete this->Internal->RenderWindowItemList.back();
  541. this->Internal->RenderWindowItemList.pop_back();
  542. --extraItem;
  543. }
  544. }
  545. this->Internal->RenderWindowRowCount = rowCount;
  546. this->Internal->RenderWindowColumnCount = columnCount;
  547. this->Internal->setupRendering();
  548. this->Internal->SetupCornerAnnotation();
  549. if (this->Internal->ImageData)
  550. {
  551. this->Internal->updateRenderWindowItemsZIndex(this->Internal->RenderWindowLayoutType);
  552. }
  553. this->Modified();
  554. }
  555. //----------------------------------------------------------------------------
  556. int vtkLightBoxRendererManager::GetRenderWindowRowCount()
  557. {
  558. return this->Internal->RenderWindowRowCount;
  559. }
  560. //----------------------------------------------------------------------------
  561. void vtkLightBoxRendererManager::SetRenderWindowRowCount(int newRowCount)
  562. {
  563. this->SetRenderWindowLayout(newRowCount, this->GetRenderWindowColumnCount());
  564. }
  565. //----------------------------------------------------------------------------
  566. int vtkLightBoxRendererManager::GetRenderWindowColumnCount()
  567. {
  568. return this->Internal->RenderWindowColumnCount;
  569. }
  570. //----------------------------------------------------------------------------
  571. void vtkLightBoxRendererManager::SetRenderWindowColumnCount(int newColumnCount)
  572. {
  573. this->SetRenderWindowLayout(this->GetRenderWindowRowCount(), newColumnCount);
  574. }
  575. //----------------------------------------------------------------------------
  576. bool vtkLightBoxRendererManager::GetHighlightedById(int id)
  577. {
  578. if (!this->IsInitialized())
  579. {
  580. vtkErrorMacro(<< "SetHighlightedById failed - vtkLightBoxRendererManager is NOT initialized");
  581. return false;
  582. }
  583. if (id < 0 || id >= static_cast<int>(this->Internal->RenderWindowItemList.size()))
  584. {
  585. return false;
  586. }
  587. return this->Internal->RenderWindowItemList.at(id)->HighlightedBoxActor->GetVisibility();
  588. }
  589. //----------------------------------------------------------------------------
  590. bool vtkLightBoxRendererManager::GetHighlighted(int rowId, int columnId)
  591. {
  592. return this->GetHighlightedById(this->ComputeRenderWindowItemId(rowId, columnId));
  593. }
  594. //----------------------------------------------------------------------------
  595. void vtkLightBoxRendererManager::SetHighlightedById(int id, bool highlighted)
  596. {
  597. if (!this->IsInitialized())
  598. {
  599. vtkErrorMacro(<< "SetHighlightedById failed - vtkLightBoxRendererManager is NOT initialized");
  600. return;
  601. }
  602. if (id < 0 || id >= static_cast<int>(this->Internal->RenderWindowItemList.size()))
  603. {
  604. return;
  605. }
  606. this->Internal->RenderWindowItemList.at(id)->HighlightedBoxActor->SetVisibility(highlighted);
  607. this->Modified();
  608. }
  609. //----------------------------------------------------------------------------
  610. void vtkLightBoxRendererManager::SetHighlighted(int rowId, int columnId, bool highlighted)
  611. {
  612. this->SetHighlightedById(this->ComputeRenderWindowItemId(rowId, columnId), highlighted);
  613. }
  614. //----------------------------------------------------------------------------
  615. void vtkLightBoxRendererManager::SetHighlightedBoxColor(double newHighlightedBoxColor[3])
  616. {
  617. if (!this->IsInitialized())
  618. {
  619. vtkErrorMacro(<< "SetHighlightedById failed - vtkLightBoxRendererManager is NOT initialized");
  620. return;
  621. }
  622. if (this->Internal->HighlightedBoxColor[0] == newHighlightedBoxColor[0] &&
  623. this->Internal->HighlightedBoxColor[1] == newHighlightedBoxColor[1] &&
  624. this->Internal->HighlightedBoxColor[2] == newHighlightedBoxColor[2])
  625. {
  626. return;
  627. }
  628. this->Internal->HighlightedBoxColor[0] = newHighlightedBoxColor[0];
  629. this->Internal->HighlightedBoxColor[1] = newHighlightedBoxColor[1];
  630. this->Internal->HighlightedBoxColor[2] = newHighlightedBoxColor[2];
  631. vtkInternal::RenderWindowItemListIt it;
  632. for(it = this->Internal->RenderWindowItemList.begin();
  633. it != this->Internal->RenderWindowItemList.end();
  634. ++it)
  635. {
  636. (*it)->SetHighlightedBoxColor(newHighlightedBoxColor);
  637. }
  638. this->Modified();
  639. }
  640. //----------------------------------------------------------------------------
  641. double* vtkLightBoxRendererManager::GetHighlightedBoxColor() const
  642. {
  643. return this->Internal->HighlightedBoxColor;
  644. }
  645. //----------------------------------------------------------------------------
  646. int vtkLightBoxRendererManager::ComputeRenderWindowItemId(int rowId, int columnId)
  647. {
  648. return this->Internal->RenderWindowColumnCount * rowId + columnId;
  649. }
  650. //----------------------------------------------------------------------------
  651. void vtkLightBoxRendererManager::SetCornerAnnotationText(const std::string& text)
  652. {
  653. if (!this->IsInitialized())
  654. {
  655. vtkErrorMacro(<< "SetCornerAnnotationText failed - "
  656. "vtkLightBoxRendererManager is NOT initialized");
  657. return;
  658. }
  659. if (text.compare(this->Internal->CornerAnnotationText) == 0)
  660. {
  661. return;
  662. }
  663. this->Internal->CornerAnnotation->ClearAllTexts();
  664. this->Internal->CornerAnnotation->SetText(2, text.c_str());
  665. this->Internal->CornerAnnotationText = text;
  666. this->Modified();
  667. }
  668. //----------------------------------------------------------------------------
  669. const std::string vtkLightBoxRendererManager::GetCornerAnnotationText() const
  670. {
  671. const char * text = this->Internal->CornerAnnotation->GetText(2);
  672. return text ? text : "";
  673. }
  674. // --------------------------------------------------------------------------
  675. vtkCornerAnnotation * vtkLightBoxRendererManager::GetCornerAnnotation() const
  676. {
  677. return this->Internal->CornerAnnotation;
  678. }
  679. // --------------------------------------------------------------------------
  680. void vtkLightBoxRendererManager::SetCornerAnnotation(vtkCornerAnnotation* annotation)
  681. {
  682. // Remove current corner annotation
  683. vtkInternal::RenderWindowItemListIt it;
  684. for(it = this->Internal->RenderWindowItemList.begin();
  685. it != this->Internal->RenderWindowItemList.end();
  686. ++it)
  687. {
  688. if (!(*it)->Renderer->HasViewProp(this->Internal->CornerAnnotation))
  689. {
  690. (*it)->Renderer->RemoveViewProp(this->Internal->CornerAnnotation);
  691. }
  692. }
  693. this->Internal->CornerAnnotation = annotation;
  694. }
  695. // --------------------------------------------------------------------------
  696. void vtkLightBoxRendererManager::SetBackgroundColor(const double newBackgroundColor[3])
  697. {
  698. if (!this->IsInitialized())
  699. {
  700. vtkErrorMacro(<< "SetBackgroundColor failed - vtkLightBoxRendererManager is NOT initialized");
  701. return;
  702. }
  703. vtkInternal::RenderWindowItemListIt it;
  704. for(it = this->Internal->RenderWindowItemList.begin();
  705. it != this->Internal->RenderWindowItemList.end();
  706. ++it)
  707. {
  708. (*it)->Renderer->SetBackground(newBackgroundColor[0],
  709. newBackgroundColor[1],
  710. newBackgroundColor[2]);
  711. }
  712. this->Internal->RendererBackgroundColor[0] = newBackgroundColor[0];
  713. this->Internal->RendererBackgroundColor[1] = newBackgroundColor[1];
  714. this->Internal->RendererBackgroundColor[2] = newBackgroundColor[2];
  715. this->Modified();
  716. }
  717. //----------------------------------------------------------------------------
  718. double* vtkLightBoxRendererManager::GetBackgroundColor()const
  719. {
  720. return this->Internal->RendererBackgroundColor;
  721. }
  722. //----------------------------------------------------------------------------
  723. void vtkLightBoxRendererManager::SetColorLevel(double colorLevel)
  724. {
  725. this->SetColorWindowAndLevel(this->Internal->ColorWindow, colorLevel);
  726. }
  727. //----------------------------------------------------------------------------
  728. double vtkLightBoxRendererManager::GetColorLevel()const
  729. {
  730. return this->Internal->ColorLevel;
  731. }
  732. //----------------------------------------------------------------------------
  733. void vtkLightBoxRendererManager::SetColorWindow(double colorWindow)
  734. {
  735. this->SetColorWindowAndLevel(colorWindow, this->Internal->ColorLevel);
  736. }
  737. //----------------------------------------------------------------------------
  738. double vtkLightBoxRendererManager::GetColorWindow()const
  739. {
  740. return this->Internal->ColorWindow;
  741. }
  742. //----------------------------------------------------------------------------
  743. void vtkLightBoxRendererManager::SetColorWindowAndLevel(double colorWindow, double colorLevel)
  744. {
  745. if (this->Internal->ColorWindow == colorWindow &&
  746. this->Internal->ColorLevel == colorLevel)
  747. {
  748. return;
  749. }
  750. vtkInternal::RenderWindowItemListIt it;
  751. for(it = this->Internal->RenderWindowItemList.begin();
  752. it != this->Internal->RenderWindowItemList.end();
  753. ++it)
  754. {
  755. (*it)->ImageMapper->SetColorWindow(colorWindow);
  756. (*it)->ImageMapper->SetColorLevel(colorLevel);
  757. }
  758. this->Internal->ColorWindow = colorWindow;
  759. this->Internal->ColorLevel = colorLevel;
  760. this->Modified();
  761. }