00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef CAL_QUATERNION_H
00012 #define CAL_QUATERNION_H
00013
00014
00015
00016
00017
00018 #include "cal3d/global.h"
00019 #include "cal3d/vector.h"
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00035 class CAL3D_API CalQuaternion
00036 {
00037
00038 public:
00039 float x;
00040 float y;
00041 float z;
00042 float w;
00043
00044
00045 public:
00046 inline CalQuaternion() : x(0.0f), y(0.0f), z(0.0f), w(1.0f){};
00047 inline CalQuaternion(const CalQuaternion& q): x(q.x), y(q.y), z(q.z), w(q.w) {};
00048 inline CalQuaternion(float qx, float qy, float qz, float qw): x(qx), y(qy), z(qz), w(qw) {};
00049 inline ~CalQuaternion() {};
00050
00051
00052 public:
00053 inline float& operator[](unsigned int index)
00054 {
00055 return (&x)[index];
00056 }
00057
00058 inline const float& operator[](unsigned int index) const
00059 {
00060 return (&x)[index];
00061 }
00062
00063 inline void operator=(const CalQuaternion& q)
00064 {
00065 x = q.x;
00066 y = q.y;
00067 z = q.z;
00068 w = q.w;
00069 }
00070
00071 inline void operator*=(const CalQuaternion& q)
00072 {
00073 float mx( x ), my( y ), mz( z ), mw( w );
00074 float qx(q.x), qy(q.y), qz(q.z), qw(q.w);
00075
00076 x = mw * qx + mx * qw + my * qz - mz * qy;
00077 y = mw * qy - mx * qz + my * qw + mz * qx;
00078 z = mw * qz + mx * qy - my * qx + mz * qw;
00079 w = mw * qw - mx * qx - my * qy - mz * qz;
00080 }
00081
00082 inline void operator+=(const CalQuaternion& q)
00083 {
00084 x += q.x;
00085 y += q.y;
00086 z += q.z;
00087 w += q.w;
00088 }
00089
00090 inline void operator*=(const CalVector& v)
00091 {
00092 float qx, qy, qz, qw;
00093 qx = x;
00094 qy = y;
00095 qz = z;
00096 qw = w;
00097
00098 x = qw * v.x + qy * v.z - qz * v.y;
00099 y = qw * v.y - qx * v.z + qz * v.x;
00100 z = qw * v.z + qx * v.y - qy * v.x;
00101 w = - qx * v.x - qy * v.y - qz * v.z;
00102 }
00103
00104 inline void operator*=( float s )
00105 {
00106 x *= s;
00107 y *= s;
00108 z *= s;
00109 w *= s;
00110 }
00111
00112 inline bool operator==(const CalQuaternion& rhs) const
00113 {
00114 return x == rhs.x &&
00115 y == rhs.y &&
00116 z == rhs.z &&
00117 w == rhs.w;
00118 }
00119
00120 inline bool operator!=(const CalQuaternion& rhs) const
00121 {
00122 return !operator==(rhs);
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 inline void blend(float d, const CalQuaternion& q)
00136 {
00137 float norm;
00138 norm = x * q.x + y * q.y + z * q.z + w * q.w;
00139
00140 bool bFlip;
00141 bFlip = false;
00142
00143 if(norm < 0.0f)
00144 {
00145 norm = -norm;
00146 bFlip = true;
00147 }
00148
00149 float inv_d;
00150 if(1.0f - norm < 0.000001f)
00151 {
00152 inv_d = 1.0f - d;
00153 }
00154 else
00155 {
00156 float theta;
00157 theta = (float) acos(norm);
00158
00159 float s;
00160 s = (float) (1.0f / sin(theta));
00161
00162 inv_d = (float) sin((1.0f - d) * theta) * s;
00163 d = (float) sin(d * theta) * s;
00164 }
00165
00166 if(bFlip)
00167 {
00168 d = -d;
00169 }
00170
00171 x = inv_d * x + d * q.x;
00172 y = inv_d * y + d * q.y;
00173 z = inv_d * z + d * q.z;
00174 w = inv_d * w + d * q.w;
00175 }
00176
00177 inline void clear()
00178 {
00179 x = 0.0f;
00180 y = 0.0f;
00181 z = 0.0f;
00182 w = 1.0f;
00183 }
00184 inline void conjugate()
00185 {
00186 x = -x;
00187 y = -y;
00188 z = -z;
00189 }
00190
00191 inline void invert()
00192 {
00193 conjugate();
00194 const float norm = (x*x) + (y*y) + (z*z) + (w*w);
00195
00196 if (norm == 0.0f) return;
00197
00198 const float inv_norm = 1 / norm;
00199 *this *= inv_norm;
00200 }
00201
00202 inline void set(float qx, float qy, float qz, float qw)
00203 {
00204 x = qx;
00205 y = qy;
00206 z = qz;
00207 w = qw;
00208 }
00209
00210 void compress(short &s0, short &s1, short &s2);
00211 void decompress(short &s0, short &s1, short &s2);
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 };
00231
00232
00233 static inline CalQuaternion operator*(const CalQuaternion& q, const CalQuaternion& r)
00234 {
00235 return CalQuaternion(
00236 r.w * q.x + r.x * q.w + r.y * q.z - r.z * q.y,
00237 r.w * q.y - r.x * q.z + r.y * q.w + r.z * q.x,
00238 r.w * q.z + r.x * q.y - r.y * q.x + r.z * q.w,
00239 r.w * q.w - r.x * q.x - r.y * q.y - r.z * q.z
00240 );
00241 }
00242
00243 static inline float dot( const CalQuaternion& q, const CalQuaternion& r )
00244 {
00245 return q.x * r.x +
00246 q.y * r.y +
00247 q.z * r.z +
00248 q.w * r.w;
00249 }
00250
00251
00252 static inline CalQuaternion shortestArc( const CalVector& from, const CalVector& to )
00253 {
00254 CalVector cross = from % to;
00255 float dot = from * to ;
00256
00257 dot = (float) sqrt( 2*(dot+1) ) ;
00258
00259 cross /= dot ;
00260
00261
00262
00263 return CalQuaternion( cross[0], cross[1], cross[2], -dot/2 ) ;
00264
00265 }
00266
00267
00268 #endif
00269
00270