]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/mathlib.h
misc fixes
[xonotic/netradiant.git] / libs / mathlib.h
1 /*
2 Copyright (C) 1999-2007 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 #include "bytebool.h"
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #endif
34
35 typedef float vec_t;
36 typedef vec_t vec3_t[3];
37 typedef vec_t vec5_t[5];
38 typedef vec_t vec4_t[4];
39
40 #define SIDE_FRONT              0
41 #define SIDE_ON                 2
42 #define SIDE_BACK               1
43 #define SIDE_CROSS              -2
44
45 // plane types are used to speed some tests
46 // 0-2 are axial planes
47 #define PLANE_X                 0
48 #define PLANE_Y                 1
49 #define PLANE_Z                 2
50 #define PLANE_NON_AXIAL 3
51
52 #define Q_PI    3.14159265358979323846f
53
54 extern vec3_t vec3_origin;
55
56 #define EQUAL_EPSILON   0.001
57
58 #ifndef VEC_MAX
59 #define VEC_MAX 3.402823466e+38F
60 #endif
61
62 qboolean VectorCompare (vec3_t v1, vec3_t v2);
63
64 #define DotProduct(x,y) ((x)[0]*(y)[0]+(x)[1]*(y)[1]+(x)[2]*(y)[2])
65 #define VectorSubtract(a,b,c) ((c)[0]=(a)[0]-(b)[0],(c)[1]=(a)[1]-(b)[1],(c)[2]=(a)[2]-(b)[2])
66 #define VectorAdd(a,b,c) ((c)[0]=(a)[0]+(b)[0],(c)[1]=(a)[1]+(b)[1],(c)[2]=(a)[2]+(b)[2])
67 #define VectorIncrement(a,b) ((b)[0]+=(a)[0],(b)[1]+=(a)[1],(b)[2]+=(a)[2])
68 #define VectorCopy(a,b) ((b)[0]=(a)[0],(b)[1]=(a)[1],(b)[2]=(a)[2])
69 #define VectorSet(v, a, b, c) ((v)[0]=(a),(v)[1]=(b),(v)[2]=(c))
70 #define VectorScale(a,b,c) ((c)[0]=(b)*(a)[0],(c)[1]=(b)*(a)[1],(c)[2]=(b)*(a)[2])
71 #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)
72 #define VectorNegative(a,b) ((b)[0]=-(a)[0],(b)[1]=-(a)[1],(b)[2]=-(a)[2])
73 #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])
74 #define VectorClear(x) ((x)[0]=(x)[1]=(x)[2]=0)
75
76 #define Q_rint(in) ((vec_t)floor(in+0.5))
77
78 vec_t VectorLength(vec3_t v);
79
80 void VectorMA( const vec3_t va, vec_t scale, const vec3_t vb, vec3_t vc );
81
82 void _CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross);
83 vec_t VectorNormalize (const vec3_t in, vec3_t out);
84 vec_t ColorNormalize( const vec3_t in, vec3_t out );
85 void VectorInverse (vec3_t v);
86 void VectorPolar(vec3_t v, float radius, float theta, float phi);
87
88 // default snapping, to 1
89 void VectorSnap(vec3_t v);
90
91 // integer snapping
92 void VectorISnap(vec3_t point, int snap);
93
94 // Gef:   added snap to float for sub-integer grid sizes
95 // TTimo: we still use the int version of VectorSnap when possible
96 //        to avoid potential rounding issues
97 // TTimo: renaming to VectorFSnap for C implementation
98 void VectorFSnap(vec3_t point, float snap);
99
100 // NOTE: added these from Ritual's Q3Radiant
101 void ClearBounds (vec3_t mins, vec3_t maxs);
102 void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs);
103
104 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up);
105 void VectorToAngles( vec3_t vec, vec3_t angles );
106
107 #define ZERO_EPSILON 1.0E-6
108 #define RAD2DEGMULT 57.29577951308232f
109 #define DEG2RADMULT 0.01745329251994329f
110 #define RAD2DEG( a ) ( (a) * RAD2DEGMULT )
111 #define DEG2RAD( a ) ( (a) * DEG2RADMULT )
112
113 void VectorRotate (vec3_t vIn, vec3_t vRotation, vec3_t out);
114 void VectorRotateOrigin (vec3_t vIn, vec3_t vRotation, vec3_t vOrigin, vec3_t out);
115
116 // some function merged from tools mathlib code
117
118 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c );
119 void NormalToLatLong( const vec3_t normal, byte bytes[2] );
120 int     PlaneTypeForNormal (vec3_t normal);
121 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
122
123 // Spog
124 // code imported from geomlib
125
126 /*!
127 \todo
128 FIXME test calls such as intersect tests should be named test_
129 */
130
131 typedef vec_t m3x3_t[9];
132 /*!NOTE 
133 m4x4 looks like this..
134
135                 x  y  z
136 x axis        ( 0  1  2)
137 y axis        ( 4  5  6)
138 z axis        ( 8  9 10)
139 translation   (12 13 14)
140 scale         ( 0  5 10)
141 */
142 typedef vec_t m4x4_t[16];
143
144 #define M4X4_INDEX(m,row,col) (m[(col<<2)+row])
145
146 typedef enum { TRANSLATE, SCALE, ROTATE } transformtype; // legacy, used only in pmesh.cpp
147
148 typedef enum { eXYZ, eYZX, eZXY, eXZY, eYXZ, eZYX } eulerOrder_t;
149
150 // constructors
151 /*! create m4x4 as identity matrix */
152 void m4x4_identity(m4x4_t matrix);
153 /*! create m4x4 as a translation matrix, for a translation vec3 */
154 void m4x4_translation_for_vec3(m4x4_t matrix, const vec3_t translation);
155 /*! create m4x4 as a rotation matrix, for an euler angles (degrees) vec3 */
156 void m4x4_rotation_for_vec3(m4x4_t matrix, const vec3_t euler, eulerOrder_t order);
157 /*! create m4x4 as a scaling matrix, for a scale vec3 */
158 void m4x4_scale_for_vec3(m4x4_t matrix, const vec3_t scale);
159 /*! create m4x4 as a rotation matrix, for a quaternion vec4 */
160 void m4x4_rotation_for_quat(m4x4_t matrix, const vec4_t rotation);
161 /*! create m4x4 as a rotation matrix, for an axis vec3 and an angle (radians) */
162 void m4x4_rotation_for_axisangle(m4x4_t matrix, const vec3_t axis, vec_t angle);
163
164 // a valid m4x4 to be modified is always first argument
165 /*! translate m4x4 by a translation vec3 */
166 void m4x4_translate_by_vec3(m4x4_t matrix, const vec3_t translation);
167 /*! rotate m4x4 by a euler (degrees) vec3 */
168 void m4x4_rotate_by_vec3(m4x4_t matrix, const vec3_t euler, eulerOrder_t order);
169 /*! scale m4x4 by a scaling vec3 */
170 void m4x4_scale_by_vec3(m4x4_t matrix, const vec3_t scale);
171 /*! rotate m4x4 by a quaternion vec4 */
172 void m4x4_rotate_by_quat(m4x4_t matrix, const vec4_t rotation);
173 /*! rotate m4x4 by an axis vec3 and an angle (radians) */
174 void m4x4_rotate_by_axisangle(m4x4_t matrix, const vec3_t axis, vec_t angle);
175 /*! transform m4x4 by translation/euler/scaling vec3 (transform = translation.euler.scale) */
176 void m4x4_transform_by_vec3(m4x4_t matrix, const vec3_t translation, const vec3_t euler, eulerOrder_t order, const vec3_t scale);
177 /*! rotate m4x4 around a pivot point by euler(degrees) vec3 */
178 void m4x4_pivoted_rotate_by_vec3(m4x4_t matrix, const vec3_t euler, eulerOrder_t order, const vec3_t pivotpoint);
179 /*! scale m4x4 around a pivot point by scaling vec3 */
180 void m4x4_pivoted_scale_by_vec3(m4x4_t matrix, const vec3_t scale, const vec3_t pivotpoint);
181 /*! transform m4x4 around a pivot point by translation/euler/scaling vec3 */
182 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);
183 /*! rotate m4x4 around a pivot point by quaternion vec4 */
184 void m4x4_pivoted_rotate_by_quat(m4x4_t matrix, const vec4_t rotation, const vec3_t pivotpoint);
185 /*! rotate m4x4 around a pivot point by axis vec3 and angle (radians) */
186 void m4x4_pivoted_rotate_by_axisangle(m4x4_t matrix, const vec3_t axis, vec_t angle, const vec3_t pivotpoint);
187 /*! post-multiply m4x4 by another m4x4 */
188 void m4x4_multiply_by_m4x4(m4x4_t matrix, const m4x4_t other);
189 /*! pre-multiply m4x4 by another m4x4 */
190 void m4x4_premultiply_by_m4x4(m4x4_t matrix, const m4x4_t other);
191
192 /*! multiply a point (x,y,z,1) by matrix */
193 void m4x4_transform_point(const m4x4_t matrix, vec3_t point);
194 /*! multiply a normal (x,y,z,0) by matrix */
195 void m4x4_transform_normal(const m4x4_t matrix, vec3_t normal);
196 /*! multiply a vec4 (x,y,z,w) by matrix */
197 void m4x4_transform_vec4(const m4x4_t matrix, vec4_t vector);
198
199 /*! multiply a point (x,y,z,1) by matrix */
200 void m4x4_transform_point(const m4x4_t matrix, vec3_t point);
201 /*! multiply a normal (x,y,z,0) by matrix */
202 void m4x4_transform_normal(const m4x4_t matrix, vec3_t normal);
203
204 /*! transpose a m4x4 */
205 void m4x4_transpose(m4x4_t matrix);
206 /*! invert an orthogonal 4x3 subset of a 4x4 matrix */
207 void m4x4_orthogonal_invert(m4x4_t matrix);
208 /*! invert any m4x4 using Kramer's rule.. return 1 if matrix is singular, else return 0 */
209 int m4x4_invert(m4x4_t matrix);
210
211 /*!
212 \todo object/ray intersection functions should maybe return a point rather than a distance?
213 */
214
215 /*!
216 aabb_t -  "axis-aligned" bounding box... 
217   origin: centre of bounding box... 
218   extents: +/- extents of box from origin... 
219   radius: cached length of extents vector... 
220 */
221 typedef struct aabb_s
222 {
223   vec3_t origin;
224   vec3_t extents;
225   vec_t radius;
226 } aabb_t;
227
228 /*!
229 bbox_t - oriented bounding box... 
230   aabb: axis-aligned bounding box... 
231   axes: orientation axes... 
232 */
233 typedef struct bbox_s
234 {
235   aabb_t aabb;
236   vec3_t axes[3];
237 } bbox_t;
238
239 /*!
240 ray_t - origin point and direction unit-vector
241 */
242 typedef struct ray_s
243 {
244   vec3_t origin;
245   vec3_t direction;
246 } ray_t;
247
248
249 /*! Generate AABB from min/max. */
250 void aabb_construct_for_vec3(aabb_t *aabb, const vec3_t min, const vec3_t max);
251 /*! Update bounding-sphere radius. */
252 void aabb_update_radius(aabb_t *aabb);
253 /*! Initialise AABB to negative size. */
254 void aabb_clear(aabb_t *aabb);
255
256 /*! Extend AABB to include point. */
257 void aabb_extend_by_point(aabb_t *aabb, const vec3_t point);
258 /*! Extend AABB to include aabb_src. */
259 void aabb_extend_by_aabb(aabb_t *aabb, const aabb_t *aabb_src);
260 /*! Extend AABB by +/- extension vector. */
261 void aabb_extend_by_vec3(aabb_t *aabb, vec3_t extension);
262
263 /*! Return 2 if point is inside, else 1 if point is on surface, else 0. */
264 int aabb_intersect_point(const aabb_t *aabb, const vec3_t point);
265 /*! Return 2 if aabb_src intersects, else 1 if aabb_src touches exactly, else 0. */
266 int aabb_intersect_aabb(const aabb_t *aabb, const aabb_t *aabb_src);
267 /*! Return 2 if aabb is behind plane, else 1 if aabb intersects plane, else 0. */
268 int aabb_intersect_plane(const aabb_t *aabb, const float *plane);
269 /*! Return 1 if aabb intersects ray, else 0... dist = closest intersection. */
270 int aabb_intersect_ray(const aabb_t *aabb, const ray_t *ray, vec_t *dist);
271 /*! Return 1 if aabb intersects ray, else 0. Faster, but does not provide point of intersection */
272 int aabb_test_ray(const aabb_t* aabb, const ray_t* ray);
273
274 /*! Generate AABB from oriented bounding box. */
275 void aabb_for_bbox(aabb_t *aabb, const bbox_t *bbox);
276 /*! Generate AABB from 2-dimensions of min/max, specified by axis. */
277 void aabb_for_area(aabb_t *aabb, vec3_t area_tl, vec3_t area_br, int axis);
278 /*! Generate AABB to contain src * transform. NOTE: transform must be orthogonal */
279 void aabb_for_transformed_aabb(aabb_t* dst, const aabb_t* src, const m4x4_t transform);
280
281
282 /*! Generate oriented bounding box from AABB and transformation matrix. */
283 /*!\todo Remove need to specify euler/scale. */
284 void bbox_for_oriented_aabb(bbox_t *bbox, const aabb_t *aabb,
285                     const m4x4_t matrix, const vec3_t euler, const vec3_t scale);
286 /*! Return 2 is bbox is behind plane, else return 1 if bbox intersects plane, else return 0. */
287 int bbox_intersect_plane(const bbox_t *bbox, const vec_t* plane);
288
289
290 /*! Generate a ray from an origin point and a direction unit-vector */
291 void ray_construct_for_vec3(ray_t *ray, const vec3_t origin, const vec3_t direction);
292   
293 /*! Transform a ray */
294 void ray_transform(ray_t *ray, const m4x4_t matrix);
295
296 /*! return true if point intersects cone formed by ray, divergence and epsilon */
297 vec_t ray_intersect_point(const ray_t *ray, const vec3_t point, vec_t epsilon, vec_t divergence);
298 /*! return true if triangle intersects ray... dist = dist from intersection point to ray-origin */
299 vec_t ray_intersect_triangle(const ray_t *ray, qboolean bCullBack, const vec3_t vert0, const vec3_t vert1, const vec3_t vert2);
300
301 #ifdef __cplusplus
302 }
303 #endif
304
305 #endif /* __MATHLIB__ */