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