| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#ifndef __MATH_QUATERNION_H__ |
| 23 |
#define __MATH_QUATERNION_H__ |
| 24 |
|
| 25 |
#include <assert.h> |
| 26 |
#include <math.h> |
| 27 |
|
| 28 |
class idVec3_t; |
| 29 |
class angles_t; |
| 30 |
class mat3_t; |
| 31 |
|
| 32 |
class quat_t { |
| 33 |
public: |
| 34 |
float x; |
| 35 |
float y; |
| 36 |
float z; |
| 37 |
float w; |
| 38 |
|
| 39 |
quat_t(); |
| 40 |
quat_t( float x, float y, float z, float w ); |
| 41 |
quat_t( const quat_t & ) = default; |
| 42 |
|
| 43 |
friend void toQuat( idVec3_t &src, quat_t &dst ); |
| 44 |
friend void toQuat( angles_t &src, quat_t &dst ); |
| 45 |
friend void toQuat( mat3_t &src, quat_t &dst ); |
| 46 |
|
| 47 |
float *vec4( void ); |
| 48 |
|
| 49 |
float operator[]( int index ) const; |
| 50 |
float &operator[]( int index ); |
| 51 |
|
| 52 |
void set( float x, float y, float z, float w ); |
| 53 |
|
| 54 |
quat_t &operator=( const quat_t &a ) = default; |
| 55 |
|
| 56 |
friend quat_t operator+( quat_t a, quat_t b ); |
| 57 |
quat_t &operator+=( quat_t a ); |
| 58 |
|
| 59 |
friend quat_t operator-( quat_t a, quat_t b ); |
| 60 |
quat_t &operator-=( quat_t a ); |
| 61 |
|
| 62 |
friend quat_t operator*( quat_t a, float b ); |
| 63 |
friend quat_t operator*( float a, quat_t b ); |
| 64 |
quat_t &operator*=( float a ); |
| 65 |
|
| 66 |
friend int operator==( quat_t a, quat_t b ); |
| 67 |
friend int operator!=( quat_t a, quat_t b ); |
| 68 |
|
| 69 |
float Length( void ); |
| 70 |
quat_t &Normalize( void ); |
| 71 |
|
| 72 |
quat_t operator-(); |
| 73 |
}; |
| 74 |
|
| 75 |
inline quat_t::quat_t() { |
| 76 |
} |
| 77 |
|
| 78 |
inline quat_t::quat_t( float x, float y, float z, float w ) { |
| 79 |
this->x = x; |
| 80 |
this->y = y; |
| 81 |
this->z = z; |
| 82 |
this->w = w; |
| 83 |
} |
| 84 |
|
| 85 |
inline float *quat_t::vec4( void ) { |
| 86 |
return &x; |
| 87 |
} |
| 88 |
|
| 89 |
inline float quat_t::operator[]( int index ) const { |
| 90 |
assert( ( index >= 0 ) && ( index < 4 ) ); |
| 91 |
return ( &x )[ index ]; |
| 92 |
} |
| 93 |
|
| 94 |
inline float& quat_t::operator[]( int index ) { |
| 95 |
assert( ( index >= 0 ) && ( index < 4 ) ); |
| 96 |
return ( &x )[ index ]; |
| 97 |
} |
| 98 |
|
| 99 |
inline void quat_t::set( float x, float y, float z, float w ) { |
| 100 |
this->x = x; |
| 101 |
this->y = y; |
| 102 |
this->z = z; |
| 103 |
this->w = w; |
| 104 |
} |
| 105 |
|
| 106 |
inline quat_t operator+( quat_t a, quat_t b ) { |
| 107 |
return quat_t( a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w ); |
| 108 |
} |
| 109 |
|
| 110 |
inline quat_t& quat_t::operator+=( quat_t a ) { |
| 111 |
x += a.x; |
| 112 |
y += a.y; |
| 113 |
z += a.z; |
| 114 |
w += a.w; |
| 115 |
|
| 116 |
return *this; |
| 117 |
} |
| 118 |
|
| 119 |
inline quat_t operator-( quat_t a, quat_t b ) { |
| 120 |
return quat_t( a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w ); |
| 121 |
} |
| 122 |
|
| 123 |
inline quat_t& quat_t::operator-=( quat_t a ) { |
| 124 |
x -= a.x; |
| 125 |
y -= a.y; |
| 126 |
z -= a.z; |
| 127 |
w -= a.w; |
| 128 |
|
| 129 |
return *this; |
| 130 |
} |
| 131 |
|
| 132 |
inline quat_t operator*( quat_t a, float b ) { |
| 133 |
return quat_t( a.x * b, a.y * b, a.z * b, a.w * b ); |
| 134 |
} |
| 135 |
|
| 136 |
inline quat_t operator*( float a, quat_t b ) { |
| 137 |
return b * a; |
| 138 |
} |
| 139 |
|
| 140 |
inline quat_t& quat_t::operator*=( float a ) { |
| 141 |
x *= a; |
| 142 |
y *= a; |
| 143 |
z *= a; |
| 144 |
w *= a; |
| 145 |
|
| 146 |
return *this; |
| 147 |
} |
| 148 |
|
| 149 |
inline int operator==( quat_t a, quat_t b ) { |
| 150 |
return ( ( a.x == b.x ) && ( a.y == b.y ) && ( a.z == b.z ) && ( a.w == b.w ) ); |
| 151 |
} |
| 152 |
|
| 153 |
inline int operator!=( quat_t a, quat_t b ) { |
| 154 |
return ( ( a.x != b.x ) || ( a.y != b.y ) || (( a.z != b.z ) && ( a.w != b.w )) ); |
| 155 |
} |
| 156 |
|
| 157 |
inline float quat_t::Length( void ) { |
| 158 |
float length; |
| 159 |
|
| 160 |
length = x * x + y * y + z * z + w * w; |
| 161 |
return ( float )sqrt( length ); |
| 162 |
} |
| 163 |
|
| 164 |
inline quat_t& quat_t::Normalize( void ) { |
| 165 |
float length; |
| 166 |
float ilength; |
| 167 |
|
| 168 |
length = this->Length(); |
| 169 |
if ( length ) { |
| 170 |
ilength = 1 / length; |
| 171 |
x *= ilength; |
| 172 |
y *= ilength; |
| 173 |
z *= ilength; |
| 174 |
w *= ilength; |
| 175 |
} |
| 176 |
|
| 177 |
return *this; |
| 178 |
} |
| 179 |
|
| 180 |
inline quat_t quat_t::operator-() { |
| 181 |
return quat_t( -x, -y, -z, -w ); |
| 182 |
} |
| 183 |
|
| 184 |
#endif |