Fraxinus  17.12-rc4
An IGT application
cxOpenIGTLinkTool.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxOpenIGTLinkTool.h"
34 
35 #include <vtkConeSource.h>
37 #include "cxLogger.h"
38 #include "cxProbeImpl.h"
39 
40 namespace cx
41 {
42 
44  ToolImpl(uid, uid),
45  mPolyData(NULL),
46  mTimestamp(0),
47  m_sMt_calibration(Transform3D::Identity())
48 {
49  connect(&mTpsTimer, SIGNAL(timeout()), this, SLOT(calculateTpsSlot()));
50 
51  mTypes = this->determineTypesBasedOnUid(Tool::mUid);
52  if (this->isProbe())
53  {
54  mProbe = ProbeImpl::New("DigitalProbe", "Digital");
55  connect(mProbe.get(), SIGNAL(sectorChanged()), this, SIGNAL(toolProbeSector()));
56  }
57 
58  this->createPolyData();
59  this->toolVisibleSlot(true);
60 }
61 
63 {
64 }
65 
66 std::set<Tool::Type> OpenIGTLinkTool::getTypes() const
67 {
68  return mTypes;
69 }
70 
72 {
73  return mPolyData;
74 }
75 
77 {
78  return mProbe;
79 }
80 
82 {
83  return mTimestamp;
84 }
85 
87 {
88  //TODO add some logic, visible if transform arrived in the last X seconds???
89  return true;
90 }
91 
93 {
94  //TODO when is a tool initialized? when it is connected to the tracker?
95  return true;
96 }
97 
98 QString OpenIGTLinkTool::getUid() const
99 {
100  return Tool::mUid;
101 }
102 
104 {
105  return Tool::mName;
106 }
107 
109 {
110  if(this->getProbe())
111  return this->getProbe()->getProbeDefinition().getDepthStart();
113 }
114 
116 {
117  if(this->getProbe())
118  return;
120 }
121 
122 std::set<Tool::Type> OpenIGTLinkTool::determineTypesBasedOnUid(const QString uid) const
123 {
124  std::set<Type> retval;
125  retval.insert(TOOL_POINTER);
126  if(uid.contains("probe", Qt::CaseInsensitive))
127  {
128  retval.insert(TOOL_US_PROBE);
129  }
130  return retval;
131 }
132 
133 bool OpenIGTLinkTool::isProbe() const
134 {
135  return (mTypes.find(TOOL_US_PROBE) != mTypes.end()) ? true : false;
136 }
137 
138 void OpenIGTLinkTool::createPolyData()
139 {
140  mPolyData = Tool::createDefaultPolyDataCone();
141 }
142 
144 {
145  Transform3D identity = Transform3D::Identity();
146  bool calibrated = !similar(m_sMt_calibration, identity);
147  CX_LOG_DEBUG() << "Checking if openiglink tool is calibratated: " << calibrated;
148 
149  return calibrated;
150 }
151 
153 {
154  return m_sMt_calibration;
155 }
156 
158 {
159  if(!similar(m_sMt_calibration, sMt))
160  {
161  m_sMt_calibration = sMt;
162  CX_LOG_INFO() << mName << " got an updated calibration";
163  }
164 }
165 
166 void OpenIGTLinkTool::toolTransformAndTimestampSlot(Transform3D prMs, double timestamp)
167 {
168  mTimestamp = timestamp;// /1000000;
169  Transform3D prMt = prMs * m_sMt_calibration;
170  Transform3D prMt_filtered = prMt;
171 
173  {
174  mTrackingPositionFilter->addPosition(prMt, mTimestamp);
175  prMt_filtered = mTrackingPositionFilter->getFilteredPosition();
176  }
177 
178  (*mPositionHistory)[mTimestamp] = prMt; // store original in history
179  m_prMt = prMt_filtered;
180  emit toolTransformAndTimestamp(m_prMt, mTimestamp);
181 }
182 
183 void OpenIGTLinkTool::calculateTpsSlot()
184 {
185  int tpsNr = 0;
186  size_t numberOfTransformsToCheck = ((mPositionHistory->size() >= 10) ? 10 : mPositionHistory->size());
187  if (numberOfTransformsToCheck <= 1)
188  {
189  emit tps(0);
190  return;
191  }
192 
193  TimedTransformMap::reverse_iterator rit = mPositionHistory->rbegin();
194  double lastTransform = rit->first;
195  for (int i = 0; i < numberOfTransformsToCheck-1; ++i)
196  {
197  ++rit;
198  }
199  double firstTransform = rit->first;
200  double secondsPassed = (lastTransform - firstTransform) / 1000;
201 
202  if (!similar(secondsPassed, 0))
203  tpsNr = (int) (numberOfTransformsToCheck / secondsPassed);
204  emit tps(tpsNr);
205 }
206 
207 void OpenIGTLinkTool::toolVisibleSlot(bool on)
208 {
209  if (on)
210  mTpsTimer.start(1000); //calculate tps every 1 seconds
211  else
212  mTpsTimer.stop();
213 }
214 
216 {
217  CX_LOG_WARNING() << "Cannot set visible on a openigtlink tool.";
218 }
219 
220 }//namespace cx
TimedTransformMapPtr mPositionHistory
Definition: cxToolImpl.h:75
TrackingPositionFilterPtr mTrackingPositionFilter
Definition: cxToolImpl.h:77
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Common functionality for Tool subclasses.
Definition: cxToolImpl.h:50
#define CX_LOG_INFO
Definition: cxLogger.h:117
void toolProbeSector()
virtual double getTooltipOffset() const
get a virtual offset extending from the tool tip.
Definition: cxToolImpl.cpp:65
void toolTransformAndTimestamp(Transform3D matrix, double timestamp)
QString mUid
Definition: cxTool.h:168
boost::shared_ptr< Probe > ProbePtr
Definition: cxProbe.h:93
QString mName
Definition: cxTool.h:169
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
void tps(int)
#define CX_LOG_DEBUG
Definition: cxLogger.h:116
static ProbeImplPtr New(QString instrumentUid, QString scannerUid, ProbeXmlConfigParserPtr xml=ProbeXmlConfigParserPtr())
Definition: cxProbeImpl.cpp:49
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
#define CX_LOG_WARNING
Definition: cxLogger.h:119
static vtkPolyDataPtr createDefaultPolyDataCone()
Definition: cxTool.cpp:41
virtual void setTooltipOffset(double val)
set a virtual offset extending from the tool tip.
Definition: cxToolImpl.cpp:70
Ultrasond probe. The tool has a Probe subinterface with a sector and a video stream.
Definition: cxTool.h:108
Navigation pointer. Pointing functionality such as tool offset.
Definition: cxTool.h:107
Transform3D m_prMt
the transform from the tool to the patient reference
Definition: cxToolImpl.h:76
Namespace for all CustusX production code.