vtkLightBoxRendererManager.cpp 28 KB

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