CustusX  2023.01.05-dev+develop.0da12
An IGT application
cxGuideRep2D.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 
14 #include "cxGuideRep2D.h"
15 #include "boost/bind.hpp"
16 
17 #include <vtkActor.h>
18 #include <vtkPolyDataMapper.h>
19 #include <vtkProperty.h>
20 #include <vtkRenderer.h>
21 #include <vtkSectorSource.h>
22 #include "cxTool.h"
23 
24 #include "cxSliceProxy.h"
25 #include "cxView.h"
26 #include "cxPatientModelService.h"
27 
28 namespace cx
29 {
30 
31 GuideRep2DPtr GuideRep2D::New(PatientModelServicePtr dataManager, const QString& uid)
32 {
33  return wrap_new(new GuideRep2D(dataManager), uid);
34 }
35 
36 GuideRep2D::GuideRep2D(PatientModelServicePtr dataManager) :
37  mDataManager(dataManager),
38  mOutlineWidth(1),
39  mRequestedAccuracy(1)
40 {
41 }
42 
43 
45 {
46  if (this->getView())
47  this->getView()->getRenderer()->RemoveActor(mCircleActor);
48 }
49 
51 {
52  if (!mMetric)
53  return;
54 
55  if (!mCircleActor && this->getView() && mMetric && mSliceProxy)
56  {
57  mCircleSource = vtkSectorSource::New();
58  mCircleSource->SetOuterRadius(mGraphicsSize);
59  mCircleSource->SetInnerRadius(0);
60  mCircleSource->SetStartAngle(0);
61  mCircleSource->SetEndAngle(360);
62  mCircleSource->SetCircumferentialResolution(60);
63  vtkPolyDataMapperPtr mapper = vtkPolyDataMapper::New();
64  mapper->SetInputConnection(mCircleSource->GetOutputPort());
65  mapper->ScalarVisibilityOff();
66  mCircleActor = vtkActor::New();
67  mCircleActor->SetMapper(mapper);
68  mCircleActor->GetProperty()->LightingOff();
69  this->getRenderer()->AddActor(mCircleActor);
70 
71  }
72 
73  if (!mCircleActor)
74  return;
75  if (!mSliceProxy->getTool())
76  {
77  return;
78  }
79 
80  double toolOffset = mSliceProxy->getTool()->getTooltipOffset();
81  Transform3D rMt = mDataManager->get_rMpr()*mSliceProxy->getTool()->get_prMt();
82  Vector3D toolOffsetPosRef = rMt.coord(Vector3D(0,0,toolOffset));
83  Vector3D toolPosRef = rMt.coord(Vector3D(0,0,0));
84 
85  Vector3D centerRef = mMetric->getRefCoord() + 0.5*(toolOffsetPosRef - mMetric->getRefCoord());
86  Vector3D position = mSliceProxy->get_sMr() * centerRef;
87  mCircleActor->SetPosition(position[0], position[1], 0);
88 
89  const double margin = 10;
90  double offsetDistance = (mMetric->getRefCoord() - toolOffsetPosRef).length();
91  double distance = (mMetric->getRefCoord() - toolPosRef).length();
92  double radius = 0.5 * offsetDistance + margin;
93  mCircleSource->SetOuterRadius(radius);
94  mCircleSource->SetInnerRadius(radius - mOutlineWidth);
95 
96  if (distance < mRequestedAccuracy)
97  {
98  mCircleActor->GetProperty()->SetColor(0, 1, 1);
99  }
100  else if (offsetDistance < mRequestedAccuracy && toolOffset >= 0 && distance < 4*mRequestedAccuracy)
101  {
102  mCircleActor->GetProperty()->SetColor(0, 1, 1.0 - (distance-mRequestedAccuracy)/(3*mRequestedAccuracy));
103  }
104  else if (offsetDistance < mRequestedAccuracy && toolOffset >= 0)
105  {
106  mCircleActor->GetProperty()->SetColor(0, 1, 0);
107  }
108  else
109  {
110  mCircleActor->GetProperty()->SetColor(1, 0, 0);
111  }
112 
113 }
114 
116 {
117  if (mSliceProxy)
118  disconnect(mSliceProxy.get(), SIGNAL(transformChanged(Transform3D)), this, SLOT(setModified()));
119  mSliceProxy = sliceProxy;
120  if (mSliceProxy)
121  connect(mSliceProxy.get(), SIGNAL(transformChanged(Transform3D)), this, SLOT(setModified()));
122  this->setModified();
123 }
124 
125 void GuideRep2D::setOutlineWidth(double width)
126 {
127  mOutlineWidth = width;
128  this->setModified();
129 }
130 
131 void GuideRep2D::setRequestedAccuracy(double accuracy)
132 {
133  mRequestedAccuracy = accuracy;
134  this->setModified();
135 }
136 
137 }
ViewPtr getView() const
Definition: cxRepImpl.cpp:83
vtkRendererPtr getRenderer()
Definition: cxRepImpl.cpp:88
static GuideRep2DPtr New(PatientModelServicePtr dataManager, const QString &uid="")
void setOutlineWidth(double width)
vtkSmartPointer< class vtkPolyDataMapper > vtkPolyDataMapperPtr
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class SliceProxy > SliceProxyPtr
static boost::shared_ptr< REP > wrap_new(REP *object, QString uid)
Definition: cxRepImpl.h:62
void setRequestedAccuracy(double accuracy)
boost::shared_ptr< class GuideRep2D > GuideRep2DPtr
virtual void onModifiedStartRender()
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
RealScalar length() const
DataMetricPtr mMetric
virtual void clear()
void setModified()
Definition: cxRepImpl.cpp:112
void setSliceProxy(SliceProxyPtr slicer)
Namespace for all CustusX production code.