Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkAnnotationUtils.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkAnnotationUtils.h"
14 #include "mitkAnnotation.h"
15 #include "vtkCallbackCommand.h"
16 #include "vtkCommand.h"
18 
19 namespace mitk
20 {
24  const std::string &rendererID)
25  {
26  // get the context
27  us::ModuleContext *context = us::GetModuleContext();
28 
29  // specify a filter that defines the requested type
30  std::string filter = "(&(" + AbstractAnnotationRenderer::US_PROPKEY_ID + "=" + arTypeID + ")(" +
32  // find the fitting service
33  std::vector<us::ServiceReferenceU> serviceReferences =
34  context->GetServiceReferences(AbstractAnnotationRenderer::US_INTERFACE_NAME, filter);
35 
36  // check if a service reference was found. It is also possible that several
37  // services were found. This is not checked here, just the first one is taken.
38  AbstractAnnotationRenderer *ar = nullptr;
39  if (serviceReferences.size())
40  {
41  ar = context->GetService<AbstractAnnotationRenderer>(serviceReferences.front());
42  }
43  // no service reference was found or found service reference has no valid source
44  return ar;
45  }
46 
48  {
50  // Define ServiceProps
53  props[AbstractAnnotationRenderer::US_PROPKEY_ID] = annotationRenderer->GetID();
54 
55  us::GetModuleContext()->RegisterService(annotationRenderer, props);
56  AnnotationRendererServices.push_back(std::unique_ptr<AbstractAnnotationRenderer>(annotationRenderer));
57  }
58 
59  void AnnotationUtils::UpdateAnnotationRenderer(const std::string &rendererID)
60  {
61  for (AbstractAnnotationRenderer *annotationRenderer : GetAnnotationRenderer(rendererID))
62  {
63  annotationRenderer->Update();
64  }
65  }
66 
68  {
69  for (AbstractAnnotationRenderer *annotationRenderer : GetAnnotationRenderer(renderer->GetName()))
70  {
71  annotationRenderer->CurrentBaseRendererChanged();
72  }
73  vtkCallbackCommand *renderCallbackCommand = vtkCallbackCommand::New();
74  renderCallbackCommand->SetCallback(AnnotationUtils::RenderWindowCallback);
75  renderer->GetRenderWindow()->AddObserver(vtkCommand::ModifiedEvent, renderCallbackCommand);
76  renderCallbackCommand->Delete();
77  }
78 
79  void AnnotationUtils::RenderWindowCallback(vtkObject *caller, unsigned long, void *, void *)
80  {
81  auto *renderWindow = dynamic_cast<vtkRenderWindow *>(caller);
82  if (!renderWindow)
83  return;
84  BaseRenderer *renderer = BaseRenderer::GetInstance(renderWindow);
85 
86  if (nullptr != renderer)
87  {
88  for (AbstractAnnotationRenderer *annotationRenderer : GetAnnotationRenderer(renderer->GetName()))
89  annotationRenderer->OnRenderWindowModified();
90  }
91  }
92 
93  Annotation *AnnotationUtils::GetAnnotation(const std::string &AnnotationID)
94  {
95  std::string ldapFilter = "(" + Annotation::US_PROPKEY_ID + "=" + AnnotationID + ")";
96  us::ModuleContext *context = us::GetModuleContext();
97  std::vector<us::ServiceReference<mitk::Annotation>> annotations =
98  context->GetServiceReferences<mitk::Annotation>(ldapFilter);
99  Annotation *annotation = nullptr;
100  if (!annotations.empty())
101  {
102  annotation = us::GetModuleContext()->GetService<mitk::Annotation>(annotations.front());
103  }
104  return annotation;
105  }
106 
107  std::vector<AbstractAnnotationRenderer *> AnnotationUtils::GetAnnotationRenderer(const std::string &rendererID)
108  {
109  us::ModuleContext *context = us::GetModuleContext();
110 
111  // specify a filter that defines the requested type
112  std::string filter = "(&(" + AbstractAnnotationRenderer::US_PROPKEY_ID + "=*)(" +
113  AbstractAnnotationRenderer::US_PROPKEY_RENDERER_ID + "=" + rendererID + "))";
114  // find the fitting service
115  std::vector<us::ServiceReferenceU> serviceReferences =
116  context->GetServiceReferences(AbstractAnnotationRenderer::US_INTERFACE_NAME, filter);
117  std::vector<AbstractAnnotationRenderer *> arList;
118  for (us::ServiceReferenceU service : serviceReferences)
119  {
120  arList.push_back(context->GetService<AbstractAnnotationRenderer>(service));
121  }
122  return arList;
123  }
124 }
static void UpdateAnnotationRenderer(const std::string &rendererID)
UpdateAnnotationRenderer is a convenience function which calls AbstractAnnotationRenderer::Update for...
static BaseRenderer * GetInstance(vtkRenderWindow *renWin)
ServiceRegistrationU RegisterService(const InterfaceMap &service, const ServiceProperties &properties=ServiceProperties())
virtual T GetService(const ServiceReferenceType &reference) const
static const std::string US_PROPKEY_ID
Organizes the rendering process.
static mitk::Annotation * GetAnnotation(const std::string &AnnotationID)
GetAnnotation returns a registered Annotation for a specified ID.
DataCollection - Class to facilitate loading/accessing structured data.
static void RegisterAnnotationRenderer(AbstractAnnotationRenderer *annotationRenderer)
RegisterAnnotationRenderer registers an AnnotationRenderer as a microservice and saves a reference to...
void * GetService(const ServiceReferenceBase &reference)
static AbstractAnnotationRenderer * GetAnnotationRenderer(const std::string &arTypeID, const std::string &rendererID)
GetAnnotationRenderer returns a registered AnnotationRenderer of a specific type and for a BaseRender...
std::vector< std::unique_ptr< AbstractAnnotationRenderer > > AnnotationRendererServices
static void BaseRendererChanged(BaseRenderer *renderer)
BaseRendererChanged has to be called in the case that the actual BaseRenderer object for a BaseRender...
Baseclass of Annotation layouters An AbstractAnnotationRenderer can be implemented to control a set o...
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
Base class for all Annotation This class is to be implemented in order to create Annotation which are...
vtkRenderWindow * GetRenderWindow() const
Access the RenderWindow into which this renderer renders.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
const char * GetName() const
get the name of the Renderer