Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 "cxToolRep3D.h"
35 
36 #include "boost/bind.hpp"
37 #include <vtkActor.h>
38 #include <vtkProperty.h>
39 #include <vtkPolyDataMapper.h>
40 #include <vtkPolyDataNormals.h>
41 #include <vtkSTLReader.h>
42 #include <vtkMatrix4x4.h>
43 #include <vtkRenderer.h>
44 
45 #include "cxGraphicalPrimitives.h"
46 #include "cxProbe.h"
47 #include "cxView.h"
48 #include "cxVideoSourceGraphics.h"
49 #include "cxTypeConversions.h"
50 #include "cxToolTracer.h"
51 #include "cxTool.h"
52 #include "cxSpaceProvider.h"
53 
54 namespace cx
55 {
56 
58  RepImpl(),
59  mSpaceProvider(spaceProvider),
60  mSphereRadiusInNormalizedViewport(false),
61  mTooltipPointColor(QColor::fromRgbF(1.0, 0.8, 0.0)),
62  mOffsetPointColor(QColor::fromRgbF(1.0, 0.8, 0.0)),
63  mOffsetLineColor(QColor::fromRgbF(1.0, 0.8, 0.0)),
64  mStipplePattern(0xFFFF)
65 {
66  mSphereRadius = 2;
67  mStayHiddenAfterVisible = false;
68  mStayVisibleAfterHide = false;
69  mOffsetPointVisibleAtZeroOffset = false;
70  mToolActor = vtkActorPtr::New();
71  mPolyDataMapper = vtkPolyDataMapperPtr::New();
72 // mSTLReader = vtkSTLReaderPtr::New();
73 
74  mOffsetPoint.reset(new GraphicalPoint3D());
75  mOffsetLine.reset(new GraphicalLine3D());
76  mTooltipPoint.reset(new GraphicalPoint3D());
77 
78  mProbeSector.reset(new ProbeSector());
79  mProbeSectorPolyDataMapper = vtkPolyDataMapperPtr::New();
80  mProbeSectorActor = vtkActorPtr::New();
81 
82  mTracer = ToolTracer::create(mSpaceProvider);
83 }
84 
86 {}
87 
89 {
90  return mTracer;
91 }
92 
94 {
95  return wrap_new(new ToolRep3D(spaceProvider), uid);
96 }
97 
98 QString ToolRep3D::getType() const
99 {
100  return "ToolRep3D";
101 }
102 
104 {
105  return mTool;
106 }
107 
108 void ToolRep3D::updateToolActor()
109 {
110  if (!mTool)
111  {
112  mToolActor->SetMapper(NULL);
113  return;
114  }
115 
116  vtkPolyDataPtr model = mTool->getGraphicsPolyData();
117 
118  if (model)
119  {
120  vtkPolyDataNormalsPtr normals = vtkPolyDataNormalsPtr::New();
121  normals->SetInputData(model);
122  mPolyDataMapper->SetInputConnection(normals->GetOutputPort());
123  mPolyDataMapper->Update();
124  mToolActor->SetMapper(mPolyDataMapper);
125  }
126 
127  //some color to 3D cursor
128  mToolActor->GetProperty()->SetColor(1.0, 1.0, 1.0);
129  if (mTool->hasType(Tool::TOOL_MANUAL))
130  {
131  setColorAndOpacity(mToolActor->GetProperty(), mTooltipPointColor);
132  }
133 
134  this->setModified();
135  mToolActor->SetVisibility(mTool->getVisible());
136 }
137 
139 {
140  if (tool == mTool)
141  return;
142 
143  mTracer->setTool(tool);
144 
145  if (mTool)
146  {
147  disconnect(mTool.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(setModified()));
148  disconnect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(receiveVisible(bool)));
149  disconnect(mTool.get(), SIGNAL(tooltipOffset(double)), this, SLOT(tooltipOffsetSlot(double)));
150  disconnect(mTool.get(), SIGNAL(toolProbeSector()), this, SLOT(probeSectorChanged()));
151  disconnect(mTool.get(), SIGNAL(toolProbeSector()), this, SLOT(updateToolActor()));
152  }
153 
154  mTool = tool;
155 
156  if (mTool)
157  {
158  connect(mTool.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(setModified()));
159  connect(mTool.get(), SIGNAL(toolVisible(bool)), this, SLOT(receiveVisible(bool)));
160  connect(mTool.get(), SIGNAL(tooltipOffset(double)), this, SLOT(tooltipOffsetSlot(double)));
161  connect(mTool.get(), SIGNAL(toolProbeSector()), this, SLOT(probeSectorChanged()));
162  connect(mTool.get(), SIGNAL(toolProbeSector()), this, SLOT(updateToolActor()));
163  }
164 
165  this->updateToolActor();
166  this->setModified();
167  this->probeSectorChanged();
168 }
169 
170 bool ToolRep3D::hasTool(ToolPtr tool) const
171 {
172  return (mTool == tool);
173 }
174 
175 void ToolRep3D::setSphereRadius(double radius)
176 {
177  mSphereRadius = radius;
178  if (mOffsetPoint)
179  mOffsetPoint->setRadius(mSphereRadius);
180  if (mTooltipPoint)
181  mTooltipPoint->setRadius(mSphereRadius);
182 }
183 
185 {
186  mTooltipPointColor = c;
187 }
188 
190 {
191  mOffsetPointColor = c;
192 }
193 
195 {
196  mOffsetLineColor = c;
197 }
198 
200 {
201  mStipplePattern = pattern;
202 }
203 
205 {
206  if (mSphereRadiusInNormalizedViewport == on)
207  return;
208 
209  mSphereRadiusInNormalizedViewport = on;
210 
211  if (on)
212  {
213  mViewportListener.reset(new ViewportListener);
214  mViewportListener->setCallback(boost::bind(&ToolRep3D::scaleSpheres, this));
215  }
216  else
217  {
218  mViewportListener.reset();
219  }
220 }
221 
223 {
224  view->getRenderer()->AddActor(mTracer->getActor());
225 
226  view->getRenderer()->AddActor(mToolActor);
227  view->getRenderer()->AddActor(mProbeSectorActor);
228 
229  mOffsetPoint.reset(new GraphicalPoint3D(view->getRenderer()));
230  mOffsetPoint->setRadius(mSphereRadius);
231  mOffsetPoint->setColor(mOffsetPointColor);
232 
233  mTooltipPoint.reset(new GraphicalPoint3D(view->getRenderer()));
234  mTooltipPoint->setRadius(mSphereRadius);
235  mTooltipPoint->setColor(mTooltipPointColor);
236 
237  mOffsetLine.reset(new GraphicalLine3D(view->getRenderer()));
238  mOffsetLine->setColor(mOffsetLineColor);
239  mOffsetLine->setStipple(mStipplePattern);
240 
241  mTooltipPoint->getActor()->SetVisibility(false);
242  mOffsetPoint->getActor()->SetVisibility(false);
243  mOffsetLine->getActor()->SetVisibility(false);
244 
245  if (mViewportListener)
246  mViewportListener->startListen(view->getRenderer());
247 }
248 
250 {
251  view->getRenderer()->RemoveActor(mTracer->getActor());
252  view->getRenderer()->RemoveActor(mToolActor);
253  view->getRenderer()->RemoveActor(mProbeSectorActor);
254 
255  mTooltipPoint.reset(new GraphicalPoint3D());
256  mOffsetPoint.reset(new GraphicalPoint3D());
257  mOffsetLine.reset(new GraphicalLine3D());
258 
259  if (mViewportListener)
260  mViewportListener->stopListen();
261 }
262 
269 void ToolRep3D::scaleSpheres()
270 {
271  if (!mViewportListener)
272  return;
273  if (!mViewportListener->isListening())
274  return;
275 
276  double size = mViewportListener->getVpnZoom();
277  double sphereSize = mSphereRadius/100/size;
278 
279  if (mOffsetPoint)
280  mOffsetPoint->setRadius(sphereSize);
281  if (mTooltipPoint)
282  mTooltipPoint->setRadius(sphereSize);
283 }
284 
286 {
287  this->update();
288 }
289 
290 
291 void ToolRep3D::update()
292 {
293  Transform3D prMt = Transform3D::Identity();
294  if (mTool)
295  prMt = mTool->get_prMt();
296  Transform3D rMpr = mSpaceProvider->get_rMpr();
297 
298  Transform3D rMt = rMpr * prMt;
299  mToolActor->SetUserMatrix(rMt.getVtkMatrix());
300 
301  if (this->showProbe())
302  {
303  Transform3D tMu = mProbeSector->get_tMu();
304  mProbeSectorActor->SetUserMatrix((rMpr * prMt * tMu).getVtkMatrix());
305  mProbeSectorActor->SetVisibility(mTool->getVisible());
306  }
307  else
308  mProbeSectorActor->SetVisibility(false);
309 
310  this->updateOffsetGraphics();
311 }
312 
313 void ToolRep3D::probeSectorChanged()
314 {
315  if (!mTool)
316  return;
317 
318  Transform3D prMt = mTool->get_prMt();
319  Transform3D rMpr = mSpaceProvider->get_rMpr();
320 
321  if (this->showProbe())
322  {
323  mProbeSector->setData(mTool->getProbe()->getProbeDefinition());
324  Transform3D tMu = mProbeSector->get_tMu();
325 
326  mProbeSectorPolyDataMapper->SetInputData(mProbeSector->getSectorLinesOnly());
327  if (mProbeSectorPolyDataMapper->GetInput())
328  {
329  mProbeSectorActor->SetMapper(mProbeSectorPolyDataMapper);
330  }
331  mProbeSectorActor->SetUserMatrix((rMpr * prMt * tMu).getVtkMatrix());
332  mProbeSectorActor->SetVisibility(mTool->getVisible());
333  }
334  else
335  mProbeSectorActor->SetVisibility(false);
336 }
337 
338 void ToolRep3D::updateOffsetGraphics()
339 {
340  bool visible = mTool && mTool->getVisible();
341 
342  if (!mStayVisibleAfterHide || (mOffsetPoint->getActor()->GetVisibility() == false))
343  {
344  mOffsetPoint->getActor()->SetVisibility(visible);
345  mTooltipPoint->getActor()->SetVisibility(visible);
346  //Don't show tooltipPoint when in tool view, as this will obscure the offsetPoint
347  if (mStayHiddenAfterVisible)
348  mTooltipPoint->getActor()->SetVisibility(false);
349  mOffsetLine->getActor()->SetVisibility(visible);
350  }
351 
352  if (similar(0.0, mTool->getTooltipOffset()))
353  {
354  if(mTool->hasType(Tool::TOOL_US_PROBE))
355  mTooltipPoint->getActor()->SetVisibility(false);
356  else
357  mTooltipPoint->getActor()->SetVisibility(visible && mOffsetPointVisibleAtZeroOffset);
358  mOffsetPoint->getActor()->SetVisibility(false);
359  mOffsetLine->getActor()->SetVisibility(false);
360  }
361 
362  if (!mTool)
363  return;
364  Transform3D rMpr = mSpaceProvider->get_rMpr();
365  Transform3D rMt = rMpr * mTool->get_prMt();
366 
367  Vector3D p0 = rMt.coord(Vector3D(0, 0, 0));
368  Vector3D p1 = rMt.coord(Vector3D(0, 0, mTool->getTooltipOffset()));
369  mOffsetPoint->setValue(p1);
370  mOffsetLine->setValue(p0, p1);
371  mTooltipPoint->setValue(Vector3D(p0));
372 
373  this->scaleSpheres();
374 }
375 
376 void ToolRep3D::receiveVisible(bool visible)
377 {
378  if (!visible && mStayVisibleAfterHide)
379  return; // don't hide
380  mToolActor->SetVisibility(visible);
381 
382  if (mStayHiddenAfterVisible)
383  mToolActor->SetVisibility(false);
384  else
385  mToolActor->SetVisibility(mTool->getVisible());
386 
387  this->update();
388 }
389 
391 {
392  mStayHiddenAfterVisible = val;
393  if (mTool)
394  receiveVisible(mTool->getVisible());
395 }
396 
402 {
403  mStayVisibleAfterHide = val;
404 }
405 
407 {
408  mOffsetPointVisibleAtZeroOffset = val;
409 }
410 
411 void ToolRep3D::tooltipOffsetSlot(double val)
412 {
413  updateOffsetGraphics();
414 }
415 
417 {
418  return mTool && (mTool->hasType(Tool::TOOL_US_PROBE)) && (mTool->getProbe()->getProbeDefinition().getType()!=ProbeDefinition::tNONE);
419 }
420 
421 } // namespace cx
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
virtual ToolPtr getTool()
virtual void onModifiedStartRender()
virtual void removeRepActorsFromViewRenderer(ViewPtr view)
virtual void setTool(ToolPtr tool)
void setTooltipPointColor(QColor c)
virtual bool hasTool(ToolPtr tool) const
virtual QString getType() const
Definition: cxToolRep3D.cpp:98
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
void setOffsetPointColor(QColor c)
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:83
ToolRep3D(SpaceProviderPtr spaceProvider)
Definition: cxToolRep3D.cpp:57
boost::shared_ptr< class ToolTracer > ToolTracerPtr
Helper for rendering a point in 3D.
boost::shared_ptr< class View > ViewPtr
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
Listens to changes in viewport and camera matrix.
void setSphereRadius(double radius)
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
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:106
Default implementation of Rep.
Definition: cxRepImpl.h:63
void setOffsetStipplePattern(int pattern)
void setSphereRadiusInNormalizedViewport(bool on)
void setOffsetPointVisibleAtZeroOffset(bool val)
if true the sphere is visible even when the offset is zero
cxLogicManager_EXPORT SpaceProviderPtr spaceProvider()
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
Utility functions for drawing an US Probe sector.
Definition: cxProbeSector.h:59
static ToolRep3DPtr New(SpaceProviderPtr spaceProvider, const QString &uid="")
Definition: cxToolRep3D.cpp:93
ToolTracerPtr getTracer()
Definition: cxToolRep3D.cpp:88
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:108
void setColorAndOpacity(vtkPropertyPtr property, QColor color)
virtual ~ToolRep3D()
Definition: cxToolRep3D.cpp:85
void setModified()
Definition: cxRepImpl.cpp:132
void setOffsetLineColor(QColor c)
boost::shared_ptr< class Tool > ToolPtr