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