13.1 Binary Space-Partitioning Trees in 2D 685
by an edge must be further processed by both the positive-child and negative-child
subtrees.
The end result of processing the line segment is a partition, a representation of
the line segment as a union of contiguous subsegments. Each subsegment lives in an
inside region, an outside region, or is on a polygon boundary. Those segments on
the polygon boundary can be further classified based on the direction of the polygon
edge and the direction of the line segment. This distinction is important when using
BSP trees to support Boolean operations on polygons. Figure 13.2 shows a splitting
line and two segments, E
0
and E
1
, that are coincident to the line. E
0
and N are in the
same direction, but E
1
and N are in opposite directions.
Figure 13.12 shows the polygon of Figure 13.3 and a line segment intersecting
the polygon. The end points of the line segment are labeled as 0 and 1. The other
labeled points, 2 through 6, are inserted into the partition as the segment is processed
recursively through the tree. The right portion of the figure shows the BSP tree for the
polygon. The segments are denoted by (i
0
, i
1
). Initially segment (0, 1) is processed at
the root node of the tree. The segment is not split by the line containing edge 9, 0,
so region 0 does not contain any portion of the original segment. The segment (0, 1)
is sent to the negative child of the root and processed. The segment is split by the
line containing edge 0, 1, the new point labeled 2. Segment (0, 2) is on the positive
side of the splitting line, but the root node has no positive child, so segment (0, 2) is
contained in region 1. Segment (2, 1) is sent to the negative child and the process is
repeated.
The final partition leads to positive segments (0, 2), (3, 5), and (6, 1). The negative
segments are (2, 3), (5, 4), and (4, 6). Observe that the subsegments do not necessarily
alternate between positive and negative. In the previous example, the subsegments
(5, 4) and (4, 6) are adjacent, but both negative. An implementation of line segment
partitioning can trap these cases and combine adjacent same-sign segments into
single segments.
The pseudocode for line segment partitioning is listed below. The inputs are the
tree, polygon, and line segment end points V
0
and V
1
. The outputs are the four sets of
subsegments. The
Pos set contains those subsegments that are in positive regions, and
the
Neg set contains those subsegments that are in negative regions. The other two sets
store subsegments that are contained by edges that are coincident to splitting lines.
The
CoSame set contains subsegments contained by edges where each subsegment is
in the same direction as the edge. The
CoDiff set contains subsegments contained by
edges where each subsegment is in the opposite direction as the edge.
void GetPartition(BspTree T, Edge E, EdgeSet Pos, EdgeSet Neg,
EdgeSet CoSame, EdgeSet CoDiff)
{
type = Classify(T.line, E, SubPos, SubNeg);
if (type is CROSSES) {
GetPosPartition(T.posChild, SubPos, Pos, Neg, CoSame, CoDiff);
GetNegPartition(T.negChild, SubNeg, Pos, Neg, CoSame, CoDiff);
} else if (type is POSITIVE) {