vtkLightBoxRendererManager.cpp 29 KB

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