24
Courtesy of Andries van Dam © Clipping Concepts, Algorithms for line clipping 1

Clipping - Washington University in St. Louisfurukawa/cse452/slides/09_clipping.pdf · Sutherland-Hodgman Polygon Clipping 17} The 2D Sutherland-Hodgman algorithm generalizes to higher

  • Upload
    lythien

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Courtesy of Andries van Dam©

Clipping Concepts, Algorithms for line clipping

1

Courtesy of Andries van Dam©Clipping 2

Assignment 3 will be out

Quiz2 next week

Courtesy of Andries van Dam©

} Inthecanonicalvolume,2operationsareeasy} Projection} Clipping

Last Time

X

Y

Z

2

2

1

(u)

(v)

(n)(P)

Courtesy of Andries van Dam©Clipping

} Easytoclip} Point?} Line?} Polygon?

4

Well, that’s a lie...

Courtesy of Andries van Dam©

} Line Clipping in 2D

5

Courtesy of Andries van Dam©

} Line Clipping in 2D

6

Courtesy of Andries van Dam©

} Line Clipping in 2D

7

Courtesy of Andries van Dam©

} ParametricformforlinesegmentParametric Line Formulation For Clipping

8

Courtesy of Andries van Dam©

} Parametricformforlinesegment

} Lineisincliprectangleifparametricvariablestlineandsedgebothin[0,1]atintersectionpointbetweenlineandedgeofcliprectangle} Slow,mustintersectlineswithalledges

Parametric Line Formulation For Clipping

9

Courtesy of Andries van Dam©

ClipRectangle} Cohen-Sutherland Line Clipping in 2D

10

Courtesy of Andries van Dam©

ClipRectangle} Cohen-Sutherland Line Clipping in 2D

11

Courtesy of Andries van Dam©

} Verysimilarto2D} Dividevolumeinto27regions(PictureaRubik’scube)

} 6-bitoutcoderecordsresultsof6boundstests} Firstbit:behindbackplane} Secondbit:infrontoffrontplane} Thirdbit:abovetopplane} Fourthbit:belowbottomplane} Fifthbit:totherightofrightplane} Sixthbit:totheleftofleftplane

} Again,lineswithOC0=0andOC1=0canbetriviallyaccepted

} Lineslyingentirelyinavolumeoutsideofaplanecanbetriviallyrejected:OC0ANDOC1≠0(i.e.,theysharean“outside”bit)

Cohen-Sutherland Line Clipping in 3D

12

Backplane000000(infront)

100000(behind)

Frontplane010000(infront)

000000(behind)

Bottomplane000000(above)

000100(below)

Leftplane000001(toleftof)

000000(torightof)

Topplane001000(above)

000000(below)

Rightplane000000(toleftof)

000010(torightof)

Courtesy of Andries van Dam©

} Ifwecanneithertriviallyaccept/reject(T/A,T/R),divideandconquer

} Subdividelineintotwosegments;thenT/AorT/Roneorbothsegments:} useaclipedgetocutline} useoutcodestochoosetheedgesthatarecrossed

} foragivenclipedge,ifaline’stwooutcodesdifferinthecorrespondingbit,thelinehasonevertexoneachsideoftheedge,thuscrosses

} pickanorderforcheckingedges:top–bottom–right–left} computetheintersectionpoint

} theclipedgefixeseitherxory} cansubstituteintothelineequation

} iterateforthenewlyshortenedline,“extra”clipsmayhappen(e.g.,E-IatH)

Cohen-Sutherland Algorithm (1/3)Cliprectangle

DC B

A

EF

GH

I

13

Courtesy of Andries van Dam©

} Cohen-Sutherland Algorithm (2/3)

ComputeOutCode(x0,y0,outcode0);ComputeOutCode(x1,y1,outcode1);

repeatcheckfortrivialrejectortrivialacceptpicktheoutsidepointandbit‘1’

ifTOPthenx=x0+(x1–x0)*(ymax–y0)/(y1–y0);y=ymax;elseifBOTTOMthenx=x0+(x1–x0)*(ymin–y0)/(y1–y0);y=ymin;

14

elseifRIGHTtheny=y0+(y1–y0)*(xmax–x0)/(x1–x0);x=xmax;elseifLEFTtheny=y0+(y1–y0)*(xmin–x0)/(x1–x0);x=xmin;

if(youpickedx0,y0)thenx0=x;y0=y;ComputeOutCode(x0,y0,outcode0)elsex1=x;y1=y;ComputeOutCode(x1,y1,outcode1)

untildone

Courtesy of Andries van Dam©

} Similaralgorithmforusing3Doutcodestoclipagainstcanonicalparallelviewvolume:Cohen-Sutherland Algorithm (3/3)

xmin=ymin=-1;xmax=ymax=1;zmin=-1;zmax=0;

ComputeOutCode(x0,y0,z0,outcode0);ComputeOutCode(x1,y1,z1,outcode1);repeat

