2 Copyright (C) 1999-2006 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 const vec3_t vec3_origin = {0.0f,0.0f,0.0f};
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, };
38 qboolean VectorIsOnAxis( vec3_t v ){
39 int i, zeroComponentCount;
41 zeroComponentCount = 0;
42 for ( i = 0; i < 3; i++ )
49 if ( zeroComponentCount > 1 ) {
50 // The zero vector will be on axis.
62 qboolean VectorIsOnAxialPlane( vec3_t v ){
65 for ( i = 0; i < 3; i++ )
68 // The zero vector will be on axial plane.
80 Given a normalized forward vector, create two
81 other perpendicular vectors
84 void MakeNormalVectors( vec3_t forward, vec3_t right, vec3_t up ){
87 // this rotate and negate guarantees a vector
88 // not colinear with the original
89 right[1] = -forward[0];
90 right[2] = forward[1];
91 right[0] = forward[2];
93 d = DotProduct( right, forward );
94 VectorMA( right, -d, forward, right );
95 VectorNormalize( right, right );
96 CrossProduct( right, forward, up );
99 vec_t VectorLength( const vec3_t v ){
104 for ( i = 0 ; i < 3 ; i++ )
105 length += v[i] * v[i];
106 length = (float)sqrt( length );
111 qboolean VectorCompare( const vec3_t v1, const vec3_t v2 ){
114 for ( i = 0 ; i < 3 ; i++ )
115 if ( fabs( v1[i] - v2[i] ) > EQUAL_EPSILON ) {
122 void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc ){
123 vc[0] = va[0] + scale * vb[0];
124 vc[1] = va[1] + scale * vb[1];
125 vc[2] = va[2] + scale * vb[2];
128 void _CrossProduct( vec3_t v1, vec3_t v2, vec3_t cross ){
129 cross[0] = v1[1] * v2[2] - v1[2] * v2[1];
130 cross[1] = v1[2] * v2[0] - v1[0] * v2[2];
131 cross[2] = v1[0] * v2[1] - v1[1] * v2[0];
134 vec_t _DotProduct( vec3_t v1, vec3_t v2 ){
135 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
138 void _VectorSubtract( vec3_t va, vec3_t vb, vec3_t out ){
139 out[0] = va[0] - vb[0];
140 out[1] = va[1] - vb[1];
141 out[2] = va[2] - vb[2];
144 void _VectorAdd( vec3_t va, vec3_t vb, vec3_t out ){
145 out[0] = va[0] + vb[0];
146 out[1] = va[1] + vb[1];
147 out[2] = va[2] + vb[2];
150 void _VectorCopy( vec3_t in, vec3_t out ){
156 vec_t VectorNormalize( const vec3_t in, vec3_t out ) {
158 #if MATHLIB_VECTOR_NORMALIZE_PRECISION_FIX
160 // The sqrt() function takes double as an input and returns double as an
161 // output according the the man pages on Debian and on FreeBSD. Therefore,
162 // I don't see a reason why using a double outright (instead of using the
163 // vec_accu_t alias for example) could possibly be frowned upon.
165 double x, y, z, length;
171 length = sqrt( ( x * x ) + ( y * y ) + ( z * z ) );
177 out[0] = (vec_t) ( x / length );
178 out[1] = (vec_t) ( y / length );
179 out[2] = (vec_t) ( z / length );
181 return (vec_t) length;
185 vec_t length, ilength;
187 length = (vec_t)sqrt( in[0] * in[0] + in[1] * in[1] + in[2] * in[2] );
193 ilength = 1.0f / length;
194 out[0] = in[0] * ilength;
195 out[1] = in[1] * ilength;
196 out[2] = in[2] * ilength;
204 vec_t ColorNormalize( const vec3_t in, vec3_t out ) {
216 out[0] = out[1] = out[2] = 1.0;
222 VectorScale( in, scale, out );
227 void VectorInverse( vec3_t v ){
234 void VectorScale (vec3_t v, vec_t scale, vec3_t out)
236 out[0] = v[0] * scale;
237 out[1] = v[1] * scale;
238 out[2] = v[2] * scale;
242 void VectorRotate( vec3_t vIn, vec3_t vRotation, vec3_t out ){
247 VectorCopy( vIn, va );
248 VectorCopy( va, vWork );
249 nIndex[0][0] = 1; nIndex[0][1] = 2;
250 nIndex[1][0] = 2; nIndex[1][1] = 0;
251 nIndex[2][0] = 0; nIndex[2][1] = 1;
253 for ( i = 0; i < 3; i++ )
255 if ( vRotation[i] != 0 ) {
256 float dAngle = vRotation[i] * Q_PI / 180.0f;
257 float c = (vec_t)cos( dAngle );
258 float s = (vec_t)sin( dAngle );
259 vWork[nIndex[i][0]] = va[nIndex[i][0]] * c - va[nIndex[i][1]] * s;
260 vWork[nIndex[i][1]] = va[nIndex[i][0]] * s + va[nIndex[i][1]] * c;
262 VectorCopy( vWork, va );
264 VectorCopy( vWork, out );
267 void VectorRotateOrigin( vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out ){
268 vec3_t vTemp, vTemp2;
270 VectorSubtract( vIn, vOrigin, vTemp );
271 VectorRotate( vTemp, vRotation, vTemp2 );
272 VectorAdd( vTemp2, vOrigin, out );
275 void VectorPolar( vec3_t v, float radius, float theta, float phi ){
276 v[0] = (float)( radius * cos( theta ) * cos( phi ) );
277 v[1] = (float)( radius * sin( theta ) * cos( phi ) );
278 v[2] = (float)( radius * sin( phi ) );
281 void VectorSnap( vec3_t v ){
283 for ( i = 0; i < 3; i++ )
285 v[i] = (vec_t)FLOAT_TO_INTEGER( v[i] );
289 void VectorISnap( vec3_t point, int snap ){
291 for ( i = 0 ; i < 3 ; i++ )
293 point[i] = (vec_t)FLOAT_SNAP( point[i], snap );
297 void VectorFSnap( vec3_t point, float snap ){
299 for ( i = 0 ; i < 3 ; i++ )
301 point[i] = (vec_t)FLOAT_SNAP( point[i], snap );
305 void _Vector5Add( vec5_t va, vec5_t vb, vec5_t out ){
306 out[0] = va[0] + vb[0];
307 out[1] = va[1] + vb[1];
308 out[2] = va[2] + vb[2];
309 out[3] = va[3] + vb[3];
310 out[4] = va[4] + vb[4];
313 void _Vector5Scale( vec5_t v, vec_t scale, vec5_t out ){
314 out[0] = v[0] * scale;
315 out[1] = v[1] * scale;
316 out[2] = v[2] * scale;
317 out[3] = v[3] * scale;
318 out[4] = v[4] * scale;
321 void _Vector53Copy( vec5_t in, vec3_t out ){
327 // NOTE: added these from Ritual's Q3Radiant
328 #define INVALID_BOUNDS 99999
329 void ClearBounds( vec3_t mins, vec3_t maxs ){
330 mins[0] = mins[1] = mins[2] = +INVALID_BOUNDS;
331 maxs[0] = maxs[1] = maxs[2] = -INVALID_BOUNDS;
334 void AddPointToBounds( vec3_t v, vec3_t mins, vec3_t maxs ){
338 if ( mins[0] == +INVALID_BOUNDS ) {
339 if ( maxs[0] == -INVALID_BOUNDS ) {
340 VectorCopy( v, mins );
341 VectorCopy( v, maxs );
345 for ( i = 0 ; i < 3 ; i++ )
348 if ( val < mins[i] ) {
351 if ( val > maxs[i] ) {
357 void AngleVectors( vec3_t angles, vec3_t forward, vec3_t right, vec3_t up ){
359 static float sr, sp, sy, cr, cp, cy;
360 // static to help MS compiler fp bugs
362 angle = angles[YAW] * ( Q_PI * 2.0f / 360.0f );
363 sy = (vec_t)sin( angle );
364 cy = (vec_t)cos( angle );
365 angle = angles[PITCH] * ( Q_PI * 2.0f / 360.0f );
366 sp = (vec_t)sin( angle );
367 cp = (vec_t)cos( angle );
368 angle = angles[ROLL] * ( Q_PI * 2.0f / 360.0f );
369 sr = (vec_t)sin( angle );
370 cr = (vec_t)cos( angle );
373 forward[0] = cp * cy;
374 forward[1] = cp * sy;
378 right[0] = -sr * sp * cy + cr * sy;
379 right[1] = -sr * sp * sy - cr * cy;
383 up[0] = cr * sp * cy + sr * sy;
384 up[1] = cr * sp * sy - sr * cy;
389 void VectorToAngles( vec3_t vec, vec3_t angles ){
393 if ( ( vec[ 0 ] == 0 ) && ( vec[ 1 ] == 0 ) ) {
395 if ( vec[ 2 ] > 0 ) {
405 yaw = (vec_t)atan2( vec[ 1 ], vec[ 0 ] ) * 180 / Q_PI;
410 forward = ( float )sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] );
411 pitch = (vec_t)atan2( vec[ 2 ], forward ) * 180 / Q_PI;
423 =====================
426 Returns false if the triangle is degenrate.
427 The normal will point out of the clock for clockwise ordered points
428 =====================
430 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
433 VectorSubtract( b, a, d1 );
434 VectorSubtract( c, a, d2 );
435 CrossProduct( d2, d1, plane );
436 if ( VectorNormalize( plane, plane ) == 0 ) {
440 plane[3] = DotProduct( a, plane );
447 ** We use two byte encoded normals in some space critical applications.
448 ** Lat = 0 at (1,0,0) to 360 (-1,0,0), encoded in 8-bit sine table format
449 ** Lng = 0 at (0,0,1) to 180 (0,0,-1), encoded in 8-bit sine table format
452 void NormalToLatLong( const vec3_t normal, byte bytes[2] ) {
453 // check for singularities
454 if ( normal[0] == 0 && normal[1] == 0 ) {
455 if ( normal[2] > 0 ) {
457 bytes[1] = 0; // lat = 0, long = 0
461 bytes[1] = 0; // lat = 0, long = 128
467 a = (int)( RAD2DEG( atan2( normal[1], normal[0] ) ) * ( 255.0f / 360.0f ) );
470 b = (int)( RAD2DEG( acos( normal[2] ) ) * ( 255.0f / 360.0f ) );
473 bytes[0] = b; // longitude
474 bytes[1] = a; // lattitude
483 int PlaneTypeForNormal( vec3_t normal ) {
484 if ( normal[0] == 1.0 || normal[0] == -1.0 ) {
487 if ( normal[1] == 1.0 || normal[1] == -1.0 ) {
490 if ( normal[2] == 1.0 || normal[2] == -1.0 ) {
494 return PLANE_NON_AXIAL;
502 void MatrixMultiply( float in1[3][3], float in2[3][3], float out[3][3] ) {
503 out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
504 in1[0][2] * in2[2][0];
505 out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
506 in1[0][2] * in2[2][1];
507 out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
508 in1[0][2] * in2[2][2];
509 out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
510 in1[1][2] * in2[2][0];
511 out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
512 in1[1][2] * in2[2][1];
513 out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
514 in1[1][2] * in2[2][2];
515 out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
516 in1[2][2] * in2[2][0];
517 out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
518 in1[2][2] * in2[2][1];
519 out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
520 in1[2][2] * in2[2][2];
523 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal ){
528 inv_denom = 1.0F / DotProduct( normal, normal );
530 d = DotProduct( normal, p ) * inv_denom;
532 n[0] = normal[0] * inv_denom;
533 n[1] = normal[1] * inv_denom;
534 n[2] = normal[2] * inv_denom;
536 dst[0] = p[0] - d * n[0];
537 dst[1] = p[1] - d * n[1];
538 dst[2] = p[2] - d * n[2];
542 ** assumes "src" is normalized
544 void PerpendicularVector( vec3_t dst, const vec3_t src ){
547 vec_t minelem = 1.0F;
551 ** find the smallest magnitude axially aligned vector
553 for ( pos = 0, i = 0; i < 3; i++ )
555 if ( fabs( src[i] ) < minelem ) {
557 minelem = (vec_t)fabs( src[i] );
560 tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
564 ** project the point onto the plane defined by src
566 ProjectPointOnPlane( dst, tempvec, src );
569 ** normalize the result
571 VectorNormalize( dst, dst );
576 RotatePointAroundVector
578 This is not implemented very well...
581 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
596 PerpendicularVector( vr, dir );
597 CrossProduct( vr, vf, vup );
611 memcpy( im, m, sizeof( im ) );
620 memset( zrot, 0, sizeof( zrot ) );
621 zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
623 rad = (float)DEG2RAD( degrees );
624 zrot[0][0] = (vec_t)cos( rad );
625 zrot[0][1] = (vec_t)sin( rad );
626 zrot[1][0] = (vec_t)-sin( rad );
627 zrot[1][1] = (vec_t)cos( rad );
629 MatrixMultiply( m, zrot, tmpmat );
630 MatrixMultiply( tmpmat, im, rot );
632 for ( i = 0; i < 3; i++ ) {
633 dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
638 ////////////////////////////////////////////////////////////////////////////////
639 // Below is double-precision math stuff. This was initially needed by the new
640 // "base winding" code in q3map2 brush processing in order to fix the famous
641 // "disappearing triangles" issue. These definitions can be used wherever extra
642 // precision is needed.
643 ////////////////////////////////////////////////////////////////////////////////
650 vec_accu_t VectorLengthAccu( const vec3_accu_t v ){
651 return (vec_accu_t) sqrt( ( v[0] * v[0] ) + ( v[1] * v[1] ) + ( v[2] * v[2] ) );
659 vec_accu_t DotProductAccu( const vec3_accu_t a, const vec3_accu_t b ){
660 return ( a[0] * b[0] ) + ( a[1] * b[1] ) + ( a[2] * b[2] );
668 void VectorSubtractAccu( const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out ){
669 out[0] = a[0] - b[0];
670 out[1] = a[1] - b[1];
671 out[2] = a[2] - b[2];
679 void VectorAddAccu( const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out ){
680 out[0] = a[0] + b[0];
681 out[1] = a[1] + b[1];
682 out[2] = a[2] + b[2];
690 void VectorCopyAccu( const vec3_accu_t in, vec3_accu_t out ){
701 void VectorScaleAccu( const vec3_accu_t in, vec_accu_t scaleFactor, vec3_accu_t out ){
702 out[0] = in[0] * scaleFactor;
703 out[1] = in[1] * scaleFactor;
704 out[2] = in[2] * scaleFactor;
712 void CrossProductAccu( const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out ){
713 out[0] = ( a[1] * b[2] ) - ( a[2] * b[1] );
714 out[1] = ( a[2] * b[0] ) - ( a[0] * b[2] );
715 out[2] = ( a[0] * b[1] ) - ( a[1] * b[0] );
723 vec_accu_t Q_rintAccu( vec_accu_t val ){
724 return (vec_accu_t) floor( val + 0.5 );
729 VectorCopyAccuToRegular
732 void VectorCopyAccuToRegular( const vec3_accu_t in, vec3_t out ){
733 out[0] = (vec_t) in[0];
734 out[1] = (vec_t) in[1];
735 out[2] = (vec_t) in[2];
740 VectorCopyRegularToAccu
743 void VectorCopyRegularToAccu( const vec3_t in, vec3_accu_t out ){
744 out[0] = (vec_accu_t) in[0];
745 out[1] = (vec_accu_t) in[1];
746 out[2] = (vec_accu_t) in[2];
754 vec_accu_t VectorNormalizeAccu( const vec3_accu_t in, vec3_accu_t out ){
755 // The sqrt() function takes double as an input and returns double as an
756 // output according the the man pages on Debian and on FreeBSD. Therefore,
757 // I don't see a reason why using a double outright (instead of using the
758 // vec_accu_t alias for example) could possibly be frowned upon.
762 length = (vec_accu_t) sqrt( ( in[0] * in[0] ) + ( in[1] * in[1] ) + ( in[2] * in[2] ) );
768 out[0] = in[0] / length;
769 out[1] = in[1] / length;
770 out[2] = in[2] / length;