private static void add(float3 p, float3[] dest, ref int pcount) { dest[pcount++] = new float3(p); Debug.Assert(pcount <= 4); }
public static PlaneTriResult planeTriIntersection(float4 plane, FaceTri triangle, float epsilon, ref float3[] front, out int fcount, ref float3[] back, out int bcount) { fcount = 0; bcount = 0; // get the three vertices of the triangle. float3 p1 = triangle.P1; float3 p2 = triangle.P2; float3 p3 = triangle.P3; PlaneTriResult r1 = getSidePlane(p1, plane, epsilon); // compute the side of the plane each vertex is on PlaneTriResult r2 = getSidePlane(p2, plane, epsilon); PlaneTriResult r3 = getSidePlane(p3, plane, epsilon); if (r1 == r2 && r1 == r3) // if all three vertices are on the same side of the plane. { if (r1 == PlaneTriResult.PTR_FRONT) // if all three are in front of the plane, then copy to the 'front' output triangle. { add(p1, front, ref fcount); add(p2, front, ref fcount); add(p3, front, ref fcount); } else { add(p1, back, ref bcount); // if all three are in 'back' then copy to the 'back' output triangle. add(p2, back, ref bcount); add(p3, back, ref bcount); } return(r1); // if all three points are on the same side of the plane return result } // ok.. we need to split the triangle at the plane. // First test ray segment P1 to P2 if (r1 == r2) // if these are both on the same side... { if (r1 == PlaneTriResult.PTR_FRONT) { add(p1, front, ref fcount); add(p2, front, ref fcount); } else { add(p1, back, ref bcount); add(p2, back, ref bcount); } } else { float3 split = new float3(); intersect(p1, p2, split, plane); if (r1 == PlaneTriResult.PTR_FRONT) { add(p1, front, ref fcount); add(split, front, ref fcount); add(split, back, ref bcount); add(p2, back, ref bcount); } else { add(p1, back, ref bcount); add(split, back, ref bcount); add(split, front, ref fcount); add(p2, front, ref fcount); } } // Next test ray segment P2 to P3 if (r2 == r3) // if these are both on the same side... { if (r3 == PlaneTriResult.PTR_FRONT) { add(p3, front, ref fcount); } else { add(p3, back, ref bcount); } } else { float3 split = new float3(); // split the point intersect(p2, p3, split, plane); if (r3 == PlaneTriResult.PTR_FRONT) { add(split, front, ref fcount); add(split, back, ref bcount); add(p3, front, ref fcount); } else { add(split, front, ref fcount); add(split, back, ref bcount); add(p3, back, ref bcount); } } // Next test ray segment P3 to P1 if (r3 != r1) // if these are both on the same side... { float3 split = new float3(); // split the point intersect(p3, p1, split, plane); if (r1 == PlaneTriResult.PTR_FRONT) { add(split, front, ref fcount); add(split, back, ref bcount); } else { add(split, front, ref fcount); add(split, back, ref bcount); } } return(PlaneTriResult.PTR_SPLIT); }
private static float DistToPt(float3 p, float4 plane) { return(p.x * plane.x + p.y * plane.y + p.z * plane.z + plane.w); }