19#include "mitkCallbackFromGUIThread.h"
22#include "usServiceReference.h"
23#include "usModuleContext.h"
24#include "usServiceEvent.h"
25#include "mitkServiceInterface.h"
26#include "usGetModuleContext.h"
29#include "igtlBindMessage.h"
30#include "igtlQuaternionTrackingDataMessage.h"
31#include "igtlTrackingDataMessage.h"
46 this->SetName(
"IGTLMessageProvider");
47 m_IsStreaming =
false;
50 this->m_StopStreamingCommand = ProviderCommand::New();
51 m_StopStreamingCommand->SetCallbackFunction(
this,
54 this->m_StreamingCommand = ProviderCommand::New();
55 m_StreamingCommand->SetCallbackFunction(
this,
61 this->InvokeEvent(StreamingStartRequiredEvent());
69 if (this->GetInput() !=
nullptr)
71 igtl::MessageBase::Pointer curMessage = this->GetInput()->GetMessage();
72 if (
dynamic_cast<igtl::TrackingDataMessage*
>(curMessage.GetPointer()) !=
nullptr)
74 igtl::TrackingDataMessage* tdMsg =
75 (igtl::TrackingDataMessage*)(curMessage.GetPointer());
76 igtl::TrackingDataElement::Pointer trackingData = igtl::TrackingDataElement::New();
77 tdMsg->GetTrackingDataElement(0, trackingData);
78 float x_pos, y_pos, z_pos;
79 trackingData->GetPosition(&x_pos, &y_pos, &z_pos);
86 if (this->m_IGTLDevice.IsNull())
89 for (
unsigned int index = 0; index < this->GetNumberOfIndexedInputs(); index++)
91 mitk::IGTLMessage::Pointer msg =
const_cast<mitk::IGTLMessage*
>(this->GetInput(index));
97 if ( !msg->IsDataValid() )
102 this->m_IGTLDevice->SendMessage(msg);
109 if (this->GetNumberOfOutputs() > 0)
111 for (
int numOP = this->GetNumberOfOutputs() - 1; numOP >= 0; numOP--)
112 this->RemoveOutput(numOP);
117 if (m_IGTLDevice.IsNull())
120 this->SetNumberOfIndexedOutputs(1);
121 if (this->GetOutput(0) ==
nullptr)
123 DataObjectPointer newOutput = this->MakeOutput(0);
124 this->SetNthOutput(0, newOutput);
146 MITK_DEBUG(
"IGTLMessageProvider") <<
"lost connection, stop streaming";
147 this->StopStreamingOfAllSources();
153 return requestType.substr(4);
159 igtl::MessageBase::Pointer curCommand = this->m_IGTLDevice->GetNextCommand();
161 const char * requestType = curCommand->GetDeviceType();
163 std::string reqType(requestType);
164 bool isGetMsg = !reqType.find(
"GET_");
165 bool isSTTMsg = !reqType.find(
"STT_");
166 bool isSTPMsg = !reqType.find(
"STP_");
171 mitk::IGTLMessageSource::Pointer source = this->GetFittingSource(type.c_str());
174 if ( source.IsNull() )
176 if ( !this->GetIGTLDevice()->SendRTSMessage(type.c_str()) )
180 MITK_WARN(
"IGTLMessageProvider") <<
"Tried to send a RTS message but did "
181 "not succeed. Check if this type ( "
182 << type <<
" ) was added to the message "
194 mitk::IGTLMessage::Pointer sourceOutput = source->GetOutput();
195 if (sourceOutput.IsNotNull() && sourceOutput->IsDataValid())
197 if ( source.IsNotNull() )
199 this->GetIGTLDevice()->SendMessage(sourceOutput);
209 igtl::MessageBase* curCommandPt = curCommand.GetPointer();
210 if ( std::strcmp( curCommand->GetDeviceType(),
"STT_BIND" ) == 0 )
212 fps = ((igtl::StartBindMessage*)curCommandPt)->GetResolution();
214 else if ( std::strcmp( curCommand->GetDeviceType(),
"STT_QTDATA" ) == 0 )
216 fps = ((igtl::StartQuaternionTrackingDataMessage*)curCommandPt)->GetResolution();
218 else if ( std::strcmp( curCommand->GetDeviceType(),
"STT_TDATA" ) == 0 )
220 fps = ((igtl::StartTrackingDataMessage*)curCommandPt)->GetResolution();
223 this->StartStreamingOfSource(source, fps);
227 this->StopStreamingOfSource(source);
238 return m_IsStreaming;
244 if ( src ==
nullptr )
249 if ( !m_IsStreaming )
253 this->ConnectTo(src);
256 this->m_StreamingTimeMutex.lock();
257 this->m_StreamingTime = 1.0 / (double) fps * 1000.0;
258 this->m_StreamingTimeMutex.unlock();
267 mitk::CallbackFromGUIThread::GetInstance()->CallThisFromGUIThread(
268 this->m_StreamingCommand);
270 this->m_IsStreaming =
true;
274 MITK_WARN(
"IGTLMessageProvider") <<
"This provider just supports the "
275 "streaming of one source.";
281 this->InvokeEvent(StreamingStartRequiredEvent());
286 this->InvokeEvent(StreamingStopRequiredEvent());
293 this->DisconnectFrom(src);
295 mitk::CallbackFromGUIThread::GetInstance()->CallThisFromGUIThread(
296 this->m_StopStreamingCommand);
299 this->m_IsStreaming =
false;
306 mitk::CallbackFromGUIThread::GetInstance()->CallThisFromGUIThread(
307 this->m_StopStreamingCommand);
310 this->m_IsStreaming =
false;
316 us::ModuleContext* context = us::GetModuleContext();
321 "=" + requestedType +
")";
323 std::vector<us::ServiceReferenceU> serviceReferences =
324 context->GetServiceReferences(interface, filter);
328 if ( serviceReferences.size() )
330 mitk::IGTLMessageSource::Pointer curSource =
333 if ( curSource.IsNotNull() )
344 MITK_INFO <<
"Sending OpenIGTLink Message: " << msg->ToString();
345 this->m_IGTLDevice->SendMessage(msg);
352 for (DataObjectPointerArraySizeType i = 0;
353 i < UpstreamFilter->GetNumberOfOutputs(); i++)
355 this->SetInput(i, UpstreamFilter->
GetOutput(i));
362 if (UpstreamFilter ==
nullptr)
365 for (DataObjectPointerArraySizeType i = 0; i < UpstreamFilter->GetNumberOfOutputs(); ++i)
367 auto input = UpstreamFilter->
GetOutput(i);
369 if (input ==
nullptr)
372 auto nb = this->GetNumberOfIndexedInputs();
374 for (DataObjectPointerArraySizeType i = 0; i < nb; ++i)
376 if (this->GetInput(i) == input)
378 this->RemoveInput(i);
Connects a mitk::IGTLDevice to a MITK-OpenIGTLink-Message-Filter-Pipeline.
~IGTLMessageProvider() override
mitk::IGTLMessageSource::Pointer GetFittingSource(const char *requestedType)
Looks for microservices that provide messages with the requested type.
void StartStreamingOfSource(mitk::IGTLMessageSource *src, unsigned int fps)
Starts the streaming of the given message source with the given fps.
void CreateOutputs()
Create the necessary outputs for the m_IGTLDevice.
void OnIncomingCommand() override
This method is called when the IGTL device hold by this class receives a new command.
bool IsStreaming()
Returns the streaming state.
void StopStreamingOfSource(mitk::IGTLMessageSource *src)
Stops the streaming of the given message source.
void ConnectTo(mitk::IGTLMessageSource *UpstreamFilter)
Connects the input of this filter to the outputs of the given IGTLMessageSource.
void InvokeStartStreamingEvent()
void Send(mitk::IGTLMessage::Pointer msg)
sends the msg to the requesting client
void OnIncomingMessage() override
This method is called when the IGTL device hold by this class receives a new message.
void DisconnectFrom(mitk::IGTLMessageSource *UpstreamFilter)
Disconnects this filter from the outputs of the given IGTLMessageSource.
void OnLostConnection() override
This method is called when the IGTL device lost the connection to the other side.
void InvokeStopStreamingEvent()
void GenerateData() override
filter execute method
void StopStreamingOfAllSources()
Stops the streaming of all message source.
OpenIGTLink message source.
static const std::string US_INTERFACE_NAME
These Constants are used in conjunction with Microservices.
static const std::string US_PROPKEY_DEVICETYPE
IGTLMessage * GetOutput(void)
return the output (output with id 0) of the filter
A wrapper for the OpenIGTLink message type.
std::string RemoveRequestPrefixes(std::string requestType)
itkEventMacroDefinition(MessageSentEvent, itk::AnyEvent)