i
i
i
i
i
i
i
i
294 12. Data Structures for Graphics
function add ( triangle T )
if (f(a) < 0 and f(b) < 0 and f(c) < 0) then
if (negative subtree is empty) then
negative-subtree = node(T )
else
negative-subtree.add (T )
else if (f(a) > 0 and f(b) > 0 and f(c) > 0) then
if positive subtree is empty then
positive-subtree = node(T )
else
positive-subtree.add (T )
else
we have assumed this case is impossible
a
b
c
A
B
Figure 12.37. Whenatri-
angle spans a plane, there
will be one vertex on one
side and two on the other.
The only thing we need to fix is the case where the triangle crosses the dividing
plane, as shown in Figure 12.37. Assume, for simplicity, that the triangle has
vertices a and b on one side of the plane, and vertex c is on the other side. In this
case, we can find the intersection points A and B and cut the triangle into three
new triangles with vertices
T
1
=(a, b, A),
T
2
=(b, B , A),
T
3
=(A, B, c),
as shown in Figure 12.38. This order of vertices is important so that the direction
of the normal remains the same as for the original triangle. If we assume that
f(c) < 0, the following code could add these three triangles to the tree assuming
the positive and negative subtrees are not empty:
positive-subtree = node (T
1
)
positive-subtree = node (T
2
)
negative-subtree = node (T
3
)
A precision problem that will plague a naive implementation occurs when a vertex
is very near the splitting plane. For example, if we have two vertices on one side of
the splitting plane and the other vertex is only an extremely small distance on the
Figure 12.38. When a
triangle is cut, we break it
into three triangles, none
of which span the cutting
plane.
other side, we will create a new triangle almost the same as the old one, a triangle
that is a sliver, and a triangle of almost zero size. It would be better to detect this
as a special case and not split into three new triangles. One might expect this case
to be rare, but because many models have tessellated planes and triangles with