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