1 /*
2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
4 
5 This file is part of Quake 2 Tools source code.
6 
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11 
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, 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 Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 ===========================================================================
21 */
22 
23 // mathlib.c -- math primitives
24 
25 #include "cmdlib.h"
26 #include "mathlib.h"
27 
28 vec3_t vec3_origin = {0,0,0};
29 
30 
VectorLength(vec3_t v)31 double VectorLength(vec3_t v)
32 {
33 	int		i;
34 	double	length;
35 
36 	length = 0;
37 	for (i=0 ; i< 3 ; i++)
38 		length += v[i]*v[i];
39 	length = sqrt (length);		// FIXME
40 
41 	return length;
42 }
43 
VectorCompare(vec3_t v1,vec3_t v2)44 qboolean VectorCompare (vec3_t v1, vec3_t v2)
45 {
46 	int		i;
47 
48 	for (i=0 ; i<3 ; i++)
49 		if (fabs(v1[i]-v2[i]) > EQUAL_EPSILON)
50 			return false;
51 
52 	return true;
53 }
54 
Q_rint(vec_t in)55 vec_t Q_rint (vec_t in)
56 {
57 	return floor (in + 0.5);
58 }
59 
VectorMA(vec3_t va,double scale,vec3_t vb,vec3_t vc)60 void VectorMA (vec3_t va, double scale, vec3_t vb, vec3_t vc)
61 {
62 	vc[0] = va[0] + scale*vb[0];
63 	vc[1] = va[1] + scale*vb[1];
64 	vc[2] = va[2] + scale*vb[2];
65 }
66 
CrossProduct(vec3_t v1,vec3_t v2,vec3_t cross)67 void CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
68 {
69 	cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
70 	cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
71 	cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
72 }
73 
_DotProduct(vec3_t v1,vec3_t v2)74 vec_t _DotProduct (vec3_t v1, vec3_t v2)
75 {
76 	return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
77 }
78 
_VectorSubtract(vec3_t va,vec3_t vb,vec3_t out)79 void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
80 {
81 	out[0] = va[0]-vb[0];
82 	out[1] = va[1]-vb[1];
83 	out[2] = va[2]-vb[2];
84 }
85 
_VectorAdd(vec3_t va,vec3_t vb,vec3_t out)86 void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
87 {
88 	out[0] = va[0]+vb[0];
89 	out[1] = va[1]+vb[1];
90 	out[2] = va[2]+vb[2];
91 }
92 
_VectorCopy(vec3_t in,vec3_t out)93 void _VectorCopy (vec3_t in, vec3_t out)
94 {
95 	out[0] = in[0];
96 	out[1] = in[1];
97 	out[2] = in[2];
98 }
99 
_VectorScale(vec3_t v,vec_t scale,vec3_t out)100 void _VectorScale (vec3_t v, vec_t scale, vec3_t out)
101 {
102 	out[0] = v[0] * scale;
103 	out[1] = v[1] * scale;
104 	out[2] = v[2] * scale;
105 }
106 
VectorNormalize(vec3_t in,vec3_t out)107 vec_t VectorNormalize (vec3_t in, vec3_t out)
108 {
109 	vec_t	length, ilength;
110 
111 	length = sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]);
112 	if (length == 0)
113 	{
114 		VectorClear (out);
115 		return 0;
116 	}
117 
118 	ilength = 1.0/length;
119 	out[0] = in[0]*ilength;
120 	out[1] = in[1]*ilength;
121 	out[2] = in[2]*ilength;
122 
123 	return length;
124 }
125 
ColorNormalize(vec3_t in,vec3_t out)126 vec_t ColorNormalize (vec3_t in, vec3_t out)
127 {
128 	float	max, scale;
129 
130 	max = in[0];
131 	if (in[1] > max)
132 		max = in[1];
133 	if (in[2] > max)
134 		max = in[2];
135 
136 	if (max == 0)
137 		return 0;
138 
139 	scale = 1.0 / max;
140 
141 	VectorScale (in, scale, out);
142 
143 	return max;
144 }
145 
146 
147 
VectorInverse(vec3_t v)148 void VectorInverse (vec3_t v)
149 {
150 	v[0] = -v[0];
151 	v[1] = -v[1];
152 	v[2] = -v[2];
153 }
154 
ClearBounds(vec3_t mins,vec3_t maxs)155 void ClearBounds (vec3_t mins, vec3_t maxs)
156 {
157 	mins[0] = mins[1] = mins[2] = 99999;
158 	maxs[0] = maxs[1] = maxs[2] = -99999;
159 }
160 
AddPointToBounds(vec3_t v,vec3_t mins,vec3_t maxs)161 void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs)
162 {
163 	int		i;
164 	vec_t	val;
165 
166 	for (i=0 ; i<3 ; i++)
167 	{
168 		val = v[i];
169 		if (val < mins[i])
170 			mins[i] = val;
171 		if (val > maxs[i])
172 			maxs[i] = val;
173 	}
174 }
175