27
CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

Embed Size (px)

Citation preview

Page 1: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

CSE 403 Computer Graphics

Clipping

• Cohen Sutherland Algorithm (Line)• Cyrus-Beck Algorithm (Line)• Sutherland-Hodgeman Algorithm (Polygon)

Page 2: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

2

(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 3: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

3

cliprectangle

Cases for clipping linesCases for clipping lines

Line Clipping

Page 4: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

4

Cases for clipping linesCases for clipping lines

A

B

A

B

cliprectangle

Line Clipping

Page 5: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

5

Cases for clipping linesCases for clipping lines

D

A

BC

D'

A

BC

D'

cliprectangle

Line Clipping

Page 6: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

6

Cases for clipping linesCases for clipping lines

D

E

F

A

BC

D'

A

BC

D'

cliprectangle

Line Clipping

Page 7: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

7

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 8: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

8

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 9: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

9

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 10: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

10

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 11: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

11

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 12: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

12

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 13: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

13

cliprectangle

AB

C

D

E

An ExampleAn Example

Cohen-Sutherland Algorithm

Page 14: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

14An ExampleAn Example

cliprectangle

B

C

D

E

Cohen-Sutherland Algorithm

Page 15: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

15An ExampleAn Example

cliprectangle

B

C

D

Cohen-Sutherland Algorithm

Page 16: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

16An ExampleAn Example

cliprectangle

B

C

Cohen-Sutherland Algorithm

Page 17: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

17

(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 18: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

18

Inside of clip rectangleOutside of clip region

0iEi PtPN

0iEi PtPN

0iEi PtPN

iEi PtP

iEP

0P

1P

iN

iE Edge

tPPPtPPPLine 01010 :

01

0

01

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-Beck Algorithm

Page 19: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

19

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-Beck Algorithm

Page 20: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

20

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-Beck Algorithm

Page 21: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

21

The Cyrus-Beck 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 22: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

22

ExampleExample

Polygon Clipping

Page 23: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

23

ExampleExample

Polygon Clipping

Page 24: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

24

ExampleExample

Polygon Clipping

Page 25: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

25

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 26: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

26

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 27: CSE 403 Computer Graphics Clipping Cohen Sutherland Algorithm (Line) Cyrus-Beck Algorithm (Line) Sutherland-Hodgeman Algorithm (Polygon)

27

Reference

FV: 3.12, 3.13, 3.14