3

Click here to load reader

Why$clip?$ Computer$Graphics$ - University of Michiganweb.eecs.umich.edu/~sugih/courses/eecs487/lectures/05-LineClipping.… · Computer$Graphics$ Lecture$5:$$ • Finish$up$line$rasterization$

  • Upload
    vothu

  • View
    213

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Why$clip?$ Computer$Graphics$ - University of Michiganweb.eecs.umich.edu/~sugih/courses/eecs487/lectures/05-LineClipping.… · Computer$Graphics$ Lecture$5:$$ • Finish$up$line$rasterization$

EECS$487:$Interactive$

Computer$Graphics$

Lecture$5:$$

•  Finish$up$line$rasterization$•  Line$clipping$

Clipping$

Clip$against$viewing$$

window/viewport$

Why$clip?$

• avoids$rasterizing$outside$window$

•  speeds$up$rasterization$•  prevents$memory$errors$

•  avoids$divide$by$0$and$overflows$

•  in$3D,$clip$against$view$volume$

•  polygons$that$are$too$close$can$obscure$view$

•  those$too$far$shouldn’t$be$visible$and$could$mess$up$depth$buffer$

Clipping$Line$Segments$

How$to$clip?$

• preprocessing$to$exclude/include$trivial$cases$•  accept/reject$bitmask$test:$CohenOSutherland$

• clip$the$intersecting$cases:$•  parametric$line$trimming:$CyrusOBeck$

?

p0

p1 p2

p3

p4

p5 p6

p7

p8

p9

p4

p5 p6

p7

p’8

p’9

before$clipping$ after$clipping$

CohenOSutherland$

Trivial$Accept/Reject$test:$compute$a$4Obit$“outcode”$(Li)$for$each$end$point$(pi):$

0000

1001 1000 1010

0010 0001

0110 0100 0101

1111 xpi < xmin

xpi > xmax

ypi < ymin

ypi > ymax

x

y

Accept$line$if$L0 | L1 = 0 (bitwise$or)$

both$points$are$inside$window$

Reject$line$if$L0 & L1 ≠ 0$(bitwise$and)$

line$is$outside$window$

Else$may$need$clipping$

p1

p0

L

xmin xmax

ymax

ymin

rightmost$bit$ bit$2

bit$3

leftmost$bit$

Page 2: Why$clip?$ Computer$Graphics$ - University of Michiganweb.eecs.umich.edu/~sugih/courses/eecs487/lectures/05-LineClipping.… · Computer$Graphics$ Lecture$5:$$ • Finish$up$line$rasterization$

Line$Clipping$Examples$

A0 = 1001 A1 = 1000 A0 | A1 = 1001 A0 & A1 = 1000 A is$Rejected$$

F0 = 0000 F1 = 0100 F0 | F1 = 0100 F0 & F1 = 0000 F needs$clipping$$

E0 = 0000 E1 = 0000 E0 | E1 = 0000 E is$Accepted$$

H0 = 0100 H1 = 0010 H0 | H1 = 0110 H0 & H1 = 0000 H needs$clipping?$

$ $ $I$needs$clipping?$

Accept$if$L0 | L1 = 0 Reject$if$L0 & L1 ≠ 0

1111 xpi

< xmin

xpi > xmax

ypi < ymin

ypi > ymax

E 0

1

A 0

1

xmin xmax

ymax

ymin

F 0

1

H 0

1

I 0

1

Each$‘1’$in$the$outcode$indicates$the$line$intersecting$an$edge,$e.g.,$0100$means$intersection$with$yOmin$

Starting$from$the$left$most$outside$$

point$(A$in$example),$going$left$to$right$

(e.g.,)$on$$the$outcode,$compute$the$

intersection$with$the$window$edge$that$

cuts$the$line$into$two$segments$

Test$the$outcodes$of$each$segment,$

clip$the$segment(s)$outside$the$window$

Recurse$until$all$segments$are$checked$

Line$Clipping$CohenOSutherland$

A C

D

B

E

1010

0100

Normal$Vectors$

