vrshoot

annotate libs/vmath/quat.inl @ 2:334d17aed7de

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