00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #if !defined(INCLUDED_MATH_QUATERNION_H)
00028 #define INCLUDED_MATH_QUATERNION_H
00029
00030 #include "math/matrix.h"
00031
00033 typedef Vector4 Quaternion;
00034
00035 const Quaternion c_quaternion_identity(0, 0, 0, 1);
00036
00037 inline Quaternion quaternion_multiplied_by_quaternion (const Quaternion& quaternion, const Quaternion& other)
00038 {
00039 return Quaternion(quaternion[3] * other[0] + quaternion[0] * other[3] + quaternion[1] * other[2] - quaternion[2]
00040 * other[1], quaternion[3] * other[1] + quaternion[1] * other[3] + quaternion[2] * other[0] - quaternion[0]
00041 * other[2], quaternion[3] * other[2] + quaternion[2] * other[3] + quaternion[0] * other[1] - quaternion[1]
00042 * other[0], quaternion[3] * other[3] - quaternion[0] * other[0] - quaternion[1] * other[1] - quaternion[2]
00043 * other[2]);
00044 }
00045
00046 inline void quaternion_multiply_by_quaternion (Quaternion& quaternion, const Quaternion& other)
00047 {
00048 quaternion = quaternion_multiplied_by_quaternion(quaternion, other);
00049 }
00050
00052 inline Quaternion quaternion_for_unit_vectors (const Vector3& from, const Vector3& to)
00053 {
00054 return Quaternion(from.crossProduct(to), static_cast<float> (from.dot(to)));
00055 }
00056
00057 inline Quaternion quaternion_for_axisangle (const Vector3& axis, double angle)
00058 {
00059 angle *= 0.5;
00060 float sa = static_cast<float> (sin(angle));
00061 return Quaternion(axis[0] * sa, axis[1] * sa, axis[2] * sa, static_cast<float> (cos(angle)));
00062 }
00063
00064 inline Quaternion quaternion_for_x (double angle)
00065 {
00066 angle *= 0.5;
00067 return Quaternion(static_cast<float> (sin(angle)), 0, 0, static_cast<float> (cos(angle)));
00068 }
00069
00070 inline Quaternion quaternion_for_y (double angle)
00071 {
00072 angle *= 0.5;
00073 return Quaternion(0, static_cast<float> (sin(angle)), 0, static_cast<float> (cos(angle)));
00074 }
00075
00076 inline Quaternion quaternion_for_z (double angle)
00077 {
00078 angle *= 0.5;
00079 return Quaternion(0, 0, static_cast<float> (sin(angle)), static_cast<float> (cos(angle)));
00080 }
00081
00082 inline Quaternion quaternion_inverse (const Quaternion& quaternion)
00083 {
00084 return Quaternion(-quaternion.getVector3(), quaternion[3]);
00085 }
00086
00087 inline void quaternion_conjugate (Quaternion& quaternion)
00088 {
00089 quaternion = quaternion_inverse(quaternion);
00090 }
00091
00092 inline Quaternion quaternion_normalised (const Quaternion& quaternion)
00093 {
00094 const double n = (1.0 / (quaternion[0] * quaternion[0] + quaternion[1] * quaternion[1] + quaternion[2]
00095 * quaternion[2] + quaternion[3] * quaternion[3]));
00096 return Quaternion(static_cast<float> (quaternion[0] * n), static_cast<float> (quaternion[1] * n),
00097 static_cast<float> (quaternion[2] * n), static_cast<float> (quaternion[3] * n));
00098 }
00099
00100 inline void quaternion_normalise (Quaternion& quaternion)
00101 {
00102 quaternion = quaternion_normalised(quaternion);
00103 }
00104
00106 inline Matrix4 matrix4_rotation_for_quaternion (const Quaternion& quaternion)
00107 {
00108 #if 0
00109 const double xx = quaternion[0] * quaternion[0];
00110 const double xy = quaternion[0] * quaternion[1];
00111 const double xz = quaternion[0] * quaternion[2];
00112 const double xw = quaternion[0] * quaternion[3];
00113
00114 const double yy = quaternion[1] * quaternion[1];
00115 const double yz = quaternion[1] * quaternion[2];
00116 const double yw = quaternion[1] * quaternion[3];
00117
00118 const double zz = quaternion[2] * quaternion[2];
00119 const double zw = quaternion[2] * quaternion[3];
00120
00121 return Matrix4(
00122 static_cast<float>( 1 - 2 * ( yy + zz ) ),
00123 static_cast<float>( 2 * ( xy + zw ) ),
00124 static_cast<float>( 2 * ( xz - yw ) ),
00125 0,
00126 static_cast<float>( 2 * ( xy - zw ) ),
00127 static_cast<float>( 1 - 2 * ( xx + zz ) ),
00128 static_cast<float>( 2 * ( yz + xw ) ),
00129 0,
00130 static_cast<float>( 2 * ( xz + yw ) ),
00131 static_cast<float>( 2 * ( yz - xw ) ),
00132 static_cast<float>( 1 - 2 * ( xx + yy ) ),
00133 0,
00134 0,
00135 0,
00136 0,
00137 1
00138 );
00139
00140 #else
00141 const double x2 = quaternion[0] + quaternion[0];
00142 const double y2 = quaternion[1] + quaternion[1];
00143 const double z2 = quaternion[2] + quaternion[2];
00144 const double xx = quaternion[0] * x2;
00145 const double xy = quaternion[0] * y2;
00146 const double xz = quaternion[0] * z2;
00147 const double yy = quaternion[1] * y2;
00148 const double yz = quaternion[1] * z2;
00149 const double zz = quaternion[2] * z2;
00150 const double wx = quaternion[3] * x2;
00151 const double wy = quaternion[3] * y2;
00152 const double wz = quaternion[3] * z2;
00153
00154 return Matrix4(static_cast<float> (1.0 - (yy + zz)), static_cast<float> (xy + wz), static_cast<float> (xz - wy), 0,
00155 static_cast<float> (xy - wz), static_cast<float> (1.0 - (xx + zz)), static_cast<float> (yz + wx), 0,
00156 static_cast<float> (xz + wy), static_cast<float> (yz - wx), static_cast<float> (1.0 - (xx + yy)), 0, 0, 0,
00157 0, 1);
00158
00159 #endif
00160 }
00161
00162 const double c_half_sqrt2 = 0.70710678118654752440084436210485;
00163 const float c_half_sqrt2f = static_cast<float> (c_half_sqrt2);
00164
00165 inline bool quaternion_component_is_90 (float component)
00166 {
00167 return (fabs(component) - c_half_sqrt2) < 0.001;
00168 }
00169
00170 inline Matrix4 matrix4_rotation_for_quaternion_quantised (const Quaternion& quaternion)
00171 {
00172 if (quaternion.y() == 0 && quaternion.z() == 0 && quaternion_component_is_90(quaternion.x())
00173 && quaternion_component_is_90(quaternion.w())) {
00174 return matrix4_rotation_for_sincos_x((quaternion.x() > 0) ? 1.f : -1.f, 0);
00175 }
00176
00177 if (quaternion.x() == 0 && quaternion.z() == 0 && quaternion_component_is_90(quaternion.y())
00178 && quaternion_component_is_90(quaternion.w())) {
00179 return matrix4_rotation_for_sincos_y((quaternion.y() > 0) ? 1.f : -1.f, 0);
00180 }
00181
00182 if (quaternion.x() == 0 && quaternion.y() == 0 && quaternion_component_is_90(quaternion.z())
00183 && quaternion_component_is_90(quaternion.w())) {
00184 return matrix4_rotation_for_sincos_z((quaternion.z() > 0) ? 1.f : -1.f, 0);
00185 }
00186
00187 return matrix4_rotation_for_quaternion(quaternion);
00188 }
00189
00190 inline Quaternion quaternion_for_matrix4_rotation (const Matrix4& matrix4)
00191 {
00192 Matrix4 transposed = matrix4_transposed(matrix4);
00193
00194 double trace = transposed[0] + transposed[5] + transposed[10] + 1.0;
00195
00196 if (trace > 0.0001) {
00197 double S = 0.5 / sqrt(trace);
00198 return Quaternion(static_cast<float> ((transposed[9] - transposed[6]) * S), static_cast<float> ((transposed[2]
00199 - transposed[8]) * S), static_cast<float> ((transposed[4] - transposed[1]) * S),
00200 static_cast<float> (0.25 / S));
00201 }
00202
00203 if (transposed[0] >= transposed[5] && transposed[0] >= transposed[10]) {
00204 double S = 2.0 * sqrt(1.0 + transposed[0] - transposed[5] - transposed[10]);
00205 return Quaternion(static_cast<float> (0.25 / S), static_cast<float> ((transposed[1] + transposed[4]) / S),
00206 static_cast<float> ((transposed[2] + transposed[8]) / S), static_cast<float> ((transposed[6]
00207 + transposed[9]) / S));
00208 }
00209
00210 if (transposed[5] >= transposed[0] && transposed[5] >= transposed[10]) {
00211 double S = 2.0 * sqrt(1.0 + transposed[5] - transposed[0] - transposed[10]);
00212 return Quaternion(static_cast<float> ((transposed[1] + transposed[4]) / S), static_cast<float> (0.25 / S),
00213 static_cast<float> ((transposed[6] + transposed[9]) / S), static_cast<float> ((transposed[2]
00214 + transposed[8]) / S));
00215 }
00216
00217 double S = 2.0 * sqrt(1.0 + transposed[10] - transposed[0] - transposed[5]);
00218 return Quaternion(static_cast<float> ((transposed[2] + transposed[8]) / S), static_cast<float> ((transposed[6]
00219 + transposed[9]) / S), static_cast<float> (0.25 / S), static_cast<float> ((transposed[1] + transposed[4])
00220 / S));
00221 }
00222
00225 inline Matrix4 matrix4_rotated_by_quaternion (const Matrix4& self, const Quaternion& rotation)
00226 {
00227 return matrix4_multiplied_by_matrix4(self, matrix4_rotation_for_quaternion(rotation));
00228 }
00229
00232 inline void matrix4_rotate_by_quaternion (Matrix4& self, const Quaternion& rotation)
00233 {
00234 self = matrix4_rotated_by_quaternion(self, rotation);
00235 }
00236
00238 inline void matrix4_pivoted_rotate_by_quaternion (Matrix4& self, const Quaternion& rotation, const Vector3& pivotpoint)
00239 {
00240 matrix4_translate_by_vec3(self, pivotpoint);
00241 matrix4_rotate_by_quaternion(self, rotation);
00242 matrix4_translate_by_vec3(self, -pivotpoint);
00243 }
00244
00245 inline Vector3 quaternion_transformed_point (const Quaternion& quaternion, const Vector3& point)
00246 {
00247 double xx = quaternion.x() * quaternion.x();
00248 double yy = quaternion.y() * quaternion.y();
00249 double zz = quaternion.z() * quaternion.z();
00250 double ww = quaternion.w() * quaternion.w();
00251
00252 double xy2 = quaternion.x() * quaternion.y() * 2;
00253 double xz2 = quaternion.x() * quaternion.z() * 2;
00254 double xw2 = quaternion.x() * quaternion.w() * 2;
00255 double yz2 = quaternion.y() * quaternion.z() * 2;
00256 double yw2 = quaternion.y() * quaternion.w() * 2;
00257 double zw2 = quaternion.z() * quaternion.w() * 2;
00258
00259 return Vector3(static_cast<float> (ww * point.x() + yw2 * point.z() - zw2 * point.y() + xx * point.x() + xy2
00260 * point.y() + xz2 * point.z() - zz * point.x() - yy * point.x()), static_cast<float> (xy2 * point.x() + yy
00261 * point.y() + yz2 * point.z() + zw2 * point.x() - zz * point.y() + ww * point.y() - xw2 * point.z() - xx
00262 * point.y()), static_cast<float> (xz2 * point.x() + yz2 * point.y() + zz * point.z() - yw2 * point.x() - yy
00263 * point.z() + xw2 * point.y() - xx * point.z() + ww * point.z()));
00264 }
00265
00267 inline Matrix4 matrix4_rotation_for_axisangle (const Vector3& axis, double angle)
00268 {
00269 return matrix4_rotation_for_quaternion(quaternion_for_axisangle(axis, angle));
00270 }
00271
00273 inline void matrix4_rotate_by_axisangle (Matrix4& self, const Vector3& axis, double angle)
00274 {
00275 matrix4_multiply_by_matrix4(self, matrix4_rotation_for_axisangle(axis, angle));
00276 }
00277
00279 inline void matrix4_pivoted_rotate_by_axisangle (Matrix4& self, const Vector3& axis, double angle,
00280 const Vector3& pivotpoint)
00281 {
00282 matrix4_translate_by_vec3(self, pivotpoint);
00283 matrix4_rotate_by_axisangle(self, axis, angle);
00284 matrix4_translate_by_vec3(self, -pivotpoint);
00285 }
00286
00287 #endif