checkfortrivialrejectortrivialacceptpickthepointthatisoutsidethecliprectangleifTOPthenx=x0+(x1–x0)*(ymax–y0)/(y1–y0);z=z0+(z1–z0)*(ymax–y0)/(y1–y0);y=ymax;elseifBOTTOMthenx=x0+(x1–x0)*(ymin–y0)/(y1–y0);z=z0+(z1–z0)*(ymin–y0)/(y1–y0);y=ymin;elseifRIGHTtheny=y0+(y1–y0)*(xmax–x0)/(x1–x0);z=z0+(z1–z0)*(xmax–x0)/(x1–x0);x=xmax;

15

elseifLEFTtheny=y0+(y1–y0)*(xmin–x0)/(x1–x0);z=z0+(z1–z0)*(xmin–x0)/(x1–x0);x=xmin;elseifNEARthenx=x0+(x1–x0)*(zmax–z0)/(z1–z0);y=y0+(y1–y0)*(zmax–z0)/(z1–z0);z=zmax;elseifFARthenx=x0+(x1–x0)*(zmin–z0)/(z1–z0);y=y0+(y1–y0)*(zmin–z0)/(z1–z0);z=zmin;

if(x0,y0,z0istheouterpoint)thenx0=x;y0=y;z0=z;ComputeOutCode(x0,y0,z0,outcode0)elsex1=x;y1=y;z1=z;ComputeOutCode(x1,y1,z1,outcode1)

untildone

Courtesy of Andries van Dam©

} Scan Conversion after Clipping

B A

x=xmin

y=ymin

y=ymin–1y=ymin–1/2

Cliprectangle

16

Courtesy of Andries van Dam©

Sutherland-Hodgman Polygon Clipping

17

} The2DSutherland-Hodgmanalgorithmgeneralizestohigherdimensions} Wecanuseittoclippolygonstothe3Dviewvolumeoneplaneatatime} SearchforBALSAClippingonyoutube(0:50,3:45)

Courtesy of Andries van Dam©

} Cyrus-Beck/Liang-Barsky Parametric Line Clipping (1/3)

18

Courtesy of Andries van Dam©

} NowsolveforthevalueoftattheintersectionofP0P1withtheedgeEi:

} First,substituteforP(t):} Next,grouptermsanddistributedotproduct:} LetDbethevectorfromP0toP1=(P1–P0),andsolvefort:} notethatthisgivesavalidvalueoftonlyifthe

denominatoroftheexpressionisnonzero.} Forthistobetrue,itmustbethecasethat:

} Ni≠0(thatis,thenormalshouldnotbe0;thiscouldoccuronlyasamistake)

} D≠0(thatis,P1≠P0)} Ni•D≠0(edgeEiandlineDarenotparallel;iftheyare,

nointersection).} Thealgorithmcheckstheseconditions.

Cyrus-Beck/Liang-Barsky Parametric Line Clipping (2/3)

19

Courtesy of Andries van Dam©

} Cyrus-Beck/Liang-Barsky Parametric Line Clipping (3/3)

20

Courtesy of Andries van Dam©

Cyrus-Beck/Liang-Barsky Line Clipping AlgorithmPre-calculateNiandselectPEiforeachedge;foreachlinesegmenttobeclippedifP1=P0thenlineisdegeneratesoclipasapoint;elsebegintE=0;tL=1;foreachcandidateintersectionwithaclipedgeifNi•D≠0then{Ignoreedgesparalleltoline}begincalculatet;{oflineandclipedgeintersection}usesignofNi•DtocategorizeasPEorPL;ifPEthentE=max(tE,t);ifPLthentL=min(tL,t);endiftE>tLthenreturnnilelsereturnP(tE)andP(tL)astrueclipintersectionsend

21

Courtesy of Andries van Dam©

} D=P1–P0=(x1–x0,y1–y0)

} LeavePEiasanarbitrarypointonclipedge:it’safreevariableanddropsout

Parametric Line Clipping for Upright Clip Rectangle (1/2)

CalculationsforParametricLineClippingAlgorithm

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

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

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

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

P0-PEiPEiNormalNiClipEdgeiDiNiE

PPiNt•−

−•=

)0(

)01(

)min0(

xx

xx

−−

)01(

)max0(

xx

xx

−−

)01(

)min0(

yy

yy

−−

)01(

)max0(

yy

yy

−−

22

Courtesy of Andries van Dam©

} Examinet:} Numeratorisjustthedirecteddistancetoanedge;signcorrespondstoOC

} Denominatorisjustthehorizontalorverticalprojectionoftheline,dxordy;signdeterminesPEorPLforagivenedge

} Ratioisconstantofproportionality:“howfarover”fromP0toP1intersectionisrelativetodxordy

Parametric Line Clipping for Upright Clip Rectangle (2/2)

23

Courtesy of Andries van Dam©Clipping

} Cohen-Sutherlandisbetterwhenmanytrivialaccepts/reject} Otherwise,Liang-Barskyisbetter

24

Pros and Cons