22
CS 335 Graphics and Multimedia Clipping

CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Embed Size (px)

Citation preview

Page 1: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

CS 335 Graphics and Multimedia

Clipping

Page 2: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Clipping Lines and Polygons

What is clipping?

Goal: only render what will be visible in the drawing window; eliminate from the rendering pipeline all those primitives which will not appear in the final viewport

World coordinate system Window in WCS Viewport on CRT

Page 3: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Clipping Approaches

Individual Pixel Tests:•Render at the pixel level•Check pixel for containment in the target region

Maintain Large Drawing Area:•Render the entire set of primitives in the WCS•Display only the portion of the set which overlaps the viewport

Analytical Methods:•Compute the intersections of primitives with the clip region•Eliminate outlying portions of the primitives

Page 4: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Analytical Clipping

bmxy +=

maxxx =

minyy =

1P

2P

{ } { }bmxyyyP +=== Imin1Brute force method: compute all intersections of line with clip rectangle

{ } { }bmxyxxP +=== Imax2

Page 5: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Cohen-Sutherland Clipper

•Use regions to accept/reject lines•Assume the clipping region is an axis-oriented rectangle

Regions are labeled according to the half-planes defined by the clipping region:

maxxx =

minyy =

maxyy =

minxx =

Page 6: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Regions

0000

1000 10101001

min

max

min

max

:left:right:bottom:top

xxxxyyyy

<><>

0001 0010

0101 0100 0110

Assign a bit to each condition to form a code for the nine regions

)()()()(

ofbit sign :4bit ofbit sign :3bit ofbit sign :2bit ofbit sign :1bit

min

max

min

max

xxxx

yyyy

−−

−−

Page 7: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

The Cohen-Sutherland AlgorithmLet c1 and c2 be the region codes for the endpoints of the line to be clipped.

Trivially Accept:Both codes are 0000 segment is inside the clip region

Trivially Reject:(code 1) && (code 2) are not equal to zero

Line is outside clip rectangle(both endpoints must be on the same side of one of the four clip lines in order to yield a bitwise & that is non-zero)

Page 8: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Iterative Part of the Algorithm

Repeat until the segment cannot be trivially accepted or rejected:

•Subdivide the segment:

•Pick the endpoint which is outside the clip rectangle (one must be outside)•Find the first non-zero bit: this corresponds to the clip edge which intersects the line•Compute the intersection of the line with the edge•Throw away the outside vertex up to the clip rectangle

{ } { }m

bxxbmxxxybmxy −=⇒+=⇒=+= min

minminI

Page 9: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

An Example

0000 0010

0100 0110

10101000

A

B

C D

top bottom right left

E1001

0001

0101

E: 1010A: 0100&: 0000

Subdivide using either A or E

Page 10: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Example, Con’t

0000 0010

0100 0110

10101000 E1001

A

B

C D0001

0101

D: 0010A: 0100&: 0000

Subdivide: Third bit of D indicates that the line intersects the right clip line

top bottom right left

Page 11: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Example (Con’t)

0000 0010

0100

1010

0110

1000 E1001

A

B

C D

top bottom right left

0001

0101

C: 0000A: 0100&: 0000

Subdivide: Second bit of A indicates that the line intersects the bottom clip line

Page 12: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Example: Conclusion

0000 0010

0100

1010

0110

1000 E1001

A

B

C D

top bottom right left

0001

0101

Trivially accept the clipped segment BC

C: 0000B: 0000&: 0000

Page 13: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Cohen-Sutherland Summary

Disadvantages:•Fixed-order decision can do needless work•Can improve using more regions (Nicholl-Lee-Nicholl Alg)•Can generate more efficient rejection tests•Clipping window must be rectangular

Advantages:•Simple to implement•Oriented for most simple window/viewport systems•Extends to 3-D cubic volumes

Page 14: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

The Liang-Barsky Line clipper (parametric line clipping)Parametric Line

ytyyxtxx∆+=∆+=

1

1

]1,0[,

where

1212

∈−=∆−=∆

tyyyxxx

Page 15: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Clipping Condition

maxxx =

minyy =

maxyy =

minxx =

max1min

max1min

yytyyxxtxx

≤∆+≤≤∆+≤

Page 16: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Intersection Conditions

max1min

max1min

yytyyxxtxx

≤∆+≤≤∆+≤

kk qtp ≤where

1max44

min133

1max22

min111

,,,,

yyqypyyqyp

xxqxpxxqxp

−=∆=−=∆−=−=∆=

−=∆−=

Page 17: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Conditions

If (pk =0) – parallel line to one of the clipping edge

(qk <0) – outside, reject(qk ≥0) – inside

If (pk <0) – from outside to inside Intersection at: t = qk/pk (OIP)

If (pk >0) – from inside to outsideIntersection at: t = qk/pk (IOP)

Key Insight: we need to find two intersection points,

one OIP, one IOP.

Page 18: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Intersection Point Computation

For (pk <0) (Outside to inside point)rk = qk/pk

OIP t1 = max(rk, 0)For (pk > 0) (Inside to outside point)

rk = qk/pk

IOP t2 = min(rk, 1)If (t1 > t2), complete outside, REJECTElse clipped line is between (t1 ,t2 )

Page 19: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Liang-Barsky Line Clipping Example

20max =x

4min =y

9max =y

10min =x

(8, 8)

P1

P2

(14, 2)

Page 20: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Summary: Liang-Barsky

(x,y) coordinates are only computed for the two final intersection pointsAt most 4 parameter values are computedThis is a non-iterative algorithmCan be extended to 3-D

Page 21: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Take Home Exercise

Work through the Cohen-Sutherland Clipping Algorithm using the following example

(7,10)

Xmin=10 Xmax=20

Ymax=9

Ymin=4

(21,3)

Start Code End Code

(7,10) 1001 (21,3) 0110

??

Page 22: CS 335 Graphics and Multimedia - Vis Centerryang/Teaching/CS335-spr05/Lectures/g_06_clipping.pdfCS 335 Graphics and Multimedia Clipping. Clipping Lines and Polygons What is clipping?

Take Home Exercise

Clip the following line using the Liang-Barskyalgorithm

20max =x

4min =y

9max =y

10min =x

(8, 8)

(16, 14)

P1

P2