nuclear@39: /* nuclear@39: libvmath - a vector math library nuclear@39: Copyright (C) 2004-2011 John Tsiombikas nuclear@39: nuclear@39: This program is free software: you can redistribute it and/or modify nuclear@39: it under the terms of the GNU Lesser General Public License as published nuclear@39: by the Free Software Foundation, either version 3 of the License, or nuclear@39: (at your option) any later version. nuclear@39: nuclear@39: This program is distributed in the hope that it will be useful, nuclear@39: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@39: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@39: GNU Lesser General Public License for more details. nuclear@39: nuclear@39: You should have received a copy of the GNU Lesser General Public License nuclear@39: along with this program. If not, see . nuclear@39: */ nuclear@39: nuclear@39: nuclear@28: #include nuclear@28: #include nuclear@28: #include "quat.h" nuclear@28: nuclear@28: void quat_print(FILE *fp, quat_t q) nuclear@28: { nuclear@28: fprintf(fp, "([ %.4f %.4f %.4f ] %.4f)", q.x, q.y, q.z, q.w); nuclear@28: } nuclear@28: nuclear@28: quat_t quat_rotate(quat_t q, scalar_t angle, scalar_t x, scalar_t y, scalar_t z) nuclear@28: { nuclear@28: quat_t rq; nuclear@28: scalar_t half_angle = angle * 0.5; nuclear@28: scalar_t sin_half = sin(half_angle); nuclear@28: nuclear@28: rq.w = cos(half_angle); nuclear@28: rq.x = x * sin_half; nuclear@28: rq.y = y * sin_half; nuclear@28: rq.z = z * sin_half; nuclear@28: nuclear@28: return quat_mul(q, rq); nuclear@28: } nuclear@28: nuclear@28: quat_t quat_rotate_quat(quat_t q, quat_t rotq) nuclear@28: { nuclear@28: return quat_mul(quat_mul(rotq, q), quat_conjugate(rotq)); nuclear@28: } nuclear@28: nuclear@28: quat_t quat_slerp(quat_t q1, quat_t q2, scalar_t t) nuclear@28: { nuclear@28: quat_t res; nuclear@28: scalar_t angle = acos(q1.w * q2.w + q1.x * q2.x + q1.y * q2.y + q1.z * q2.z); nuclear@28: scalar_t a = sin((1.0f - t) * angle); nuclear@28: scalar_t b = sin(t * angle); nuclear@28: scalar_t c = sin(angle); nuclear@28: nuclear@28: res.x = (q1.x * a + q2.x * b) / c; nuclear@28: res.y = (q1.y * a + q2.y * b) / c; nuclear@28: res.z = (q1.z * a + q2.z * b) / c; nuclear@28: res.w = (q1.w * a + q2.w * b) / c; nuclear@28: return quat_normalize(res); nuclear@28: }