CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Clipping
Concepts, Algorithms for line clipping
Andries van Dam
Clipping - 10/16/12
1 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Line Clipping in 2D
Clipping endpoints
Endpoint analysis for lines:
If < < and < < , the point is inside the clip rectangle. if both endpoints in , do trivial acceptance if one endpoint inside, one outside, must clip if both endpoints out, dont know
( , )
( , )
( , )
Brute force clip: solve simultaneous equations using = + for line and four clip edges
Andries van Dam
slope-intercept formula handles infinite lines only doesnt handle vertical lines
Clipping - 10/16/12
2 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Parametric Line Formulation For Clipping
Parametric form for line segment
Line is in clip rectangle if parametric variables tline and sedge both in [0,1] at intersection point between line and edge of clip rectangle
= 0 + (1 0 ) Y = 0 + (1 0 ) 0 1 = 0 + 1 0 = 1 0 + 1
Slow, must intersect lines with all edges
Andries van Dam
Clipping - 10/16/12
3 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Cohen-Sutherland Line Clipping in 2D
Divide plane into 9 regions Compute the sign bit of 4 comparisons between a vertex and an edge
Clip Rectangle
4 bit outcode records results of four bounds tests:
; ; ; point lies inside only if all four sign bits are 0, otherwise exceeds edge First bit: Second bit: Third bit: Fourth bit: above top edge below bottom edge to the right of right edge to the left of left edge
Compute outcodes for both vertices of each line (denoted OC0 and OC1) Lines with OC0 = 0 (i.e., 0000) and OC1 = 0 can be trivially accepted. Lines lying entirely in a half plane outside an edge can be trivially rejected if (OC0 AND OC1) 0 (i.e., they share an outside bit)
Andries van Dam Clipping - 10/16/12
4 of 16
Cohen-Sutherland Line Clipping in 3D
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Very similar to 2D Divide volume into 27 regions (Picture a Rubiks cube) 6-bit outcode records results of 6 bounds tests
Back plane
000000 (in front) 100000 (behind)
Front plane
010000 (in front) 000000 (behind)
Andries van Dam
Again, lines with OC0 = 0 and OC1 = 0 can be trivially accepted Lines lying entirely in a volume outside of a plane can be trivially rejected: OC0 AND OC1 0 (i.e., they share an outside bit)
Clipping - 10/16/12
First bit: behind back plane Second bit: in front of front plane Third bit: above top plane Fourth bit: below bottom plane Fifth bit: to the right of right plane Sixth bit: to the left of left plane
Top plane
001000 (above) 000000 (below)
Bottom plane
000000 (above) 000100 (below)
Right plane
000000 (to left of) 000010 (to right of)
Left plane
000001 (to left of) 000000 (to right of)
5 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Cohen-Sutherland Algorithm (1/3)
If we can neither trivially accept/reject (T/A, T/R), D divide and conquer Subdivide line into two segments; then T/A or T/R one or both segments:
use a clip edge to cut line use outcodes to choose the edges that are crossed
pick an order for checking edges: top bottom right left compute the intersection point
for a given clip edge, if a lines two outcodes differ in the corresponding bit, the line has one vertex on each side of the edge, thus crosses the clip edge fixes either x or y can substitute into the line equation
Clip rectangle F G
Andries van Dam
iterate for the newly shortened line, extra clips may happen (e.g., E-I at H)
Clipping - 10/16/12
6 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Cohen-Sutherland Algorithm (2/3)
= 0 + ( 0 ) and = 0 + Algorithm:
( 0 )
ComputeOutCode(x0, y0, outcode0); ComputeOutCode(x1, y1, outcode1);
repeat check for trivial reject or trivial accept pick the point that is outside the clip rectangle if TOP then x = x0 + (x1 x0) * (ymax y0) / (y1 y0); y = ymax; else if BOTTOM then x = x0 + (x1 x0) * (ymin y0) / (y1 y0); y = ymin; Andries van Dam Clipping - 10/16/12
if (x0, y0 is the outer point) then x0 = x; y0 = y; ComputeOutCode(x0, y0, outcode0) else x1 = x; y1 = y; ComputeOutCode(x1, y1, outcode1) until done 7 of 16
else if RIGHT then y = y0 + (y1 y0) * (xmax x0) / (x1 x0); x = xmax; else if LEFT then y = y0 + (y1 y0) * (xmin x0) / (x1 x0); x = xmin;
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Cohen-Sutherland Algorithm (3/3)
xmin = ymin = -1; xmax = ymax = 1; zmin = -1; zmax = 0;
Similar algorithm for using 3D outcodes to clip against canonical parallel view volume:
else if LEFT then y = y0 + (y1 y0) * (xmin x0) / (x1 x0); z = z0 + (z1 z0) * (xmin x0) / (x1 x0); x = xmin; else if NEAR then x = x0 + (x1 x0) * (zmax z0) / (z1 z0); y = y0 + (y1 y0) * (zmax z0) / (z1 z0); z = zmax; else if FAR then x = x0 + (x1 x0) * (zmin z0) / (z1 z0); y = y0 + (y1 y0) * (zmin z0) / (z1 z0); z = zmin;
ComputeOutCode(x0, y0, z0, outcode0); ComputeOutCode(x1, y1, z1, outcode1); repeat check for trivial reject or trivial accept pick the point that is outside the clip rectangle if TOP then x = x0 + (x1 x0) * (ymax y0) / (y1 y0); z = z0 + (z1 z0) * (ymax y0) / (y1 y0); y = ymax; else if BOTTOM then x = x0 + (x1 x0) * (ymin y0) / (y1 y0); z = z0 + (z1 z0) * (ymin y0) / (y1 y0); y = ymin; else if RIGHT then y = y0 + (y1 y0) * (xmax x0) / (x1 x0); z = z0 + (z1 z0) * (xmax x0) / (x1 x0); x = xmax;
Andries van Dam
Clipping - 10/16/12
if (x0, y0, z0 is the outer point) then x0 = x; y0 = y; z0 = z; ComputeOutCode(x0, y0, z0, outcode0) else x1 = x; y1 = y; z1 = z; ComputeOutCode(x1, y1, z1, outcode1) until done
8 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Scan Conversion after Clipping
Dont round and then scan convert, because the line will ( , ( + )) have the wrong slope: calculate decision variable based on pixel ( , + ) chosen on left edge (remember: = + ) Horizontal edge problem:
Clip rectangle
x = xmin
Clipping/rounding produces pixel A; to get pixel B, round up x of the intersection of line with = and pick pixel above:
Clipping - 10/16/12
y = ymin y = ymin 1/2 y = ymin 1
Andries van Dam
9 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Sutherland-Hodgman Polygon Clipping
The 2D Sutherland-Hodgman algorithm generalizes to higher dimensions
We can use it to clip polygons to the 3D view volume one plane at a time Section 36.5 in textbook
Andries van Dam
10 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Cyrus-Beck/Liang-Barsky Parametric Line Clipping (1/3)
Use parametric line formulation: Determine where line intersects the infinite line formed by each clip rectangle edge
= 0 + 1 0
Andries van Dam
For : use any point on edge Ei
Clipping - 10/16/12
solve for t multiple times depending on the number of clip edges crossed decide which of these intersections actually occur on the rectangle
11 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Cyrus-Beck/Liang-Barsky Parametric Line Clipping (2/3)
Now solve for the value of t at the intersection of P0 P1 with the edge Ei: Let D be the vector from P0 to P1 = (P1 P0), and solve for t:
First, substitute for P(t):
Next, group terms and distribute dot product: For this to be true, it must be the case that:
0 + 1 0 = 0 = [0 ]
[0 + 1 0 ] = 0
= 0
note that this gives a valid value of t only if the denominator of the expression is nonzero.
Andries van Dam
The algorithm checks these conditions.
Clipping - 10/16/12
Ni 0 (that is, the normal should not be 0; this could occur only as a mistake) D 0 (that is, P1 P0) Ni D 0 (edge Ei and line D are not parallel; if they are, no intersection).
12 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Cyrus-Beck/Liang-Barsky Parametric Line Clipping (3/3)
Eliminate s outside [0,1] on the line Which remaining s produce interior intersections? Cant just take the innermost values! Move from P0 to P1; for a given edge, just before crossing:
Andries van Dam
Pick inner PE/PL pair: for with max , for with min , and > 0, < 1 If < , no intersection
Clipping - 10/16/12
if Ni D < 0 Potentially Entering (PE); if Ni D > 0 Potentially Leaving (PL)
13 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Pre-calculate Ni and select PEi for each edge; for each line segment to be clipped if P1 = P0 then line is degenerate so clip as a point; else begin tE = 0; tL = 1; for each candidate intersection with a clip edge if Ni D 0 then {Ignore edges parallel to line} begin calculate t; {of line and clip edge intersection} use sign of Ni D to categorize as PE or PL; if PE then tE = max(tE,t); if PL then tL = min(tL,t); end if tE > tL then return nil else return P(tE) and P(tL) as true clip intersections end
Andries van Dam Clipping - 10/16/12
Cyrus-Beck/Liang-Barsky Line Clipping Algorithm
14 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Parametric Line Clipping for Upright Clip Rectangle (1/2)
D = P1 P0 = (x1 x0, y1 y0)
Leave PEi as an arbitrary point on clip edge: its a free variable and drops out
left: x = xmin Clip Edgei
Calculations for Parametric Line Clipping Algorithm
Normal Ni (-1,0) (1,0) (xmin, y) PEi P0-PEi
t=
N (P P ) i 0 Ei N D i
right: x = xmax
top: y = ymax Andries van Dam
bottom: y = ymin
(0,1)
(0,-1)
(xmax,y)
(x0- xmin,y0-y)
) (x x 0 min (x x ) 1 0 (x x ) 0 max (x x ) 1 0 (y y ) 0 min (y y ) 1 0 ) (y y 0 max (y y ) 1 0
(x, ymin)
(x0- xmax,y0-y) (x0-x,y0- ymin)
(x, ymax)
(x0-x,y0- ymax)
Clipping - 10/16/12
15 of 16
CS123 | I NTRODUCTI ON TO COM PUTER GRAPHI CS
Parametric Line Clipping for Upright Clip Rectangle (2/2)
Examine t:
Numerator is just the directed distance to an edge; sign corresponds to OC Denominator is just the horizontal or vertical projection of the line, dx or dy; sign determines PE or PL for a given edge Ratio is constant of proportionality: how far over from P0 to P1 intersection is relative to dx or dy
Clipping - 10/16/12
Andries van Dam
16 of 16