CustusX  22.04-rc5
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 
13 #include "cxToolTracer.h"
14 
15 #include "vtkImageData.h"
16 #include <vtkPointData.h>
17 #include <vtkUnsignedCharArray.h>
18 #include <vtkPolyData.h>
19 #include <vtkPolyDataMapper.h>
20 #include <vtkActor.h>
21 #include <vtkCellArray.h>
22 #include <vtkFloatArray.h>
23 #include <vtkProperty.h>
24 #include <QColor>
25 #include <vtkMatrix4x4.h>
26 
27 #include "cxTool.h"
28 #include "cxBoundingBox3D.h"
29 #include "cxVolumeHelpers.h"
30 #include "cxSpaceProvider.h"
31 #include "cxSpaceListener.h"
32 #include "cxLogger.h"
33 
34 namespace cx
35 {
36 
38 {
39  ToolTracerPtr retval(new ToolTracer(spaceProvider));
40  return retval;
41 }
42 
43 ToolTracer::ToolTracer(SpaceProviderPtr spaceProvider)
44 {
45  mSpaceProvider = spaceProvider;
46  mRunning = false;
47  mPolyData = vtkPolyDataPtr::New();
48  mActor = vtkActorPtr::New();
49  mPolyDataMapper = vtkPolyDataMapperPtr::New();
50 
51  mPolyDataMapper->SetInputData(mPolyData);
52  mActor->SetMapper(mPolyDataMapper);
53 
54  mProperty = vtkPropertyPtr::New();
55  mActor->SetProperty( mProperty );
56  mProperty->SetPointSize(4);
57 
58  this->setColor(QColor("red"));
59 
60  mPoints = vtkPointsPtr::New();
61  mLines = vtkCellArrayPtr::New();
62 
63  mPolyData->SetPoints(mPoints);
64  mPolyData->SetLines(mLines);
65  mPolyData->SetVerts(mLines);
66  mFirstPoint = false;
67  mMinDistance = -1.0;
68  mSkippedPoints = 0;
69 
70  mSpaceListener = mSpaceProvider->createListener();
71  mSpaceListener->setSpace(CoordinateSystem::patientReference());
72  connect(mSpaceListener.get(), &SpaceListener::changed, this, &ToolTracer::onSpaceChanged);
73  this->onSpaceChanged();
74 }
75 
77 {
78  if (mRunning)
79  return;
80  mRunning = true;
81  mFirstPoint = true;
82  mSkippedPoints = 0;
83  this->connectTool();
84 }
85 
87 {
88  if (!mRunning)
89  return;
90  this->disconnectTool();
91  mRunning = false;
92 }
93 
95 {
96  mPoints->Reset();
97  mLines->Reset();
98  mPolyData->Modified();
99 }
100 
101 void ToolTracer::connectTool()
102 {
103  if (mTool && mRunning)
104  {
105  connect(mTool.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(receiveTransforms(Transform3D, double)));
106  //connect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(receiveVisible(bool)));
107  }
108 }
109 
110 void ToolTracer::disconnectTool()
111 {
112  if (mTool && mRunning)
113  {
114  disconnect(mTool.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(receiveTransforms(Transform3D, double)));
115  //disconnect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(receiveVisible(bool)));
116  }
117 }
118 
119 void ToolTracer::setColor(QColor color)
120 {
121  mActor->GetProperty()->SetColor(color.redF(), color.greenF(), color.blueF());
122 }
123 
125 {
126  this->disconnectTool();
127  mTool = tool;
128  this->connectTool();
129 }
130 
132 {
133  return mPolyData;
134 }
135 
137 {
138  return mActor;
139 }
140 
141 void ToolTracer::onSpaceChanged()
142 {
143  Transform3D rMpr = mSpaceProvider->get_rMpr();
144 // std::cout << "rMpr ToolTracer: \n" << rMpr << std::endl;
145  mActor->SetUserMatrix(rMpr.getVtkMatrix());
146 }
147 
149 {
150  return mRunning;
151 }
152 
153 void ToolTracer::receiveTransforms(Transform3D prMt, double timestamp)
154 {
155  Vector3D p = prMt.coord(Vector3D(0,0,0));
156 
157  if (mMinDistance > 0.0)
158  {
159  if (!mFirstPoint && (mPreviousPoint - p).length() < mMinDistance)
160  {
161  ++mSkippedPoints;
162  return;
163  }
164  }
165  mFirstPoint = false;
166  mPreviousPoint = p;
167  mPoints->InsertNextPoint(p.begin());
168 
169  if (mPoints->GetNumberOfPoints() > 1)
170  {
171  // fill cell points for the entire polydata.
172  // This is very ineffective, but I have no idea how to update mLines incrementally.
173  mLines->Initialize();
174  std::vector<vtkIdType> ids(mPoints->GetNumberOfPoints());
175  for (unsigned i=0; i<ids.size(); ++i)
176  ids[i] = i;
177  mLines->InsertNextCell(ids.size(), &(*ids.begin()));
178  mLines->Modified();
179 
180  mPolyData->Modified();
181  }
182 }
183 
184 void ToolTracer::addManyPositions(TimedTransformMap trackerRecordedData_prMt)
185 {
186  for(TimedTransformMap::iterator iter=trackerRecordedData_prMt.begin(); iter!=trackerRecordedData_prMt.end(); ++iter)
187  {
188  double timestamp = iter->first;
189  Transform3D prMt = iter->second;
190  this->receiveTransforms(prMt, timestamp);
191  }
192 }
193 
194 
195 }
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:47
void addManyPositions(TimedTransformMap trackerRecordedData_prMt)
vtkPolyDataPtr getPolyData()
vtkActorPtr getActor()
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
RealScalar length() const
static CoordinateSystem patientReference()
bool isRunning() const
static ToolTracerPtr create(SpaceProviderPtr spaceProvider)
void setColor(QColor color)
std::map< double, Transform3D > TimedTransformMap
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr