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