OPC_TriTriOverlap.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //! if OPC_TRITRI_EPSILON_TEST is true then we do a check (if |dv|<EPSILON then dv=0.0;) else no check is done (which is less robust, but faster)
  2. #define LOCAL_EPSILON 0.000001f
  3. //! sort so that a<=b
  4. #define SORT(a,b) \
  5. if(a>b) \
  6. { \
  7. const float c=a; \
  8. a=b; \
  9. b=c; \
  10. }
  11. //! Edge to edge test based on Franlin Antonio's gem: "Faster Line Segment Intersection", in Graphics Gems III, pp. 199-202
  12. #define EDGE_EDGE_TEST(V0, U0, U1) \
  13. Bx = U0[i0] - U1[i0]; \
  14. By = U0[i1] - U1[i1]; \
  15. Cx = V0[i0] - U0[i0]; \
  16. Cy = V0[i1] - U0[i1]; \
  17. f = Ay*Bx - Ax*By; \
  18. d = By*Cx - Bx*Cy; \
  19. if((f>0.0f && d>=0.0f && d<=f) || (f<0.0f && d<=0.0f && d>=f)) \
  20. { \
  21. const float e=Ax*Cy - Ay*Cx; \
  22. if(f>0.0f) \
  23. { \
  24. if(e>=0.0f && e<=f) return TRUE; \
  25. } \
  26. else \
  27. { \
  28. if(e<=0.0f && e>=f) return TRUE; \
  29. } \
  30. }
  31. //! TO BE DOCUMENTED
  32. #define EDGE_AGAINST_TRI_EDGES(V0, V1, U0, U1, U2) \
  33. { \
  34. float Bx,By,Cx,Cy,d,f; \
  35. const float Ax = V1[i0] - V0[i0]; \
  36. const float Ay = V1[i1] - V0[i1]; \
  37. /* test edge U0,U1 against V0,V1 */ \
  38. EDGE_EDGE_TEST(V0, U0, U1); \
  39. /* test edge U1,U2 against V0,V1 */ \
  40. EDGE_EDGE_TEST(V0, U1, U2); \
  41. /* test edge U2,U1 against V0,V1 */ \
  42. EDGE_EDGE_TEST(V0, U2, U0); \
  43. }
  44. //! TO BE DOCUMENTED
  45. #define POINT_IN_TRI(V0, U0, U1, U2) \
  46. { \
  47. /* is T1 completly inside T2? */ \
  48. /* check if V0 is inside tri(U0,U1,U2) */ \
  49. float a = U1[i1] - U0[i1]; \
  50. float b = -(U1[i0] - U0[i0]); \
  51. float c = -a*U0[i0] - b*U0[i1]; \
  52. float d0 = a*V0[i0] + b*V0[i1] + c; \
  53. \
  54. a = U2[i1] - U1[i1]; \
  55. b = -(U2[i0] - U1[i0]); \
  56. c = -a*U1[i0] - b*U1[i1]; \
  57. const float d1 = a*V0[i0] + b*V0[i1] + c; \
  58. \
  59. a = U0[i1] - U2[i1]; \
  60. b = -(U0[i0] - U2[i0]); \
  61. c = -a*U2[i0] - b*U2[i1]; \
  62. const float d2 = a*V0[i0] + b*V0[i1] + c; \
  63. if(d0*d1>0.0f) \
  64. { \
  65. if(d0*d2>0.0f) return TRUE; \
  66. } \
  67. }
  68. //! TO BE DOCUMENTED
  69. BOOL CoplanarTriTri(const Point& n, const Point& v0, const Point& v1, const Point& v2, const Point& u0, const Point& u1, const Point& u2)
  70. {
  71. float A[3];
  72. short i0,i1;
  73. /* first project onto an axis-aligned plane, that maximizes the area */
  74. /* of the triangles, compute indices: i0,i1. */
  75. A[0] = fabsf(n[0]);
  76. A[1] = fabsf(n[1]);
  77. A[2] = fabsf(n[2]);
  78. if(A[0]>A[1])
  79. {
  80. if(A[0]>A[2])
  81. {
  82. i0=1; /* A[0] is greatest */
  83. i1=2;
  84. }
  85. else
  86. {
  87. i0=0; /* A[2] is greatest */
  88. i1=1;
  89. }
  90. }
  91. else /* A[0]<=A[1] */
  92. {
  93. if(A[2]>A[1])
  94. {
  95. i0=0; /* A[2] is greatest */
  96. i1=1;
  97. }
  98. else
  99. {
  100. i0=0; /* A[1] is greatest */
  101. i1=2;
  102. }
  103. }
  104. /* test all edges of triangle 1 against the edges of triangle 2 */
  105. EDGE_AGAINST_TRI_EDGES(v0, v1, u0, u1, u2);
  106. EDGE_AGAINST_TRI_EDGES(v1, v2, u0, u1, u2);
  107. EDGE_AGAINST_TRI_EDGES(v2, v0, u0, u1, u2);
  108. /* finally, test if tri1 is totally contained in tri2 or vice versa */
  109. POINT_IN_TRI(v0, u0, u1, u2);
  110. POINT_IN_TRI(u0, v0, v1, v2);
  111. return FALSE;
  112. }
  113. //! TO BE DOCUMENTED
  114. #define NEWCOMPUTE_INTERVALS(VV0, VV1, VV2, D0, D1, D2, D0D1, D0D2, A, B, C, X0, X1) \
  115. { \
  116. if(D0D1>0.0f) \
  117. { \
  118. /* here we know that D0D2<=0.0 */ \
  119. /* that is D0, D1 are on the same side, D2 on the other or on the plane */ \
  120. A=VV2; B=(VV0 - VV2)*D2; C=(VV1 - VV2)*D2; X0=D2 - D0; X1=D2 - D1; \
  121. } \
  122. else if(D0D2>0.0f) \
  123. { \
  124. /* here we know that d0d1<=0.0 */ \
  125. A=VV1; B=(VV0 - VV1)*D1; C=(VV2 - VV1)*D1; X0=D1 - D0; X1=D1 - D2; \
  126. } \
  127. else if(D1*D2>0.0f || D0!=0.0f) \
  128. { \
  129. /* here we know that d0d1<=0.0 or that D0!=0.0 */ \
  130. A=VV0; B=(VV1 - VV0)*D0; C=(VV2 - VV0)*D0; X0=D0 - D1; X1=D0 - D2; \
  131. } \
  132. else if(D1!=0.0f) \
  133. { \
  134. A=VV1; B=(VV0 - VV1)*D1; C=(VV2 - VV1)*D1; X0=D1 - D0; X1=D1 - D2; \
  135. } \
  136. else if(D2!=0.0f) \
  137. { \
  138. A=VV2; B=(VV0 - VV2)*D2; C=(VV1 - VV2)*D2; X0=D2 - D0; X1=D2 - D1; \
  139. } \
  140. else \
  141. { \
  142. /* triangles are coplanar */ \
  143. return CoplanarTriTri(N1, V0, V1, V2, U0, U1, U2); \
  144. } \
  145. }
  146. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  147. /**
  148. * Triangle/triangle intersection test routine,
  149. * by Tomas Moller, 1997.
  150. * See article "A Fast Triangle-Triangle Intersection Test",
  151. * Journal of Graphics Tools, 2(2), 1997
  152. *
  153. * Updated June 1999: removed the divisions -- a little faster now!
  154. * Updated October 1999: added {} to CROSS and SUB macros
  155. *
  156. * int NoDivTriTriIsect(float V0[3],float V1[3],float V2[3],
  157. * float U0[3],float U1[3],float U2[3])
  158. *
  159. * \param V0 [in] triangle 0, vertex 0
  160. * \param V1 [in] triangle 0, vertex 1
  161. * \param V2 [in] triangle 0, vertex 2
  162. * \param U0 [in] triangle 1, vertex 0
  163. * \param U1 [in] triangle 1, vertex 1
  164. * \param U2 [in] triangle 1, vertex 2
  165. * \return true if triangles overlap
  166. */
  167. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  168. inline_ BOOL AABBTreeCollider::TriTriOverlap(const Point& V0, const Point& V1, const Point& V2, const Point& U0, const Point& U1, const Point& U2)
  169. {
  170. // Stats
  171. mNbPrimPrimTests++;
  172. // Compute plane equation of triangle(V0,V1,V2)
  173. Point E1 = V1 - V0;
  174. Point E2 = V2 - V0;
  175. const Point N1 = E1 ^ E2;
  176. const float d1 =-N1 | V0;
  177. // Plane equation 1: N1.X+d1=0
  178. // Put U0,U1,U2 into plane equation 1 to compute signed distances to the plane
  179. float du0 = (N1|U0) + d1;
  180. float du1 = (N1|U1) + d1;
  181. float du2 = (N1|U2) + d1;
  182. // Coplanarity robustness check
  183. #ifdef OPC_TRITRI_EPSILON_TEST
  184. if(fabsf(du0)<LOCAL_EPSILON) du0 = 0.0f;
  185. if(fabsf(du1)<LOCAL_EPSILON) du1 = 0.0f;
  186. if(fabsf(du2)<LOCAL_EPSILON) du2 = 0.0f;
  187. #endif
  188. const float du0du1 = du0 * du1;
  189. const float du0du2 = du0 * du2;
  190. if(du0du1>0.0f && du0du2>0.0f) // same sign on all of them + not equal 0 ?
  191. return FALSE; // no intersection occurs
  192. // Compute plane of triangle (U0,U1,U2)
  193. E1 = U1 - U0;
  194. E2 = U2 - U0;
  195. const Point N2 = E1 ^ E2;
  196. const float d2=-N2 | U0;
  197. // plane equation 2: N2.X+d2=0
  198. // put V0,V1,V2 into plane equation 2
  199. float dv0 = (N2|V0) + d2;
  200. float dv1 = (N2|V1) + d2;
  201. float dv2 = (N2|V2) + d2;
  202. #ifdef OPC_TRITRI_EPSILON_TEST
  203. if(fabsf(dv0)<LOCAL_EPSILON) dv0 = 0.0f;
  204. if(fabsf(dv1)<LOCAL_EPSILON) dv1 = 0.0f;
  205. if(fabsf(dv2)<LOCAL_EPSILON) dv2 = 0.0f;
  206. #endif
  207. const float dv0dv1 = dv0 * dv1;
  208. const float dv0dv2 = dv0 * dv2;
  209. if(dv0dv1>0.0f && dv0dv2>0.0f) // same sign on all of them + not equal 0 ?
  210. return FALSE; // no intersection occurs
  211. // Compute direction of intersection line
  212. const Point D = N1^N2;
  213. // Compute and index to the largest component of D
  214. float max=fabsf(D[0]);
  215. short index=0;
  216. float bb=fabsf(D[1]);
  217. float cc=fabsf(D[2]);
  218. if(bb>max) max=bb,index=1;
  219. if(cc>max) max=cc,index=2;
  220. // This is the simplified projection onto L
  221. const float vp0 = V0[index];
  222. const float vp1 = V1[index];
  223. const float vp2 = V2[index];
  224. const float up0 = U0[index];
  225. const float up1 = U1[index];
  226. const float up2 = U2[index];
  227. // Compute interval for triangle 1
  228. float a,b,c,x0,x1;
  229. NEWCOMPUTE_INTERVALS(vp0,vp1,vp2,dv0,dv1,dv2,dv0dv1,dv0dv2,a,b,c,x0,x1);
  230. // Compute interval for triangle 2
  231. float d,e,f,y0,y1;
  232. NEWCOMPUTE_INTERVALS(up0,up1,up2,du0,du1,du2,du0du1,du0du2,d,e,f,y0,y1);
  233. const float xx=x0*x1;
  234. const float yy=y0*y1;
  235. const float xxyy=xx*yy;
  236. float isect1[2], isect2[2];
  237. float tmp=a*xxyy;
  238. isect1[0]=tmp+b*x1*yy;
  239. isect1[1]=tmp+c*x0*yy;
  240. tmp=d*xxyy;
  241. isect2[0]=tmp+e*xx*y1;
  242. isect2[1]=tmp+f*xx*y0;
  243. SORT(isect1[0],isect1[1]);
  244. SORT(isect2[0],isect2[1]);
  245. if(isect1[1]<isect2[0] || isect2[1]<isect1[0]) return FALSE;
  246. return TRUE;
  247. }