Medical Imaging Interaction Toolkit  2018.4.99-1640525a
Medical Imaging Interaction Toolkit
mitkTrackingTool.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 
13 #include "mitkTrackingTool.h"
14 #include <itkMutexLockHolder.h>
15 
17 
19 : itk::Object(),
20  m_ToolName(""),
21  m_ErrorMessage(""),
22  m_IGTTimeStamp(0),
23  m_MyMutex(itk::FastMutexLock::New()),
24  m_TrackingError(0.0f),
25  m_Enabled(true),
26  m_DataValid(false),
27  m_ToolTipSet(false)
28 {
29  m_Position[0] = 0.0f;
30  m_Position[1] = 0.0f;
31  m_Position[2] = 0.0f;
32  m_Orientation[0] = 0.0f;
33  m_Orientation[1] = 0.0f;
34  m_Orientation[2] = 0.0f;
35  m_Orientation[3] = 0.0f;
36  // this should not be necessary as the tools bring their own tooltip transformation
37  m_ToolTipPosition[0] = 0.0f;
38  m_ToolTipPosition[1] = 0.0f;
39  m_ToolTipPosition[2] = 0.0f;
40  m_ToolAxisOrientation[0] = 0.0f;
41  m_ToolAxisOrientation[1] = 0.0f;
42  m_ToolAxisOrientation[2] = 0.0f;
43  m_ToolAxisOrientation[3] = 1.0f;
44 }
45 
47 {
48  m_MyMutex->Unlock();
49  m_MyMutex = nullptr;
50 }
51 
52 void mitk::TrackingTool::PrintSelf(std::ostream& os, itk::Indent indent) const
53 {
54  Superclass::PrintSelf(os, indent);
55  os << indent << "ToolName: " << m_ToolName << std::endl;
56  os << indent << "ErrorMesage: " << m_ErrorMessage << std::endl;
57  os << indent << "Position: " << m_Position << std::endl;
58  os << indent << "Orientation: " << m_Orientation << std::endl;
59  os << indent << "TrackingError: " << m_TrackingError << std::endl;
60  os << indent << "Enabled: " << m_Enabled << std::endl;
61  os << indent << "DataValid: " << m_DataValid << std::endl;
62  os << indent << "ToolTip: " << m_ToolTipPosition << std::endl;
63  os << indent << "ToolTipRotation: " << m_ToolAxisOrientation << std::endl;
64  os << indent << "ToolTipSet: " << m_ToolTipSet << std::endl;
65 }
66 
68 {
69  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
70  return this->m_ToolName.c_str();
71 }
72 
73 void mitk::TrackingTool::SetToolName(const char* _arg)
74 {
75  itkDebugMacro("setting m_ToolName to " << _arg);
76  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
77  if ( _arg && (_arg == this->m_ToolName) )
78  {
79  return;
80  }
81  if (_arg)
82  {
83  this->m_ToolName= _arg;
84  }
85  else
86  {
87  this->m_ToolName= "";
88  }
89  this->Modified();
90 }
91 
92 
93 void mitk::TrackingTool::SetToolName( const std::string _arg )
94 {
95  this->SetToolName(_arg.c_str());
96 }
97 
99 {
100  MutexLockHolder lock(*m_MyMutex);
101  return m_ToolTipPosition;
102 }
103 
105 {
106  MutexLockHolder lock(*m_MyMutex);
107  return m_ToolAxisOrientation;
108 }
109 
111  mitk::Quaternion orientation,
113 {
114  if ( !Equal(m_ToolTipPosition, toolTipPosition, eps) ||
115  !Equal(m_ToolAxisOrientation, orientation, eps) )
116  {
117  if( (toolTipPosition[0] == 0) &&
118  (toolTipPosition[1] == 0) &&
119  (toolTipPosition[2] == 0) &&
120  (orientation.x() == 0) &&
121  (orientation.y() == 0) &&
122  (orientation.z() == 0) &&
123  (orientation.r() == 1))
124  {
125  m_ToolTipSet = false;
126  }
127  else
128  {
129  m_ToolTipSet = true;
130  }
131  m_ToolTipPosition = toolTipPosition;
132  m_ToolAxisOrientation = orientation;
133  this->Modified();
134  }
135 }
136 
138 {
139  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
140  return m_ToolTipSet;
141 }
142 
144 {
145  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
146  if (m_ToolTipSet)
147  {
148  // Compute the position of tool tip in the coordinate frame of the
149  // tracking device: Rotate the position of the tip into the tracking
150  // device coordinate frame then add to the position of the tracking
151  // sensor
152  vnl_vector<mitk::ScalarType> pos_vnl = m_Position.GetVnlVector() + m_Orientation.rotate( m_ToolTipPosition.GetVnlVector() ) ;
153 
154  position[0] = pos_vnl[0];
155  position[1] = pos_vnl[1];
156  position[2] = pos_vnl[2];
157  }
158  else
159  {
160  position[0] = m_Position[0];
161  position[1] = m_Position[1];
162  position[2] = m_Position[2];
163  }
164  this->Modified();
165 }
166 
168 {
169  itkDebugMacro("setting m_Position to " << position);
170 
171  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
172  if (m_Position != position)
173  {
174  m_Position = position;
175  this->Modified();
176  }
177 }
178 
180 {
181  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
182  if (m_ToolTipSet)
183  {
184  // Compute the orientation of the tool tip in the coordinate frame of
185  // the tracking device.
186  //
187  // * m_Orientation is the orientation of the sensor relative to the transmitter
188  // * m_ToolAxisOrientation is the orientation of the tool tip relative to the sensor
189  orientation = m_Orientation * m_ToolAxisOrientation;
190  }
191  else
192  {
193  orientation = m_Orientation;
194  }
195 }
196 
198 {
199  itkDebugMacro("setting m_Orientation to " << orientation);
200 
201  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
202  if (m_Orientation != orientation)
203  {
204  m_Orientation = orientation;
205  this->Modified();
206  }
207 }
208 
210 {
211  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
212  if (m_Enabled == false)
213  {
214  this->m_Enabled = true;
215  this->Modified();
216  }
217  return true;
218 }
219 
221 {
222  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
223  if (m_Enabled == true)
224  {
225  this->m_Enabled = false;
226  this->Modified();
227  }
228  return true;
229 }
230 
232 {
233  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
234  return m_Enabled;
235 }
236 
237 void mitk::TrackingTool::SetDataValid(bool isDataValid)
238 {
239  itkDebugMacro("setting m_DataValid to " << isDataValid);
240  if (this->m_DataValid != isDataValid)
241  {
242  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
243  this->m_DataValid = isDataValid;
244  this->Modified();
245  }
246 }
247 
249 {
250  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
251  return m_DataValid;
252 }
253 
255 {
256  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
257  return m_TrackingError;
258 }
259 
261 {
262  itkDebugMacro("setting m_TrackingError to " << error);
263  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
264  if (m_TrackingError != error)
265  {
266  m_TrackingError = error;
267  this->Modified();
268  }
269 }
270 
272 {
273  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
274  return this->m_ErrorMessage.c_str();
275 }
276 
278 {
279  itkDebugMacro("setting m_ErrorMessage to " << _arg);
280  MutexLockHolder lock(*m_MyMutex); // lock and unlock the mutex
281  if ((_arg == nullptr) || (_arg == this->m_ErrorMessage))
282  return;
283 
284  if (_arg != nullptr)
285  this->m_ErrorMessage = _arg;
286  else
287  this->m_ErrorMessage = "";
288  this->Modified();
289 }
virtual void SetErrorMessage(const char *_arg)
sets the error message
bool m_DataValid
if true, data in m_Position and m_Orientation is valid, e.g. true tracking data
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Quaternion m_Orientation
holds the orientation of the tool´in global tracking coordinates
double ScalarType
std::string m_ErrorMessage
if a tool is invalid, this member should contain a human readable explanation of why it is invalid ...
virtual float GetTrackingError() const
returns one value that corresponds to the overall tracking error.
itk::FastMutexLock::Pointer m_MyMutex
mutex to control concurrent access to the tool
Quaternion m_ToolAxisOrientation
holds the rotation of the sensor coordinate system such that the z-axis coincides with the main tool ...
Point3D m_Position
holds the position of the tool in global tracking coordinates
virtual void SetOrientation(Quaternion orientation)
sets the orientation as a quaternion
virtual void SetPosition(Point3D position)
sets the position
virtual void GetOrientation(Quaternion &orientation) const
returns the current orientation of the tool as a quaternion in a mitk::Point4D (in the tracking devic...
virtual void SetToolTipPosition(Point3D toolTipPosition, Quaternion orientation, ScalarType eps=0.0)
defines a tool tip for this tool in tool coordinates. GetPosition() and GetOrientation() return the d...
virtual bool Disable()
disables the tool, so that it will not be tracked anymore
bool m_Enabled
if true, tool is enabled and should receive tracking updates from the tracking device ...
Point3D m_ToolTipPosition
holds the position of the tool tip in the coordinate system of the tracking sensor ...
Quaternion GetToolAxisOrientation() const
returns the transformation of the tool axis with respect to the MITK-IGT main tool axis (0...
itk::MutexLockHolder< itk::FastMutexLock > MutexLockHolder
float m_TrackingError
holds the tracking error of the tool
vnl_quaternion< ScalarType > Quaternion
virtual bool IsDataValid() const
returns true if the current position data is valid (no error during tracking, tracking error below th...
Point3D GetToolTipPosition() const
returns the tool tip in tool coordinates, which where set by SetToolTip
virtual const char * GetErrorMessage() const
if the data is not valid, ErrorMessage should contain a string explaining why it is invalid (the Set-...
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
virtual bool IsEnabled() const
returns whether the tool is enabled or disabled
std::string m_ToolName
every tool has a name that can be used to identify it.
virtual bool IsToolTipSet() const
returns true if a tool tip is set, false if not
MITKCORE_EXPORT const ScalarType eps
virtual void SetDataValid(bool isDataValid)
sets if the tracking data (position & orientation) is valid
virtual void SetToolName(const std::string _arg)
Sets the name of the tool.
virtual void GetPosition(Point3D &position) const
returns the current position of the tool as an array of three floats (in the tracking device coordina...
virtual const char * GetToolName() const
every tool has a name thatgit can be used to identify it.
virtual bool Enable()
enables the tool, so that it will be tracked
virtual void SetTrackingError(float error)
sets the tracking error