mathlib.h 16 KB

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