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 #include "mathlib.h"
24 // we use memcpy and memset
25 #include <memory.h>
26
27 const vec3_t vec3_origin = {0.0f,0.0f,0.0f};
28
29 const vec3_t g_vec3_axis_x = { 1, 0, 0, };
30 const vec3_t g_vec3_axis_y = { 0, 1, 0, };
31 const vec3_t g_vec3_axis_z = { 0, 0, 1, };
32
33 /*
34 ================
35 MakeNormalVectors
36
37 Given a normalized forward vector, create two
38 other perpendicular vectors
39 ================
40 */
MakeNormalVectors(vec3_t forward,vec3_t right,vec3_t up)41 void MakeNormalVectors (vec3_t forward, vec3_t right, vec3_t up)
42 {
43 float d;
44
45 // this rotate and negate guarantees a vector
46 // not colinear with the original
47 right[1] = -forward[0];
48 right[2] = forward[1];
49 right[0] = forward[2];
50
51 d = DotProduct (right, forward);
52 VectorMA (right, -d, forward, right);
53 VectorNormalize (right, right);
54 CrossProduct (right, forward, up);
55 }
56
VectorLength(const vec3_t v)57 vec_t VectorLength(const vec3_t v)
58 {
59 int i;
60 float length;
61
62 length = 0.0f;
63 for (i=0 ; i< 3 ; i++)
64 length += v[i]*v[i];
65 length = (float)sqrt (length);
66
67 return length;
68 }
69
VectorCompare(const vec3_t v1,const vec3_t v2)70 qboolean VectorCompare (const vec3_t v1, const vec3_t v2)
71 {
72 int i;
73
74 for (i=0 ; i<3 ; i++)
75 if (fabs(v1[i]-v2[i]) > EQUAL_EPSILON)
76 return qfalse;
77
78 return qtrue;
79 }
80
VectorMA(const vec3_t va,vec_t scale,const vec3_t vb,vec3_t vc)81 void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc )
82 {
83 vc[0] = va[0] + scale*vb[0];
84 vc[1] = va[1] + scale*vb[1];
85 vc[2] = va[2] + scale*vb[2];
86 }
87
_CrossProduct(vec3_t v1,vec3_t v2,vec3_t cross)88 void _CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
89 {
90 cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
91 cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
92 cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
93 }
94
_DotProduct(vec3_t v1,vec3_t v2)95 vec_t _DotProduct (vec3_t v1, vec3_t v2)
96 {
97 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
98 }
99
_VectorSubtract(vec3_t va,vec3_t vb,vec3_t out)100 void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
101 {
102 out[0] = va[0]-vb[0];
103 out[1] = va[1]-vb[1];
104 out[2] = va[2]-vb[2];
105 }
106
_VectorAdd(vec3_t va,vec3_t vb,vec3_t out)107 void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
108 {
109 out[0] = va[0]+vb[0];
110 out[1] = va[1]+vb[1];
111 out[2] = va[2]+vb[2];
112 }
113
_VectorCopy(vec3_t in,vec3_t out)114 void _VectorCopy (vec3_t in, vec3_t out)
115 {
116 out[0] = in[0];
117 out[1] = in[1];
118 out[2] = in[2];
119 }
120
VectorNormalize(const vec3_t in,vec3_t out)121 vec_t VectorNormalize( const vec3_t in, vec3_t out ) {
122 vec_t length, ilength;
123
124 length = (vec_t)sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]);
125 if (length == 0)
126 {
127 VectorClear (out);
128 return 0;
129 }
130
131 ilength = 1.0f/length;
132 out[0] = in[0]*ilength;
133 out[1] = in[1]*ilength;
134 out[2] = in[2]*ilength;
135
136 return length;
137 }
138
ColorNormalize(const vec3_t in,vec3_t out)139 vec_t ColorNormalize( const vec3_t in, vec3_t out ) {
140 float max, scale;
141
142 max = in[0];
143 if (in[1] > max)
144 max = in[1];
145 if (in[2] > max)
146 max = in[2];
147
148 if (max == 0) {
149 out[0] = out[1] = out[2] = 1.0;
150 return 0;
151 }
152
153 scale = 1.0f / max;
154
155 VectorScale (in, scale, out);
156
157 return max;
158 }
159
VectorInverse(vec3_t v)160 void VectorInverse (vec3_t v)
161 {
162 v[0] = -v[0];
163 v[1] = -v[1];
164 v[2] = -v[2];
165 }
166
167 /*
168 void VectorScale (vec3_t v, vec_t scale, vec3_t out)
169 {
170 out[0] = v[0] * scale;
171 out[1] = v[1] * scale;
172 out[2] = v[2] * scale;
173 }
174 */
175
VectorRotate(vec3_t vIn,vec3_t vRotation,vec3_t out)176 void VectorRotate (vec3_t vIn, vec3_t vRotation, vec3_t out)
177 {
178 vec3_t vWork, va;
179 int nIndex[3][2];
180 int i;
181
182 VectorCopy(vIn, va);
183 VectorCopy(va, vWork);
184 nIndex[0][0] = 1; nIndex[0][1] = 2;
185 nIndex[1][0] = 2; nIndex[1][1] = 0;
186 nIndex[2][0] = 0; nIndex[2][1] = 1;
187
188 for (i = 0; i < 3; i++)
189 {
190 if (vRotation[i] != 0)
191 {
192 float dAngle = vRotation[i] * Q_PI / 180.0f;
193 float c = (vec_t)cos(dAngle);
194 float s = (vec_t)sin(dAngle);
195 vWork[nIndex[i][0]] = va[nIndex[i][0]] * c - va[nIndex[i][1]] * s;
196 vWork[nIndex[i][1]] = va[nIndex[i][0]] * s + va[nIndex[i][1]] * c;
197 }
198 VectorCopy(vWork, va);
199 }
200 VectorCopy(vWork, out);
201 }
202
VectorRotateOrigin(vec3_t vIn,vec3_t vRotation,vec3_t vOrigin,vec3_t out)203 void VectorRotateOrigin (vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out)
204 {
205 vec3_t vTemp, vTemp2;
206
207 VectorSubtract(vIn, vOrigin, vTemp);
208 VectorRotate(vTemp, vRotation, vTemp2);
209 VectorAdd(vTemp2, vOrigin, out);
210 }
211
VectorPolar(vec3_t v,float radius,float theta,float phi)212 void VectorPolar(vec3_t v, float radius, float theta, float phi)
213 {
214 v[0]=(float)(radius * cos(theta) * cos(phi));
215 v[1]=(float)(radius * sin(theta) * cos(phi));
216 v[2]=(float)(radius * sin(phi));
217 }
218
VectorSnap(vec3_t v)219 void VectorSnap(vec3_t v)
220 {
221 int i;
222 for (i = 0; i < 3; i++)
223 {
224 v[i] = (vec_t)FLOAT_TO_INTEGER(v[i]);
225 }
226 }
227
VectorISnap(vec3_t point,int snap)228 void VectorISnap(vec3_t point, int snap)
229 {
230 int i;
231 for (i = 0 ;i < 3 ; i++)
232 {
233 point[i] = (vec_t)FLOAT_SNAP(point[i], snap);
234 }
235 }
236
VectorFSnap(vec3_t point,float snap)237 void VectorFSnap(vec3_t point, float snap)
238 {
239 int i;
240 for (i = 0 ;i < 3 ; i++)
241 {
242 point[i] = (vec_t)FLOAT_SNAP(point[i], snap);
243 }
244 }
245
_Vector5Add(vec5_t va,vec5_t vb,vec5_t out)246 void _Vector5Add (vec5_t va, vec5_t vb, vec5_t out)
247 {
248 out[0] = va[0]+vb[0];
249 out[1] = va[1]+vb[1];
250 out[2] = va[2]+vb[2];
251 out[3] = va[3]+vb[3];
252 out[4] = va[4]+vb[4];
253 }
254
_Vector5Scale(vec5_t v,vec_t scale,vec5_t out)255 void _Vector5Scale (vec5_t v, vec_t scale, vec5_t out)
256 {
257 out[0] = v[0] * scale;
258 out[1] = v[1] * scale;
259 out[2] = v[2] * scale;
260 out[3] = v[3] * scale;
261 out[4] = v[4] * scale;
262 }
263
_Vector53Copy(vec5_t in,vec3_t out)264 void _Vector53Copy (vec5_t in, vec3_t out)
265 {
266 out[0] = in[0];
267 out[1] = in[1];
268 out[2] = in[2];
269 }
270
271 // NOTE: added these from Ritual's Q3Radiant
ClearBounds(vec3_t mins,vec3_t maxs)272 void ClearBounds (vec3_t mins, vec3_t maxs)
273 {
274 mins[0] = mins[1] = mins[2] = 99999;
275 maxs[0] = maxs[1] = maxs[2] = -99999;
276 }
277
AddPointToBounds(vec3_t v,vec3_t mins,vec3_t maxs)278 void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs)
279 {
280 int i;
281 vec_t val;
282
283 for (i=0 ; i<3 ; i++)
284 {
285 val = v[i];
286 if (val < mins[i])
287 mins[i] = val;
288 if (val > maxs[i])
289 maxs[i] = val;
290 }
291 }
292
AngleVectors(vec3_t angles,vec3_t forward,vec3_t right,vec3_t up)293 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
294 {
295 float angle;
296 static float sr, sp, sy, cr, cp, cy;
297 // static to help MS compiler fp bugs
298
299 angle = angles[YAW] * (Q_PI*2.0f / 360.0f);
300 sy = (vec_t)sin(angle);
301 cy = (vec_t)cos(angle);
302 angle = angles[PITCH] * (Q_PI*2.0f / 360.0f);
303 sp = (vec_t)sin(angle);
304 cp = (vec_t)cos(angle);
305 angle = angles[ROLL] * (Q_PI*2.0f / 360.0f);
306 sr = (vec_t)sin(angle);
307 cr = (vec_t)cos(angle);
308
309 if (forward)
310 {
311 forward[0] = cp*cy;
312 forward[1] = cp*sy;
313 forward[2] = -sp;
314 }
315 if (right)
316 {
317 right[0] = -sr*sp*cy+cr*sy;
318 right[1] = -sr*sp*sy-cr*cy;
319 right[2] = -sr*cp;
320 }
321 if (up)
322 {
323 up[0] = cr*sp*cy+sr*sy;
324 up[1] = cr*sp*sy-sr*cy;
325 up[2] = cr*cp;
326 }
327 }
328
VectorToAngles(vec3_t vec,vec3_t angles)329 void VectorToAngles( vec3_t vec, vec3_t angles )
330 {
331 float forward;
332 float yaw, pitch;
333
334 if ( ( vec[ 0 ] == 0 ) && ( vec[ 1 ] == 0 ) )
335 {
336 yaw = 0;
337 if ( vec[ 2 ] > 0 )
338 {
339 pitch = 90;
340 }
341 else
342 {
343 pitch = 270;
344 }
345 }
346 else
347 {
348 yaw = (vec_t)atan2( vec[ 1 ], vec[ 0 ] ) * 180 / Q_PI;
349 if ( yaw < 0 )
350 {
351 yaw += 360;
352 }
353
354 forward = ( float )sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] );
355 pitch = (vec_t)atan2( vec[ 2 ], forward ) * 180 / Q_PI;
356 if ( pitch < 0 )
357 {
358 pitch += 360;
359 }
360 }
361
362 angles[ 0 ] = pitch;
363 angles[ 1 ] = yaw;
364 angles[ 2 ] = 0;
365 }
366
367 /*
368 =====================
369 PlaneFromPoints
370
371 Returns false if the triangle is degenrate.
372 The normal will point out of the clock for clockwise ordered points
373 =====================
374 */
PlaneFromPoints(vec4_t plane,const vec3_t a,const vec3_t b,const vec3_t c)375 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
376 vec3_t d1, d2;
377
378 VectorSubtract( b, a, d1 );
379 VectorSubtract( c, a, d2 );
380 CrossProduct( d2, d1, plane );
381 if ( VectorNormalize( plane, plane ) == 0 ) {
382 return qfalse;
383 }
384
385 plane[3] = DotProduct( a, plane );
386 return qtrue;
387 }
388
389 /*
390 ** NormalToLatLong
391 **
392 ** We use two byte encoded normals in some space critical applications.
393 ** Lat = 0 at (1,0,0) to 360 (-1,0,0), encoded in 8-bit sine table format
394 ** Lng = 0 at (0,0,1) to 180 (0,0,-1), encoded in 8-bit sine table format
395 **
396 */
NormalToLatLong(const vec3_t normal,byte bytes[2])397 void NormalToLatLong( const vec3_t normal, byte bytes[2] ) {
398 // check for singularities
399 if ( normal[0] == 0 && normal[1] == 0 ) {
400 if ( normal[2] > 0 ) {
401 bytes[0] = 0;
402 bytes[1] = 0; // lat = 0, long = 0
403 } else {
404 bytes[0] = 128;
405 bytes[1] = 0; // lat = 0, long = 128
406 }
407 } else {
408 int a, b;
409
410 a = (int)( RAD2DEG( atan2( normal[1], normal[0] ) ) * (255.0f / 360.0f ) );
411 a &= 0xff;
412
413 b = (int)( RAD2DEG( acos( normal[2] ) ) * ( 255.0f / 360.0f ) );
414 b &= 0xff;
415
416 bytes[0] = b; // longitude
417 bytes[1] = a; // lattitude
418 }
419 }
420
421 /*
422 =================
423 PlaneTypeForNormal
424 =================
425 */
PlaneTypeForNormal(vec3_t normal)426 int PlaneTypeForNormal (vec3_t normal) {
427 if (normal[0] == 1.0 || normal[0] == -1.0)
428 return PLANE_X;
429 if (normal[1] == 1.0 || normal[1] == -1.0)
430 return PLANE_Y;
431 if (normal[2] == 1.0 || normal[2] == -1.0)
432 return PLANE_Z;
433
434 return PLANE_NON_AXIAL;
435 }
436
437 /*
438 ================
439 MatrixMultiply
440 ================
441 */
MatrixMultiply(float in1[3][3],float in2[3][3],float out[3][3])442 void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
443 out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
444 in1[0][2] * in2[2][0];
445 out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
446 in1[0][2] * in2[2][1];
447 out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
448 in1[0][2] * in2[2][2];
449 out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
450 in1[1][2] * in2[2][0];
451 out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
452 in1[1][2] * in2[2][1];
453 out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
454 in1[1][2] * in2[2][2];
455 out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
456 in1[2][2] * in2[2][0];
457 out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
458 in1[2][2] * in2[2][1];
459 out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
460 in1[2][2] * in2[2][2];
461 }
462
ProjectPointOnPlane(vec3_t dst,const vec3_t p,const vec3_t normal)463 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
464 {
465 float d;
466 vec3_t n;
467 float inv_denom;
468
469 inv_denom = 1.0F / DotProduct( normal, normal );
470
471 d = DotProduct( normal, p ) * inv_denom;
472
473 n[0] = normal[0] * inv_denom;
474 n[1] = normal[1] * inv_denom;
475 n[2] = normal[2] * inv_denom;
476
477 dst[0] = p[0] - d * n[0];
478 dst[1] = p[1] - d * n[1];
479 dst[2] = p[2] - d * n[2];
480 }
481
482 /*
483 ** assumes "src" is normalized
484 */
PerpendicularVector(vec3_t dst,const vec3_t src)485 void PerpendicularVector( vec3_t dst, const vec3_t src )
486 {
487 int pos;
488 int i;
489 vec_t minelem = 1.0F;
490 vec3_t tempvec;
491
492 /*
493 ** find the smallest magnitude axially aligned vector
494 */
495 for ( pos = 0, i = 0; i < 3; i++ )
496 {
497 if ( fabs( src[i] ) < minelem )
498 {
499 pos = i;
500 minelem = (vec_t)fabs( src[i] );
501 }
502 }
503 tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
504 tempvec[pos] = 1.0F;
505
506 /*
507 ** project the point onto the plane defined by src
508 */
509 ProjectPointOnPlane( dst, tempvec, src );
510
511 /*
512 ** normalize the result
513 */
514 VectorNormalize( dst, dst );
515 }
516
517 /*
518 ===============
519 RotatePointAroundVector
520
521 This is not implemented very well...
522 ===============
523 */
RotatePointAroundVector(vec3_t dst,const vec3_t dir,const vec3_t point,float degrees)524 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
525 float degrees ) {
526 float m[3][3];
527 float im[3][3];
528 float zrot[3][3];
529 float tmpmat[3][3];
530 float rot[3][3];
531 int i;
532 vec3_t vr, vup, vf;
533 float rad;
534
535 vf[0] = dir[0];
536 vf[1] = dir[1];
537 vf[2] = dir[2];
538
539 PerpendicularVector( vr, dir );
540 CrossProduct( vr, vf, vup );
541
542 m[0][0] = vr[0];
543 m[1][0] = vr[1];
544 m[2][0] = vr[2];
545
546 m[0][1] = vup[0];
547 m[1][1] = vup[1];
548 m[2][1] = vup[2];
549
550 m[0][2] = vf[0];
551 m[1][2] = vf[1];
552 m[2][2] = vf[2];
553
554 memcpy( im, m, sizeof( im ) );
555
556 im[0][1] = m[1][0];
557 im[0][2] = m[2][0];
558 im[1][0] = m[0][1];
559 im[1][2] = m[2][1];
560 im[2][0] = m[0][2];
561 im[2][1] = m[1][2];
562
563 memset( zrot, 0, sizeof( zrot ) );
564 zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
565
566 rad = (float)DEG2RAD( degrees );
567 zrot[0][0] = (vec_t)cos( rad );
568 zrot[0][1] = (vec_t)sin( rad );
569 zrot[1][0] = (vec_t)-sin( rad );
570 zrot[1][1] = (vec_t)cos( rad );
571
572 MatrixMultiply( m, zrot, tmpmat );
573 MatrixMultiply( tmpmat, im, rot );
574
575 for ( i = 0; i < 3; i++ ) {
576 dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
577 }
578 }
579