What$is$the$normal$vector$of$a$line?$

A$vector$perpendicular$to$the$line$

$

What$is$a$unit$normal?$

Normal$vector$of$magnitude$one: n/||n||$

Normal$vectors$are$important$to$many$graphics$

calculations$

u

n

Implicit$Line$Eqn.$Using$Vectors$

Let$n$be$a$normal$vector$of$the$line$and$q$a$point$on$the$line$

•  the$point$p$is$on$the$line$iff$f (p) = n•(p – q) = 0 •  the$point$p’$is$above$the$line$iff f (p’) > 0$(θ’ < 90˚)$•  the$point$p”$is$below$the$line$iff$f (p”) < 0$(θ” > 90˚)$•  if$f (p) ≠ 0,$p’s$projection$onto$n$has$a$nonOzero$length$

θ$measured$in$the$direction$of$travel$

q

n

p θ’

p’

p” θ”

Page 3: Why$clip?$ Computer$Graphics$ - University of Michiganweb.eecs.umich.edu/~sugih/courses/eecs487/lectures/05-LineClipping.… · Computer$Graphics$ Lecture$5:$$ • Finish$up$line$rasterization$

i

Compute$the$intersection$between$line$u$and$edge$i Let:$

•  u$the$vector$from$p0$to$p1:$u = (p1 – p0) •  ni$be$the$normal$of$edge$i,$pointing$away$from$the$clipping$window$

•  pei$an$arbitrary$point$on$edge$i$

then:$

•  if$ni•u = 0$the$line$is$parallel$to$the$edge$i •  otherwise,$let$p(ti)$be$the$intersection$of$u!and$edge$i •  solve$for$ti$(repeat$for$each$of$the$four$edges):!

CyrusOBeck$Line$Clipping$

ni • [p(ti )− pei ] = 0ni • [p0 + ti (p1 − p0 )− pei ] = 0

ni • p0 − pei⎡⎣ ⎤⎦ + ni • tiu = 0

ti =ni • [p0 − pei ]

−ni • u=ni • [pei − p0 ]ni • u

p0 = p(0)

p1 = p(1)

L

ni

Foley$et$al.94$

u pei p(ti)

CyrusOBeck$Line$Clipping$

Let$two$sides$of$the$clipping$window$define$a$

region$E(nter)$that$the$line$enters$and$never$leaves$$

Let$the$other$two$sides$define$a$region$L(eave)$that$the$line$starts$in$and$eventually$leaves$

$

(Algorithm$determines$E$and$L$automatically!)$

$

The$dot$products$of$the$line$and$

the$normal$(n)$of$the$boundary$edges$determine$the$parameters$

(the$t’s)$at$the$intersection$points$

p0 = p(0)

p1 = p(1)

L

n

n

E

Now$classify$each$intersection$point$as$$

Potentially$Entering$(PE)$or$$

Potentially$Leaving$(PL)$at$edge$i:$•  if$ni•u < 0,$intersection$is$PE$(why?)$

•  if$ni•u > 0,$intersection$is$PL$(why?)$

$

Let$tL$be$the$MIN$of$the$ti’s$that$are$PL$$

and$tE$the$MAX$of$the$ti’s$that$are$PE$

•  if$tL < tE , the$line$is$outside$the$clipping$window$(Line$2)$•  otherwise$(tE, tL)$are$the$clipped$line’s$bounding$points$•  in$case$actual$line$segment$starts$or$ends$inside$window,$tE < 0 or$

tL > 1$respectively,$we$let$max(0, tE)$and$min(1, tL)$be$the$clipped$line’s$bounding$points$

PE

PE

PL

Line$2

tL

PE

CyrusOBeck$Line$Clipping$

ni

tE

PL

Line$1

u

PL

Foley$et$al.94$

tE

tL

CyrusOBeck$Line$Clipping$

Whereas$CohenOSutherland$is$limited$to$

upright$rectangle,$CyrusOBeck$works$well$with$

arbitrary$convex$polygon$as$clipping$area$

E

PE

L PL