istereo2

annotate libs/vmath/quat.inl @ 8:661bf09db398

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