CustusX  22.04
An IGT application
cxToolUsingIGSTK.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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #define _USE_MATH_DEFINES
13 #include "cxToolUsingIGSTK.h"
14 
15 #include <QDateTime>
16 #include <QStringList>
17 #include <QTextStream>
18 
19 #include "cxTypeConversions.h"
20 #include "cxProbeDefinition.h"
21 #include "cxProbeImpl.h"
22 #include "cxIgstkTool.h"
24 
25 namespace cx
26 {
27 
29  ToolImpl(""),
30  mTool(igstkTool),
31  mValid(false), mConfigured(false), mTracked(false)
32 {
33  mTimestamp = 0;
36  mValid = igstkTool->isValid();
37 
38  this->createToolGraphic();
39 
40  connect(mTool.get(), &IgstkTool::toolTransformAndTimestamp, this,
41  &ToolUsingIGSTK::toolTransformAndTimestampSlot);
42  connect(mTool.get(), SIGNAL(attachedToTracker(bool)), this, SIGNAL(attachedToTracker(bool)));
43  connect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(toolVisibleSlot(bool)));
44  connect(mTool.get(), SIGNAL(toolVisible(bool)), this, SIGNAL(toolVisible(bool)));
45  connect(&mTpsTimer, SIGNAL(timeout()), this, SLOT(calculateTpsSlot()));
46 
47  if (getToolFileToolStructure()->mIsProbe)
48  {
49  mProbe = ProbeImpl::New(getToolFileToolStructure()->mInstrumentId,
50  getToolFileToolStructure()->mInstrumentScannerId);
51  connect(mProbe.get(), SIGNAL(sectorChanged()), this, SIGNAL(toolProbeSector()));
52  }
53 }
54 
56 {
57 }
58 
60 {
61  return mProbe;
62 }
63 
65 {
66  return mTool->isVisible();
67 }
68 
70 {
71  return mTool->isInitialized();
72 }
73 
74 QString ToolUsingIGSTK::getUid() const
75 {
76  return Tool::mUid;
77 }
78 
79 QString ToolUsingIGSTK::getName() const
80 {
81  return Tool::mName;
82 }
83 
85 {
86  if(this->getProbe())
87  return this->getProbe()->getProbeDefinition().getDepthStart();
89 }
90 
92 {
93  if(this->getProbe())
94  return;
96 }
97 
99 {
100  return mValid;
101 }
102 
104 {
105  Transform3D identity = Transform3D::Identity();
106  Transform3D sMt = getToolFileToolStructure()->getCalibrationAsSSC();
107  return !similar(sMt, identity);
108 }
109 
111 {
112  Transform3D sMt = getToolFileToolStructure()->getCalibrationAsSSC();
113 
114  return sMt;
115 }
116 
118 {
119  mTool->updateCalibration(calibration);
120 }
121 
123 {
124  return getToolFileToolStructure()->mCalibrationFilename;
125 }
126 
128 {
129  return getToolFileToolStructure()->mTrackerType;
130 }
131 
132 void ToolUsingIGSTK::printInternalStructure()
133 {
134  mTool->printInternalStructure();
135 }
136 
138 {
139  return mTool->getInternalStructure();
140 }
141 
142 void ToolUsingIGSTK::addXml(QDomNode& dataNode)
143 {
144  QDomDocument doc = dataNode.ownerDocument();
145  dataNode.toElement().setAttribute("uid", qstring_cast(this->getUid()));
146  if (mProbe && mProbe->isValid())
147  {
148  QDomElement probeNode = doc.createElement("probe");
149  mProbe->addXml(probeNode);
150  dataNode.appendChild(probeNode);
151  }
152 }
153 
154 void ToolUsingIGSTK::parseXml(QDomNode& dataNode)
155 {
156  if (dataNode.isNull())
157  return;
158  if (mProbe)
159  {
160  QDomNode probeNode = dataNode.namedItem("probe");
161  mProbe->parseXml(probeNode);
162  }
163 }
164 
165 void ToolUsingIGSTK::toolTransformAndTimestampSlot(Transform3D matrix, double timestamp, ToolPositionMetadata metadata)
166 {
167  Transform3D prMt_filtered = matrix;
168 
170  {
171  mTrackingPositionFilter->addPosition(matrix, timestamp);
172  prMt_filtered = mTrackingPositionFilter->getFilteredPosition();
173  }
174 
175  mTimestamp = timestamp;
176  mMetadata[timestamp] = metadata;
177 
178  // Store positions in history, but only if visible - the history has no concept of visibility
179  if (this->getVisible())
180  (*mPositionHistory)[timestamp] = matrix;
181  m_prMt = prMt_filtered;
182  emit toolTransformAndTimestamp(m_prMt, timestamp);
183 
184 // ToolImpl::set_prMt(matrix, timestamp);
185 }
186 
187 void ToolUsingIGSTK::calculateTpsSlot()
188 {
189  int tpsNr = 0;
190 
191  size_t numberOfTransformsToCheck = ((mPositionHistory->size() >= 10) ? 10 : mPositionHistory->size());
192  if ( numberOfTransformsToCheck <= 1)
193  {
194  emit tps(0);
195  return;
196  }
197 
198  TimedTransformMap::reverse_iterator it = mPositionHistory->rbegin();
199  double lastTransform = it->first;
200  for (size_t i = 0; i < numberOfTransformsToCheck; ++i)
201  ++it;
202  double firstTransform = it->first;
203  double secondsPassed = (lastTransform - firstTransform) / 1000;
204 
205  if (!similar(secondsPassed, 0))
206  tpsNr = int(numberOfTransformsToCheck / secondsPassed);
207 
208  emit tps(tpsNr);
209 }
210 
211 void ToolUsingIGSTK::toolVisibleSlot(bool on)
212 {
213  if (on)
214  mTpsTimer.start(1000); //calculate tps every 1 seconds
215  else
216  mTpsTimer.stop();
217 }
218 
219 void ToolUsingIGSTK::set_prMt(const Transform3D& prMt, double timestamp)
220 {
221  Q_UNUSED(prMt);
222  Q_UNUSED(timestamp);
223 }
224 
226 {
227  Q_UNUSED(vis);
228 }
229 
230 
231 }//namespace cx
TimedTransformMapPtr mPositionHistory
Definition: cxToolImpl.h:60
QString qstring_cast(const T &val)
boost::shared_ptr< ToolInternalStructure > ToolInternalStructurePtr
QString getCalibrationFileName() const
returns the path to the tools calibration file
TrackingPositionFilterPtr mTrackingPositionFilter
Definition: cxToolImpl.h:62
virtual void set_prMt(const Transform3D &prMt, double timestamp)
if available for this type, set pos, ts<0 means use current time
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
virtual void setVisible(bool vis)
if available for this type, set visibility
boost::shared_ptr< class IgstkTool > IgstkToolPtr
Common functionality for Tool subclasses.
Definition: cxToolImpl.h:30
virtual bool isCalibrated() const
true if calibration is different from identity
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)
void attachedToTracker(bool)
virtual QString getUid() const
QString mUid
Definition: cxTool.h:149
void toolTransformAndTimestamp(Transform3D matrix, double timestamp, ToolPositionMetadata metadata)
virtual void setCalibration_sMt(Transform3D calibration)
requests to use the calibration and replaces the tools calibration file
bool isValid() const
whether this tool is constructed correctly or not
boost::shared_ptr< Probe > ProbePtr
Definition: cxProbe.h:72
virtual bool isInitialized() const
void toolVisible(bool visible)
QString mName
Definition: cxTool.h:150
virtual Transform3D getCalibration_sMt() const
get the calibration transform from tool space to sensor space (where the spheres or similar live) ...
virtual ProbePtr getProbe() const
additional information if the tool represents an US Probe. Extends getProbeSector() ...
virtual void setTooltipOffset(double val)
set a virtual offset extending from the tool tip.
virtual double getTooltipOffset() const
get a virtual offset extending from the tool tip.
virtual QString getName() const
void tps(int)
virtual ToolFileParser::ToolInternalStructurePtr getToolFileToolStructure() const
static ProbeImplPtr New(QString instrumentUid, QString scannerUid, ProbeXmlConfigParserPtr xml=ProbeXmlConfigParserPtr())
Definition: cxProbeImpl.cpp:28
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
std::map< double, ToolPositionMetadata > mMetadata
Definition: cxToolImpl.h:63
virtual void parseXml(QDomNode &dataNode)
ToolUsingIGSTK(IgstkToolPtr igstkTool)
virtual void setTooltipOffset(double val)
set a virtual offset extending from the tool tip.
Definition: cxToolImpl.cpp:70
virtual bool getVisible() const
TRACKING_SYSTEM getTrackerType()
the type of tracker this tool belongs to
void createToolGraphic()
Definition: cxToolImpl.cpp:122
virtual void addXml(QDomNode &dataNode)
Transform3D m_prMt
the transform from the tool to the patient reference
Definition: cxToolImpl.h:61
Namespace for all CustusX production code.