00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __MATH_MATRIX_H__
00023 #define __MATH_MATRIX_H__
00024
00025 #include <string.h>
00026 #include "math_vector.h"
00027
00028 #ifndef ID_INLINE
00029 #ifdef _WIN32
00030 #define ID_INLINE __inline
00031 #else
00032 #define ID_INLINE inline
00033 #endif
00034 #endif
00035
00036 class quat_t;
00037 class angles_t;
00038
00039 class mat3_t {
00040 public:
00041 idVec3_t mat[ 3 ];
00042
00043 mat3_t();
00044 mat3_t( float src[ 3 ][ 3 ] );
00045 mat3_t( idVec3_t const &x, idVec3_t const &y, idVec3_t const &z );
00046 mat3_t( const float xx, const float xy, const float xz, const float yx, const float yy, const float yz, const float zx, const float zy, const float zz );
00047
00048 friend void toMatrix( quat_t const &src, mat3_t &dst );
00049 friend void toMatrix( angles_t const &src, mat3_t &dst );
00050 friend void toMatrix( idVec3_t const &src, mat3_t &dst );
00051
00052 idVec3_t operator[]( int index ) const;
00053 idVec3_t &operator[]( int index );
00054
00055 idVec3_t operator*( const idVec3_t &vec ) const;
00056 mat3_t operator*( const mat3_t &a ) const;
00057 mat3_t operator*( float a ) const;
00058 mat3_t operator+( mat3_t const &a ) const;
00059 mat3_t operator-( mat3_t const &a ) const;
00060
00061 friend idVec3_t operator*( const idVec3_t &vec, const mat3_t &mat );
00062 friend mat3_t operator*( float a, mat3_t const &b );
00063
00064 mat3_t &operator*=( float a );
00065 mat3_t &operator+=( mat3_t const &a );
00066 mat3_t &operator-=( mat3_t const &a );
00067
00068 void Clear( void );
00069
00070 void ProjectVector( const idVec3_t &src, idVec3_t &dst ) const;
00071 void UnprojectVector( const idVec3_t &src, idVec3_t &dst ) const;
00072
00073 void OrthoNormalize( void );
00074 void Transpose( mat3_t &matrix );
00075 void Transpose( void );
00076 mat3_t Inverse( void ) const;
00077 void Identity( void );
00078
00079 friend void InverseMultiply( const mat3_t &inv, const mat3_t &b, mat3_t &dst );
00080 friend mat3_t SkewSymmetric( idVec3_t const &src );
00081 };
00082
00083 ID_INLINE mat3_t::mat3_t() {
00084 }
00085
00086 ID_INLINE mat3_t::mat3_t( float src[ 3 ][ 3 ] ) {
00087 memcpy( mat, src, sizeof( src ) );
00088 }
00089
00090 ID_INLINE mat3_t::mat3_t( idVec3_t const &x, idVec3_t const &y, idVec3_t const &z ) {
00091 mat[ 0 ].x = x.x; mat[ 0 ].y = x.y; mat[ 0 ].z = x.z;
00092 mat[ 1 ].x = y.x; mat[ 1 ].y = y.y; mat[ 1 ].z = y.z;
00093 mat[ 2 ].x = z.x; mat[ 2 ].y = z.y; mat[ 2 ].z = z.z;
00094 }
00095
00096 ID_INLINE mat3_t::mat3_t( const float xx, const float xy, const float xz, const float yx, const float yy, const float yz, const float zx, const float zy, const float zz ) {
00097 mat[ 0 ].x = xx; mat[ 0 ].y = xy; mat[ 0 ].z = xz;
00098 mat[ 1 ].x = yx; mat[ 1 ].y = yy; mat[ 1 ].z = yz;
00099 mat[ 2 ].x = zx; mat[ 2 ].y = zy; mat[ 2 ].z = zz;
00100 }
00101
00102 ID_INLINE idVec3_t mat3_t::operator[]( int index ) const {
00103 assert( ( index >= 0 ) && ( index < 3 ) );
00104 return mat[ index ];
00105 }
00106
00107 ID_INLINE idVec3_t& mat3_t::operator[]( int index ) {
00108 assert( ( index >= 0 ) && ( index < 3 ) );
00109 return mat[ index ];
00110 }
00111
00112 ID_INLINE idVec3_t mat3_t::operator*( const idVec3_t &vec ) const {
00113 return idVec3_t(
00114 mat[ 0 ].x * vec.x + mat[ 1 ].x * vec.y + mat[ 2 ].x * vec.z,
00115 mat[ 0 ].y * vec.x + mat[ 1 ].y * vec.y + mat[ 2 ].y * vec.z,
00116 mat[ 0 ].z * vec.x + mat[ 1 ].z * vec.y + mat[ 2 ].z * vec.z );
00117 }
00118
00119 ID_INLINE mat3_t mat3_t::operator*( const mat3_t &a ) const {
00120 return mat3_t(
00121 mat[0].x * a[0].x + mat[0].y * a[1].x + mat[0].z * a[2].x,
00122 mat[0].x * a[0].y + mat[0].y * a[1].y + mat[0].z * a[2].y,
00123 mat[0].x * a[0].z + mat[0].y * a[1].z + mat[0].z * a[2].z,
00124 mat[1].x * a[0].x + mat[1].y * a[1].x + mat[1].z * a[2].x,
00125 mat[1].x * a[0].y + mat[1].y * a[1].y + mat[1].z * a[2].y,
00126 mat[1].x * a[0].z + mat[1].y * a[1].z + mat[1].z * a[2].z,
00127 mat[2].x * a[0].x + mat[2].y * a[1].x + mat[2].z * a[2].x,
00128 mat[2].x * a[0].y + mat[2].y * a[1].y + mat[2].z * a[2].y,
00129 mat[2].x * a[0].z + mat[2].y * a[1].z + mat[2].z * a[2].z );
00130 }
00131
00132 ID_INLINE mat3_t mat3_t::operator*( float a ) const {
00133 return mat3_t(
00134 mat[0].x * a, mat[0].y * a, mat[0].z * a,
00135 mat[1].x * a, mat[1].y * a, mat[1].z * a,
00136 mat[2].x * a, mat[2].y * a, mat[2].z * a );
00137 }
00138
00139 ID_INLINE mat3_t mat3_t::operator+( mat3_t const &a ) const {
00140 return mat3_t(
00141 mat[0].x + a[0].x, mat[0].y + a[0].y, mat[0].z + a[0].z,
00142 mat[1].x + a[1].x, mat[1].y + a[1].y, mat[1].z + a[1].z,
00143 mat[2].x + a[2].x, mat[2].y + a[2].y, mat[2].z + a[2].z );
00144 }
00145
00146 ID_INLINE mat3_t mat3_t::operator-( mat3_t const &a ) const {
00147 return mat3_t(
00148 mat[0].x - a[0].x, mat[0].y - a[0].y, mat[0].z - a[0].z,
00149 mat[1].x - a[1].x, mat[1].y - a[1].y, mat[1].z - a[1].z,
00150 mat[2].x - a[2].x, mat[2].y - a[2].y, mat[2].z - a[2].z );
00151 }
00152
00153 ID_INLINE idVec3_t operator*( const idVec3_t &vec, const mat3_t &mat ) {
00154 return idVec3_t(
00155 mat[ 0 ].x * vec.x + mat[ 1 ].x * vec.y + mat[ 2 ].x * vec.z,
00156 mat[ 0 ].y * vec.x + mat[ 1 ].y * vec.y + mat[ 2 ].y * vec.z,
00157 mat[ 0 ].z * vec.x + mat[ 1 ].z * vec.y + mat[ 2 ].z * vec.z );
00158 }
00159
00160 ID_INLINE mat3_t operator*( float a, mat3_t const &b ) {
00161 return mat3_t(
00162 b[0].x * a, b[0].y * a, b[0].z * a,
00163 b[1].x * a, b[1].y * a, b[1].z * a,
00164 b[2].x * a, b[2].y * a, b[2].z * a );
00165 }
00166
00167 ID_INLINE mat3_t &mat3_t::operator*=( float a ) {
00168 mat[0].x *= a; mat[0].y *= a; mat[0].z *= a;
00169 mat[1].x *= a; mat[1].y *= a; mat[1].z *= a;
00170 mat[2].x *= a; mat[2].y *= a; mat[2].z *= a;
00171
00172 return *this;
00173 }
00174
00175 ID_INLINE mat3_t &mat3_t::operator+=( mat3_t const &a ) {
00176 mat[0].x += a[0].x; mat[0].y += a[0].y; mat[0].z += a[0].z;
00177 mat[1].x += a[1].x; mat[1].y += a[1].y; mat[1].z += a[1].z;
00178 mat[2].x += a[2].x; mat[2].y += a[2].y; mat[2].z += a[2].z;
00179
00180 return *this;
00181 }
00182
00183 ID_INLINE mat3_t &mat3_t::operator-=( mat3_t const &a ) {
00184 mat[0].x -= a[0].x; mat[0].y -= a[0].y; mat[0].z -= a[0].z;
00185 mat[1].x -= a[1].x; mat[1].y -= a[1].y; mat[1].z -= a[1].z;
00186 mat[2].x -= a[2].x; mat[2].y -= a[2].y; mat[2].z -= a[2].z;
00187
00188 return *this;
00189 }
00190
00191 ID_INLINE void mat3_t::OrthoNormalize( void ) {
00192 mat[ 0 ].Normalize();
00193 mat[ 2 ].Cross( mat[ 0 ], mat[ 1 ] );
00194 mat[ 2 ].Normalize();
00195 mat[ 1 ].Cross( mat[ 2 ], mat[ 0 ] );
00196 mat[ 1 ].Normalize();
00197 }
00198
00199 ID_INLINE void mat3_t::Identity( void ) {
00200 mat[ 0 ].x = 1.f; mat[ 0 ].y = 0.f; mat[ 0 ].z = 0.f;
00201 mat[ 1 ].x = 0.f; mat[ 1 ].y = 1.f; mat[ 1 ].z = 0.f;
00202 mat[ 2 ].x = 0.f; mat[ 2 ].y = 0.f; mat[ 2 ].z = 1.f;
00203 }
00204
00205 ID_INLINE void InverseMultiply( const mat3_t &inv, const mat3_t &b, mat3_t &dst ) {
00206 dst[0].x = inv[0].x * b[0].x + inv[1].x * b[1].x + inv[2].x * b[2].x;
00207 dst[0].y = inv[0].x * b[0].y + inv[1].x * b[1].y + inv[2].x * b[2].y;
00208 dst[0].z = inv[0].x * b[0].z + inv[1].x * b[1].z + inv[2].x * b[2].z;
00209 dst[1].x = inv[0].y * b[0].x + inv[1].y * b[1].x + inv[2].y * b[2].x;
00210 dst[1].y = inv[0].y * b[0].y + inv[1].y * b[1].y + inv[2].y * b[2].y;
00211 dst[1].z = inv[0].y * b[0].z + inv[1].y * b[1].z + inv[2].y * b[2].z;
00212 dst[2].x = inv[0].z * b[0].x + inv[1].z * b[1].x + inv[2].z * b[2].x;
00213 dst[2].y = inv[0].z * b[0].y + inv[1].z * b[1].y + inv[2].z * b[2].y;
00214 dst[2].z = inv[0].z * b[0].z + inv[1].z * b[1].z + inv[2].z * b[2].z;
00215 }
00216
00217 ID_INLINE mat3_t SkewSymmetric( idVec3_t const &src ) {
00218 return mat3_t( 0.0f, -src.z, src.y, src.z, 0.0f, -src.x, -src.y, src.x, 0.0f );
00219 }
00220
00221 extern mat3_t mat3_default;
00222
00223 #endif