goat3d

annotate libs/vmath/quat_c.c @ 27:4deb0b12fe14

wtf... corrupted heap?
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Sep 2013 08:20:19 +0300
parents
children
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
nuclear@27 20 #include <stdio.h>
nuclear@27 21 #include <math.h>
nuclear@27 22 #include "quat.h"
nuclear@27 23
nuclear@27 24 void quat_print(FILE *fp, quat_t q)
nuclear@27 25 {
nuclear@27 26 fprintf(fp, "([ %.4f %.4f %.4f ] %.4f)", q.x, q.y, q.z, q.w);
nuclear@27 27 }
nuclear@27 28
nuclear@27 29 quat_t quat_rotate(quat_t q, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
nuclear@27 30 {
nuclear@27 31 quat_t rq;
nuclear@27 32 scalar_t half_angle = angle * 0.5;
nuclear@27 33 scalar_t sin_half = sin(half_angle);
nuclear@27 34
nuclear@27 35 rq.w = cos(half_angle);
nuclear@27 36 rq.x = x * sin_half;
nuclear@27 37 rq.y = y * sin_half;
nuclear@27 38 rq.z = z * sin_half;
nuclear@27 39
nuclear@27 40 return quat_mul(q, rq);
nuclear@27 41 }
nuclear@27 42
nuclear@27 43 quat_t quat_rotate_quat(quat_t q, quat_t rotq)
nuclear@27 44 {
nuclear@27 45 return quat_mul(quat_mul(rotq, q), quat_conjugate(rotq));
nuclear@27 46 }
nuclear@27 47
nuclear@27 48 quat_t quat_slerp(quat_t q1, quat_t q2, scalar_t t)
nuclear@27 49 {
nuclear@27 50 quat_t res;
nuclear@27 51 scalar_t a, b, angle, sin_angle, dot;
nuclear@27 52
nuclear@27 53 dot = q1.w * q2.w + q1.x * q2.x + q1.y * q2.y + q1.z * q2.z;
nuclear@27 54 if(dot < 0.0) {
nuclear@27 55 /* make sure we interpolate across the shortest arc */
nuclear@27 56 q1.x = -q1.x;
nuclear@27 57 q1.y = -q1.y;
nuclear@27 58 q1.z = -q1.z;
nuclear@27 59 q1.w = -q1.w;
nuclear@27 60 dot = -dot;
nuclear@27 61 }
nuclear@27 62
nuclear@27 63 /* clamp dot to [-1, 1] in order to avoid domain errors in acos due to
nuclear@27 64 * floating point imprecisions
nuclear@27 65 */
nuclear@27 66 if(dot < -1.0) dot = -1.0;
nuclear@27 67 if(dot > 1.0) dot = 1.0;
nuclear@27 68
nuclear@27 69 angle = acos(dot);
nuclear@27 70 sin_angle = sin(angle);
nuclear@27 71
nuclear@27 72 if(fabs(sin_angle) < SMALL_NUMBER) {
nuclear@27 73 /* for very small angles or completely opposite orientations
nuclear@27 74 * use linear interpolation to avoid div/zero (in the first case it makes sense,
nuclear@27 75 * the second case is pretty much undefined anyway I guess ...
nuclear@27 76 */
nuclear@27 77 a = 1.0f - t;
nuclear@27 78 b = t;
nuclear@27 79 } else {
nuclear@27 80 a = sin((1.0f - t) * angle) / sin_angle;
nuclear@27 81 b = sin(t * angle) / sin_angle;
nuclear@27 82 }
nuclear@27 83
nuclear@27 84 res.x = q1.x * a + q2.x * b;
nuclear@27 85 res.y = q1.y * a + q2.y * b;
nuclear@27 86 res.z = q1.z * a + q2.z * b;
nuclear@27 87 res.w = q1.w * a + q2.w * b;
nuclear@27 88 return res;
nuclear@27 89 }