3dphotoshoot

annotate libs/vmath/quat_c.c @ 15:2d48f65da357

assman
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Jun 2015 20:40:37 +0300
parents
children
rev   line source
nuclear@10 1 /*
nuclear@10 2 libvmath - a vector math library
nuclear@10 3 Copyright (C) 2004-2015 John Tsiombikas <nuclear@member.fsf.org>
nuclear@10 4
nuclear@10 5 This program is free software: you can redistribute it and/or modify
nuclear@10 6 it under the terms of the GNU Lesser General Public License as published
nuclear@10 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@10 8 (at your option) any later version.
nuclear@10 9
nuclear@10 10 This program is distributed in the hope that it will be useful,
nuclear@10 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@10 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@10 13 GNU Lesser General Public License for more details.
nuclear@10 14
nuclear@10 15 You should have received a copy of the GNU Lesser General Public License
nuclear@10 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@10 17 */
nuclear@10 18
nuclear@10 19
nuclear@10 20 #include <stdio.h>
nuclear@10 21 #include <math.h>
nuclear@10 22 #include "quat.h"
nuclear@10 23
nuclear@10 24 void quat_print(FILE *fp, quat_t q)
nuclear@10 25 {
nuclear@10 26 fprintf(fp, "([ %.4f %.4f %.4f ] %.4f)", q.x, q.y, q.z, q.w);
nuclear@10 27 }
nuclear@10 28
nuclear@10 29 quat_t quat_rotate(quat_t q, scalar_t angle, scalar_t x, scalar_t y, scalar_t z)
nuclear@10 30 {
nuclear@10 31 quat_t rq;
nuclear@10 32 scalar_t half_angle = angle * 0.5;
nuclear@10 33 scalar_t sin_half = sin(half_angle);
nuclear@10 34
nuclear@10 35 rq.w = cos(half_angle);
nuclear@10 36 rq.x = x * sin_half;
nuclear@10 37 rq.y = y * sin_half;
nuclear@10 38 rq.z = z * sin_half;
nuclear@10 39
nuclear@10 40 return quat_mul(q, rq);
nuclear@10 41 }
nuclear@10 42
nuclear@10 43 quat_t quat_rotate_quat(quat_t q, quat_t rotq)
nuclear@10 44 {
nuclear@10 45 return quat_mul(quat_mul(rotq, q), quat_conjugate(rotq));
nuclear@10 46 }
nuclear@10 47
nuclear@10 48 quat_t quat_slerp(quat_t q1, quat_t q2, scalar_t t)
nuclear@10 49 {
nuclear@10 50 quat_t res;
nuclear@10 51 scalar_t a, b, angle, sin_angle, dot;
nuclear@10 52
nuclear@10 53 dot = q1.w * q2.w + q1.x * q2.x + q1.y * q2.y + q1.z * q2.z;
nuclear@10 54 if(dot < 0.0) {
nuclear@10 55 /* make sure we interpolate across the shortest arc */
nuclear@10 56 q1.x = -q1.x;
nuclear@10 57 q1.y = -q1.y;
nuclear@10 58 q1.z = -q1.z;
nuclear@10 59 q1.w = -q1.w;
nuclear@10 60 dot = -dot;
nuclear@10 61 }
nuclear@10 62
nuclear@10 63 /* clamp dot to [-1, 1] in order to avoid domain errors in acos due to
nuclear@10 64 * floating point imprecisions
nuclear@10 65 */
nuclear@10 66 if(dot < -1.0) dot = -1.0;
nuclear@10 67 if(dot > 1.0) dot = 1.0;
nuclear@10 68
nuclear@10 69 angle = acos(dot);
nuclear@10 70 sin_angle = sin(angle);
nuclear@10 71
nuclear@10 72 if(fabs(sin_angle) < SMALL_NUMBER) {
nuclear@10 73 /* for very small angles or completely opposite orientations
nuclear@10 74 * use linear interpolation to avoid div/zero (in the first case it makes sense,
nuclear@10 75 * the second case is pretty much undefined anyway I guess ...
nuclear@10 76 */
nuclear@10 77 a = 1.0f - t;
nuclear@10 78 b = t;
nuclear@10 79 } else {
nuclear@10 80 a = sin((1.0f - t) * angle) / sin_angle;
nuclear@10 81 b = sin(t * angle) / sin_angle;
nuclear@10 82 }
nuclear@10 83
nuclear@10 84 res.x = q1.x * a + q2.x * b;
nuclear@10 85 res.y = q1.y * a + q2.y * b;
nuclear@10 86 res.z = q1.z * a + q2.z * b;
nuclear@10 87 res.w = q1.w * a + q2.w * b;
nuclear@10 88 return res;
nuclear@10 89 }