CustusX  22.04-rc3
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 = settings()->value("View3D/sphereRadius").value<double>();
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 size = mViewportListener->getVpnZoom();
264  double sphereSize = mSphereRadius/100/size;
265 
266  if (mOffsetPoint)
267  mOffsetPoint->setRadius(sphereSize);
268  if (mTooltipPoint)
269  mTooltipPoint->setRadius(sphereSize);
270 }
271 
273 {
274  this->update();
275 }
276 
277 
278 void ToolRep3D::update()
279 {
280  Transform3D prMt = Transform3D::Identity();
281  if (mTool)
282  prMt = mTool->get_prMt();
283  Transform3D rMpr = mSpaceProvider->get_rMpr();
284 
285  Transform3D rMt = rMpr * prMt;
286  mToolActor->SetUserMatrix(rMt.getVtkMatrix());
287 
288  if (this->showProbe())
289  {
290  Transform3D tMu = mProbeSector->get_tMu();
291  mProbeSectorActor->SetUserMatrix((rMpr * prMt * tMu).getVtkMatrix());
292  mProbeSectorActor->SetVisibility(mTool->getVisible());
293  }
294  else
295  mProbeSectorActor->SetVisibility(false);
296 
297  this->updateOffsetGraphics();
298 }
299 
300 void ToolRep3D::probeSectorChanged()
301 {
302  if (!mTool)
303  return;
304 
305  Transform3D prMt = mTool->get_prMt();
306  Transform3D rMpr = mSpaceProvider->get_rMpr();
307 
308  if (this->showProbe())
309  {
310  mProbeSector->setData(mTool->getProbe()->getProbeDefinition());
311  Transform3D tMu = mProbeSector->get_tMu();
312 
313  mProbeSectorPolyDataMapper->SetInputData(mProbeSector->getSectorLinesOnly());
314  if (mProbeSectorPolyDataMapper->GetInput())
315  {
316  mProbeSectorActor->SetMapper(mProbeSectorPolyDataMapper);
317  }
318  mProbeSectorActor->SetUserMatrix((rMpr * prMt * tMu).getVtkMatrix());
319  mProbeSectorActor->SetVisibility(mTool->getVisible());
320  }
321  else
322  mProbeSectorActor->SetVisibility(false);
323 }
324 
325 void ToolRep3D::updateOffsetGraphics()
326 {
327  bool visible = mTool && mTool->getVisible();
328 
329  if (!mStayVisibleAfterHide || (mOffsetPoint->getActor()->GetVisibility() == false))
330  {
331  mOffsetPoint->getActor()->SetVisibility(visible);
332  mTooltipPoint->getActor()->SetVisibility(visible);
333  //Don't show tooltipPoint when in tool view, as this will obscure the offsetPoint
334  if (mStayHiddenAfterVisible)
335  mTooltipPoint->getActor()->SetVisibility(false);
336  mOffsetLine->getActor()->SetVisibility(visible);
337  }
338 
339  if (similar(0.0, mTool->getTooltipOffset()))
340  {
341  if(mTool->hasType(Tool::TOOL_US_PROBE))
342  mTooltipPoint->getActor()->SetVisibility(false);
343  else
344  mTooltipPoint->getActor()->SetVisibility(visible && mOffsetPointVisibleAtZeroOffset);
345  mOffsetPoint->getActor()->SetVisibility(false);
346  mOffsetLine->getActor()->SetVisibility(false);
347  }
348 
349  if (!mTool)
350  return;
351  Transform3D rMpr = mSpaceProvider->get_rMpr();
352  Transform3D rMt = rMpr * mTool->get_prMt();
353 
354  Vector3D p0 = rMt.coord(Vector3D(0, 0, 0));
355  Vector3D p1 = rMt.coord(Vector3D(0, 0, mTool->getTooltipOffset()));
356  mOffsetPoint->setValue(p1);
357  mOffsetLine->setValue(p0, p1);
358  mTooltipPoint->setValue(Vector3D(p0));
359 
360  this->scaleSpheres();
361 }
362 
363 void ToolRep3D::receiveVisible(bool visible)
364 {
365  if (!visible && mStayVisibleAfterHide)
366  return; // don't hide
367  mToolActor->SetVisibility(visible);
368 
369  if (mStayHiddenAfterVisible)
370  mToolActor->SetVisibility(false);
371  else
372  mToolActor->SetVisibility(mTool->getVisible());
373 
374  this->update();
375 }
376 
378 {
379  mStayHiddenAfterVisible = val;
380  if (mTool)
381  receiveVisible(mTool->getVisible());
382 }
383 
389 {
390  mStayVisibleAfterHide = val;
391 }
392 
394 {
395  mOffsetPointVisibleAtZeroOffset = val;
396 }
397 
398 void ToolRep3D::tooltipOffsetSlot(double val)
399 {
400  updateOffsetGraphics();
401 }
402 
404 {
405  return mTool && (mTool->hasType(Tool::TOOL_US_PROBE)) && (mTool->getProbe()->getProbeDefinition().getType()!=ProbeDefinition::tNONE);
406 }
407 
408 } // 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