goat3d

annotate libs/vmath/quat.inl @ 27:4deb0b12fe14

wtf... corrupted heap?
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Sep 2013 08:20:19 +0300
parents
children 9ba3e2fb8a33
rev   line source
nuclear@27 1 /*
nuclear@27 2 libvmath - a vector math library
nuclear@27 3 Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@27 4
nuclear@27 5 This program is free software: you can redistribute it and/or modify
nuclear@27 6 it under the terms of the GNU Lesser General Public License as published
nuclear@27 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@27 8 (at your option) any later version.
nuclear@27 9
nuclear@27 10 This program is distributed in the hope that it will be useful,
nuclear@27 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@27 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@27 13 GNU Lesser General Public License for more details.
nuclear@27 14
nuclear@27 15 You should have received a copy of the GNU Lesser General Public License
nuclear@27 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@27 17 */
nuclear@27 18
nuclear@27 19 #include "vector.h"
nuclear@27 20 #include "matrix.h"
nuclear@27 21
nuclear@27 22 #ifdef __cplusplus
nuclear@27 23 extern "C" {
nuclear@27 24 #endif /* __cplusplus */
nuclear@27 25
nuclear@27 26 static inline quat_t quat_mul(quat_t q1, quat_t q2)
nuclear@27 27 {
nuclear@27 28 quat_t res;
nuclear@27 29 vec3_t v1 = quat_vec(q1);
nuclear@27 30 vec3_t v2 = quat_vec(q2);
nuclear@27 31
nuclear@27 32 res.w = q1.w * q2.w - v3_dot(v1, v2);
nuclear@27 33 /* resvec = v2 * q1 + v1 * q2 + cross(v1, v2) */
nuclear@27 34 res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y);
nuclear@27 35 res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z);
nuclear@27 36 res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x);
nuclear@27 37 return res;
nuclear@27 38 }
nuclear@27 39
nuclear@27 40 static inline quat_t quat_conjugate(quat_t q)
nuclear@27 41 {
nuclear@27 42 q.x = -q.x;
nuclear@27 43 q.y = -q.y;
nuclear@27 44 q.z = -q.z;
nuclear@27 45 return q;
nuclear@27 46 }
nuclear@27 47
nuclear@27 48 static inline quat_t quat_inverse(quat_t q)
nuclear@27 49 {
nuclear@27 50 scalar_t lensq = quat_length_sq(q);
nuclear@27 51 q = quat_conjugate(q);
nuclear@27 52 q.x /= lensq;
nuclear@27 53 q.y /= lensq;
nuclear@27 54 q.z /= lensq;
nuclear@27 55 q.w /= lensq;
nuclear@27 56 return q;
nuclear@27 57 }
nuclear@27 58
nuclear@27 59 static inline void quat_to_mat3(mat3_t res, quat_t q)
nuclear@27 60 {
nuclear@27 61 m3_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y - 2.0 * q.w * q.z, 2.0 * q.z * q.x + 2.0 * q.w * q.y,
nuclear@27 62 2.0 * q.x * q.y + 2.0 * q.w * q.z, 1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z - 2.0 * q.w * q.x,
nuclear@27 63 2.0 * q.z * q.x - 2.0 * q.w * q.y, 2.0 * q.y * q.z + 2.0 * q.w * q.x, 1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y);
nuclear@27 64 }
nuclear@27 65
nuclear@27 66 static inline void quat_to_mat4(mat4_t res, quat_t q)
nuclear@27 67 {
nuclear@27 68 m4_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y - 2.0 * q.w * q.z, 2.0 * q.z * q.x + 2.0 * q.w * q.y, 0,
nuclear@27 69 2.0 * q.x * q.y + 2.0 * q.w * q.z, 1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z - 2.0 * q.w * q.x, 0,
nuclear@27 70 2.0 * q.z * q.x - 2.0 * q.w * q.y, 2.0 * q.y * q.z + 2.0 * q.w * q.x, 1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y, 0,
nuclear@27 71 0, 0, 0, 1);
nuclear@27 72 }
nuclear@27 73
nuclear@27 74 #ifdef __cplusplus
nuclear@27 75 } /* extern "C" */
nuclear@27 76
nuclear@27 77 inline Quaternion lerp(const Quaternion &a, const Quaternion &b, scalar_t t)
nuclear@27 78 {
nuclear@27 79 return slerp(a, b, t);
nuclear@27 80 }
nuclear@27 81 #endif /* __cplusplus */