Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkVtkLayerController.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkVtkLayerController.h"
18 
19 #include <algorithm>
20 #include <vtkObjectFactory.h>
21 #include <vtkRenderWindow.h>
22 #include <vtkRenderer.h>
23 #include <vtkRendererCollection.h>
24 
25 mitk::VtkLayerController::vtkLayerControllerMapType mitk::VtkLayerController::s_LayerControllerMap;
26 
27 mitk::VtkLayerController *mitk::VtkLayerController::GetInstance(vtkSmartPointer<vtkRenderWindow> renWin)
28 {
29  for (auto mapit = s_LayerControllerMap.begin(); mapit != s_LayerControllerMap.end(); ++mapit)
30  {
31  if ((*mapit).first == renWin)
32  return (*mapit).second;
33  }
34  return nullptr;
35 }
36 
37 void mitk::VtkLayerController::AddInstance(vtkSmartPointer<vtkRenderWindow> renWin,
38  vtkSmartPointer<vtkRenderer> mitkSceneRenderer)
39 {
40  // ensure that no vtkRenderWindow is managed twice
42 
43  // instanciate controller, add it to the map
44  mitk::VtkLayerController *ControllerInstance = new mitk::VtkLayerController(renWin);
45  ControllerInstance->InsertSceneRenderer(mitkSceneRenderer);
46 
47  s_LayerControllerMap.insert(vtkLayerControllerMapType::value_type(renWin, ControllerInstance));
48 }
49 
50 void mitk::VtkLayerController::RemoveInstance(vtkSmartPointer<vtkRenderWindow> renWin)
51 {
52  auto mapit = s_LayerControllerMap.find(renWin);
53  if (mapit != s_LayerControllerMap.end())
54  {
55  delete mapit->second;
56  s_LayerControllerMap.erase(mapit);
57  }
58 }
59 
60 mitk::VtkLayerController::VtkLayerController(vtkSmartPointer<vtkRenderWindow> renderWindow)
61 {
62  m_RenderWindow = renderWindow;
63  m_RenderWindow->Register(nullptr);
64  m_BackgroundRenderers.clear();
65  m_ForegroundRenderers.clear();
66  m_SceneRenderers.clear();
67 }
68 
70 {
71  if (m_RenderWindow != nullptr)
72  {
73  m_RenderWindow->UnRegister(nullptr);
74  }
75 }
76 
82 void mitk::VtkLayerController::InsertBackgroundRenderer(vtkSmartPointer<vtkRenderer> renderer,
83  bool forceAbsoluteBackground)
84 {
85  if (renderer == nullptr)
86  return;
87 
88  // Remove renderer if it already exists
89  RemoveRenderer(renderer);
90 
91  if (forceAbsoluteBackground)
92  {
93  auto it = m_BackgroundRenderers.begin();
94  m_BackgroundRenderers.insert(it, renderer);
95  }
96  else
97  m_BackgroundRenderers.push_back(renderer);
98  UpdateLayers();
99 }
105 void mitk::VtkLayerController::InsertForegroundRenderer(vtkSmartPointer<vtkRenderer> renderer,
106  bool forceAbsoluteForeground)
107 {
108  if (renderer == nullptr)
109  return;
110 
111  // Remove renderer if it already exists
112  RemoveRenderer(renderer);
113 
114  if (forceAbsoluteForeground)
115  {
116  auto it = m_ForegroundRenderers.begin();
117  m_ForegroundRenderers.insert(it, renderer);
118  }
119  else
120  m_ForegroundRenderers.push_back(renderer);
121 
122  renderer->PreserveDepthBufferOn();
123  UpdateLayers();
124 }
128 vtkSmartPointer<vtkRenderer> mitk::VtkLayerController::GetSceneRenderer()
129 {
130  if (m_SceneRenderers.size() > 0)
131  {
132  auto it = m_SceneRenderers.begin();
133  return (*it);
134  }
135  else
136  return nullptr;
137 }
142 void mitk::VtkLayerController::InsertSceneRenderer(vtkSmartPointer<vtkRenderer> renderer)
143 {
144  if (renderer == nullptr)
145  return;
146 
147  // Remove renderer if it already exists
148  RemoveRenderer(renderer);
149 
150  m_SceneRenderers.push_back(renderer);
151  UpdateLayers();
152 }
157 void mitk::VtkLayerController::RemoveRenderer(vtkSmartPointer<vtkRenderer> renderer)
158 {
159  RendererVectorType::iterator it;
160  // background layers
161  if (m_BackgroundRenderers.size() > 0)
162  {
163  it = std::find(m_BackgroundRenderers.begin(), m_BackgroundRenderers.end(), renderer);
164  if (it != m_BackgroundRenderers.end())
165  {
166  m_BackgroundRenderers.erase(it);
167  UpdateLayers();
168  return;
169  }
170  }
171  // scene layers
172  if (m_SceneRenderers.size() > 0)
173  {
174  it = std::find(m_SceneRenderers.begin(), m_SceneRenderers.end(), renderer);
175  if (it != m_SceneRenderers.end())
176  {
177  m_SceneRenderers.erase(it);
178  UpdateLayers();
179  return;
180  }
181  }
182  // foreground layers
183  if (m_ForegroundRenderers.size() > 0)
184  {
185  it = std::find(m_ForegroundRenderers.begin(), m_ForegroundRenderers.end(), renderer);
186  if (it != m_ForegroundRenderers.end())
187  {
188  m_ForegroundRenderers.erase(it);
189  UpdateLayers();
190  return;
191  }
192  }
193 }
197 void mitk::VtkLayerController::SetRenderWindow(vtkSmartPointer<vtkRenderWindow> renwin)
198 {
199  if (renwin != nullptr)
200  {
201  RendererVectorType::iterator it;
202  // Tell all renderers that there is a new renderwindow
203  for (it = m_BackgroundRenderers.begin(); it != m_BackgroundRenderers.end(); ++it)
204  {
205  (*it)->SetRenderWindow(renwin);
206  }
207  for (it = m_SceneRenderers.begin(); it != m_SceneRenderers.end(); ++it)
208  {
209  (*it)->SetRenderWindow(renwin);
210  }
211  for (it = m_ForegroundRenderers.begin(); it != m_ForegroundRenderers.end(); ++it)
212  {
213  (*it)->SetRenderWindow(renwin);
214  }
215  // Set the new RenderWindow
216  m_RenderWindow = renwin;
217  }
218  // Now sort renderers and add them to the renderwindow
219  UpdateLayers();
220 }
221 
225 bool mitk::VtkLayerController::IsRendererInserted(vtkSmartPointer<vtkRenderer> renderer)
226 {
227  RendererVectorType::iterator it;
228  // background layers
229  if (m_BackgroundRenderers.size() > 0)
230  {
231  it = std::find(m_BackgroundRenderers.begin(), m_BackgroundRenderers.end(), renderer);
232  if (it != m_BackgroundRenderers.end())
233  {
234  return true;
235  }
236  }
237  // scene layers
238  if (m_SceneRenderers.size() > 0)
239  {
240  it = std::find(m_SceneRenderers.begin(), m_SceneRenderers.end(), renderer);
241  if (it != m_SceneRenderers.end())
242  {
243  return true;
244  }
245  }
246  // foreground layers
247  if (m_ForegroundRenderers.size() > 0)
248  {
249  it = std::find(m_ForegroundRenderers.begin(), m_ForegroundRenderers.end(), renderer);
250  if (it != m_ForegroundRenderers.end())
251  {
252  return true;
253  }
254  }
255  return false;
256 }
262 void mitk::VtkLayerController::UpdateLayers()
263 {
264  // Remove all Renderers from renderwindow
265  vtkSmartPointer<vtkRendererCollection> v = m_RenderWindow->GetRenderers();
266  v->RemoveAllItems();
267 
268  unsigned int numberOfLayers =
269  static_cast<unsigned int>(m_BackgroundRenderers.size() + m_SceneRenderers.size() + m_ForegroundRenderers.size());
270  int currentLayerNumber;
271  bool traverseUpwards;
272 
273  currentLayerNumber = 0;
274  traverseUpwards = true;
275 
276  m_RenderWindow->SetNumberOfLayers(numberOfLayers);
277  RendererVectorType::iterator it;
278  // assign a layer number for the backround renderers
279  for (it = m_BackgroundRenderers.begin(); it != m_BackgroundRenderers.end(); ++it)
280  {
281  (*it)->SetRenderWindow(m_RenderWindow);
282  (*it)->SetLayer(currentLayerNumber);
283  m_RenderWindow->AddRenderer((*it));
284 
285  if (traverseUpwards)
286  currentLayerNumber++;
287  else
288  currentLayerNumber--;
289  }
290  // assign a layer number for the scene renderers
291  for (it = m_SceneRenderers.begin(); it != m_SceneRenderers.end(); ++it)
292  {
293  (*it)->SetRenderWindow(m_RenderWindow);
294  (*it)->SetLayer(currentLayerNumber);
295  m_RenderWindow->AddRenderer((*it));
296 
297  if (traverseUpwards)
298  currentLayerNumber++;
299  else
300  currentLayerNumber--;
301  }
302  // assign a layer number for the foreground renderers
303  for (it = m_ForegroundRenderers.begin(); it != m_ForegroundRenderers.end(); ++it)
304  {
305  (*it)->SetRenderWindow(m_RenderWindow);
306  (*it)->SetLayer(currentLayerNumber);
307  m_RenderWindow->AddRenderer((*it));
308 
309  if (traverseUpwards)
310  currentLayerNumber++;
311  else
312  currentLayerNumber--;
313  }
314 }
319 {
320  return static_cast<unsigned int>(m_BackgroundRenderers.size() + m_SceneRenderers.size() +
321  m_ForegroundRenderers.size());
322 }
323 
325 {
326  this->m_RenderWindow->SetErase(i);
327 
328  RendererVectorType::iterator it;
329  for (it = m_BackgroundRenderers.begin(); it != m_BackgroundRenderers.end(); ++it)
330  (*it)->SetErase(i);
331 
332  for (it = m_SceneRenderers.begin(); it != m_SceneRenderers.end(); ++it)
333  (*it)->SetErase(i);
334 
335  for (it = m_ForegroundRenderers.begin(); it != m_ForegroundRenderers.end(); ++it)
336  (*it)->SetErase(i);
337 }
void InsertSceneRenderer(vtkSmartPointer< vtkRenderer > renderer)
void InsertForegroundRenderer(vtkSmartPointer< vtkRenderer > renderer, bool forceAbsoluteForeground)
static void AddInstance(vtkSmartPointer< vtkRenderWindow > renWin, vtkSmartPointer< vtkRenderer > mitkSceneRenderer)
static VtkLayerController * GetInstance(vtkSmartPointer< vtkRenderWindow > renWin)
void SetRenderWindow(vtkSmartPointer< vtkRenderWindow > renwin)
VtkLayerController(vtkSmartPointer< vtkRenderWindow > renderWindow)
vtkSmartPointer< vtkRenderer > GetSceneRenderer()
void RemoveRenderer(vtkSmartPointer< vtkRenderer > renderer)
bool IsRendererInserted(vtkSmartPointer< vtkRenderer > renderer)
void InsertBackgroundRenderer(vtkSmartPointer< vtkRenderer > renderer, bool forceAbsoluteBackground)
static void RemoveInstance(vtkSmartPointer< vtkRenderWindow > renWin)