2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef __MATH_MATRIX_H__
23 #define __MATH_MATRIX_H__
26 #include "math_vector.h"
30 #define ID_INLINE __inline
32 #define ID_INLINE inline
44 mat3_t( float src[ 3 ][ 3 ] );
45 mat3_t( idVec3 const &x, idVec3 const &y, idVec3 const &z );
46 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 );
48 friend void toMatrix( quat_t const &src, mat3_t &dst );
49 friend void toMatrix( angles_t const &src, mat3_t &dst );
50 friend void toMatrix( idVec3 const &src, mat3_t &dst );
52 idVec3 operator[]( int index ) const;
53 idVec3 &operator[]( int index );
55 idVec3 operator*( const idVec3 &vec ) const;
56 mat3_t operator*( const mat3_t &a ) const;
57 mat3_t operator*( float a ) const;
58 mat3_t operator+( mat3_t const &a ) const;
59 mat3_t operator-( mat3_t const &a ) const;
61 friend idVec3 operator*( const idVec3 &vec, const mat3_t &mat );
62 friend mat3_t operator*( float a, mat3_t const &b );
64 mat3_t &operator*=( float a );
65 mat3_t &operator+=( mat3_t const &a );
66 mat3_t &operator-=( mat3_t const &a );
70 void ProjectVector( const idVec3 &src, idVec3 &dst ) const;
71 void UnprojectVector( const idVec3 &src, idVec3 &dst ) const;
73 void OrthoNormalize( void );
74 void Transpose( mat3_t &matrix );
75 void Transpose( void );
76 mat3_t Inverse( void ) const;
77 void Identity( void );
79 friend void InverseMultiply( const mat3_t &inv, const mat3_t &b, mat3_t &dst );
80 friend mat3_t SkewSymmetric( idVec3 const &src );
83 ID_INLINE mat3_t::mat3_t() {
86 ID_INLINE mat3_t::mat3_t( float src[ 3 ][ 3 ] ) {
87 //memcpy( mat, src, sizeof( src ) );
88 for( unsigned int i = 0; i < 3; i++ ) {
95 ID_INLINE mat3_t::mat3_t( idVec3 const &x, idVec3 const &y, idVec3 const &z ) {
96 mat[ 0 ].x = x.x; mat[ 0 ].y = x.y; mat[ 0 ].z = x.z;
97 mat[ 1 ].x = y.x; mat[ 1 ].y = y.y; mat[ 1 ].z = y.z;
98 mat[ 2 ].x = z.x; mat[ 2 ].y = z.y; mat[ 2 ].z = z.z;
101 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 ) {
102 mat[ 0 ].x = xx; mat[ 0 ].y = xy; mat[ 0 ].z = xz;
103 mat[ 1 ].x = yx; mat[ 1 ].y = yy; mat[ 1 ].z = yz;
104 mat[ 2 ].x = zx; mat[ 2 ].y = zy; mat[ 2 ].z = zz;
107 ID_INLINE idVec3 mat3_t::operator[]( int index ) const {
108 assert( ( index >= 0 ) && ( index < 3 ) );
112 ID_INLINE idVec3& mat3_t::operator[]( int index ) {
113 assert( ( index >= 0 ) && ( index < 3 ) );
117 ID_INLINE idVec3 mat3_t::operator*( const idVec3 &vec ) const {
119 mat[ 0 ].x * vec.x + mat[ 1 ].x * vec.y + mat[ 2 ].x * vec.z,
120 mat[ 0 ].y * vec.x + mat[ 1 ].y * vec.y + mat[ 2 ].y * vec.z,
121 mat[ 0 ].z * vec.x + mat[ 1 ].z * vec.y + mat[ 2 ].z * vec.z );
124 ID_INLINE mat3_t mat3_t::operator*( const mat3_t &a ) const {
126 mat[0].x * a[0].x + mat[0].y * a[1].x + mat[0].z * a[2].x,
127 mat[0].x * a[0].y + mat[0].y * a[1].y + mat[0].z * a[2].y,
128 mat[0].x * a[0].z + mat[0].y * a[1].z + mat[0].z * a[2].z,
129 mat[1].x * a[0].x + mat[1].y * a[1].x + mat[1].z * a[2].x,
130 mat[1].x * a[0].y + mat[1].y * a[1].y + mat[1].z * a[2].y,
131 mat[1].x * a[0].z + mat[1].y * a[1].z + mat[1].z * a[2].z,
132 mat[2].x * a[0].x + mat[2].y * a[1].x + mat[2].z * a[2].x,
133 mat[2].x * a[0].y + mat[2].y * a[1].y + mat[2].z * a[2].y,
134 mat[2].x * a[0].z + mat[2].y * a[1].z + mat[2].z * a[2].z );
137 ID_INLINE mat3_t mat3_t::operator*( float a ) const {
139 mat[0].x * a, mat[0].y * a, mat[0].z * a,
140 mat[1].x * a, mat[1].y * a, mat[1].z * a,
141 mat[2].x * a, mat[2].y * a, mat[2].z * a );
144 ID_INLINE mat3_t mat3_t::operator+( mat3_t const &a ) const {
146 mat[0].x + a[0].x, mat[0].y + a[0].y, mat[0].z + a[0].z,
147 mat[1].x + a[1].x, mat[1].y + a[1].y, mat[1].z + a[1].z,
148 mat[2].x + a[2].x, mat[2].y + a[2].y, mat[2].z + a[2].z );
151 ID_INLINE mat3_t mat3_t::operator-( mat3_t const &a ) const {
153 mat[0].x - a[0].x, mat[0].y - a[0].y, mat[0].z - a[0].z,
154 mat[1].x - a[1].x, mat[1].y - a[1].y, mat[1].z - a[1].z,
155 mat[2].x - a[2].x, mat[2].y - a[2].y, mat[2].z - a[2].z );
158 ID_INLINE idVec3 operator*( const idVec3 &vec, const mat3_t &mat ) {
160 mat[ 0 ].x * vec.x + mat[ 1 ].x * vec.y + mat[ 2 ].x * vec.z,
161 mat[ 0 ].y * vec.x + mat[ 1 ].y * vec.y + mat[ 2 ].y * vec.z,
162 mat[ 0 ].z * vec.x + mat[ 1 ].z * vec.y + mat[ 2 ].z * vec.z );
165 ID_INLINE mat3_t operator*( float a, mat3_t const &b ) {
167 b[0].x * a, b[0].y * a, b[0].z * a,
168 b[1].x * a, b[1].y * a, b[1].z * a,
169 b[2].x * a, b[2].y * a, b[2].z * a );
172 ID_INLINE mat3_t &mat3_t::operator*=( float a ) {
173 mat[0].x *= a; mat[0].y *= a; mat[0].z *= a;
174 mat[1].x *= a; mat[1].y *= a; mat[1].z *= a;
175 mat[2].x *= a; mat[2].y *= a; mat[2].z *= a;
180 ID_INLINE mat3_t &mat3_t::operator+=( mat3_t const &a ) {
181 mat[0].x += a[0].x; mat[0].y += a[0].y; mat[0].z += a[0].z;
182 mat[1].x += a[1].x; mat[1].y += a[1].y; mat[1].z += a[1].z;
183 mat[2].x += a[2].x; mat[2].y += a[2].y; mat[2].z += a[2].z;
188 ID_INLINE mat3_t &mat3_t::operator-=( mat3_t const &a ) {
189 mat[0].x -= a[0].x; mat[0].y -= a[0].y; mat[0].z -= a[0].z;
190 mat[1].x -= a[1].x; mat[1].y -= a[1].y; mat[1].z -= a[1].z;
191 mat[2].x -= a[2].x; mat[2].y -= a[2].y; mat[2].z -= a[2].z;
196 ID_INLINE void mat3_t::OrthoNormalize( void ) {
197 mat[ 0 ].Normalize();
198 mat[ 2 ].Cross( mat[ 0 ], mat[ 1 ] );
199 mat[ 2 ].Normalize();
200 mat[ 1 ].Cross( mat[ 2 ], mat[ 0 ] );
201 mat[ 1 ].Normalize();
204 ID_INLINE void mat3_t::Identity( void ) {
205 mat[ 0 ].x = 1.f; mat[ 0 ].y = 0.f; mat[ 0 ].z = 0.f;
206 mat[ 1 ].x = 0.f; mat[ 1 ].y = 1.f; mat[ 1 ].z = 0.f;
207 mat[ 2 ].x = 0.f; mat[ 2 ].y = 0.f; mat[ 2 ].z = 1.f;
210 ID_INLINE void InverseMultiply( const mat3_t &inv, const mat3_t &b, mat3_t &dst ) {
211 dst[0].x = inv[0].x * b[0].x + inv[1].x * b[1].x + inv[2].x * b[2].x;
212 dst[0].y = inv[0].x * b[0].y + inv[1].x * b[1].y + inv[2].x * b[2].y;
213 dst[0].z = inv[0].x * b[0].z + inv[1].x * b[1].z + inv[2].x * b[2].z;
214 dst[1].x = inv[0].y * b[0].x + inv[1].y * b[1].x + inv[2].y * b[2].x;
215 dst[1].y = inv[0].y * b[0].y + inv[1].y * b[1].y + inv[2].y * b[2].y;
216 dst[1].z = inv[0].y * b[0].z + inv[1].y * b[1].z + inv[2].y * b[2].z;
217 dst[2].x = inv[0].z * b[0].x + inv[1].z * b[1].x + inv[2].z * b[2].x;
218 dst[2].y = inv[0].z * b[0].y + inv[1].z * b[1].y + inv[2].z * b[2].y;
219 dst[2].z = inv[0].z * b[0].z + inv[1].z * b[1].z + inv[2].z * b[2].z;
222 ID_INLINE mat3_t SkewSymmetric( idVec3 const &src ) {
223 return mat3_t( 0.0f, -src.z, src.y, src.z, 0.0f, -src.x, -src.y, src.x, 0.0f );
226 extern mat3_t mat3_default;
228 #endif /* !__MATH_MATRIX_H__ */