forked from dgobbi/ToolCursor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkSpinCameraTool.cxx
132 lines (101 loc) · 3.5 KB
/
vtkSpinCameraTool.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*=========================================================================
Program: ToolCursor
Module: vtkSpinCameraTool.cxx
Copyright (c) 2010 David Gobbi
All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSpinCameraTool.h"
#include "vtkObjectFactory.h"
#include "vtkToolCursor.h"
#include "vtkCamera.h"
#include "vtkRenderer.h"
#include "vtkTransform.h"
#include "vtkMath.h"
#include "vtkVolumePicker.h"
vtkStandardNewMacro(vtkSpinCameraTool);
//----------------------------------------------------------------------------
vtkSpinCameraTool::vtkSpinCameraTool()
{
this->Transform = vtkTransform::New();
}
//----------------------------------------------------------------------------
vtkSpinCameraTool::~vtkSpinCameraTool()
{
this->Transform->Delete();
}
//----------------------------------------------------------------------------
void vtkSpinCameraTool::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
//----------------------------------------------------------------------------
void vtkSpinCameraTool::StartAction()
{
this->Superclass::StartAction();
vtkToolCursor *cursor = this->GetToolCursor();
vtkCamera *camera = cursor->GetRenderer()->GetActiveCamera();
camera->GetViewUp(this->StartCameraViewUp);
this->Transform->Identity();
}
//----------------------------------------------------------------------------
void vtkSpinCameraTool::StopAction()
{
this->Superclass::StopAction();
}
//----------------------------------------------------------------------------
void vtkSpinCameraTool::DoAction()
{
this->Superclass::DoAction();
vtkToolCursor *cursor = this->GetToolCursor();
vtkCamera *camera = cursor->GetRenderer()->GetActiveCamera();
vtkMatrix4x4 *viewMatrix = camera->GetViewTransformMatrix();
// Get the camera's x, y, and z axes
double cvx[3], cvy[3], cvz[3];
for (int i = 0; i < 3; i++)
{
cvx[i] = viewMatrix->GetElement(0, i);
cvy[i] = viewMatrix->GetElement(1, i);
cvz[i] = viewMatrix->GetElement(2, i);
}
double f[3];
camera->GetFocalPoint(f);
// Get the initial point.
double p0[3];
this->GetStartPosition(p0);
// Get the display position.
double x, y;
this->GetDisplayPosition(x, y);
// Get any point along the view ray.
double p[3];
this->DisplayToWorld(x, y, 0.5, p);
// Find positions relative to focal point
double u[3];
u[0] = p0[0] - f[0];
u[1] = p0[1] - f[1];
u[2] = p0[2] - f[2];
double v[3];
v[0] = p[0] - f[0];
v[1] = p[1] - f[1];
v[2] = p[2] - f[2];
// Convert to camera coords, centered on focal point
double cx0 = vtkMath::Dot(u, cvx);
double cy0 = vtkMath::Dot(u, cvy);
double cr0 = sqrt(cx0*cx0 + cy0*cy0);
double cx = vtkMath::Dot(v, cvx);
double cy = vtkMath::Dot(v, cvy);
double cr = sqrt(cx*cx + cy*cy);
// Find the rotation angle
double costheta = (cx0*cx + cy0*cy)/(cr0*cr);
double sintheta = (cx0*cy - cy0*cx)/(cr0*cr);
double theta = atan2(sintheta, costheta);
// Increment by the new rotation
this->Transform->PostMultiply();
this->Transform->RotateWXYZ(vtkMath::DegreesFromRadians(-theta), cvz);
// Apply the transform
double cameraViewUp[3];
this->Transform->TransformVector(this->StartCameraViewUp, cameraViewUp);
camera->SetViewUp(cameraViewUp);
}