]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/mathlib.h
Merge commit '0c0ed6c1a75ce05a91209fdf3ad8378a7b7e7f6a' into master-merge
[xonotic/netradiant.git] / libs / mathlib.h
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 #ifndef __MATHLIB__
23 #define __MATHLIB__
24
25 // mathlib.h
26 #include <math.h>
27 #include <float.h>
28
29 #ifdef __cplusplus
30
31 // start declarations of functions defined in C library.
32 extern "C"
33 {
34
35 #endif
36
37 #include "bytebool.h"
38
39 typedef float vec_t;
40 typedef vec_t vec3_t[3];
41 typedef vec_t vec5_t[5];
42 typedef vec_t vec4_t[4];
43
44 // Smallest positive value for vec_t such that 1.0 + VEC_SMALLEST_EPSILON_AROUND_ONE != 1.0.
45 // In the case of 32 bit floats (which is almost certainly the case), it's 0.00000011921.
46 // Don't forget that your epsilons should depend on the possible range of values,
47 // because for example adding VEC_SMALLEST_EPSILON_AROUND_ONE to 1024.0 will have no effect.
48 #define VEC_SMALLEST_EPSILON_AROUND_ONE FLT_EPSILON
49
50 #define SIDE_FRONT      0
51 #define SIDE_ON         2
52 #define SIDE_BACK       1
53 #define SIDE_CROSS      -2
54
55 // plane types are used to speed some tests
56 // 0-2 are axial planes
57 #define PLANE_X         0
58 #define PLANE_Y         1
59 #define PLANE_Z         2
60 #define PLANE_NON_AXIAL 3
61
62 #define Q_PI    3.14159265358979323846f
63
64 extern const vec3_t vec3_origin;
65
66 extern const vec3_t g_vec3_axis_x;
67 extern const vec3_t g_vec3_axis_y;
68 extern const vec3_t g_vec3_axis_z;
69
70 #define EQUAL_EPSILON   0.001
71
72 #define DotProduct( x,y ) ( ( x )[0] * ( y )[0] + ( x )[1] * ( y )[1] + ( x )[2] * ( y )[2] )
73 #define VectorSubtract( a,b,c ) ( ( c )[0] = ( a )[0] - ( b )[0],( c )[1] = ( a )[1] - ( b )[1],( c )[2] = ( a )[2] - ( b )[2] )
74 #define VectorAdd( a,b,c ) ( ( c )[0] = ( a )[0] + ( b )[0],( c )[1] = ( a )[1] + ( b )[1],( c )[2] = ( a )[2] + ( b )[2] )
75 #define VectorIncrement( a,b ) ( ( b )[0] += ( a )[0],( b )[1] += ( a )[1],( b )[2] += ( a )[2] )
76 #define VectorCopy( a,b ) ( ( b )[0] = ( a )[0],( b )[1] = ( a )[1],( b )[2] = ( a )[2] )
77 #define VectorSet( v, a, b, c ) ( ( v )[0] = ( a ),( v )[1] = ( b ),( v )[2] = ( c ) )
78 #define VectorScale( a,b,c ) ( ( c )[0] = ( b ) * ( a )[0],( c )[1] = ( b ) * ( a )[1],( c )[2] = ( b ) * ( a )[2] )
79 #define VectorMid( a,b,c ) ( ( c )[0] = ( ( a )[0] + ( b )[0] ) * 0.5f,( c )[1] = ( ( a )[1] + ( b )[1] ) * 0.5f,( c )[2] = ( ( a )[2] + ( b )[2] ) * 0.5f )
80 #define VectorNegate( a,b ) ( ( b )[0] = -( a )[0],( b )[1] = -( a )[1],( b )[2] = -( a )[2] )
81 #define CrossProduct( a,b,c ) ( ( c )[0] = ( a )[1] * ( b )[2] - ( a )[2] * ( b )[1],( c )[1] = ( a )[2] * ( b )[0] - ( a )[0] * ( b )[2],( c )[2] = ( a )[0] * ( b )[1] - ( a )[1] * ( b )[0] )
82 #define VectorClear( x ) ( ( x )[0] = ( x )[1] = ( x )[2] = 0 )
83
84 #define FLOAT_SNAP( f,snap ) ( (float)( floor( ( f ) / ( snap ) + 0.5 ) * ( snap ) ) )
85 #define FLOAT_TO_INTEGER( f ) ( (float)( floor( ( f ) + 0.5 ) ) )
86
87 #define RGBTOGRAY( x ) ( (float)( ( x )[0] ) * 0.2989f + (float)( ( x )[1] ) * 0.5870f + (float)( ( x )[2] ) * 0.1140f )
88
89 #define Q_rint( in ) ( (vec_t)floor( in + 0.5 ) )
90
91 qboolean VectorCompare( const vec3_t v1, const vec3_t v2 );
92
93 qboolean VectorIsOnAxis( vec3_t v );
94 qboolean VectorIsOnAxialPlane( vec3_t v );
95
96 vec_t VectorLength( const vec3_t v );
97
98 void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc );
99
100 void _CrossProduct( vec3_t v1, vec3_t v2, vec3_t cross );
101 // I need this define in order to test some of the regression tests from time to time.
102 // This define affect the precision of VectorNormalize() function only.
103 #define MATHLIB_VECTOR_NORMALIZE_PRECISION_FIX 1
104 vec_t VectorAccurateNormalize( const vec3_t in, vec3_t out );
105 vec_t VectorFastNormalize( const vec3_t in, vec3_t out );
106 #if MATHLIB_VECTOR_NORMALIZE_PRECISION_FIX
107 #define VectorNormalize VectorAccurateNormalize
108 #else
109 #define VectorNormalize VectorFastNormalize
110 #endif
111 vec_t ColorNormalize( const vec3_t in, vec3_t out );
112 void VectorInverse( vec3_t v );
113 void VectorPolar( vec3_t v, float radius, float theta, float phi );
114
115 // default snapping, to 1
116 void VectorSnap( vec3_t v );
117
118 // integer snapping
119 void VectorISnap( vec3_t point, int snap );
120
121 // Gef:   added snap to float for sub-integer grid sizes
122 // TTimo: we still use the int version of VectorSnap when possible
123 //        to avoid potential rounding issues
124 // TTimo: renaming to VectorFSnap for C implementation
125 void VectorFSnap( vec3_t point, float snap );
126
127 // NOTE: added these from Ritual's Q3Radiant
128 void ClearBounds( vec3_t mins, vec3_t maxs );
129 void AddPointToBounds( vec3_t v, vec3_t mins, vec3_t maxs );
130
131
132 #define PITCH               0       // up / down
133 #define YAW                 1       // left / right
134 #define ROLL                2       // fall over
135
136 void AngleVectors( vec3_t angles, vec3_t forward, vec3_t right, vec3_t up );
137 void VectorToAngles( vec3_t vec, vec3_t angles );
138
139 #define ZERO_EPSILON 1.0E-6
140 #define RAD2DEGMULT 57.29577951308232f
141 #define DEG2RADMULT 0.01745329251994329f
142 #define RAD2DEG( a ) ( ( a ) * RAD2DEGMULT )
143 #define DEG2RAD( a ) ( ( a ) * DEG2RADMULT )
144
145 void VectorRotate( vec3_t vIn, vec3_t vRotation, vec3_t out );
146 void VectorRotateOrigin( vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out );
147
148 // some function merged from tools mathlib code
149
150 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c );
151 void NormalToLatLong( const vec3_t normal, byte bytes[2] );
152 int PlaneTypeForNormal( vec3_t normal );
153 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
154
155
156 /*!
157    \todo
158    FIXME test calls such as intersect tests should be named test_
159  */
160
161 typedef vec_t m3x3_t[9];
162 /*!NOTE
163    m4x4 looks like this..
164
165                 x  y  z
166    x axis        ( 0  1  2)
167    y axis        ( 4  5  6)
168    z axis        ( 8  9 10)
169    translation   (12 13 14)
170    scale         ( 0  5 10)
171  */
172 typedef vec_t m4x4_t[16];
173
174 #define M4X4_INDEX( m,row,col ) ( m[( col << 2 ) + row] )
175
176 typedef enum { eXYZ, eYZX, eZXY, eXZY, eYXZ, eZYX } eulerOrder_t;
177
178 #define CLIP_PASS 0x00 // 000000
179 #define CLIP_LT_X 0x01 // 000001
180 #define CLIP_GT_X 0x02 // 000010
181 #define CLIP_LT_Y 0x04 // 000100
182 #define CLIP_GT_Y 0x08 // 001000
183 #define CLIP_LT_Z 0x10 // 010000
184 #define CLIP_GT_Z 0x20 // 100000
185 #define CLIP_FAIL 0x3F // 111111
186 typedef unsigned char clipmask_t;
187
188 extern const m4x4_t g_m4x4_identity;
189
190 #define M4X4_COPY( dst,src ) ( \
191                 ( dst )[0] = ( src )[0], \
192                 ( dst )[1] = ( src )[1], \
193                 ( dst )[2] = ( src )[2], \
194                 ( dst )[3] = ( src )[3], \
195                 ( dst )[4] = ( src )[4], \
196                 ( dst )[5] = ( src )[5], \
197                 ( dst )[6] = ( src )[6], \
198                 ( dst )[7] = ( src )[7], \
199                 ( dst )[8] = ( src )[8], \
200                 ( dst )[9] = ( src )[9], \
201                 ( dst )[10] = ( src )[10], \
202                 ( dst )[11] = ( src )[11], \
203                 ( dst )[12] = ( src )[12], \
204                 ( dst )[13] = ( src )[13], \
205                 ( dst )[14] = ( src )[14], \
206                 ( dst )[15] = ( src )[15] )
207
208 typedef enum
209 {
210         eRightHanded = 0,
211         eLeftHanded = 1,
212 }
213 m4x4Handedness_t;
214
215 m4x4Handedness_t m4x4_handedness( const m4x4_t matrix );
216
217 /*! assign other m4x4 to this m4x4 */
218 void m4x4_assign( m4x4_t matrix, const m4x4_t other );
219
220 // constructors
221 /*! create m4x4 as identity matrix */
222 void m4x4_identity( m4x4_t matrix );
223 /*! create m4x4 as a translation matrix, for a translation vec3 */
224 void m4x4_translation_for_vec3( m4x4_t matrix, const vec3_t translation );
225 /*! create m4x4 as a rotation matrix, for an euler angles (degrees) vec3 */
226 void m4x4_rotation_for_vec3( m4x4_t matrix, const vec3_t euler, eulerOrder_t order );
227 /*! create m4x4 as a scaling matrix, for a scale vec3 */
228 void m4x4_scale_for_vec3( m4x4_t matrix, const vec3_t scale );
229 /*! create m4x4 as a rotation matrix, for a quaternion vec4 */
230 void m4x4_rotation_for_quat( m4x4_t matrix, const vec4_t rotation );
231 /*! create m4x4 as a rotation matrix, for an axis vec3 and an angle (radians) */
232 void m4x4_rotation_for_axisangle( m4x4_t matrix, const vec3_t axis, double angle );
233 /*! generate a perspective matrix by specifying the view frustum */
234 void m4x4_frustum( m4x4_t matrix, vec_t left, vec_t right, vec_t bottom, vec_t top, vec_t nearval, vec_t farval );
235
236 // a valid m4x4 to access is always first argument
237 /*! extract translation vec3 from matrix */
238 void m4x4_get_translation_vec3( const m4x4_t matrix, vec3_t translation );
239 /*! extract euler rotation angles from a rotation-only matrix */
240 void m4x4_get_rotation_vec3( const m4x4_t matrix, vec3_t euler, eulerOrder_t order );
241 /*! extract scale vec3 from matrix */
242 void m4x4_get_scale_vec3( const m4x4_t matrix, vec3_t scale );
243 /*! extract translation/euler/scale from an orthogonal matrix. NOTE: requires right-handed axis-base */
244 void m4x4_get_transform_vec3( const m4x4_t matrix, vec3_t translation, vec3_t euler, eulerOrder_t order, vec3_t scale );
245
246 // a valid m4x4 to be modified is always first argument
247 /*! translate m4x4 by a translation vec3 */
248 void m4x4_translate_by_vec3( m4x4_t matrix, const vec3_t translation );
249 /*! rotate m4x4 by a euler (degrees) vec3 */
250 void m4x4_rotate_by_vec3( m4x4_t matrix, const vec3_t euler, eulerOrder_t order );
251 /*! scale m4x4 by a scaling vec3 */
252 void m4x4_scale_by_vec3( m4x4_t matrix, const vec3_t scale );
253 /*! rotate m4x4 by a quaternion vec4 */
254 void m4x4_rotate_by_quat( m4x4_t matrix, const vec4_t rotation );
255 /*! rotate m4x4 by an axis vec3 and an angle (radians) */
256 void m4x4_rotate_by_axisangle( m4x4_t matrix, const vec3_t axis, double angle );
257 /*! transform m4x4 by translation/eulerZYX/scaling vec3 (transform = scale * eulerZ * eulerY * eulerX * translation) */
258 void m4x4_transform_by_vec3( m4x4_t matrix, const vec3_t translation, const vec3_t euler, eulerOrder_t order, const vec3_t scale );
259 /*! rotate m4x4 around a pivot point by eulerZYX vec3 */
260 void m4x4_pivoted_rotate_by_vec3( m4x4_t matrix, const vec3_t euler, eulerOrder_t order, const vec3_t pivotpoint );
261 /*! scale m4x4 around a pivot point by scaling vec3 */
262 void m4x4_pivoted_scale_by_vec3( m4x4_t matrix, const vec3_t scale, const vec3_t pivotpoint );
263 /*! transform m4x4 around a pivot point by translation/eulerZYX/scaling vec3 */
264 void m4x4_pivoted_transform_by_vec3( m4x4_t matrix, const vec3_t translation, const vec3_t euler, eulerOrder_t order, const vec3_t scale, const vec3_t pivotpoint );
265 /*! transform m4x4 around a pivot point by translation/rotation/scaling vec3 */
266 void m4x4_pivoted_transform_by_rotation( m4x4_t matrix, const vec3_t translation, const m4x4_t rotation, const vec3_t scale, const vec3_t pivotpoint );
267 /*! rotate m4x4 around a pivot point by quaternion vec4 */
268 void m4x4_pivoted_rotate_by_quat( m4x4_t matrix, const vec4_t quat, const vec3_t pivotpoint );
269 /*! rotate m4x4 around a pivot point by axis vec3 and angle (radians) */
270 void m4x4_pivoted_rotate_by_axisangle( m4x4_t matrix, const vec3_t axis, double angle, const vec3_t pivotpoint );
271
272 /*! postmultiply m4x4 by another m4x4 */
273 void m4x4_multiply_by_m4x4( m4x4_t matrix, const m4x4_t matrix_src );
274 /*! premultiply m4x4 by another m4x4 */
275 void m4x4_premultiply_by_m4x4( m4x4_t matrix, const m4x4_t matrix_src );
276 /*! postmultiply orthogonal m4x4 by another orthogonal m4x4 */
277 void m4x4_orthogonal_multiply_by_m4x4( m4x4_t matrix, const m4x4_t matrix_src );
278 /*! premultiply orthogonal m4x4 by another orthogonal m4x4 */
279 void m4x4_orthogonal_premultiply_by_m4x4( m4x4_t matrix, const m4x4_t matrix_src );
280
281 /*! multiply a point (x,y,z,1) by matrix */
282 void m4x4_transform_point( const m4x4_t matrix, vec3_t point );
283 /*! multiply a normal (x,y,z,0) by matrix */
284 void m4x4_transform_normal( const m4x4_t matrix, vec3_t normal );
285 /*! multiply a vec4 (x,y,z,w) by matrix */
286 void m4x4_transform_vec4( const m4x4_t matrix, vec4_t vector );
287
288 /*! multiply a point (x,y,z,1) by matrix */
289 void m4x4_transform_point( const m4x4_t matrix, vec3_t point );
290 /*! multiply a normal (x,y,z,0) by matrix */
291 void m4x4_transform_normal( const m4x4_t matrix, vec3_t normal );
292
293 /*! transpose a m4x4 */
294 void m4x4_transpose( m4x4_t matrix );
295 /*! invert an orthogonal 4x3 subset of a 4x4 matrix */
296 int m4x4_orthogonal_invert( m4x4_t matrix );
297 /*! m4_det */
298 float m4_det( m4x4_t mr );
299 /*! invert any m4x4 using Kramer's rule.. return 1 if matrix is singular, else return 0 */
300 int m4x4_invert( m4x4_t matrix );
301
302 /*! clip a point (x,y,z,1) by canonical matrix */
303 clipmask_t m4x4_clip_point( const m4x4_t matrix, const vec3_t point, vec4_t clipped );
304 /*! device-space polygon for clipped triangle */
305 unsigned int m4x4_clip_triangle( const m4x4_t matrix, const vec3_t p0, const vec3_t p1, const vec3_t p2, vec4_t clipped[9] );
306 /*! device-space line for clipped line  */
307 unsigned int m4x4_clip_line( const m4x4_t matrix, const vec3_t p0, const vec3_t p1, vec4_t clipped[2] );
308
309
310 //! quaternion identity
311 void quat_identity( vec4_t quat );
312 //! quaternion from two unit vectors
313 void quat_for_unit_vectors( vec4_t quat, const vec3_t from, const vec3_t to );
314 //! quaternion from axis and angle (radians)
315 void quat_for_axisangle( vec4_t quat, const vec3_t axis, double angle );
316 //! concatenates two rotations.. equivalent to m4x4_multiply_by_m4x4 .. postmultiply.. the right-hand side is the first rotation performed
317 void quat_multiply_by_quat( vec4_t quat, const vec4_t other );
318 //! negate a quaternion
319 void quat_conjugate( vec4_t quat );
320 //! normalise a quaternion
321 void quat_normalise( vec4_t quat );
322
323
324
325 /*!
326    \todo object/ray intersection functions should maybe return a point rather than a distance?
327  */
328
329 /*!
330    aabb_t -  "axis-aligned" bounding box...
331    origin: centre of bounding box...
332    extents: +/- extents of box from origin...
333  */
334 typedef struct aabb_s
335 {
336         vec3_t origin;
337         vec3_t extents;
338 } aabb_t;
339
340 extern const aabb_t g_aabb_null;
341
342 /*!
343    bbox_t - oriented bounding box...
344    aabb: axis-aligned bounding box...
345    axes: orientation axes...
346  */
347 typedef struct bbox_s
348 {
349         aabb_t aabb;
350         vec3_t axes[3];
351         vec_t radius;
352 } bbox_t;
353
354 /*!
355    ray_t - origin point and direction unit-vector
356  */
357 typedef struct ray_s
358 {
359         vec3_t origin;
360         vec3_t direction;
361 } ray_t;
362
363 /*!
364    line_t - centre point and displacement of end point from centre
365  */
366 typedef struct line_s
367 {
368         vec3_t origin;
369         vec3_t extents;
370 } line_t;
371
372
373 /*! Generate line from start/end points. */
374 void line_construct_for_vec3( line_t* line, const vec3_t start, const vec3_t end );
375 /*! Return 2 if line is behind plane, else return 1 if line intersects plane, else return 0. */
376 int line_test_plane( const line_t* line, const vec4_t plane );
377
378 /*! Generate AABB from min/max. */
379 void aabb_construct_for_vec3( aabb_t* aabb, const vec3_t min, const vec3_t max );
380 /*! Initialise AABB to negative size. */
381 void aabb_clear( aabb_t* aabb );
382
383 /*! Extend AABB to include point. */
384 void aabb_extend_by_point( aabb_t* aabb, const vec3_t point );
385 /*! Extend AABB to include aabb_src. */
386 void aabb_extend_by_aabb( aabb_t* aabb, const aabb_t* aabb_src );
387 /*! Extend AABB by +/- extension vector. */
388 void aabb_extend_by_vec3( aabb_t* aabb, vec3_t extension );
389
390 /*! Return 2 if point is inside, else 1 if point is on surface, else 0. */
391 int aabb_test_point( const aabb_t* aabb, const vec3_t point );
392 /*! Return 2 if aabb_src intersects, else 1 if aabb_src touches exactly, else 0. */
393 int aabb_test_aabb( const aabb_t* aabb, const aabb_t* aabb_src );
394 /*! Return 2 if aabb is behind plane, else 1 if aabb intersects plane, else 0. */
395 int aabb_test_plane( const aabb_t* aabb, const float* plane );
396 /*! Return 1 if aabb intersects ray, else 0... dist = closest intersection. */
397 int aabb_intersect_ray( const aabb_t* aabb, const ray_t* ray, vec3_t intersection );
398 /*! Return 1 if aabb intersects ray, else 0. Faster, but does not provide point of intersection */
399 int aabb_test_ray( const aabb_t* aabb, const ray_t* ray );
400
401 /*! Return 2 if oriented aabb is behind plane, else 1 if aabb intersects plane, else 0. */
402 int aabb_oriented_intersect_plane( const aabb_t* aabb, const m4x4_t transform, const vec_t* plane );
403
404 /*! Calculate the corners of the aabb. */
405 void aabb_corners( const aabb_t * aabb, vec3_t corners[8] );
406
407 /*! (deprecated) Generate AABB from oriented bounding box. */
408 void aabb_for_bbox( aabb_t* aabb, const bbox_t* bbox );
409 /*! (deprecated) Generate AABB from 2-dimensions of min/max, specified by axis. */
410 void aabb_for_area( aabb_t* aabb, vec3_t area_tl, vec3_t area_br, int axis );
411 /*! Generate AABB to contain src*  transform. NOTE: transform must be orthogonal */
412 void aabb_for_transformed_aabb( aabb_t* dst, const aabb_t* src, const m4x4_t transform );
413
414 /*! Update bounding-sphere radius. */
415 void bbox_update_radius( bbox_t* bbox );
416 /*! Generate oriented bounding box from AABB and transformation matrix. */
417 /*!\todo Remove need to specify eulerZYX/scale. */
418 void bbox_for_oriented_aabb( bbox_t* bbox, const aabb_t* aabb,
419                                                          const m4x4_t matrix, const vec3_t eulerZYX, const vec3_t scale );
420 /*! Return 2 if bbox is behind plane, else return 1 if bbox intersects plane, else return 0. */
421 int bbox_intersect_plane( const bbox_t* bbox, const vec_t* plane );
422
423
424 /*! Generate a ray from an origin point and a direction unit-vector */
425 void ray_construct_for_vec3( ray_t* ray, const vec3_t origin, const vec3_t direction );
426
427 /*! Transform a ray */
428 void ray_transform( ray_t* ray, const m4x4_t matrix );
429
430 /*! distance from ray origin in ray direction to point. FLT_MAX if no intersection. */
431 vec_t ray_intersect_point( const ray_t* ray, const vec3_t point, vec_t epsilon, vec_t divergence );
432 /*! distance from ray origin in ray direction to triangle. FLT_MAX if no intersection. */
433 vec_t ray_intersect_triangle( const ray_t* ray, qboolean bCullBack, const vec3_t vert0, const vec3_t vert1, const vec3_t vert2 );
434 /*! distance from ray origin in ray direction to plane. */
435 vec_t ray_intersect_plane( const ray_t* ray, const vec3_t normal, vec_t dist );
436
437
438 int plane_intersect_planes( const vec4_t plane1, const vec4_t plane2, const vec4_t plane3, vec3_t intersection );
439
440
441
442 ////////////////////////////////////////////////////////////////////////////////
443 // Below is double-precision math stuff.  This was initially needed by the new
444 // "base winding" code in q3map2 brush processing in order to fix the famous
445 // "disappearing triangles" issue.  These definitions can be used wherever extra
446 // precision is needed.
447 ////////////////////////////////////////////////////////////////////////////////
448
449 typedef double vec_accu_t;
450 typedef vec_accu_t vec3_accu_t[3];
451
452 // Smallest positive value for vec_accu_t such that 1.0 + VEC_ACCU_SMALLEST_EPSILON_AROUND_ONE != 1.0.
453 // In the case of 64 bit doubles (which is almost certainly the case), it's 0.00000000000000022204.
454 // Don't forget that your epsilons should depend on the possible range of values,
455 // because for example adding VEC_ACCU_SMALLEST_EPSILON_AROUND_ONE to 1024.0 will have no effect.
456 #define VEC_ACCU_SMALLEST_EPSILON_AROUND_ONE DBL_EPSILON
457
458 vec_accu_t VectorLengthAccu( const vec3_accu_t v );
459
460 // I have a feeling it may be safer to break these #define functions out into actual functions
461 // in order to avoid accidental loss of precision.  For example, say you call
462 // VectorScaleAccu(vec3_t, vec_t, vec3_accu_t).  The scale would take place in 32 bit land
463 // and the result would be cast to 64 bit, which would cause total loss of precision when
464 // scaling by a large factor.
465 //#define DotProductAccu(x, y) ((x)[0] * (y)[0] + (x)[1] * (y)[1] + (x)[2] * (y)[2])
466 //#define VectorSubtractAccu(a, b, c) ((c)[0] = (a)[0] - (b)[0], (c)[1] = (a)[1] - (b)[1], (c)[2] = (a)[2] - (b)[2])
467 //#define VectorAddAccu(a, b, c) ((c)[0] = (a)[0] + (b)[0], (c)[1] = (a)[1] + (b)[1], (c)[2] = (a)[2] + (b)[2])
468 //#define VectorCopyAccu(a, b) ((b)[0] = (a)[0], (b)[1] = (a)[1], (b)[2] = (a)[2])
469 //#define VectorScaleAccu(a, b, c) ((c)[0] = (b) * (a)[0], (c)[1] = (b) * (a)[1], (c)[2] = (b) * (a)[2])
470 //#define CrossProductAccu(a, b, c) ((c)[0] = (a)[1] * (b)[2] - (a)[2] * (b)[1], (c)[1] = (a)[2] * (b)[0] - (a)[0] * (b)[2], (c)[2] = (a)[0] * (b)[1] - (a)[1] * (b)[0])
471 //#define Q_rintAccu(in) ((vec_accu_t) floor(in + 0.5))
472
473 vec_accu_t DotProductAccu( const vec3_accu_t a, const vec3_accu_t b );
474 void VectorSubtractAccu( const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out );
475 void VectorAddAccu( const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out );
476 void VectorCopyAccu( const vec3_accu_t in, vec3_accu_t out );
477 void VectorScaleAccu( const vec3_accu_t in, vec_accu_t scaleFactor, vec3_accu_t out );
478 void CrossProductAccu( const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out );
479 vec_accu_t Q_rintAccu( vec_accu_t val );
480
481 void VectorCopyAccuToRegular( const vec3_accu_t in, vec3_t out );
482 void VectorCopyRegularToAccu( const vec3_t in, vec3_accu_t out );
483 vec_accu_t VectorNormalizeAccu( const vec3_accu_t in, vec3_accu_t out );
484
485 #ifdef __cplusplus
486 }
487 #endif
488
489 #endif /* __MATHLIB__ */