tri_coll_test.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* Triangle/triangle intersection test routine,
  2. * by Tomas Moller, 1997.
  3. * See article "A Fast Triangle-Triangle Intersection Test",
  4. * Journal of Graphics Tools, 2(2), 1997
  5. *
  6. * int tri_tri_intersect(float V0[3],float V1[3],float V2[3],
  7. * float U0[3],float U1[3],float U2[3])
  8. *
  9. * parameters: vertices of triangle 1: V0,V1,V2
  10. * vertices of triangle 2: U0,U1,U2
  11. * result : returns 1 if the triangles intersect, otherwise 0
  12. *
  13. */
  14. // leave this at the top for PCH reasons...
  15. #include "common_headers.h"
  16. #include <math.h>
  17. #include "../game/q_shared.h"
  18. #include "../game/g_local.h"
  19. /* if USE_EPSILON_TEST is true then we do a check:
  20. if |dv|<EPSILON then dv=0.0;
  21. else no check is done (which is less robust)
  22. */
  23. #define USE_EPSILON_TEST 1
  24. #define EPSILON 0.000001
  25. /* some macros */
  26. #define CROSS(dest,v1,v2) \
  27. dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
  28. dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
  29. dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
  30. #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
  31. #define SUB(dest,v1,v2) \
  32. dest[0]=v1[0]-v2[0]; \
  33. dest[1]=v1[1]-v2[1]; \
  34. dest[2]=v1[2]-v2[2];
  35. /* sort so that a<=b */
  36. #define SORT(a,b) \
  37. if(a>b) \
  38. { \
  39. float c; \
  40. c=a; \
  41. a=b; \
  42. b=c; \
  43. }
  44. #define ISECT(VV0,VV1,VV2,D0,D1,D2,isect0,isect1) \
  45. isect0=VV0+(VV1-VV0)*D0/(D0-D1); \
  46. isect1=VV0+(VV2-VV0)*D0/(D0-D2);
  47. #define COMPUTE_INTERVALS(VV0,VV1,VV2,D0,D1,D2,D0D1,D0D2,isect0,isect1) \
  48. if(D0D1>0.0f) \
  49. { \
  50. /* here we know that D0D2<=0.0 */ \
  51. /* that is D0, D1 are on the same side, D2 on the other or on the plane */ \
  52. ISECT(VV2,VV0,VV1,D2,D0,D1,isect0,isect1); \
  53. } \
  54. else if(D0D2>0.0f) \
  55. { \
  56. /* here we know that d0d1<=0.0 */ \
  57. ISECT(VV1,VV0,VV2,D1,D0,D2,isect0,isect1); \
  58. } \
  59. else if(D1*D2>0.0f || D0!=0.0f) \
  60. { \
  61. /* here we know that d0d1<=0.0 or that D0!=0.0 */ \
  62. ISECT(VV0,VV1,VV2,D0,D1,D2,isect0,isect1); \
  63. } \
  64. else if(D1!=0.0f) \
  65. { \
  66. ISECT(VV1,VV0,VV2,D1,D0,D2,isect0,isect1); \
  67. } \
  68. else if(D2!=0.0f) \
  69. { \
  70. ISECT(VV2,VV0,VV1,D2,D0,D1,isect0,isect1); \
  71. } \
  72. else \
  73. { \
  74. /* triangles are coplanar */ \
  75. return coplanar_tri_tri(N1,V0,V1,V2,U0,U1,U2); \
  76. }
  77. /* this edge to edge test is based on Franlin Antonio's gem:
  78. "Faster Line Segment Intersection", in Graphics Gems III,
  79. pp. 199-202 */
  80. #define EDGE_EDGE_TEST(V0,U0,U1) \
  81. Bx=U0[i0]-U1[i0]; \
  82. By=U0[i1]-U1[i1]; \
  83. Cx=V0[i0]-U0[i0]; \
  84. Cy=V0[i1]-U0[i1]; \
  85. f=Ay*Bx-Ax*By; \
  86. d=By*Cx-Bx*Cy; \
  87. if((f>0 && d>=0 && d<=f) || (f<0 && d<=0 && d>=f)) \
  88. { \
  89. e=Ax*Cy-Ay*Cx; \
  90. if(f>0) \
  91. { \
  92. if(e>=0 && e<=f) return 1; \
  93. } \
  94. else \
  95. { \
  96. if(e<=0 && e>=f) return 1; \
  97. } \
  98. }
  99. #define EDGE_AGAINST_TRI_EDGES(V0,V1,U0,U1,U2) \
  100. { \
  101. float Ax,Ay,Bx,By,Cx,Cy,e,d,f; \
  102. Ax=V1[i0]-V0[i0]; \
  103. Ay=V1[i1]-V0[i1]; \
  104. /* test edge U0,U1 against V0,V1 */ \
  105. EDGE_EDGE_TEST(V0,U0,U1); \
  106. /* test edge U1,U2 against V0,V1 */ \
  107. EDGE_EDGE_TEST(V0,U1,U2); \
  108. /* test edge U2,U1 against V0,V1 */ \
  109. EDGE_EDGE_TEST(V0,U2,U0); \
  110. }
  111. #define POINT_IN_TRI(V0,U0,U1,U2) \
  112. { \
  113. float a,b,c,d0,d1,d2; \
  114. /* is T1 completly inside T2? */ \
  115. /* check if V0 is inside tri(U0,U1,U2) */ \
  116. a=U1[i1]-U0[i1]; \
  117. b=-(U1[i0]-U0[i0]); \
  118. c=-a*U0[i0]-b*U0[i1]; \
  119. d0=a*V0[i0]+b*V0[i1]+c; \
  120. \
  121. a=U2[i1]-U1[i1]; \
  122. b=-(U2[i0]-U1[i0]); \
  123. c=-a*U1[i0]-b*U1[i1]; \
  124. d1=a*V0[i0]+b*V0[i1]+c; \
  125. \
  126. a=U0[i1]-U2[i1]; \
  127. b=-(U0[i0]-U2[i0]); \
  128. c=-a*U2[i0]-b*U2[i1]; \
  129. d2=a*V0[i0]+b*V0[i1]+c; \
  130. if(d0*d1>0.0) \
  131. { \
  132. if(d0*d2>0.0) return 1; \
  133. } \
  134. }
  135. qboolean coplanar_tri_tri(vec3_t N,vec3_t V0,vec3_t V1,vec3_t V2,
  136. vec3_t U0,vec3_t U1,vec3_t U2)
  137. {
  138. vec3_t A;
  139. short i0,i1;
  140. /* first project onto an axis-aligned plane, that maximizes the area */
  141. /* of the triangles, compute indices: i0,i1. */
  142. A[0]=fabs(N[0]);
  143. A[1]=fabs(N[1]);
  144. A[2]=fabs(N[2]);
  145. if(A[0]>A[1])
  146. {
  147. if(A[0]>A[2])
  148. {
  149. i0=1; /* A[0] is greatest */
  150. i1=2;
  151. }
  152. else
  153. {
  154. i0=0; /* A[2] is greatest */
  155. i1=1;
  156. }
  157. }
  158. else /* A[0]<=A[1] */
  159. {
  160. if(A[2]>A[1])
  161. {
  162. i0=0; /* A[2] is greatest */
  163. i1=1;
  164. }
  165. else
  166. {
  167. i0=0; /* A[1] is greatest */
  168. i1=2;
  169. }
  170. }
  171. /* test all edges of triangle 1 against the edges of triangle 2 */
  172. EDGE_AGAINST_TRI_EDGES(V0,V1,U0,U1,U2);
  173. EDGE_AGAINST_TRI_EDGES(V1,V2,U0,U1,U2);
  174. EDGE_AGAINST_TRI_EDGES(V2,V0,U0,U1,U2);
  175. /* finally, test if tri1 is totally contained in tri2 or vice versa */
  176. POINT_IN_TRI(V0,U0,U1,U2);
  177. POINT_IN_TRI(U0,V0,V1,V2);
  178. return qfalse;
  179. }
  180. qboolean tri_tri_intersect(vec3_t V0,vec3_t V1,vec3_t V2,
  181. vec3_t U0,vec3_t U1,vec3_t U2)
  182. {
  183. vec3_t E1,E2;
  184. vec3_t N1,N2;
  185. float d1,d2;
  186. float du0,du1,du2,dv0,dv1,dv2;
  187. vec3_t D;
  188. float isect1[2], isect2[2];
  189. float du0du1,du0du2,dv0dv1,dv0dv2;
  190. short index;
  191. float vp0,vp1,vp2;
  192. float up0,up1,up2;
  193. float b,c,max;
  194. /* compute plane equation of triangle(V0,V1,V2) */
  195. SUB(E1,V1,V0);
  196. SUB(E2,V2,V0);
  197. CROSS(N1,E1,E2);
  198. d1=-DOT(N1,V0);
  199. /* plane equation 1: N1.X+d1=0 */
  200. /* put U0,U1,U2 into plane equation 1 to compute signed distances to the plane*/
  201. du0=DOT(N1,U0)+d1;
  202. du1=DOT(N1,U1)+d1;
  203. du2=DOT(N1,U2)+d1;
  204. /* coplanarity robustness check */
  205. #if USE_EPSILON_TEST
  206. if(fabs(du0)<EPSILON) du0=0.0;
  207. if(fabs(du1)<EPSILON) du1=0.0;
  208. if(fabs(du2)<EPSILON) du2=0.0;
  209. #endif
  210. du0du1=du0*du1;
  211. du0du2=du0*du2;
  212. if(du0du1>0.0f && du0du2>0.0f) /* same sign on all of them + not equal 0 ? */
  213. return 0; /* no intersection occurs */
  214. /* compute plane of triangle (U0,U1,U2) */
  215. SUB(E1,U1,U0);
  216. SUB(E2,U2,U0);
  217. CROSS(N2,E1,E2);
  218. d2=-DOT(N2,U0);
  219. /* plane equation 2: N2.X+d2=0 */
  220. /* put V0,V1,V2 into plane equation 2 */
  221. dv0=DOT(N2,V0)+d2;
  222. dv1=DOT(N2,V1)+d2;
  223. dv2=DOT(N2,V2)+d2;
  224. #if USE_EPSILON_TEST
  225. if(fabs(dv0)<EPSILON) dv0=0.0;
  226. if(fabs(dv1)<EPSILON) dv1=0.0;
  227. if(fabs(dv2)<EPSILON) dv2=0.0;
  228. #endif
  229. dv0dv1=dv0*dv1;
  230. dv0dv2=dv0*dv2;
  231. if(dv0dv1>0.0f && dv0dv2>0.0f) /* same sign on all of them + not equal 0 ? */
  232. return 0; /* no intersection occurs */
  233. /* compute direction of intersection line */
  234. CROSS(D,N1,N2);
  235. /* compute and index to the largest component of D */
  236. max=fabs(D[0]);
  237. index=0;
  238. b=fabs(D[1]);
  239. c=fabs(D[2]);
  240. if(b>max) max=b,index=1;
  241. if(c>max) max=c,index=2;
  242. /* this is the simplified projection onto L*/
  243. vp0=V0[index];
  244. vp1=V1[index];
  245. vp2=V2[index];
  246. up0=U0[index];
  247. up1=U1[index];
  248. up2=U2[index];
  249. /* compute interval for triangle 1 */
  250. COMPUTE_INTERVALS(vp0,vp1,vp2,dv0,dv1,dv2,dv0dv1,dv0dv2,isect1[0],isect1[1]);
  251. /* compute interval for triangle 2 */
  252. COMPUTE_INTERVALS(up0,up1,up2,du0,du1,du2,du0du1,du0du2,isect2[0],isect2[1]);
  253. SORT(isect1[0],isect1[1]);
  254. SORT(isect2[0],isect2[1]);
  255. if(isect1[1]<isect2[0] || isect2[1]<isect1[0]) return qtrue;
  256. return qfalse;
  257. }
  258. float LineSegmentDistance( vec3_t a, vec3_t b, vec3_t c, vec3_t d )
  259. {
  260. vec3_t v1, v2, v3, cross;
  261. //FIXME: what if parallel or intersect?
  262. //FIXME: this doesn't take into account the endpoints...
  263. //get the two lines
  264. VectorSubtract( b, a, v1 );
  265. VectorSubtract( c, d, v2 );
  266. //get their normalized cross product
  267. CrossProduct( v1, v2, cross );
  268. /*
  269. float crossLength = VectorLength( cross );
  270. if ( crossLength == 0 )
  271. {//intersect! Or... parallel?
  272. return 0;
  273. }
  274. VectorScale( cross, 1/crossLength, cross );
  275. */
  276. VectorNormalize( cross );
  277. //now get a vector from v1 to v2
  278. VectorSubtract( d, a, v3 );
  279. //distance is dot product of that new vector and the normalized cross product
  280. float dist = fabs( DotProduct( v3, cross ) );
  281. return dist;
  282. }
  283. extern qboolean G_FindClosestPointOnLineSegment( const vec3_t start, const vec3_t end, const vec3_t from, vec3_t result );
  284. float ShortestLineSegBewteen2LineSegs( vec3_t start1, vec3_t end1, vec3_t start2, vec3_t end2, vec3_t close_pnt1, vec3_t close_pnt2 )
  285. {
  286. float current_dist, new_dist;
  287. vec3_t new_pnt;
  288. //start1, end1 : the first segment
  289. //start2, end2 : the second segment
  290. //output, one point on each segment, the closest two points on the segments.
  291. //compute some temporaries:
  292. //vec start_dif = start2 - start1
  293. vec3_t start_dif;
  294. VectorSubtract( start2, start1, start_dif );
  295. //vec v1 = end1 - start1
  296. vec3_t v1;
  297. VectorSubtract( end1, start1, v1 );
  298. //vec v2 = end2 - start2
  299. vec3_t v2;
  300. VectorSubtract( end2, start2, v2 );
  301. //
  302. float v1v1 = DotProduct( v1, v1 );
  303. float v2v2 = DotProduct( v2, v2 );
  304. float v1v2 = DotProduct( v1, v2 );
  305. //the main computation
  306. float denom = (v1v2 * v1v2) - (v1v1 * v2v2);
  307. //if denom is small, then skip all this and jump to the section marked below
  308. if ( fabs(denom) > 0.001f )
  309. {
  310. float s = -( (v2v2*DotProduct( v1, start_dif )) - (v1v2*DotProduct( v2, start_dif )) ) / denom;
  311. float t = ( (v1v1*DotProduct( v2, start_dif )) - (v1v2*DotProduct( v1, start_dif )) ) / denom;
  312. qboolean done = qtrue;
  313. if ( s < 0 )
  314. {
  315. done = qfalse;
  316. s = 0;// and see note below
  317. }
  318. if ( s > 1 )
  319. {
  320. done = qfalse;
  321. s = 1;// and see note below
  322. }
  323. if ( t < 0 )
  324. {
  325. done = qfalse;
  326. t = 0;// and see note below
  327. }
  328. if ( t > 1 )
  329. {
  330. done = qfalse;
  331. t = 1;// and see note below
  332. }
  333. //vec close_pnt1 = start1 + s * v1
  334. VectorMA( start1, s, v1, close_pnt1 );
  335. //vec close_pnt2 = start2 + t * v2
  336. VectorMA( start2, t, v2, close_pnt2 );
  337. current_dist = Distance( close_pnt1, close_pnt2 );
  338. //now, if none of those if's fired, you are done.
  339. if ( done )
  340. {
  341. return current_dist;
  342. }
  343. //If they did fire, then we need to do some additional tests.
  344. //What we are gonna do is see if we can find a shorter distance than the above
  345. //involving the endpoints.
  346. }
  347. else
  348. {
  349. //******start here for paralell lines with current_dist = infinity****
  350. current_dist = Q3_INFINITE;
  351. }
  352. //test 2 close_pnts first
  353. /*
  354. G_FindClosestPointOnLineSegment( start1, end1, close_pnt2, new_pnt );
  355. new_dist = Distance( close_pnt2, new_pnt );
  356. if ( new_dist < current_dist )
  357. {//then update close_pnt1 close_pnt2 and current_dist
  358. VectorCopy( new_pnt, close_pnt1 );
  359. VectorCopy( close_pnt2, close_pnt2 );
  360. current_dist = new_dist;
  361. }
  362. G_FindClosestPointOnLineSegment( start2, end2, close_pnt1, new_pnt );
  363. new_dist = Distance( close_pnt1, new_pnt );
  364. if ( new_dist < current_dist )
  365. {//then update close_pnt1 close_pnt2 and current_dist
  366. VectorCopy( close_pnt1, close_pnt1 );
  367. VectorCopy( new_pnt, close_pnt2 );
  368. current_dist = new_dist;
  369. }
  370. */
  371. //test all the endpoints
  372. new_dist = Distance( start1, start2 );
  373. if ( new_dist < current_dist )
  374. {//then update close_pnt1 close_pnt2 and current_dist
  375. VectorCopy( start1, close_pnt1 );
  376. VectorCopy( start2, close_pnt2 );
  377. current_dist = new_dist;
  378. }
  379. new_dist = Distance( start1, end2 );
  380. if ( new_dist < current_dist )
  381. {//then update close_pnt1 close_pnt2 and current_dist
  382. VectorCopy( start1, close_pnt1 );
  383. VectorCopy( end2, close_pnt2 );
  384. current_dist = new_dist;
  385. }
  386. new_dist = Distance( end1, start2 );
  387. if ( new_dist < current_dist )
  388. {//then update close_pnt1 close_pnt2 and current_dist
  389. VectorCopy( end1, close_pnt1 );
  390. VectorCopy( start2, close_pnt2 );
  391. current_dist = new_dist;
  392. }
  393. new_dist = Distance( end1, end2 );
  394. if ( new_dist < current_dist )
  395. {//then update close_pnt1 close_pnt2 and current_dist
  396. VectorCopy( end1, close_pnt1 );
  397. VectorCopy( end2, close_pnt2 );
  398. current_dist = new_dist;
  399. }
  400. //Then we have 4 more point / segment tests
  401. G_FindClosestPointOnLineSegment( start2, end2, start1, new_pnt );
  402. new_dist = Distance( start1, new_pnt );
  403. if ( new_dist < current_dist )
  404. {//then update close_pnt1 close_pnt2 and current_dist
  405. VectorCopy( start1, close_pnt1 );
  406. VectorCopy( new_pnt, close_pnt2 );
  407. current_dist = new_dist;
  408. }
  409. G_FindClosestPointOnLineSegment( start2, end2, end1, new_pnt );
  410. new_dist = Distance( end1, new_pnt );
  411. if ( new_dist < current_dist )
  412. {//then update close_pnt1 close_pnt2 and current_dist
  413. VectorCopy( end1, close_pnt1 );
  414. VectorCopy( new_pnt, close_pnt2 );
  415. current_dist = new_dist;
  416. }
  417. G_FindClosestPointOnLineSegment( start1, end1, start2, new_pnt );
  418. new_dist = Distance( start2, new_pnt );
  419. if ( new_dist < current_dist )
  420. {//then update close_pnt1 close_pnt2 and current_dist
  421. VectorCopy( new_pnt, close_pnt1 );
  422. VectorCopy( start2, close_pnt2 );
  423. current_dist = new_dist;
  424. }
  425. G_FindClosestPointOnLineSegment( start1, end1, end2, new_pnt );
  426. new_dist = Distance( end2, new_pnt );
  427. if ( new_dist < current_dist )
  428. {//then update close_pnt1 close_pnt2 and current_dist
  429. VectorCopy( new_pnt, close_pnt1 );
  430. VectorCopy( end2, close_pnt2 );
  431. current_dist = new_dist;
  432. }
  433. return current_dist;
  434. }