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