goat3d

annotate libs/vmath/quat.cc @ 37:d259a5094390

trying to figure out how to properly call C functions from the python plugin
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 06 Oct 2013 17:42:43 +0300
parents
children
rev   line source
nuclear@27 1 #include "quat.h"
nuclear@27 2 #include "vmath.h"
nuclear@27 3
nuclear@27 4 Quaternion::Quaternion()
nuclear@27 5 {
nuclear@27 6 s = 1.0;
nuclear@27 7 v.x = v.y = v.z = 0.0;
nuclear@27 8 }
nuclear@27 9
nuclear@27 10 Quaternion::Quaternion(scalar_t s, const Vector3 &v)
nuclear@27 11 {
nuclear@27 12 this->s = s;
nuclear@27 13 this->v = v;
nuclear@27 14 }
nuclear@27 15
nuclear@27 16 Quaternion::Quaternion(scalar_t s, scalar_t x, scalar_t y, scalar_t z)
nuclear@27 17 {
nuclear@27 18 v.x = x;
nuclear@27 19 v.y = y;
nuclear@27 20 v.z = z;
nuclear@27 21 this->s = s;
nuclear@27 22 }
nuclear@27 23
nuclear@27 24 Quaternion::Quaternion(const Vector3 &axis, scalar_t angle)
nuclear@27 25 {
nuclear@27 26 set_rotation(axis, angle);
nuclear@27 27 }
nuclear@27 28
nuclear@27 29 Quaternion::Quaternion(const quat_t &quat)
nuclear@27 30 {
nuclear@27 31 v.x = quat.x;
nuclear@27 32 v.y = quat.y;
nuclear@27 33 v.z = quat.z;
nuclear@27 34 s = quat.w;
nuclear@27 35 }
nuclear@27 36
nuclear@27 37 Quaternion Quaternion::operator +(const Quaternion &quat) const
nuclear@27 38 {
nuclear@27 39 return Quaternion(s + quat.s, v + quat.v);
nuclear@27 40 }
nuclear@27 41
nuclear@27 42 Quaternion Quaternion::operator -(const Quaternion &quat) const
nuclear@27 43 {
nuclear@27 44 return Quaternion(s - quat.s, v - quat.v);
nuclear@27 45 }
nuclear@27 46
nuclear@27 47 Quaternion Quaternion::operator -() const
nuclear@27 48 {
nuclear@27 49 return Quaternion(-s, -v);
nuclear@27 50 }
nuclear@27 51
nuclear@27 52 /** Quaternion Multiplication:
nuclear@27 53 * Q1*Q2 = [s1*s2 - v1.v2, s1*v2 + s2*v1 + v1(x)v2]
nuclear@27 54 */
nuclear@27 55 Quaternion Quaternion::operator *(const Quaternion &quat) const
nuclear@27 56 {
nuclear@27 57 Quaternion newq;
nuclear@27 58 newq.s = s * quat.s - dot_product(v, quat.v);
nuclear@27 59 newq.v = quat.v * s + v * quat.s + cross_product(v, quat.v);
nuclear@27 60 return newq;
nuclear@27 61 }
nuclear@27 62
nuclear@27 63 void Quaternion::operator +=(const Quaternion &quat)
nuclear@27 64 {
nuclear@27 65 *this = Quaternion(s + quat.s, v + quat.v);
nuclear@27 66 }
nuclear@27 67
nuclear@27 68 void Quaternion::operator -=(const Quaternion &quat)
nuclear@27 69 {
nuclear@27 70 *this = Quaternion(s - quat.s, v - quat.v);
nuclear@27 71 }
nuclear@27 72
nuclear@27 73 void Quaternion::operator *=(const Quaternion &quat)
nuclear@27 74 {
nuclear@27 75 *this = *this * quat;
nuclear@27 76 }
nuclear@27 77
nuclear@27 78 void Quaternion::reset_identity()
nuclear@27 79 {
nuclear@27 80 s = 1.0;
nuclear@27 81 v.x = v.y = v.z = 0.0;
nuclear@27 82 }
nuclear@27 83
nuclear@27 84 Quaternion Quaternion::conjugate() const
nuclear@27 85 {
nuclear@27 86 return Quaternion(s, -v);
nuclear@27 87 }
nuclear@27 88
nuclear@27 89 scalar_t Quaternion::length() const
nuclear@27 90 {
nuclear@27 91 return (scalar_t)sqrt(v.x*v.x + v.y*v.y + v.z*v.z + s*s);
nuclear@27 92 }
nuclear@27 93
nuclear@27 94 /** Q * ~Q = ||Q||^2 */
nuclear@27 95 scalar_t Quaternion::length_sq() const
nuclear@27 96 {
nuclear@27 97 return v.x*v.x + v.y*v.y + v.z*v.z + s*s;
nuclear@27 98 }
nuclear@27 99
nuclear@27 100 void Quaternion::normalize()
nuclear@27 101 {
nuclear@27 102 scalar_t len = (scalar_t)sqrt(v.x*v.x + v.y*v.y + v.z*v.z + s*s);
nuclear@27 103 v.x /= len;
nuclear@27 104 v.y /= len;
nuclear@27 105 v.z /= len;
nuclear@27 106 s /= len;
nuclear@27 107 }
nuclear@27 108
nuclear@27 109 Quaternion Quaternion::normalized() const
nuclear@27 110 {
nuclear@27 111 Quaternion nq = *this;
nuclear@27 112 scalar_t len = (scalar_t)sqrt(v.x*v.x + v.y*v.y + v.z*v.z + s*s);
nuclear@27 113 nq.v.x /= len;
nuclear@27 114 nq.v.y /= len;
nuclear@27 115 nq.v.z /= len;
nuclear@27 116 nq.s /= len;
nuclear@27 117 return nq;
nuclear@27 118 }
nuclear@27 119
nuclear@27 120 /** Quaternion Inversion: Q^-1 = ~Q / ||Q||^2 */
nuclear@27 121 Quaternion Quaternion::inverse() const
nuclear@27 122 {
nuclear@27 123 Quaternion inv = conjugate();
nuclear@27 124 scalar_t lensq = length_sq();
nuclear@27 125 inv.v /= lensq;
nuclear@27 126 inv.s /= lensq;
nuclear@27 127
nuclear@27 128 return inv;
nuclear@27 129 }
nuclear@27 130
nuclear@27 131
nuclear@27 132 void Quaternion::set_rotation(const Vector3 &axis, scalar_t angle)
nuclear@27 133 {
nuclear@27 134 scalar_t half_angle = angle / 2.0;
nuclear@27 135 s = cos(half_angle);
nuclear@27 136 v = axis * sin(half_angle);
nuclear@27 137 }
nuclear@27 138
nuclear@27 139 void Quaternion::rotate(const Vector3 &axis, scalar_t angle)
nuclear@27 140 {
nuclear@27 141 Quaternion q;
nuclear@27 142 scalar_t half_angle = angle / 2.0;
nuclear@27 143 q.s = cos(half_angle);
nuclear@27 144 q.v = axis * sin(half_angle);
nuclear@27 145
nuclear@27 146 *this *= q;
nuclear@27 147 }
nuclear@27 148
nuclear@27 149 void Quaternion::rotate(const Quaternion &q)
nuclear@27 150 {
nuclear@27 151 *this = q * *this * q.conjugate();
nuclear@27 152 }
nuclear@27 153
nuclear@27 154 Matrix3x3 Quaternion::get_rotation_matrix() const
nuclear@27 155 {
nuclear@27 156 return Matrix3x3(
nuclear@27 157 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@27 158 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@27 159 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@27 160 }
nuclear@27 161
nuclear@27 162
nuclear@27 163 /** Spherical linear interpolation (slerp) */
nuclear@27 164 Quaternion slerp(const Quaternion &quat1, const Quaternion &q2, scalar_t t)
nuclear@27 165 {
nuclear@27 166 Quaternion q1;
nuclear@27 167 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@27 168
nuclear@27 169 if(dot < 0.0) {
nuclear@27 170 /* make sure we interpolate across the shortest arc */
nuclear@27 171 q1 = -quat1;
nuclear@27 172 dot = -dot;
nuclear@27 173 } else {
nuclear@27 174 q1 = quat1;
nuclear@27 175 }
nuclear@27 176
nuclear@27 177 /* clamp dot to [-1, 1] in order to avoid domain errors in acos due to
nuclear@27 178 * floating point imprecisions
nuclear@27 179 */
nuclear@27 180 if(dot < -1.0) dot = -1.0;
nuclear@27 181 if(dot > 1.0) dot = 1.0;
nuclear@27 182
nuclear@27 183 scalar_t angle = acos(dot);
nuclear@27 184 scalar_t a, b;
nuclear@27 185
nuclear@27 186 scalar_t sin_angle = sin(angle);
nuclear@27 187 if(fabs(sin_angle) < SMALL_NUMBER) {
nuclear@27 188 /* for very small angles or completely opposite orientations
nuclear@27 189 * use linear interpolation to avoid div/zero (in the first case it makes sense,
nuclear@27 190 * the second case is pretty much undefined anyway I guess ...
nuclear@27 191 */
nuclear@27 192 a = 1.0f - t;
nuclear@27 193 b = t;
nuclear@27 194 } else {
nuclear@27 195 a = sin((1.0f - t) * angle) / sin_angle;
nuclear@27 196 b = sin(t * angle) / sin_angle;
nuclear@27 197 }
nuclear@27 198
nuclear@27 199 scalar_t x = q1.v.x * a + q2.v.x * b;
nuclear@27 200 scalar_t y = q1.v.y * a + q2.v.y * b;
nuclear@27 201 scalar_t z = q1.v.z * a + q2.v.z * b;
nuclear@27 202 scalar_t s = q1.s * a + q2.s * b;
nuclear@27 203
nuclear@27 204 return Quaternion(s, Vector3(x, y, z));
nuclear@27 205 }
nuclear@27 206
nuclear@27 207
nuclear@27 208 std::ostream &operator <<(std::ostream &out, const Quaternion &q)
nuclear@27 209 {
nuclear@27 210 out << "(" << q.s << ", " << q.v << ")";
nuclear@27 211 return out;
nuclear@27 212 }