30
CSE 423 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Back Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon) Cohen Sutherland Algorithm (3d)

06 clipping

Embed Size (px)

DESCRIPTION

it gives the idea about how the clipping is done in computer graphics.

Citation preview

Page 1: 06 clipping

CSE 423 Computer Graphics

Clipping• Cohen Sutherland Algorithm (Line)• Cyrus-Back Algorithm (Line)• Sutherland-Hodgeman Algorithm (Polygon)• Cohen Sutherland Algorithm (3d)

Page 2: 06 clipping

maxmin

maxmin

yyy

xxx

For a point For a point ((x,yx,y) ) to be inside the clip rectangleto be inside the clip rectangle::

(xmin , ymin )

(xmax , ymax )

x = xmin x = xmax

y = ymin

y = ymax

cliprectangle

Point Clipping

Page 3: 06 clipping

(xmin , ymin )

(xmax , ymax )

x = xmin x = xmax

y = ymin

y = ymax

(x1, y1)

cliprectangle

maxmin

maxmin

yyy

xxx

For a point For a point ((x,yx,y) ) to be inside the clip rectangleto be inside the clip rectangle::

Point Clipping

Page 4: 06 clipping

cliprectangle

Cases for clipping linesCases for clipping lines

Line Clipping

Page 5: 06 clipping

Cases for clipping linesCases for clipping lines

A

B

A

B

cliprectangle

Line Clipping

Page 6: 06 clipping

Cases for clipping linesCases for clipping lines

D

A

BC

D'

A

BC

D'

cliprectangle

Line Clipping

Page 7: 06 clipping

Cases for clipping linesCases for clipping lines

D

E

F

A

BC

D'

A

BC

D'

cliprectangle

Line Clipping

Page 8: 06 clipping

Cases for clipping linesCases for clipping lines

D

E

F

A

BC

D'

G

H

G'

H'A

BC

D'

G'

H'

cliprectangle

Line Clipping

Page 9: 06 clipping

Cases for clipping linesCases for clipping lines

D

E

F

A

BC

D'

G

H

G'

H'

I

J

I'

J'

A

BC

D'

G'

H'

cliprectangle

Line Clipping

Page 10: 06 clipping

Clipping Lines by Solving Simultaneous EquationsClipping Lines by Solving Simultaneous Equations

cliprectangle

(xa, ya)(xb , yb)

(xc, yc) (xd , yd)

(x0, y0)

(x1, y1)

(x , y )

cliprectangle

(xa, ya)(xb , yb)

(xc, yc) (xd , yd)

(x0, y0)

(x1, y1)

(x , y )

abedgeaabedgea

lineline

yytyyxxtxx

yytyyxxtxx

,

, 010010

Line Clipping

Page 11: 06 clipping

The Cohen-Sutherland Line-Clipping Algorithm performs initial tests on a line to determine whether intersection calculations can be avoided.1. First, end-point pairs are checked for Trivial

Acceptance.2. If the line cannot be trivially accepted, region

checks are done for Trivial Rejection.3. If the line segment can be neither trivially

accepted or rejected, it is divided into two segments at a clip edge, so that one segment can be trivially rejected.These three steps are performed iteratively until what remains can be trivially accepted or rejected.

Cohen-Sutherland Algorithm

Page 12: 06 clipping

0000

1000

0100

1001 1010

0001 0010

0101 0110clip

rectangle

Region outcodesRegion outcodes

min

max

yy

yy

:1bit

: 0bit

min

max

xx

xx

:3bit

: 2bit

Cohen-Sutherland Algorithm

Page 13: 06 clipping

1. A line segment can be trivially accepted if the outcodes of both the endpoints are zero.

2. A line segment can be trivially rejected if the logical AND of the outcodes of the endpoints is not zero.

3. A key property of the outcode is that bits that are set in nonzero outcode correspond to edges crossed.

Cohen-Sutherland Algorithm

Page 14: 06 clipping

cliprectangle

AB

C

D

E

An ExampleAn Example

Cohen-Sutherland Algorithm

Page 15: 06 clipping

An ExampleAn Example

cliprectangle

B

C

D

E

Cohen-Sutherland Algorithm

Page 16: 06 clipping

An ExampleAn Example

cliprectangle

B

C

D

Cohen-Sutherland Algorithm

Page 17: 06 clipping

An ExampleAn Example

cliprectangle

B

C

Cohen-Sutherland Algorithm

