Fraxinus  16.5.0-fx-rc4
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxGraphicalDisk.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 #include "cxGraphicalDisk.h"
34 
35 #include <QColor>
36 #include <vtkRenderer.h>
37 #include <vtkActor.h>
38 #include <vtkPolyDataMapper.h>
39 #include <vtkProperty.h>
40 #include <vtkSectorSource.h>
41 #include "cxVtkHelperClasses.h"
42 #include "vtkMatrix4x4.h"
43 #include "vtkLinearExtrusionFilter.h"
44 #include "vtkPolyDataNormals.h"
45 
46 namespace cx
47 {
48 
50  mOutlineWidth(0.1)
51 {
52  mDirection = Vector3D::UnitZ();
53 // std::cout << "GraphicalDisk::create " << mDirection << std::endl;
54 
55  mRadius = 1;
56  mColor = QColor(Qt::blue);
57  mFillVisible = true;
58  mOutlineColor = QColor(Qt::magenta);
59  mUseLighting = false;
60  mHeight = 0;
61 }
62 
64 {
65  this->removeActors();
66 }
67 
68 void GraphicalDisk::setRadius(double radius)
69 {
70  mRadius = radius;
71 }
73 {
74  mOutlineWidth = width;
75 }
77 {
78  mFillVisible = val;
79 }
81 {
82  mOutlineColor = color;
83 }
84 void GraphicalDisk::setColor(QColor color)
85 {
86  mColor = color;
87 }
89 {
90  mPosition = pos;
91 }
93 {
94 // std::cout << "GraphicalDisk::setDirection " << direction << std::endl;
95 
96  mDirection = direction;
97 }
99 {
100  mUseLighting = on;
101 }
102 void GraphicalDisk::setHeight(double height)
103 {
104  mHeight = height;
105 }
106 
108 {
109  this->removeActors();
110  mRenderer = renderer;
111  this->addActors();
112 }
113 
114 void GraphicalDisk::addActors()
115 {
116  if (mRenderer && mCircleActor && mOutlineActor)
117  {
118  mRenderer->AddActor(mCircleActor);
119  mRenderer->AddActor(mOutlineActor);
120  }
121 }
122 
123 void GraphicalDisk::removeActors()
124 {
125  if (mRenderer && mCircleActor && mOutlineActor)
126  {
127  mRenderer->RemoveActor(mCircleActor);
128  mRenderer->RemoveActor(mOutlineActor);
129  }
130 }
131 
133 {
134  if (!mRenderer)
135  return;
136 
137  if (!mCircleActor)
138  {
139  this->createActors();
140  this->addActors();
141  }
142 
143  if (!mCircleActor)
144  return;
145 
146  mCircleActor->GetProperty()->SetColor(getColorAsVector3D(mColor).begin());
147  mOutlineActor->GetProperty()->SetColor(getColorAsVector3D(mOutlineColor).begin());
148 
149  double innerRadius = std::max(0.0, mRadius*(1.0 - mOutlineWidth));
150  mCircleSource->SetOuterRadius(innerRadius);
151  mOutlineSource->SetInnerRadius(innerRadius);
152  mOutlineSource->SetOuterRadius(mRadius);
153 
154  mCircleExtruder->SetScaleFactor(mHeight);
155  mOutlineExtruder->SetScaleFactor(mHeight);
156 
157  mCircleActor->SetVisibility(mFillVisible);
158 
159  this->updateOrientation();
160 }
161 
162 void GraphicalDisk::createActors()
163 {
164  if (mCircleActor)
165  return;
166 
167  int resolution = 40;
168 
169  mCircleSource = vtkSectorSourcePtr::New();
170  mCircleSource->SetOuterRadius(mRadius);
171  mCircleSource->SetInnerRadius(0);
172  mCircleSource->SetStartAngle(0);
173  mCircleSource->SetEndAngle(360);
174  mCircleSource->SetCircumferentialResolution(resolution);
175 
176  mCircleExtruder = vtkLinearExtrusionFilterPtr::New();
177  mCircleExtruder->SetInputConnection(mCircleSource->GetOutputPort());
178  mCircleExtruder->SetScaleFactor(mHeight);
179  mCircleExtruder->SetExtrusionTypeToVectorExtrusion();
180  mCircleExtruder->SetVector(0,0,1);
181 
182  vtkPolyDataMapperPtr mapper = vtkPolyDataMapperPtr::New();
183  mapper->SetInputConnection(mCircleExtruder->GetOutputPort());
184  mapper->ScalarVisibilityOff();
185  mCircleActor = vtkActorPtr::New();
186  mCircleActor->SetMapper(mapper);
187  mCircleActor->GetProperty()->SetLighting(mUseLighting);
188 
189  mOutlineSource = vtkSectorSourcePtr::New();
190  mOutlineSource->SetOuterRadius(mRadius);
191  mOutlineSource->SetInnerRadius(0);
192  mOutlineSource->SetStartAngle(0);
193  mOutlineSource->SetEndAngle(360);
194  mOutlineSource->SetCircumferentialResolution(resolution);
195 
196  mOutlineExtruder = vtkLinearExtrusionFilterPtr::New();
197  mOutlineExtruder->SetInputConnection(mOutlineSource->GetOutputPort());
198  mOutlineExtruder->SetScaleFactor(mHeight);
199  mOutlineExtruder->SetExtrusionTypeToVectorExtrusion();
200  mOutlineExtruder->SetVector(0,0,1);
201 
202  vtkPolyDataNormalsPtr normals = vtkPolyDataNormalsPtr::New();
203  normals->SetInputConnection(mOutlineExtruder->GetOutputPort());
204 
205  vtkPolyDataMapperPtr outlineMapper = vtkPolyDataMapperPtr::New();
206  outlineMapper->SetInputConnection(normals->GetOutputPort());
207  outlineMapper->ScalarVisibilityOff();
208  mOutlineActor = vtkActorPtr::New();
209  mOutlineActor->SetMapper(outlineMapper);
210  mOutlineActor->GetProperty()->SetLighting(mUseLighting);
211 }
212 
213 
214 void GraphicalDisk::updateOrientation()
215 {
216 // Transform3D M = createTransformRotationBetweenVectors(Vector3D::UnitX(), mDirection.normal());
217 
218  Vector3D from = Vector3D::UnitZ();
219  Transform3D M;
220  bool directionAlongYAxis = similar(dot(from, mDirection.normal()), 1.0);
221 
222  if (directionAlongYAxis)
223  {
224  M = Transform3D::Identity();
225  }
226  else
227  {
228  Vector3D newXAxis = cross(from, mDirection).normal();
229  Vector3D newZAxis = mDirection;
230  Vector3D ivec = newXAxis;
231  Vector3D jvec = cross(newZAxis, newXAxis);
232  Vector3D center = Vector3D::Zero();
233  M = createTransformIJC(ivec, jvec, center);
234 
235  }
236 
237  Transform3D T = createTransformTranslate(mPosition);
238  M = T*M;
239 
240  mCircleActor->SetUserMatrix(M.getVtkMatrix());
241  mOutlineActor->SetUserMatrix(M.getVtkMatrix());
242 
243 // mCircleActor->SetPosition(mPosition[0], mPosition[1], mPosition[2]);
244 // mOutlineActor->SetPosition(mPosition[0], mPosition[1], mPosition[2]);
245 
246 }
247 
248 void GraphicalDisk::setRadiusBySlicingSphere(double sphereRadius, double sliceHeight)
249 {
250  double r = this->getRadiusOfCircleSlicedFromSphere(sphereRadius, sliceHeight);
251  this->setRadius(r);
252 }
253 
254 double GraphicalDisk::getRadiusOfCircleSlicedFromSphere(double sphereRadius, double sliceHeight) const
255 {
256  double retval = 0;
257 
258  if (std::abs(sliceHeight) > sphereRadius)
259  {
260  retval = 0;
261  }
262  else
263  {
264  retval = sphereRadius*cos(asin(sliceHeight/sphereRadius));
265  }
266 
267  return retval;
268 }
269 
270 
271 } // namespace cx
272 
273 
void setRadius(double radius)
Vector3D getColorAsVector3D(QColor color)
Scalar * begin()
void setColor(QColor color)
vtkSmartPointer< class vtkPolyDataMapper > vtkPolyDataMapperPtr
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
void setLighting(bool on)
void setPosition(Vector3D pos)
void setRenderer(vtkRendererPtr renderer=vtkRendererPtr())
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:62
void setOutlineWidth(double width)
void setHeight(double height)
Transform3D createTransformIJC(const Vector3D &ivec, const Vector3D &jvec, const Vector3D &center)
vtkSmartPointer< class vtkRenderer > vtkRendererPtr
vtkSmartPointer< class vtkPolyDataNormals > vtkPolyDataNormalsPtr
void setOutlineColor(QColor color)
void setRadiusBySlicingSphere(double sphereRadius, double sliceHeight)
void setDirection(Vector3D direction)
Transform3D createTransformTranslate(const Vector3D &translation)
double dot(const Vector3D &a, const Vector3D &b)
compute inner product (or dot product) of a and b.
Definition: cxVector3D.cpp:67
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
void setFillVisible(bool val)