]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/splines/math_angles.cpp
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / libs / splines / math_angles.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 #include <float.h>\r
24 \r
25 angles_t ang_zero( 0.0f, 0.0f, 0.0f );\r
26 \r
27 void toAngles( mat3_t &src, angles_t &dst ) {\r
28         double          theta;\r
29         double          cp;\r
30         double          sp;\r
31 \r
32         sp = src[ 0 ][ 2 ];\r
33 \r
34         // cap off our sin value so that we don't get any NANs\r
35         if ( sp > 1.0 ) {\r
36                 sp = 1.0;\r
37         } else if ( sp < -1.0 ) {\r
38                 sp = -1.0;\r
39         }\r
40 \r
41         theta = -asin( sp );\r
42         cp = cos( theta );\r
43 \r
44         if ( cp > 8192 * FLT_EPSILON ) {\r
45                 dst.pitch       = theta * 180 / M_PI;\r
46                 dst.yaw         = atan2( src[ 0 ][ 1 ], src[ 0 ][ 0 ] ) * 180 / M_PI;\r
47                 dst.roll        = atan2( src[ 1 ][ 2 ], src[ 2 ][ 2 ] ) * 180 / M_PI;\r
48         } else {\r
49                 dst.pitch       = theta * 180 / M_PI;\r
50                 dst.yaw         = -atan2( src[ 1 ][ 0 ], src[ 1 ][ 1 ] ) * 180 / M_PI;\r
51                 dst.roll        = 0;\r
52         }\r
53 }\r
54 \r
55 void toAngles( quat_t &src, angles_t &dst ) {\r
56         mat3_t temp;\r
57 \r
58         toMatrix( src, temp );\r
59         toAngles( temp, dst );\r
60 }\r
61 \r
62 void toAngles( idVec3 &src, angles_t &dst ) {\r
63         dst.pitch       = src[ 0 ];\r
64         dst.yaw         = src[ 1 ];\r
65         dst.roll        = src[ 2 ];\r
66 }\r
67 \r
68 void angles_t::toVectors( idVec3 *forward, idVec3 *right, idVec3 *up ) {\r
69         float                   angle;\r
70         static float    sr, sp, sy, cr, cp, cy; // static to help MS compiler fp bugs\r
71         \r
72         angle = yaw * ( M_PI * 2 / 360 );\r
73         sy = sin( angle );\r
74         cy = cos( angle );\r
75 \r
76         angle = pitch * ( M_PI * 2 / 360 );\r
77         sp = sin( angle );\r
78         cp = cos( angle );\r
79 \r
80         angle = roll * ( M_PI * 2 / 360 );\r
81         sr = sin( angle );\r
82         cr = cos( angle );\r
83 \r
84         if ( forward ) {\r
85                 forward->set( cp * cy, cp * sy, -sp );\r
86         }\r
87 \r
88         if ( right ) {\r
89                 right->set( -sr * sp * cy + cr * sy, -sr * sp * sy + -cr * cy, -sr * cp );\r
90         }\r
91 \r
92         if ( up ) {\r
93                 up->set( cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp );\r
94         }\r
95 }\r
96 \r
97 idVec3 angles_t::toForward( void ) {\r
98         float                   angle;\r
99         static float    sp, sy, cp, cy; // static to help MS compiler fp bugs\r
100         \r
101         angle = yaw * ( M_PI * 2 / 360 );\r
102         sy = sin( angle );\r
103         cy = cos( angle );\r
104 \r
105         angle = pitch * ( M_PI * 2 / 360 );\r
106         sp = sin( angle );\r
107         cp = cos( angle );\r
108 \r
109         return idVec3( cp * cy, cp * sy, -sp );\r
110 }\r
111 \r
112 /*\r
113 =================\r
114 Normalize360\r
115 \r
116 returns angles normalized to the range [0 <= angle < 360]\r
117 =================\r
118 */\r
119 angles_t& angles_t::Normalize360( void ) {\r
120         pitch   = (360.0 / 65536) * ( ( int )( pitch    * ( 65536 / 360.0 ) ) & 65535 );\r
121         yaw             = (360.0 / 65536) * ( ( int )( yaw              * ( 65536 / 360.0 ) ) & 65535 );\r
122         roll    = (360.0 / 65536) * ( ( int )( roll             * ( 65536 / 360.0 ) ) & 65535 );\r
123 \r
124         return *this;\r
125 }\r
126 \r
127 \r
128 /*\r
129 =================\r
130 Normalize180\r
131 \r
132 returns angles normalized to the range [-180 < angle <= 180]\r
133 =================\r
134 */\r
135 angles_t& angles_t::Normalize180( void ) {\r
136         Normalize360();\r
137 \r
138         if ( pitch > 180.0 ) {\r
139                 pitch -= 360.0;\r
140         }\r
141         \r
142         if ( yaw > 180.0 ) {\r
143                 yaw  -= 360.0;\r
144         }\r
145 \r
146         if ( roll > 180.0 ) {\r
147                 roll -= 360.0;\r
148         }\r
149         return *this;\r
150 }\r