Fraxinus  16.5.0-fx-rc4
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxGraphicalTorus3D.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 "cxGraphicalTorus3D.h"
34 
35 #include <QColor>
36 #include "cxVtkHelperClasses.h"
37 #include "vtkSuperquadricSource.h"
38 
39 #include <vtkPolyDataMapper.h>
40 #include <vtkProperty.h>
41 #include <vtkRenderer.h>
42 #include <vtkRenderWindow.h>
43 #include "cxTypeConversions.h"
44 #include "cxGraphicalPrimitives.h"
45 #include "vtkMatrix4x4.h"
46 #include <vtkActor.h>
47 #include <vtkProperty.h>
48 
49 namespace cx
50 {
51 
53 {
54  mPoint = Vector3D(0,0,0);
55  mDirection = Vector3D(0,1,0);
56 
57  source = vtkSuperquadricSourcePtr::New();
58  source->SetToroidal(true);
59  source->SetSize(10);
60  source->SetThickness(0.5);
61  source->SetThetaResolution(source->GetThetaResolution()*2);
62 
63  mapper = vtkPolyDataMapperPtr::New();
64  mapper->SetInputConnection(source->GetOutputPort());
65 
66  actor = vtkActorPtr::New();
67  actor->SetMapper(mapper);
68 
69  this->setRenderer(renderer);
70 }
71 
73 {
74  this->setRenderer(NULL);
75 }
76 
78 {
79  if (mRenderer)
80  mRenderer->RemoveActor(actor);
81  mRenderer = renderer;
82  if (mRenderer)
83  mRenderer->AddActor(actor);
84 }
85 
86 void GraphicalTorus3D::setRadius(double value)
87 {
88  source->SetSize(value);
89 }
90 
92 {
93  source->SetThickness(value);
94 }
95 
96 void GraphicalTorus3D::setColor(QColor color)
97 {
98  setColorAndOpacity(actor->GetProperty(), color);
99 }
100 
102 {
103  mPoint = point;
104  this->updateOrientation();
105 }
106 
108 {
109  mDirection = direction;
110 
111  this->updateOrientation();
112 }
113 
114 void GraphicalTorus3D::updateOrientation()
115 {
116 // Transform3D M = createTransformRotationBetweenVectors(Vector3D::UnitY(), mDirection.normal());
117  Transform3D M;
118  bool directionAlongYAxis = similar(dot(Vector3D::UnitY(), mDirection.normal()), 1.0);
119 
120  if (directionAlongYAxis)
121  {
122  M = Transform3D::Identity();
123  }
124  else
125  {
126  Vector3D ivec = cross(Vector3D::UnitY(), mDirection).normal();
127  Vector3D jvec = mDirection.normal();
128  Vector3D center = Vector3D::Zero();
129  M = createTransformIJC(ivec, jvec, center);
130  }
131 
133  M = T*M;
134 
135 // std::cout << "M end:\n" << M << std::endl;
137  actor->SetUserMatrix(M.getVtkMatrix());
138 }
139 
141 {
142  return actor;
143 }
144 
146 {
147  return source->GetOutput();
148 }
149 
150 
151 } // namespace cx
152 
vtkSmartPointer< class vtkActor > vtkActorPtr
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
void setThickness(double radius)
vtkPolyDataPtr getPolyData()
void setDirection(Vector3D direction)
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 setRenderer(vtkRendererPtr renderer=vtkRendererPtr())
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
Transform3D createTransformIJC(const Vector3D &ivec, const Vector3D &jvec, const Vector3D &center)
vtkSmartPointer< class vtkRenderer > vtkRendererPtr
GraphicalTorus3D(vtkRendererPtr renderer=vtkRendererPtr())
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 setRadius(double value)
void setColor(QColor color)
void setPosition(Vector3D point)
void setColorAndOpacity(vtkPropertyPtr property, QColor color)