CustusX  18.04-rc1
An IGT application
cxToolRep3D.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 "cxToolRep3D.h"
14 
15 #include "boost/bind.hpp"
16 #include <vtkActor.h>
17 #include <vtkProperty.h>
18 #include <vtkPolyDataMapper.h>
19 #include <vtkPolyDataNormals.h>
20 #include <vtkSTLReader.h>
21 #include <vtkMatrix4x4.h>
22 #include <vtkRenderer.h>
23 
24 #include "cxGraphicalPrimitives.h"
25 #include "cxProbe.h"
26 #include "cxView.h"
27 #include "cxVideoSourceGraphics.h"
28 #include "cxTypeConversions.h"
29 #include "cxToolTracer.h"
30 #include "cxTool.h"
31 #include "cxSpaceProvider.h"
32 #include "cxSettings.h"
33 
34 namespace cx
35 {
36 
38  RepImpl(),
39  mSpaceProvider(spaceProvider),
40  mSphereRadiusInNormalizedViewport(false),
41  mStipplePattern(0xFFFF)
42 {
43  mTooltipPointColor = settings()->value("View/toolTipPointColor").value<QColor>();
44  mOffsetPointColor = settings()->value("View/toolOffsetPointColor").value<QColor>();
45  mOffsetLineColor = settings()->value("View/toolOffsetLineColor").value<QColor>();
46 
47  mSphereRadius = 2;
48  mStayHiddenAfterVisible = false;
49  mStayVisibleAfterHide = false;
50  mOffsetPointVisibleAtZeroOffset = false;
51  mToolActor = vtkActorPtr::New();
52  mPolyDataMapper = vtkPolyDataMapperPtr::New();
53 // mSTLReader = vtkSTLReaderPtr::New();
54 
55  mOffsetPoint.reset(new GraphicalPoint3D());
56  mOffsetLine.reset(new GraphicalLine3D());
57  mTooltipPoint.reset(new GraphicalPoint3D());
58 
59  mProbeSector.reset(new ProbeSector());
60  mProbeSectorPolyDataMapper = vtkPolyDataMapperPtr::New();
61  mProbeSectorActor = vtkActorPtr::New();
62 
63  mTracer = ToolTracer::create(mSpaceProvider);
64 }
65 
67 {}
68 
70 {
71  return mTracer;
72 }
73 
74 ToolRep3DPtr ToolRep3D::New(SpaceProviderPtr spaceProvider, const QString& uid)
75 {
76  return wrap_new(new ToolRep3D(spaceProvider), uid);
77 }
78 
79 QString ToolRep3D::getType() const
80 {
81  return "ToolRep3D";
82 }
83 
85 {
86  return mTool;
87 }
88 
89 void ToolRep3D::updateToolActor()
90 {
91  if (!mTool)
92  {
93  mToolActor->SetMapper(NULL);
94  return;
95  }
96 
97  vtkPolyDataPtr model = mTool->getGraphicsPolyData();
98 
99  if (model)
100  {
101  vtkPolyDataNormalsPtr normals = vtkPolyDataNormalsPtr::New();
102  normals->SetInputData(model);
103  mPolyDataMapper->SetInputConnection(normals->GetOutputPort());
104  mPolyDataMapper->Update();
105  mToolActor->SetMapper(mPolyDataMapper);
106  }
107 
108  //some color to 3D cursor
109  mToolActor->GetProperty()->SetColor(1.0, 1.0, 1.0);
110  if (mTool->hasType(Tool::TOOL_MANUAL))
111  {
112  setColorAndOpacity(mToolActor->GetProperty(), mTooltipPointColor);
113  }
114 
115  this->setModified();
116  mToolActor->SetVisibility(mTool->getVisible());
117 }
118 
120 {
121  if (tool == mTool)
122  return;
123 
124  mTracer->setTool(tool);
125 
126  if (mTool)
127  {
128  disconnect(mTool.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(setModified()));
129  disconnect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(receiveVisible(bool)));
130  disconnect(mTool.get(), SIGNAL(tooltipOffset(double)), this, SLOT(tooltipOffsetSlot(double)));
131  disconnect(mTool.get(), SIGNAL(toolProbeSector()), this, SLOT(probeSectorChanged()));
132  disconnect(mTool.get(), SIGNAL(toolProbeSector()), this, SLOT(updateToolActor()));
133  }
134 
135  mTool = tool;
136 
137  if (mTool)
138  {
139  connect(mTool.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(setModified()));
140  connect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(receiveVisible(bool)));
141  connect(mTool.get(), SIGNAL(tooltipOffset(double)), this, SLOT(tooltipOffsetSlot(double)));
142  connect(mTool.get(), SIGNAL(toolProbeSector()), this, SLOT(probeSectorChanged()));
143  connect(mTool.get(), SIGNAL(toolProbeSector()), this, SLOT(updateToolActor()));
144  }
145 
146  this->updateToolActor();
147  this->setModified();
148  this->probeSectorChanged();
149 }
150 
151 bool ToolRep3D::hasTool(ToolPtr tool) const
152 {
153  return (mTool == tool);
154 }
155 
156 void ToolRep3D::setSphereRadius(double radius)
157 {
158  mSphereRadius = radius;
159  if (mOffsetPoint)
160  mOffsetPoint->setRadius(mSphereRadius);
161  if (mTooltipPoint)
162  mTooltipPoint->setRadius(mSphereRadius);
163 }
164 
166 {
167  if (mSphereRadiusInNormalizedViewport == on)
168  return;
169 
170  mSphereRadiusInNormalizedViewport = on;
171 
172  if (on)
173  {
174  mViewportListener.reset(new ViewportListener);
175  mViewportListener->setCallback(boost::bind(&ToolRep3D::scaleSpheres, this));
176  }
177  else
178  {
179  mViewportListener.reset();
180  }
181 }
182 
188 void ToolRep3D::setTooltipPointColor(const QColor& color)
189 {
190  if(mToolActor)
191  setColorAndOpacity(mToolActor->GetProperty(), color);
192  if(mTooltipPoint)
193  mTooltipPoint->setColor(color);
194 }
195 
196 void ToolRep3D::setToolOffsetPointColor(const QColor& color)
197 {
198  if(mOffsetPoint)
199  mOffsetPoint->setColor(color);
200 }
201 
202 void ToolRep3D::setToolOffsetLineColor(const QColor& color)
203 {
204  if(mOffsetLine)
205  mOffsetLine->setColor(color);
206 }
207 
209 {
210  view->getRenderer()->AddActor(mTracer->getActor());
211 
212  view->getRenderer()->AddActor(mToolActor);
213  view->getRenderer()->AddActor(mProbeSectorActor);
214 
215  mOffsetPoint.reset(new GraphicalPoint3D(view->getRenderer()));
216  mOffsetPoint->setRadius(mSphereRadius);
217  mOffsetPoint->setColor(mOffsetPointColor);
218 
219  mTooltipPoint.reset(new GraphicalPoint3D(view->getRenderer()));
220  mTooltipPoint->setRadius(mSphereRadius);
221  mTooltipPoint->setColor(mTooltipPointColor);
222 
223  mOffsetLine.reset(new GraphicalLine3D(view->getRenderer()));
224  mOffsetLine->setColor(mOffsetLineColor);
225  mOffsetLine->setStipple(mStipplePattern);
226 
227  mTooltipPoint->getActor()->SetVisibility(false);
228  mOffsetPoint->getActor()->SetVisibility(false);
229  mOffsetLine->getActor()->SetVisibility(false);
230 
231  if (mViewportListener)
232  mViewportListener->startListen(view->getRenderer());
233 }
234 
236 {
237  view->getRenderer()->RemoveActor(mTracer->getActor());
238  view->getRenderer()->RemoveActor(mToolActor);
239  view->getRenderer()->RemoveActor(mProbeSectorActor);
240 
241  mTooltipPoint.reset(new GraphicalPoint3D());
242  mOffsetPoint.reset(new GraphicalPoint3D());
243  mOffsetLine.reset(new GraphicalLine3D());
244 
245  if (mViewportListener)
246  mViewportListener->stopListen();
247 }
248 
255 void ToolRep3D::scaleSpheres()
256 {
257  if (!mViewportListener)
258  return;
259  if (!mViewportListener->isListening())
260  return;
261 
262  double size = mViewportListener->getVpnZoom(this->getTool()->get_prMt().translation());
263  double sphereSize = mSphereRadius/100/size;
264 
265  if (mOffsetPoint)
266  mOffsetPoint->setRadius(sphereSize);
267  if (mTooltipPoint)
268  mTooltipPoint->setRadius(sphereSize);
269 }
270 
272 {
273  this->update();
274 }
275 
276 
277 void ToolRep3D::update()
278 {
279  Transform3D prMt = Transform3D::Identity();
280  if (mTool)
281  prMt = mTool->get_prMt();
282  Transform3D rMpr = mSpaceProvider->get_rMpr();
283 
284  Transform3D rMt = rMpr * prMt;
285  mToolActor->SetUserMatrix(rMt.getVtkMatrix());
286 
287  if (this->showProbe())
288  {
289  Transform3D tMu = mProbeSector->get_tMu();
290  mProbeSectorActor->SetUserMatrix((rMpr * prMt * tMu).getVtkMatrix());
291  mProbeSectorActor->SetVisibility(mTool->getVisible());
292  }
293  else
294  mProbeSectorActor->SetVisibility(false);
295 
296  this->updateOffsetGraphics();
297 }
298 
299 void ToolRep3D::probeSectorChanged()
300 {
301  if (!mTool)
302  return;
303 
304  Transform3D prMt = mTool->get_prMt();
305  Transform3D rMpr = mSpaceProvider->get_rMpr();
306 
307  if (this->showProbe())
308  {
309  mProbeSector->setData(mTool->getProbe()->getProbeDefinition());
310  Transform3D tMu = mProbeSector->get_tMu();
311 
312  mProbeSectorPolyDataMapper->SetInputData(mProbeSector->getSectorLinesOnly());
313  if (mProbeSectorPolyDataMapper->GetInput())
314  {
315  mProbeSectorActor->SetMapper(mProbeSectorPolyDataMapper);
316  }
317  mProbeSectorActor->SetUserMatrix((rMpr * prMt * tMu).getVtkMatrix());
318  mProbeSectorActor->SetVisibility(mTool->getVisible());
319  }
320  else
321  mProbeSectorActor->SetVisibility(false);
322 }
323 
324 void ToolRep3D::updateOffsetGraphics()
325 {
326  bool visible = mTool && mTool->getVisible();
327 
328  if (!mStayVisibleAfterHide || (mOffsetPoint->getActor()->GetVisibility() == false))
329  {
330  mOffsetPoint->getActor()->SetVisibility(visible);
331  mTooltipPoint->getActor()->SetVisibility(visible);
332  //Don't show tooltipPoint when in tool view, as this will obscure the offsetPoint
333  if (mStayHiddenAfterVisible)
334  mTooltipPoint->getActor()->SetVisibility(false);
335  mOffsetLine->getActor()->SetVisibility(visible);
336  }
337 
338  if (similar(0.0, mTool->getTooltipOffset()))
339  {
340  if(mTool->hasType(Tool::TOOL_US_PROBE))
341  mTooltipPoint->getActor()->SetVisibility(false);
342  else
343  mTooltipPoint->getActor()->SetVisibility(visible && mOffsetPointVisibleAtZeroOffset);
344  mOffsetPoint->getActor()->SetVisibility(false);
345  mOffsetLine->getActor()->SetVisibility(false);
346  }
347 
348  if (!mTool)
349  return;
350  Transform3D rMpr = mSpaceProvider->get_rMpr();
351  Transform3D rMt = rMpr * mTool->get_prMt();
352 
353  Vector3D p0 = rMt.coord(Vector3D(0, 0, 0));
354  Vector3D p1 = rMt.coord(Vector3D(0, 0, mTool->getTooltipOffset()));
355  mOffsetPoint->setValue(p1);
356  mOffsetLine->setValue(p0, p1);
357  mTooltipPoint->setValue(Vector3D(p0));
358 
359  this->scaleSpheres();
360 }
361 
362 void ToolRep3D::receiveVisible(bool visible)
363 {
364  if (!visible && mStayVisibleAfterHide)
365  return; // don't hide
366  mToolActor->SetVisibility(visible);
367 
368  if (mStayHiddenAfterVisible)
369  mToolActor->SetVisibility(false);
370  else
371  mToolActor->SetVisibility(mTool->getVisible());
372 
373  this->update();
374 }
375 
377 {
378  mStayHiddenAfterVisible = val;
379  if (mTool)
380  receiveVisible(mTool->getVisible());
381 }
382 
388 {
389  mStayVisibleAfterHide = val;
390 }
391 
393 {
394  mOffsetPointVisibleAtZeroOffset = val;
395 }
396 
397 void ToolRep3D::tooltipOffsetSlot(double val)
398 {
399  updateOffsetGraphics();
400 }
401 
403 {
404  return mTool && (mTool->hasType(Tool::TOOL_US_PROBE)) && (mTool->getProbe()->getProbeDefinition().getType()!=ProbeDefinition::tNONE);
405 }
406 
407 } // namespace cx
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
virtual ToolPtr getTool()
Definition: cxToolRep3D.cpp:84
virtual void onModifiedStartRender()
virtual void removeRepActorsFromViewRenderer(ViewPtr view)
virtual void setTool(ToolPtr tool)
virtual bool hasTool(ToolPtr tool) const
virtual QString getType() const
Definition: cxToolRep3D.cpp:79
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
void setStayVisibleAfterHide(bool val)
if true, tool is still rendered as visible after visibility status is hidden.
static boost::shared_ptr< REP > wrap_new(REP *object, QString uid)
Definition: cxRepImpl.h:62
ToolRep3D(SpaceProviderPtr spaceProvider)
Definition: cxToolRep3D.cpp:37
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:66
boost::shared_ptr< class ToolTracer > ToolTracerPtr
Helper for rendering a point in 3D.
boost::shared_ptr< class View > ViewPtr
Listens to changes in viewport and camera matrix.
void setToolOffsetLineColor(const QColor &color)
void setSphereRadius(double radius)
vtkMatrix4x4Ptr getVtkMatrix(const Eigen::Affine3d *self)
void setStayHiddenAfterVisible(bool val)
virtual void addRepActorsToViewRenderer(ViewPtr view)
vtkSmartPointer< class vtkPolyDataNormals > vtkPolyDataNormalsPtr
Helper for rendering a line in 3D.
Representation of a mouse/keyboard-controlled virtual tool.
Definition: cxTool.h:85
Default implementation of Rep.
Definition: cxRepImpl.h:42
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
void setSphereRadiusInNormalizedViewport(bool on)
void setOffsetPointVisibleAtZeroOffset(bool val)
if true the sphere is visible even when the offset is zero
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
Utility functions for drawing an US Probe sector.
Definition: cxProbeSector.h:38
void setTooltipPointColor(const QColor &color)
ToolRep3D::setTooltipPointColor.
static ToolRep3DPtr New(SpaceProviderPtr spaceProvider, const QString &uid="")
Definition: cxToolRep3D.cpp:74
ToolTracerPtr getTracer()
Definition: cxToolRep3D.cpp:69
boost::shared_ptr< class ToolRep3D > ToolRep3DPtr
static ToolTracerPtr create(SpaceProviderPtr spaceProvider)
Ultrasond probe. The tool has a Probe subinterface with a sector and a video stream.
Definition: cxTool.h:87
void setToolOffsetPointColor(const QColor &color)
void setColorAndOpacity(vtkPropertyOrProperty2DPtr property, QColor color)
virtual ~ToolRep3D()
Definition: cxToolRep3D.cpp:66
void setModified()
Definition: cxRepImpl.cpp:112
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr