• Aucun résultat trouvé

Liang-Barsky Polygon Clipping

Dans le document Computer Graphics and Geometric Modeling (Page 102-105)

3.3 Polygon-Clipping Algorithms

3.3.3 Liang-Barsky Polygon Clipping

This section gives a brief outline of the Liang-Barsky polygon-clipping algorithm ([LiaB83]). The algorithm is claimed to be twice as fast as the Sutherland-Hodgman clipping algorithm. This algorithm and the next one, the Maillot algorithm, base their success on their ability to detect turning points efficiently. Before we get to the algo-rithm, some comments on turning points are in order.

I S S I I I S I I I S I I1 7 1 2 1, 3 2 4 3, 5 4 6 5,andI S I I7 5 8 7. I I I S I I I I S I I2 3 4 3 5 6 7 8 6 1 2.

Figure 3.11. Weiler polygon clipping.

Assume that [P1,P2] is an edge of a polygon. It is easy to see that the only time that this edge is relevant to the issue of turning points is if it enters or exits a corner region associated to the window. Figure 3.12 shows some cases of polygons (the shaded regions) and how the exiting edge [P1,P2] affects whether or not the corner Cbecomes a turning point and needs to be added to the output. One sees the following:

(1) The analysis divides into two cases: whether the polygon is to the right or left of the edge. (Figure 3.12(a,b) versus Figure 3.12(c,d))

(2) There are two subcases that depend on which side of the ray from P2toCthe segment [P2,P3] is located.

(3) The decision as to whether a turning point will be needed cannot be made on the basis of only a few edges. In principle one might have to look at all the edges of the polygon first.

It is observation (3) that complicates life for polygon-clipping algorithms that process edges sequentially in one pass. One could simplify life and generate a turning point whenever we run into an edge that enters, lies in, or exits a corner region. The problem with this approach is that one will generate bogus edges for our clipped polygon. The polygon in Figure 3.12(c) would generate the “dangling” edge [B,C].

Bogus edges were already encountered in Sutherland-Hodgman clipping (but for dif-ferent reasons). These edges might not cause any problems, as in the case where one is simply filling two-dimensional regions. On the other hand, one would like to min-imize the number of such edges, but avoiding them entirely would be very compli-cated with some algorithms like the Liang-Barsky and Maillot algorithm.

With this introduction, let us describe the Liang-Barsky algorithm. We shall be brief because it does not contain much in the way of new insights given the fact that Figure 3.12. Different types of turning

points.

it is built on the approach they use in their line-clipping algorithm. Furthermore, another algorithm, the Maillot algorithm, is better. Our discussion here will follow [FVFH90], who have modified the algorithm so that it is easier to follow although it has the potential disadvantage of creating more bogus edges.

Consider Figure 3.13. We extend the edges of our rectangular window to divide the plane into nine regions. If we are at a point of the polygon that lies in one of the four outside regions that meet the window in an edge, then the next edge of the polygon can only meet the window in that edge. See Figure 3.13(a). On the other hand, if the point is in one of the four corner regions, then the next edge could meet the window in one of two possible edges. Without look-ahead, we really cannot tell whether the adjacent corner of the window will become a turning point and will have to be added to the output polygon. In such a situation we shall play it safe and always include the corner point (even though this may create some of these unwanted edges that we have been talking about). This is the first point to make about the algorithm.

The other point is that the algorithm rests on the idea of entry and exit points for the edges of the polygon that correspond to the entry and exit values used in the line-clipping algorithm described in Section 3.2.3. By analyzing these points one can tell if an edge intersects the window or if it gives rise to a turning point. As we work our way through the edges of the polygon, assume that the current edge e = [pi,pi+1] is neither vertical nor horizontal. Then e will intersect all the boundary lines of our window. If we parameterize the line containing ein the form pi+tpipi+1, then the four intersection points correspond to four parameter values and can again be classified as two entry and two exit points. We shall denote these associated parameter values by t_in1, t_in2, t_out1, and t_out2, respectively. It is easy to see that the smallest of these is an entry value and we shall let t_in1 be that one. The largest is an exit value and we shall let t_out2 be that one. Nothing can be said about the relative size of the remaining two values in general. From Section 3.2.3 we know, however, that if t_in2

<t_out1, then the line intersects the window and if t_out2 <t_in1, then the line inter-sects a corner region.

If a line does not intersect the window, then it must intersect three corner regions.

The conditions for that are that 0 <t_out1£1 and 0 <t_out2£ 1. The last statement also holds if the line intersects the window. Putting all these facts together leads to Algorithm 3.3.3.1. However, we were assuming in the discussion that edges were neither horizontal nor vertical. We could deal with such lines by means of special cases, but the easiest way to deal with them and preserve the structure of Algorithm 3.3.3.1 is to use a trick and assign dummy ±• values to the missing entering and leaving parameters. See [FVFH90].

Figure 3.13. Testing for turning points.

Dans le document Computer Graphics and Geometric Modeling (Page 102-105)