32 #include <itkCommand.h>
40 this->
SetName(
"IGTLDeviceSource (no defined type)");
45 if (m_IGTLDevice.IsNotNull())
49 this->StopCommunication();
55 this->RemoveObservers();
62 if (m_IGTLDevice.IsNull())
69 if (msgIn.IsNotNull())
74 msgOut->
SetName(msgIn->GetDeviceName());
84 if (this->m_IGTLDevice.IsNotNull())
86 this->m_IGTLDevice->RemoveObserver(m_IncomingMessageObserverTag);
87 this->m_IGTLDevice->RemoveObserver(m_IncomingCommandObserverTag);
88 this->m_IGTLDevice->RemoveObserver(m_LostConnectionObserverTag);
94 MITK_DEBUG <<
"Setting IGTLDevice to " << igtlDevice;
95 if (this->m_IGTLDevice.GetPointer() != igtlDevice)
98 if (this->m_IGTLDevice.IsNotNull())
101 this->RemoveObservers();
104 this->m_IGTLDevice = igtlDevice;
105 this->CreateOutputs();
106 std::stringstream name;
107 name <<
"OIGTL Device Source ( " << igtlDevice->GetName() <<
" )";
108 this->SetName(name.str());
111 typedef itk::SimpleMemberCommand<IGTLDeviceSource> DeviceSrcCommand;
115 this->m_IncomingMessageObserverTag =
116 this->m_IGTLDevice->AddObserver(mitk::MessageReceivedEvent(), msgReceivedCommand);
120 this->m_IncomingCommandObserverTag =
121 this->m_IGTLDevice->AddObserver(mitk::CommandReceivedEvent(), cmdReceivedCommand);
125 this->m_LostConnectionObserverTag =
126 this->m_IGTLDevice->AddObserver(mitk::LostConnectionEvent(), connectionLostCommand);
133 if (this->GetNumberOfOutputs() > 0)
135 for (
int numOP = this->GetNumberOfOutputs() - 1; numOP >= 0; numOP--)
136 this->RemoveOutput(numOP);
141 if (m_IGTLDevice.IsNull())
144 this->SetNumberOfIndexedOutputs(1);
145 if (this->GetOutput(0) == NULL)
147 DataObjectPointer newOutput = this->MakeOutput(0);
148 this->SetNthOutput(0, newOutput);
155 if (m_IGTLDevice.IsNull())
157 throw std::invalid_argument(
"mitk::IGTLDeviceSource: "
158 "No OpenIGTLink device set");
160 if (this->IsConnected())
166 m_IGTLDevice->OpenConnection();
170 throw std::runtime_error(std::string(
"mitk::IGTLDeviceSource: Could not open"
171 "connection to OpenIGTLink device. Error: ") + e.GetDescription());
177 if (m_IGTLDevice.IsNull())
178 throw std::invalid_argument(
"mitk::IGTLDeviceSource: "
179 "No OpenIGTLink device set");
182 if (m_IGTLDevice->StartCommunication() ==
false)
183 throw std::runtime_error(
"mitk::IGTLDeviceSource: "
184 "Could not start communication");
189 if (m_IGTLDevice.IsNull())
190 throw std::invalid_argument(
"mitk::IGTLDeviceSource: "
191 "No OpenIGTLink device set");
192 if (m_IGTLDevice->CloseConnection() ==
false)
193 throw std::runtime_error(
"mitk::IGTLDeviceSource: Could not close connection"
194 " to OpenIGTLink device");
199 if (m_IGTLDevice.IsNull())
200 throw std::invalid_argument(
"mitk::IGTLDeviceSource: "
201 "No OpenIGTLink device set");
202 if (m_IGTLDevice->StopCommunication() ==
false)
203 throw std::runtime_error(
"mitk::IGTLDeviceSource: "
204 "Could not stop communicating");
210 Superclass::UpdateOutputInformation();
217 this->RemoveInput(idx);
222 this->ProcessObject::SetNthInput(idx, const_cast<IGTLMessage*>(msg));
229 if (m_IGTLDevice.IsNull())
238 if (m_IGTLDevice.IsNull())
253 props[US_PROPKEY_ID] = uidGen.
GetUID();
254 props[US_PROPKEY_DEVICENAME] = this->
GetName();
255 props[US_PROPKEY_IGTLDEVICENAME] = m_Name;
256 props[US_PROPKEY_DEVICETYPE] = m_Type;
257 m_ServiceRegistration = context->RegisterService(
this, props);
259 MITK_INFO <<
"Registered new DeviceSource as microservice: " << uidGen.
GetUID();
276 if (this->GetNumberOfInputs() < 1)
279 return static_cast<const IGTLMessage*
>(this->ProcessObject::GetInput(0));
285 if (this->GetNumberOfInputs() < 1)
288 return static_cast<const IGTLMessage*
>(this->ProcessObject::GetInput(idx));
294 const DataObjectPointerArray& inputs =
const_cast<Self*
>(
this)->GetInputs();
295 for (DataObjectPointerArray::const_iterator it = inputs.begin();
296 it != inputs.end(); ++it)
297 if (std::string(msgName) ==
298 (
static_cast<IGTLMessage*
>(it->GetPointer()))->GetName())
299 return static_cast<IGTLMessage*>(it->GetPointer());
303 itk::ProcessObject::DataObjectPointerArraySizeType
306 DataObjectPointerArray outputs = this->GetInputs();
307 for (DataObjectPointerArray::size_type i = 0; i < outputs.size(); ++i)
309 (static_cast<IGTLMessage*>(outputs.at(i).GetPointer()))->GetName())
311 throw std::invalid_argument(
"output name does not exist");
virtual bool IsCommunicating()
returns true if communication is in progress
itk::SmartPointer< Self > Pointer
virtual void OnIncomingCommand()
This method is called when the IGTL device hold by this class receives a new command.
static const std::string US_PROPKEY_IGTLDEVICENAME
These Constants are used in conjunction with Microservices.
void SetMessage(igtl::MessageBase::Pointer msg)
Sets the OpenIGTLink message.
void StopCommunication()
stops the communication of the device.
virtual void OnLostConnection()
This method is called when the IGTL device lost the connection to the other side. ...
void Disconnect()
Closes the connection to the OpenIGTLink device.
DataCollection - Class to facilitate loading/accessing structured data.
virtual void UpdateOutputInformation() override
Used for pipeline update.
virtual void GenerateData() override
filter execute method
virtual void SetIGTLDevice(mitk::IGTLDevice *td)
sets the OpenIGTLink device that will be used as a data source
virtual void SetName(const char *_arg)
set the name of the IGTLMessage object
void CreateOutputs()
Create the necessary outputs for the m_IGTLDevice.
virtual ~IGTLDeviceSource()
A wrapper for the OpenIGTLink message type.
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
virtual void RegisterAsMicroservice() override
Registers this object as a Microservice, making it available to every module and/or plugin...
OpenIGTLink message source.
void StartCommunication()
starts the communication of the device. This needs to be called before Update() or GetOutput()->Updat...
virtual void OnIncomingMessage()
This method is called when the IGTL device hold by this class receives a new message.
virtual void RemoveObservers()
Removes all observers that listen to the igtl device.
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
virtual void SetInput(unsigned int idx, const IGTLMessage *msg)
Set input with id idx of this filter.
Interface for all OpenIGTLink Devices.
void Connect()
Establishes a connection to the OpenIGTLink device. If there is already a connection the method does ...
virtual void SetName(std::string _arg)
Sets the human readable name of this source. There is also a default name, but you can use this metho...
virtual bool IsConnected()
returns true if a connection to the OpenIGTLink device is established
static std::string GetName(std::string fileName, std::string suffix)
static const std::string US_INTERFACE_NAME
These Constants are used in conjunction with Microservices.
const IGTLMessage * GetInput(void) const
Get the input of this filter.
DataObjectPointerArraySizeType GetInputIndex(std::string msgName)
return the index of the input with name msgName, throw std::invalid_argument exception if that name w...
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.