Blame view

tira/math/quaternion.h 4.27 KB
ce6381d7   David Mayerich   updating to TIRA
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
197
198
199
200
201
202
203
204
205
206
207
208
  #ifndef TIRA_QUATERNION_H
  #define TIRA_QUATERNION_H
  
  #include <stim/math/matrix_sq.h>
  #include <stim/cuda/cudatools/callable.h>
  
  namespace tira {
  
  template<typename T>
  class quaternion
  {
  public:
  	T w;
  	T x;
  	T y;
  	T z;
  
  	CUDA_CALLABLE void normalize(){
  
  		double length=sqrt(w*w + x*x + y*y + z*z);
  		w=w/length;
  		x=x/length;
  		y=y/length;
  		z=z/length;
  	}
  
  	//calculate the quaternion length (norm)
  	CUDA_CALLABLE T norm() {
  		return sqrt(w*w + x * x + y * y + z * z);
  	}
  
  	CUDA_CALLABLE void CreateRotation(T theta, T ux, T uy, T uz){
  
  		vec3<T> u(ux, uy, uz);
  		CreateRotation(theta, u);		
  	}
  
  	CUDA_CALLABLE void CreateRotation(T theta, vec3<T> u){
  
  		vec3<T> u_hat = u.norm();
  
  		//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);
  	}
  
  	CUDA_CALLABLE void CreateRotation(vec3<T> from, vec3<T> to){
  
  		from = from.norm();
  		to = to.norm();
  		vec3<T> r = from.cross(to);			//compute the rotation vector
  		//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
  		//deal with a zero vector (both k and kn point in the same direction)
  		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
  		if(theta == (T)0){
  			return;
  		}
  
  		//create a quaternion to capture the rotation
  		CreateRotation(theta, r.norm());
  	}
  
  
  
  	CUDA_CALLABLE quaternion<T> operator *(quaternion<T> &param){
  
  		float A, B, C, D, E, F, G, H;
  
  
  		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);
  
  		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;
  
  		return result;
  	}
  	
  	CUDA_CALLABLE matrix_sq<T, 3> toMatrix3(){
  
  		matrix_sq<T, 3> result;
  
  
  	    T wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
  
  
  	    // 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;
  
  		result(0, 0) = 1 - (yy + zz);
  		result(0, 1) = xy - wz;
  
  		result(0, 2) = xz + wy;
  
  		result(1, 0) = xy + wz;
  		result(1, 1) = 1 - (xx + zz);
  
  		result(1, 2) = yz - wx;
  
  		result(2, 0) = xz - wy;
  		result(2, 1) = yz + wx;
  
  		result(2, 2) = 1 - (xx + yy);
  
  		return result;
  	}
  
  	CUDA_CALLABLE matrix_sq<T, 4> toMatrix4(){
  		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;
  
  	    // 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;
  
  		result(0, 0) = 1 - (yy + zz);
  		result(0, 1) = xy - wz;
  
  		result(0, 2) = xz + wy;
  
  		result(1, 0) = xy + wz;
  		result(1, 1) = 1 - (xx + zz);
  
  		result(1, 2) = yz - wx;
  
  		result(2, 0) = xz - wy;
  		result(2, 1) = yz + wx;
  
  		result(2, 2) = 1 - (xx + yy);
  
  		result(3, 3) = 1;*/
  
  		return R;
  	}
  
  
  	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;
  	}
  
  	// 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;
  	}
  
  };
  
  }	//end rts namespace
  
  
  #endif