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 vec_t VectorNormalize (const vec3_t in, vec3_t out);
102 vec_t ColorNormalize( const vec3_t in, vec3_t out );
103 void VectorInverse (vec3_t v);
104 void VectorPolar(vec3_t v, float radius, float theta, float phi);
106 // default snapping, to 1
107 void VectorSnap(vec3_t v);
110 void VectorISnap(vec3_t point, int snap);
112 // Gef: added snap to float for sub-integer grid sizes
113 // TTimo: we still use the int version of VectorSnap when possible
114 // to avoid potential rounding issues
115 // TTimo: renaming to VectorFSnap for C implementation
116 void VectorFSnap(vec3_t point, float snap);
118 // NOTE: added these from Ritual's Q3Radiant
119 void ClearBounds (vec3_t mins, vec3_t maxs);
120 void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs);
123 #define PITCH 0 // up / down
124 #define YAW 1 // left / right
125 #define ROLL 2 // fall over
127 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up);
128 void VectorToAngles( vec3_t vec, vec3_t angles );
130 #define ZERO_EPSILON 1.0E-6
131 #define RAD2DEGMULT 57.29577951308232f
132 #define DEG2RADMULT 0.01745329251994329f
133 #define RAD2DEG( a ) ( (a) * RAD2DEGMULT )
134 #define DEG2RAD( a ) ( (a) * DEG2RADMULT )
136 void VectorRotate (vec3_t vIn, vec3_t vRotation, vec3_t out);
137 void VectorRotateOrigin (vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out);
139 // some function merged from tools mathlib code
141 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c );
142 void NormalToLatLong( const vec3_t normal, byte bytes[2] );
143 int PlaneTypeForNormal (vec3_t normal);
144 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
149 FIXME test calls such as intersect tests should be named test_
152 typedef vec_t m3x3_t[9];
154 m4x4 looks like this..
160 translation (12 13 14)
163 typedef vec_t m4x4_t[16];
165 #define M4X4_INDEX(m,row,col) (m[(col<<2)+row])
167 typedef enum { eXYZ, eYZX, eZXY, eXZY, eYXZ, eZYX } eulerOrder_t;
169 #define CLIP_PASS 0x00 // 000000
170 #define CLIP_LT_X 0x01 // 000001
171 #define CLIP_GT_X 0x02 // 000010
172 #define CLIP_LT_Y 0x04 // 000100
173 #define CLIP_GT_Y 0x08 // 001000
174 #define CLIP_LT_Z 0x10 // 010000
175 #define CLIP_GT_Z 0x20 // 100000
176 #define CLIP_FAIL 0x3F // 111111
177 typedef unsigned char clipmask_t;
179 extern const m4x4_t g_m4x4_identity;
181 #define M4X4_COPY(dst,src) (\
192 (dst)[10]=(src)[10],\
193 (dst)[11]=(src)[11],\
194 (dst)[12]=(src)[12],\
195 (dst)[13]=(src)[13],\
196 (dst)[14]=(src)[14],\
206 m4x4Handedness_t m4x4_handedness(const m4x4_t matrix);
208 /*! assign other m4x4 to this m4x4 */
209 void m4x4_assign(m4x4_t matrix, const m4x4_t other);
212 /*! create m4x4 as identity matrix */
213 void m4x4_identity(m4x4_t matrix);
214 /*! create m4x4 as a translation matrix, for a translation vec3 */
215 void m4x4_translation_for_vec3(m4x4_t matrix, const vec3_t translation);
216 /*! create m4x4 as a rotation matrix, for an euler angles (degrees) vec3 */
217 void m4x4_rotation_for_vec3(m4x4_t matrix, const vec3_t euler, eulerOrder_t order);
218 /*! create m4x4 as a scaling matrix, for a scale vec3 */
219 void m4x4_scale_for_vec3(m4x4_t matrix, const vec3_t scale);
220 /*! create m4x4 as a rotation matrix, for a quaternion vec4 */
221 void m4x4_rotation_for_quat(m4x4_t matrix, const vec4_t rotation);
222 /*! create m4x4 as a rotation matrix, for an axis vec3 and an angle (radians) */
223 void m4x4_rotation_for_axisangle(m4x4_t matrix, const vec3_t axis, double angle);
224 /*! generate a perspective matrix by specifying the view frustum */
225 void m4x4_frustum(m4x4_t matrix, vec_t left, vec_t right, vec_t bottom, vec_t top, vec_t nearval, vec_t farval);
227 // a valid m4x4 to access is always first argument
228 /*! extract translation vec3 from matrix */
229 void m4x4_get_translation_vec3(const m4x4_t matrix, vec3_t translation);
230 /*! extract euler rotation angles from a rotation-only matrix */
231 void m4x4_get_rotation_vec3(const m4x4_t matrix, vec3_t euler, eulerOrder_t order);
232 /*! extract scale vec3 from matrix */
233 void m4x4_get_scale_vec3(const m4x4_t matrix, vec3_t scale);
234 /*! extract translation/euler/scale from an orthogonal matrix. NOTE: requires right-handed axis-base */
235 void m4x4_get_transform_vec3(const m4x4_t matrix, vec3_t translation, vec3_t euler, eulerOrder_t order, vec3_t scale);
237 // a valid m4x4 to be modified is always first argument
238 /*! translate m4x4 by a translation vec3 */
239 void m4x4_translate_by_vec3(m4x4_t matrix, const vec3_t translation);
240 /*! rotate m4x4 by a euler (degrees) vec3 */
241 void m4x4_rotate_by_vec3(m4x4_t matrix, const vec3_t euler, eulerOrder_t order);
242 /*! scale m4x4 by a scaling vec3 */
243 void m4x4_scale_by_vec3(m4x4_t matrix, const vec3_t scale);
244 /*! rotate m4x4 by a quaternion vec4 */
245 void m4x4_rotate_by_quat(m4x4_t matrix, const vec4_t rotation);
246 /*! rotate m4x4 by an axis vec3 and an angle (radians) */
247 void m4x4_rotate_by_axisangle(m4x4_t matrix, const vec3_t axis, double angle);
248 /*! transform m4x4 by translation/eulerZYX/scaling vec3 (transform = scale * eulerZ * eulerY * eulerX * translation) */
249 void m4x4_transform_by_vec3(m4x4_t matrix, const vec3_t translation, const vec3_t euler, eulerOrder_t order, const vec3_t scale);
250 /*! rotate m4x4 around a pivot point by eulerZYX vec3 */
251 void m4x4_pivoted_rotate_by_vec3(m4x4_t matrix, const vec3_t euler, eulerOrder_t order, const vec3_t pivotpoint);
252 /*! scale m4x4 around a pivot point by scaling vec3 */
253 void m4x4_pivoted_scale_by_vec3(m4x4_t matrix, const vec3_t scale, const vec3_t pivotpoint);
254 /*! transform m4x4 around a pivot point by translation/eulerZYX/scaling vec3 */
255 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);
256 /*! transform m4x4 around a pivot point by translation/rotation/scaling vec3 */
257 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);
258 /*! rotate m4x4 around a pivot point by quaternion vec4 */
259 void m4x4_pivoted_rotate_by_quat(m4x4_t matrix, const vec4_t quat, const vec3_t pivotpoint);
260 /*! rotate m4x4 around a pivot point by axis vec3 and angle (radians) */
261 void m4x4_pivoted_rotate_by_axisangle(m4x4_t matrix, const vec3_t axis, double angle, const vec3_t pivotpoint);
263 /*! postmultiply m4x4 by another m4x4 */
264 void m4x4_multiply_by_m4x4(m4x4_t matrix, const m4x4_t matrix_src);
265 /*! premultiply m4x4 by another m4x4 */
266 void m4x4_premultiply_by_m4x4(m4x4_t matrix, const m4x4_t matrix_src);
267 /*! postmultiply orthogonal m4x4 by another orthogonal m4x4 */
268 void m4x4_orthogonal_multiply_by_m4x4(m4x4_t matrix, const m4x4_t matrix_src);
269 /*! premultiply orthogonal m4x4 by another orthogonal m4x4 */
270 void m4x4_orthogonal_premultiply_by_m4x4(m4x4_t matrix, const m4x4_t matrix_src);
272 /*! multiply a point (x,y,z,1) by matrix */
273 void m4x4_transform_point(const m4x4_t matrix, vec3_t point);
274 /*! multiply a normal (x,y,z,0) by matrix */
275 void m4x4_transform_normal(const m4x4_t matrix, vec3_t normal);
276 /*! multiply a vec4 (x,y,z,w) by matrix */
277 void m4x4_transform_vec4(const m4x4_t matrix, vec4_t vector);
279 /*! multiply a point (x,y,z,1) by matrix */
280 void m4x4_transform_point(const m4x4_t matrix, vec3_t point);
281 /*! multiply a normal (x,y,z,0) by matrix */
282 void m4x4_transform_normal(const m4x4_t matrix, vec3_t normal);
284 /*! transpose a m4x4 */
285 void m4x4_transpose(m4x4_t matrix);
286 /*! invert an orthogonal 4x3 subset of a 4x4 matrix */
287 int m4x4_orthogonal_invert(m4x4_t matrix);
289 float m4_det( m4x4_t mr );
290 /*! invert any m4x4 using Kramer's rule.. return 1 if matrix is singular, else return 0 */
291 int m4x4_invert(m4x4_t matrix);
293 /*! clip a point (x,y,z,1) by canonical matrix */
294 clipmask_t m4x4_clip_point(const m4x4_t matrix, const vec3_t point, vec4_t clipped);
295 /*! device-space polygon for clipped triangle */
296 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]);
297 /*! device-space line for clipped line */
298 unsigned int m4x4_clip_line(const m4x4_t matrix, const vec3_t p0, const vec3_t p1, vec4_t clipped[2]);
301 //! quaternion identity
302 void quat_identity(vec4_t quat);
303 //! quaternion from two unit vectors
304 void quat_for_unit_vectors(vec4_t quat, const vec3_t from, const vec3_t to);
305 //! quaternion from axis and angle (radians)
306 void quat_for_axisangle(vec4_t quat, const vec3_t axis, double angle);
307 //! concatenates two rotations.. equivalent to m4x4_multiply_by_m4x4 .. postmultiply.. the right-hand side is the first rotation performed
308 void quat_multiply_by_quat(vec4_t quat, const vec4_t other);
309 //! negate a quaternion
310 void quat_conjugate(vec4_t quat);
311 //! normalise a quaternion
312 void quat_normalise(vec4_t quat);
317 \todo object/ray intersection functions should maybe return a point rather than a distance?
321 aabb_t - "axis-aligned" bounding box...
322 origin: centre of bounding box...
323 extents: +/- extents of box from origin...
325 typedef struct aabb_s
331 extern const aabb_t g_aabb_null;
334 bbox_t - oriented bounding box...
335 aabb: axis-aligned bounding box...
336 axes: orientation axes...
338 typedef struct bbox_s
346 ray_t - origin point and direction unit-vector
355 line_t - centre point and displacement of end point from centre
357 typedef struct line_s
364 /*! Generate line from start/end points. */
365 void line_construct_for_vec3(line_t* line, const vec3_t start, const vec3_t end);
366 /*! Return 2 if line is behind plane, else return 1 if line intersects plane, else return 0. */
367 int line_test_plane(const line_t* line, const vec4_t plane);
369 /*! Generate AABB from min/max. */
370 void aabb_construct_for_vec3(aabb_t* aabb, const vec3_t min, const vec3_t max);
371 /*! Initialise AABB to negative size. */
372 void aabb_clear(aabb_t* aabb);
374 /*! Extend AABB to include point. */
375 void aabb_extend_by_point(aabb_t* aabb, const vec3_t point);
376 /*! Extend AABB to include aabb_src. */
377 void aabb_extend_by_aabb(aabb_t* aabb, const aabb_t* aabb_src);
378 /*! Extend AABB by +/- extension vector. */
379 void aabb_extend_by_vec3(aabb_t* aabb, vec3_t extension);
381 /*! Return 2 if point is inside, else 1 if point is on surface, else 0. */
382 int aabb_test_point(const aabb_t* aabb, const vec3_t point);
383 /*! Return 2 if aabb_src intersects, else 1 if aabb_src touches exactly, else 0. */
384 int aabb_test_aabb(const aabb_t* aabb, const aabb_t* aabb_src);
385 /*! Return 2 if aabb is behind plane, else 1 if aabb intersects plane, else 0. */
386 int aabb_test_plane(const aabb_t* aabb, const float* plane);
387 /*! Return 1 if aabb intersects ray, else 0... dist = closest intersection. */
388 int aabb_intersect_ray(const aabb_t* aabb, const ray_t* ray, vec3_t intersection);
389 /*! Return 1 if aabb intersects ray, else 0. Faster, but does not provide point of intersection */
390 int aabb_test_ray(const aabb_t* aabb, const ray_t* ray);
392 /*! Return 2 if oriented aabb is behind plane, else 1 if aabb intersects plane, else 0. */
393 int aabb_oriented_intersect_plane(const aabb_t* aabb, const m4x4_t transform, const vec_t* plane);
395 /*! Calculate the corners of the aabb. */
396 void aabb_corners(const aabb_t* aabb, vec3_t corners[8]);
398 /*! (deprecated) Generate AABB from oriented bounding box. */
399 void aabb_for_bbox(aabb_t* aabb, const bbox_t* bbox);
400 /*! (deprecated) Generate AABB from 2-dimensions of min/max, specified by axis. */
401 void aabb_for_area(aabb_t* aabb, vec3_t area_tl, vec3_t area_br, int axis);
402 /*! Generate AABB to contain src* transform. NOTE: transform must be orthogonal */
403 void aabb_for_transformed_aabb(aabb_t* dst, const aabb_t* src, const m4x4_t transform);
405 /*! Update bounding-sphere radius. */
406 void bbox_update_radius(bbox_t* bbox);
407 /*! Generate oriented bounding box from AABB and transformation matrix. */
408 /*!\todo Remove need to specify eulerZYX/scale. */
409 void bbox_for_oriented_aabb(bbox_t* bbox, const aabb_t* aabb,
410 const m4x4_t matrix, const vec3_t eulerZYX, const vec3_t scale);
411 /*! Return 2 if bbox is behind plane, else return 1 if bbox intersects plane, else return 0. */
412 int bbox_intersect_plane(const bbox_t* bbox, const vec_t* plane);
415 /*! Generate a ray from an origin point and a direction unit-vector */
416 void ray_construct_for_vec3(ray_t* ray, const vec3_t origin, const vec3_t direction);
418 /*! Transform a ray */
419 void ray_transform(ray_t* ray, const m4x4_t matrix);
421 /*! distance from ray origin in ray direction to point. FLT_MAX if no intersection. */
422 vec_t ray_intersect_point(const ray_t* ray, const vec3_t point, vec_t epsilon, vec_t divergence);
423 /*! distance from ray origin in ray direction to triangle. FLT_MAX if no intersection. */
424 vec_t ray_intersect_triangle(const ray_t* ray, qboolean bCullBack, const vec3_t vert0, const vec3_t vert1, const vec3_t vert2);
425 /*! distance from ray origin in ray direction to plane. */
426 vec_t ray_intersect_plane(const ray_t* ray, const vec3_t normal, vec_t dist);
429 int plane_intersect_planes(const vec4_t plane1, const vec4_t plane2, const vec4_t plane3, vec3_t intersection);
433 ////////////////////////////////////////////////////////////////////////////////
434 // Below is double-precision math stuff. This was initially needed by the new
435 // "base winding" code in q3map2 brush processing in order to fix the famous
436 // "disappearing triangles" issue. These definitions can be used wherever extra
437 // precision is needed.
438 ////////////////////////////////////////////////////////////////////////////////
440 typedef double vec_accu_t;
441 typedef vec_accu_t vec3_accu_t[3];
443 // Smallest positive value for vec_accu_t such that 1.0 + VEC_ACCU_SMALLEST_EPSILON_AROUND_ONE != 1.0.
444 // In the case of 64 bit doubles (which is almost certainly the case), it's 0.00000000000000022204.
445 // Don't forget that your epsilons should depend on the possible range of values,
446 // because for example adding VEC_ACCU_SMALLEST_EPSILON_AROUND_ONE to 1024.0 will have no effect.
447 #define VEC_ACCU_SMALLEST_EPSILON_AROUND_ONE DBL_EPSILON
449 vec_accu_t VectorLengthAccu(const vec3_accu_t v);
451 // I have a feeling it may be safer to break these #define functions out into actual functions
452 // in order to avoid accidental loss of precision. For example, say you call
453 // VectorScaleAccu(vec3_t, vec_t, vec3_accu_t). The scale would take place in 32 bit land
454 // and the result would be cast to 64 bit, which would cause total loss of precision when
455 // scaling by a large factor.
456 //#define DotProductAccu(x, y) ((x)[0] * (y)[0] + (x)[1] * (y)[1] + (x)[2] * (y)[2])
457 //#define VectorSubtractAccu(a, b, c) ((c)[0] = (a)[0] - (b)[0], (c)[1] = (a)[1] - (b)[1], (c)[2] = (a)[2] - (b)[2])
458 //#define VectorAddAccu(a, b, c) ((c)[0] = (a)[0] + (b)[0], (c)[1] = (a)[1] + (b)[1], (c)[2] = (a)[2] + (b)[2])
459 //#define VectorCopyAccu(a, b) ((b)[0] = (a)[0], (b)[1] = (a)[1], (b)[2] = (a)[2])
460 //#define VectorScaleAccu(a, b, c) ((c)[0] = (b) * (a)[0], (c)[1] = (b) * (a)[1], (c)[2] = (b) * (a)[2])
461 //#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])
462 //#define Q_rintAccu(in) ((vec_accu_t) floor(in + 0.5))
464 vec_accu_t DotProductAccu(const vec3_accu_t a, const vec3_accu_t b);
465 void VectorSubtractAccu(const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out);
466 void VectorAddAccu(const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out);
467 void VectorCopyAccu(const vec3_accu_t in, vec3_accu_t out);
468 void VectorScaleAccu(const vec3_accu_t in, vec_accu_t scaleFactor, vec3_accu_t out);
469 void CrossProductAccu(const vec3_accu_t a, const vec3_accu_t b, vec3_accu_t out);
470 vec_accu_t Q_rintAccu(vec_accu_t val);
472 void VectorCopyAccuToRegular(const vec3_accu_t in, vec3_t out);
473 void VectorCopyRegularToAccu(const vec3_t in, vec3_accu_t out);
474 vec_accu_t VectorNormalizeAccu(const vec3_accu_t in, vec3_accu_t out);
480 #endif /* __MATHLIB__ */