vtkLightBoxRendererManager.cpp 31 KB

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