Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkIGTLMessageSource.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 "mitkIGTLMessageSource.h"
18 #include "mitkUIDGenerator.h"
19 
20 //Microservices
21 #include <usGetModuleContext.h>
22 #include <usModule.h>
23 #include <usServiceProperties.h>
24 #include <usModuleContext.h>
25 
27  "org.mitk.services.IGTLMessageSource";
29  US_INTERFACE_NAME + ".devicename";
31  US_INTERFACE_NAME + ".devicetype";
33  US_INTERFACE_NAME + ".id";
35  US_INTERFACE_NAME + ".isActive";
36 
38  : itk::ProcessObject(), m_Name("IGTLMessageSource (no defined type)"),
39  m_Type("NONE"), m_StreamingFPS(0)
40 {
42 }
43 
45 {
46  //this->UnRegisterMicroservice();
47 }
48 
50 {
51  if (this->GetNumberOfIndexedOutputs() < 1)
52  {
53  MITK_WARN << "IGTLMessageSource contained no outputs. Returning nullptr.";
54  return nullptr;
55  }
56 
57  return static_cast<IGTLMessage*>(this->ProcessObject::GetPrimaryOutput());
58 }
59 
61  DataObjectPointerArraySizeType idx)
62 {
63  IGTLMessage* out =
64  dynamic_cast<IGTLMessage*>( this->ProcessObject::GetOutput(idx) );
65  if ( out == nullptr && this->ProcessObject::GetOutput(idx) != NULL )
66  {
67  itkWarningMacro (<< "Unable to convert output number " << idx << " to type "
68  << typeid( IGTLMessage ).name () );
69  }
70  return out;
71 }
72 
74  const std::string& messageName)
75 {
76  DataObjectPointerArray outputs = this->GetOutputs();
77  for (DataObjectPointerArray::iterator it = outputs.begin();
78  it != outputs.end();
79  ++it)
80  {
81  if (messageName ==
82  (static_cast<IGTLMessage*>(it->GetPointer()))->GetName())
83  {
84  return static_cast<IGTLMessage*>(it->GetPointer());
85  }
86  }
87  return nullptr;
88 }
89 
90 itk::ProcessObject::DataObjectPointerArraySizeType
91 mitk::IGTLMessageSource::GetOutputIndex( std::string messageName )
92 {
93  DataObjectPointerArray outputs = this->GetOutputs();
94  for (DataObjectPointerArray::size_type i = 0; i < outputs.size(); ++i)
95  {
96  if (messageName ==
97  (static_cast<IGTLMessage*>(outputs.at(i).GetPointer()))->GetName())
98  {
99  return i;
100  }
101  }
102  throw std::invalid_argument("output name does not exist");
103 }
104 
106 {
107  // Get Context
108  us::ModuleContext* context = us::GetModuleContext();
109 
110  // Define ServiceProps
111  us::ServiceProperties props;
112  mitk::UIDGenerator uidGen =
113  mitk::UIDGenerator ("org.mitk.services.IGTLMessageSource.id_", 16);
114  props[ US_PROPKEY_ID ] = uidGen.GetUID();
115  props[ US_PROPKEY_DEVICENAME ] = m_Name;
116  props[ US_PROPKEY_DEVICETYPE ] = m_Type;
117  m_ServiceRegistration = context->RegisterService(this, props);
118 }
119 
121 {
122  if (m_ServiceRegistration != nullptr)
123  m_ServiceRegistration.Unregister();
124  m_ServiceRegistration = 0;
125 }
126 
128 {
129  us::Any referenceProperty =
130  this->m_ServiceRegistration.GetReference().GetProperty(US_PROPKEY_ID);
131  return referenceProperty.ToString();
132 }
133 
134 void mitk::IGTLMessageSource::GraftOutput(itk::DataObject *graft)
135 {
136  this->GraftNthOutput(0, graft);
137 }
138 
140  itk::DataObject *graft)
141 {
142  if ( idx >= this->GetNumberOfIndexedOutputs() )
143  {
144  itkExceptionMacro(<<"Requested to graft output " << idx << " but this filter"
145  "only has " << this->GetNumberOfIndexedOutputs() << " Outputs.");
146  }
147 
148  if ( !graft )
149  {
150  itkExceptionMacro(<<"Requested to graft output with a NULL pointer object" );
151  }
152 
153  itk::DataObject* output = this->GetOutput(idx);
154  if ( !output )
155  {
156  itkExceptionMacro(<<"Requested to graft output that is a NULL pointer" );
157  }
158  // Call Graft on IGTLMessage to copy member data
159  output->Graft( graft );
160 }
161 
162 itk::DataObject::Pointer mitk::IGTLMessageSource::MakeOutput ( DataObjectPointerArraySizeType /*idx*/ )
163 {
164  return IGTLMessage::New().GetPointer();
165 }
166 
167 itk::DataObject::Pointer mitk::IGTLMessageSource::MakeOutput( const DataObjectIdentifierType & name )
168 {
169  itkDebugMacro("MakeOutput(" << name << ")");
170  if( this->IsIndexedOutputName(name) )
171  {
172  return this->MakeOutput( this->MakeIndexFromOutputName(name) );
173  }
174  return static_cast<itk::DataObject *>(IGTLMessage::New().GetPointer());
175 }
176 
178 {
180  // add properties to p like this:
181  //p->SetProperty("MyFilter_MyParameter", mitk::PropertyDataType::New(m_MyParameter));
183 }
184 
185 void mitk::IGTLMessageSource::SetFPS(unsigned int fps)
186 {
187  this->m_StreamingFPSMutex->Lock();
188  this->m_StreamingFPS = fps;
189  this->m_StreamingFPSMutex->Unlock();
190 }
191 
192 
194 {
195  unsigned int fps = 0;
196  this->m_StreamingFPSMutex->Lock();
197  fps = this->m_StreamingFPS;
198  this->m_StreamingFPSMutex->Unlock();
199  return fps;
200 }
virtual void RegisterAsMicroservice()
Registers this object as a Microservice, making it available to every module and/or plugin...
itk::SmartPointer< Self > Pointer
static Pointer New()
virtual void UnRegisterMicroservice()
Registers this object as a Microservice, making it available to every module and/or plugin...
Generated unique IDs.
static const std::string US_PROPKEY_ISACTIVE
virtual itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override
virtual void GraftNthOutput(unsigned int idx, itk::DataObject *graft)
Graft the specified DataObject onto this ProcessObject's output.
unsigned int GetFPS()
Gets the fps used for streaming this source.
IGTLMessage * GetOutput(void)
return the output (output with id 0) of the filter
#define MITK_WARN
Definition: mitkLogMacros.h:23
A wrapper for the OpenIGTLink message type.
static const std::string US_PROPKEY_DEVICETYPE
Definition: usAny.h:163
std::string GetMicroserviceID()
Returns the id that this device is registered with. The id will only be valid, if the IGTLMessageSour...
itk::SmartPointer< const Self > ConstPointer
virtual mitk::PropertyList::ConstPointer GetParameters() const
Get all filter parameters as a PropertyList.
itk::FastMutexLock::Pointer m_StreamingFPSMutex
static const std::string US_PROPKEY_ID
std::string ToString() const
Definition: usAny.h:257
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
static const std::string US_PROPKEY_DEVICENAME
static Pointer New()
virtual void GraftOutput(itk::DataObject *graft)
Graft the specified DataObject onto this ProcessObject's output.
static const std::string US_INTERFACE_NAME
These Constants are used in conjunction with Microservices.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
DataObjectPointerArraySizeType GetOutputIndex(std::string messageName)
return the index of the output with name messageName, -1 if no output with that name was found ...
void SetFPS(unsigned int fps)
Sets the fps used for streaming this source.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.