tritritest.h

Go to the documentation of this file.
00001 /* Triangle/triangle intersection test routine,
00002  * by Tomas Moller, 1997.
00003  * See article "A Fast Triangle-Triangle Intersection Test",
00004  * Journal of Graphics Tools, 2(2), 1997
00005  * updated: 2001-06-20 (added line of intersection)
00006  *
00007  * int tri_tri_intersect(float V0[3],float V1[3],float V2[3],
00008  *                       float U0[3],float U1[3],float U2[3])
00009  *
00010  * parameters: vertices of triangle 1: V0,V1,V2
00011  *             vertices of triangle 2: U0,U1,U2
00012  * result    : returns 1 if the triangles intersect, otherwise 0
00013  *
00014  * Here is a version withouts divisions (a little faster)
00015  * int NoDivTriTriIsect(float V0[3],float V1[3],float V2[3],
00016  *                      float U0[3],float U1[3],float U2[3]);
00017  * 
00018  * This version computes the line of intersection as well (if they are not coplanar):
00019  * int tri_tri_intersect_with_isectline(float V0[3],float V1[3],float V2[3], 
00020  *        float U0[3],float U1[3],float U2[3],int *coplanar,
00021  *        float isectpt1[3],float isectpt2[3]);
00022  * coplanar returns whether the tris are coplanar
00023  * isectpt1, isectpt2 are the endpoints of the line of intersection
00024  */
00025 
00026 #include <math.h>
00027 
00028 #define FABS(x) ((float)fabs(x))        /* implement as is fastest on your machine */
00029 
00030 /* if USE_EPSILON_TEST is true then we do a check: 
00031          if |dv|<EPSILON then dv=0.0;
00032    else no check is done (which is less robust)
00033 */
00034 #define USE_EPSILON_TEST TRUE  
00035 #define EPSILON 0.000001
00036 
00037 
00038 /* some macros */
00039 #define CROSS(dest,v1,v2)                      \
00040               dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
00041               dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
00042               dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
00043 
00044 #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
00045 
00046 #define SUB(dest,v1,v2) dest[0]=v1[0]-v2[0]; dest[1]=v1[1]-v2[1]; dest[2]=v1[2]-v2[2]; 
00047 
00048 #define ADD(dest,v1,v2) dest[0]=v1[0]+v2[0]; dest[1]=v1[1]+v2[1]; dest[2]=v1[2]+v2[2]; 
00049 
00050 #define MULT(dest,v,factor) dest[0]=factor*v[0]; dest[1]=factor*v[1]; dest[2]=factor*v[2];
00051 
00052 #define SET(dest,src) dest[0]=src[0]; dest[1]=src[1]; dest[2]=src[2]; 
00053 
00054 /* sort so that a<=b */
00055 #define SORT(a,b)       \
00056              if(a>b)    \
00057              {          \
00058                float c; \
00059                c=a;     \
00060                a=b;     \
00061                b=c;     \
00062              }
00063 
00064 #define ISECT(VV0,VV1,VV2,D0,D1,D2,isect0,isect1) \
00065               isect0=VV0+(VV1-VV0)*D0/(D0-D1);    \
00066               isect1=VV0+(VV2-VV0)*D0/(D0-D2);
00067 
00068 
00069 #define COMPUTE_INTERVALS(VV0,VV1,VV2,D0,D1,D2,D0D1,D0D2,isect0,isect1) \
00070   if(D0D1>0.0f)                                         \
00071   {                                                     \
00072     /* here we know that D0D2<=0.0 */                   \
00073     /* that is D0, D1 are on the same side, D2 on the other or on the plane */ \
00074     ISECT(VV2,VV0,VV1,D2,D0,D1,isect0,isect1);          \
00075   }                                                     \
00076   else if(D0D2>0.0f)                                    \
00077   {                                                     \
00078     /* here we know that d0d1<=0.0 */                   \
00079     ISECT(VV1,VV0,VV2,D1,D0,D2,isect0,isect1);          \
00080   }                                                     \
00081   else if(D1*D2>0.0f || D0!=0.0f)                       \
00082   {                                                     \
00083     /* here we know that d0d1<=0.0 or that D0!=0.0 */   \
00084     ISECT(VV0,VV1,VV2,D0,D1,D2,isect0,isect1);          \
00085   }                                                     \
00086   else if(D1!=0.0f)                                     \
00087   {                                                     \
00088     ISECT(VV1,VV0,VV2,D1,D0,D2,isect0,isect1);          \
00089   }                                                     \
00090   else if(D2!=0.0f)                                     \
00091   {                                                     \
00092     ISECT(VV2,VV0,VV1,D2,D0,D1,isect0,isect1);          \
00093   }                                                     \
00094   else                                                  \
00095   {                                                     \
00096     /* triangles are coplanar */                        \
00097     return coplanar_tri_tri(N1,V0,V1,V2,U0,U1,U2);      \
00098   }
00099 
00100 
00101 
00102 /* this edge to edge test is based on Franlin Antonio's gem:
00103    "Faster Line Segment Intersection", in Graphics Gems III,
00104    pp. 199-202 */ 
00105 #define EDGE_EDGE_TEST(V0,U0,U1)                      \
00106   Bx=U0[i0]-U1[i0];                                   \
00107   By=U0[i1]-U1[i1];                                   \
00108   Cx=V0[i0]-U0[i0];                                   \
00109   Cy=V0[i1]-U0[i1];                                   \
00110   f=Ay*Bx-Ax*By;                                      \
00111   d=By*Cx-Bx*Cy;                                      \
00112   if((f>0 && d>=0 && d<=f) || (f<0 && d<=0 && d>=f))  \
00113   {                                                   \
00114     e=Ax*Cy-Ay*Cx;                                    \
00115     if(f>0)                                           \
00116     {                                                 \
00117       if(e>=0 && e<=f) return 1;                      \
00118     }                                                 \
00119     else                                              \
00120     {                                                 \
00121       if(e<=0 && e>=f) return 1;                      \
00122     }                                                 \
00123   }                                
00124 
00125 #define EDGE_AGAINST_TRI_EDGES(V0,V1,U0,U1,U2) \
00126 {                                              \
00127   float Ax,Ay,Bx,By,Cx,Cy,e,d,f;               \
00128   Ax=V1[i0]-V0[i0];                            \
00129   Ay=V1[i1]-V0[i1];                            \
00130   /* test edge U0,U1 against V0,V1 */          \
00131   EDGE_EDGE_TEST(V0,U0,U1);                    \
00132   /* test edge U1,U2 against V0,V1 */          \
00133   EDGE_EDGE_TEST(V0,U1,U2);                    \
00134   /* test edge U2,U1 against V0,V1 */          \
00135   EDGE_EDGE_TEST(V0,U2,U0);                    \
00136 }
00137 
00138 #define POINT_IN_TRI(V0,U0,U1,U2)           \
00139 {                                           \
00140   float a,b,c,d0,d1,d2;                     \
00141   /* is T1 completly inside T2? */          \
00142   /* check if V0 is inside tri(U0,U1,U2) */ \
00143   a=U1[i1]-U0[i1];                          \
00144   b=-(U1[i0]-U0[i0]);                       \
00145   c=-a*U0[i0]-b*U0[i1];                     \
00146   d0=a*V0[i0]+b*V0[i1]+c;                   \
00147                                             \
00148   a=U2[i1]-U1[i1];                          \
00149   b=-(U2[i0]-U1[i0]);                       \
00150   c=-a*U1[i0]-b*U1[i1];                     \
00151   d1=a*V0[i0]+b*V0[i1]+c;                   \
00152                                             \
00153   a=U0[i1]-U2[i1];                          \
00154   b=-(U0[i0]-U2[i0]);                       \
00155   c=-a*U2[i0]-b*U2[i1];                     \
00156   d2=a*V0[i0]+b*V0[i1]+c;                   \
00157   if(d0*d1>0.0)                             \
00158   {                                         \
00159     if(d0*d2>0.0) return 1;                 \
00160   }                                         \
00161 }
00162 
00163 int coplanar_tri_tri(float N[3],float V0[3],float V1[3],float V2[3],
00164                      float U0[3],float U1[3],float U2[3])
00165 {
00166    float A[3];
00167    short i0,i1;
00168    /* first project onto an axis-aligned plane, that maximizes the area */
00169    /* of the triangles, compute indices: i0,i1. */
00170    A[0]=fabs(N[0]);
00171    A[1]=fabs(N[1]);
00172    A[2]=fabs(N[2]);
00173    if(A[0]>A[1])
00174    {
00175       if(A[0]>A[2])  
00176       {
00177           i0=1;      /* A[0] is greatest */
00178           i1=2;
00179       }
00180       else
00181       {
00182           i0=0;      /* A[2] is greatest */
00183           i1=1;
00184       }
00185    }
00186    else   /* A[0]<=A[1] */
00187    {
00188       if(A[2]>A[1])
00189       {
00190           i0=0;      /* A[2] is greatest */
00191           i1=1;                                           
00192       }
00193       else
00194       {
00195           i0=0;      /* A[1] is greatest */
00196           i1=2;
00197       }
00198     }               
00199                 
00200     /* test all edges of triangle 1 against the edges of triangle 2 */
00201     EDGE_AGAINST_TRI_EDGES(V0,V1,U0,U1,U2);
00202     EDGE_AGAINST_TRI_EDGES(V1,V2,U0,U1,U2);
00203     EDGE_AGAINST_TRI_EDGES(V2,V0,U0,U1,U2);
00204                 
00205     /* finally, test if tri1 is totally contained in tri2 or vice versa */
00206     POINT_IN_TRI(V0,U0,U1,U2);
00207     POINT_IN_TRI(U0,V0,V1,V2);
00208 
00209     return 0;
00210 }
00211 
00212 
00213 int tri_tri_intersect(float V0[3],float V1[3],float V2[3],
00214                       float U0[3],float U1[3],float U2[3])
00215 {
00216   float E1[3],E2[3];
00217   float N1[3],N2[3],d1,d2;
00218   float du0,du1,du2,dv0,dv1,dv2;
00219   float D[3];
00220   float isect1[2], isect2[2];
00221   float du0du1,du0du2,dv0dv1,dv0dv2;
00222   short index;
00223   float vp0,vp1,vp2;
00224   float up0,up1,up2;
00225   float b,c,max;
00226 
00227   /* compute plane equation of triangle(V0,V1,V2) */
00228   SUB(E1,V1,V0);
00229   SUB(E2,V2,V0);
00230   CROSS(N1,E1,E2);
00231   d1=-DOT(N1,V0);
00232   /* plane equation 1: N1.X+d1=0 */
00233 
00234   /* put U0,U1,U2 into plane equation 1 to compute signed distances to the plane*/
00235   du0=DOT(N1,U0)+d1;
00236   du1=DOT(N1,U1)+d1;
00237   du2=DOT(N1,U2)+d1;
00238 
00239   /* coplanarity robustness check */
00240 #if USE_EPSILON_TEST==TRUE
00241   if(fabs(du0)<EPSILON) du0=0.0;
00242   if(fabs(du1)<EPSILON) du1=0.0;
00243   if(fabs(du2)<EPSILON) du2=0.0;
00244 #endif
00245   du0du1=du0*du1;
00246   du0du2=du0*du2;
00247 
00248   if(du0du1>0.0f && du0du2>0.0f) /* same sign on all of them + not equal 0 ? */
00249     return 0;                    /* no intersection occurs */
00250 
00251   /* compute plane of triangle (U0,U1,U2) */
00252   SUB(E1,U1,U0);
00253   SUB(E2,U2,U0);
00254   CROSS(N2,E1,E2);
00255   d2=-DOT(N2,U0);
00256   /* plane equation 2: N2.X+d2=0 */
00257 
00258   /* put V0,V1,V2 into plane equation 2 */
00259   dv0=DOT(N2,V0)+d2;
00260   dv1=DOT(N2,V1)+d2;
00261   dv2=DOT(N2,V2)+d2;
00262 
00263 #if USE_EPSILON_TEST==TRUE
00264   if(fabs(dv0)<EPSILON) dv0=0.0;
00265   if(fabs(dv1)<EPSILON) dv1=0.0;
00266   if(fabs(dv2)<EPSILON) dv2=0.0;
00267 #endif
00268 
00269   dv0dv1=dv0*dv1;
00270   dv0dv2=dv0*dv2;
00271         
00272   if(dv0dv1>0.0f && dv0dv2>0.0f) /* same sign on all of them + not equal 0 ? */
00273     return 0;                    /* no intersection occurs */
00274 
00275   /* compute direction of intersection line */
00276   CROSS(D,N1,N2);
00277 
00278   /* compute and index to the largest component of D */
00279   max=fabs(D[0]);
00280   index=0;
00281   b=fabs(D[1]);
00282   c=fabs(D[2]);
00283   if(b>max) max=b,index=1;
00284   if(c>max) max=c,index=2;
00285 
00286   /* this is the simplified projection onto L*/
00287   vp0=V0[index];
00288   vp1=V1[index];
00289   vp2=V2[index];
00290   
00291   up0=U0[index];
00292   up1=U1[index];
00293   up2=U2[index];
00294 
00295   /* compute interval for triangle 1 */
00296   COMPUTE_INTERVALS(vp0,vp1,vp2,dv0,dv1,dv2,dv0dv1,dv0dv2,isect1[0],isect1[1]);
00297 
00298   /* compute interval for triangle 2 */
00299   COMPUTE_INTERVALS(up0,up1,up2,du0,du1,du2,du0du1,du0du2,isect2[0],isect2[1]);
00300 
00301   SORT(isect1[0],isect1[1]);
00302   SORT(isect2[0],isect2[1]);
00303 
00304   if(isect1[1]<isect2[0] || isect2[1]<isect1[0]) return 0;
00305   return 1;
00306 }
00307 
00308 
00309 #define NEWCOMPUTE_INTERVALS(VV0,VV1,VV2,D0,D1,D2,D0D1,D0D2,A,B,C,X0,X1) \
00310 { \
00311         if(D0D1>0.0f) \
00312         { \
00313                 /* here we know that D0D2<=0.0 */ \
00314             /* that is D0, D1 are on the same side, D2 on the other or on the plane */ \
00315                 A=VV2; B=(VV0-VV2)*D2; C=(VV1-VV2)*D2; X0=D2-D0; X1=D2-D1; \
00316         } \
00317         else if(D0D2>0.0f)\
00318         { \
00319                 /* here we know that d0d1<=0.0 */ \
00320             A=VV1; B=(VV0-VV1)*D1; C=(VV2-VV1)*D1; X0=D1-D0; X1=D1-D2; \
00321         } \
00322         else if(D1*D2>0.0f || D0!=0.0f) \
00323         { \
00324                 /* here we know that d0d1<=0.0 or that D0!=0.0 */ \
00325                 A=VV0; B=(VV1-VV0)*D0; C=(VV2-VV0)*D0; X0=D0-D1; X1=D0-D2; \
00326         } \
00327         else if(D1!=0.0f) \
00328         { \
00329                 A=VV1; B=(VV0-VV1)*D1; C=(VV2-VV1)*D1; X0=D1-D0; X1=D1-D2; \
00330         } \
00331         else if(D2!=0.0f) \
00332         { \
00333                 A=VV2; B=(VV0-VV2)*D2; C=(VV1-VV2)*D2; X0=D2-D0; X1=D2-D1; \
00334         } \
00335         else \
00336         { \
00337                 /* triangles are coplanar */ \
00338                 return coplanar_tri_tri(N1,V0,V1,V2,U0,U1,U2); \
00339         } \
00340 }
00341 
00342 
00343 
00344 int NoDivTriTriIsect(float V0[3],float V1[3],float V2[3],
00345                      float U0[3],float U1[3],float U2[3])
00346 {
00347   float E1[3],E2[3];
00348   float N1[3],N2[3],d1,d2;
00349   float du0,du1,du2,dv0,dv1,dv2;
00350   float D[3];
00351   float isect1[2], isect2[2];
00352   float du0du1,du0du2,dv0dv1,dv0dv2;
00353   short index;
00354   float vp0,vp1,vp2;
00355   float up0,up1,up2;
00356   float bb,cc,max;
00357   float a,b,c,x0,x1;
00358   float d,e,f,y0,y1;
00359   float xx,yy,xxyy,tmp;
00360 
00361   /* compute plane equation of triangle(V0,V1,V2) */
00362   SUB(E1,V1,V0);
00363   SUB(E2,V2,V0);
00364   CROSS(N1,E1,E2);
00365   d1=-DOT(N1,V0);
00366   /* plane equation 1: N1.X+d1=0 */
00367 
00368   /* put U0,U1,U2 into plane equation 1 to compute signed distances to the plane*/
00369   du0=DOT(N1,U0)+d1;
00370   du1=DOT(N1,U1)+d1;
00371   du2=DOT(N1,U2)+d1;
00372 
00373   /* coplanarity robustness check */
00374 #if USE_EPSILON_TEST==TRUE
00375   if(FABS(du0)<EPSILON) du0=0.0;
00376   if(FABS(du1)<EPSILON) du1=0.0;
00377   if(FABS(du2)<EPSILON) du2=0.0;
00378 #endif
00379   du0du1=du0*du1;
00380   du0du2=du0*du2;
00381 
00382   if(du0du1>0.0f && du0du2>0.0f) /* same sign on all of them + not equal 0 ? */
00383     return 0;                    /* no intersection occurs */
00384 
00385   /* compute plane of triangle (U0,U1,U2) */
00386   SUB(E1,U1,U0);
00387   SUB(E2,U2,U0);
00388   CROSS(N2,E1,E2);
00389   d2=-DOT(N2,U0);
00390   /* plane equation 2: N2.X+d2=0 */
00391 
00392   /* put V0,V1,V2 into plane equation 2 */
00393   dv0=DOT(N2,V0)+d2;
00394   dv1=DOT(N2,V1)+d2;
00395   dv2=DOT(N2,V2)+d2;
00396 
00397 #if USE_EPSILON_TEST==TRUE
00398   if(FABS(dv0)<EPSILON) dv0=0.0;
00399   if(FABS(dv1)<EPSILON) dv1=0.0;
00400   if(FABS(dv2)<EPSILON) dv2=0.0;
00401 #endif
00402 
00403   dv0dv1=dv0*dv1;
00404   dv0dv2=dv0*dv2;
00405 
00406   if(dv0dv1>0.0f && dv0dv2>0.0f) /* same sign on all of them + not equal 0 ? */
00407     return 0;                    /* no intersection occurs */
00408 
00409   /* compute direction of intersection line */
00410   CROSS(D,N1,N2);
00411 
00412   /* compute and index to the largest component of D */
00413   max=(float)FABS(D[0]);
00414   index=0;
00415   bb=(float)FABS(D[1]);
00416   cc=(float)FABS(D[2]);
00417   if(bb>max) max=bb,index=1;
00418   if(cc>max) max=cc,index=2;
00419 
00420   /* this is the simplified projection onto L*/
00421   vp0=V0[index];
00422   vp1=V1[index];
00423   vp2=V2[index];
00424 
00425   up0=U0[index];
00426   up1=U1[index];
00427   up2=U2[index];
00428 
00429   /* compute interval for triangle 1 */
00430   NEWCOMPUTE_INTERVALS(vp0,vp1,vp2,dv0,dv1,dv2,dv0dv1,dv0dv2,a,b,c,x0,x1);
00431 
00432   /* compute interval for triangle 2 */
00433   NEWCOMPUTE_INTERVALS(up0,up1,up2,du0,du1,du2,du0du1,du0du2,d,e,f,y0,y1);
00434 
00435   xx=x0*x1;
00436   yy=y0*y1;
00437   xxyy=xx*yy;
00438 
00439   tmp=a*xxyy;
00440   isect1[0]=tmp+b*x1*yy;
00441   isect1[1]=tmp+c*x0*yy;
00442 
00443   tmp=d*xxyy;
00444   isect2[0]=tmp+e*xx*y1;
00445   isect2[1]=tmp+f*xx*y0;
00446 
00447   SORT(isect1[0],isect1[1]);
00448   SORT(isect2[0],isect2[1]);
00449 
00450   if(isect1[1]<isect2[0] || isect2[1]<isect1[0]) return 0;
00451   return 1;
00452 }
00453 
00454 /* sort so that a<=b */
00455 #define SORT2(a,b,smallest)       \
00456              if(a>b)       \
00457              {             \
00458                float c;    \
00459                c=a;        \
00460                a=b;        \
00461                b=c;        \
00462                smallest=1; \
00463              }             \
00464              else smallest=0;
00465 
00466 
00467 inline void isect2(float VTX0[3],float VTX1[3],float VTX2[3],float VV0,float VV1,float VV2,
00468     float D0,float D1,float D2,float *isect0,float *isect1,float isectpoint0[3],float isectpoint1[3]) 
00469 {
00470   float tmp=D0/(D0-D1);          
00471   float diff[3];
00472   *isect0=VV0+(VV1-VV0)*tmp;         
00473   SUB(diff,VTX1,VTX0);
00474   MULT(diff,diff,tmp);
00475   ADD(isectpoint0,diff,VTX0);
00476   tmp=D0/(D0-D2);                    
00477   *isect1=VV0+(VV2-VV0)*tmp;          
00478   SUB(diff,VTX2,VTX0);
00479   MULT(diff,diff,tmp);
00480   ADD(isectpoint1,VTX0,diff);
00481 }
00482 
00483 
00484 #if 0
00485 #define ISECT2(VTX0,VTX1,VTX2,VV0,VV1,VV2,D0,D1,D2,isect0,isect1,isectpoint0,isectpoint1) \
00486               tmp=D0/(D0-D1);                    \
00487               isect0=VV0+(VV1-VV0)*tmp;          \
00488       SUB(diff,VTX1,VTX0);               \
00489       MULT(diff,diff,tmp);               \
00490               ADD(isectpoint0,diff,VTX0);        \
00491               tmp=D0/(D0-D2);
00492 /*              isect1=VV0+(VV2-VV0)*tmp;          \ */
00493 /*              SUB(diff,VTX2,VTX0);               \     */
00494 /*              MULT(diff,diff,tmp);               \   */
00495 /*              ADD(isectpoint1,VTX0,diff);           */
00496 #endif
00497 
00498 inline int compute_intervals_isectline(float VERT0[3],float VERT1[3],float VERT2[3],
00499        float VV0,float VV1,float VV2,float D0,float D1,float D2,
00500        float D0D1,float D0D2,float *isect0,float *isect1,
00501        float isectpoint0[3],float isectpoint1[3])
00502 {
00503   if(D0D1>0.0f)                                        
00504   {                                                    
00505     /* here we know that D0D2<=0.0 */                  
00506     /* that is D0, D1 are on the same side, D2 on the other or on the plane */
00507     isect2(VERT2,VERT0,VERT1,VV2,VV0,VV1,D2,D0,D1,isect0,isect1,isectpoint0,isectpoint1);
00508   } 
00509   else if(D0D2>0.0f)                                   
00510     {                                                   
00511     /* here we know that d0d1<=0.0 */             
00512     isect2(VERT1,VERT0,VERT2,VV1,VV0,VV2,D1,D0,D2,isect0,isect1,isectpoint0,isectpoint1);
00513   }                                                  
00514   else if(D1*D2>0.0f || D0!=0.0f)   
00515   {                                   
00516     /* here we know that d0d1<=0.0 or that D0!=0.0 */
00517     isect2(VERT0,VERT1,VERT2,VV0,VV1,VV2,D0,D1,D2,isect0,isect1,isectpoint0,isectpoint1);   
00518   }                                                  
00519   else if(D1!=0.0f)                                  
00520   {                                               
00521     isect2(VERT1,VERT0,VERT2,VV1,VV0,VV2,D1,D0,D2,isect0,isect1,isectpoint0,isectpoint1); 
00522   }                                         
00523   else if(D2!=0.0f)                                  
00524   {                                                   
00525     isect2(VERT2,VERT0,VERT1,VV2,VV0,VV1,D2,D0,D1,isect0,isect1,isectpoint0,isectpoint1);     
00526   }                                                 
00527   else                                               
00528   {                                                   
00529     /* triangles are coplanar */    
00530     return 1;
00531   }
00532   return 0;
00533 }
00534 
00535 #define COMPUTE_INTERVALS_ISECTLINE(VERT0,VERT1,VERT2,VV0,VV1,VV2,D0,D1,D2,D0D1,D0D2,isect0,isect1,isectpoint0,isectpoint1) \
00536   if(D0D1>0.0f)                                         \
00537   {                                                     \
00538     /* here we know that D0D2<=0.0 */                   \
00539     /* that is D0, D1 are on the same side, D2 on the other or on the plane */ \
00540     isect2(VERT2,VERT0,VERT1,VV2,VV0,VV1,D2,D0,D1,&isect0,&isect1,isectpoint0,isectpoint1);          \
00541   }                                                     
00542 #if 0
00543   else if(D0D2>0.0f)                                    \
00544   {                                                     \
00545     /* here we know that d0d1<=0.0 */                   \
00546     isect2(VERT1,VERT0,VERT2,VV1,VV0,VV2,D1,D0,D2,&isect0,&isect1,isectpoint0,isectpoint1);          \
00547   }                                                     \
00548   else if(D1*D2>0.0f || D0!=0.0f)                       \
00549   {                                                     \
00550     /* here we know that d0d1<=0.0 or that D0!=0.0 */   \
00551     isect2(VERT0,VERT1,VERT2,VV0,VV1,VV2,D0,D1,D2,&isect0,&isect1,isectpoint0,isectpoint1);          \
00552   }                                                     \
00553   else if(D1!=0.0f)                                     \
00554   {                                                     \
00555     isect2(VERT1,VERT0,VERT2,VV1,VV0,VV2,D1,D0,D2,&isect0,&isect1,isectpoint0,isectpoint1);          \
00556   }                                                     \
00557   else if(D2!=0.0f)                                     \
00558   {                                                     \
00559     isect2(VERT2,VERT0,VERT1,VV2,VV0,VV1,D2,D0,D1,&isect0,&isect1,isectpoint0,isectpoint1);          \
00560   }                                                     \
00561   else                                                  \
00562   {                                                     \
00563     /* triangles are coplanar */                        \
00564     coplanar=1;                                         \
00565     return coplanar_tri_tri(N1,V0,V1,V2,U0,U1,U2);      \
00566   }
00567 #endif
00568 
00569 int tri_tri_intersect_with_isectline(float V0[3],float V1[3],float V2[3],
00570      float U0[3],float U1[3],float U2[3],int *coplanar,
00571      float isectpt1[3],float isectpt2[3])
00572 {
00573   float E1[3],E2[3];
00574   float N1[3],N2[3],d1,d2;
00575   float du0,du1,du2,dv0,dv1,dv2;
00576   float D[3];
00577   float isect1[2]={0.0f,0.0f}, isect2[2]={0.0f,0.0f};
00578   float isectpointA1[3]={0.0f,0.0f,0.0f},isectpointA2[3]={0.0f,0.0f,0.0f};
00579   float isectpointB1[3]={0.0f,0.0f,0.0f},isectpointB2[3]={0.0f,0.0f,0.0f};
00580   float du0du1,du0du2,dv0dv1,dv0dv2;
00581   short index;
00582   float vp0,vp1,vp2;
00583   float up0,up1,up2;
00584   float b,c,max;
00585   int smallest1,smallest2;
00586 
00587   /* compute plane equation of triangle(V0,V1,V2) */
00588   SUB(E1,V1,V0);
00589   SUB(E2,V2,V0);
00590   CROSS(N1,E1,E2);
00591   d1=-DOT(N1,V0);
00592   /* plane equation 1: N1.X+d1=0 */
00593 
00594   /* put U0,U1,U2 into plane equation 1 to compute signed distances to the plane*/
00595   du0=DOT(N1,U0)+d1;
00596   du1=DOT(N1,U1)+d1;
00597   du2=DOT(N1,U2)+d1;
00598 
00599   /* coplanarity robustness check */
00600 #if USE_EPSILON_TEST==TRUE
00601   if(fabs(du0)<EPSILON) du0=0.0;
00602   if(fabs(du1)<EPSILON) du1=0.0;
00603   if(fabs(du2)<EPSILON) du2=0.0;
00604 #endif
00605   du0du1=du0*du1;
00606   du0du2=du0*du2;
00607 
00608   if(du0du1>0.0f && du0du2>0.0f) /* same sign on all of them + not equal 0 ? */
00609     return 0;                    /* no intersection occurs */
00610 
00611   /* compute plane of triangle (U0,U1,U2) */
00612   SUB(E1,U1,U0);
00613   SUB(E2,U2,U0);
00614   CROSS(N2,E1,E2);
00615   d2=-DOT(N2,U0);
00616   /* plane equation 2: N2.X+d2=0 */
00617 
00618   /* put V0,V1,V2 into plane equation 2 */
00619   dv0=DOT(N2,V0)+d2;
00620   dv1=DOT(N2,V1)+d2;
00621   dv2=DOT(N2,V2)+d2;
00622 
00623 #if USE_EPSILON_TEST==TRUE
00624   if(fabs(dv0)<EPSILON) dv0=0.0;
00625   if(fabs(dv1)<EPSILON) dv1=0.0;
00626   if(fabs(dv2)<EPSILON) dv2=0.0;
00627 #endif
00628 
00629   dv0dv1=dv0*dv1;
00630   dv0dv2=dv0*dv2;
00631         
00632   if(dv0dv1>0.0f && dv0dv2>0.0f) /* same sign on all of them + not equal 0 ? */
00633     return 0;                    /* no intersection occurs */
00634 
00635   /* compute direction of intersection line */
00636   CROSS(D,N1,N2);
00637 
00638   /* compute and index to the largest component of D */
00639   max=fabs(D[0]);
00640   index=0;
00641   b=fabs(D[1]);
00642   c=fabs(D[2]);
00643   if(b>max) max=b,index=1;
00644   if(c>max) max=c,index=2;
00645 
00646   /* this is the simplified projection onto L*/
00647   vp0=V0[index];
00648   vp1=V1[index];
00649   vp2=V2[index];
00650   
00651   up0=U0[index];
00652   up1=U1[index];
00653   up2=U2[index];
00654 
00655   /* compute interval for triangle 1 */
00656   *coplanar=compute_intervals_isectline(V0,V1,V2,vp0,vp1,vp2,dv0,dv1,dv2,
00657        dv0dv1,dv0dv2,&isect1[0],&isect1[1],isectpointA1,isectpointA2);
00658   if(*coplanar) return coplanar_tri_tri(N1,V0,V1,V2,U0,U1,U2);     
00659 
00660 
00661   /* compute interval for triangle 2 */
00662   compute_intervals_isectline(U0,U1,U2,up0,up1,up2,du0,du1,du2,
00663       du0du1,du0du2,&isect2[0],&isect2[1],isectpointB1,isectpointB2);
00664 
00665   SORT2(isect1[0],isect1[1],smallest1);
00666   SORT2(isect2[0],isect2[1],smallest2);
00667 
00668   if(isect1[1]<isect2[0] || isect2[1]<isect1[0]) return 0;
00669 
00670   /* at this point, we know that the triangles intersect */
00671 
00672   if(isect2[0]<isect1[0])
00673   {
00674     if(smallest1==0) { SET(isectpt1,isectpointA1); }
00675     else { SET(isectpt1,isectpointA2); }
00676 
00677     if(isect2[1]<isect1[1])
00678     {
00679       if(smallest2==0) { SET(isectpt2,isectpointB2); }
00680       else { SET(isectpt2,isectpointB1); }
00681     }
00682     else
00683     {
00684       if(smallest1==0) { SET(isectpt2,isectpointA2); }
00685       else { SET(isectpt2,isectpointA1); }
00686     }
00687   }
00688   else
00689   {
00690     if(smallest2==0) { SET(isectpt1,isectpointB1); }
00691     else { SET(isectpt1,isectpointB2); }
00692 
00693     if(isect2[1]>isect1[1])
00694     {
00695       if(smallest1==0) { SET(isectpt2,isectpointA2); }
00696       else { SET(isectpt2,isectpointA1); }      
00697     }
00698     else
00699     {
00700       if(smallest2==0) { SET(isectpt2,isectpointB2); }
00701       else { SET(isectpt2,isectpointB1); } 
00702     }
00703   }
00704   return 1;
00705 }
00706 

Generated on Wed Nov 23 19:00:51 2011 for FreeCAD by  doxygen 1.6.1