forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkOpenVROverlayInternal.h
196 lines (169 loc) · 5.67 KB
/
vtkOpenVROverlayInternal.h
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*=========================================================================
Program: Visualization Toolkit
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
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 "vtkVectorOperators.h"
class vtkOpenVRCameraPose
{
public:
double Position[3];
double PhysicalViewUp[3];
double PhysicalViewDirection[3];
double ViewDirection[3];
double Translation[3];
double Distance;
double MotionFactor = 1.0;
bool Loaded = false;
// return a vector based on in that is orthogonal to normal
vtkVector3d SanitizeVector(vtkVector3d& in, vtkVector3d& normal) {
vtkVector3d result;
if (fabs(in.Dot(normal)) > 0.999) // some epsilon
{
if (fabs(normal[0]) < 0.1)
{
result.Set(1.0,0.0,0.0);
}
else
{
result.Set(0.0,1.0,0.0);
}
}
else
{
result = in - (in.Dot(normal))*normal;
result.Normalize();
}
return result;
};
void Set(vtkOpenVRCamera *cam, vtkOpenVRRenderWindow *win) {
win->GetPhysicalTranslation(this->Translation);
win->GetPhysicalViewUp(this->PhysicalViewUp);
this->Distance = win->GetPhysicalScale();
vtkInteractorStyle3D *is =
static_cast<vtkInteractorStyle3D *>(win->GetInteractor()->GetInteractorStyle());
this->MotionFactor = is->GetDollyMotionFactor();
cam->GetPosition(this->Position);
win->GetPhysicalViewDirection(this->PhysicalViewDirection);
cam->GetDirectionOfProjection(this->ViewDirection);
this->Loaded = true;
};
void Apply(vtkOpenVRCamera *cam, vtkOpenVRRenderWindow *win) {
// s = saved values
vtkVector3d svup(this->PhysicalViewUp);
vtkVector3d svdir(this->ViewDirection);
vtkVector3d strans(this->Translation);
vtkVector3d spos(this->Position);
double sdistance = this->Distance;
// c = current values
vtkVector3d cvup;
win->GetPhysicalViewUp(cvup.GetData());
vtkVector3d cpos;
cam->GetPosition(cpos.GetData());
vtkVector3d ctrans;
win->GetPhysicalTranslation(ctrans.GetData());
vtkVector3d cvdir;
cam->GetDirectionOfProjection(cvdir.GetData());
vtkVector3d civdir;
win->GetPhysicalViewDirection(civdir.GetData());
double cdistance = win->GetPhysicalScale();
// n = new values
vtkVector3d nvup = svup;
win->SetPhysicalViewUp(nvup.GetData());
// sanitize the svdir, must be orthogonal to nvup
svdir = this->SanitizeVector(svdir, nvup);
// make sure cvdir and civdir are orthogonal to our nvup
cvdir = this->SanitizeVector(cvdir, nvup);
civdir = this->SanitizeVector(civdir, nvup);
vtkVector3d civright = civdir.Cross(nvup);
// find the new initialvdir
vtkVector3d nivdir;
double theta = acos(svdir.Dot(cvdir));
if (nvup.Dot(cvdir.Cross(svdir)) < 0.0)
{
theta = -theta;
}
// rotate civdir by theta
nivdir = civdir*cos(theta) - civright*sin(theta);
win->SetPhysicalViewDirection(nivdir.GetData());
vtkVector3d nivright = nivdir.Cross(nvup);
// adjust translation so that we are in the same spot
// as when the camera was saved
vtkVector3d ntrans;
vtkVector3d cppwc;
cppwc = cpos + ctrans;
double x = cppwc.Dot(civdir)/cdistance;
double y = cppwc.Dot(civright)/cdistance;
ntrans =
strans*nvup +
nivdir*(x*sdistance - spos.Dot(nivdir)) +
nivright*(y*sdistance - spos.Dot(nivright));
win->SetPhysicalTranslation(ntrans.GetData());
cam->SetPosition(cpos.GetData());
// this really only sets the distance as the render loop
// sets focal point and position every frame
vtkVector3d nfp;
nfp = cpos + nivdir*sdistance;
cam->SetFocalPoint(nfp.GetData());
win->SetPhysicalScale(sdistance);
#if 0
win->SetPhysicalViewDirection(this->PhysicalViewDirection);
cam->SetTranslation(this->Translation);
cam->SetFocalPoint(this->FocalPoint);
cam->SetPosition(
this->FocalPoint[0] - this->PhysicalViewDirection[0]*this->Distance,
this->FocalPoint[1] - this->PhysicalViewDirection[1]*this->Distance,
this->FocalPoint[2] - this->PhysicalViewDirection[2]*this->Distance);
#endif
win->SetPhysicalViewUp(this->PhysicalViewUp);
vtkInteractorStyle3D *is =
static_cast<vtkInteractorStyle3D *>(win->GetInteractor()->GetInteractorStyle());
is->SetDollyMotionFactor(this->MotionFactor);
}
};
class vtkOpenVROverlaySpot
{
public:
vtkOpenVROverlaySpot(int x1, int x2, int y1, int y2, vtkCommand *cb) {
this->xmin = x1;
this->xmax = x2;
this->ymin = y1;
this->ymax = y2;
this->Callback = cb;
cb->Register(nullptr);
this->Active = false;
}
~vtkOpenVROverlaySpot() {
if (this->Callback)
{
this->Callback->Delete();
this->Callback = nullptr;
}
}
bool Active;
int xmin;
int xmax;
int ymin;
int ymax;
vtkCommand *Callback;
std::string Group;
int GroupId;
vtkOpenVROverlaySpot(const vtkOpenVROverlaySpot &in) {
this->xmin = in.xmin;
this->xmax = in.xmax;
this->ymin = in.ymin;
this->ymax = in.ymax;
this->Callback = in.Callback;
this->Callback->Register(0);
this->Active = in.Active;
this->Group = in.Group;
this->GroupId = in.GroupId;
}
vtkOpenVROverlaySpot& operator=(const vtkOpenVROverlaySpot&) = delete;
};
//****************************************************************************
// VTK-HeaderTest-Exclude: vtkOpenVROverlayInternal.h