Page 18: 06 clipping

(1)(1) This fundamentally different (from Cohen-This fundamentally different (from Cohen-Sutherland algorithm) and generally more Sutherland algorithm) and generally more efficient algorithm was originally published by efficient algorithm was originally published by CyrusCyrus and and BeckBeck..

(2)(2) LiangLiang and and BarskyBarsky later independently later independently developed a more efficient algorithm that is developed a more efficient algorithm that is especially fast in the special cases of upright especially fast in the special cases of upright 2D and 3D clipping regions.They also 2D and 3D clipping regions.They also introduced more efficient trivial rejection introduced more efficient trivial rejection tests for general clip regions.tests for general clip regions.

Parametric Line-Clipping

Page 19: 06 clipping

Inside of clip rectangleOutside of clip region

0iEi PtPN

0iEi PtPN

0iEi PtPN

iEi PtP

iEP

0P

1P

iN

iE Edge tPPPtPPPLine 01010 :

10

0

10

0

010

010

,

0

0

0

PPDDN

PPNt

PPN

PPNt

PtPPPN

PtPPPN

PtPN

i

Ei

i

Ei

Ei

Ei

Ei

i

i

i

i

i

The Cyrus-Back Algorithm

Page 20: 06 clipping

Inside of clip rectangleOutside of clip region

0iEi PtPN

0iEi PtPN

0iEi PtPN

iEi PtP

iEP

0P

1P

iN

iE Edge

DN

PPNt

i

Ei i

0

0 3

0 2

0 1

whenexists

10

DN

PPD

N

t

i

i

The Cyrus-Back Algorithm

Page 21: 06 clipping

PE

PL

P 0

P 1

t = 0

t = 1

PE

PL

P 1

t = 1

P 0

t = 0

PE

PE

PLPL

P 0

t = 0

P 1

t = 1Line 2

Line 1

Line 3

Cliprectangle

90

0

Angle

PEDNi

90

0

Angle

PLDNi

PE = Potentially Entering PL = Potentially Leaving

The Cyrus-Back Algorithm

Page 22: 06 clipping

The Cyrus-Back AlgorithmPrecalculate Ni and PEi for each edgefor (each line segment to be clipped) {

if (P1 == P0)line is degenerated, so clip as a point;

else {tE = 0; tL = 1;for (each candidate intersection with a clip edge) {

if (Ni • D != 0) { /* Ignore edges parallel to line */calculate t;use sign of Ni • D to categorize as PE or PL;if (PE) tE = max(tE , t);if (PL) tL = min(tL , t);

}}if (tE > tL) return NULL;else return P(tE) and P(tL) as true clip intersection;

} }

Page 23: 06 clipping

ExampleExample

Polygon Clipping

Page 24: 06 clipping

ExampleExample

Polygon Clipping

Page 25: 06 clipping

ExampleExample

Polygon Clipping

Page 26: 06 clipping

Initial ConditionInitial Condition

Sutherland-Hodgeman Algo.

Clip Against Right Clipping Clip Against Right Clipping BoundaryBoundary

Clip Against Top Clipping Clip Against Top Clipping BoundaryBoundary

The Clipped PolygonThe Clipped PolygonClip Against Bottom Clipping Clip Against Bottom Clipping BoundaryBoundary

Clip Against Left Clipping Clip Against Left Clipping BoundaryBoundary

Page 27: 06 clipping

Case 1Case 1

Inside Outside

Polygonbeingclipped

Clipboundary

s

p:output

4 Cases of Polygon Clipping

Case 2Case 2

Inside Outside

s

p

i:output

Case 3Case 3

Inside Outside

s

p

(no output)

Case 4Case 4

Inside Outside

s

i:first output

p:second output

Page 28: 06 clipping

Algorithm Input vertex P

First Point YesNo

F=P

Does SP intersectE?

No

Yes

ComputeIntersection Point

I

Outputvertex I

S=P

Is S on leftside of E?

Exit

NO

Yes

Outputvertex S

Close Polygon entry

Does SFintersect E?

ComputeIntersection I

Yes

Outputvertex I

Exit

No

Page 29: 06 clipping

3D Clipping• Both the Cohen-Sutherland and Cyrus-Beck clipping algorithm

readily extend to 3D.• For Cohen-Sutherland algorithm use two extra-bit in outcode for

incorporating z < zmin and z > zmax regions

Page 30: 06 clipping

Thank You