vtkLightBoxRendererManager.cpp 32 KB

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