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