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