// Returns true iff the mesh contains two index defined traingles that intersect. bool check_triangle_intersections(ofMesh &mesh) { int len = mesh.getNumIndices(); for(int i = 0; i < len; i += 3) { ofVec3f v1 = mesh.getVertex(mesh.getIndex(i)); ofVec3f v2 = mesh.getVertex(mesh.getIndex(i + 1)); ofVec3f v3 = mesh.getVertex(mesh.getIndex(i + 2)); for(int j = 0; j < len; j += 3) { ofVec3f p1 = mesh.getVertex(mesh.getIndex(j)); ofVec3f p2 = mesh.getVertex(mesh.getIndex(j + 1)); ofVec3f p3 = mesh.getVertex(mesh.getIndex(j + 2)); int b = Intersecting(p1, p2, v1, v2, v3); int b2 = Intersecting(p2, p3, v1, v2, v3); int b3 = Intersecting(p3, p1, v1, v2, v3); if(b == 1 || b2 == 1 || b3 == 1) { return true; } if(point_triangle_intersection(v1, v2, v3, p1)|| point_triangle_intersection(v1, v2, v3, p2)|| point_triangle_intersection(v1, v2, v3, p3)) { return true; } } } return false; }
long t_c_intersection(Triangle3 t) { long v1_test,v2_test,v3_test; float d,denom; Point3 vect12,vect13,norm; Point3 hitpp,hitpn,hitnp,hitnn; /* First compare all three vertexes with all six face-planes */ /* If any vertex is inside the cube, return immediately! */ if ((v1_test = face_plane(t.v1)) == INSIDE) return(INSIDE); if ((v2_test = face_plane(t.v2)) == INSIDE) return(INSIDE); if ((v3_test = face_plane(t.v3)) == INSIDE) return(INSIDE); /* If all three vertexes were outside of one or more face-planes, */ /* return immediately with a trivial rejection! */ if ((v1_test & v2_test & v3_test) != 0) return(OUTSIDE); /* Now do the same trivial rejection test for the 12 edge planes */ v1_test |= bevel_2d(t.v1) << 8; v2_test |= bevel_2d(t.v2) << 8; v3_test |= bevel_2d(t.v3) << 8; if ((v1_test & v2_test & v3_test) != 0) return(OUTSIDE); /* Now do the same trivial rejection test for the 8 corner planes */ v1_test |= bevel_3d(t.v1) << 24; v2_test |= bevel_3d(t.v2) << 24; v3_test |= bevel_3d(t.v3) << 24; if ((v1_test & v2_test & v3_test) != 0) return(OUTSIDE); /* If vertex 1 and 2, as a pair, cannot be trivially rejected */ /* by the above tests, then see if the v1-->v2 triangle edge */ /* intersects the cube. Do the same for v1-->v3 and v2-->v3. */ /* Pass to the intersection algorithm the "OR" of the outcode */ /* bits, so that only those cube faces which are spanned by */ /* each triangle edge need be tested. */ if ((v1_test & v2_test) == 0) if (check_line(t.v1,t.v2,v1_test|v2_test) == INSIDE) return(INSIDE); if ((v1_test & v3_test) == 0) if (check_line(t.v1,t.v3,v1_test|v3_test) == INSIDE) return(INSIDE); if ((v2_test & v3_test) == 0) if (check_line(t.v2,t.v3,v2_test|v3_test) == INSIDE) return(INSIDE); /* By now, we know that the triangle is not off to any side, */ /* and that its sides do not penetrate the cube. We must now */ /* test for the cube intersecting the interior of the triangle. */ /* We do this by looking for intersections between the cube */ /* diagonals and the triangle...first finding the intersection */ /* of the four diagonals with the plane of the triangle, and */ /* then if that intersection is inside the cube, pursuing */ /* whether the intersection point is inside the triangle itself. */ /* To find plane of the triangle, first perform crossproduct on */ /* two triangle side vectors to compute the normal vector. */ SUB(t.v1,t.v2,vect12); SUB(t.v1,t.v3,vect13); CROSS(vect12,vect13,norm) /* The normal vector "norm" X,Y,Z components are the coefficients */ /* of the triangles AX + BY + CZ + D = 0 plane equation. If we */ /* solve the plane equation for X=Y=Z (a diagonal), we get */ /* -D/(A+B+C) as a metric of the distance from cube center to the */ /* diagonal/plane intersection. If this is between -0.5 and 0.5, */ /* the intersection is inside the cube. If so, we continue by */ /* doing a point/triangle intersection. */ /* Do this for all four diagonals. */ d = norm.x * t.v1.x + norm.y * t.v1.y + norm.z * t.v1.z; /* if one of the diagonals is parallel to the plane, the other will intersect the plane */ if(fabs(denom=(norm.x + norm.y + norm.z))>EPS) /* skip parallel diagonals to the plane; division by 0 can occur */ { hitpp.x = hitpp.y = hitpp.z = d / denom; if (fabs(hitpp.x) <= 0.5) if (point_triangle_intersection(hitpp,t) == INSIDE) return(INSIDE); } if(fabs(denom=(norm.x + norm.y - norm.z))>EPS) { hitpn.z = -(hitpn.x = hitpn.y = d / denom); if (fabs(hitpn.x) <= 0.5) if (point_triangle_intersection(hitpn,t) == INSIDE) return(INSIDE); } if(fabs(denom=(norm.x - norm.y + norm.z))>EPS) { hitnp.y = -(hitnp.x = hitnp.z = d / denom); if (fabs(hitnp.x) <= 0.5) if (point_triangle_intersection(hitnp,t) == INSIDE) return(INSIDE); } if(fabs(denom=(norm.x - norm.y - norm.z))>EPS) { hitnn.y = hitnn.z = -(hitnn.x = d / denom); if (fabs(hitnn.x) <= 0.5) if (point_triangle_intersection(hitnn,t) == INSIDE) return(INSIDE); } /* No edge touched the cube; no cube diagonal touched the triangle. */ /* We're done...there was no intersection. */ return(OUTSIDE); }