vtkLightBoxRendererManager.cpp 30 KB

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