vtkLightBoxRendererManager.cpp 32 KB

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