vtkLightBoxRendererManager.cpp 29 KB

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