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