2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\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
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
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
22 // mathlib.c -- math primitives
\r
23 #include "mathlib.h"
\r
24 // we use memcpy and memset
\r
27 vec3_t vec3_origin = {0.0f,0.0f,0.0f};
\r
33 Given a normalized forward vector, create two
\r
34 other perpendicular vectors
\r
37 void MakeNormalVectors (vec3_t forward, vec3_t right, vec3_t up)
\r
41 // this rotate and negate guarantees a vector
\r
42 // not colinear with the original
\r
43 right[1] = -forward[0];
\r
44 right[2] = forward[1];
\r
45 right[0] = forward[2];
\r
47 d = DotProduct (right, forward);
\r
48 VectorMA (right, -d, forward, right);
\r
49 VectorNormalize (right, right);
\r
50 CrossProduct (right, forward, up);
\r
53 vec_t VectorLength(vec3_t v)
\r
59 for (i=0 ; i< 3 ; i++)
\r
60 length += v[i]*v[i];
\r
61 length = (float)sqrt (length);
\r
66 qboolean VectorCompare (vec3_t v1, vec3_t v2)
\r
70 for (i=0 ; i<3 ; i++)
\r
71 if (fabs(v1[i]-v2[i]) > EQUAL_EPSILON)
\r
78 // FIXME TTimo this implementation has to be particular to radiant
\r
79 // through another name I'd say
\r
80 vec_t Q_rint (vec_t in)
\r
82 if (g_PrefsDlg.m_bNoClamp)
\r
85 return (float)floor (in + 0.5);
\r
89 void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc )
\r
91 vc[0] = va[0] + scale*vb[0];
\r
92 vc[1] = va[1] + scale*vb[1];
\r
93 vc[2] = va[2] + scale*vb[2];
\r
96 void _CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
\r
98 cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
\r
99 cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
\r
100 cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
\r
103 vec_t _DotProduct (vec3_t v1, vec3_t v2)
\r
105 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
\r
108 void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
\r
110 out[0] = va[0]-vb[0];
\r
111 out[1] = va[1]-vb[1];
\r
112 out[2] = va[2]-vb[2];
\r
115 void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
\r
117 out[0] = va[0]+vb[0];
\r
118 out[1] = va[1]+vb[1];
\r
119 out[2] = va[2]+vb[2];
\r
122 void _VectorCopy (vec3_t in, vec3_t out)
\r
129 vec_t VectorNormalize( const vec3_t in, vec3_t out ) {
\r
130 vec_t length, ilength;
\r
132 length = (vec_t)sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]);
\r
139 ilength = 1.0f/length;
\r
140 out[0] = in[0]*ilength;
\r
141 out[1] = in[1]*ilength;
\r
142 out[2] = in[2]*ilength;
\r
147 vec_t ColorNormalize( const vec3_t in, vec3_t out ) {
\r
157 out[0] = out[1] = out[2] = 1.0;
\r
161 scale = 1.0f / max;
\r
163 VectorScale (in, scale, out);
\r
168 void VectorInverse (vec3_t v)
\r
176 void VectorScale (vec3_t v, vec_t scale, vec3_t out)
\r
178 out[0] = v[0] * scale;
\r
179 out[1] = v[1] * scale;
\r
180 out[2] = v[2] * scale;
\r
184 void VectorRotate (vec3_t vIn, vec3_t vRotation, vec3_t out)
\r
190 VectorCopy(vIn, va);
\r
191 VectorCopy(va, vWork);
\r
192 nIndex[0][0] = 1; nIndex[0][1] = 2;
\r
193 nIndex[1][0] = 2; nIndex[1][1] = 0;
\r
194 nIndex[2][0] = 0; nIndex[2][1] = 1;
\r
196 for (i = 0; i < 3; i++)
\r
198 if (vRotation[i] != 0)
\r
200 float dAngle = vRotation[i] * Q_PI / 180.0f;
\r
201 float c = (vec_t)cos(dAngle);
\r
202 float s = (vec_t)sin(dAngle);
\r
203 vWork[nIndex[i][0]] = va[nIndex[i][0]] * c - va[nIndex[i][1]] * s;
\r
204 vWork[nIndex[i][1]] = va[nIndex[i][0]] * s + va[nIndex[i][1]] * c;
\r
206 VectorCopy(vWork, va);
\r
208 VectorCopy(vWork, out);
\r
211 void VectorRotateOrigin (vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out)
\r
213 vec3_t vTemp, vTemp2;
\r
215 VectorSubtract(vIn, vOrigin, vTemp);
\r
216 VectorRotate(vTemp, vRotation, vTemp2);
\r
217 VectorAdd(vTemp2, vOrigin, out);
\r
220 void VectorPolar(vec3_t v, float radius, float theta, float phi)
\r
222 v[0]=(float)(radius * cos(theta) * cos(phi));
\r
223 v[1]=(float)(radius * sin(theta) * cos(phi));
\r
224 v[2]=(float)(radius * sin(phi));
\r
227 void VectorSnap(vec3_t v)
\r
230 for (i = 0; i < 3; i++)
\r
232 v[i] = (vec_t)floor (v[i] + 0.5);
\r
236 void VectorISnap(vec3_t point, int snap)
\r
239 for (i = 0 ;i < 3 ; i++)
\r
241 point[i] = (vec_t)floor (point[i] / snap + 0.5) * snap;
\r
245 void VectorFSnap(vec3_t point, float snap)
\r
248 for (i = 0 ;i < 3 ; i++)
\r
250 point[i] = (vec_t)floor (point[i] / snap + 0.5) * snap;
\r
254 void _Vector5Add (vec5_t va, vec5_t vb, vec5_t out)
\r
256 out[0] = va[0]+vb[0];
\r
257 out[1] = va[1]+vb[1];
\r
258 out[2] = va[2]+vb[2];
\r
259 out[3] = va[3]+vb[3];
\r
260 out[4] = va[4]+vb[4];
\r
263 void _Vector5Scale (vec5_t v, vec_t scale, vec5_t out)
\r
265 out[0] = v[0] * scale;
\r
266 out[1] = v[1] * scale;
\r
267 out[2] = v[2] * scale;
\r
268 out[3] = v[3] * scale;
\r
269 out[4] = v[4] * scale;
\r
272 void _Vector53Copy (vec5_t in, vec3_t out)
\r
279 // NOTE: added these from Ritual's Q3Radiant
\r
280 void ClearBounds (vec3_t mins, vec3_t maxs)
\r
282 mins[0] = mins[1] = mins[2] = 99999;
\r
283 maxs[0] = maxs[1] = maxs[2] = -99999;
\r
286 void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs)
\r
291 for (i=0 ; i<3 ; i++)
\r
301 #define PITCH 0 // up / down
\r
302 #define YAW 1 // left / right
\r
303 #define ROLL 2 // fall over
\r
305 #define M_PI 3.14159265358979323846f // matches value in gcc v2 math.h
\r
308 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
\r
311 static float sr, sp, sy, cr, cp, cy;
\r
312 // static to help MS compiler fp bugs
\r
314 angle = angles[YAW] * (M_PI*2.0f / 360.0f);
\r
315 sy = (vec_t)sin(angle);
\r
316 cy = (vec_t)cos(angle);
\r
317 angle = angles[PITCH] * (M_PI*2.0f / 360.0f);
\r
318 sp = (vec_t)sin(angle);
\r
319 cp = (vec_t)cos(angle);
\r
320 angle = angles[ROLL] * (M_PI*2.0f / 360.0f);
\r
321 sr = (vec_t)sin(angle);
\r
322 cr = (vec_t)cos(angle);
\r
326 forward[0] = cp*cy;
\r
327 forward[1] = cp*sy;
\r
332 right[0] = -sr*sp*cy+cr*sy;
\r
333 right[1] = -sr*sp*sy-cr*cy;
\r
338 up[0] = cr*sp*cy+sr*sy;
\r
339 up[1] = cr*sp*sy-sr*cy;
\r
344 void VectorToAngles( vec3_t vec, vec3_t angles )
\r
349 if ( ( vec[ 0 ] == 0 ) && ( vec[ 1 ] == 0 ) )
\r
352 if ( vec[ 2 ] > 0 )
\r
363 yaw = (vec_t)atan2( vec[ 1 ], vec[ 0 ] ) * 180 / M_PI;
\r
369 forward = ( float )sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] );
\r
370 pitch = (vec_t)atan2( vec[ 2 ], forward ) * 180 / M_PI;
\r
377 angles[ 0 ] = pitch;
\r
383 =====================
\r
386 Returns false if the triangle is degenrate.
\r
387 The normal will point out of the clock for clockwise ordered points
\r
388 =====================
\r
390 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
\r
393 VectorSubtract( b, a, d1 );
\r
394 VectorSubtract( c, a, d2 );
\r
395 CrossProduct( d2, d1, plane );
\r
396 if ( VectorNormalize( plane, plane ) == 0 ) {
\r
400 plane[3] = DotProduct( a, plane );
\r
407 ** We use two byte encoded normals in some space critical applications.
\r
408 ** Lat = 0 at (1,0,0) to 360 (-1,0,0), encoded in 8-bit sine table format
\r
409 ** Lng = 0 at (0,0,1) to 180 (0,0,-1), encoded in 8-bit sine table format
\r
412 void NormalToLatLong( const vec3_t normal, byte bytes[2] ) {
\r
413 // check for singularities
\r
414 if ( normal[0] == 0 && normal[1] == 0 ) {
\r
415 if ( normal[2] > 0 ) {
\r
417 bytes[1] = 0; // lat = 0, long = 0
\r
420 bytes[1] = 0; // lat = 0, long = 128
\r
425 a = (int)( RAD2DEG( atan2( normal[1], normal[0] ) ) * (255.0f / 360.0f ) );
\r
428 b = (int)( RAD2DEG( acos( normal[2] ) ) * ( 255.0f / 360.0f ) );
\r
431 bytes[0] = b; // longitude
\r
432 bytes[1] = a; // lattitude
\r
441 int PlaneTypeForNormal (vec3_t normal) {
\r
442 if (normal[0] == 1.0 || normal[0] == -1.0)
\r
444 if (normal[1] == 1.0 || normal[1] == -1.0)
\r
446 if (normal[2] == 1.0 || normal[2] == -1.0)
\r
449 return PLANE_NON_AXIAL;
\r
457 void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
\r
458 out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
\r
459 in1[0][2] * in2[2][0];
\r
460 out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
\r
461 in1[0][2] * in2[2][1];
\r
462 out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
\r
463 in1[0][2] * in2[2][2];
\r
464 out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
\r
465 in1[1][2] * in2[2][0];
\r
466 out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
\r
467 in1[1][2] * in2[2][1];
\r
468 out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
\r
469 in1[1][2] * in2[2][2];
\r
470 out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
\r
471 in1[2][2] * in2[2][0];
\r
472 out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
\r
473 in1[2][2] * in2[2][1];
\r
474 out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
\r
475 in1[2][2] * in2[2][2];
\r
478 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
\r
484 inv_denom = 1.0F / DotProduct( normal, normal );
\r
486 d = DotProduct( normal, p ) * inv_denom;
\r
488 n[0] = normal[0] * inv_denom;
\r
489 n[1] = normal[1] * inv_denom;
\r
490 n[2] = normal[2] * inv_denom;
\r
492 dst[0] = p[0] - d * n[0];
\r
493 dst[1] = p[1] - d * n[1];
\r
494 dst[2] = p[2] - d * n[2];
\r
498 ** assumes "src" is normalized
\r
500 void PerpendicularVector( vec3_t dst, const vec3_t src )
\r
504 vec_t minelem = 1.0F;
\r
508 ** find the smallest magnitude axially aligned vector
\r
510 for ( pos = 0, i = 0; i < 3; i++ )
\r
512 if ( fabs( src[i] ) < minelem )
\r
515 minelem = (vec_t)fabs( src[i] );
\r
518 tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
\r
519 tempvec[pos] = 1.0F;
\r
522 ** project the point onto the plane defined by src
\r
524 ProjectPointOnPlane( dst, tempvec, src );
\r
527 ** normalize the result
\r
529 VectorNormalize( dst, dst );
\r
534 RotatePointAroundVector
\r
536 This is not implemented very well...
\r
539 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
\r
544 float tmpmat[3][3];
\r
547 vec3_t vr, vup, vf;
\r
554 PerpendicularVector( vr, dir );
\r
555 CrossProduct( vr, vf, vup );
\r
569 memcpy( im, m, sizeof( im ) );
\r
571 im[0][1] = m[1][0];
\r
572 im[0][2] = m[2][0];
\r
573 im[1][0] = m[0][1];
\r
574 im[1][2] = m[2][1];
\r
575 im[2][0] = m[0][2];
\r
576 im[2][1] = m[1][2];
\r
578 memset( zrot, 0, sizeof( zrot ) );
\r
579 zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
\r
581 rad = DEG2RAD( degrees );
\r
582 zrot[0][0] = (vec_t)cos( rad );
\r
583 zrot[0][1] = (vec_t)sin( rad );
\r
584 zrot[1][0] = (vec_t)-sin( rad );
\r
585 zrot[1][1] = (vec_t)cos( rad );
\r
587 MatrixMultiply( m, zrot, tmpmat );
\r
588 MatrixMultiply( tmpmat, im, rot );
\r
590 for ( i = 0; i < 3; i++ ) {
\r
591 dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
\r