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};
33 Given a normalized forward vector, create two
34 other perpendicular vectors
37 void MakeNormalVectors (vec3_t forward, vec3_t right, vec3_t up)
41 // this rotate and negate guarantees a vector
42 // not colinear with the original
43 right[1] = -forward[0];
44 right[2] = forward[1];
45 right[0] = forward[2];
47 d = DotProduct (right, forward);
48 VectorMA (right, -d, forward, right);
49 VectorNormalize (right, right);
50 CrossProduct (right, forward, up);
53 vec_t VectorLength(vec3_t v)
59 for (i=0 ; i< 3 ; i++)
61 length = (float)sqrt (length);
66 qboolean VectorCompare (vec3_t v1, vec3_t v2)
71 if (fabs(v1[i]-v2[i]) > EQUAL_EPSILON)
78 // FIXME TTimo this implementation has to be particular to radiant
79 // through another name I'd say
80 vec_t Q_rint (vec_t in)
82 if (g_PrefsDlg.m_bNoClamp)
85 return (float)floor (in + 0.5);
89 void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc )
91 vc[0] = va[0] + scale*vb[0];
92 vc[1] = va[1] + scale*vb[1];
93 vc[2] = va[2] + scale*vb[2];
96 void _CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
98 cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
99 cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
100 cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
103 vec_t _DotProduct (vec3_t v1, vec3_t v2)
105 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
108 void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
110 out[0] = va[0]-vb[0];
111 out[1] = va[1]-vb[1];
112 out[2] = va[2]-vb[2];
115 void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
117 out[0] = va[0]+vb[0];
118 out[1] = va[1]+vb[1];
119 out[2] = va[2]+vb[2];
122 void _VectorCopy (vec3_t in, vec3_t out)
129 vec_t VectorNormalize( const vec3_t in, vec3_t out ) {
132 length = (vec_t)sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]);
139 out[0] = in[0]/length;
140 out[1] = in[1]/length;
141 out[2] = in[2]/length;
146 vec_t VectorSetLength(const vec3_t in, vec_t length, vec3_t out) {
149 origLength = (vec_t) sqrt((in[0] * in[0]) + (in[1] * in[1]) + (in[2] * in[2]));
156 VectorScale(in, length / origLength, out);
161 vec_t ColorNormalize( const vec3_t in, vec3_t out ) {
171 out[0] = out[1] = out[2] = 1.0;
177 VectorScale (in, scale, out);
182 void VectorInverse (vec3_t v)
190 void VectorScale (vec3_t v, vec_t scale, vec3_t out)
192 out[0] = v[0] * scale;
193 out[1] = v[1] * scale;
194 out[2] = v[2] * scale;
198 void VectorRotate (vec3_t vIn, vec3_t vRotation, vec3_t out)
205 VectorCopy(va, vWork);
206 nIndex[0][0] = 1; nIndex[0][1] = 2;
207 nIndex[1][0] = 2; nIndex[1][1] = 0;
208 nIndex[2][0] = 0; nIndex[2][1] = 1;
210 for (i = 0; i < 3; i++)
212 if (vRotation[i] != 0)
214 float dAngle = vRotation[i] * Q_PI / 180.0f;
215 float c = (vec_t)cos(dAngle);
216 float s = (vec_t)sin(dAngle);
217 vWork[nIndex[i][0]] = va[nIndex[i][0]] * c - va[nIndex[i][1]] * s;
218 vWork[nIndex[i][1]] = va[nIndex[i][0]] * s + va[nIndex[i][1]] * c;
220 VectorCopy(vWork, va);
222 VectorCopy(vWork, out);
225 void VectorRotateOrigin (vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out)
227 vec3_t vTemp, vTemp2;
229 VectorSubtract(vIn, vOrigin, vTemp);
230 VectorRotate(vTemp, vRotation, vTemp2);
231 VectorAdd(vTemp2, vOrigin, out);
234 void VectorPolar(vec3_t v, float radius, float theta, float phi)
236 v[0]=(float)(radius * cos(theta) * cos(phi));
237 v[1]=(float)(radius * sin(theta) * cos(phi));
238 v[2]=(float)(radius * sin(phi));
241 void VectorSnap(vec3_t v)
244 for (i = 0; i < 3; i++)
246 v[i] = (vec_t)floor (v[i] + 0.5);
250 void VectorISnap(vec3_t point, int snap)
253 for (i = 0 ;i < 3 ; i++)
255 point[i] = (vec_t)floor (point[i] / snap + 0.5) * snap;
259 void VectorFSnap(vec3_t point, float snap)
262 for (i = 0 ;i < 3 ; i++)
264 point[i] = (vec_t)floor (point[i] / snap + 0.5) * snap;
268 void _Vector5Add (vec5_t va, vec5_t vb, vec5_t out)
270 out[0] = va[0]+vb[0];
271 out[1] = va[1]+vb[1];
272 out[2] = va[2]+vb[2];
273 out[3] = va[3]+vb[3];
274 out[4] = va[4]+vb[4];
277 void _Vector5Scale (vec5_t v, vec_t scale, vec5_t out)
279 out[0] = v[0] * scale;
280 out[1] = v[1] * scale;
281 out[2] = v[2] * scale;
282 out[3] = v[3] * scale;
283 out[4] = v[4] * scale;
286 void _Vector53Copy (vec5_t in, vec3_t out)
293 // NOTE: added these from Ritual's Q3Radiant
294 void ClearBounds (vec3_t mins, vec3_t maxs)
296 mins[0] = mins[1] = mins[2] = 99999;
297 maxs[0] = maxs[1] = maxs[2] = -99999;
300 void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs)
305 for (i=0 ; i<3 ; i++)
315 #define PITCH 0 // up / down
316 #define YAW 1 // left / right
317 #define ROLL 2 // fall over
319 #define M_PI 3.14159265358979323846f // matches value in gcc v2 math.h
322 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
325 static float sr, sp, sy, cr, cp, cy;
326 // static to help MS compiler fp bugs
328 angle = angles[YAW] * (M_PI*2.0f / 360.0f);
329 sy = (vec_t)sin(angle);
330 cy = (vec_t)cos(angle);
331 angle = angles[PITCH] * (M_PI*2.0f / 360.0f);
332 sp = (vec_t)sin(angle);
333 cp = (vec_t)cos(angle);
334 angle = angles[ROLL] * (M_PI*2.0f / 360.0f);
335 sr = (vec_t)sin(angle);
336 cr = (vec_t)cos(angle);
346 right[0] = -sr*sp*cy+cr*sy;
347 right[1] = -sr*sp*sy-cr*cy;
352 up[0] = cr*sp*cy+sr*sy;
353 up[1] = cr*sp*sy-sr*cy;
358 void VectorToAngles( vec3_t vec, vec3_t angles )
363 if ( ( vec[ 0 ] == 0 ) && ( vec[ 1 ] == 0 ) )
377 yaw = (vec_t)atan2( vec[ 1 ], vec[ 0 ] ) * 180 / M_PI;
383 forward = ( float )sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] );
384 pitch = (vec_t)atan2( vec[ 2 ], forward ) * 180 / M_PI;
397 =====================
400 Returns false if the triangle is degenrate.
401 The normal will point out of the clock for clockwise ordered points
402 =====================
404 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
407 VectorSubtract( b, a, d1 );
408 VectorSubtract( c, a, d2 );
409 CrossProduct( d2, d1, plane );
410 if ( VectorNormalize( plane, plane ) == 0 ) {
414 plane[3] = DotProduct( a, plane );
421 ** We use two byte encoded normals in some space critical applications.
422 ** Lat = 0 at (1,0,0) to 360 (-1,0,0), encoded in 8-bit sine table format
423 ** Lng = 0 at (0,0,1) to 180 (0,0,-1), encoded in 8-bit sine table format
426 void NormalToLatLong( const vec3_t normal, byte bytes[2] ) {
427 // check for singularities
428 if ( normal[0] == 0 && normal[1] == 0 ) {
429 if ( normal[2] > 0 ) {
431 bytes[1] = 0; // lat = 0, long = 0
434 bytes[1] = 0; // lat = 0, long = 128
439 a = (int)( RAD2DEG( atan2( normal[1], normal[0] ) ) * (255.0f / 360.0f ) );
442 b = (int)( RAD2DEG( acos( normal[2] ) ) * ( 255.0f / 360.0f ) );
445 bytes[0] = b; // longitude
446 bytes[1] = a; // lattitude
455 int PlaneTypeForNormal (vec3_t normal) {
456 if (normal[0] == 1.0 || normal[0] == -1.0)
458 if (normal[1] == 1.0 || normal[1] == -1.0)
460 if (normal[2] == 1.0 || normal[2] == -1.0)
463 return PLANE_NON_AXIAL;
471 void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
472 out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
473 in1[0][2] * in2[2][0];
474 out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
475 in1[0][2] * in2[2][1];
476 out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
477 in1[0][2] * in2[2][2];
478 out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
479 in1[1][2] * in2[2][0];
480 out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
481 in1[1][2] * in2[2][1];
482 out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
483 in1[1][2] * in2[2][2];
484 out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
485 in1[2][2] * in2[2][0];
486 out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
487 in1[2][2] * in2[2][1];
488 out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
489 in1[2][2] * in2[2][2];
492 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
498 inv_denom = 1.0F / DotProduct( normal, normal );
500 d = DotProduct( normal, p ) * inv_denom;
502 n[0] = normal[0] * inv_denom;
503 n[1] = normal[1] * inv_denom;
504 n[2] = normal[2] * inv_denom;
506 dst[0] = p[0] - d * n[0];
507 dst[1] = p[1] - d * n[1];
508 dst[2] = p[2] - d * n[2];
512 ** assumes "src" is normalized
514 void PerpendicularVector( vec3_t dst, const vec3_t src )
518 vec_t minelem = 1.0F;
522 ** find the smallest magnitude axially aligned vector
524 for ( pos = 0, i = 0; i < 3; i++ )
526 if ( fabs( src[i] ) < minelem )
529 minelem = (vec_t)fabs( src[i] );
532 tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
536 ** project the point onto the plane defined by src
538 ProjectPointOnPlane( dst, tempvec, src );
541 ** normalize the result
543 VectorNormalize( dst, dst );
548 RotatePointAroundVector
550 This is not implemented very well...
553 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
568 PerpendicularVector( vr, dir );
569 CrossProduct( vr, vf, vup );
583 memcpy( im, m, sizeof( im ) );
592 memset( zrot, 0, sizeof( zrot ) );
593 zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
595 rad = DEG2RAD( degrees );
596 zrot[0][0] = (vec_t)cos( rad );
597 zrot[0][1] = (vec_t)sin( rad );
598 zrot[1][0] = (vec_t)-sin( rad );
599 zrot[1][1] = (vec_t)cos( rad );
601 MatrixMultiply( m, zrot, tmpmat );
602 MatrixMultiply( tmpmat, im, rot );
604 for ( i = 0; i < 3; i++ ) {
605 dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];