nuclear@0: #include "quat.h" nuclear@0: #include "vmath.h" nuclear@0: nuclear@0: Quaternion::Quaternion() nuclear@0: { nuclear@0: s = 1.0; nuclear@0: v.x = v.y = v.z = 0.0; nuclear@0: } nuclear@0: nuclear@0: Quaternion::Quaternion(scalar_t s, const Vector3 &v) nuclear@0: { nuclear@0: this->s = s; nuclear@0: this->v = v; nuclear@0: } nuclear@0: nuclear@0: Quaternion::Quaternion(scalar_t s, scalar_t x, scalar_t y, scalar_t z) nuclear@0: { nuclear@0: v.x = x; nuclear@0: v.y = y; nuclear@0: v.z = z; nuclear@0: this->s = s; nuclear@0: } nuclear@0: nuclear@0: Quaternion::Quaternion(const Vector3 &axis, scalar_t angle) nuclear@0: { nuclear@0: set_rotation(axis, angle); nuclear@0: } nuclear@0: nuclear@0: Quaternion::Quaternion(const quat_t &quat) nuclear@0: { nuclear@0: v.x = quat.x; nuclear@0: v.y = quat.y; nuclear@0: v.z = quat.z; nuclear@0: s = quat.w; nuclear@0: } nuclear@0: nuclear@0: Quaternion Quaternion::operator +(const Quaternion &quat) const nuclear@0: { nuclear@0: return Quaternion(s + quat.s, v + quat.v); nuclear@0: } nuclear@0: nuclear@0: Quaternion Quaternion::operator -(const Quaternion &quat) const nuclear@0: { nuclear@0: return Quaternion(s - quat.s, v - quat.v); nuclear@0: } nuclear@0: nuclear@0: Quaternion Quaternion::operator -() const nuclear@0: { nuclear@0: return Quaternion(-s, -v); nuclear@0: } nuclear@0: nuclear@0: /** Quaternion Multiplication: nuclear@0: * Q1*Q2 = [s1*s2 - v1.v2, s1*v2 + s2*v1 + v1(x)v2] nuclear@0: */ nuclear@0: Quaternion Quaternion::operator *(const Quaternion &quat) const nuclear@0: { nuclear@0: Quaternion newq; nuclear@0: newq.s = s * quat.s - dot_product(v, quat.v); nuclear@0: newq.v = quat.v * s + v * quat.s + cross_product(v, quat.v); nuclear@0: return newq; nuclear@0: } nuclear@0: nuclear@0: void Quaternion::operator +=(const Quaternion &quat) nuclear@0: { nuclear@0: *this = Quaternion(s + quat.s, v + quat.v); nuclear@0: } nuclear@0: nuclear@0: void Quaternion::operator -=(const Quaternion &quat) nuclear@0: { nuclear@0: *this = Quaternion(s - quat.s, v - quat.v); nuclear@0: } nuclear@0: nuclear@0: void Quaternion::operator *=(const Quaternion &quat) nuclear@0: { nuclear@0: *this = *this * quat; nuclear@0: } nuclear@0: nuclear@0: void Quaternion::reset_identity() nuclear@0: { nuclear@0: s = 1.0; nuclear@0: v.x = v.y = v.z = 0.0; nuclear@0: } nuclear@0: nuclear@0: Quaternion Quaternion::conjugate() const nuclear@0: { nuclear@0: return Quaternion(s, -v); nuclear@0: } nuclear@0: nuclear@0: scalar_t Quaternion::length() const nuclear@0: { nuclear@0: return (scalar_t)sqrt(v.x*v.x + v.y*v.y + v.z*v.z + s*s); nuclear@0: } nuclear@0: nuclear@0: /** Q * ~Q = ||Q||^2 */ nuclear@0: scalar_t Quaternion::length_sq() const nuclear@0: { nuclear@0: return v.x*v.x + v.y*v.y + v.z*v.z + s*s; nuclear@0: } nuclear@0: nuclear@0: void Quaternion::normalize() nuclear@0: { nuclear@0: scalar_t len = (scalar_t)sqrt(v.x*v.x + v.y*v.y + v.z*v.z + s*s); nuclear@0: v.x /= len; nuclear@0: v.y /= len; nuclear@0: v.z /= len; nuclear@0: s /= len; nuclear@0: } nuclear@0: nuclear@0: Quaternion Quaternion::normalized() const nuclear@0: { nuclear@0: Quaternion nq = *this; nuclear@0: scalar_t len = (scalar_t)sqrt(v.x*v.x + v.y*v.y + v.z*v.z + s*s); nuclear@0: nq.v.x /= len; nuclear@0: nq.v.y /= len; nuclear@0: nq.v.z /= len; nuclear@0: nq.s /= len; nuclear@0: return nq; nuclear@0: } nuclear@0: nuclear@0: /** Quaternion Inversion: Q^-1 = ~Q / ||Q||^2 */ nuclear@0: Quaternion Quaternion::inverse() const nuclear@0: { nuclear@0: Quaternion inv = conjugate(); nuclear@0: scalar_t lensq = length_sq(); nuclear@0: inv.v /= lensq; nuclear@0: inv.s /= lensq; nuclear@0: nuclear@0: return inv; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: void Quaternion::set_rotation(const Vector3 &axis, scalar_t angle) nuclear@0: { nuclear@0: scalar_t half_angle = angle / 2.0; nuclear@0: s = cos(half_angle); nuclear@0: v = axis * sin(half_angle); nuclear@0: } nuclear@0: nuclear@0: void Quaternion::rotate(const Vector3 &axis, scalar_t angle) nuclear@0: { nuclear@0: Quaternion q; nuclear@0: scalar_t half_angle = angle / 2.0; nuclear@0: q.s = cos(half_angle); nuclear@0: q.v = axis * sin(half_angle); nuclear@0: nuclear@0: *this *= q; nuclear@0: } nuclear@0: nuclear@0: void Quaternion::rotate(const Quaternion &q) nuclear@0: { nuclear@0: *this = q * *this * q.conjugate(); nuclear@0: } nuclear@0: nuclear@0: Matrix3x3 Quaternion::get_rotation_matrix() const nuclear@0: { nuclear@0: return Matrix3x3( nuclear@0: 1.0 - 2.0 * v.y*v.y - 2.0 * v.z*v.z, 2.0 * v.x * v.y - 2.0 * s * v.z, 2.0 * v.z * v.x + 2.0 * s * v.y, nuclear@0: 2.0 * v.x * v.y + 2.0 * s * v.z, 1.0 - 2.0 * v.x*v.x - 2.0 * v.z*v.z, 2.0 * v.y * v.z - 2.0 * s * v.x, nuclear@0: 2.0 * v.z * v.x - 2.0 * s * v.y, 2.0 * v.y * v.z + 2.0 * s * v.x, 1.0 - 2.0 * v.x*v.x - 2.0 * v.y*v.y); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: /** Spherical linear interpolation (slerp) */ nuclear@0: Quaternion slerp(const Quaternion &quat1, const Quaternion &q2, scalar_t t) nuclear@0: { nuclear@0: Quaternion q1; nuclear@0: scalar_t dot = q1.s * q2.s + q1.v.x * q2.v.x + q1.v.y * q2.v.y + q1.v.z * q2.v.z; nuclear@0: nuclear@0: if(dot < 0.0) { nuclear@0: /* make sure we interpolate across the shortest arc */ nuclear@0: q1 = -quat1; nuclear@0: dot = -dot; nuclear@0: } else { nuclear@0: q1 = quat1; nuclear@0: } nuclear@0: nuclear@0: /* clamp dot to [-1, 1] in order to avoid domain errors in acos due to nuclear@0: * floating point imprecisions nuclear@0: */ nuclear@0: if(dot < -1.0) dot = -1.0; nuclear@0: if(dot > 1.0) dot = 1.0; nuclear@0: nuclear@0: scalar_t angle = acos(dot); nuclear@0: scalar_t a, b; nuclear@0: nuclear@0: scalar_t sin_angle = sin(angle); nuclear@0: if(fabs(sin_angle) < SMALL_NUMBER) { nuclear@0: /* for very small angles or completely opposite orientations nuclear@0: * use linear interpolation to avoid div/zero (in the first case it makes sense, nuclear@0: * the second case is pretty much undefined anyway I guess ... nuclear@0: */ nuclear@0: a = 1.0f - t; nuclear@0: b = t; nuclear@0: } else { nuclear@0: a = sin((1.0f - t) * angle) / sin_angle; nuclear@0: b = sin(t * angle) / sin_angle; nuclear@0: } nuclear@0: nuclear@0: scalar_t x = q1.v.x * a + q2.v.x * b; nuclear@0: scalar_t y = q1.v.y * a + q2.v.y * b; nuclear@0: scalar_t z = q1.v.z * a + q2.v.z * b; nuclear@0: scalar_t s = q1.s * a + q2.s * b; nuclear@0: nuclear@0: return Quaternion(s, Vector3(x, y, z)); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: std::ostream &operator <<(std::ostream &out, const Quaternion &q) nuclear@0: { nuclear@0: out << "(" << q.s << ", " << q.v << ")"; nuclear@0: return out; nuclear@0: }