CustusX  15.8
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 "cxProbeData.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(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this,
66  SLOT(toolTransformAndTimestampSlot(Transform3D, double)));
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()->getProbeData().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  vtkConeSourcePtr coneSource = vtkConeSourcePtr::New();
161  coneSource->SetResolution(25);
162  coneSource->SetRadius(10);
163  coneSource->SetHeight(100);
164 
165  coneSource->SetDirection(0, 0, 1);
166  double newCenter[3];
167  coneSource->GetCenter(newCenter);
168  newCenter[2] = newCenter[2] - coneSource->GetHeight() / 2;
169  coneSource->SetCenter(newCenter);
170 
171  coneSource->Update();
172  mPolyData = coneSource->GetOutput();
173  }
174 }
175 
177 {
178  Transform3D identity = Transform3D::Identity();
179  Transform3D sMt = mTool->getInternalStructure().getCalibrationAsSSC();
180  return !similar(sMt, identity);
181 }
182 
184 {
185  Transform3D sMt = mTool->getInternalStructure().getCalibrationAsSSC();
186 
187  return sMt;
188 }
189 
191 {
192  mTool->updateCalibration(calibration);
193 }
194 
196 {
197  return mTool->getInternalStructure().mCalibrationFilename;
198 }
199 
201 {
202  return mTool->getInternalStructure().mTrackerType;
203 }
204 
205 void ToolUsingIGSTK::printInternalStructure()
206 {
207  mTool->printInternalStructure();
208 }
209 
210 std::map<int, Vector3D> ToolUsingIGSTK::getReferencePoints() const
211 {
212  return mTool->getInternalStructure().mReferencePoints;
213 }
214 
216 {
217  return this->getReferencePoints().count(id);
218 }
219 
220 void ToolUsingIGSTK::addXml(QDomNode& dataNode)
221 {
222  QDomDocument doc = dataNode.ownerDocument();
223  dataNode.toElement().setAttribute("uid", qstring_cast(this->getUid()));
224  if (mProbe && mProbe->isValid())
225  {
226  QDomElement probeNode = doc.createElement("probe");
227  mProbe->addXml(probeNode);
228  dataNode.appendChild(probeNode);
229  }
230 }
231 
232 void ToolUsingIGSTK::parseXml(QDomNode& dataNode)
233 {
234  if (dataNode.isNull())
235  return;
236  if (mProbe)
237  {
238  QDomNode probeNode = dataNode.namedItem("probe");
239  mProbe->parseXml(probeNode);
240  }
241 }
242 
243 void ToolUsingIGSTK::toolTransformAndTimestampSlot(Transform3D matrix, double timestamp)
244 {
245  Transform3D prMt_filtered = matrix;
246 
248  {
249  mTrackingPositionFilter->addPosition(matrix, timestamp);
250  prMt_filtered = mTrackingPositionFilter->getFilteredPosition();
251  }
252 
253  mTimestamp = timestamp;
254  (*mPositionHistory)[timestamp] = matrix; // store original in history
255  m_prMt = prMt_filtered;
256  emit toolTransformAndTimestamp(m_prMt, timestamp);
257 
258 // ToolImpl::set_prMt(matrix, timestamp);
259 }
260 
261 void ToolUsingIGSTK::calculateTpsSlot()
262 {
263  int tpsNr = 0;
264 
265  int numberOfTransformsToCheck = ((mPositionHistory->size() >= 10) ? 10 : mPositionHistory->size());
266  if ( numberOfTransformsToCheck <= 1)
267  {
268  emit tps(0);
269  return;
270  }
271 
272  TimedTransformMap::reverse_iterator it = mPositionHistory->rbegin();
273  double lastTransform = it->first;
274  for (int i = 0; i < numberOfTransformsToCheck; ++i)
275  ++it;
276  double firstTransform = it->first;
277  double secondsPassed = (lastTransform - firstTransform) / 1000;
278 
279  if (!similar(secondsPassed, 0))
280  tpsNr = (int) (numberOfTransformsToCheck / secondsPassed);
281 
282  emit tps(tpsNr);
283 }
284 
285 void ToolUsingIGSTK::toolVisibleSlot(bool on)
286 {
287  if (on)
288  mTpsTimer.start(1000); //calculate tps every 1 seconds
289  else
290  mTpsTimer.stop();
291 }
292 
293 void ToolUsingIGSTK::set_prMt(const Transform3D& prMt, double timestamp)
294 {
295 
296 }
297 
299 {
300 
301 }
302 
303 
304 }//namespace cx
TimedTransformMapPtr mPositionHistory
Definition: cxToolImpl.h:69
QString qstring_cast(const T &val)
QString getCalibrationFileName() const
returns the path to the tools calibration file
TrackingPositionFilterPtr mTrackingPositionFilter
Definition: cxToolImpl.h:71
boost::shared_ptr< IgstkTool > IgstkToolPtr
Definition: cxIgstkTool.h:60
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
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:52
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)
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
Definition: cxProbeSector.h:47
Reference tool.
Definition: cxTool.h:90
QString mUid
Definition: cxTool.h:149
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
virtual vtkPolyDataPtr getGraphicsPolyData() const
get geometric 3D description
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
void toolVisible(bool visible)
QString mName
Definition: cxTool.h:150
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)
vtkSmartPointer< class vtkConeSource > vtkConeSourcePtr
static ProbeImplPtr New(QString instrumentUid, QString scannerUid, ProbeXmlConfigParserPtr xml=ProbeXmlConfigParserPtr())
Definition: cxProbeImpl.cpp:49
void parseXml(QDomNode &dataNode)
ToolUsingIGSTK(IgstkToolPtr igstkTool)
virtual void setTooltipOffset(double val)
set a virtual offset extending from the tool tip.
Definition: cxToolImpl.cpp:57
virtual bool getVisible() const
Ultrasond probe. The tool has a Probe subinterface with a sector and a video stream.
Definition: cxTool.h:93
TRACKING_SYSTEM getTrackerType()
the type of tracker this tool belongs to
void addXml(QDomNode &dataNode)
Navigation pointer. Pointing functionality such as tool offset.
Definition: cxTool.h:92
Transform3D m_prMt
the transform from the tool to the patient reference
Definition: cxToolImpl.h:70