2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
22 // mathlib.c -- math primitives
24 // we use memcpy and memset
27 vec3_t vec3_origin = {0.0f,0.0f,0.0f};
34 qboolean VectorIsOnAxis( vec3_t v ){
35 int i, zeroComponentCount;
37 zeroComponentCount = 0;
38 for ( i = 0; i < 3; i++ )
45 if ( zeroComponentCount > 1 ) {
46 // The zero vector will be on axis.
58 qboolean VectorIsOnAxialPlane( vec3_t v ){
61 for ( i = 0; i < 3; i++ )
64 // The zero vector will be on axial plane.
76 Given a normalized forward vector, create two
77 other perpendicular vectors
80 void MakeNormalVectors( vec3_t forward, vec3_t right, vec3_t up ){
83 // this rotate and negate guarantees a vector
84 // not colinear with the original
85 right[1] = -forward[0];
86 right[2] = forward[1];
87 right[0] = forward[2];
89 d = DotProduct( right, forward );
90 VectorMA( right, -d, forward, right );
91 VectorNormalize( right, right );
92 CrossProduct( right, forward, up );
95 vec_t VectorLength( vec3_t v ){
100 for ( i = 0 ; i < 3 ; i++ )
101 length += v[i] * v[i];
102 length = (float)sqrt( length );
107 qboolean VectorCompare( vec3_t v1, vec3_t v2 ){
110 for ( i = 0 ; i < 3 ; i++ )
111 if ( fabs( v1[i] - v2[i] ) > EQUAL_EPSILON ) {
119 // FIXME TTimo this implementation has to be particular to radiant
120 // through another name I'd say
121 vec_t Q_rint (vec_t in)
123 if (g_PrefsDlg.m_bNoClamp)
126 return (float)floor (in + 0.5);
130 void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc ){
131 vc[0] = va[0] + scale * vb[0];
132 vc[1] = va[1] + scale * vb[1];
133 vc[2] = va[2] + scale * vb[2];
136 void _CrossProduct( vec3_t v1, vec3_t v2, vec3_t cross ){
137 cross[0] = v1[1] * v2[2] - v1[2] * v2[1];
138 cross[1] = v1[2] * v2[0] - v1[0] * v2[2];
139 cross[2] = v1[0] * v2[1] - v1[1] * v2[0];
142 vec_t _DotProduct( vec3_t v1, vec3_t v2 ){
143 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
146 void _VectorSubtract( vec3_t va, vec3_t vb, vec3_t out ){
147 out[0] = va[0] - vb[0];
148 out[1] = va[1] - vb[1];
149 out[2] = va[2] - vb[2];
152 void _VectorAdd( vec3_t va, vec3_t vb, vec3_t out ){
153 out[0] = va[0] + vb[0];
154 out[1] = va[1] + vb[1];
155 out[2] = va[2] + vb[2];
158 void _VectorCopy( vec3_t in, vec3_t out ){
164 vec_t VectorNormalize( const vec3_t in, vec3_t out ) {
166 #if MATHLIB_VECTOR_NORMALIZE_PRECISION_FIX
168 // The sqrt() function takes double as an input and returns double as an
169 // output according the the man pages on Debian and on FreeBSD. Therefore,
170 // I don't see a reason why using a double outright (instead of using the
171 // vec_accu_t alias for example) could possibly be frowned upon.
173 double x, y, z, length;
179 length = sqrt( ( x * x ) + ( y * y ) + ( z * z ) );
185 out[0] = (vec_t) ( x / length );
186 out[1] = (vec_t) ( y / length );
187 out[2] = (vec_t) ( z / length );
189 return (vec_t) length;
193 vec_t length, ilength;
195 length = (vec_t)sqrt( in[0] * in[0] + in[1] * in[1] + in[2] * in[2] );
201 ilength = 1.0f / length;
202 out[0] = in[0] * ilength;
203 out[1] = in[1] * ilength;
204 out[2] = in[2] * ilength;
212 vec_t ColorNormalize( const vec3_t in, vec3_t out ) {
224 out[0] = out[1] = out[2] = 1.0;
230 VectorScale( in, scale, out );
235 void VectorInverse( vec3_t v ){
242 void VectorScale (vec3_t v, vec_t scale, vec3_t out)
244 out[0] = v[0] * scale;
245 out[1] = v[1] * scale;
246 out[2] = v[2] * scale;
250 void VectorRotate( vec3_t vIn, vec3_t vRotation, vec3_t out ){
255 VectorCopy( vIn, va );
256 VectorCopy( va, vWork );
257 nIndex[0][0] = 1; nIndex[0][1] = 2;
258 nIndex[1][0] = 2; nIndex[1][1] = 0;
259 nIndex[2][0] = 0; nIndex[2][1] = 1;
261 for ( i = 0; i < 3; i++ )
263 if ( vRotation[i] != 0 ) {
264 float dAngle = vRotation[i] * Q_PI / 180.0f;
265 float c = (vec_t)cos( dAngle );
266 float s = (vec_t)sin( dAngle );
267 vWork[nIndex[i][0]] = va[nIndex[i][0]] * c - va[nIndex[i][1]] * s;
268 vWork[nIndex[i][1]] = va[nIndex[i][0]] * s + va[nIndex[i][1]] * c;
270 VectorCopy( vWork, va );
272 VectorCopy( vWork, out );
275 void VectorRotateOrigin( vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out ){
276 vec3_t vTemp, vTemp2;
278 VectorSubtract( vIn, vOrigin, vTemp );
279 VectorRotate( vTemp, vRotation, vTemp2 );
280 VectorAdd( vTemp2, vOrigin, out );
283 void VectorPolar( vec3_t v, float radius, float theta, float phi ){
284 v[0] = (float)( radius * cos( theta ) * cos( phi ) );
285 v[1] = (float)( radius * sin( theta ) * cos( phi ) );
286 v[2] = (float)( radius * sin( phi ) );
289 void VectorSnap( vec3_t v ){
291 for ( i = 0; i < 3; i++ )
293 v[i] = (vec_t)floor( v[i] + 0.5 );
297 void VectorISnap( vec3_t point, int snap ){
299 for ( i = 0 ; i < 3 ; i++ )
301 point[i] = (vec_t)floor( point[i] / snap + 0.5 ) * snap;
305 void VectorFSnap( vec3_t point, float snap ){
307 for ( i = 0 ; i < 3 ; i++ )
309 point[i] = (vec_t)floor( point[i] / snap + 0.5 ) * snap;
313 void _Vector5Add( vec5_t va, vec5_t vb, vec5_t out ){
314 out[0] = va[0] + vb[0];
315 out[1] = va[1] + vb[1];
316 out[2] = va[2] + vb[2];
317 out[3] = va[3] + vb[3];
318 out[4] = va[4] + vb[4];
321 void _Vector5Scale( vec5_t v, vec_t scale, vec5_t out ){
322 out[0] = v[0] * scale;
323 out[1] = v[1] * scale;
324 out[2] = v[2] * scale;
325 out[3] = v[3] * scale;
326 out[4] = v[4] * scale;
329 void _Vector53Copy( vec5_t in, vec3_t out ){
335 // NOTE: added these from Ritual's Q3Radiant
336 void ClearBounds( vec3_t mins, vec3_t maxs ){
337 mins[0] = mins[1] = mins[2] = 99999;
338 maxs[0] = maxs[1] = maxs[2] = -99999;
341 void AddPointToBounds( vec3_t v, vec3_t mins, vec3_t maxs ){
345 for ( i = 0 ; i < 3 ; i++ )
348 if ( val < mins[i] ) {
351 if ( val > maxs[i] ) {
357 #define PITCH 0 // up / down
358 #define YAW 1 // left / right
359 #define ROLL 2 // fall over
361 #define M_PI 3.14159265358979323846f // matches value in gcc v2 math.h
364 void AngleVectors( vec3_t angles, vec3_t forward, vec3_t right, vec3_t up ){
366 static float sr, sp, sy, cr, cp, cy;
367 // static to help MS compiler fp bugs
369 angle = angles[YAW] * ( M_PI * 2.0f / 360.0f );
370 sy = (vec_t)sin( angle );
371 cy = (vec_t)cos( angle );
372 angle = angles[PITCH] * ( M_PI * 2.0f / 360.0f );
373 sp = (vec_t)sin( angle );
374 cp = (vec_t)cos( angle );
375 angle = angles[ROLL] * ( M_PI * 2.0f / 360.0f );
376 sr = (vec_t)sin( angle );
377 cr = (vec_t)cos( angle );
380 forward[0] = cp * cy;
381 forward[1] = cp * sy;
385 right[0] = -sr * sp * cy + cr * sy;
386 right[1] = -sr * sp * sy - cr * cy;
390 up[0] = cr * sp * cy + sr * sy;
391 up[1] = cr * sp * sy - sr * cy;
396 void VectorToAngles( vec3_t vec, vec3_t angles ){
400 if ( ( vec[ 0 ] == 0 ) && ( vec[ 1 ] == 0 ) ) {
402 if ( vec[ 2 ] > 0 ) {
412 yaw = (vec_t)atan2( vec[ 1 ], vec[ 0 ] ) * 180 / M_PI;
417 forward = ( float )sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] );
418 pitch = (vec_t)atan2( vec[ 2 ], forward ) * 180 / M_PI;
430 =====================
433 Returns false if the triangle is degenrate.
434 The normal will point out of the clock for clockwise ordered points
435 =====================
437 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
440 VectorSubtract( b, a, d1 );
441 VectorSubtract( c, a, d2 );
442 CrossProduct( d2, d1, plane );
443 if ( VectorNormalize( plane, plane ) == 0 ) {
447 plane[3] = DotProduct( a, plane );
454 ** We use two byte encoded normals in some space critical applications.
455 ** Lat = 0 at (1,0,0) to 360 (-1,0,0), encoded in 8-bit sine table format
456 ** Lng = 0 at (0,0,1) to 180 (0,0,-1), encoded in 8-bit sine table format
459 void NormalToLatLong( const vec3_t normal, byte bytes[2] ) {
460 // check for singularities
461 if ( normal[0] == 0 && normal[1] == 0 ) {
462 if ( normal[2] > 0 ) {
464 bytes[1] = 0; // lat = 0, long = 0
468 bytes[1] = 0; // lat = 0, long = 128
474 a = (int)( RAD2DEG( atan2( normal[1], normal[0] ) ) * ( 255.0f / 360.0f ) );
477 b = (int)( RAD2DEG( acos( normal[2] ) ) * ( 255.0f / 360.0f ) );
480 bytes[0] = b; // longitude
481 bytes[1] = a; // lattitude
490 int PlaneTypeForNormal( vec3_t normal ) {
491 if ( normal[0] == 1.0 || normal[0] == -1.0 ) {
494 if ( normal[1] == 1.0 || normal[1] == -1.0 ) {
497 if ( normal[2] == 1.0 || normal[2] == -1.0 ) {
501 return PLANE_NON_AXIAL;
509 void MatrixMultiply( float in1[3][3], float in2[3][3], float out[3][3] ) {
510 out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
511 in1[0][2] * in2[2][0];
512 out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
513 in1[0][2] * in2[2][1];
514 out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
515 in1[0][2] * in2[2][2];
516 out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
517 in1[1][2] * in2[2][0];
518 out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
519 in1[1][2] * in2[2][1];
520 out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
521 in1[1][2] * in2[2][2];
522 out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
523 in1[2][2] * in2[2][0];
524 out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
525 in1[2][2] * in2[2][1];
526 out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
527 in1[2][2] * in2[2][2];
530 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal ){
535 inv_denom = 1.0F / DotProduct( normal, normal );
537 d = DotProduct( normal, p ) * inv_denom;
539 n[0] = normal[0] * inv_denom;
540 n[1] = normal[1] * inv_denom;
541 n[2] = normal[2] * inv_denom;
543 dst[0] = p[0] - d * n[0];
544 dst[1] = p[1] - d * n[1];
545 dst[2] = p[2] - d * n[2];
549 ** assumes "src" is normalized
551 void PerpendicularVector( vec3_t dst, const vec3_t src ){
554 vec_t minelem = 1.0F;
558 ** find the smallest magnitude axially aligned vector
560 for ( pos = 0, i = 0; i < 3; i++ )
562 if ( fabs( src[i] ) < minelem ) {
564 minelem = (vec_t)fabs( src[i] );
567 tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
571 ** project the point onto the plane defined by src
573 ProjectPointOnPlane( dst, tempvec, src );
576 ** normalize the result
578 VectorNormalize( dst, dst );
583 RotatePointAroundVector
585 This is not implemented very well...
588 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
603 PerpendicularVector( vr, dir );
604 CrossProduct( vr, vf, vup );
618 memcpy( im, m, sizeof( im ) );
627 memset( zrot, 0, sizeof( zrot ) );
628 zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
630 rad = DEG2RAD( degrees );
631 zrot[0][0] = (vec_t)cos( rad );
632 zrot[0][1] = (vec_t)sin( rad );
633 zrot[1][0] = (vec_t)-sin( rad );
634 zrot[1][1] = (vec_t)cos( rad );
636 MatrixMultiply( m, zrot, tmpmat );
637 MatrixMultiply( tmpmat, im, rot );
639 for ( i = 0; i < 3; i++ ) {
640 dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
645 ////////////////////////////////////////////////////////////////////////////////
646 // Below is double-precision math stuff. This was initially needed by the new
647 // "base winding" code in q3map2 brush processing in order to fix the famous
648 // "disappearing triangles" issue. These definitions can be used wherever extra
649 // precision is needed.
650 ////////////////////////////////////////////////////////////////////////////////
657 vec_accu_t VectorLengthAccu( const vec3_accu_t v ){
658 return (vec_accu_t) sqrt( ( v[0] * v[0] ) + ( v[1] * v[1] ) + ( v[2] * v[2] ) );
666 vec_accu_t DotProductAccu( const vec3_accu_t a, const vec3_accu_t b ){
667 return ( a[0] * b[0] ) + ( a[1] * b[1] ) + ( a[2] * b[2] );
675 void VectorSubtractAccu( const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out ){
676 out[0] = a[0] - b[0];
677 out[1] = a[1] - b[1];
678 out[2] = a[2] - b[2];
686 void VectorAddAccu( const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out ){
687 out[0] = a[0] + b[0];
688 out[1] = a[1] + b[1];
689 out[2] = a[2] + b[2];
697 void VectorCopyAccu( const vec3_accu_t in, vec3_accu_t out ){
708 void VectorScaleAccu( const vec3_accu_t in, vec_accu_t scaleFactor, vec3_accu_t out ){
709 out[0] = in[0] * scaleFactor;
710 out[1] = in[1] * scaleFactor;
711 out[2] = in[2] * scaleFactor;
719 void CrossProductAccu( const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out ){
720 out[0] = ( a[1] * b[2] ) - ( a[2] * b[1] );
721 out[1] = ( a[2] * b[0] ) - ( a[0] * b[2] );
722 out[2] = ( a[0] * b[1] ) - ( a[1] * b[0] );
730 vec_accu_t Q_rintAccu( vec_accu_t val ){
731 return (vec_accu_t) floor( val + 0.5 );
736 VectorCopyAccuToRegular
739 void VectorCopyAccuToRegular( const vec3_accu_t in, vec3_t out ){
740 out[0] = (vec_t) in[0];
741 out[1] = (vec_t) in[1];
742 out[2] = (vec_t) in[2];
747 VectorCopyRegularToAccu
750 void VectorCopyRegularToAccu( const vec3_t in, vec3_accu_t out ){
751 out[0] = (vec_accu_t) in[0];
752 out[1] = (vec_accu_t) in[1];
753 out[2] = (vec_accu_t) in[2];
761 vec_accu_t VectorNormalizeAccu( const vec3_accu_t in, vec3_accu_t out ){
762 // The sqrt() function takes double as an input and returns double as an
763 // output according the the man pages on Debian and on FreeBSD. Therefore,
764 // I don't see a reason why using a double outright (instead of using the
765 // vec_accu_t alias for example) could possibly be frowned upon.
769 length = (vec_accu_t) sqrt( ( in[0] * in[0] ) + ( in[1] * in[1] ) + ( in[2] * in[2] ) );
775 out[0] = in[0] / length;
776 out[1] = in[1] / length;
777 out[2] = in[2] / length;