]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/splines/math_matrix.cpp
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / libs / splines / math_matrix.cpp
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 #include "q_shared.h"\r
23 \r
24 mat3_t mat3_default( idVec3( 1, 0, 0 ), idVec3( 0, 1, 0 ), idVec3( 0, 0, 1 ) );\r
25 \r
26 void toMatrix( quat_t const &src, mat3_t &dst ) {\r
27         float   wx, wy, wz;\r
28         float   xx, yy, yz;\r
29         float   xy, xz, zz;\r
30         float   x2, y2, z2;\r
31 \r
32         x2 = src.x + src.x;\r
33         y2 = src.y + src.y;\r
34         z2 = src.z + src.z;\r
35 \r
36         xx = src.x * x2;\r
37         xy = src.x * y2;\r
38         xz = src.x * z2;\r
39 \r
40         yy = src.y * y2;\r
41         yz = src.y * z2;\r
42         zz = src.z * z2;\r
43 \r
44         wx = src.w * x2;\r
45         wy = src.w * y2;\r
46         wz = src.w * z2;\r
47 \r
48         dst[ 0 ][ 0 ] = 1.0f - ( yy + zz );\r
49         dst[ 0 ][ 1 ] = xy - wz;\r
50         dst[ 0 ][ 2 ] = xz + wy;\r
51 \r
52         dst[ 1 ][ 0 ] = xy + wz;\r
53         dst[ 1 ][ 1 ] = 1.0f - ( xx + zz );\r
54         dst[ 1 ][ 2 ] = yz - wx;\r
55 \r
56         dst[ 2 ][ 0 ] = xz - wy;\r
57         dst[ 2 ][ 1 ] = yz + wx;\r
58         dst[ 2 ][ 2 ] = 1.0f - ( xx + yy );\r
59 }\r
60 \r
61 void toMatrix( angles_t const &src, mat3_t &dst ) {\r
62         float                   angle;\r
63         static float    sr, sp, sy, cr, cp, cy; // static to help MS compiler fp bugs\r
64                 \r
65         angle = src.yaw * ( M_PI * 2.0f / 360.0f );\r
66         sy = sin( angle );\r
67         cy = cos( angle );\r
68 \r
69         angle = src.pitch * ( M_PI * 2.0f / 360.0f );\r
70         sp = sin( angle );\r
71         cp = cos( angle );\r
72 \r
73         angle = src.roll * ( M_PI * 2.0f / 360.0f );\r
74         sr = sin( angle );\r
75         cr = cos( angle );\r
76 \r
77         dst[ 0 ].set( cp * cy, cp * sy, -sp );\r
78         dst[ 1 ].set( sr * sp * cy + cr * -sy, sr * sp * sy + cr * cy, sr * cp );\r
79         dst[ 2 ].set( cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp );\r
80 }\r
81 \r
82 void toMatrix( idVec3 const &src, mat3_t &dst ) {\r
83         angles_t sup = src;\r
84         toMatrix(sup, dst);\r
85 }\r
86 \r
87 void mat3_t::ProjectVector( const idVec3 &src, idVec3 &dst ) const {\r
88         dst.x = src * mat[ 0 ];\r
89         dst.y = src * mat[ 1 ];\r
90         dst.z = src * mat[ 2 ];\r
91 }\r
92 \r
93 void mat3_t::UnprojectVector( const idVec3 &src, idVec3 &dst ) const {\r
94         dst = mat[ 0 ] * src.x + mat[ 1 ] * src.y + mat[ 2 ] * src.z;\r
95 }\r
96 \r
97 void mat3_t::Transpose( mat3_t &matrix ) {\r
98         int     i;\r
99         int     j;\r
100    \r
101         for( i = 0; i < 3; i++ ) {\r
102                 for( j = 0; j < 3; j++ ) {\r
103                         matrix[ i ][ j ] = mat[ j ][ i ];\r
104         }\r
105         }\r
106 }\r
107 \r
108 void mat3_t::Transpose( void ) {\r
109         float   temp;\r
110         int             i;\r
111         int             j;\r
112    \r
113         for( i = 0; i < 3; i++ ) {\r
114                 for( j = i + 1; j < 3; j++ ) {\r
115                         temp = mat[ i ][ j ];\r
116                         mat[ i ][ j ] = mat[ j ][ i ];\r
117                         mat[ j ][ i ] = temp;\r
118         }\r
119         }\r
120 }\r
121 \r
122 mat3_t mat3_t::Inverse( void ) const {\r
123         mat3_t inv( *this );\r
124 \r
125         inv.Transpose();\r
126 \r
127         return inv;\r
128 }\r
129 \r
130 void mat3_t::Clear( void ) {\r
131         mat[0].set( 1, 0, 0 );\r
132         mat[1].set( 0, 1, 0 );\r
133         mat[2].set( 0, 0, 1 );\r
134 }\r