Clipping: Introduction To Computer Graphic S

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

INTRODUCTION TO COMPUTER GRAPHIC S

Clipping

Andries van Dam October 1st, 2009 2D Clipping 1/16


INTRODUCTION TO COMPUTER GRAPHIC S

Clipping
Chapter 13, Section 10

Andries van Dam October 1st, 2009 2D Clipping 2/16


INTRODUCTION TO COMPUTER GRAPHIC S

Line Clipping
• Clipping endpoints

(Xmax , Ymax)

(Xmin , Ymin)

xmin < x < xmax and ymin < y < ymax point inside

• Endpoint analysis for lines:

– if both endpoints in , do “trivial acceptance”


– if one endpoint inside, one outside, must clip
– if both endpoints out, don’t know

• Brute force clip: solve simultaneous equations


using y = mx + b for line and four clip edges

– slope-intercept formula handles infinite lines only


– doesn’t handle vertical lines

Andries van Dam October 1st, 2009 2D Clipping 3/16


INTRODUCTION TO COMPUTER GRAPHIC S

Parametric Line Formulation


For Clipping
• Parametric form for line segment

X = x0 + t(x1 – x0) 0<t<1


Y = y0 + t(y1 – y0)

P(t) = P0 + t(P1 – P0)

• “true,” i.e., interior intersection, if sedge and


tline in [0,1]

Andries van Dam October 1st, 2009 2D Clipping 4/16


INTRODUCTION TO COMPUTER GRAPHIC S

Outcodes for Cohen-Sutherland


Line Clipping in 2D
• Divide plane into 9 regions
• Compute the sign bit of 4 comparisons between a vertex and an edge
– ymax – y; y – ymin; xmax – x; x - xmin
– point lies inside only if all for sign bits are 0, otherwise exceeds edge

Clip Rectangle

• 4 bit outcode records results of four bounds tests:

First bit: outside halfplane of top edge, above top edge


Second bit: outside halfplane of bottom edge, below bottom edge
Third bit: outside halfplane of right edge, to right of right edge
Fourth bit: outside halfplane of left edge, to left of left edge

• Lines with OC0 = 0 and OC1 = 0 can be trivially accepted

• Lines lying entirely in a half plane outside an edge can be trivially


rejected: OC0 AND OC1  0 (i.e., they share an “outside” bit)
Andries van Dam October 1st, 2009 2D Clipping 5/16
INTRODUCTION TO COMPUTER GRAPHIC S

Outcodes for Cohen-Sutherland


Line Clipping in 3D
• Very similar to 2D
• Divide volume into 27 regions (Picture a Rubik’s cube)
• 6-bit outcode records results of 6 bounds tests
Top plane Bottom plane Front plane
001000 (above) 000000 (above) 010000 (in front)
000000 (below) 000100 (below) 000000 (behind)

Left plane Right plane Back plane


000001 (to left of) 000000 (to left of) 000000 (in front)
000000 (to right of) 000010 (to right of) 100000 (behind)

First bit: outside back plane, behind back plane


Second bit: outside front plane, in front of front plane
Third bit: outside top plane, above top plane
Fourth bit: outside bottom plane, below bottom plane
Fifth bit: outside right plane, to right of right plane
Sixth bit: outside left plane, to left of left plane

• Lines with OC0 = 0 and OC1 = 0 can be trivially accepted

• Lines lying entirely in a volume on outside of a plane can be


trivially rejected: OC0 AND OC1  0 (i.e., they share an
“outside” bit)
Andries van Dam October 1st, 2009 2D Clipping 6/16
INTRODUCTION TO COMPUTER GRAPHIC S

Cohen-Sutherland Algorithm
• If we can neither trivially reject/accept,
divide and conquer
• subdivide line into two segments; then T/A
or T/R one or both segments:
D
C
B I
A H

Clip G
rectangle F

E
– use a clip edge to cut line
– use outcodes to choose edge that is crossed
• Edges where the two outcodes differ at that
particular bit are crossed
– pick an order for checking edges

• top – bottom – right – left


– compute the intersection point
• the clip edge fixes either x or y
• can substitute into the line equation
– iterate for the newly shortened line
– “extra” clips may happen (e.g., E-I at H)

Andries van Dam October 1st, 2009 2D Clipping 7/16


INTRODUCTION TO COMPUTER GRAPHIC S

