vtkLightBoxRendererManager.cpp 32 KB

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