2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
31 // start declarations of functions defined in C library.
40 typedef vec_t vec3_t[3];
41 typedef vec_t vec5_t[5];
42 typedef vec_t vec4_t[4];
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
55 // plane types are used to speed some tests
56 // 0-2 are axial planes
60 #define PLANE_NON_AXIAL 3
62 #define Q_PI 3.14159265358979323846f
64 extern const vec3_t vec3_origin;
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;
70 #define EQUAL_EPSILON 0.001
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)
84 #define FLOAT_SNAP(f,snap) ( (float)( floor( (f) / (snap) + 0.5 ) * (snap) ) )
85 #define FLOAT_TO_INTEGER(f) ( (float)( floor( (f) + 0.5 ) ) )
87 #define RGBTOGRAY(x) ( (float)((x)[0]) * 0.2989f + (float)((x)[1]) * 0.5870f + (float)((x)[2]) * 0.1140f )
89 #define Q_rint(in) ((vec_t)floor(in+0.5))
91 qboolean VectorCompare (const vec3_t v1, const vec3_t v2);
93 qboolean VectorIsOnAxis(vec3_t v);
94 qboolean VectorIsOnAxialPlane(vec3_t v);
96 vec_t VectorLength(const vec3_t v);
98 void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc );
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 VectorNormalize (const vec3_t in, vec3_t out);
105 vec_t ColorNormalize( const vec3_t in, vec3_t out );
106 void VectorInverse (vec3_t v);
107 void VectorPolar(vec3_t v, float radius, float theta, float phi);
109 // default snapping, to 1
110 void VectorSnap(vec3_t v);
113 void VectorISnap(vec3_t point, int snap);
115 // Gef: added snap to float for sub-integer grid sizes
116 // TTimo: we still use the int version of VectorSnap when possible
117 // to avoid potential rounding issues
118 // TTimo: renaming to VectorFSnap for C implementation
119 void VectorFSnap(vec3_t point, float snap);
121 // NOTE: added these from Ritual's Q3Radiant
122 void ClearBounds (vec3_t mins, vec3_t maxs);
123 void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs);
126 #define PITCH 0 // up / down
127 #define YAW 1 // left / right
128 #define ROLL 2 // fall over
130 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up);
131 void VectorToAngles( vec3_t vec, vec3_t angles );
133 #define ZERO_EPSILON 1.0E-6
134 #define RAD2DEGMULT 57.29577951308232f
135 #define DEG2RADMULT 0.01745329251994329f
136 #define RAD2DEG( a ) ( (a) * RAD2DEGMULT )
137 #define DEG2RAD( a ) ( (a) * DEG2RADMULT )
139 void VectorRotate (vec3_t vIn, vec3_t vRotation, vec3_t out);
140 void VectorRotateOrigin (vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out);
142 // some function merged from tools mathlib code
144 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c );
145 void NormalToLatLong( const vec3_t normal, byte bytes[2] );
146 int PlaneTypeForNormal (vec3_t normal);
147 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
152 FIXME test calls such as intersect tests should be named test_
155 typedef vec_t m3x3_t[9];
157 m4x4 looks like this..
163 translation (12 13 14)
166 typedef vec_t m4x4_t[16];
168 #define M4X4_INDEX(m,row,col) (m[(col<<2)+row])
170 typedef enum { eXYZ, eYZX, eZXY, eXZY, eYXZ, eZYX } eulerOrder_t;
172 #define CLIP_PASS 0x00 // 000000
173 #define CLIP_LT_X 0x01 // 000001
174 #define CLIP_GT_X 0x02 // 000010
175 #define CLIP_LT_Y 0x04 // 000100
176 #define CLIP_GT_Y 0x08 // 001000
177 #define CLIP_LT_Z 0x10 // 010000
178 #define CLIP_GT_Z 0x20 // 100000
179 #define CLIP_FAIL 0x3F // 111111
180 typedef unsigned char clipmask_t;
182 extern const m4x4_t g_m4x4_identity;
184 #define M4X4_COPY(dst,src) (\
195 (dst)[10]=(src)[10],\
196 (dst)[11]=(src)[11],\
197 (dst)[12]=(src)[12],\
198 (dst)[13]=(src)[13],\
199 (dst)[14]=(src)[14],\
209 m4x4Handedness_t m4x4_handedness(const m4x4_t matrix);
211 /*! assign other m4x4 to this m4x4 */
212 void m4x4_assign(m4x4_t matrix, const m4x4_t other);
215 /*! create m4x4 as identity matrix */
216 void m4x4_identity(m4x4_t matrix);
217 /*! create m4x4 as a translation matrix, for a translation vec3 */
218 void m4x4_translation_for_vec3(m4x4_t matrix, const vec3_t translation);
219 /*! create m4x4 as a rotation matrix, for an euler angles (degrees) vec3 */
220 void m4x4_rotation_for_vec3(m4x4_t matrix, const vec3_t euler, eulerOrder_t order);
221 /*! create m4x4 as a scaling matrix, for a scale vec3 */
222 void m4x4_scale_for_vec3(m4x4_t matrix, const vec3_t scale);
223 /*! create m4x4 as a rotation matrix, for a quaternion vec4 */
224 void m4x4_rotation_for_quat(m4x4_t matrix, const vec4_t rotation);
225 /*! create m4x4 as a rotation matrix, for an axis vec3 and an angle (radians) */
226 void m4x4_rotation_for_axisangle(m4x4_t matrix, const vec3_t axis, double angle);
227 /*! generate a perspective matrix by specifying the view frustum */
228 void m4x4_frustum(m4x4_t matrix, vec_t left, vec_t right, vec_t bottom, vec_t top, vec_t nearval, vec_t farval);
230 // a valid m4x4 to access is always first argument
231 /*! extract translation vec3 from matrix */
232 void m4x4_get_translation_vec3(const m4x4_t matrix, vec3_t translation);
233 /*! extract euler rotation angles from a rotation-only matrix */
234 void m4x4_get_rotation_vec3(const m4x4_t matrix, vec3_t euler, eulerOrder_t order);
235 /*! extract scale vec3 from matrix */
236 void m4x4_get_scale_vec3(const m4x4_t matrix, vec3_t scale);
237 /*! extract translation/euler/scale from an orthogonal matrix. NOTE: requires right-handed axis-base */
238 void m4x4_get_transform_vec3(const m4x4_t matrix, vec3_t translation, vec3_t euler, eulerOrder_t order, vec3_t scale);
240 // a valid m4x4 to be modified is always first argument
241 /*! translate m4x4 by a translation vec3 */
242 void m4x4_translate_by_vec3(m4x4_t matrix, const vec3_t translation);
243 /*! rotate m4x4 by a euler (degrees) vec3 */
244 void m4x4_rotate_by_vec3(m4x4_t matrix, const vec3_t euler, eulerOrder_t order);
245 /*! scale m4x4 by a scaling vec3 */
246 void m4x4_scale_by_vec3(m4x4_t matrix, const vec3_t scale);
247 /*! rotate m4x4 by a quaternion vec4 */
248 void m4x4_rotate_by_quat(m4x4_t matrix, const vec4_t rotation);
249 /*! rotate m4x4 by an axis vec3 and an angle (radians) */
250 void m4x4_rotate_by_axisangle(m4x4_t matrix, const vec3_t axis, double angle);
251 /*! transform m4x4 by translation/eulerZYX/scaling vec3 (transform = scale * eulerZ * eulerY * eulerX * translation) */
252 void m4x4_transform_by_vec3(m4x4_t matrix, const vec3_t translation, const vec3_t euler, eulerOrder_t order, const vec3_t scale);
253 /*! rotate m4x4 around a pivot point by eulerZYX vec3 */
254 void m4x4_pivoted_rotate_by_vec3(m4x4_t matrix, const vec3_t euler, eulerOrder_t order, const vec3_t pivotpoint);
255 /*! scale m4x4 around a pivot point by scaling vec3 */
256 void m4x4_pivoted_scale_by_vec3(m4x4_t matrix, const vec3_t scale, const vec3_t pivotpoint);
257 /*! transform m4x4 around a pivot point by translation/eulerZYX/scaling vec3 */
258 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);
259 /*! transform m4x4 around a pivot point by translation/rotation/scaling vec3 */
260 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);
261 /*! rotate m4x4 around a pivot point by quaternion vec4 */
262 void m4x4_pivoted_rotate_by_quat(m4x4_t matrix, const vec4_t quat, const vec3_t pivotpoint);
263 /*! rotate m4x4 around a pivot point by axis vec3 and angle (radians) */
264 void m4x4_pivoted_rotate_by_axisangle(m4x4_t matrix, const vec3_t axis, double angle, const vec3_t pivotpoint);
266 /*! postmultiply m4x4 by another m4x4 */
267 void m4x4_multiply_by_m4x4(m4x4_t matrix, const m4x4_t matrix_src);
268 /*! premultiply m4x4 by another m4x4 */
269 void m4x4_premultiply_by_m4x4(m4x4_t matrix, const m4x4_t matrix_src);
270 /*! postmultiply orthogonal m4x4 by another orthogonal m4x4 */
271 void m4x4_orthogonal_multiply_by_m4x4(m4x4_t matrix, const m4x4_t matrix_src);
272 /*! premultiply orthogonal m4x4 by another orthogonal m4x4 */
273 void m4x4_orthogonal_premultiply_by_m4x4(m4x4_t matrix, const m4x4_t matrix_src);
275 /*! multiply a point (x,y,z,1) by matrix */
276 void m4x4_transform_point(const m4x4_t matrix, vec3_t point);
277 /*! multiply a normal (x,y,z,0) by matrix */
278 void m4x4_transform_normal(const m4x4_t matrix, vec3_t normal);
279 /*! multiply a vec4 (x,y,z,w) by matrix */
280 void m4x4_transform_vec4(const m4x4_t matrix, vec4_t vector);
282 /*! multiply a point (x,y,z,1) by matrix */
283 void m4x4_transform_point(const m4x4_t matrix, vec3_t point);
284 /*! multiply a normal (x,y,z,0) by matrix */
285 void m4x4_transform_normal(const m4x4_t matrix, vec3_t normal);
287 /*! transpose a m4x4 */
288 void m4x4_transpose(m4x4_t matrix);
289 /*! invert an orthogonal 4x3 subset of a 4x4 matrix */
290 int m4x4_orthogonal_invert(m4x4_t matrix);
292 float m4_det( m4x4_t mr );
293 /*! invert any m4x4 using Kramer's rule.. return 1 if matrix is singular, else return 0 */
294 int m4x4_invert(m4x4_t matrix);
296 /*! clip a point (x,y,z,1) by canonical matrix */
297 clipmask_t m4x4_clip_point(const m4x4_t matrix, const vec3_t point, vec4_t clipped);
298 /*! device-space polygon for clipped triangle */
299 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]);
300 /*! device-space line for clipped line */
301 unsigned int m4x4_clip_line(const m4x4_t matrix, const vec3_t p0, const vec3_t p1, vec4_t clipped[2]);
304 //! quaternion identity
305 void quat_identity(vec4_t quat);
306 //! quaternion from two unit vectors
307 void quat_for_unit_vectors(vec4_t quat, const vec3_t from, const vec3_t to);
308 //! quaternion from axis and angle (radians)
309 void quat_for_axisangle(vec4_t quat, const vec3_t axis, double angle);
310 //! concatenates two rotations.. equivalent to m4x4_multiply_by_m4x4 .. postmultiply.. the right-hand side is the first rotation performed
311 void quat_multiply_by_quat(vec4_t quat, const vec4_t other);
312 //! negate a quaternion
313 void quat_conjugate(vec4_t quat);
314 //! normalise a quaternion
315 void quat_normalise(vec4_t quat);
320 \todo object/ray intersection functions should maybe return a point rather than a distance?
324 aabb_t - "axis-aligned" bounding box...
325 origin: centre of bounding box...
326 extents: +/- extents of box from origin...
328 typedef struct aabb_s
334 extern const aabb_t g_aabb_null;
337 bbox_t - oriented bounding box...
338 aabb: axis-aligned bounding box...
339 axes: orientation axes...
341 typedef struct bbox_s
349 ray_t - origin point and direction unit-vector
358 line_t - centre point and displacement of end point from centre
360 typedef struct line_s
367 /*! Generate line from start/end points. */
368 void line_construct_for_vec3(line_t* line, const vec3_t start, const vec3_t end);
369 /*! Return 2 if line is behind plane, else return 1 if line intersects plane, else return 0. */
370 int line_test_plane(const line_t* line, const vec4_t plane);
372 /*! Generate AABB from min/max. */
373 void aabb_construct_for_vec3(aabb_t* aabb, const vec3_t min, const vec3_t max);
374 /*! Initialise AABB to negative size. */
375 void aabb_clear(aabb_t* aabb);
377 /*! Extend AABB to include point. */
378 void aabb_extend_by_point(aabb_t* aabb, const vec3_t point);
379 /*! Extend AABB to include aabb_src. */
380 void aabb_extend_by_aabb(aabb_t* aabb, const aabb_t* aabb_src);
381 /*! Extend AABB by +/- extension vector. */
382 void aabb_extend_by_vec3(aabb_t* aabb, vec3_t extension);
384 /*! Return 2 if point is inside, else 1 if point is on surface, else 0. */
385 int aabb_test_point(const aabb_t* aabb, const vec3_t point);
386 /*! Return 2 if aabb_src intersects, else 1 if aabb_src touches exactly, else 0. */
387 int aabb_test_aabb(const aabb_t* aabb, const aabb_t* aabb_src);
388 /*! Return 2 if aabb is behind plane, else 1 if aabb intersects plane, else 0. */
389 int aabb_test_plane(const aabb_t* aabb, const float* plane);
390 /*! Return 1 if aabb intersects ray, else 0... dist = closest intersection. */
391 int aabb_intersect_ray(const aabb_t* aabb, const ray_t* ray, vec3_t intersection);
392 /*! Return 1 if aabb intersects ray, else 0. Faster, but does not provide point of intersection */
393 int aabb_test_ray(const aabb_t* aabb, const ray_t* ray);
395 /*! Return 2 if oriented aabb is behind plane, else 1 if aabb intersects plane, else 0. */
396 int aabb_oriented_intersect_plane(const aabb_t* aabb, const m4x4_t transform, const vec_t* plane);
398 /*! Calculate the corners of the aabb. */
399 void aabb_corners(const aabb_t* aabb, vec3_t corners[8]);
401 /*! (deprecated) Generate AABB from oriented bounding box. */
402 void aabb_for_bbox(aabb_t* aabb, const bbox_t* bbox);
403 /*! (deprecated) Generate AABB from 2-dimensions of min/max, specified by axis. */
404 void aabb_for_area(aabb_t* aabb, vec3_t area_tl, vec3_t area_br, int axis);
405 /*! Generate AABB to contain src* transform. NOTE: transform must be orthogonal */
406 void aabb_for_transformed_aabb(aabb_t* dst, const aabb_t* src, const m4x4_t transform);
408 /*! Update bounding-sphere radius. */
409 void bbox_update_radius(bbox_t* bbox);
410 /*! Generate oriented bounding box from AABB and transformation matrix. */
411 /*!\todo Remove need to specify eulerZYX/scale. */
412 void bbox_for_oriented_aabb(bbox_t* bbox, const aabb_t* aabb,
413 const m4x4_t matrix, const vec3_t eulerZYX, const vec3_t scale);
414 /*! Return 2 if bbox is behind plane, else return 1 if bbox intersects plane, else return 0. */
415 int bbox_intersect_plane(const bbox_t* bbox, const vec_t* plane);
418 /*! Generate a ray from an origin point and a direction unit-vector */
419 void ray_construct_for_vec3(ray_t* ray, const vec3_t origin, const vec3_t direction);
421 /*! Transform a ray */
422 void ray_transform(ray_t* ray, const m4x4_t matrix);
424 /*! distance from ray origin in ray direction to point. FLT_MAX if no intersection. */
425 vec_t ray_intersect_point(const ray_t* ray, const vec3_t point, vec_t epsilon, vec_t divergence);
426 /*! distance from ray origin in ray direction to triangle. FLT_MAX if no intersection. */
427 vec_t ray_intersect_triangle(const ray_t* ray, qboolean bCullBack, const vec3_t vert0, const vec3_t vert1, const vec3_t vert2);
428 /*! distance from ray origin in ray direction to plane. */
429 vec_t ray_intersect_plane(const ray_t* ray, const vec3_t normal, vec_t dist);
432 int plane_intersect_planes(const vec4_t plane1, const vec4_t plane2, const vec4_t plane3, vec3_t intersection);
436 ////////////////////////////////////////////////////////////////////////////////
437 // Below is double-precision math stuff. This was initially needed by the new
438 // "base winding" code in q3map2 brush processing in order to fix the famous
439 // "disappearing triangles" issue. These definitions can be used wherever extra
440 // precision is needed.
441 ////////////////////////////////////////////////////////////////////////////////
443 typedef double vec_accu_t;
444 typedef vec_accu_t vec3_accu_t[3];
446 // Smallest positive value for vec_accu_t such that 1.0 + VEC_ACCU_SMALLEST_EPSILON_AROUND_ONE != 1.0.
447 // In the case of 64 bit doubles (which is almost certainly the case), it's 0.00000000000000022204.
448 // Don't forget that your epsilons should depend on the possible range of values,
449 // because for example adding VEC_ACCU_SMALLEST_EPSILON_AROUND_ONE to 1024.0 will have no effect.
450 #define VEC_ACCU_SMALLEST_EPSILON_AROUND_ONE DBL_EPSILON
452 vec_accu_t VectorLengthAccu(const vec3_accu_t v);
454 // I have a feeling it may be safer to break these #define functions out into actual functions
455 // in order to avoid accidental loss of precision. For example, say you call
456 // VectorScaleAccu(vec3_t, vec_t, vec3_accu_t). The scale would take place in 32 bit land
457 // and the result would be cast to 64 bit, which would cause total loss of precision when
458 // scaling by a large factor.
459 //#define DotProductAccu(x, y) ((x)[0] * (y)[0] + (x)[1] * (y)[1] + (x)[2] * (y)[2])
460 //#define VectorSubtractAccu(a, b, c) ((c)[0] = (a)[0] - (b)[0], (c)[1] = (a)[1] - (b)[1], (c)[2] = (a)[2] - (b)[2])
461 //#define VectorAddAccu(a, b, c) ((c)[0] = (a)[0] + (b)[0], (c)[1] = (a)[1] + (b)[1], (c)[2] = (a)[2] + (b)[2])
462 //#define VectorCopyAccu(a, b) ((b)[0] = (a)[0], (b)[1] = (a)[1], (b)[2] = (a)[2])
463 //#define VectorScaleAccu(a, b, c) ((c)[0] = (b) * (a)[0], (c)[1] = (b) * (a)[1], (c)[2] = (b) * (a)[2])
464 //#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])
465 //#define Q_rintAccu(in) ((vec_accu_t) floor(in + 0.5))
467 vec_accu_t DotProductAccu(const vec3_accu_t a, const vec3_accu_t b);
468 void VectorSubtractAccu(const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out);
469 void VectorAddAccu(const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out);
470 void VectorCopyAccu(const vec3_accu_t in, vec3_accu_t out);
471 void VectorScaleAccu(const vec3_accu_t in, vec_accu_t scaleFactor, vec3_accu_t out);
472 void CrossProductAccu(const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out);
473 vec_accu_t Q_rintAccu(vec_accu_t val);
475 void VectorCopyAccuToRegular(const vec3_accu_t in, vec3_t out);
476 void VectorCopyRegularToAccu(const vec3_t in, vec3_accu_t out);
477 vec_accu_t VectorNormalizeAccu(const vec3_accu_t in, vec3_accu_t out);
483 #endif /* __MATHLIB__ */