Pseudocode for the Cohen-


Sutherland Algorithm
• y = y0 + slope*(x - x0) and x = x0 + (1/slope)*(y - y0)

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;
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;

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

Andries van Dam October 1st, 2009 2D Clipping 8/16


INTRODUCTION TO COMPUTER GRAPHIC S

Scan Conversion after Clipping


a) Don’t round and then scan convert:
calculate decision variable based on pixel chosen on left edge

b) Horizontal edge problem:


clipping/rounding produces pixel A; to get pixel B, round up x of
the intersection of line with y = ymin - ½ and pick pixel above:

x = xmin

B A
y = ymin
y = ymin – 1/2

y = ymin – 1

Andries van Dam October 1st, 2009 2D Clipping 9/16


INTRODUCTION TO COMPUTER GRAPHIC S

Sutherland-Hodgman Polygon
Clipping

Andries van Dam October 1st, 2009 2D Clipping 10/16


INTRODUCTION TO COMPUTER GRAPHIC S

Cyrus-Beck/Liang-Barsky
Parametric Line Clipping-1
• Use parametric line formulation
P(t) = P0 + (P1 – P0)t
• Determine where the line intersects the
infinite line formed by each edge by solving
for t 4 times. Decide which of these
intersections actually occur on the rectangle

• For any point PEi on edge Ei


Andries van Dam October 1st, 2009 2D Clipping 11/16
INTRODUCTION TO COMPUTER GRAPHIC S

C-B/L-B Param. Line Clipping-2


Now solve for the value of t at the intersection
of P0 P1 with the edge Ei:
Ni • [P(t) – PEi] = 0
First, substitute for P(t):
Ni • [P0 + (P1 – P0)t – PEi] = 0
Next, group terms and distribute dot product:
Ni • [P0 – PEi] + Ni • [P1 – P0]t = 0
Let D be the vector from P0 to P1 = (P1 – P0),
N [P  P ]
and solve for t: t  i 0 E i
N D
i

Note that this gives a valid value of t only if the denominator


of the expression is nonzero.

For this to be true, it must be the case that:


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).

The algorithm checks these conditions.


Andries van Dam October 1st, 2009 2D Clipping 12/16
INTRODUCTION TO COMPUTER GRAPHIC S

C-B/L-B Param. Line Clipping-3


• Eliminate t’s outside [0,1] on the line
• Which remaining t’s produce interior intersections?
• Can’t just take the innermost t values!

• Move from P0 to P1; for a given edge, just before


crossing: if Ni • D < 0 Potentially Entering (PE),
if Ni • D > 0 Potentially Leaving (PL)
• Pick inner PE, PL pair: tE for PPE with max t,
tL for PPL with min t, and tE > 0, tL < 1.
• If tL < tE, no intersection

Andries van Dam October 1st, 2009 2D Clipping 13/16


INTRODUCTION TO COMPUTER GRAPHIC S

Pseudocode for Cyrus-Beck/


Liang-Barsky Line Clipping
Algorithm
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 October 1st, 2009 2D Clipping 14/16


INTRODUCTION TO COMPUTER GRAPHIC S

Calculations for Parametric


Line Clipping for Upright Clip
Rectangle (1/2)
• D = P1 – P0 = (x1 – x0, y1 – y0)
• Leave PEi as an arbitrary point on the clip
edge; it’s a free variable and drops out

Calculations for Parametric Line Clipping Algorithm

Clip Edgei Normal Ni PEi P0-PEi N  (P  P )


i 0 Ei
t
N D
i

left: x = xmin (-1,0) (xmin, y) (x0- xmin,y0-y)  (x  x


0 min
)
(x  x )
1 0

right: x = xmax (1,0) (xmax,y) (x0- xmax,y0-y)  (x  x )


0 max
(x  x )
1 0

bottom: y = ymin (0,-1) (x, ymin) (x0-x,y0- ymin) (y  y )


0 min
(y  y )
1 0

top: y = ymax (0,1) (x, ymax) (x0-x,y0- ymax) (y  y )


0 max
(y  y )
1 0

Andries van Dam October 1st, 2009 2D Clipping 15/16


INTRODUCTION TO COMPUTER GRAPHIC S

Calculations for 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

Andries van Dam October 1st, 2009 2D Clipping 16/16

You might also like