Debian Codegraph @

tools/heretic2/common/mathlib.c

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