CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxToolTracer.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 
34 #include "cxToolTracer.h"
35 
36 #include "vtkImageData.h"
37 #include <vtkPointData.h>
38 #include <vtkUnsignedCharArray.h>
39 #include <vtkPolyData.h>
40 #include <vtkPolyDataMapper.h>
41 #include <vtkActor.h>
42 #include <vtkCellArray.h>
43 #include <vtkFloatArray.h>
44 #include <vtkProperty.h>
45 #include <QColor>
46 #include <vtkMatrix4x4.h>
47 
48 #include "cxTool.h"
49 #include "cxBoundingBox3D.h"
50 #include "cxVolumeHelpers.h"
51 #include "cxSpaceProvider.h"
52 
53 namespace cx
54 {
55 
57 {
58  ToolTracerPtr retval(new ToolTracer);
59  retval->mSpaceProvider = spaceProvider;
60  return retval;
61 }
62 
63 ToolTracer::ToolTracer()
64 {
65  mRunning = false;
66  mPolyData = vtkPolyDataPtr::New();
67  mActor = vtkActorPtr::New();
68  mPolyDataMapper = vtkPolyDataMapperPtr::New();
69 
70  mPolyDataMapper->SetInputData(mPolyData);
71  mActor->SetMapper(mPolyDataMapper);
72 
73  mProperty = vtkPropertyPtr::New();
74  mActor->SetProperty( mProperty );
75  mProperty->SetPointSize(4);
76 
77  this->setColor(QColor("red"));
78 
79  mPoints = vtkPointsPtr::New();
80  mLines = vtkCellArrayPtr::New();
81 
82  mPolyData->SetPoints(mPoints);
83  mPolyData->SetLines(mLines);
84  mPolyData->SetVerts(mLines);
85  mFirstPoint = false;
86  mMinDistance = -1.0;
87  mSkippedPoints = 0;
88 }
89 
91 {
92  if (mRunning)
93  return;
94  mRunning = true;
95  mFirstPoint = true;
96  mSkippedPoints = 0;
97  this->connectTool();
98 }
99 
101 {
102  if (!mRunning)
103  return;
104  this->disconnectTool();
105  mRunning = false;
106 }
107 
109 {
110  mPoints->Reset();
111  mLines->Reset();
112  mPolyData->Modified();
113 }
114 
115 void ToolTracer::connectTool()
116 {
117  if (mTool && mRunning)
118  {
119  connect(mTool.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(receiveTransforms(Transform3D, double)));
120  //connect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(receiveVisible(bool)));
121  }
122 }
123 
124 void ToolTracer::disconnectTool()
125 {
126  if (mTool && mRunning)
127  {
128  disconnect(mTool.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(receiveTransforms(Transform3D, double)));
129  //disconnect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(receiveVisible(bool)));
130  }
131 }
132 
133 void ToolTracer::setColor(QColor color)
134 {
135  mActor->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
136 }
137 
139 {
140  this->disconnectTool();
141  mTool = tool;
142  this->connectTool();
143 }
144 
146 {
147  return mPolyData;
148 }
149 
151 {
152  return mActor;
153 }
154 
156 {
157  return mRunning;
158 }
159 
160 void ToolTracer::receiveTransforms(Transform3D prMt, double timestamp)
161 {
162  Transform3D rMpr = mSpaceProvider->get_rMpr();
163  Transform3D rMt = rMpr * prMt;
164 
165  Vector3D p = rMt.coord(Vector3D(0,0,0));
166 
167  if (mMinDistance > 0.0)
168  {
169  if (!mFirstPoint && (mPreviousPoint - p).length() < mMinDistance)
170  {
171  ++mSkippedPoints;
172  return;
173  }
174  }
175  mFirstPoint = false;
176  mPreviousPoint = p;
177  mPoints->InsertNextPoint(p.begin());
178 
179  if (mPoints->GetNumberOfPoints() > 1)
180  {
181  // fill cell points for the entire polydata.
182  // This is very ineffective, but I have no idea how to update mLines incrementally.
183  mLines->Initialize();
184  std::vector<vtkIdType> ids(mPoints->GetNumberOfPoints());
185  for (unsigned i=0; i<ids.size(); ++i)
186  ids[i] = i;
187  mLines->InsertNextCell(ids.size(), &(*ids.begin()));
188 
189  mPolyData->Modified();
190  }
191 }
192 
193 void ToolTracer::addManyPositions(TimedTransformMap trackerRecordedData_prMt)
194 {
195  for(TimedTransformMap::iterator iter=trackerRecordedData_prMt.begin(); iter!=trackerRecordedData_prMt.end(); ++iter)
196  {
197  double timestamp = iter->first;
198  Transform3D prMt = iter->second;
199  this->receiveTransforms(prMt, timestamp);
200  }
201 }
202 
203 
204 }
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
vtkSmartPointer< class vtkActor > vtkActorPtr
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class ToolTracer > ToolTracerPtr
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
Definition: cxProbeSector.h:47
void setTool(ToolPtr tool)
3D Graphics class for displaying the trace path traversed by a tool.
Definition: cxToolTracer.h:68
void addManyPositions(TimedTransformMap trackerRecordedData_prMt)
vtkPolyDataPtr getPolyData()
cxLogicManager_EXPORT SpaceProviderPtr spaceProvider()
vtkActorPtr getActor()
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
RealScalar length() const
bool isRunning() const
static ToolTracerPtr create(SpaceProviderPtr spaceProvider)
void setColor(QColor color)
std::map< double, Transform3D > TimedTransformMap
boost::shared_ptr< class Tool > ToolPtr