Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkOverlayManager.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 "mitkOverlayManager.h"
18 #include "mitkBaseRenderer.h"
19 
20 #include <mitkVtkLayerController.h>
21 
22 namespace mitk
23 {
24  itkEventMacroDefinition(OverlayAddEvent, itk::AnyEvent)
25 
27  {
28  }
29 
31  {
34  }
35 
37  {
38  if (!m_ForegroundRenderer[renderer])
39  {
40  m_ForegroundRenderer[renderer] = vtkSmartPointer<vtkRenderer>::New();
41  vtkRenderer *rendererVtk = m_ForegroundRenderer[renderer];
42  rendererVtk->SetActiveCamera(renderer->GetVtkRenderer()->GetActiveCamera());
43  VtkLayerController::GetInstance(renderer->GetRenderWindow())->InsertForegroundRenderer(rendererVtk, false);
44  rendererVtk->SetInteractive(false);
45  }
46  std::pair<BaseRendererSet::iterator, bool> inSet;
47  inSet = m_BaseRendererSet.insert(renderer);
48  if (inSet.second)
49  {
50  OverlaySet::const_iterator it;
51  for (it = m_OverlaySet.cbegin(); it != m_OverlaySet.cend(); ++it)
52  {
53  if ((*it)->IsForceInForeground())
54  (*it)->AddToRenderer(renderer, m_ForegroundRenderer[renderer]);
55  else
56  (*it)->AddToBaseRenderer(renderer);
57  }
58  }
59  }
60 
62  {
63  if (!renderer)
64  return;
65 
66  vtkRenderer *forgroundRenderer = m_ForegroundRenderer[renderer];
67  OverlaySet::const_iterator it;
68  for (it = m_OverlaySet.cbegin(); it != m_OverlaySet.cend(); ++it)
69  {
70  (*it)->RemoveFromBaseRenderer(renderer);
71  if (forgroundRenderer)
72  (*it)->RemoveFromRenderer(renderer, forgroundRenderer);
73  }
74 
75  BaseRendererSet::const_iterator i = m_BaseRendererSet.find(renderer);
76  if (i == m_BaseRendererSet.cend())
77  return;
78 
79  m_BaseRendererSet.erase(i);
80 
81  m_ForegroundRenderer[renderer] = NULL;
82  }
83 
85  {
86  BaseRendererSet::const_iterator it;
87  for (it = m_BaseRendererSet.cbegin(); it != m_BaseRendererSet.cend(); ++it)
88  {
89  this->RemoveBaseRenderer(*it);
90  }
91  }
92 
95  {
97  for (auto renderwindow : renderwindows)
98  {
99  BaseRenderer *renderer = BaseRenderer::GetInstance(renderwindow);
100  if (renderer && renderer->GetOverlayManager().IsNotNull())
101  return renderer->GetOverlayManager();
102  }
103  return nullptr;
104  }
105 
106  void OverlayManager::AddOverlay(const Overlay::Pointer &overlay, bool ForceInForeground)
107  {
108  std::pair<OverlaySet::iterator, bool> inSet;
109  inSet = m_OverlaySet.insert(overlay);
110  if (inSet.second)
111  {
112  BaseRendererSet::const_iterator it;
113  for (it = m_BaseRendererSet.cbegin(); it != m_BaseRendererSet.cend(); ++it)
114  {
115  if (ForceInForeground)
116  {
117  vtkRenderer *forgroundRenderer = m_ForegroundRenderer[*it];
118  overlay->AddToRenderer(*it, forgroundRenderer);
119  }
120  else
121  overlay->AddToBaseRenderer(*it);
122  this->InvokeEvent(OverlayAddEvent());
123  }
124  }
125  }
126 
127  void OverlayManager::AddOverlay(const Overlay::Pointer &overlay, BaseRenderer *renderer, bool ForceInForeground)
128  {
129  std::pair<OverlaySet::iterator, bool> inSet;
130  inSet = m_OverlaySet.insert(overlay);
131  if (inSet.second)
132  {
133  if (ForceInForeground)
134  {
135  vtkRenderer *forgroundRenderer = m_ForegroundRenderer[renderer];
136  overlay->AddToRenderer(renderer, forgroundRenderer);
137  }
138  else
139  overlay->AddToBaseRenderer(renderer);
140  this->InvokeEvent(OverlayAddEvent());
141  }
142  }
143 
145  {
146  OverlaySet::const_iterator overlayIt = m_OverlaySet.find(overlay);
147  if (overlayIt == m_OverlaySet.cend())
148  return;
149 
150  BaseRendererSet::const_iterator it;
151  for (it = m_BaseRendererSet.cbegin(); it != m_BaseRendererSet.cend(); ++it)
152  {
153  overlay->RemoveFromBaseRenderer(*it);
154  vtkRenderer *forgroundRenderer = m_ForegroundRenderer[*it];
155  if (forgroundRenderer)
156  overlay->RemoveFromRenderer(*it, forgroundRenderer);
157  }
158 
159  m_OverlaySet.erase(overlayIt);
160  this->InvokeEvent(OverlayAddEvent());
161  }
162 
164  {
165  while (!m_OverlaySet.empty())
166  RemoveOverlay(*m_OverlaySet.cbegin());
167  }
168 
170  {
171  OverlaySet::const_iterator it;
172  for (it = m_OverlaySet.cbegin(); it != m_OverlaySet.cend(); ++it)
173  {
174  (*it)->Update(baseRenderer);
175  }
176  UpdateLayouts(baseRenderer);
177  }
178 
179  void OverlayManager::SetLayouter(Overlay *overlay, const std::string &identifier, BaseRenderer *renderer)
180  {
181  if (renderer)
182  {
183  AbstractOverlayLayouter::Pointer layouter = GetLayouter(renderer, identifier);
184  if (layouter.IsNull())
185  {
186  MITK_WARN << "Layouter " << identifier << " cannot be found or created!";
187  return;
188  }
189  else
190  {
191  layouter->AddOverlay(overlay);
192  }
193  }
194  }
195 
197  {
198  const LayouterMap layouters = m_LayouterMap[renderer];
199  LayouterMap::const_iterator it;
200  for (it = layouters.cbegin(); it != layouters.cend(); ++it)
201  {
202  (it->second)->PrepareLayout();
203  }
204  }
205 
207  {
208  AbstractOverlayLayouter::Pointer layouter = m_LayouterMap[renderer][identifier];
209  return layouter;
210  }
211 
213  {
214  if (layouter.IsNotNull())
215  {
217  m_LayouterMap[layouter->GetBaseRenderer()][layouter->GetIdentifier()];
218  if (oldLayouter.IsNotNull() && oldLayouter.GetPointer() != layouter.GetPointer())
219  {
220  MITK_WARN << "Layouter " << layouter->GetIdentifier() << " does already exist!";
221  }
222  else if (oldLayouter.IsNull())
223  {
224  m_LayouterMap[layouter->GetBaseRenderer()][layouter->GetIdentifier()] = layouter;
225  }
226  }
227  }
228 }
itk::SmartPointer< Self > Pointer
itkEventMacroDefinition(OverlayAddEvent, itk::AnyEvent) OverlayManager
void AddLayouter(const AbstractOverlayLayouter::Pointer &layouter)
Add a layouter to provide it with the use of the SetLayouter method.
static BaseRenderer * GetInstance(vtkRenderWindow *renWin)
static OverlayManager * GetInstance()
Organizes the rendering process.
DataCollection - Class to facilitate loading/accessing structured data.
std::set< Overlay::Pointer > OverlaySet
AbstractOverlayLayouter::Pointer GetLayouter(BaseRenderer *renderer, const std::string &identifier)
Returns the Layouter specified by renderer and the identifier.
static VtkLayerController * GetInstance(vtkSmartPointer< vtkRenderWindow > renWin)
#define MITK_WARN
Definition: mitkLogMacros.h:23
void RemoveOverlay(const Overlay::Pointer &overlay)
static RenderingManager * GetInstance()
void SetLayouter(Overlay *overlay, const std::string &identifier, BaseRenderer *renderer)
Adds the overlay to the layouter specified by identifier and renderer.
void RemoveBaseRenderer(mitk::BaseRenderer *renderer)
itk::SmartPointer< OverlayManager > GetOverlayManager()
Get the OverlayManager registered with this renderer if none was set, it will be created at this poin...
void AddBaseRenderer(BaseRenderer *renderer)
std::map< const std::string, AbstractOverlayLayouter::Pointer > LayouterMap
Base class for all overlays.
Definition: mitkOverlay.h:34
void UpdateLayouts(BaseRenderer *renderer)
Calls all layouters to update the position and size of the registered Overlays.
void AddOverlay(const Overlay::Pointer &overlay, bool ForceInForeground=true)
void UpdateOverlays(BaseRenderer *baseRenderer)
The layout of each Overlay will be prepared and the properties of each Overlay is updated...
void RemoveAllOverlays()
Clears the manager of all Overlays.
const RenderWindowVector & GetAllRegisteredRenderWindows()
const OverlaySet & GetAllOverlays()
The OverlayManager updates and manages Overlays and the respective Layouters.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.