CustusX  16.5.0-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 #define _USE_MATH_DEFINES
34 #include "cxToolUsingIGSTK.h"
35 
36 #include <vtkPolyData.h>
37 #include <vtkConeSource.h>
38 #include <vtkSTLReader.h>
39 #include <QDir>
40 #include <QDateTime>
41 #include <QStringList>
42 #include <QTextStream>
43 
44 #include "cxTypeConversions.h"
45 #include "cxProbeDefinition.h"
46 #include "cxProbeImpl.h"
47 #include "cxIgstkTool.h"
49 
50 namespace cx
51 {
52 
54  ToolImpl(""),
55  mTool(igstkTool), mPolyData(NULL),
56  mValid(false), mConfigured(false), mTracked(false)
57 {
58  mTimestamp = 0;
59  Tool::mUid = mTool->getInternalStructure().mUid;
60  Tool::mName = mTool->getInternalStructure().mName;
61  mValid = igstkTool->isValid();
62 
63  this->createPolyData();
64 
65  connect(mTool.get(), &IgstkTool::toolTransformAndTimestamp, this,
66  &ToolUsingIGSTK::toolTransformAndTimestampSlot);
67  connect(mTool.get(), SIGNAL(attachedToTracker(bool)), this, SIGNAL(attachedToTracker(bool)));
68  connect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(toolVisibleSlot(bool)));
69  connect(mTool.get(), SIGNAL(toolVisible(bool)), this, SIGNAL(toolVisible(bool)));
70  connect(&mTpsTimer, SIGNAL(timeout()), this, SLOT(calculateTpsSlot()));
71 
72  if (mTool->getInternalStructure().mIsProbe)
73  {
74  mProbe = ProbeImpl::New(mTool->getInternalStructure().mInstrumentId,
75  mTool->getInternalStructure().mInstrumentScannerId);
76  connect(mProbe.get(), SIGNAL(sectorChanged()), this, SIGNAL(toolProbeSector()));
77  }
78 }
79 
81 {
82 }
83 
84 std::set<ToolUsingIGSTK::Type> ToolUsingIGSTK::getTypes() const
85 {
86  std::set<Type> retval;
87 
88  if (mTool->getInternalStructure().mIsReference)
89  retval.insert(ToolUsingIGSTK::TOOL_REFERENCE);
90  if (mTool->getInternalStructure().mIsPointer)
91  retval.insert(ToolUsingIGSTK::TOOL_POINTER);
92  if (mTool->getInternalStructure().mIsProbe)
93  retval.insert(ToolUsingIGSTK::TOOL_US_PROBE);
94 
95  return retval;
96 }
97 
99 {
100  return mPolyData;
101 }
102 
104 {
105  return mProbe;
106 }
107 
109 {
110  return mTool->isVisible();
111 }
112 
114 {
115  return mTool->isInitialized();
116 }
117 
118 QString ToolUsingIGSTK::getUid() const
119 {
120  return Tool::mUid;
121 }
122 
123 QString ToolUsingIGSTK::getName() const
124 {
125  return Tool::mName;
126 }
127 
129 {
130  if(this->getProbe())
131  return this->getProbe()->getProbeDefinition().getDepthStart();
133 }
134 
136 {
137  if(this->getProbe())
138  return;
140 }
141 
143 {
144  return mValid;
145 }
146 
147 void ToolUsingIGSTK::createPolyData()
148 {
149  QDir dir;
150  if (!mTool->getInternalStructure().mGraphicsFileName.isEmpty()
151  && dir.exists(mTool->getInternalStructure().mGraphicsFileName))
152  {
153  vtkSTLReaderPtr reader = vtkSTLReaderPtr::New();
154  reader->SetFileName(cstring_cast(mTool->getInternalStructure().mGraphicsFileName));
155  reader->Update();
156  mPolyData = reader->GetOutput();
157  }
158  else
159  {
160  mPolyData = Tool::createDefaultPolyDataCone();
161  }
162 }
163 
165 {
166  Transform3D identity = Transform3D::Identity();
167  Transform3D sMt = mTool->getInternalStructure().getCalibrationAsSSC();
168  return !similar(sMt, identity);
169 }
170 
172 {
173  Transform3D sMt = mTool->getInternalStructure().getCalibrationAsSSC();
174 
175  return sMt;
176 }
177 
179 {
180  mTool->updateCalibration(calibration);
181 }
182 
184 {
185  return mTool->getInternalStructure().mCalibrationFilename;
186 }
187 
189 {
190  return mTool->getInternalStructure().mTrackerType;
191 }
192 
193 void ToolUsingIGSTK::printInternalStructure()
194 {
195  mTool->printInternalStructure();
196 }
197 
198 std::map<int, Vector3D> ToolUsingIGSTK::getReferencePoints() const
199 {
200  return mTool->getInternalStructure().mReferencePoints;
201 }
202 
204 {
205  return this->getReferencePoints().count(id);
206 }
207 
208 void ToolUsingIGSTK::addXml(QDomNode& dataNode)
209 {
210  QDomDocument doc = dataNode.ownerDocument();
211  dataNode.toElement().setAttribute("uid", qstring_cast(this->getUid()));
212  if (mProbe && mProbe->isValid())
213  {
214  QDomElement probeNode = doc.createElement("probe");
215  mProbe->addXml(probeNode);
216  dataNode.appendChild(probeNode);
217  }
218 }
219 
220 void ToolUsingIGSTK::parseXml(QDomNode& dataNode)
221 {
222  if (dataNode.isNull())
223  return;
224  if (mProbe)
225  {
226  QDomNode probeNode = dataNode.namedItem("probe");
227  mProbe->parseXml(probeNode);
228  }
229 }
230 
231 void ToolUsingIGSTK::toolTransformAndTimestampSlot(Transform3D matrix, double timestamp, ToolPositionMetadata metadata)
232 {
233  Transform3D prMt_filtered = matrix;
234 
236  {
237  mTrackingPositionFilter->addPosition(matrix, timestamp);
238  prMt_filtered = mTrackingPositionFilter->getFilteredPosition();
239  }
240 
241  mTimestamp = timestamp;
242  mMetadata[timestamp] = metadata;
243 
244  // Store positions in history, but only if visible - the history has no concept of visibility
245  if (this->getVisible())
246  (*mPositionHistory)[timestamp] = matrix;
247  m_prMt = prMt_filtered;
248  emit toolTransformAndTimestamp(m_prMt, timestamp);
249 
250 // ToolImpl::set_prMt(matrix, timestamp);
251 }
252 
253 void ToolUsingIGSTK::calculateTpsSlot()
254 {
255  int tpsNr = 0;
256 
257  int numberOfTransformsToCheck = ((mPositionHistory->size() >= 10) ? 10 : mPositionHistory->size());
258  if ( numberOfTransformsToCheck <= 1)
259  {
260  emit tps(0);
261  return;
262  }
263 
264  TimedTransformMap::reverse_iterator it = mPositionHistory->rbegin();
265  double lastTransform = it->first;
266  for (int i = 0; i < numberOfTransformsToCheck; ++i)
267  ++it;
268  double firstTransform = it->first;
269  double secondsPassed = (lastTransform - firstTransform) / 1000;
270 
271  if (!similar(secondsPassed, 0))
272  tpsNr = (int) (numberOfTransformsToCheck / secondsPassed);
273 
274  emit tps(tpsNr);
275 }
276 
277 void ToolUsingIGSTK::toolVisibleSlot(bool on)
278 {
279  if (on)
280  mTpsTimer.start(1000); //calculate tps every 1 seconds
281  else
282  mTpsTimer.stop();
283 }
284 
285 void ToolUsingIGSTK::set_prMt(const Transform3D& prMt, double timestamp)
286 {
287 
288 }
289 
291 {
292 
293 }
294 
295 
296 }//namespace cx
TimedTransformMapPtr mPositionHistory
Definition: cxToolImpl.h:75
QString qstring_cast(const T &val)
QString getCalibrationFileName() const
returns the path to the tools calibration file
TrackingPositionFilterPtr mTrackingPositionFilter
Definition: cxToolImpl.h:77
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:50
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
cstring_cast_Placeholder cstring_cast(const T &val)
void toolTransformAndTimestamp(Transform3D matrix, double timestamp)
void attachedToTracker(bool)
virtual QString getUid() const
virtual bool hasReferencePointWithId(int id)
Reference tool.
Definition: cxTool.h:105
QString mUid
Definition: cxTool.h:168
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
virtual vtkPolyDataPtr getGraphicsPolyData() const
get geometric 3D description
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:93
virtual bool isInitialized() const
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
void toolVisible(bool visible)
QString mName
Definition: cxTool.h:169
vtkSmartPointer< class vtkSTLReader > vtkSTLReaderPtr
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 std::map< int, Vector3D > getReferencePoints() const
Get the optional reference points from this tool.
virtual std::set< Type > getTypes() const
virtual QString getName() const
void tps(int)
static ProbeImplPtr New(QString instrumentUid, QString scannerUid, ProbeXmlConfigParserPtr xml=ProbeXmlConfigParserPtr())
Definition: cxProbeImpl.cpp:49
std::map< double, ToolPositionMetadata > mMetadata
Definition: cxToolImpl.h:78
virtual void parseXml(QDomNode &dataNode)
static vtkPolyDataPtr createDefaultPolyDataCone()
Definition: cxTool.cpp:41
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
Ultrasond probe. The tool has a Probe subinterface with a sector and a video stream.
Definition: cxTool.h:108
TRACKING_SYSTEM getTrackerType()
the type of tracker this tool belongs to
virtual void addXml(QDomNode &dataNode)
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