]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/mathlib/mathlib.c
Undoing revision 377 (reverting just those files modified by that
[xonotic/netradiant.git] / libs / mathlib / mathlib.c
1 /*
2 Copyright (C) 1999-2007 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 vec3_t vec3_origin = {0.0f,0.0f,0.0f};
28
29 /*
30 ================
31 MakeNormalVectors
32
33 Given a normalized forward vector, create two
34 other perpendicular vectors
35 ================
36 */
37 void MakeNormalVectors (vec3_t forward, vec3_t right, vec3_t up)
38 {
39         float           d;
40
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];
46
47         d = DotProduct (right, forward);
48         VectorMA (right, -d, forward, right);
49         VectorNormalize (right, right);
50         CrossProduct (right, forward, up);
51 }
52
53 vec_t VectorLength(vec3_t v)
54 {
55         int             i;
56         float   length;
57         
58         length = 0.0f;
59         for (i=0 ; i< 3 ; i++)
60                 length += v[i]*v[i];
61         length = (float)sqrt (length);
62
63         return length;
64 }
65
66 qboolean VectorCompare (vec3_t v1, vec3_t v2)
67 {
68         int             i;
69         
70         for (i=0 ; i<3 ; i++)
71                 if (fabs(v1[i]-v2[i]) > EQUAL_EPSILON)
72                         return qfalse;
73                         
74         return qtrue;
75 }
76
77 /*
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)
81 {
82   if (g_PrefsDlg.m_bNoClamp)
83     return in;
84   else
85     return (float)floor (in + 0.5);
86 }
87 */
88
89 void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc )
90 {
91         vc[0] = va[0] + scale*vb[0];
92         vc[1] = va[1] + scale*vb[1];
93         vc[2] = va[2] + scale*vb[2];
94 }
95
96 void _CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
97 {
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];
101 }
102
103 vec_t _DotProduct (vec3_t v1, vec3_t v2)
104 {
105         return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
106 }
107
108 void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
109 {
110         out[0] = va[0]-vb[0];
111         out[1] = va[1]-vb[1];
112         out[2] = va[2]-vb[2];
113 }
114
115 void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
116 {
117         out[0] = va[0]+vb[0];
118         out[1] = va[1]+vb[1];
119         out[2] = va[2]+vb[2];
120 }
121
122 void _VectorCopy (vec3_t in, vec3_t out)
123 {
124         out[0] = in[0];
125         out[1] = in[1];
126         out[2] = in[2];
127 }
128
129 vec_t VectorNormalize( const vec3_t in, vec3_t out ) {
130         vec_t   length;
131
132         length = (vec_t)sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]);
133         if (length == 0)
134         {
135                 VectorClear (out);
136                 return 0;
137         }
138
139         out[0] = in[0]/length;
140         out[1] = in[1]/length;
141         out[2] = in[2]/length;
142
143         return length;
144 }
145
146 vec_t VectorSetLength(const vec3_t in, vec_t length, vec3_t out) {
147         vec_t   origLength;
148
149         origLength = (vec_t) sqrt((in[0] * in[0]) + (in[1] * in[1]) + (in[2] * in[2]));
150         if (origLength == 0)
151         {
152                 VectorClear(out);
153                 return 0;
154         }
155
156         VectorScale(in, length / origLength, out);
157
158         return origLength;
159 }
160
161 vec_t ColorNormalize( const vec3_t in, vec3_t out ) {
162         float   max, scale;
163
164         max = in[0];
165         if (in[1] > max)
166                 max = in[1];
167         if (in[2] > max)
168                 max = in[2];
169
170         if (max == 0) {
171                 out[0] = out[1] = out[2] = 1.0;
172                 return 0;
173         }
174
175         scale = 1.0f / max;
176
177         VectorScale (in, scale, out);
178
179         return max;
180 }
181
182 void VectorInverse (vec3_t v)
183 {
184         v[0] = -v[0];
185         v[1] = -v[1];
186         v[2] = -v[2];
187 }
188
189 /*
190 void VectorScale (vec3_t v, vec_t scale, vec3_t out)
191 {
192         out[0] = v[0] * scale;
193         out[1] = v[1] * scale;
194         out[2] = v[2] * scale;
195 }
196 */
197
198 void VectorRotate (vec3_t vIn, vec3_t vRotation, vec3_t out)
199 {
200   vec3_t vWork, va;
201   int nIndex[3][2];
202   int i;
203
204   VectorCopy(vIn, va);
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;
209
210   for (i = 0; i < 3; i++)
211   {
212     if (vRotation[i] != 0)
213     {
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;
219     }
220     VectorCopy(vWork, va);
221   }
222   VectorCopy(vWork, out);
223 }
224
225 void VectorRotateOrigin (vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out)
226 {
227   vec3_t vTemp, vTemp2;
228
229   VectorSubtract(vIn, vOrigin, vTemp);
230   VectorRotate(vTemp, vRotation, vTemp2);
231   VectorAdd(vTemp2, vOrigin, out);
232 }
233
234 void VectorPolar(vec3_t v, float radius, float theta, float phi)
235 {
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));
239 }
240
241 void VectorSnap(vec3_t v)
242 {
243   int i;
244   for (i = 0; i < 3; i++)
245   {
246     v[i] = (vec_t)floor (v[i] + 0.5);
247   }
248 }
249
250 void VectorISnap(vec3_t point, int snap)
251 {
252   int i;
253         for (i = 0 ;i < 3 ; i++)
254         {
255                 point[i] = (vec_t)floor (point[i] / snap + 0.5) * snap;
256         }
257 }
258
259 void VectorFSnap(vec3_t point, float snap)
260 {
261   int i;
262         for (i = 0 ;i < 3 ; i++)
263         {
264                 point[i] = (vec_t)floor (point[i] / snap + 0.5) * snap;
265         }
266 }
267
268 void _Vector5Add (vec5_t va, vec5_t vb, vec5_t out)
269 {
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];
275 }
276
277 void _Vector5Scale (vec5_t v, vec_t scale, vec5_t out)
278 {
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;
284 }
285
286 void _Vector53Copy (vec5_t in, vec3_t out)
287 {
288         out[0] = in[0];
289         out[1] = in[1];
290         out[2] = in[2];
291 }
292
293 // NOTE: added these from Ritual's Q3Radiant
294 void ClearBounds (vec3_t mins, vec3_t maxs)
295 {
296         mins[0] = mins[1] = mins[2] = 99999;
297         maxs[0] = maxs[1] = maxs[2] = -99999;
298 }
299
300 void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs)
301 {
302         int             i;
303         vec_t   val;
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 #define PITCH                           0               // up / down
316 #define YAW                                     1               // left / right
317 #define ROLL                            2               // fall over
318 #ifndef M_PI
319 #define M_PI            3.14159265358979323846f // matches value in gcc v2 math.h
320 #endif
321
322 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
323 {
324         float           angle;
325         static float            sr, sp, sy, cr, cp, cy;
326         // static to help MS compiler fp bugs
327         
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);
337         
338         if (forward)
339         {
340                 forward[0] = cp*cy;
341                 forward[1] = cp*sy;
342                 forward[2] = -sp;
343         }
344         if (right)
345         {
346                 right[0] = -sr*sp*cy+cr*sy;
347                 right[1] = -sr*sp*sy-cr*cy;
348                 right[2] = -sr*cp;
349         }
350         if (up)
351         {
352                 up[0] = cr*sp*cy+sr*sy;
353                 up[1] = cr*sp*sy-sr*cy;
354                 up[2] = cr*cp;
355         }
356 }
357
358 void VectorToAngles( vec3_t vec, vec3_t angles )
359 {
360         float forward;
361         float yaw, pitch;
362         
363         if ( ( vec[ 0 ] == 0 ) && ( vec[ 1 ] == 0 ) )
364         {
365                 yaw = 0;
366                 if ( vec[ 2 ] > 0 )
367                 {
368                         pitch = 90;
369                 }
370                 else
371                 {
372                         pitch = 270;
373                 }
374         }
375         else
376         {
377                 yaw = (vec_t)atan2( vec[ 1 ], vec[ 0 ] ) * 180 / M_PI;
378                 if ( yaw < 0 )
379                 {
380                         yaw += 360;
381                 }
382                 
383                 forward = ( float )sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] );
384                 pitch = (vec_t)atan2( vec[ 2 ], forward ) * 180 / M_PI;
385                 if ( pitch < 0 )
386                 {
387                         pitch += 360;
388                 }
389         }
390         
391         angles[ 0 ] = pitch;
392         angles[ 1 ] = yaw;
393         angles[ 2 ] = 0;
394 }
395
396 /*
397 =====================
398 PlaneFromPoints
399
400 Returns false if the triangle is degenrate.
401 The normal will point out of the clock for clockwise ordered points
402 =====================
403 */
404 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
405         vec3_t  d1, d2;
406
407         VectorSubtract( b, a, d1 );
408         VectorSubtract( c, a, d2 );
409         CrossProduct( d2, d1, plane );
410         if ( VectorNormalize( plane, plane ) == 0 ) {
411                 return qfalse;
412         }
413
414         plane[3] = DotProduct( a, plane );
415         return qtrue;
416 }
417
418 /*
419 ** NormalToLatLong
420 **
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
424 **
425 */
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 ) {
430                         bytes[0] = 0;
431                         bytes[1] = 0;           // lat = 0, long = 0
432                 } else {
433                         bytes[0] = 128;
434                         bytes[1] = 0;           // lat = 0, long = 128
435                 }
436         } else {
437                 int     a, b;
438
439                 a = (int)( RAD2DEG( atan2( normal[1], normal[0] ) ) * (255.0f / 360.0f ) );
440                 a &= 0xff;
441
442                 b = (int)( RAD2DEG( acos( normal[2] ) ) * ( 255.0f / 360.0f ) );
443                 b &= 0xff;
444
445                 bytes[0] = b;   // longitude
446                 bytes[1] = a;   // lattitude
447         }
448 }
449
450 /*
451 =================
452 PlaneTypeForNormal
453 =================
454 */
455 int     PlaneTypeForNormal (vec3_t normal) {
456         if (normal[0] == 1.0 || normal[0] == -1.0)
457                 return PLANE_X;
458         if (normal[1] == 1.0 || normal[1] == -1.0)
459                 return PLANE_Y;
460         if (normal[2] == 1.0 || normal[2] == -1.0)
461                 return PLANE_Z;
462         
463         return PLANE_NON_AXIAL;
464 }
465
466 /*
467 ================
468 MatrixMultiply
469 ================
470 */
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];
490 }
491
492 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
493 {
494         float d;
495         vec3_t n;
496         float inv_denom;
497
498         inv_denom = 1.0F / DotProduct( normal, normal );
499
500         d = DotProduct( normal, p ) * inv_denom;
501
502         n[0] = normal[0] * inv_denom;
503         n[1] = normal[1] * inv_denom;
504         n[2] = normal[2] * inv_denom;
505
506         dst[0] = p[0] - d * n[0];
507         dst[1] = p[1] - d * n[1];
508         dst[2] = p[2] - d * n[2];
509 }
510
511 /*
512 ** assumes "src" is normalized
513 */
514 void PerpendicularVector( vec3_t dst, const vec3_t src )
515 {
516         int     pos;
517         int i;
518         vec_t minelem = 1.0F;
519         vec3_t tempvec;
520
521         /*
522         ** find the smallest magnitude axially aligned vector
523         */
524         for ( pos = 0, i = 0; i < 3; i++ )
525         {
526                 if ( fabs( src[i] ) < minelem )
527                 {
528                         pos = i;
529                         minelem = (vec_t)fabs( src[i] );
530                 }
531         }
532         tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
533         tempvec[pos] = 1.0F;
534
535         /*
536         ** project the point onto the plane defined by src
537         */
538         ProjectPointOnPlane( dst, tempvec, src );
539
540         /*
541         ** normalize the result
542         */
543         VectorNormalize( dst, dst );
544 }
545
546 /*
547 ===============
548 RotatePointAroundVector
549
550 This is not implemented very well...
551 ===============
552 */
553 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
554                                                          float degrees ) {
555         float   m[3][3];
556         float   im[3][3];
557         float   zrot[3][3];
558         float   tmpmat[3][3];
559         float   rot[3][3];
560         int     i;
561         vec3_t vr, vup, vf;
562         float   rad;
563
564         vf[0] = dir[0];
565         vf[1] = dir[1];
566         vf[2] = dir[2];
567
568         PerpendicularVector( vr, dir );
569         CrossProduct( vr, vf, vup );
570
571         m[0][0] = vr[0];
572         m[1][0] = vr[1];
573         m[2][0] = vr[2];
574
575         m[0][1] = vup[0];
576         m[1][1] = vup[1];
577         m[2][1] = vup[2];
578
579         m[0][2] = vf[0];
580         m[1][2] = vf[1];
581         m[2][2] = vf[2];
582
583         memcpy( im, m, sizeof( im ) );
584
585         im[0][1] = m[1][0];
586         im[0][2] = m[2][0];
587         im[1][0] = m[0][1];
588         im[1][2] = m[2][1];
589         im[2][0] = m[0][2];
590         im[2][1] = m[1][2];
591
592         memset( zrot, 0, sizeof( zrot ) );
593         zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
594
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 );
600
601         MatrixMultiply( m, zrot, tmpmat );
602         MatrixMultiply( tmpmat, im, rot );
603
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];
606         }
607 }