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