vtkLightBoxRendererManager.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. vtkImageData* vtkLightBoxRendererManager::GetImageData()const
  384. {
  385. return this->Internal->ImageData;
  386. }
  387. //----------------------------------------------------------------------------
  388. vtkCamera* vtkLightBoxRendererManager::GetActiveCamera()
  389. {
  390. if (this->Internal->RenderWindowItemList.size() == 0)
  391. {
  392. return 0;
  393. }
  394. // Obtain reference of the first renderer
  395. vtkRenderer * firstRenderer = this->Internal->RenderWindowItemList.at(0)->Renderer;
  396. if (firstRenderer->IsActiveCameraCreated())
  397. {
  398. return firstRenderer->GetActiveCamera();
  399. }
  400. else
  401. {
  402. return 0;
  403. }
  404. return 0;
  405. }
  406. //----------------------------------------------------------------------------
  407. void vtkLightBoxRendererManager::SetActiveCamera(vtkCamera* newActiveCamera)
  408. {
  409. if (!this->IsInitialized())
  410. {
  411. vtkErrorMacro(<< "SetActiveCamera failed - vtkLightBoxRendererManager is NOT initialized");
  412. return;
  413. }
  414. if (newActiveCamera == this->GetActiveCamera())
  415. {
  416. return;
  417. }
  418. newActiveCamera->ParallelProjectionOn();
  419. vtkInternal::RenderWindowItemListIt it;
  420. for(it = this->Internal->RenderWindowItemList.begin();
  421. it != this->Internal->RenderWindowItemList.end();
  422. ++it)
  423. {
  424. (*it)->Renderer->SetActiveCamera(newActiveCamera);
  425. }
  426. this->Modified();
  427. }
  428. //----------------------------------------------------------------------------
  429. void vtkLightBoxRendererManager::ResetCamera()
  430. {
  431. if (!this->IsInitialized())
  432. {
  433. vtkErrorMacro(<< "ResetCamera failed - vtkLightBoxRendererManager is NOT initialized");
  434. return;
  435. }
  436. vtkInternal::RenderWindowItemListIt it;
  437. for(it = this->Internal->RenderWindowItemList.begin();
  438. it != this->Internal->RenderWindowItemList.end();
  439. ++it)
  440. {
  441. (*it)->Renderer->ResetCamera();
  442. }
  443. this->Modified();
  444. }
  445. //----------------------------------------------------------------------------
  446. int vtkLightBoxRendererManager::GetRenderWindowItemCount()
  447. {
  448. return static_cast<int>(this->Internal->RenderWindowItemList.size());
  449. }
  450. //----------------------------------------------------------------------------
  451. vtkRenderer* vtkLightBoxRendererManager::GetRenderer(int id)
  452. {
  453. if (id < 0 || id >= static_cast<int>(this->Internal->RenderWindowItemList.size()))
  454. {
  455. return 0;
  456. }
  457. return this->Internal->RenderWindowItemList.at(id)->Renderer;
  458. }
  459. //----------------------------------------------------------------------------
  460. vtkRenderer* vtkLightBoxRendererManager::GetRenderer(int rowId, int columnId)
  461. {
  462. return this->GetRenderer(this->ComputeRenderWindowItemId(rowId, columnId));
  463. }
  464. //----------------------------------------------------------------------------
  465. void vtkLightBoxRendererManager::SetRenderWindowLayoutType(int layoutType)
  466. {
  467. if (this->Internal->RenderWindowLayoutType == layoutType)
  468. {
  469. return;
  470. }
  471. if (this->Internal->ImageData)
  472. {
  473. this->Internal->updateRenderWindowItemsZIndex(layoutType);
  474. }
  475. this->Internal->RenderWindowLayoutType = layoutType;
  476. this->Modified();
  477. }
  478. //----------------------------------------------------------------------------
  479. int vtkLightBoxRendererManager::GetRenderWindowLayoutType() const
  480. {
  481. return this->Internal->RenderWindowLayoutType;
  482. }
  483. //----------------------------------------------------------------------------
  484. void vtkLightBoxRendererManager::SetRenderWindowLayout(int rowCount, int columnCount)
  485. {
  486. if (!this->IsInitialized())
  487. {
  488. vtkErrorMacro(<< "SetRenderWindowLayout failed - "
  489. "vtkLightBoxRendererManager is NOT initialized");
  490. return;
  491. }
  492. // Sanity checks
  493. assert(rowCount >= 0 && columnCount >= 0);
  494. if(!(rowCount >= 0 && columnCount >= 0))
  495. {
  496. return;
  497. }
  498. if (this->Internal->RenderWindowRowCount == rowCount &&
  499. this->Internal->RenderWindowColumnCount == columnCount)
  500. {
  501. return;
  502. }
  503. int extraItem = (rowCount * columnCount)
  504. - static_cast<int>(this->Internal->RenderWindowItemList.size());
  505. if (extraItem > 0)
  506. {
  507. //std::cout << "Creating " << extraItem << " RenderWindowItem";
  508. // Create extra RenderWindowItem(s)
  509. while(extraItem > 0)
  510. {
  511. RenderWindowItem * item =
  512. new RenderWindowItem(this->Internal->RendererBackgroundColor,
  513. this->Internal->HighlightedBoxColor,
  514. this->Internal->ColorWindow, this->Internal->ColorLevel);
  515. item->Renderer->SetLayer(this->Internal->RendererLayer);
  516. item->ImageMapper->SetInput(this->Internal->ImageData);
  517. this->Internal->RenderWindowItemList.push_back(item);
  518. --extraItem;
  519. }
  520. }
  521. else
  522. {
  523. //std::cout << "Removing " << extraItem << " RenderWindowItem";
  524. // Remove extra RenderWindowItem(s)
  525. extraItem = extraItem >= 0 ? extraItem : -extraItem; // Compute Abs
  526. while(extraItem > 0)
  527. {
  528. delete this->Internal->RenderWindowItemList.back();
  529. this->Internal->RenderWindowItemList.pop_back();
  530. --extraItem;
  531. }
  532. }
  533. this->Internal->RenderWindowRowCount = rowCount;
  534. this->Internal->RenderWindowColumnCount = columnCount;
  535. this->Internal->setupRendering();
  536. this->Internal->SetupCornerAnnotation();
  537. if (this->Internal->ImageData)
  538. {
  539. this->Internal->updateRenderWindowItemsZIndex(this->Internal->RenderWindowLayoutType);
  540. }
  541. this->Modified();
  542. }
  543. //----------------------------------------------------------------------------
  544. int vtkLightBoxRendererManager::GetRenderWindowRowCount()
  545. {
  546. return this->Internal->RenderWindowRowCount;
  547. }
  548. //----------------------------------------------------------------------------
  549. void vtkLightBoxRendererManager::SetRenderWindowRowCount(int newRowCount)
  550. {
  551. this->SetRenderWindowLayout(newRowCount, this->GetRenderWindowColumnCount());
  552. }
  553. //----------------------------------------------------------------------------
  554. int vtkLightBoxRendererManager::GetRenderWindowColumnCount()
  555. {
  556. return this->Internal->RenderWindowColumnCount;
  557. }
  558. //----------------------------------------------------------------------------
  559. void vtkLightBoxRendererManager::SetRenderWindowColumnCount(int newColumnCount)
  560. {
  561. this->SetRenderWindowLayout(this->GetRenderWindowRowCount(), newColumnCount);
  562. }
  563. //----------------------------------------------------------------------------
  564. bool vtkLightBoxRendererManager::GetHighlightedById(int id)
  565. {
  566. if (!this->IsInitialized())
  567. {
  568. vtkErrorMacro(<< "SetHighlightedById failed - vtkLightBoxRendererManager is NOT initialized");
  569. return false;
  570. }
  571. if (id < 0 || id >= static_cast<int>(this->Internal->RenderWindowItemList.size()))
  572. {
  573. return false;
  574. }
  575. return this->Internal->RenderWindowItemList.at(id)->HighlightedBoxActor->GetVisibility();
  576. }
  577. //----------------------------------------------------------------------------
  578. bool vtkLightBoxRendererManager::GetHighlighted(int rowId, int columnId)
  579. {
  580. return this->GetHighlightedById(this->ComputeRenderWindowItemId(rowId, columnId));
  581. }
  582. //----------------------------------------------------------------------------
  583. void vtkLightBoxRendererManager::SetHighlightedById(int id, bool highlighted)
  584. {
  585. if (!this->IsInitialized())
  586. {
  587. vtkErrorMacro(<< "SetHighlightedById failed - vtkLightBoxRendererManager is NOT initialized");
  588. return;
  589. }
  590. if (id < 0 || id >= static_cast<int>(this->Internal->RenderWindowItemList.size()))
  591. {
  592. return;
  593. }
  594. this->Internal->RenderWindowItemList.at(id)->HighlightedBoxActor->SetVisibility(highlighted);
  595. this->Modified();
  596. }
  597. //----------------------------------------------------------------------------
  598. void vtkLightBoxRendererManager::SetHighlighted(int rowId, int columnId, bool highlighted)
  599. {
  600. this->SetHighlightedById(this->ComputeRenderWindowItemId(rowId, columnId), highlighted);
  601. }
  602. //----------------------------------------------------------------------------
  603. void vtkLightBoxRendererManager::SetHighlightedBoxColor(double newHighlightedBoxColor[3])
  604. {
  605. if (!this->IsInitialized())
  606. {
  607. vtkErrorMacro(<< "SetHighlightedById failed - vtkLightBoxRendererManager is NOT initialized");
  608. return;
  609. }
  610. if (this->Internal->HighlightedBoxColor[0] == newHighlightedBoxColor[0] &&
  611. this->Internal->HighlightedBoxColor[1] == newHighlightedBoxColor[1] &&
  612. this->Internal->HighlightedBoxColor[2] == newHighlightedBoxColor[2])
  613. {
  614. return;
  615. }
  616. this->Internal->HighlightedBoxColor[0] = newHighlightedBoxColor[0];
  617. this->Internal->HighlightedBoxColor[1] = newHighlightedBoxColor[1];
  618. this->Internal->HighlightedBoxColor[2] = newHighlightedBoxColor[2];
  619. vtkInternal::RenderWindowItemListIt it;
  620. for(it = this->Internal->RenderWindowItemList.begin();
  621. it != this->Internal->RenderWindowItemList.end();
  622. ++it)
  623. {
  624. (*it)->SetHighlightedBoxColor(newHighlightedBoxColor);
  625. }
  626. this->Modified();
  627. }
  628. //----------------------------------------------------------------------------
  629. double* vtkLightBoxRendererManager::GetHighlightedBoxColor() const
  630. {
  631. return this->Internal->HighlightedBoxColor;
  632. }
  633. //----------------------------------------------------------------------------
  634. int vtkLightBoxRendererManager::ComputeRenderWindowItemId(int rowId, int columnId)
  635. {
  636. return this->Internal->RenderWindowColumnCount * rowId + columnId;
  637. }
  638. //----------------------------------------------------------------------------
  639. void vtkLightBoxRendererManager::SetCornerAnnotationText(const std::string& text)
  640. {
  641. if (!this->IsInitialized())
  642. {
  643. vtkErrorMacro(<< "SetCornerAnnotationText failed - "
  644. "vtkLightBoxRendererManager is NOT initialized");
  645. return;
  646. }
  647. if (text.compare(this->Internal->CornerAnnotationText) == 0)
  648. {
  649. return;
  650. }
  651. this->Internal->CornerAnnotation->ClearAllTexts();
  652. this->Internal->CornerAnnotation->SetText(2, text.c_str());
  653. this->Internal->CornerAnnotationText = text;
  654. this->Modified();
  655. }
  656. //----------------------------------------------------------------------------
  657. const std::string vtkLightBoxRendererManager::GetCornerAnnotationText() const
  658. {
  659. const char * text = this->Internal->CornerAnnotation->GetText(2);
  660. return text ? text : "";
  661. }
  662. // --------------------------------------------------------------------------
  663. vtkCornerAnnotation * vtkLightBoxRendererManager::GetCornerAnnotation() const
  664. {
  665. return this->Internal->CornerAnnotation;
  666. }
  667. // --------------------------------------------------------------------------
  668. void vtkLightBoxRendererManager::SetCornerAnnotation(vtkCornerAnnotation* annotation)
  669. {
  670. // Remove current corner annotation
  671. vtkInternal::RenderWindowItemListIt it;
  672. for(it = this->Internal->RenderWindowItemList.begin();
  673. it != this->Internal->RenderWindowItemList.end();
  674. ++it)
  675. {
  676. if (!(*it)->Renderer->HasViewProp(this->Internal->CornerAnnotation))
  677. {
  678. (*it)->Renderer->RemoveViewProp(this->Internal->CornerAnnotation);
  679. }
  680. }
  681. this->Internal->CornerAnnotation = annotation;
  682. }
  683. // --------------------------------------------------------------------------
  684. void vtkLightBoxRendererManager::SetBackgroundColor(const double newBackgroundColor[3])
  685. {
  686. if (!this->IsInitialized())
  687. {
  688. vtkErrorMacro(<< "SetBackgroundColor failed - vtkLightBoxRendererManager is NOT initialized");
  689. return;
  690. }
  691. vtkInternal::RenderWindowItemListIt it;
  692. for(it = this->Internal->RenderWindowItemList.begin();
  693. it != this->Internal->RenderWindowItemList.end();
  694. ++it)
  695. {
  696. (*it)->Renderer->SetBackground(newBackgroundColor[0],
  697. newBackgroundColor[1],
  698. newBackgroundColor[2]);
  699. }
  700. this->Internal->RendererBackgroundColor[0] = newBackgroundColor[0];
  701. this->Internal->RendererBackgroundColor[1] = newBackgroundColor[1];
  702. this->Internal->RendererBackgroundColor[2] = newBackgroundColor[2];
  703. this->Modified();
  704. }
  705. //----------------------------------------------------------------------------
  706. double* vtkLightBoxRendererManager::GetBackgroundColor()const
  707. {
  708. return this->Internal->RendererBackgroundColor;
  709. }
  710. //----------------------------------------------------------------------------
  711. void vtkLightBoxRendererManager::SetColorLevel(double colorLevel)
  712. {
  713. this->SetColorWindowAndLevel(this->Internal->ColorWindow, colorLevel);
  714. }
  715. //----------------------------------------------------------------------------
  716. double vtkLightBoxRendererManager::GetColorLevel()const
  717. {
  718. return this->Internal->ColorLevel;
  719. }
  720. //----------------------------------------------------------------------------
  721. void vtkLightBoxRendererManager::SetColorWindow(double colorWindow)
  722. {
  723. this->SetColorWindowAndLevel(colorWindow, this->Internal->ColorLevel);
  724. }
  725. //----------------------------------------------------------------------------
  726. double vtkLightBoxRendererManager::GetColorWindow()const
  727. {
  728. return this->Internal->ColorWindow;
  729. }
  730. //----------------------------------------------------------------------------
  731. void vtkLightBoxRendererManager::SetColorWindowAndLevel(double colorWindow, double colorLevel)
  732. {
  733. if (this->Internal->ColorWindow == colorWindow &&
  734. this->Internal->ColorLevel == colorLevel)
  735. {
  736. return;
  737. }
  738. vtkInternal::RenderWindowItemListIt it;
  739. for(it = this->Internal->RenderWindowItemList.begin();
  740. it != this->Internal->RenderWindowItemList.end();
  741. ++it)
  742. {
  743. (*it)->ImageMapper->SetColorWindow(colorWindow);
  744. (*it)->ImageMapper->SetColorLevel(colorLevel);
  745. }
  746. this->Internal->ColorWindow = colorWindow;
  747. this->Internal->ColorLevel = colorLevel;
  748. this->Modified();
  749. }