]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/splines/math_matrix.h
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / libs / splines / math_matrix.h
1 /*\r
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.\r
4 \r
5 This file is part of GtkRadiant.\r
6 \r
7 GtkRadiant is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 GtkRadiant is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with GtkRadiant; if not, write to the Free Software\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
20 */\r
21 \r
22 #ifndef __MATH_MATRIX_H__\r
23 #define __MATH_MATRIX_H__\r
24 \r
25 #include <string.h>\r
26 #include "math_vector.h"\r
27 \r
28 #ifndef ID_INLINE\r
29 #ifdef _WIN32\r
30 #define ID_INLINE __inline \r
31 #else\r
32 #define ID_INLINE inline\r
33 #endif\r
34 #endif\r
35 \r
36 class quat_t;\r
37 class angles_t;\r
38 \r
39 class mat3_t {\r
40 public:\r
41         idVec3                  mat[ 3 ];\r
42 \r
43                                         mat3_t();\r
44                                         mat3_t( float src[ 3 ][ 3 ] );\r
45                                         mat3_t( idVec3 const &x, idVec3 const &y, idVec3 const &z );\r
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 );\r
47 \r
48         friend void             toMatrix( quat_t const &src, mat3_t &dst );\r
49         friend void             toMatrix( angles_t const &src, mat3_t &dst );\r
50         friend void             toMatrix( idVec3 const &src, mat3_t &dst );\r
51 \r
52         idVec3                  operator[]( int index ) const;\r
53         idVec3                  &operator[]( int index );\r
54 \r
55         idVec3                  operator*( const idVec3 &vec ) const;\r
56         mat3_t                  operator*( const mat3_t &a ) const;\r
57         mat3_t                  operator*( float a ) const;\r
58         mat3_t                  operator+( mat3_t const &a ) const;\r
59         mat3_t                  operator-( mat3_t const &a ) const;\r
60 \r
61         friend idVec3   operator*( const idVec3 &vec, const mat3_t &mat );\r
62         friend mat3_t   operator*( float a, mat3_t const &b );\r
63 \r
64         mat3_t                  &operator*=( float a );\r
65         mat3_t                  &operator+=( mat3_t const &a );\r
66         mat3_t                  &operator-=( mat3_t const &a );\r
67 \r
68         void                    Clear( void );\r
69 \r
70         void                    ProjectVector( const idVec3 &src, idVec3 &dst ) const;\r
71         void                    UnprojectVector( const idVec3 &src, idVec3 &dst ) const;\r
72 \r
73         void                    OrthoNormalize( void );\r
74         void                    Transpose( mat3_t &matrix );\r
75         void                    Transpose( void );\r
76         mat3_t                  Inverse( void ) const;\r
77         void                    Identity( void );\r
78 \r
79         friend void             InverseMultiply( const mat3_t &inv, const mat3_t &b, mat3_t &dst );\r
80         friend mat3_t   SkewSymmetric( idVec3 const &src );\r
81 };\r
82 \r
83 ID_INLINE mat3_t::mat3_t() {\r
84 }\r
85 \r
86 ID_INLINE mat3_t::mat3_t( float src[ 3 ][ 3 ] ) {\r
87         memcpy( mat, src, sizeof( src ) );\r
88 }\r
89 \r
90 ID_INLINE mat3_t::mat3_t( idVec3 const &x, idVec3 const &y, idVec3 const &z ) {\r
91         mat[ 0 ].x = x.x; mat[ 0 ].y = x.y; mat[ 0 ].z = x.z;\r
92         mat[ 1 ].x = y.x; mat[ 1 ].y = y.y; mat[ 1 ].z = y.z;\r
93         mat[ 2 ].x = z.x; mat[ 2 ].y = z.y; mat[ 2 ].z = z.z;\r
94 }\r
95 \r
96 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 ) {\r
97         mat[ 0 ].x = xx; mat[ 0 ].y = xy; mat[ 0 ].z = xz;\r
98         mat[ 1 ].x = yx; mat[ 1 ].y = yy; mat[ 1 ].z = yz;\r
99         mat[ 2 ].x = zx; mat[ 2 ].y = zy; mat[ 2 ].z = zz;\r
100 }\r
101 \r
102 ID_INLINE idVec3 mat3_t::operator[]( int index ) const {\r
103         assert( ( index >= 0 ) && ( index < 3 ) );\r
104         return mat[ index ];\r
105 }\r
106 \r
107 ID_INLINE idVec3& mat3_t::operator[]( int index ) {\r
108         assert( ( index >= 0 ) && ( index < 3 ) );\r
109         return mat[ index ];\r
110 }\r
111 \r
112 ID_INLINE idVec3 mat3_t::operator*( const idVec3 &vec ) const {\r
113         return idVec3( \r
114                 mat[ 0 ].x * vec.x + mat[ 1 ].x * vec.y + mat[ 2 ].x * vec.z,\r
115                 mat[ 0 ].y * vec.x + mat[ 1 ].y * vec.y + mat[ 2 ].y * vec.z,\r
116                 mat[ 0 ].z * vec.x + mat[ 1 ].z * vec.y + mat[ 2 ].z * vec.z );\r
117 }\r
118 \r
119 ID_INLINE mat3_t mat3_t::operator*( const mat3_t &a ) const {\r
120         return mat3_t( \r
121                 mat[0].x * a[0].x + mat[0].y * a[1].x + mat[0].z * a[2].x,\r
122                 mat[0].x * a[0].y + mat[0].y * a[1].y + mat[0].z * a[2].y,\r
123                 mat[0].x * a[0].z + mat[0].y * a[1].z + mat[0].z * a[2].z,\r
124                 mat[1].x * a[0].x + mat[1].y * a[1].x + mat[1].z * a[2].x,\r
125                 mat[1].x * a[0].y + mat[1].y * a[1].y + mat[1].z * a[2].y,\r
126                 mat[1].x * a[0].z + mat[1].y * a[1].z + mat[1].z * a[2].z,\r
127                 mat[2].x * a[0].x + mat[2].y * a[1].x + mat[2].z * a[2].x,\r
128                 mat[2].x * a[0].y + mat[2].y * a[1].y + mat[2].z * a[2].y,\r
129                 mat[2].x * a[0].z + mat[2].y * a[1].z + mat[2].z * a[2].z );\r
130 }\r
131 \r
132 ID_INLINE mat3_t mat3_t::operator*( float a ) const {\r
133         return mat3_t( \r
134                 mat[0].x * a, mat[0].y * a, mat[0].z * a, \r
135                 mat[1].x * a, mat[1].y * a, mat[1].z * a, \r
136                 mat[2].x * a, mat[2].y * a, mat[2].z * a );\r
137 }\r
138 \r
139 ID_INLINE mat3_t mat3_t::operator+( mat3_t const &a ) const {\r
140         return mat3_t( \r
141                 mat[0].x + a[0].x, mat[0].y + a[0].y, mat[0].z + a[0].z, \r
142                 mat[1].x + a[1].x, mat[1].y + a[1].y, mat[1].z + a[1].z, \r
143                 mat[2].x + a[2].x, mat[2].y + a[2].y, mat[2].z + a[2].z );\r
144 }\r
145     \r
146 ID_INLINE mat3_t mat3_t::operator-( mat3_t const &a ) const {\r
147         return mat3_t( \r
148                 mat[0].x - a[0].x, mat[0].y - a[0].y, mat[0].z - a[0].z, \r
149                 mat[1].x - a[1].x, mat[1].y - a[1].y, mat[1].z - a[1].z, \r
150                 mat[2].x - a[2].x, mat[2].y - a[2].y, mat[2].z - a[2].z );\r
151 }\r
152 \r
153 ID_INLINE idVec3 operator*( const idVec3 &vec, const mat3_t &mat ) {\r
154         return idVec3( \r
155                 mat[ 0 ].x * vec.x + mat[ 1 ].x * vec.y + mat[ 2 ].x * vec.z,\r
156                 mat[ 0 ].y * vec.x + mat[ 1 ].y * vec.y + mat[ 2 ].y * vec.z,\r
157                 mat[ 0 ].z * vec.x + mat[ 1 ].z * vec.y + mat[ 2 ].z * vec.z );\r
158 }\r
159 \r
160 ID_INLINE mat3_t operator*( float a, mat3_t const &b ) {\r
161         return mat3_t( \r
162                 b[0].x * a, b[0].y * a, b[0].z * a, \r
163                 b[1].x * a, b[1].y * a, b[1].z * a, \r
164                 b[2].x * a, b[2].y * a, b[2].z * a );\r
165 }\r
166 \r
167 ID_INLINE mat3_t &mat3_t::operator*=( float a ) {\r
168         mat[0].x *= a; mat[0].y *= a; mat[0].z *= a;\r
169         mat[1].x *= a; mat[1].y *= a; mat[1].z *= a; \r
170         mat[2].x *= a; mat[2].y *= a; mat[2].z *= a;\r
171 \r
172     return *this;\r
173 }\r
174 \r
175 ID_INLINE mat3_t &mat3_t::operator+=( mat3_t const &a ) {\r
176         mat[0].x += a[0].x; mat[0].y += a[0].y; mat[0].z += a[0].z;\r
177         mat[1].x += a[1].x; mat[1].y += a[1].y; mat[1].z += a[1].z;\r
178         mat[2].x += a[2].x; mat[2].y += a[2].y; mat[2].z += a[2].z;\r
179 \r
180     return *this;\r
181 }\r
182 \r
183 ID_INLINE mat3_t &mat3_t::operator-=( mat3_t const &a ) {\r
184         mat[0].x -= a[0].x; mat[0].y -= a[0].y; mat[0].z -= a[0].z;\r
185         mat[1].x -= a[1].x; mat[1].y -= a[1].y; mat[1].z -= a[1].z;\r
186         mat[2].x -= a[2].x; mat[2].y -= a[2].y; mat[2].z -= a[2].z;\r
187 \r
188     return *this;\r
189 }\r
190 \r
191 ID_INLINE void mat3_t::OrthoNormalize( void ) {\r
192         mat[ 0 ].Normalize();\r
193         mat[ 2 ].Cross( mat[ 0 ], mat[ 1 ] );\r
194         mat[ 2 ].Normalize();\r
195         mat[ 1 ].Cross( mat[ 2 ], mat[ 0 ] );\r
196         mat[ 1 ].Normalize();\r
197 }\r
198 \r
199 ID_INLINE void mat3_t::Identity( void ) {\r
200         mat[ 0 ].x = 1.f; mat[ 0 ].y = 0.f; mat[ 0 ].z = 0.f;\r
201         mat[ 1 ].x = 0.f; mat[ 1 ].y = 1.f; mat[ 1 ].z = 0.f;\r
202         mat[ 2 ].x = 0.f; mat[ 2 ].y = 0.f; mat[ 2 ].z = 1.f;\r
203 }\r
204 \r
205 ID_INLINE void InverseMultiply( const mat3_t &inv, const mat3_t &b, mat3_t &dst ) {\r
206         dst[0].x = inv[0].x * b[0].x + inv[1].x * b[1].x + inv[2].x * b[2].x;\r
207         dst[0].y = inv[0].x * b[0].y + inv[1].x * b[1].y + inv[2].x * b[2].y;\r
208         dst[0].z = inv[0].x * b[0].z + inv[1].x * b[1].z + inv[2].x * b[2].z;\r
209         dst[1].x = inv[0].y * b[0].x + inv[1].y * b[1].x + inv[2].y * b[2].x;\r
210         dst[1].y = inv[0].y * b[0].y + inv[1].y * b[1].y + inv[2].y * b[2].y;\r
211         dst[1].z = inv[0].y * b[0].z + inv[1].y * b[1].z + inv[2].y * b[2].z;\r
212         dst[2].x = inv[0].z * b[0].x + inv[1].z * b[1].x + inv[2].z * b[2].x;\r
213         dst[2].y = inv[0].z * b[0].y + inv[1].z * b[1].y + inv[2].z * b[2].y;\r
214         dst[2].z = inv[0].z * b[0].z + inv[1].z * b[1].z + inv[2].z * b[2].z;\r
215 }\r
216 \r
217 ID_INLINE mat3_t SkewSymmetric( idVec3 const &src ) {\r
218         return mat3_t( 0.0f, -src.z,  src.y, src.z,   0.0f, -src.x, -src.y,  src.x,   0.0f );\r
219 }\r
220 \r
221 extern mat3_t mat3_default;\r
222 \r
223 #endif /* !__MATH_MATRIX_H__ */\r