Debian Codegraph @

test/Bullet2/vectormath/scalar/quat_aos.h

raw symbols SCIP
1 /*
2 Copyright (C) 2009 Sony Computer Entertainment Inc.
3 All rights reserved.
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14
15 */
16
17 #ifndef _VECTORMATH_QUAT_AOS_CPP_H
18 #define _VECTORMATH_QUAT_AOS_CPP_H
19
20 //-----------------------------------------------------------------------------
21 // Definitions
22
23 #ifndef _VECTORMATH_INTERNAL_FUNCTIONS
24 #define _VECTORMATH_INTERNAL_FUNCTIONS
25
26 #endif
27
28 namespace Vectormath
29 {
30 namespace Aos
31 {
32 inline Quat::Quat(const Quat &quat)
33 {
34 mX = quat.mX;
35 mY = quat.mY;
36 mZ = quat.mZ;
37 mW = quat.mW;
38 }
39
40 inline Quat::Quat(float _x, float _y, float _z, float _w)
41 {
42 mX = _x;
43 mY = _y;
44 mZ = _z;
45 mW = _w;
46 }
47
48 inline Quat::Quat(const Vector3 &xyz, float _w)
49 {
50 this->setXYZ(xyz);
51 this->setW(_w);
52 }
53
54 inline Quat::Quat(const Vector4 &vec)
55 {
56 mX = vec.getX();
57 mY = vec.getY();
58 mZ = vec.getZ();
59 mW = vec.getW();
60 }
61
62 inline Quat::Quat(float scalar)
63 {
64 mX = scalar;
65 mY = scalar;
66 mZ = scalar;
67 mW = scalar;
68 }
69
70 inline const Quat Quat::identity()
71 {
72 return Quat(0.0f, 0.0f, 0.0f, 1.0f);
73 }
74
75 inline const Quat lerp(float t, const Quat &quat0, const Quat &quat1)
76 {
77 return (quat0 + ((quat1 - quat0) * t));
78 }
79
80 inline const Quat slerp(float t, const Quat &unitQuat0, const Quat &unitQuat1)
81 {
82 Quat start;
83 float recipSinAngle, scale0, scale1, cosAngle, angle;
84 cosAngle = dot(unitQuat0, unitQuat1);
85 if (cosAngle < 0.0f)
86 {
87 cosAngle = -cosAngle;
88 start = (-unitQuat0);
89 }
90 else
91 {
92 start = unitQuat0;
93 }
94 if (cosAngle < _VECTORMATH_SLERP_TOL)
95 {
96 angle = acosf(cosAngle);
97 recipSinAngle = (1.0f / sinf(angle));
98 scale0 = (sinf(((1.0f - t) * angle)) * recipSinAngle);
99 scale1 = (sinf((t * angle)) * recipSinAngle);
100 }
101 else
102 {
103 scale0 = (1.0f - t);
104 scale1 = t;
105 }
106 return ((start * scale0) + (unitQuat1 * scale1));
107 }
108
109 inline const Quat squad(float t, const Quat &unitQuat0, const Quat &unitQuat1, const Quat &unitQuat2, const Quat &unitQuat3)
110 {
111 Quat tmp0, tmp1;
112 tmp0 = slerp(t, unitQuat0, unitQuat3);
113 tmp1 = slerp(t, unitQuat1, unitQuat2);
114 return slerp(((2.0f * t) * (1.0f - t)), tmp0, tmp1);
115 }
116
117 inline void loadXYZW(Quat &quat, const float *fptr)
118 {
119 quat = Quat(fptr[0], fptr[1], fptr[2], fptr[3]);
120 }
121
122 inline void storeXYZW(const Quat &quat, float *fptr)
123 {
124 fptr[0] = quat.getX();
125 fptr[1] = quat.getY();
126 fptr[2] = quat.getZ();
127 fptr[3] = quat.getW();
128 }
129
130 inline Quat &Quat::operator=(const Quat &quat)
131 {
132 mX = quat.mX;
133 mY = quat.mY;
134 mZ = quat.mZ;
135 mW = quat.mW;
136 return *this;
137 }
138
139 inline Quat &Quat::setXYZ(const Vector3 &vec)
140 {
141 mX = vec.getX();
142 mY = vec.getY();
143 mZ = vec.getZ();
144 return *this;
145 }
146
147 inline const Vector3 Quat::getXYZ() const
148 {
149 return Vector3(mX, mY, mZ);
150 }
151
152 inline Quat &Quat::setX(float _x)
153 {
154 mX = _x;
155 return *this;
156 }
157
158 inline float Quat::getX() const
159 {
160 return mX;
161 }
162
163 inline Quat &Quat::setY(float _y)
164 {
165 mY = _y;
166 return *this;
167 }
168
169 inline float Quat::getY() const
170 {
171 return mY;
172 }
173
174 inline Quat &Quat::setZ(float _z)
175 {
176 mZ = _z;
177 return *this;
178 }
179
180 inline float Quat::getZ() const
181 {
182 return mZ;
183 }
184
185 inline Quat &Quat::setW(float _w)
186 {
187 mW = _w;
188 return *this;
189 }
190
191 inline float Quat::getW() const
192 {
193 return mW;
194 }
195
196 inline Quat &Quat::setElem(int idx, float value)
197 {
198 *(&mX + idx) = value;
199 return *this;
200 }
201
202 inline float Quat::getElem(int idx) const
203 {
204 return *(&mX + idx);
205 }
206
207 inline float &Quat::operator[](int idx)
208 {
209 return *(&mX + idx);
210 }
211
212 inline float Quat::operator[](int idx) const
213 {
214 return *(&mX + idx);
215 }
216
217 inline const Quat Quat::operator+(const Quat &quat) const
218 {
219 return Quat(
220 (mX + quat.mX),
221 (mY + quat.mY),
222 (mZ + quat.mZ),
223 (mW + quat.mW));
224 }
225
226 inline const Quat Quat::operator-(const Quat &quat) const
227 {
228 return Quat(
229 (mX - quat.mX),
230 (mY - quat.mY),
231 (mZ - quat.mZ),
232 (mW - quat.mW));
233 }
234
235 inline const Quat Quat::operator*(float scalar) const
236 {
237 return Quat(
238 (mX * scalar),
239 (mY * scalar),
240 (mZ * scalar),
241 (mW * scalar));
242 }
243
244 inline Quat &Quat::operator+=(const Quat &quat)
245 {
246 *this = *this + quat;
247 return *this;
248 }
249
250 inline Quat &Quat::operator-=(const Quat &quat)
251 {
252 *this = *this - quat;
253 return *this;
254 }
255
256 inline Quat &Quat::operator*=(float scalar)
257 {
258 *this = *this * scalar;
259 return *this;
260 }
261
262 inline const Quat Quat::operator/(float scalar) const
263 {
264 return Quat(
265 (mX / scalar),
266 (mY / scalar),
267 (mZ / scalar),
268 (mW / scalar));
269 }
270
271 inline Quat &Quat::operator/=(float scalar)
272 {
273 *this = *this / scalar;
274 return *this;
275 }
276
277 inline const Quat Quat::operator-() const
278 {
279 return Quat(
280 -mX,
281 -mY,
282 -mZ,
283 -mW);
284 }
285
286 inline const Quat operator*(float scalar, const Quat &quat)
287 {
288 return quat * scalar;
289 }
290
291 inline float dot(const Quat &quat0, const Quat &quat1)
292 {
293 float result;
294 result = (quat0.getX() * quat1.getX());
295 result = (result + (quat0.getY() * quat1.getY()));
296 result = (result + (quat0.getZ() * quat1.getZ()));
297 result = (result + (quat0.getW() * quat1.getW()));
298 return result;
299 }
300
301 inline float norm(const Quat &quat)
302 {
303 float result;
304 result = (quat.getX() * quat.getX());
305 result = (result + (quat.getY() * quat.getY()));
306 result = (result + (quat.getZ() * quat.getZ()));
307 result = (result + (quat.getW() * quat.getW()));
308 return result;
309 }
310
311 inline float length(const Quat &quat)
312 {
313 return ::sqrtf(norm(quat));
314 }
315
316 inline const Quat normalize(const Quat &quat)
317 {
318 float lenSqr, lenInv;
319 lenSqr = norm(quat);
320 lenInv = (1.0f / sqrtf(lenSqr));
321 return Quat(
322 (quat.getX() * lenInv),
323 (quat.getY() * lenInv),
324 (quat.getZ() * lenInv),
325 (quat.getW() * lenInv));
326 }
327
328 inline const Quat Quat::rotation(const Vector3 &unitVec0, const Vector3 &unitVec1)
329 {
330 float cosHalfAngleX2, recipCosHalfAngleX2;
331 cosHalfAngleX2 = sqrtf((2.0f * (1.0f + dot(unitVec0, unitVec1))));
332 recipCosHalfAngleX2 = (1.0f / cosHalfAngleX2);
333 return Quat((cross(unitVec0, unitVec1) * recipCosHalfAngleX2), (cosHalfAngleX2 * 0.5f));
334 }
335
336 inline const Quat Quat::rotation(float radians, const Vector3 &unitVec)
337 {
338 float s, c, angle;
339 angle = (radians * 0.5f);
340 s = sinf(angle);
341 c = cosf(angle);
342 return Quat((unitVec * s), c);
343 }
344
345 inline const Quat Quat::rotationX(float radians)
346 {
347 float s, c, angle;
348 angle = (radians * 0.5f);
349 s = sinf(angle);
350 c = cosf(angle);
351 return Quat(s, 0.0f, 0.0f, c);
352 }
353
354 inline const Quat Quat::rotationY(float radians)
355 {
356 float s, c, angle;
357 angle = (radians * 0.5f);
358 s = sinf(angle);
359 c = cosf(angle);
360 return Quat(0.0f, s, 0.0f, c);
361 }
362
363 inline const Quat Quat::rotationZ(float radians)
364 {
365 float s, c, angle;
366 angle = (radians * 0.5f);
367 s = sinf(angle);
368 c = cosf(angle);
369 return Quat(0.0f, 0.0f, s, c);
370 }
371
372 inline const Quat Quat::operator*(const Quat &quat) const
373 {
374 return Quat(
375 ((((mW * quat.mX) + (mX * quat.mW)) + (mY * quat.mZ)) - (mZ * quat.mY)),
376 ((((mW * quat.mY) + (mY * quat.mW)) + (mZ * quat.mX)) - (mX * quat.mZ)),
377 ((((mW * quat.mZ) + (mZ * quat.mW)) + (mX * quat.mY)) - (mY * quat.mX)),
378 ((((mW * quat.mW) - (mX * quat.mX)) - (mY * quat.mY)) - (mZ * quat.mZ)));
379 }
380
381 inline Quat &Quat::operator*=(const Quat &quat)
382 {
383 *this = *this * quat;
384 return *this;
385 }
386
387 inline const Vector3 rotate(const Quat &quat, const Vector3 &vec)
388 {
389 float tmpX, tmpY, tmpZ, tmpW;
390 tmpX = (((quat.getW() * vec.getX()) + (quat.getY() * vec.getZ())) - (quat.getZ() * vec.getY()));
391 tmpY = (((quat.getW() * vec.getY()) + (quat.getZ() * vec.getX())) - (quat.getX() * vec.getZ()));
392 tmpZ = (((quat.getW() * vec.getZ()) + (quat.getX() * vec.getY())) - (quat.getY() * vec.getX()));
393 tmpW = (((quat.getX() * vec.getX()) + (quat.getY() * vec.getY())) + (quat.getZ() * vec.getZ()));
394 return Vector3(
395 ((((tmpW * quat.getX()) + (tmpX * quat.getW())) - (tmpY * quat.getZ())) + (tmpZ * quat.getY())),
396 ((((tmpW * quat.getY()) + (tmpY * quat.getW())) - (tmpZ * quat.getX())) + (tmpX * quat.getZ())),
397 ((((tmpW * quat.getZ()) + (tmpZ * quat.getW())) - (tmpX * quat.getY())) + (tmpY * quat.getX())));
398 }
399
400 inline const Quat conj(const Quat &quat)
401 {
402 return Quat(-quat.getX(), -quat.getY(), -quat.getZ(), quat.getW());
403 }
404
405 inline const Quat select(const Quat &quat0, const Quat &quat1, bool select1)
406 {
407 return Quat(
408 (select1) ? quat1.getX() : quat0.getX(),
409 (select1) ? quat1.getY() : quat0.getY(),
410 (select1) ? quat1.getZ() : quat0.getZ(),
411 (select1) ? quat1.getW() : quat0.getW());
412 }
413
414 #ifdef _VECTORMATH_DEBUG
415
416 inline void print(const Quat &quat)
417 {
418 printf("( %f %f %f %f )\n", quat.getX(), quat.getY(), quat.getZ(), quat.getW());
419 }
420
421 inline void print(const Quat &quat, const char *name)
422 {
423 printf("%s: ( %f %f %f %f )\n", name, quat.getX(), quat.getY(), quat.getZ(), quat.getW());
424 }
425
426 #endif
427
428 } // namespace Aos
429 } // namespace Vectormath
430
431 #endif