]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/extra/netradiant-src/libs/mathlib/mathlib.c
Rename the compiled fteqcc to fteqcc-win32 (as that's what it is)
[voretournament/voretournament.git] / misc / mediasource / extra / netradiant-src / 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 VectorIsOnAxis
36 ================
37 */
38 qboolean VectorIsOnAxis(vec3_t v)
39 {
40         int     i, zeroComponentCount;
41
42         zeroComponentCount = 0;
43         for (i = 0; i < 3; i++)
44         {
45                 if (v[i] == 0.0)
46                 {
47                         zeroComponentCount++;
48                 }
49         }
50
51         if (zeroComponentCount > 1)
52         {
53                 // The zero vector will be on axis.
54                 return qtrue;
55         }
56
57         return qfalse;
58 }
59
60 /*
61 ================
62 VectorIsOnAxialPlane
63 ================
64 */
65 qboolean VectorIsOnAxialPlane(vec3_t v)
66 {
67         int     i;
68
69         for (i = 0; i < 3; i++)
70         {
71                 if (v[i] == 0.0)
72                 {
73                         // The zero vector will be on axial plane.
74                         return qtrue;
75                 }
76         }
77
78         return qfalse;
79 }
80
81 /*
82 ================
83 MakeNormalVectors
84
85 Given a normalized forward vector, create two
86 other perpendicular vectors
87 ================
88 */
89 void MakeNormalVectors (vec3_t forward, vec3_t right, vec3_t up)
90 {
91         float           d;
92
93         // this rotate and negate guarantees a vector
94         // not colinear with the original
95         right[1] = -forward[0];
96         right[2] = forward[1];
97         right[0] = forward[2];
98
99         d = DotProduct (right, forward);
100         VectorMA (right, -d, forward, right);
101         VectorNormalize (right, right);
102         CrossProduct (right, forward, up);
103 }
104
105 vec_t VectorLength(const vec3_t v)
106 {
107         int             i;
108         float   length;
109         
110         length = 0.0f;
111         for (i=0 ; i< 3 ; i++)
112                 length += v[i]*v[i];
113         length = (float)sqrt (length);
114
115         return length;
116 }
117
118 qboolean VectorCompare (const vec3_t v1, const vec3_t v2)
119 {
120         int             i;
121         
122         for (i=0 ; i<3 ; i++)
123                 if (fabs(v1[i]-v2[i]) > EQUAL_EPSILON)
124                         return qfalse;
125                         
126         return qtrue;
127 }
128
129 void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc )
130 {
131         vc[0] = va[0] + scale*vb[0];
132         vc[1] = va[1] + scale*vb[1];
133         vc[2] = va[2] + scale*vb[2];
134 }
135
136 void _CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
137 {
138         cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
139         cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
140         cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
141 }
142
143 vec_t _DotProduct (vec3_t v1, vec3_t v2)
144 {
145         return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
146 }
147
148 void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
149 {
150         out[0] = va[0]-vb[0];
151         out[1] = va[1]-vb[1];
152         out[2] = va[2]-vb[2];
153 }
154
155 void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
156 {
157         out[0] = va[0]+vb[0];
158         out[1] = va[1]+vb[1];
159         out[2] = va[2]+vb[2];
160 }
161
162 void _VectorCopy (vec3_t in, vec3_t out)
163 {
164         out[0] = in[0];
165         out[1] = in[1];
166         out[2] = in[2];
167 }
168
169 vec_t VectorNormalize( const vec3_t in, vec3_t out ) {
170
171 #if MATHLIB_VECTOR_NORMALIZE_PRECISION_FIX
172
173         // The sqrt() function takes double as an input and returns double as an
174         // output according the the man pages on Debian and on FreeBSD.  Therefore,
175         // I don't see a reason why using a double outright (instead of using the
176         // vec_accu_t alias for example) could possibly be frowned upon.
177
178         double  x, y, z, length;
179
180         x = (double) in[0];
181         y = (double) in[1];
182         z = (double) in[2];
183
184         length = sqrt((x * x) + (y * y) + (z * z));
185         if (length == 0)
186         {
187                 VectorClear (out);
188                 return 0;
189         }
190
191         out[0] = (vec_t) (x / length);
192         out[1] = (vec_t) (y / length);
193         out[2] = (vec_t) (z / length);
194
195         return (vec_t) length;
196
197 #else
198
199         vec_t   length, ilength;
200
201         length = (vec_t)sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]);
202         if (length == 0)
203         {
204                 VectorClear (out);
205                 return 0;
206         }
207
208         ilength = 1.0f/length;
209         out[0] = in[0]*ilength;
210         out[1] = in[1]*ilength;
211         out[2] = in[2]*ilength;
212
213         return length;
214
215 #endif
216
217 }
218
219 vec_t ColorNormalize( const vec3_t in, vec3_t out ) {
220         float   max, scale;
221
222         max = in[0];
223         if (in[1] > max)
224                 max = in[1];
225         if (in[2] > max)
226                 max = in[2];
227
228         if (max == 0) {
229                 out[0] = out[1] = out[2] = 1.0;
230                 return 0;
231         }
232
233         scale = 1.0f / max;
234
235         VectorScale (in, scale, out);
236
237         return max;
238 }
239
240 void VectorInverse (vec3_t v)
241 {
242         v[0] = -v[0];
243         v[1] = -v[1];
244         v[2] = -v[2];
245 }
246
247 /*
248 void VectorScale (vec3_t v, vec_t scale, vec3_t out)
249 {
250         out[0] = v[0] * scale;
251         out[1] = v[1] * scale;
252         out[2] = v[2] * scale;
253 }
254 */
255
256 void VectorRotate (vec3_t vIn, vec3_t vRotation, vec3_t out)
257 {
258   vec3_t vWork, va;
259   int nIndex[3][2];
260   int i;
261
262   VectorCopy(vIn, va);
263   VectorCopy(va, vWork);
264   nIndex[0][0] = 1; nIndex[0][1] = 2;
265   nIndex[1][0] = 2; nIndex[1][1] = 0;
266   nIndex[2][0] = 0; nIndex[2][1] = 1;
267
268   for (i = 0; i < 3; i++)
269   {
270     if (vRotation[i] != 0)
271     {
272       float dAngle = vRotation[i] * Q_PI / 180.0f;
273             float c = (vec_t)cos(dAngle);
274       float s = (vec_t)sin(dAngle);
275       vWork[nIndex[i][0]] = va[nIndex[i][0]] * c - va[nIndex[i][1]] * s;
276       vWork[nIndex[i][1]] = va[nIndex[i][0]] * s + va[nIndex[i][1]] * c;
277     }
278     VectorCopy(vWork, va);
279   }
280   VectorCopy(vWork, out);
281 }
282
283 void VectorRotateOrigin (vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out)
284 {
285   vec3_t vTemp, vTemp2;
286
287   VectorSubtract(vIn, vOrigin, vTemp);
288   VectorRotate(vTemp, vRotation, vTemp2);
289   VectorAdd(vTemp2, vOrigin, out);
290 }
291
292 void VectorPolar(vec3_t v, float radius, float theta, float phi)
293 {
294         v[0]=(float)(radius * cos(theta) * cos(phi));
295         v[1]=(float)(radius * sin(theta) * cos(phi));
296         v[2]=(float)(radius * sin(phi));
297 }
298
299 void VectorSnap(vec3_t v)
300 {
301   int i;
302   for (i = 0; i < 3; i++)
303   {
304     v[i] = (vec_t)FLOAT_TO_INTEGER(v[i]);
305   }
306 }
307
308 void VectorISnap(vec3_t point, int snap)
309 {
310   int i;
311         for (i = 0 ;i < 3 ; i++)
312         {
313                 point[i] = (vec_t)FLOAT_SNAP(point[i], snap);
314         }
315 }
316
317 void VectorFSnap(vec3_t point, float snap)
318 {
319   int i;
320         for (i = 0 ;i < 3 ; i++)
321         {
322                 point[i] = (vec_t)FLOAT_SNAP(point[i], snap);
323         }
324 }
325
326 void _Vector5Add (vec5_t va, vec5_t vb, vec5_t out)
327 {
328         out[0] = va[0]+vb[0];
329         out[1] = va[1]+vb[1];
330         out[2] = va[2]+vb[2];
331         out[3] = va[3]+vb[3];
332         out[4] = va[4]+vb[4];
333 }
334
335 void _Vector5Scale (vec5_t v, vec_t scale, vec5_t out)
336 {
337         out[0] = v[0] * scale;
338         out[1] = v[1] * scale;
339         out[2] = v[2] * scale;
340         out[3] = v[3] * scale;
341         out[4] = v[4] * scale;
342 }
343
344 void _Vector53Copy (vec5_t in, vec3_t out)
345 {
346         out[0] = in[0];
347         out[1] = in[1];
348         out[2] = in[2];
349 }
350
351 // NOTE: added these from Ritual's Q3Radiant
352 #define INVALID_BOUNDS 99999
353 void ClearBounds (vec3_t mins, vec3_t maxs)
354 {
355         mins[0] = mins[1] = mins[2] = +INVALID_BOUNDS;
356         maxs[0] = maxs[1] = maxs[2] = -INVALID_BOUNDS;
357 }
358
359 void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs)
360 {
361         int             i;
362         vec_t   val;
363
364         if(mins[0] == +INVALID_BOUNDS)
365         if(maxs[0] == -INVALID_BOUNDS)
366         {
367                 VectorCopy(v, mins);
368                 VectorCopy(v, maxs);
369         }
370
371         for (i=0 ; i<3 ; i++)
372         {
373                 val = v[i];
374                 if (val < mins[i])
375                         mins[i] = val;
376                 if (val > maxs[i])
377                         maxs[i] = val;
378         }
379 }
380
381 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
382 {
383         float           angle;
384         static float            sr, sp, sy, cr, cp, cy;
385         // static to help MS compiler fp bugs
386         
387         angle = angles[YAW] * (Q_PI*2.0f / 360.0f);
388         sy = (vec_t)sin(angle);
389         cy = (vec_t)cos(angle);
390         angle = angles[PITCH] * (Q_PI*2.0f / 360.0f);
391         sp = (vec_t)sin(angle);
392         cp = (vec_t)cos(angle);
393         angle = angles[ROLL] * (Q_PI*2.0f / 360.0f);
394         sr = (vec_t)sin(angle);
395         cr = (vec_t)cos(angle);
396         
397         if (forward)
398         {
399                 forward[0] = cp*cy;
400                 forward[1] = cp*sy;
401                 forward[2] = -sp;
402         }
403         if (right)
404         {
405                 right[0] = -sr*sp*cy+cr*sy;
406                 right[1] = -sr*sp*sy-cr*cy;
407                 right[2] = -sr*cp;
408         }
409         if (up)
410         {
411                 up[0] = cr*sp*cy+sr*sy;
412                 up[1] = cr*sp*sy-sr*cy;
413                 up[2] = cr*cp;
414         }
415 }
416
417 void VectorToAngles( vec3_t vec, vec3_t angles )
418 {
419         float forward;
420         float yaw, pitch;
421         
422         if ( ( vec[ 0 ] == 0 ) && ( vec[ 1 ] == 0 ) )
423         {
424                 yaw = 0;
425                 if ( vec[ 2 ] > 0 )
426                 {
427                         pitch = 90;
428                 }
429                 else
430                 {
431                         pitch = 270;
432                 }
433         }
434         else
435         {
436                 yaw = (vec_t)atan2( vec[ 1 ], vec[ 0 ] ) * 180 / Q_PI;
437                 if ( yaw < 0 )
438                 {
439                         yaw += 360;
440                 }
441                 
442                 forward = ( float )sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] );
443                 pitch = (vec_t)atan2( vec[ 2 ], forward ) * 180 / Q_PI;
444                 if ( pitch < 0 )
445                 {
446                         pitch += 360;
447                 }
448         }
449         
450         angles[ 0 ] = pitch;
451         angles[ 1 ] = yaw;
452         angles[ 2 ] = 0;
453 }
454
455 /*
456 =====================
457 PlaneFromPoints
458
459 Returns false if the triangle is degenrate.
460 The normal will point out of the clock for clockwise ordered points
461 =====================
462 */
463 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
464         vec3_t  d1, d2;
465
466         VectorSubtract( b, a, d1 );
467         VectorSubtract( c, a, d2 );
468         CrossProduct( d2, d1, plane );
469         if ( VectorNormalize( plane, plane ) == 0 ) {
470                 return qfalse;
471         }
472
473         plane[3] = DotProduct( a, plane );
474         return qtrue;
475 }
476
477 /*
478 ** NormalToLatLong
479 **
480 ** We use two byte encoded normals in some space critical applications.
481 ** Lat = 0 at (1,0,0) to 360 (-1,0,0), encoded in 8-bit sine table format
482 ** Lng = 0 at (0,0,1) to 180 (0,0,-1), encoded in 8-bit sine table format
483 **
484 */
485 void NormalToLatLong( const vec3_t normal, byte bytes[2] ) {
486         // check for singularities
487         if ( normal[0] == 0 && normal[1] == 0 ) {
488                 if ( normal[2] > 0 ) {
489                         bytes[0] = 0;
490                         bytes[1] = 0;           // lat = 0, long = 0
491                 } else {
492                         bytes[0] = 128;
493                         bytes[1] = 0;           // lat = 0, long = 128
494                 }
495         } else {
496                 int     a, b;
497
498                 a = (int)( RAD2DEG( atan2( normal[1], normal[0] ) ) * (255.0f / 360.0f ) );
499                 a &= 0xff;
500
501                 b = (int)( RAD2DEG( acos( normal[2] ) ) * ( 255.0f / 360.0f ) );
502                 b &= 0xff;
503
504                 bytes[0] = b;   // longitude
505                 bytes[1] = a;   // lattitude
506         }
507 }
508
509 /*
510 =================
511 PlaneTypeForNormal
512 =================
513 */
514 int     PlaneTypeForNormal (vec3_t normal) {
515         if (normal[0] == 1.0 || normal[0] == -1.0)
516                 return PLANE_X;
517         if (normal[1] == 1.0 || normal[1] == -1.0)
518                 return PLANE_Y;
519         if (normal[2] == 1.0 || normal[2] == -1.0)
520                 return PLANE_Z;
521         
522         return PLANE_NON_AXIAL;
523 }
524
525 /*
526 ================
527 MatrixMultiply
528 ================
529 */
530 void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
531         out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
532                                 in1[0][2] * in2[2][0];
533         out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
534                                 in1[0][2] * in2[2][1];
535         out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
536                                 in1[0][2] * in2[2][2];
537         out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
538                                 in1[1][2] * in2[2][0];
539         out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
540                                 in1[1][2] * in2[2][1];
541         out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
542                                 in1[1][2] * in2[2][2];
543         out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
544                                 in1[2][2] * in2[2][0];
545         out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
546                                 in1[2][2] * in2[2][1];
547         out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
548                                 in1[2][2] * in2[2][2];
549 }
550
551 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
552 {
553         float d;
554         vec3_t n;
555         float inv_denom;
556
557         inv_denom = 1.0F / DotProduct( normal, normal );
558
559         d = DotProduct( normal, p ) * inv_denom;
560
561         n[0] = normal[0] * inv_denom;
562         n[1] = normal[1] * inv_denom;
563         n[2] = normal[2] * inv_denom;
564
565         dst[0] = p[0] - d * n[0];
566         dst[1] = p[1] - d * n[1];
567         dst[2] = p[2] - d * n[2];
568 }
569
570 /*
571 ** assumes "src" is normalized
572 */
573 void PerpendicularVector( vec3_t dst, const vec3_t src )
574 {
575         int     pos;
576         int i;
577         vec_t minelem = 1.0F;
578         vec3_t tempvec;
579
580         /*
581         ** find the smallest magnitude axially aligned vector
582         */
583         for ( pos = 0, i = 0; i < 3; i++ )
584         {
585                 if ( fabs( src[i] ) < minelem )
586                 {
587                         pos = i;
588                         minelem = (vec_t)fabs( src[i] );
589                 }
590         }
591         tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
592         tempvec[pos] = 1.0F;
593
594         /*
595         ** project the point onto the plane defined by src
596         */
597         ProjectPointOnPlane( dst, tempvec, src );
598
599         /*
600         ** normalize the result
601         */
602         VectorNormalize( dst, dst );
603 }
604
605 /*
606 ===============
607 RotatePointAroundVector
608
609 This is not implemented very well...
610 ===============
611 */
612 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
613                                                          float degrees ) {
614         float   m[3][3];
615         float   im[3][3];
616         float   zrot[3][3];
617         float   tmpmat[3][3];
618         float   rot[3][3];
619         int     i;
620         vec3_t vr, vup, vf;
621         float   rad;
622
623         vf[0] = dir[0];
624         vf[1] = dir[1];
625         vf[2] = dir[2];
626
627         PerpendicularVector( vr, dir );
628         CrossProduct( vr, vf, vup );
629
630         m[0][0] = vr[0];
631         m[1][0] = vr[1];
632         m[2][0] = vr[2];
633
634         m[0][1] = vup[0];
635         m[1][1] = vup[1];
636         m[2][1] = vup[2];
637
638         m[0][2] = vf[0];
639         m[1][2] = vf[1];
640         m[2][2] = vf[2];
641
642         memcpy( im, m, sizeof( im ) );
643
644         im[0][1] = m[1][0];
645         im[0][2] = m[2][0];
646         im[1][0] = m[0][1];
647         im[1][2] = m[2][1];
648         im[2][0] = m[0][2];
649         im[2][1] = m[1][2];
650
651         memset( zrot, 0, sizeof( zrot ) );
652         zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
653
654         rad = (float)DEG2RAD( degrees );
655         zrot[0][0] = (vec_t)cos( rad );
656         zrot[0][1] = (vec_t)sin( rad );
657         zrot[1][0] = (vec_t)-sin( rad );
658         zrot[1][1] = (vec_t)cos( rad );
659
660         MatrixMultiply( m, zrot, tmpmat );
661         MatrixMultiply( tmpmat, im, rot );
662
663         for ( i = 0; i < 3; i++ ) {
664                 dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
665         }
666 }
667
668
669 ////////////////////////////////////////////////////////////////////////////////
670 // Below is double-precision math stuff.  This was initially needed by the new
671 // "base winding" code in q3map2 brush processing in order to fix the famous
672 // "disappearing triangles" issue.  These definitions can be used wherever extra
673 // precision is needed.
674 ////////////////////////////////////////////////////////////////////////////////
675
676 /*
677 =================
678 VectorLengthAccu
679 =================
680 */
681 vec_accu_t VectorLengthAccu(const vec3_accu_t v)
682 {
683         return (vec_accu_t) sqrt((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]));
684 }
685
686 /*
687 =================
688 DotProductAccu
689 =================
690 */
691 vec_accu_t DotProductAccu(const vec3_accu_t a, const vec3_accu_t b)
692 {
693         return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]);
694 }
695
696 /*
697 =================
698 VectorSubtractAccu
699 =================
700 */
701 void VectorSubtractAccu(const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out)
702 {
703         out[0] = a[0] - b[0];
704         out[1] = a[1] - b[1];
705         out[2] = a[2] - b[2];
706 }
707
708 /*
709 =================
710 VectorAddAccu
711 =================
712 */
713 void VectorAddAccu(const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out)
714 {
715         out[0] = a[0] + b[0];
716         out[1] = a[1] + b[1];
717         out[2] = a[2] + b[2];
718 }
719
720 /*
721 =================
722 VectorCopyAccu
723 =================
724 */
725 void VectorCopyAccu(const vec3_accu_t in, vec3_accu_t out)
726 {
727         out[0] = in[0];
728         out[1] = in[1];
729         out[2] = in[2];
730 }
731
732 /*
733 =================
734 VectorScaleAccu
735 =================
736 */
737 void VectorScaleAccu(const vec3_accu_t in, vec_accu_t scaleFactor, vec3_accu_t out)
738 {
739         out[0] = in[0] * scaleFactor;
740         out[1] = in[1] * scaleFactor;
741         out[2] = in[2] * scaleFactor;
742 }
743
744 /*
745 =================
746 CrossProductAccu
747 =================
748 */
749 void CrossProductAccu(const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out)
750 {
751         out[0] = (a[1] * b[2]) - (a[2] * b[1]);
752         out[1] = (a[2] * b[0]) - (a[0] * b[2]);
753         out[2] = (a[0] * b[1]) - (a[1] * b[0]);
754 }
755
756 /*
757 =================
758 Q_rintAccu
759 =================
760 */
761 vec_accu_t Q_rintAccu(vec_accu_t val)
762 {
763         return (vec_accu_t) floor(val + 0.5);
764 }
765
766 /*
767 =================
768 VectorCopyAccuToRegular
769 =================
770 */
771 void VectorCopyAccuToRegular(const vec3_accu_t in, vec3_t out)
772 {
773         out[0] = (vec_t) in[0];
774         out[1] = (vec_t) in[1];
775         out[2] = (vec_t) in[2];
776 }
777
778 /*
779 =================
780 VectorCopyRegularToAccu
781 =================
782 */
783 void VectorCopyRegularToAccu(const vec3_t in, vec3_accu_t out)
784 {
785         out[0] = (vec_accu_t) in[0];
786         out[1] = (vec_accu_t) in[1];
787         out[2] = (vec_accu_t) in[2];
788 }
789
790 /*
791 =================
792 VectorNormalizeAccu
793 =================
794 */
795 vec_accu_t VectorNormalizeAccu(const vec3_accu_t in, vec3_accu_t out)
796 {
797         // The sqrt() function takes double as an input and returns double as an
798         // output according the the man pages on Debian and on FreeBSD.  Therefore,
799         // I don't see a reason why using a double outright (instead of using the
800         // vec_accu_t alias for example) could possibly be frowned upon.
801
802         vec_accu_t      length;
803
804         length = (vec_accu_t) sqrt((in[0] * in[0]) + (in[1] * in[1]) + (in[2] * in[2]));
805         if (length == 0)
806         {
807                 VectorClear(out);
808                 return 0;
809         }
810
811         out[0] = in[0] / length;
812         out[1] = in[1] / length;
813         out[2] = in[2] / length;
814
815         return length;
816 }
817
818