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