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