Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkNavigationDataToIGTLMessageFilter.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 
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 
37 {
38 }
39 
41 {
42  switch (m_OperationMode)
43  {
44  case ModeSendQTDataMsg:
46  break;
47  case ModeSendTDataMsg:
49  break;
50  case ModeSendQTransMsg:
52  break;
53  case ModeSendTransMsg:
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));
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));
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 
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
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 = 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 
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 = 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 }
NavigationData * GetOutput(void)
return the output (output with id 0) of the filter
#define MITK_INFO
Definition: mitkLogMacros.h:18
void SetMessage(igtl::MessageBase::Pointer msg)
Sets the OpenIGTLink message.
itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override
Navigation Data.
mitk::AffineTransform3D::Pointer GetAffineTransform3D() const
Calculate AffineTransform3D from the transformation held by this NavigationData. TODO: should throw a...
unsigned int m_CurrentTimeStep
Indicates the current timestamp.
igtl::TimeStamp::Pointer ConvertToIGTLTimeStamp(double IGTTimeStamp)
virtual const char * GetName() const
returns the name of the NavigationData object
virtual void GenerateDataModeSendQTDataMsg()
Generates the output.
Constants for most interaction classes, due to the generic StateMachines.
mitk::Quaternion OrientationType
Type that holds the orientation part of the tracking data.
Time stamp in milliseconds.
virtual void SetType(std::string _arg)
void ConvertAffineTransformationIntoIGTLMatrix(mitk::AffineTransform3D *trans, igtl::Matrix4x4 igtlTransform)
virtual void GenerateDataModeSendQTransMsg()
Generates the output for ModeSendQTransMsg.
virtual PositionType GetPosition() const
returns position of the NavigationData object
virtual void ConnectTo(mitk::NavigationDataSource *UpstreamFilter)
Connects the input of this filter to the outputs of the given NavigationDataSource.
virtual TimeStampType GetIGTTimeStamp() const
gets the IGT timestamp of the NavigationData object in milliseconds Please note, that there is also t...
IGTLMessage * GetOutput(void)
return the output (output with id 0) of the filter
virtual void CreateOutputsForAllInputs()
create output objects according to OperationMode for all inputs
A wrapper for the OpenIGTLink message type.
OperationMode m_OperationMode
Stores the mode. See enum OperationMode.
virtual void SetInput(const mitk::NavigationData *NavigationData)
Sets one input NavigationData.
virtual void GenerateDataModeSendTransMsg()
Generates the output for ModeSendTransMsg.
itk::AffineGeometryFrame< ScalarType, 3 >::TransformType AffineTransform3D
const mitk::NavigationData * GetInput()
Returns the input of this filter.
virtual void GenerateDataModeSendTDataMsg()
Generates the output for ModeSendTDataMsg.
static Pointer New()
virtual OrientationType GetOrientation() const
returns the orientation of the NavigationData object
virtual bool IsDataValid() const
returns true if the object contains valid data
virtual void SetOperationMode(OperationMode mode)
Sets the mode of this filter.
OperationMode
There are four different operation modes.