Debian Codegraph @

libs/mathlib/mathlib.c

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