23 #ifndef _deQuaternionf_h
24 #define _deQuaternionf_h
35 extern void deColumnV3Q4S1(deFloat* res,
const deFloat* q1,
const int col);
36 extern void deConsistentSignQ4Q4Q4(deFloat* res,
const deFloat* q1,
const deFloat* q2);
37 extern void deSlerpQ4Q4Q4S2(deFloat* res,
const deFloat* q1,
const deFloat* q2,
const deFloat t,
const deFloat addedSpins);
38 extern void deAxisAngleV3S1Q4(deFloat* axis, deFloat *angle,
const deFloat* q1);
39 extern void deSetQ4zyxV3(deFloat* res,
const deFloat *v);
43 DE_MATH_API
void deSetQ4S4(deFloat* res,
const deFloat x,
const deFloat y,
const deFloat z,
const deFloat w)
45 res[0] = x; res[1] = y; res[2] = z; res[3] = w;
48 DE_MATH_API
void deIdentityQ4(deFloat* res)
50 res[0] = res[1] = res[2] = 0; res[3] = 1;
53 DE_MATH_API
void deZeroQ4(deFloat* res)
55 res[0] = res[1] = res[2] = res[3] = 0;
58 DE_MATH_API
void deNegateQ4Q4(deFloat* res,
const deFloat* q1)
66 DE_MATH_API
void deInvertQ4Q4(deFloat* res,
const deFloat* q1)
74 DE_MATH_API
void deSetQ4Q4(deFloat* res,
const deFloat* q1)
82 DE_MATH_API
void deAddQ4Q4Q4(deFloat* res,
const deFloat* q1,
const deFloat* q2)
84 res[0] = q1[0] + q2[0];
85 res[1] = q1[1] + q2[1];
86 res[2] = q1[2] + q2[2];
87 res[3] = q1[3] + q2[3];
90 DE_MATH_API
void deSubQ4Q4Q4(deFloat* res,
const deFloat* q1,
const deFloat* q2)
92 res[0] = q1[0] - q2[0];
93 res[1] = q1[1] - q2[1];
94 res[2] = q1[2] - q2[2];
95 res[3] = q1[3] - q2[3];
98 DE_MATH_API deFloat deDotQ4Q4(
const deFloat* q1,
const deFloat* q2)
100 return (q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2] + q1[3] * q2[3]);
104 DE_MATH_API
void deMulQ4Q4Q4(deFloat* res,
const deFloat* q1,
const deFloat* q2)
106 res[0] = q1[0] * q2[3] + q2[0] * q1[3] + (q1[1] * q2[2] - q1[2] * q2[1]);
107 res[1] = q1[1] * q2[3] + q2[1] * q1[3] + (q1[2] * q2[0] - q1[0] * q2[2]);
108 res[2] = q1[2] * q2[3] + q2[2] * q1[3] + (q1[0] * q2[1] - q1[1] * q2[0]);
109 res[3] = q1[3] * q2[3] - (q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2]);
112 DE_MATH_API
void deMulQ4Q4iQ4(deFloat* res,
const deFloat* q1,
const deFloat* q2)
115 res[0] = -q1[0] * q2[3] + q2[0] * q1[3] - (q1[1] * q2[2] - q1[2] * q2[1]);
116 res[1] = -q1[1] * q2[3] + q2[1] * q1[3] - (q1[2] * q2[0] - q1[0] * q2[2]);
117 res[2] = -q1[2] * q2[3] + q2[2] * q1[3] - (q1[0] * q2[1] - q1[1] * q2[0]);
118 res[3] = q1[3] * q2[3] + (q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2]);
121 DE_MATH_API
void deMulQ4Q4Q4i(deFloat* res,
const deFloat* q1,
const deFloat* q2)
124 res[0] = q1[0] * q2[3] - q2[0] * q1[3] - (q1[1] * q2[2] - q1[2] * q2[1]);
125 res[1] = q1[1] * q2[3] - q2[1] * q1[3] - (q1[2] * q2[0] - q1[0] * q2[2]);
126 res[2] = q1[2] * q2[3] - q2[2] * q1[3] - (q1[0] * q2[1] - q1[1] * q2[0]);
127 res[3] = q1[3] * q2[3] + (q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2]);
130 DE_MATH_API
void deMulQ4S1(deFloat* res,
const deFloat s)
138 DE_MATH_API
void deAddQ4Q4(deFloat* res,
const deFloat* q1)
146 DE_MATH_API
void deSubQ4Q4(deFloat* res,
const deFloat* q1)
166 DE_MATH_API
void deMulV3Q4V3(deFloat* res,
const deFloat* q1,
const deFloat* v2)
168 deFloat m11 = q1[3] * q1[3] - (q1[0] * q1[0] + q1[1] * q1[1] + q1[2] * q1[2]);
169 deFloat m12 = 2 * (q1[0] * v2[0] + q1[1] * v2[1] + q1[2] * v2[2]);
170 deFloat w2 = q1[3] + q1[3];
172 res[0] = q1[0] * m12 + v2[0] * m11 + (q1[1] * v2[2] - q1[2] * v2[1]) * w2;
173 res[1] = q1[1] * m12 + v2[1] * m11 + (q1[2] * v2[0] - q1[0] * v2[2]) * w2;
174 res[2] = q1[2] * m12 + v2[2] * m11 + (q1[0] * v2[1] - q1[1] * v2[0]) * w2;
181 DE_MATH_API
void deMulV3Q4iV3(deFloat* res,
const deFloat* q1,
const deFloat* v2)
184 deFloat m11 = q1[3] * q1[3] - (q1[0] * q1[0] + q1[1] * q1[1] + q1[2] * q1[2]);
185 deFloat m12 = 2 * (q1[0] * v2[0] + q1[1] * v2[1] + q1[2] * v2[2]);
186 deFloat w2 = -(q1[3] + q1[3]);
188 res[0] = q1[0] * m12 + v2[0] * m11 + (q1[1] * v2[2] - q1[2] * v2[1]) * w2;
189 res[1] = q1[1] * m12 + v2[1] * m11 + (q1[2] * v2[0] - q1[0] * v2[2]) * w2;
190 res[2] = q1[2] * m12 + v2[2] * m11 + (q1[0] * v2[1] - q1[1] * v2[0]) * w2;
193 DE_MATH_API
int deIsEqualQ4Q4(
const deFloat* q1,
const deFloat* q2)
195 deFloat mag2 = q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2] + q1[3] * q2[3];
196 return (mag2 > DE_QUATERNION_COS_THRESHHOLD || mag2 < -DE_QUATERNION_COS_THRESHHOLD);
200 DE_MATH_API
void deNormalizeQ4(deFloat* res)
202 deFloat mag = 1 / deSqrt(res[0] * res[0] + res[1] * res[1] + res[2] * res[2] + res[3] * res[3]);
210 DE_MATH_API
void deSetQ4V3S1(deFloat* res,
const deFloat* axis,
const deFloat angle)
212 deFloat s = deSin(0.5 * angle);
213 res[0] = axis[0] * s;
214 res[1] = axis[1] * s;
215 res[2] = axis[2] * s;
216 res[3] = deCos(0.5 * angle);
220 DE_MATH_API
void deSetQ4S2(deFloat* res,
const int axis,
const deFloat angle)
223 res[0] = res[1] = res[2] = 0;
224 res[axis] = deSin(0.5 * angle);
225 res[3] = deCos(0.5 * angle);
243 DE_MATH_API
void deAngularErrorV3Q4Q4(deFloat* res,
const deFloat* q1,
const deFloat* q2)
245 res[0] = -2 * ( q1[3] * q2[0] - q1[2] * q2[1] + q1[1] * q2[2] - q1[0] * q2[3]);
246 res[1] = -2 * ( q1[2] * q2[0] + q1[3] * q2[1] - q1[0] * q2[2] - q1[1] * q2[3]);
247 res[2] = -2 * (-q1[1] * q2[0] + q1[0] * q2[1] + q1[3] * q2[2] - q1[2] * q2[3]);
254 DE_MATH_API
void deVelocityQ4Q4V3(deFloat* res,
const deFloat* q1,
const deFloat* v2)
256 res[0] = (deFloat)0.5 * ( q1[3] * v2[0] + q1[2] * v2[1] - q1[1] * v2[2]);
257 res[1] = (deFloat)0.5 * (-q1[2] * v2[0] + q1[3] * v2[1] + q1[0] * v2[2]);
258 res[2] = (deFloat)0.5 * ( q1[1] * v2[0] - q1[0] * v2[1] + q1[3] * v2[2]);
259 res[3] = (deFloat)0.5 * (-q1[0] * v2[0] - q1[1] * v2[1] - q1[2] * v2[2]);
265 DE_MATH_API
void deLerpQ4Q4Q4S1(deFloat* res,
const deFloat* q1,
const deFloat* q2,
const deFloat t)
267 res[0] = q1[0] + t * (q2[0] - q1[0]);
268 res[1] = q1[1] + t * (q2[1] - q1[1]);
269 res[2] = q1[2] + t * (q2[2] - q1[2]);
270 res[3] = q1[3] + t * (q2[3] - q1[3]);
276 DE_MATH_API
void deSetV3Q4zyx(deFloat* res,
const deFloat* q)
278 deFloat sqx = q[0] * q[0];
279 deFloat sqy = q[1] * q[1];
280 deFloat sqz = q[2] * q[2];
281 deFloat sqw = q[3] * q[3];
283 res[0] = deAtan2(2 * (q[1] * q[2] + q[0] * q[3]), (-sqx - sqy + sqz + sqw));
284 res[1] = deAsin(-2 * (q[0] * q[2] - q[1] * q[3]));
285 res[2] = deAtan2(2 * (q[0] * q[1] + q[2] * q[3]), (sqx - sqy - sqz + sqw));