nuclear@27: /* nuclear@27: libvmath - a vector math library nuclear@27: Copyright (C) 2004-2011 John Tsiombikas nuclear@27: nuclear@27: This program is free software: you can redistribute it and/or modify nuclear@27: it under the terms of the GNU Lesser General Public License as published nuclear@27: by the Free Software Foundation, either version 3 of the License, or nuclear@27: (at your option) any later version. nuclear@27: nuclear@27: This program is distributed in the hope that it will be useful, nuclear@27: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@27: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@27: GNU Lesser General Public License for more details. nuclear@27: nuclear@27: You should have received a copy of the GNU Lesser General Public License nuclear@27: along with this program. If not, see . nuclear@27: */ nuclear@27: nuclear@27: nuclear@27: #include nuclear@27: #include nuclear@27: #include "quat.h" nuclear@27: nuclear@27: void quat_print(FILE *fp, quat_t q) nuclear@27: { nuclear@27: fprintf(fp, "([ %.4f %.4f %.4f ] %.4f)", q.x, q.y, q.z, q.w); nuclear@27: } nuclear@27: nuclear@27: quat_t quat_rotate(quat_t q, scalar_t angle, scalar_t x, scalar_t y, scalar_t z) nuclear@27: { nuclear@27: quat_t rq; nuclear@27: scalar_t half_angle = angle * 0.5; nuclear@27: scalar_t sin_half = sin(half_angle); nuclear@27: nuclear@27: rq.w = cos(half_angle); nuclear@27: rq.x = x * sin_half; nuclear@27: rq.y = y * sin_half; nuclear@27: rq.z = z * sin_half; nuclear@27: nuclear@27: return quat_mul(q, rq); nuclear@27: } nuclear@27: nuclear@27: quat_t quat_rotate_quat(quat_t q, quat_t rotq) nuclear@27: { nuclear@27: return quat_mul(quat_mul(rotq, q), quat_conjugate(rotq)); nuclear@27: } nuclear@27: nuclear@27: quat_t quat_slerp(quat_t q1, quat_t q2, scalar_t t) nuclear@27: { nuclear@27: quat_t res; nuclear@27: scalar_t a, b, angle, sin_angle, dot; nuclear@27: nuclear@27: dot = q1.w * q2.w + q1.x * q2.x + q1.y * q2.y + q1.z * q2.z; nuclear@27: if(dot < 0.0) { nuclear@27: /* make sure we interpolate across the shortest arc */ nuclear@27: q1.x = -q1.x; nuclear@27: q1.y = -q1.y; nuclear@27: q1.z = -q1.z; nuclear@27: q1.w = -q1.w; nuclear@27: dot = -dot; nuclear@27: } nuclear@27: nuclear@27: /* clamp dot to [-1, 1] in order to avoid domain errors in acos due to nuclear@27: * floating point imprecisions nuclear@27: */ nuclear@27: if(dot < -1.0) dot = -1.0; nuclear@27: if(dot > 1.0) dot = 1.0; nuclear@27: nuclear@27: angle = acos(dot); nuclear@27: sin_angle = sin(angle); nuclear@27: nuclear@27: if(fabs(sin_angle) < SMALL_NUMBER) { nuclear@27: /* for very small angles or completely opposite orientations nuclear@27: * use linear interpolation to avoid div/zero (in the first case it makes sense, nuclear@27: * the second case is pretty much undefined anyway I guess ... nuclear@27: */ nuclear@27: a = 1.0f - t; nuclear@27: b = t; nuclear@27: } else { nuclear@27: a = sin((1.0f - t) * angle) / sin_angle; nuclear@27: b = sin(t * angle) / sin_angle; nuclear@27: } nuclear@27: nuclear@27: res.x = q1.x * a + q2.x * b; nuclear@27: res.y = q1.y * a + q2.y * b; nuclear@27: res.z = q1.z * a + q2.z * b; nuclear@27: res.w = q1.w * a + q2.w * b; nuclear@27: return res; nuclear@27: }