]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/splines/math_vector.cpp
7237de5252778f8d6192687092be8309bc058a3f
[xonotic/netradiant.git] / libs / splines / math_vector.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 "math_vector.h"\r
23 #include <assert.h>\r
24 #include <math.h>\r
25 #include <stdio.h>\r
26 #include <stdarg.h>\r
27 #include <string.h>\r
28 #include <stdlib.h>\r
29 #include <time.h>\r
30 #include <ctype.h>\r
31 \r
32 #define M_PI            3.14159265358979323846  // matches value in gcc v2 math.h\r
33 \r
34 #define LERP_DELTA 1e-6\r
35 \r
36 idVec3 vec_zero( 0.0f, 0.0f, 0.0f );\r
37 \r
38 Bounds  boundsZero;\r
39 \r
40 float idVec3::toYaw( void ) {\r
41         float yaw;\r
42         \r
43         if ( ( y == 0 ) && ( x == 0 ) ) {\r
44                 yaw = 0;\r
45         } else {\r
46                 yaw = atan2( y, x ) * 180 / M_PI;\r
47                 if ( yaw < 0 ) {\r
48                         yaw += 360;\r
49                 }\r
50         }\r
51 \r
52         return yaw;\r
53 }\r
54 \r
55 float idVec3::toPitch( void ) {\r
56         float   forward;\r
57         float   pitch;\r
58         \r
59         if ( ( x == 0 ) && ( y == 0 ) ) {\r
60                 if ( z > 0 ) {\r
61                         pitch = 90;\r
62                 } else {\r
63                         pitch = 270;\r
64                 }\r
65         } else {\r
66                 forward = ( float )idSqrt( x * x + y * y );\r
67                 pitch = atan2( z, forward ) * 180 / M_PI;\r
68                 if ( pitch < 0 ) {\r
69                         pitch += 360;\r
70                 }\r
71         }\r
72 \r
73         return pitch;\r
74 }\r
75 \r
76 /*\r
77 angles_t idVec3::toAngles( void ) {\r
78         float forward;\r
79         float yaw;\r
80         float pitch;\r
81         \r
82         if ( ( x == 0 ) && ( y == 0 ) ) {\r
83                 yaw = 0;\r
84                 if ( z > 0 ) {\r
85                         pitch = 90;\r
86                 } else {\r
87                         pitch = 270;\r
88                 }\r
89         } else {\r
90                 yaw = atan2( y, x ) * 180 / M_PI;\r
91                 if ( yaw < 0 ) {\r
92                         yaw += 360;\r
93                 }\r
94 \r
95                 forward = ( float )idSqrt( x * x + y * y );\r
96                 pitch = atan2( z, forward ) * 180 / M_PI;\r
97                 if ( pitch < 0 ) {\r
98                         pitch += 360;\r
99                 }\r
100         }\r
101 \r
102         return angles_t( -pitch, yaw, 0 );\r
103 }\r
104 */\r
105 \r
106 idVec3 LerpVector( idVec3 &w1, idVec3 &w2, const float t ) {\r
107         float omega, cosom, sinom, scale0, scale1;\r
108 \r
109         cosom = w1 * w2;\r
110         if ( ( 1.0 - cosom ) > LERP_DELTA ) {\r
111                 omega = acos( cosom );\r
112                 sinom = sin( omega );\r
113                 scale0 = sin( ( 1.0 - t ) * omega ) / sinom;\r
114                 scale1 = sin( t * omega ) / sinom;\r
115         } else {\r
116                 scale0 = 1.0 - t;\r
117                 scale1 = t;\r
118         }\r
119 \r
120         return ( w1 * scale0 + w2 * scale1 );\r
121 }\r
122 \r
123 /*\r
124 =============\r
125 idVec3::string\r
126 \r
127 This is just a convenience function\r
128 for printing vectors\r
129 =============\r
130 */\r
131 char *idVec3::string( void ) {\r
132         static  int             index = 0;\r
133         static  char    str[ 8 ][ 36 ];\r
134         char    *s;\r
135 \r
136         // use an array so that multiple toString's won't collide\r
137         s = str[ index ];\r
138         index = (index + 1)&7;\r
139 \r
140         sprintf( s, "%.2f %.2f %.2f", x, y, z );\r
141 \r
142         return s;\r
143 }\r