]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/common/mathlib.c
Remove -Wno-sign-compare
[xonotic/netradiant.git] / tools / quake2 / common / mathlib.c
1 /*
2    Copyright (C) 1999-2006 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 // mathlib.c -- math primitives
22
23 #include "cmdlib.h"
24 #include "mathlib.h"
25
26 vec3_t vec3_origin = {0,0,0};
27
28
29 double VectorLength( vec3_t v ){
30         int i;
31         double length;
32
33         length = 0;
34         for ( i = 0 ; i < 3 ; i++ )
35                 length += v[i] * v[i];
36         length = sqrt( length );     // FIXME
37
38         return length;
39 }
40
41 qboolean VectorCompare( vec3_t v1, vec3_t v2 ){
42         int i;
43
44         for ( i = 0 ; i < 3 ; i++ )
45                 if ( fabs( v1[i] - v2[i] ) > EQUAL_EPSILON ) {
46                         return false;
47                 }
48
49         return true;
50 }
51
52 vec_t Q_rint( vec_t in ){
53         return floor( in + 0.5 );
54 }
55
56 void VectorMA( vec3_t va, double scale, vec3_t vb, vec3_t vc ){
57         vc[0] = va[0] + scale * vb[0];
58         vc[1] = va[1] + scale * vb[1];
59         vc[2] = va[2] + scale * vb[2];
60 }
61
62 void CrossProduct( vec3_t v1, vec3_t v2, vec3_t cross ){
63         cross[0] = v1[1] * v2[2] - v1[2] * v2[1];
64         cross[1] = v1[2] * v2[0] - v1[0] * v2[2];
65         cross[2] = v1[0] * v2[1] - v1[1] * v2[0];
66 }
67
68 vec_t _DotProduct( vec3_t v1, vec3_t v2 ){
69         return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
70 }
71
72 void _VectorSubtract( vec3_t va, vec3_t vb, vec3_t out ){
73         out[0] = va[0] - vb[0];
74         out[1] = va[1] - vb[1];
75         out[2] = va[2] - vb[2];
76 }
77
78 void _VectorAdd( vec3_t va, vec3_t vb, vec3_t out ){
79         out[0] = va[0] + vb[0];
80         out[1] = va[1] + vb[1];
81         out[2] = va[2] + vb[2];
82 }
83
84 void _VectorCopy( vec3_t in, vec3_t out ){
85         out[0] = in[0];
86         out[1] = in[1];
87         out[2] = in[2];
88 }
89
90 void _VectorScale( vec3_t v, vec_t scale, vec3_t out ){
91         out[0] = v[0] * scale;
92         out[1] = v[1] * scale;
93         out[2] = v[2] * scale;
94 }
95
96 vec_t VectorNormalize( vec3_t in, vec3_t out ){
97         vec_t length, ilength;
98
99         length = sqrt( in[0] * in[0] + in[1] * in[1] + in[2] * in[2] );
100         if ( length == 0 ) {
101                 VectorClear( out );
102                 return 0;
103         }
104
105         ilength = 1.0 / length;
106         out[0] = in[0] * ilength;
107         out[1] = in[1] * ilength;
108         out[2] = in[2] * ilength;
109
110         return length;
111 }
112
113 vec_t ColorNormalize( vec3_t in, vec3_t out ){
114         float max, scale;
115
116         max = in[0];
117         if ( in[1] > max ) {
118                 max = in[1];
119         }
120         if ( in[2] > max ) {
121                 max = in[2];
122         }
123
124         if ( max == 0 ) {
125                 return 0;
126         }
127
128         scale = 1.0 / max;
129
130         VectorScale( in, scale, out );
131
132         return max;
133 }
134
135
136
137 void VectorInverse( vec3_t v ){
138         v[0] = -v[0];
139         v[1] = -v[1];
140         v[2] = -v[2];
141 }
142
143 void ClearBounds( vec3_t mins, vec3_t maxs ){
144         mins[0] = mins[1] = mins[2] = 99999;
145         maxs[0] = maxs[1] = maxs[2] = -99999;
146 }
147
148 void AddPointToBounds( vec3_t v, vec3_t mins, vec3_t maxs ){
149         int i;
150         vec_t val;
151
152         for ( i = 0 ; i < 3 ; i++ )
153         {
154                 val = v[i];
155                 if ( val < mins[i] ) {
156                         mins[i] = val;
157                 }
158                 if ( val > maxs[i] ) {
159                         maxs[i] = val;
160                 }
161         }
162 }