gim_basic_geometry_operations.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #ifndef GIM_BASIC_GEOMETRY_OPERATIONS_H_INCLUDED
  2. #define GIM_BASIC_GEOMETRY_OPERATIONS_H_INCLUDED
  3. /*! \file gim_basic_geometry_operations.h
  4. *\author Francisco Leon Najera
  5. type independant geometry routines
  6. */
  7. /*
  8. -----------------------------------------------------------------------------
  9. This source file is part of GIMPACT Library.
  10. For the latest info, see http://gimpact.sourceforge.net/
  11. Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
  12. email: [email protected]
  13. This library is free software; you can redistribute it and/or
  14. modify it under the terms of EITHER:
  15. (1) The GNU Lesser General Public License as published by the Free
  16. Software Foundation; either version 2.1 of the License, or (at
  17. your option) any later version. The text of the GNU Lesser
  18. General Public License is included with this library in the
  19. file GIMPACT-LICENSE-LGPL.TXT.
  20. (2) The BSD-style license that is included with this library in
  21. the file GIMPACT-LICENSE-BSD.TXT.
  22. (3) The zlib/libpng license that is included with this library in
  23. the file GIMPACT-LICENSE-ZLIB.TXT.
  24. This library is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  27. GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
  28. -----------------------------------------------------------------------------
  29. */
  30. #include "gim_linear_math.h"
  31. #ifndef PLANEDIREPSILON
  32. #define PLANEDIREPSILON 0.0000001f
  33. #endif
  34. #ifndef PARALELENORMALS
  35. #define PARALELENORMALS 0.000001f
  36. #endif
  37. #define TRIANGLE_NORMAL(v1, v2, v3, n) \
  38. { \
  39. vec3f _dif1, _dif2; \
  40. VEC_DIFF(_dif1, v2, v1); \
  41. VEC_DIFF(_dif2, v3, v1); \
  42. VEC_CROSS(n, _dif1, _dif2); \
  43. VEC_NORMALIZE(n); \
  44. }
  45. #define TRIANGLE_NORMAL_FAST(v1, v2, v3, n) \
  46. { \
  47. vec3f _dif1, _dif2; \
  48. VEC_DIFF(_dif1, v2, v1); \
  49. VEC_DIFF(_dif2, v3, v1); \
  50. VEC_CROSS(n, _dif1, _dif2); \
  51. }
  52. /// plane is a vec4f
  53. #define TRIANGLE_PLANE(v1, v2, v3, plane) \
  54. { \
  55. TRIANGLE_NORMAL(v1, v2, v3, plane); \
  56. plane[3] = VEC_DOT(v1, plane); \
  57. }
  58. /// plane is a vec4f
  59. #define TRIANGLE_PLANE_FAST(v1, v2, v3, plane) \
  60. { \
  61. TRIANGLE_NORMAL_FAST(v1, v2, v3, plane); \
  62. plane[3] = VEC_DOT(v1, plane); \
  63. }
  64. /// Calc a plane from an edge an a normal. plane is a vec4f
  65. #define EDGE_PLANE(e1, e2, n, plane) \
  66. { \
  67. vec3f _dif; \
  68. VEC_DIFF(_dif, e2, e1); \
  69. VEC_CROSS(plane, _dif, n); \
  70. VEC_NORMALIZE(plane); \
  71. plane[3] = VEC_DOT(e1, plane); \
  72. }
  73. #define DISTANCE_PLANE_POINT(plane, point) (VEC_DOT(plane, point) - plane[3])
  74. #define PROJECT_POINT_PLANE(point, plane, projected) \
  75. { \
  76. GREAL _dis; \
  77. _dis = DISTANCE_PLANE_POINT(plane, point); \
  78. VEC_SCALE(projected, -_dis, plane); \
  79. VEC_SUM(projected, projected, point); \
  80. }
  81. //! Verifies if a point is in the plane hull
  82. template <typename CLASS_POINT, typename CLASS_PLANE>
  83. SIMD_FORCE_INLINE bool POINT_IN_HULL(
  84. const CLASS_POINT &point, const CLASS_PLANE *planes, GUINT plane_count)
  85. {
  86. GREAL _dis;
  87. for (GUINT _i = 0; _i < plane_count; ++_i)
  88. {
  89. _dis = DISTANCE_PLANE_POINT(planes[_i], point);
  90. if (_dis > 0.0f) return false;
  91. }
  92. return true;
  93. }
  94. template <typename CLASS_POINT, typename CLASS_PLANE>
  95. SIMD_FORCE_INLINE void PLANE_CLIP_SEGMENT(
  96. const CLASS_POINT &s1,
  97. const CLASS_POINT &s2, const CLASS_PLANE &plane, CLASS_POINT &clipped)
  98. {
  99. GREAL _dis1, _dis2;
  100. _dis1 = DISTANCE_PLANE_POINT(plane, s1);
  101. VEC_DIFF(clipped, s2, s1);
  102. _dis2 = VEC_DOT(clipped, plane);
  103. VEC_SCALE(clipped, -_dis1 / _dis2, clipped);
  104. VEC_SUM(clipped, clipped, s1);
  105. }
  106. enum ePLANE_INTERSECTION_TYPE
  107. {
  108. G_BACK_PLANE = 0,
  109. G_COLLIDE_PLANE,
  110. G_FRONT_PLANE
  111. };
  112. enum eLINE_PLANE_INTERSECTION_TYPE
  113. {
  114. G_FRONT_PLANE_S1 = 0,
  115. G_FRONT_PLANE_S2,
  116. G_BACK_PLANE_S1,
  117. G_BACK_PLANE_S2,
  118. G_COLLIDE_PLANE_S1,
  119. G_COLLIDE_PLANE_S2
  120. };
  121. //! Confirms if the plane intersect the edge or nor
  122. /*!
  123. intersection type must have the following values
  124. <ul>
  125. <li> 0 : Segment in front of plane, s1 closest
  126. <li> 1 : Segment in front of plane, s2 closest
  127. <li> 2 : Segment in back of plane, s1 closest
  128. <li> 3 : Segment in back of plane, s2 closest
  129. <li> 4 : Segment collides plane, s1 in back
  130. <li> 5 : Segment collides plane, s2 in back
  131. </ul>
  132. */
  133. template <typename CLASS_POINT, typename CLASS_PLANE>
  134. SIMD_FORCE_INLINE eLINE_PLANE_INTERSECTION_TYPE PLANE_CLIP_SEGMENT2(
  135. const CLASS_POINT &s1,
  136. const CLASS_POINT &s2,
  137. const CLASS_PLANE &plane, CLASS_POINT &clipped)
  138. {
  139. GREAL _dis1 = DISTANCE_PLANE_POINT(plane, s1);
  140. GREAL _dis2 = DISTANCE_PLANE_POINT(plane, s2);
  141. if (_dis1 > -G_EPSILON && _dis2 > -G_EPSILON)
  142. {
  143. if (_dis1 < _dis2) return G_FRONT_PLANE_S1;
  144. return G_FRONT_PLANE_S2;
  145. }
  146. else if (_dis1 < G_EPSILON && _dis2 < G_EPSILON)
  147. {
  148. if (_dis1 > _dis2) return G_BACK_PLANE_S1;
  149. return G_BACK_PLANE_S2;
  150. }
  151. VEC_DIFF(clipped, s2, s1);
  152. _dis2 = VEC_DOT(clipped, plane);
  153. VEC_SCALE(clipped, -_dis1 / _dis2, clipped);
  154. VEC_SUM(clipped, clipped, s1);
  155. if (_dis1 < _dis2) return G_COLLIDE_PLANE_S1;
  156. return G_COLLIDE_PLANE_S2;
  157. }
  158. //! Confirms if the plane intersect the edge or not
  159. /*!
  160. clipped1 and clipped2 are the vertices behind the plane.
  161. clipped1 is the closest
  162. intersection_type must have the following values
  163. <ul>
  164. <li> 0 : Segment in front of plane, s1 closest
  165. <li> 1 : Segment in front of plane, s2 closest
  166. <li> 2 : Segment in back of plane, s1 closest
  167. <li> 3 : Segment in back of plane, s2 closest
  168. <li> 4 : Segment collides plane, s1 in back
  169. <li> 5 : Segment collides plane, s2 in back
  170. </ul>
  171. */
  172. template <typename CLASS_POINT, typename CLASS_PLANE>
  173. SIMD_FORCE_INLINE eLINE_PLANE_INTERSECTION_TYPE PLANE_CLIP_SEGMENT_CLOSEST(
  174. const CLASS_POINT &s1,
  175. const CLASS_POINT &s2,
  176. const CLASS_PLANE &plane,
  177. CLASS_POINT &clipped1, CLASS_POINT &clipped2)
  178. {
  179. eLINE_PLANE_INTERSECTION_TYPE intersection_type = PLANE_CLIP_SEGMENT2(s1, s2, plane, clipped1);
  180. switch (intersection_type)
  181. {
  182. case G_FRONT_PLANE_S1:
  183. VEC_COPY(clipped1, s1);
  184. VEC_COPY(clipped2, s2);
  185. break;
  186. case G_FRONT_PLANE_S2:
  187. VEC_COPY(clipped1, s2);
  188. VEC_COPY(clipped2, s1);
  189. break;
  190. case G_BACK_PLANE_S1:
  191. VEC_COPY(clipped1, s1);
  192. VEC_COPY(clipped2, s2);
  193. break;
  194. case G_BACK_PLANE_S2:
  195. VEC_COPY(clipped1, s2);
  196. VEC_COPY(clipped2, s1);
  197. break;
  198. case G_COLLIDE_PLANE_S1:
  199. VEC_COPY(clipped2, s1);
  200. break;
  201. case G_COLLIDE_PLANE_S2:
  202. VEC_COPY(clipped2, s2);
  203. break;
  204. }
  205. return intersection_type;
  206. }
  207. //! Finds the 2 smallest cartesian coordinates of a plane normal
  208. #define PLANE_MINOR_AXES(plane, i0, i1) VEC_MINOR_AXES(plane, i0, i1)
  209. //! Ray plane collision in one way
  210. /*!
  211. Intersects plane in one way only. The ray must face the plane (normals must be in opossite directions).<br/>
  212. It uses the PLANEDIREPSILON constant.
  213. */
  214. template <typename T, typename CLASS_POINT, typename CLASS_PLANE>
  215. SIMD_FORCE_INLINE bool RAY_PLANE_COLLISION(
  216. const CLASS_PLANE &plane,
  217. const CLASS_POINT &vDir,
  218. const CLASS_POINT &vPoint,
  219. CLASS_POINT &pout, T &tparam)
  220. {
  221. GREAL _dis, _dotdir;
  222. _dotdir = VEC_DOT(plane, vDir);
  223. if (_dotdir < PLANEDIREPSILON)
  224. {
  225. return false;
  226. }
  227. _dis = DISTANCE_PLANE_POINT(plane, vPoint);
  228. tparam = -_dis / _dotdir;
  229. VEC_SCALE(pout, tparam, vDir);
  230. VEC_SUM(pout, vPoint, pout);
  231. return true;
  232. }
  233. //! line collision
  234. /*!
  235. *\return
  236. -0 if the ray never intersects
  237. -1 if the ray collides in front
  238. -2 if the ray collides in back
  239. */
  240. template <typename T, typename CLASS_POINT, typename CLASS_PLANE>
  241. SIMD_FORCE_INLINE GUINT LINE_PLANE_COLLISION(
  242. const CLASS_PLANE &plane,
  243. const CLASS_POINT &vDir,
  244. const CLASS_POINT &vPoint,
  245. CLASS_POINT &pout,
  246. T &tparam,
  247. T tmin, T tmax)
  248. {
  249. GREAL _dis, _dotdir;
  250. _dotdir = VEC_DOT(plane, vDir);
  251. if (btFabs(_dotdir) < PLANEDIREPSILON)
  252. {
  253. tparam = tmax;
  254. return 0;
  255. }
  256. _dis = DISTANCE_PLANE_POINT(plane, vPoint);
  257. char returnvalue = _dis < 0.0f ? 2 : 1;
  258. tparam = -_dis / _dotdir;
  259. if (tparam < tmin)
  260. {
  261. returnvalue = 0;
  262. tparam = tmin;
  263. }
  264. else if (tparam > tmax)
  265. {
  266. returnvalue = 0;
  267. tparam = tmax;
  268. }
  269. VEC_SCALE(pout, tparam, vDir);
  270. VEC_SUM(pout, vPoint, pout);
  271. return returnvalue;
  272. }
  273. /*! \brief Returns the Ray on which 2 planes intersect if they do.
  274. Written by Rodrigo Hernandez on ODE convex collision
  275. \param p1 Plane 1
  276. \param p2 Plane 2
  277. \param p Contains the origin of the ray upon returning if planes intersect
  278. \param d Contains the direction of the ray upon returning if planes intersect
  279. \return true if the planes intersect, 0 if paralell.
  280. */
  281. template <typename CLASS_POINT, typename CLASS_PLANE>
  282. SIMD_FORCE_INLINE bool INTERSECT_PLANES(
  283. const CLASS_PLANE &p1,
  284. const CLASS_PLANE &p2,
  285. CLASS_POINT &p,
  286. CLASS_POINT &d)
  287. {
  288. VEC_CROSS(d, p1, p2);
  289. GREAL denom = VEC_DOT(d, d);
  290. if (GIM_IS_ZERO(denom)) return false;
  291. vec3f _n;
  292. _n[0] = p1[3] * p2[0] - p2[3] * p1[0];
  293. _n[1] = p1[3] * p2[1] - p2[3] * p1[1];
  294. _n[2] = p1[3] * p2[2] - p2[3] * p1[2];
  295. VEC_CROSS(p, _n, d);
  296. p[0] /= denom;
  297. p[1] /= denom;
  298. p[2] /= denom;
  299. return true;
  300. }
  301. //***************** SEGMENT and LINE FUNCTIONS **********************************///
  302. /*! Finds the closest point(cp) to (v) on a segment (e1,e2)
  303. */
  304. template <typename CLASS_POINT>
  305. SIMD_FORCE_INLINE void CLOSEST_POINT_ON_SEGMENT(
  306. CLASS_POINT &cp, const CLASS_POINT &v,
  307. const CLASS_POINT &e1, const CLASS_POINT &e2)
  308. {
  309. vec3f _n;
  310. VEC_DIFF(_n, e2, e1);
  311. VEC_DIFF(cp, v, e1);
  312. GREAL _scalar = VEC_DOT(cp, _n);
  313. _scalar /= VEC_DOT(_n, _n);
  314. if (_scalar < 0.0f)
  315. {
  316. VEC_COPY(cp, e1);
  317. }
  318. else if (_scalar > 1.0f)
  319. {
  320. VEC_COPY(cp, e2);
  321. }
  322. else
  323. {
  324. VEC_SCALE(cp, _scalar, _n);
  325. VEC_SUM(cp, cp, e1);
  326. }
  327. }
  328. /*! \brief Finds the line params where these lines intersect.
  329. \param dir1 Direction of line 1
  330. \param point1 Point of line 1
  331. \param dir2 Direction of line 2
  332. \param point2 Point of line 2
  333. \param t1 Result Parameter for line 1
  334. \param t2 Result Parameter for line 2
  335. \param dointersect 0 if the lines won't intersect, else 1
  336. */
  337. template <typename T, typename CLASS_POINT>
  338. SIMD_FORCE_INLINE bool LINE_INTERSECTION_PARAMS(
  339. const CLASS_POINT &dir1,
  340. CLASS_POINT &point1,
  341. const CLASS_POINT &dir2,
  342. CLASS_POINT &point2,
  343. T &t1, T &t2)
  344. {
  345. GREAL det;
  346. GREAL e1e1 = VEC_DOT(dir1, dir1);
  347. GREAL e1e2 = VEC_DOT(dir1, dir2);
  348. GREAL e2e2 = VEC_DOT(dir2, dir2);
  349. vec3f p1p2;
  350. VEC_DIFF(p1p2, point1, point2);
  351. GREAL p1p2e1 = VEC_DOT(p1p2, dir1);
  352. GREAL p1p2e2 = VEC_DOT(p1p2, dir2);
  353. det = e1e2 * e1e2 - e1e1 * e2e2;
  354. if (GIM_IS_ZERO(det)) return false;
  355. t1 = (e1e2 * p1p2e2 - e2e2 * p1p2e1) / det;
  356. t2 = (e1e1 * p1p2e2 - e1e2 * p1p2e1) / det;
  357. return true;
  358. }
  359. //! Find closest points on segments
  360. template <typename CLASS_POINT>
  361. SIMD_FORCE_INLINE void SEGMENT_COLLISION(
  362. const CLASS_POINT &vA1,
  363. const CLASS_POINT &vA2,
  364. const CLASS_POINT &vB1,
  365. const CLASS_POINT &vB2,
  366. CLASS_POINT &vPointA,
  367. CLASS_POINT &vPointB)
  368. {
  369. CLASS_POINT _AD, _BD, n;
  370. vec4f _M; //plane
  371. VEC_DIFF(_AD, vA2, vA1);
  372. VEC_DIFF(_BD, vB2, vB1);
  373. VEC_CROSS(n, _AD, _BD);
  374. GREAL _tp = VEC_DOT(n, n);
  375. if (_tp < G_EPSILON) //ARE PARALELE
  376. {
  377. //project B over A
  378. bool invert_b_order = false;
  379. _M[0] = VEC_DOT(vB1, _AD);
  380. _M[1] = VEC_DOT(vB2, _AD);
  381. if (_M[0] > _M[1])
  382. {
  383. invert_b_order = true;
  384. GIM_SWAP_NUMBERS(_M[0], _M[1]);
  385. }
  386. _M[2] = VEC_DOT(vA1, _AD);
  387. _M[3] = VEC_DOT(vA2, _AD);
  388. //mid points
  389. n[0] = (_M[0] + _M[1]) * 0.5f;
  390. n[1] = (_M[2] + _M[3]) * 0.5f;
  391. if (n[0] < n[1])
  392. {
  393. if (_M[1] < _M[2])
  394. {
  395. vPointB = invert_b_order ? vB1 : vB2;
  396. vPointA = vA1;
  397. }
  398. else if (_M[1] < _M[3])
  399. {
  400. vPointB = invert_b_order ? vB1 : vB2;
  401. CLOSEST_POINT_ON_SEGMENT(vPointA, vPointB, vA1, vA2);
  402. }
  403. else
  404. {
  405. vPointA = vA2;
  406. CLOSEST_POINT_ON_SEGMENT(vPointB, vPointA, vB1, vB2);
  407. }
  408. }
  409. else
  410. {
  411. if (_M[3] < _M[0])
  412. {
  413. vPointB = invert_b_order ? vB2 : vB1;
  414. vPointA = vA2;
  415. }
  416. else if (_M[3] < _M[1])
  417. {
  418. vPointA = vA2;
  419. CLOSEST_POINT_ON_SEGMENT(vPointB, vPointA, vB1, vB2);
  420. }
  421. else
  422. {
  423. vPointB = invert_b_order ? vB1 : vB2;
  424. CLOSEST_POINT_ON_SEGMENT(vPointA, vPointB, vA1, vA2);
  425. }
  426. }
  427. return;
  428. }
  429. VEC_CROSS(_M, n, _BD);
  430. _M[3] = VEC_DOT(_M, vB1);
  431. LINE_PLANE_COLLISION(_M, _AD, vA1, vPointA, _tp, btScalar(0), btScalar(1));
  432. /*Closest point on segment*/
  433. VEC_DIFF(vPointB, vPointA, vB1);
  434. _tp = VEC_DOT(vPointB, _BD);
  435. _tp /= VEC_DOT(_BD, _BD);
  436. _tp = GIM_CLAMP(_tp, 0.0f, 1.0f);
  437. VEC_SCALE(vPointB, _tp, _BD);
  438. VEC_SUM(vPointB, vPointB, vB1);
  439. }
  440. //! Line box intersection in one dimension
  441. /*!
  442. *\param pos Position of the ray
  443. *\param dir Projection of the Direction of the ray
  444. *\param bmin Minimum bound of the box
  445. *\param bmax Maximum bound of the box
  446. *\param tfirst the minimum projection. Assign to 0 at first.
  447. *\param tlast the maximum projection. Assign to INFINITY at first.
  448. *\return true if there is an intersection.
  449. */
  450. template <typename T>
  451. SIMD_FORCE_INLINE bool BOX_AXIS_INTERSECT(T pos, T dir, T bmin, T bmax, T &tfirst, T &tlast)
  452. {
  453. if (GIM_IS_ZERO(dir))
  454. {
  455. return !(pos < bmin || pos > bmax);
  456. }
  457. GREAL a0 = (bmin - pos) / dir;
  458. GREAL a1 = (bmax - pos) / dir;
  459. if (a0 > a1) GIM_SWAP_NUMBERS(a0, a1);
  460. tfirst = GIM_MAX(a0, tfirst);
  461. tlast = GIM_MIN(a1, tlast);
  462. if (tlast < tfirst) return false;
  463. return true;
  464. }
  465. //! Sorts 3 componets
  466. template <typename T>
  467. SIMD_FORCE_INLINE void SORT_3_INDICES(
  468. const T *values,
  469. GUINT *order_indices)
  470. {
  471. //get minimum
  472. order_indices[0] = values[0] < values[1] ? (values[0] < values[2] ? 0 : 2) : (values[1] < values[2] ? 1 : 2);
  473. //get second and third
  474. GUINT i0 = (order_indices[0] + 1) % 3;
  475. GUINT i1 = (i0 + 1) % 3;
  476. if (values[i0] < values[i1])
  477. {
  478. order_indices[1] = i0;
  479. order_indices[2] = i1;
  480. }
  481. else
  482. {
  483. order_indices[1] = i1;
  484. order_indices[2] = i0;
  485. }
  486. }
  487. #endif // GIM_VECTOR_H_INCLUDED