57729e5b
David Mayerich
changed directory...
|
1
2
3
|
#ifndef RTS_QUATERNION_H
#define RTS_QUATERNION_H
|
6c4afcac
David Mayerich
introduced a gene...
|
4
|
#include <stim/math/matrix_sq.h>
|
7d1d153a
Pavel Govyadinov
fixed the include...
|
5
|
#include <stim/cuda/cudatools/callable.h>
|
d609550e
David Mayerich
fixed bug in plan...
|
6
|
|
8a86bd56
David Mayerich
changed rts names...
|
7
|
namespace stim{
|
57729e5b
David Mayerich
changed directory...
|
8
9
10
11
12
13
14
15
16
17
|
template<typename T>
class quaternion
{
public:
T w;
T x;
T y;
T z;
|
d609550e
David Mayerich
fixed bug in plan...
|
18
|
CUDA_CALLABLE void normalize(){
|
57729e5b
David Mayerich
changed directory...
|
19
|
|
d609550e
David Mayerich
fixed bug in plan...
|
20
21
22
23
24
25
|
double length=sqrt(w*w + x*x + y*y + z*z);
w=w/length;
x=x/length;
y=y/length;
z=z/length;
}
|
57729e5b
David Mayerich
changed directory...
|
26
|
|
c685653d
David Mayerich
fixed a bug in th...
|
27
28
29
30
31
|
//calculate the quaternion length (norm)
CUDA_CALLABLE T norm() {
return sqrt(w*w + x * x + y * y + z * z);
}
|
d609550e
David Mayerich
fixed bug in plan...
|
32
|
CUDA_CALLABLE void CreateRotation(T theta, T ux, T uy, T uz){
|
57729e5b
David Mayerich
changed directory...
|
33
|
|
8e4f8364
David Mayerich
started a new opt...
|
34
|
vec3<T> u(ux, uy, uz);
|
d609550e
David Mayerich
fixed bug in plan...
|
35
36
|
CreateRotation(theta, u);
}
|
57729e5b
David Mayerich
changed directory...
|
37
|
|
8e4f8364
David Mayerich
started a new opt...
|
38
|
CUDA_CALLABLE void CreateRotation(T theta, vec3<T> u){
|
57729e5b
David Mayerich
changed directory...
|
39
|
|
8e4f8364
David Mayerich
started a new opt...
|
40
|
vec3<T> u_hat = u.norm();
|
8d4f0940
David Mayerich
ERROR in plane wa...
|
41
|
|
d609550e
David Mayerich
fixed bug in plan...
|
42
43
44
45
46
47
|
//assign the given Euler rotation to this quaternion
w = (T)cos(theta/2);
x = u_hat[0]*(T)sin(theta/2);
y = u_hat[1]*(T)sin(theta/2);
z = u_hat[2]*(T)sin(theta/2);
}
|
57729e5b
David Mayerich
changed directory...
|
48
|
|
8e4f8364
David Mayerich
started a new opt...
|
49
|
CUDA_CALLABLE void CreateRotation(vec3<T> from, vec3<T> to){
|
8d4f0940
David Mayerich
ERROR in plane wa...
|
50
|
|
9339fbad
David Mayerich
implementing mie ...
|
51
52
|
from = from.norm();
to = to.norm();
|
8e4f8364
David Mayerich
started a new opt...
|
53
|
vec3<T> r = from.cross(to); //compute the rotation vector
|
c685653d
David Mayerich
fixed a bug in th...
|
54
55
56
|
//T l = r.len();
//if (l > 1) l = 1; //we have seen degenerate cases where |r| > 1 (probably due to loss of precision in the cross product)
//T theta = asin(l); //compute the angle of the rotation about r
|
d609550e
David Mayerich
fixed bug in plan...
|
57
|
//deal with a zero vector (both k and kn point in the same direction)
|
c685653d
David Mayerich
fixed a bug in th...
|
58
59
|
T cos_theta = from.dot(to); //calculate the cosine between the two vectors
T theta = acos(cos_theta); //calculate the angle between the two vectors
|
591f04ec
Pavel Govyadinov
fixed a very subt...
|
60
|
if(theta == (T)0){
|
d609550e
David Mayerich
fixed bug in plan...
|
61
|
return;
|
591f04ec
Pavel Govyadinov
fixed a very subt...
|
62
|
}
|
57729e5b
David Mayerich
changed directory...
|
63
|
|
d609550e
David Mayerich
fixed bug in plan...
|
64
65
66
|
//create a quaternion to capture the rotation
CreateRotation(theta, r.norm());
}
|
57729e5b
David Mayerich
changed directory...
|
67
68
|
|
57729e5b
David Mayerich
changed directory...
|
69
|
|
d609550e
David Mayerich
fixed bug in plan...
|
70
|
CUDA_CALLABLE quaternion<T> operator *(quaternion<T> ¶m){
|
57729e5b
David Mayerich
changed directory...
|
71
|
|
d609550e
David Mayerich
fixed bug in plan...
|
72
|
float A, B, C, D, E, F, G, H;
|
57729e5b
David Mayerich
changed directory...
|
73
|
|
57729e5b
David Mayerich
changed directory...
|
74
|
|
d609550e
David Mayerich
fixed bug in plan...
|
75
76
77
78
79
80
81
82
|
A = (w + x)*(param.w + param.x);
B = (z - y)*(param.y - param.z);
C = (w - x)*(param.y + param.z);
D = (y + z)*(param.w - param.x);
E = (x + z)*(param.x + param.y);
F = (x - z)*(param.x - param.y);
G = (w + y)*(param.w - param.z);
H = (w - y)*(param.w + param.z);
|
57729e5b
David Mayerich
changed directory...
|
83
|
|
d609550e
David Mayerich
fixed bug in plan...
|
84
85
86
87
88
|
quaternion<T> result;
result.w = B + (-E - F + G + H) /2;
result.x = A - (E + F + G + H)/2;
result.y = C + (E - F + G - H)/2;
result.z = D + (E - F - G + H)/2;
|
57729e5b
David Mayerich
changed directory...
|
89
|
|
d609550e
David Mayerich
fixed bug in plan...
|
90
91
92
|
return result;
}
|
6c4afcac
David Mayerich
introduced a gene...
|
93
|
CUDA_CALLABLE matrix_sq<T, 3> toMatrix3(){
|
57729e5b
David Mayerich
changed directory...
|
94
|
|
6c4afcac
David Mayerich
introduced a gene...
|
95
|
matrix_sq<T, 3> result;
|
57729e5b
David Mayerich
changed directory...
|
96
|
|
57729e5b
David Mayerich
changed directory...
|
97
|
|
d609550e
David Mayerich
fixed bug in plan...
|
98
|
T wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
|
57729e5b
David Mayerich
changed directory...
|
99
|
|
57729e5b
David Mayerich
changed directory...
|
100
|
|
d609550e
David Mayerich
fixed bug in plan...
|
101
102
103
104
105
106
|
// calculate coefficients
x2 = x + x; y2 = y + y;
z2 = z + z;
xx = x * x2; xy = x * y2; xz = x * z2;
yy = y * y2; yz = y * z2; zz = z * z2;
wx = w * x2; wy = w * y2; wz = w * z2;
|
57729e5b
David Mayerich
changed directory...
|
107
|
|
d609550e
David Mayerich
fixed bug in plan...
|
108
109
|
result(0, 0) = 1 - (yy + zz);
result(0, 1) = xy - wz;
|
57729e5b
David Mayerich
changed directory...
|
110
|
|
d609550e
David Mayerich
fixed bug in plan...
|
111
|
result(0, 2) = xz + wy;
|
57729e5b
David Mayerich
changed directory...
|
112
|
|
d609550e
David Mayerich
fixed bug in plan...
|
113
114
|
result(1, 0) = xy + wz;
result(1, 1) = 1 - (xx + zz);
|
57729e5b
David Mayerich
changed directory...
|
115
|
|
d609550e
David Mayerich
fixed bug in plan...
|
116
|
result(1, 2) = yz - wx;
|
57729e5b
David Mayerich
changed directory...
|
117
|
|
d609550e
David Mayerich
fixed bug in plan...
|
118
119
|
result(2, 0) = xz - wy;
result(2, 1) = yz + wx;
|
57729e5b
David Mayerich
changed directory...
|
120
|
|
d609550e
David Mayerich
fixed bug in plan...
|
121
|
result(2, 2) = 1 - (xx + yy);
|
57729e5b
David Mayerich
changed directory...
|
122
|
|
d609550e
David Mayerich
fixed bug in plan...
|
123
124
|
return result;
}
|
57729e5b
David Mayerich
changed directory...
|
125
|
|
6c4afcac
David Mayerich
introduced a gene...
|
126
|
CUDA_CALLABLE matrix_sq<T, 4> toMatrix4(){
|
c685653d
David Mayerich
fixed a bug in th...
|
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
|
matrix_sq<T, 4> R;
T s, wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
s = sqrt(norm());
xx = x * x; xy = x * y; xz = x * z;
yy = y * y; yz = y * z;
zz = z * z;
wx = w * x; wy = w * y; wz = w * z;
R(0, 0) = 1 - 2 * s * (yy + zz);
R(0, 1) = 2 * s * (xy - wz);
R(0, 2) = 2 * s * (xz + wy);
R(1, 0) = 2 * s * (xy + wz);
R(1, 1) = 1 - 2 * s * (xx + zz);
R(1, 2) = 2 * s * (yz - wx);
R(2, 0) = 2 * s * (xz - wy);
R(2, 1) = 2 * s * (yz + wx);
R(2, 2) = 1 - 2 * s * (xx + yy);
R(0, 3) = 0;
R(1, 3) = 0;
R(2, 3) = 0;
R(3, 0) = 0;
R(3, 1) = 0;
R(3, 2) = 0;
R(3, 3) = 1;
|
57729e5b
David Mayerich
changed directory...
|
152
|
|
d609550e
David Mayerich
fixed bug in plan...
|
153
|
// calculate coefficients
|
c685653d
David Mayerich
fixed a bug in th...
|
154
|
/*x2 = x + x; y2 = y + y;
|
d609550e
David Mayerich
fixed bug in plan...
|
155
156
157
158
|
z2 = z + z;
xx = x * x2; xy = x * y2; xz = x * z2;
yy = y * y2; yz = y * z2; zz = z * z2;
wx = w * x2; wy = w * y2; wz = w * z2;
|
57729e5b
David Mayerich
changed directory...
|
159
|
|
d609550e
David Mayerich
fixed bug in plan...
|
160
161
|
result(0, 0) = 1 - (yy + zz);
result(0, 1) = xy - wz;
|
57729e5b
David Mayerich
changed directory...
|
162
|
|
d609550e
David Mayerich
fixed bug in plan...
|
163
|
result(0, 2) = xz + wy;
|
57729e5b
David Mayerich
changed directory...
|
164
|
|
d609550e
David Mayerich
fixed bug in plan...
|
165
166
|
result(1, 0) = xy + wz;
result(1, 1) = 1 - (xx + zz);
|
57729e5b
David Mayerich
changed directory...
|
167
|
|
d609550e
David Mayerich
fixed bug in plan...
|
168
|
result(1, 2) = yz - wx;
|
57729e5b
David Mayerich
changed directory...
|
169
|
|
d609550e
David Mayerich
fixed bug in plan...
|
170
171
|
result(2, 0) = xz - wy;
result(2, 1) = yz + wx;
|
57729e5b
David Mayerich
changed directory...
|
172
|
|
d609550e
David Mayerich
fixed bug in plan...
|
173
|
result(2, 2) = 1 - (xx + yy);
|
57729e5b
David Mayerich
changed directory...
|
174
|
|
c685653d
David Mayerich
fixed a bug in th...
|
175
|
result(3, 3) = 1;*/
|
57729e5b
David Mayerich
changed directory...
|
176
|
|
c685653d
David Mayerich
fixed a bug in th...
|
177
|
return R;
|
d609550e
David Mayerich
fixed bug in plan...
|
178
179
180
181
182
183
184
185
186
187
188
|
}
CUDA_CALLABLE quaternion(){
w=0; x=0; y=0; z=0;
}
CUDA_CALLABLE quaternion(T c, T i, T j, T k){
w=c; x=i; y=j; z=k;
}
|
c685653d
David Mayerich
fixed a bug in th...
|
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
// create a pure quaternion from a vector
CUDA_CALLABLE quaternion(vec3<T> v){
w = 0; x = v[0]; y = v[1]; z = v[2];
}
CUDA_CALLABLE quaternion<T> conj(){
quaternion<T> c;
c.w = w;
c.x = -x;
c.y = -y;
c.z = -z;
return c;
}
|
d609550e
David Mayerich
fixed bug in plan...
|
203
|
};
|
57729e5b
David Mayerich
changed directory...
|
204
205
206
|
} //end rts namespace
|
57729e5b
David Mayerich
changed directory...
|
207
208
|
#endif
|