MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkNavigationDataToIGTLMessageFilter.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14
15#include "igtlQuaternionTrackingDataMessage.h"
16#include "igtlTrackingDataMessage.h"
17#include "igtlTransformMessage.h"
18#include "igtlPositionMessage.h"
19
20#include <mitkInteractionConst.h>
21#include <itksys/SystemTools.hxx>
22
24{
25 mitk::IGTLMessage::Pointer output = mitk::IGTLMessage::New();
26 this->SetNumberOfRequiredOutputs(1);
27 this->SetNthOutput(0, output.GetPointer());
28 this->SetNumberOfRequiredInputs(1);
29
30 // m_OperationMode = Mode3D;
32 // m_RingBufferSize = 50; //the default ring buffer size
33 // m_NumberForMean = 100;
34}
35
39
41{
42 switch (m_OperationMode)
43 {
44 case ModeSendQTDataMsg:
45 this->GenerateDataModeSendQTDataMsg();
46 break;
47 case ModeSendTDataMsg:
48 this->GenerateDataModeSendTDataMsg();
49 break;
50 case ModeSendQTransMsg:
51 this->GenerateDataModeSendQTransMsg();
52 break;
53 case ModeSendTransMsg:
54 this->GenerateDataModeSendTransMsg();
55 break;
56 default:
57 break;
58 }
59}
60
62{
63 // Process object is not const-correct so the const_cast is required here
64 this->ProcessObject::SetNthInput(0, const_cast<NavigationData*>(nd));
65 this->CreateOutputsForAllInputs();
66}
67
69{
70 // Process object is not const-correct so the const_cast is required here
71 this->ProcessObject::SetNthInput(idx, const_cast<NavigationData*>(nd));
72 this->CreateOutputsForAllInputs();
73}
74
76{
77 if (this->GetNumberOfInputs() < 1)
78 return nullptr;
79 return static_cast<const NavigationData*>(this->ProcessObject::GetInput(0));
80}
81
83{
84 if (this->GetNumberOfInputs() < 1)
85 return nullptr;
86 return static_cast<const NavigationData*>(this->ProcessObject::GetInput(idx));
87}
88
90{
91 switch (m_OperationMode)
92 {
93 case ModeSendQTDataMsg:
94 // create one message output for all navigation data inputs
95 this->SetNumberOfIndexedOutputs(1);
96 // set the type for this filter
97 this->SetType("QTDATA");
98 break;
99 case ModeSendTDataMsg:
100 // create one message output for all navigation data inputs
101 this->SetNumberOfIndexedOutputs(1);
102 // set the type for this filter
103 this->SetType("TDATA");
104 break;
105 case ModeSendQTransMsg:
106 // create one message output for all navigation data input together
107 this->SetNumberOfIndexedOutputs(1);
108 // set the type for this filter
109 this->SetType("POSITION");
110 break;
111 case ModeSendTransMsg:
112 // create one message output for each navigation data input
113 this->SetNumberOfIndexedOutputs(this->GetNumberOfIndexedInputs());
114 // set the type for this filter
115 this->SetType("TRANS");
116 break;
117 default:
118 break;
119 }
120
121 for (unsigned int idx = 0; idx < this->GetNumberOfIndexedOutputs(); ++idx)
122 {
123 if (this->GetOutput(idx) == nullptr)
124 {
125 DataObjectPointer newOutput = this->MakeOutput(idx);
126 this->SetNthOutput(idx, newOutput);
127 }
128 this->Modified();
129 }
130}
131
132void ConvertAffineTransformationIntoIGTLMatrix(mitk::AffineTransform3D* trans,
133 igtl::Matrix4x4 igtlTransform)
134{
135 const mitk::AffineTransform3D::MatrixType& matrix = trans->GetMatrix();
136 mitk::Vector3D position = trans->GetOffset();
137 //copy the data into a matrix type that igtl understands
138 for (unsigned int r = 0; r < 3; r++)
139 {
140 for (unsigned int c = 0; c < 3; c++)
141 {
142 igtlTransform[r][c] = matrix(r, c);
143 }
144 igtlTransform[r][3] = position[r];
145 }
146 for (unsigned int c = 0; c < 3; c++)
147 {
148 igtlTransform[3][c] = 0.0;
149 }
150 igtlTransform[3][3] = 1.0;
151}
152
154{
155 // for each output message
156 for (unsigned int i = 0; i < this->GetNumberOfIndexedInputs(); ++i)
157 {
158 mitk::IGTLMessage* output = this->GetOutput(i);
159 assert(output);
160 const mitk::NavigationData* input = this->GetInput(i);
161 assert(input);
162 // do not add navigation data to message if input is invalid
163 if (input->IsDataValid() == false)
164 continue;
165
166 //get the navigation data components
167 mitk::NavigationData::PositionType pos = input->GetPosition();
168 mitk::NavigationData::OrientationType ori = input->GetOrientation();
169
170 //insert this information into the message
171 igtl::PositionMessage::Pointer posMsg = igtl::PositionMessage::New();
172 posMsg->SetPosition(pos[0], pos[1], pos[2]);
173 posMsg->SetQuaternion(ori[0], ori[1], ori[2], ori[3]);
174 igtl::TimeStamp::Pointer timestamp = ConvertToIGTLTimeStamp(input->GetIGTTimeStamp());
175 posMsg->SetTimeStamp(timestamp);
176 posMsg->SetDeviceName(input->GetName());
177 posMsg->Pack();
178
179 //add the igtl message to the mitk::IGTLMessage
180 output->SetMessage(posMsg.GetPointer());
181 }
182}
183
185{
186 // for each output message
187 for (unsigned int i = 0; i < this->GetNumberOfIndexedInputs(); ++i)
188 {
189 mitk::IGTLMessage* output = this->GetOutput(i);
190 assert(output);
191 const mitk::NavigationData* input = this->GetInput(i);
192 assert(input);
193 // do not add navigation data to message if input is invalid
194 if (input->IsDataValid() == false)
195 continue;
196
197 //get the navigation data components
198 mitk::AffineTransform3D::Pointer transform = input->GetAffineTransform3D();
199 mitk::NavigationData::PositionType position = Point3D(transform->GetOffset());
200
201 //convert the transform into a igtl type
202 igtl::Matrix4x4 igtlTransform;
203 ConvertAffineTransformationIntoIGTLMatrix(transform, igtlTransform);
204
205 //insert this information into the message
206 igtl::TransformMessage::Pointer transMsg = igtl::TransformMessage::New();
207 transMsg->SetMatrix(igtlTransform);
208 transMsg->SetPosition(position[0], position[1], position[2]);
209 igtl::TimeStamp::Pointer timestamp = ConvertToIGTLTimeStamp(input->GetIGTTimeStamp());
210 transMsg->SetTimeStamp(timestamp);
211 transMsg->SetDeviceName(input->GetName());
212 transMsg->Pack();
213
214 //add the igtl message to the mitk::IGTLMessage
215 output->SetMessage(transMsg.GetPointer());
216 }
217}
219{
220 igtl::TimeStamp::Pointer timestamp = igtl::TimeStamp::New();
221 timestamp->SetTime(IGTTimeStamp / 1000, (int)(IGTTimeStamp) % 1000);
222 return timestamp;
223}
224
226{
227 mitk::IGTLMessage* output = this->GetOutput();
228 assert(output);
229
230 //create a output igtl message
231 igtl::QuaternionTrackingDataMessage::Pointer qtdMsg =
232 igtl::QuaternionTrackingDataMessage::New();
233
236
237 for (unsigned int index = 0; index < this->GetNumberOfIndexedInputs(); index++)
238 {
239 const mitk::NavigationData* nd = GetInput(index);
240 assert(nd);
241
242 //get the navigation data components
243 pos = nd->GetPosition();
244 ori = nd->GetOrientation();
245
246 //insert the information into the tracking element
247 igtl::QuaternionTrackingDataElement::Pointer tde =
248 igtl::QuaternionTrackingDataElement::New();
249 tde->SetPosition(pos[0], pos[1], pos[2]);
250 tde->SetQuaternion(ori[0], ori[1], ori[2], ori[3]);
251 tde->SetName(nd->GetName());
252
253 //insert this element into the tracking data message
254 qtdMsg->AddQuaternionTrackingDataElement(tde);
255
256 MITK_INFO << ConvertToIGTLTimeStamp(nd->GetIGTTimeStamp());
257 }
258 qtdMsg->Pack();
259
260 //add the igtl message to the mitk::IGTLMessage
261 output->SetMessage(qtdMsg.GetPointer());
262}
263
265{
266 igtl::TrackingDataMessage::Pointer tdMsg = igtl::TrackingDataMessage::New();
267 mitk::IGTLMessage* output = this->GetOutput(0);
268 assert(output);
269
270 // for each output message
271 for (unsigned int i = 0; i < this->GetNumberOfIndexedInputs(); ++i)
272 {
273 const mitk::NavigationData* input = this->GetInput(i);
274 assert(input);
275 // do not add navigation data to message if input is invalid
276 if (input->IsDataValid() == false)
277 continue;
278
279 //get the navigation data components
280 mitk::AffineTransform3D::Pointer transform = input->GetAffineTransform3D();
281 mitk::NavigationData::PositionType position = Point3D(transform->GetOffset());
282
283 //convert the transform into a igtl type
284 igtl::Matrix4x4 igtlTransform;
285 ConvertAffineTransformationIntoIGTLMatrix(transform, igtlTransform);
286
287 //insert this information into the message
288 igtl::TrackingDataElement::Pointer tde = igtl::TrackingDataElement::New();
289 tde->SetMatrix(igtlTransform);
290 tde->SetPosition(position[0], position[1], position[2]);
291 tde->SetName(input->GetName());
292 tde->SetType(igtl::TrackingDataElement::TYPE_6D);
293 tdMsg->AddTrackingDataElement(tde);
294 }
295
296 //use time stamp from first input
297 igtl::TimeStamp::Pointer timestamp = ConvertToIGTLTimeStamp(this->GetInput(0)->GetIGTTimeStamp());
298 tdMsg->SetTimeStamp(timestamp);
299 //tdMsg->SetDeviceName("MITK OpenIGTLink Connection");
300 tdMsg->Pack();
301 tdMsg->SetDeviceName("MITK OpenIGTLink Source");
302 output->SetMessage(tdMsg.GetPointer());
303}
304
306{
307 m_OperationMode = mode;
308 this->Modified();
309}
310
312 mitk::NavigationDataSource* UpstreamFilter)
313{
314 for (DataObjectPointerArraySizeType i = 0;
315 i < UpstreamFilter->GetNumberOfOutputs(); i++)
316 {
317 this->SetInput(i, UpstreamFilter->GetOutput(i));
318 }
319}
A wrapper for the OpenIGTLink message type.
void SetMessage(igtl::MessageBase::Pointer msg)
Sets the OpenIGTLink message.
Time stamp in milliseconds.
NavigationData * GetOutput(void)
return the output (output with id 0) of the filter
igtl::TimeStamp::Pointer ConvertToIGTLTimeStamp(double IGTTimeStamp)
unsigned int m_CurrentTimeStep
Indicates the current timestamp.
const mitk::NavigationData * GetInput()
Returns the input of this filter.
virtual void CreateOutputsForAllInputs()
create output objects according to OperationMode for all inputs
virtual void GenerateDataModeSendTransMsg()
Generates the output for ModeSendTransMsg.
virtual void ConnectTo(mitk::NavigationDataSource *UpstreamFilter)
Connects the input of this filter to the outputs of the given NavigationDataSource.
virtual void SetInput(const mitk::NavigationData *NavigationData)
Sets one input NavigationData.
virtual void GenerateDataModeSendTDataMsg()
Generates the output for ModeSendTDataMsg.
OperationMode
There are four different operation modes.
virtual void SetOperationMode(OperationMode mode)
Sets the mode of this filter.
virtual void GenerateDataModeSendQTransMsg()
Generates the output for ModeSendQTransMsg.
mitk::AffineTransform3D::Pointer GetAffineTransform3D() const
Calculate AffineTransform3D from the transformation held by this NavigationData. TODO: should throw a...
mitk::Quaternion OrientationType
Type that holds the orientation part of the tracking data.
virtual bool IsDataValid() const
returns true if the object contains valid data
mitk::Point3D PositionType
Type that holds the position part of the tracking data.
void ConvertAffineTransformationIntoIGTLMatrix(mitk::AffineTransform3D *trans, igtl::Matrix4x4 igtlTransform)