Fraxinus  16.5.0-fx-rc1
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 #include "cxSpaceListener.h"
53 
54 namespace cx
55 {
56 
58 {
59  ToolTracerPtr retval(new ToolTracer(spaceProvider));
60  return retval;
61 }
62 
63 ToolTracer::ToolTracer(SpaceProviderPtr spaceProvider)
64 {
65  mSpaceProvider = spaceProvider;
66  mRunning = false;
67  mPolyData = vtkPolyDataPtr::New();
68  mActor = vtkActorPtr::New();
69  mPolyDataMapper = vtkPolyDataMapperPtr::New();
70 
71  mPolyDataMapper->SetInputData(mPolyData);
72  mActor->SetMapper(mPolyDataMapper);
73 
74  mProperty = vtkPropertyPtr::New();
75  mActor->SetProperty( mProperty );
76  mProperty->SetPointSize(4);
77 
78  this->setColor(QColor("red"));
79 
80  mPoints = vtkPointsPtr::New();
81  mLines = vtkCellArrayPtr::New();
82 
83  mPolyData->SetPoints(mPoints);
84  mPolyData->SetLines(mLines);
85  mPolyData->SetVerts(mLines);
86  mFirstPoint = false;
87  mMinDistance = -1.0;
88  mSkippedPoints = 0;
89 
90  mSpaceListener = mSpaceProvider->createListener();
91  mSpaceListener->setSpace(CoordinateSystem::patientReference());
92  connect(mSpaceListener.get(), &SpaceListener::changed, this, &ToolTracer::onSpaceChanged);
93  this->onSpaceChanged();
94 }
95 
97 {
98  if (mRunning)
99  return;
100  mRunning = true;
101  mFirstPoint = true;
102  mSkippedPoints = 0;
103  this->connectTool();
104 }
105 
107 {
108  if (!mRunning)
109  return;
110  this->disconnectTool();
111  mRunning = false;
112 }
113 
115 {
116  mPoints->Reset();
117  mLines->Reset();
118  mPolyData->Modified();
119 }
120 
121 void ToolTracer::connectTool()
122 {
123  if (mTool && mRunning)
124  {
125  connect(mTool.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(receiveTransforms(Transform3D, double)));
126  //connect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(receiveVisible(bool)));
127  }
128 }
129 
130 void ToolTracer::disconnectTool()
131 {
132  if (mTool && mRunning)
133  {
134  disconnect(mTool.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(receiveTransforms(Transform3D, double)));
135  //disconnect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(receiveVisible(bool)));
136  }
137 }
138 
139 void ToolTracer::setColor(QColor color)
140 {
141  mActor->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
142 }
143 
145 {
146  this->disconnectTool();
147  mTool = tool;
148  this->connectTool();
149 }
150 
152 {
153  return mPolyData;
154 }
155 
157 {
158  return mActor;
159 }
160 
161 void ToolTracer::onSpaceChanged()
162 {
163  Transform3D rMpr = mSpaceProvider->get_rMpr();
164 // std::cout << "rMpr ToolTracer: \n" << rMpr << std::endl;
165  mActor->SetUserMatrix(rMpr.getVtkMatrix());
166 }
167 
169 {
170  return mRunning;
171 }
172 
173 void ToolTracer::receiveTransforms(Transform3D prMt, double timestamp)
174 {
175  Transform3D rMpr = Transform3D::Identity(); // handle rMpr in actor
176 // Transform3D rMpr = mSpaceProvider->get_rMpr();
177  Transform3D rMt = rMpr * prMt;
178 
179  Vector3D p = rMt.coord(Vector3D(0,0,0));
180 
181  if (mMinDistance > 0.0)
182  {
183  if (!mFirstPoint && (mPreviousPoint - p).length() < mMinDistance)
184  {
185  ++mSkippedPoints;
186  return;
187  }
188  }
189  mFirstPoint = false;
190  mPreviousPoint = p;
191  mPoints->InsertNextPoint(p.begin());
192 
193  if (mPoints->GetNumberOfPoints() > 1)
194  {
195  // fill cell points for the entire polydata.
196  // This is very ineffective, but I have no idea how to update mLines incrementally.
197  mLines->Initialize();
198  std::vector<vtkIdType> ids(mPoints->GetNumberOfPoints());
199  for (unsigned i=0; i<ids.size(); ++i)
200  ids[i] = i;
201  mLines->InsertNextCell(ids.size(), &(*ids.begin()));
202 
203  mPolyData->Modified();
204  }
205 }
206 
207 void ToolTracer::addManyPositions(TimedTransformMap trackerRecordedData_prMt)
208 {
209  for(TimedTransformMap::iterator iter=trackerRecordedData_prMt.begin(); iter!=trackerRecordedData_prMt.end(); ++iter)
210  {
211  double timestamp = iter->first;
212  Transform3D prMt = iter->second;
213  this->receiveTransforms(prMt, timestamp);
214  }
215 }
216 
217 
218 }
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
void setTool(ToolPtr tool)
3D Graphics class for displaying the trace path traversed by a tool.
Definition: cxToolTracer.h:68
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
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
static CoordinateSystem patientReference()
bool isRunning() const
static ToolTracerPtr create(SpaceProviderPtr spaceProvider)
void setColor(QColor color)
std::map< double, Transform3D > TimedTransformMap
boost::shared_ptr< class Tool > ToolPtr