Document

CSCI 4620/8626
Computer Graphics
Clipping Algorithms (Chapter 8-5 …)
Last update: 2015-03-16
Clipping Algorithms
A clipping algorithm is any procedure that
eliminates those portions of a picture outside a
specified region of space (usually a rectangle in
standard position).
Considered here are 2D clipping algorithms for
Points
Lines (straight-line segments)
Fill areas
Curves
Text
2
2D Point Clipping
Point clipping is reasonably obvious.
If the point’s coordinates (x,y) satisfy the
following inequalities, then it is inside the clipping
region:xw  x  xw
min
max
yw min  y  yw max
If any of the inequalities is not met, then the point
is dropped (clipped).
3
1
2D Line Clipping (1)
Assume a rectangular clipping window in standard
position and a line segment (i.e. two vertices).
The results of a line clipping algorithm can be
(1) the line is entirely outside the clipping window; or
(2) the line is entirely inside the clipping window; or
(3) the line intersects the clipping window boundary;
part is inside and part is outside.
The most expensive part of the algorithm deals
with result (3), so we seek algorithms that
minimize the work needed for that result.
4
2D Line Clipping (2)
If both endpoints of a segment are inside the
clipping window (e.g. P1-P2 in Fig. 8-9), the
segment is entirely in the clipping window.
If both endpoints of a segment are outside any
one of the four boundaries of the clipping window
(e.g. P3-P4), the segment is entirely outside the
clipping window. (P9-P10 doesn’t fit this case!)
Otherwise the segment may intersect the boundary
of the clipping window and may extend into its
interior.
5
Figure 8-9 Clipping straight-line segments
using a standard rectangular clipping window.
2
2D Line Clipping (3)
One way to determine where a line segment
crosses a clipping window edge:
Assume (x0,y0) and (xend,yend) are the line segment
endpoints.
Use these parametric equations of the line:
•
•
x  x 0  ux end  x 0 
y  y 0  uy end  y 0 
0  u 1
Sequentially substitute the x/y window boundary values
(4 of them) for the x/y variables and solve for u. If u is
in [0,1], then solve for y/x (the other variable). If it’s in
the allowed range for the clipping window, then we clip
the line.
7
Clipping Efficiency (1)
The basic clipping algorithm just discussed is
straightforward, but inefficient.
Recall that floating point arithmetic is vastly slower
than integer arithmetic, and floating point division
is enormously slower than addition.
For example, on a 2.66GHz Intel Xeon
108 fp additions (or subtractions) took 515 msec
108 fp multiplications took 584 msec
108 fp divisions took 1168 msec
8
Clipping Efficiency (2)
Since “real” images typically have many line
segments, and clipping is done frequently, we
must be concerned about the time required to
perform the clipping operation.
Many clipping algorithms do more initial testing to
reduce the processing time for a set of line
segments.
Some of these are designed specifically for 2D
pictures, while others can be easily adapted to 3D.
9
3
Cohen-Sutherland Line Clipping
In this algorithm, each line endpoint is first
classified by assigning it a 4-bit region code
(sometimes called an “out” code).
Think of an infinite line through a clipping window
edge as separating the plane into two half-planes.
Each bit of the region code identifies the half-plane
in which a line endpoint lies.
0 is used if the endpoint lies in the half-plane that
contains the clipping region (“inside” half space).
1 is used if the endpoint lies in the half-plane that is
outside the clipping region (“outside” half space). 10
Figure 8-10 A possible ordering for the clipping
window boundaries corresponding to the bit positions
in the Cohen- Sutherland endpoint region code.
Figure 8-11 The nine binary region codes for
identifying the position of a line endpoint,
relative to the clipping-window boundaries.
4
Setting the Region Code Bits
It should be easy to see how the bits in the region
code for a line segment’s endpoint are set.
For example, given the region code bit
arrangement shown in figure 8-10, the value of bit
1 is the result of evaluating x  xw min
The other three bits in the region code are set in a
similar manner.
The bits are easily computed by keeping just the
sign bits of the differences; for example, the sign
bit of x – xwmin would be used for bit 1.
13
Simple Cases
Once region codes are established, it’s easy to
determine which line segments are completely
inside or outside the clipping window.
Any line that is completely within the clipping
window will have a region code of 0000 for each
endpoint.
Any line that is completely outside the clipping
window will have endpoint region codes with 1 in
the same bit position.
14
Simple Case Examples
It should be easy to see why 0000 is associated
with each endpoint of a segment within the
clipping window.
Imagine a line segment that begins to the left of
the clipping window and extends to the upper left.
Bit position 1 will be 1 for both endpoints, since each of
them is in the half-plane to the left of the clipping
window.
Bit position 4 will be 0 in one endpoint and 1 in the
other.
15
5
Implementation of Simple Cases
If we do a bitwise OR operation on the two region
codes and get 0000, then each region code must
have been 0000, and the line segment is
completely within the clipping region; keep it.
If we do a bitwise AND operation on the two
region codes and get a non-zero value, then one of
the same bits in each region code must have been
1; discard the line segment as being completely
outside the clipping region.
Recall that zero/non-zero values are treated as
false/true in C/C++.
16
Not So Simple…
Lines that can’t be easily classified must be
checked for intersection with the clipping window
boundaries. This may require several intersection
calculations.
Note that a line segment can intersect several
clipping window boundary lines (that is, cross
between multiple of the half-planes) without
necessarily entering the interior of the clipping
window.
17
Figure 8-12 Lines extending from one clipping-window region
to another may cross into the clipping window, or they could
intersect one or more clipping boundaries without entering the
window.
6
Does It Cross?
To determine if a line segment crosses from one
half-plane to another (and potentially into the
clipping window), we examine the corresponding
bit positions in the two region codes.
If the value of the bits in corresponding positions
differ, then the segment crosses the boundary line
between the two half-planes.
As each clipping-window edge is processed, a
section of the line is clipped, and the remainder of
the line is checked against the other borders.
19
Termination Conditions
After each “clip” of a line segment, the modified
segment is checked to see if it is totally inside or
outside the clipping window. If so, we’re done.
Otherwise, the modified line segment is checked
against the remaining clipping window borders.
It is possible that an intersection position may be
calculated at each of the four clipping boundaries,
as illustrated in Figure 8-13.
20
Figure 8-13 Four intersection positions (labeled from 1 to 4)
for a line segment that is clipped against the window
boundaries in the order left, right, bottom, top.
7
Boundary Intersections (1)
It’s relatively easy to determine a boundary
intersection for a line segment.
Given (x0, y0) and (xend, yend) as the line endpoints,
and x is the position of a vertical clipping border
line (i.e. xwmin or xwmax), compute the y coordinate
of the intersection as follows:
m  y end  y 0 /x end  x 0 
y  y 0  mx  x 0 
22
Boundary Intersections (2)
If we’re looking for the intersection with a
horizontal border, then the x coordinate can be
calculated as follows (where y is ywmin or ywmax):
x  x0 
y  y0
m
23
Liang-Barsky Line Clipping
Cyrus and Beck, then later Liang and Barsky, developed
faster line-clipping algorithms based on additional line
testing using the parametric form for lines.
Given the usual endpoint definitions, the line can be
parametrically described as follows (same as before):
24
8
Combine with Point-Clipping Conditions
Liang-Barsky then combines the parametric line
equations with the point-clipping conditions seen
earlier.
This yields these four inequalities (given in pairs):
xwmin  x 0  ux  xw max
ywmin  y 0  uy  yw max
25
Rewriting the Inequalities
The four inequalities can be rewritten like this:
u pk  qk
k  1,2,3,4
p and q are defined as
26
Example: p1 and q1 Derivation
Let’s consider one inequality:
Subtract uΔx from each side:
Subtract xwmin from each side:
If we now define p1 = -Δx and q1 = x0 – xwmin, we
obtain the expression given for the first inequality:
up1 £ q1
The remaining inequalities can be rewritten to
yield the other cases for upk ≤ qk .
27
9
Cases to Consider
If pk = 0, then the line is parallel to a boundary. If
qk < 0 for the same k, then the line can be
discarded. If qk ≥ 0 then the corresponding pointclipping inequality is satisfied.
If pk < 0, then the segment potentially enters the
clipping window across the k-th boundary; call this
case PE.
If pk > 0, then the segment potentially leaves the
clipping window across the k-th boundary; call this
case PL.
28
Definitions
Let KPE = { k : pk < 0 } denote the set of indices of
clipping boundaries across which the line
potentially enters the clip rectangle.
Similarly let KPL = { k : pk > 0 } denote the set of
indices of clipping boundaries across which the line
potentially leaves the clip rectangle.
Let uk = qk / pk represent the parameter value at
the k-th boundary crossing.
29
Visible Segment
With those definitions, the visible portion of the
line segment is that where the parameter value u
is in the range umin £ u £ umax
where
umin = max [0,uk ] umax = min [1,uk ]
kÎK PE
kÎK PL
If umin > umax then the entire segment is clipped.
30
10
Example of Liang-Barsky
“In the fullness of time” there will be a slide or two
that demonstrates Liang-Barsky with a specific set
of lines and a specific clipping window.
31
Nicholl-Lee-Nicholl Line Clipping
The Nicholl-Lee-Nicholl (NLN) line clipping
algorithm creates more regions around the clipping
window to avoid multiple line-intersection
calculations, thus using fewer comparisons and
divisions.
The tradeoff is that Cohen-Sutherland and LiangBarsky can be easily extended to three dimensions,
but NLN cannot.
32
NLN Rationale
In general, a line segment (being clipped) can
intersect all four of the (extended) clipping window
boundaries (review fig. 8-13). At most two of these
intersections are relevant to the clipping problem.
We assume most of the computational overhead is
associated with finding intersections, so it follows
that irrelevant intersection computations should be
avoided.
Only three cases need to be considered.
33
11
NLN - Initial Testing
Initially, a line’s endpoints are assigned region
codes as before.
Then the “easy” tests to determine if the line is
entirely inside or outside the clipping region are
performed. Obviously we’re done (as before) if
one of the easy tests succeeds.
Otherwise NLN proceeds to set up additional
clipping regions.
34
Endpoint Positions to Consider
One of the endpoints (called P0) of each line
considered by NLN must be in one of three
positions. That is, P0 must have one of three
region codes:
0000 = inside the clipping region
0001 = immediately left of the clipping region
1001 = left and above the clipping region
If P0 isn’t in one of those regions, but Pend is, then
P0 and Pend can be swapped.
The other endpoint positions can be mapped into
one of the three positions by rotating the line and
the clipping region by 90, 180, or 270 degrees.
35
Figure 8-14 Three possible positions for a line
endpoint P0 in the NLN line-clipping algorithm.
12
NLN: P0 inside the window
If P0 is inside the clipping window, and Pend is
outside, then four regions are defined as shown in
figure 8-15.
These regions are defined by the semi-infinite lines
beginning with P0 and extending through the four
vertices defining the corners of the clipping
window.
The region containing P0 – Pend is easily identified
by comparing the line’s slope with the slopes of the
lines bordering the regions (more later).
37
Figure 8-15 The four regions used in the NLN
algorithm when P0 is inside the clipping window
and Pend is outside.
NLN: P0 left of the window
As before, four lines are drawn from P0 through
the vertices of the clipping window (see fig. 8-16).
We now have four regions, labeled for the clipping
window edges the line could intersect: L (inside
the window), LT, LR, and LB.
If Pend is in L, it is easily identified by its region
code (0000).
The other regions are identified, as before, by
comparing the slope of P0 – Pend with the slopes of
the region boundary lines.
39
13
Figure 8-16 The four clipping regions
used in the NLN algorithm when P0 is
directly to the left of the clip window.
NLN: P0 above left of the window
The regions in this case depend on whether P0 is
closer to the (extended) left boundary edge or the
top boundary edge of the clipping window.
These two cases are shown in fig. 8-17 (a) and
(b), along with the various regions they define;
labeled for the edges the clipped line intersects.
The region containing Pend – and therefore the
edge(s) of the clipping window that are intersected
– is(are) identified by comparing the slope of P0 –
Pend with the slope of the boundary lines of the
regions.
41
Figure 8-17 The two possible sets of clipping
regions used in the NLN algorithm when P0 is
above and to the left of the clipping window.
14
Slopes
Assume the line being clipped has endpoint
coordinates (x0,y0) and (xend,yend).
The slope of this line is easily seen to be
yend - y0
xend - x0
In the case illustrated in fig. 8-16, the slopes of the
lines bounding the top regions (L and LT) are
yT - y0
x R - x0
and
yT - y0
x L - x0
43
Slope Comparisons
If the line being clipped is in region L or LT, then
we must have
yT - y0 yend - y0 yT - y0
£
£
x R - x0 xend - x0 x L - x0
Look at the first (left) inequality (as an example).
It can be rewritten as
( yT - y0 ) ( xend - x0 ) £ ( yend - y0 ) ( xR - x0 )
Note this involves no divisions!
44
Intersection Calculations
Once the clipping window edge(s) that intersect
the line being clipped is(are) identified, the
intersection point can be determined using only a
single division for each intersection.
The coordinate difference calculations and product
calculations used in the slope comparisons are
saved and reused in the intersection calculations.
Although only a single region was used here as an
example, the work for other regions is very similar.
45
15