Fraxinus  17.12
An IGT application
cxGraphicalObjectWithDirection.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 
34 #include "vtkMatrix4x4.h"
35 #include "cxVtkHelperClasses.h"
36 #include <vtkActor.h>
37 #include <vtkPolyDataMapper.h>
38 #include <vtkSuperquadricSource.h>
39 #include <vtkRenderer.h>
40 
41 
42 namespace cx
43 {
44 
46 {
47  mPoint = Vector3D(0,0,0);
48  mDirection = Vector3D(0,1,0);
49  mVectorUp = Vector3D(1,0,0);
50  mScale = Vector3D(1,1,1);
51  mSource = vtkSuperquadricSourcePtr::New();
52 
53  mMapper = vtkPolyDataMapperPtr::New();
54  mMapper->SetInputConnection(mSource->GetOutputPort());
55 
56  mActor = vtkActorPtr::New();
57  mActor->SetMapper(mMapper);
58 
59  this->setRenderer(renderer);
60 }
61 
63 {
64  this->setRenderer(NULL);
65 }
66 
68 {
69  return mActor;
70 }
71 
73 {
74  return mSource->GetOutput();
75 }
76 
78 {
79  return mMapper;
80 }
81 
83 {
84  mPoint = point;
85  this->updateOrientation();
86 }
87 
89 {
90  mDirection = direction;
91  this->updateOrientation();
92 }
93 
95 {
96  mVectorUp = up;
97  this->updateOrientation();
98 }
99 
101 {
102  mScale = scale;
103  this->updateOrientation();
104 }
105 
107 {
108  if (mRenderer)
109  mRenderer->RemoveActor(mActor);
110  mRenderer = renderer;
111  if (mRenderer)
112  mRenderer->AddActor(mActor);
113 }
114 
116 {
117  Transform3D R;
118  bool directionAlongUp = similar(dot(mVectorUp, mDirection.normal()), 1.0);
119 
120  if (directionAlongUp)
121  {
122  R = Transform3D::Identity();
123  }
124 
125  else
126  {
127  Vector3D jvec = mDirection.normal();
128  Vector3D kvec = cross(mVectorUp, mDirection).normal();
129  Vector3D ivec = cross(jvec, kvec).normal();
130  Vector3D center = Vector3D::Zero();
131  R = createTransformIJC(ivec, jvec, center);
132  }
133 
136  Transform3D M = T*R*S;
137  mActor->SetUserMatrix(M.getVtkMatrix());
138 }
139 
140 
141 } // namespace cx
vtkSmartPointer< class vtkActor > vtkActorPtr
vtkSmartPointer< class vtkPolyDataMapper > vtkPolyDataMapperPtr
Transform3D createTransformScale(const Vector3D &scale_)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:62
Transform3D createTransformIJC(const Vector3D &ivec, const Vector3D &jvec, const Vector3D &center)
vtkSmartPointer< class vtkRenderer > 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
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
GraphicalObjectWithDirection(vtkRendererPtr renderer=vtkRendererPtr())
Namespace for all CustusX production code.
void setRenderer(vtkRendererPtr renderer=vtkRendererPtr())