Graphics Reference
In-Depth Information
void SplitPolygon(Polygon &poly, Plane plane, Polygon **frontPoly, Polygon **backPoly)
{
int numFront = 0, numBack = 0;
Point frontVerts[MAX_POINTS], backVerts[MAX_POINTS];
// Test all edges (a, b) starting with edge from last to first vertex
int numVerts = poly.NumVertices();
Point a = poly.GetVertex(numVerts - 1);
int aSide = ClassifyPointToPlane(a, plane);
// Loop over all edges given by vertex pair (n - 1, n)
for(intn=0;n<numVerts; n++) {
Point b = poly.GetVertex(n);
int bSide = ClassifyPointToPlane(b, plane);
if (bSide == POINT_IN_FRONT_OF_PLANE) {
if (aSide == POINT_BEHIND_PLANE) {
// Edge (a, b) straddles, output intersection point to both sides
Point i = IntersectEdgeAgainstPlane(a, b, plane);
assert(ClassifyPointToPlane(i, plane) == POINT_ON_PLANE);
frontVerts[numFront++] = backVerts[numBack++] = i;
}
// In all three cases, output b to the front side
frontVerts[numFront++] = b;
} else if (bSide == POINT_BEHIND_PLANE) {
if (aSide == POINT_IN_FRONT_OF_PLANE) {
// Edge (a, b) straddles plane, output intersection point
Point i = IntersectEdgeAgainstPlane(a, b, plane);
assert(ClassifyPointToPlane(i, plane) == POINT_ON_PLANE);
frontVerts[numFront++] = backVerts[numBack++] = i;
} else if (aSide == POINT_ON_PLANE) {
// Output a when edge (a, b) goes from 'on' to 'behind' plane
backVerts[numBack++] = a;
}
// In all three cases, output b to the back side
backVerts[numBack++] = b;
} else {
// b is on the plane. In all three cases output b to the front side
frontVerts[numFront++] = b;
// In one case, also output b to back side
if (aSide == POINT_BEHIND_PLANE)
backVerts[numBack++] = b;
}