CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

50
CS 445 / 645 Introduction to Computer Graphics Lecture 9 Lecture 9 Clipping Clipping

Transcript of CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Page 1: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

CS 445 / 645Introduction to Computer Graphics

Lecture 9Lecture 9

ClippingClipping

Lecture 9Lecture 9

ClippingClipping

Page 2: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Assignment 1Do a writeup (call it README)Do a writeup (call it README)

• Indicate which version of Visual Studio you usedIndicate which version of Visual Studio you used

• Also indicate what you do for extra creditAlso indicate what you do for extra credit

• Also, if your program doesn’t work, explain what you did to help us provide Also, if your program doesn’t work, explain what you did to help us provide partial creditpartial credit

Copy all code (including source) required to run your program toCopy all code (including source) required to run your program to

• username/Assignment1username/Assignment1

Name executableName executable

• assign1.exeassign1.exe

Do a writeup (call it README)Do a writeup (call it README)

• Indicate which version of Visual Studio you usedIndicate which version of Visual Studio you used

• Also indicate what you do for extra creditAlso indicate what you do for extra credit

• Also, if your program doesn’t work, explain what you did to help us provide Also, if your program doesn’t work, explain what you did to help us provide partial creditpartial credit

Copy all code (including source) required to run your program toCopy all code (including source) required to run your program to

• username/Assignment1username/Assignment1

Name executableName executable

• assign1.exeassign1.exe

Page 3: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Review: Polygon Rasterization

A B

D C

F

EH G

For scanline, determine all intersections of polygon edges with scanlineSort edge intersections in least to greatest orderUse parity count to determine when pixels are drawnHorizontal lines do not contribute to parity countYmin endpoints do contribute to parity countYmax endpoints do not contribute to parity count

Bottom edge drawn because A is min of AH. AB does not contribute

Not drawn because H is max of AHAnd HG does not contribute

Not drawn because D is min of EDAnd increments counter to 2.

DC doesn’t contribute

Page 4: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Next Topic: Clipping

We’ve been assuming that all primitives (lines, triangles, We’ve been assuming that all primitives (lines, triangles, polygons) lie entirely within the polygons) lie entirely within the viewportviewport

• In general, this assumption will not hold:In general, this assumption will not hold:

We’ve been assuming that all primitives (lines, triangles, We’ve been assuming that all primitives (lines, triangles, polygons) lie entirely within the polygons) lie entirely within the viewportviewport

• In general, this assumption will not hold:In general, this assumption will not hold:

Page 5: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Clipping

Analytically calculating the portions of primitives Analytically calculating the portions of primitives within the viewportwithin the viewport

Analytically calculating the portions of primitives Analytically calculating the portions of primitives within the viewportwithin the viewport

Page 6: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Why Clip?

Bad idea to rasterize outside of framebuffer Bad idea to rasterize outside of framebuffer bounds bounds

Also, don’t waste time scan converting pixels Also, don’t waste time scan converting pixels outside windowoutside window

Bad idea to rasterize outside of framebuffer Bad idea to rasterize outside of framebuffer bounds bounds

Also, don’t waste time scan converting pixels Also, don’t waste time scan converting pixels outside windowoutside window

Page 7: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Clipping

The naïve approach to clipping lines:The naïve approach to clipping lines:

for each line segmentfor each line segment

for each edge of viewportfor each edge of viewport

find intersection pointfind intersection point

pick “nearest” pointpick “nearest” point

if anything is left, draw itif anything is left, draw it

What do we mean by “nearest”?What do we mean by “nearest”?

How can we optimize this?How can we optimize this?

The naïve approach to clipping lines:The naïve approach to clipping lines:

for each line segmentfor each line segment

for each edge of viewportfor each edge of viewport

find intersection pointfind intersection point

pick “nearest” pointpick “nearest” point

if anything is left, draw itif anything is left, draw it

What do we mean by “nearest”?What do we mean by “nearest”?

How can we optimize this?How can we optimize this?A

B

CD

Page 8: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Trivial Accepts

Big optimization: trivial accept/rejectsBig optimization: trivial accept/rejects

How can we quickly determine whether a line segment is How can we quickly determine whether a line segment is entirely inside the viewport?entirely inside the viewport?

A: test both endpoints.A: test both endpoints.

Big optimization: trivial accept/rejectsBig optimization: trivial accept/rejects

How can we quickly determine whether a line segment is How can we quickly determine whether a line segment is entirely inside the viewport?entirely inside the viewport?

A: test both endpoints.A: test both endpoints.

Page 9: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Trivial Rejects

How can we know a line is outside viewport?How can we know a line is outside viewport?

A: if both endpoints on wrong side of A: if both endpoints on wrong side of samesame edge, edge, can trivially reject linecan trivially reject line

How can we know a line is outside viewport?How can we know a line is outside viewport?

A: if both endpoints on wrong side of A: if both endpoints on wrong side of samesame edge, edge, can trivially reject linecan trivially reject line

Page 10: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Clipping Lines To Viewport

Combining trivial accepts/rejectsCombining trivial accepts/rejects

• Trivially Trivially acceptaccept lines with both endpoints lines with both endpoints inside all edges of the viewportinside all edges of the viewport

• Trivially Trivially reject reject lines with both endpoints lines with both endpoints outside the same edge of the outside the same edge of the viewportviewport

• Otherwise, reduce to trivial casesOtherwise, reduce to trivial cases by splitting into two segmentsby splitting into two segments

Combining trivial accepts/rejectsCombining trivial accepts/rejects

• Trivially Trivially acceptaccept lines with both endpoints lines with both endpoints inside all edges of the viewportinside all edges of the viewport

• Trivially Trivially reject reject lines with both endpoints lines with both endpoints outside the same edge of the outside the same edge of the viewportviewport

• Otherwise, reduce to trivial casesOtherwise, reduce to trivial cases by splitting into two segmentsby splitting into two segments

Page 11: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Cohen-Sutherland Line Clipping

Divide Divide viewplaneviewplane into regions defined by into regions defined by viewportviewport edgesedges

Assign each region a 4-bit Assign each region a 4-bit outcodeoutcode: :

Divide Divide viewplaneviewplane into regions defined by into regions defined by viewportviewport edgesedges

Assign each region a 4-bit Assign each region a 4-bit outcodeoutcode: :

0000 00100001

1001

0101 0100

1000 1010

0110

Page 12: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Cohen-Sutherland Line Clipping

Assign an outcode to each vertex of line to testAssign an outcode to each vertex of line to test

• Bit 1 = sign bit of (yBit 1 = sign bit of (ymaxmax – y) – y)

• If both outcodes = 0, trivial acceptIf both outcodes = 0, trivial accept

– bitwise bitwise OROR

• bitwise bitwise ANDAND vertex outcodes together vertex outcodes together

– if result if result 0, trivial reject 0, trivial reject

Assign an outcode to each vertex of line to testAssign an outcode to each vertex of line to test

• Bit 1 = sign bit of (yBit 1 = sign bit of (ymaxmax – y) – y)

• If both outcodes = 0, trivial acceptIf both outcodes = 0, trivial accept

– bitwise bitwise OROR

• bitwise bitwise ANDAND vertex outcodes together vertex outcodes together

– if result if result 0, trivial reject 0, trivial reject

Page 13: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Cohen-Sutherland Line Clipping

If line cannot be trivially accepted or rejected, subdivide If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discardedso that one or both segments can be discarded

Pick an edge that the line crosses (Pick an edge that the line crosses (how?how?))

Intersect line with edge (Intersect line with edge (how?how?))

Discard portion on wrong side of edge and assign Discard portion on wrong side of edge and assign outcode to new vertexoutcode to new vertex

Apply trivial accept/reject tests; repeat if necessary Apply trivial accept/reject tests; repeat if necessary

If line cannot be trivially accepted or rejected, subdivide If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discardedso that one or both segments can be discarded

Pick an edge that the line crosses (Pick an edge that the line crosses (how?how?))

Intersect line with edge (Intersect line with edge (how?how?))

Discard portion on wrong side of edge and assign Discard portion on wrong side of edge and assign outcode to new vertexoutcode to new vertex

Apply trivial accept/reject tests; repeat if necessary Apply trivial accept/reject tests; repeat if necessary

Page 14: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Cohen-Sutherland Line Clipping

If line cannot be trivially accepted or rejected, subdivide If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discardedso that one or both segments can be discarded

Pick an edge that the line crossesPick an edge that the line crosses

• Check against edges in same order each timeCheck against edges in same order each time

– For example: top, bottom, right, leftFor example: top, bottom, right, left

If line cannot be trivially accepted or rejected, subdivide If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discardedso that one or both segments can be discarded

Pick an edge that the line crossesPick an edge that the line crosses

• Check against edges in same order each timeCheck against edges in same order each time

– For example: top, bottom, right, leftFor example: top, bottom, right, left

A

B

D E

C

Page 15: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Cohen-Sutherland Line Clipping

Intersect line with edge (Intersect line with edge (how?how?))Intersect line with edge (Intersect line with edge (how?how?))

A

B

D E

C

Page 16: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Discard portion on wrong side of edge and assign outcode to new vertexDiscard portion on wrong side of edge and assign outcode to new vertex

Apply trivial accept/reject tests and repeat if necessary Apply trivial accept/reject tests and repeat if necessary

Discard portion on wrong side of edge and assign outcode to new vertexDiscard portion on wrong side of edge and assign outcode to new vertex

Apply trivial accept/reject tests and repeat if necessary Apply trivial accept/reject tests and repeat if necessary

Cohen-Sutherland Line Clipping

A

B

DC

Page 17: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Viewport Intersection Code

• (x(x11, y, y11), (x), (x22, y, y22) intersect with vertical edge at x) intersect with vertical edge at xrightright

– yyintersectintersect = y = y11 + m(x + m(xrightright – x1), m=(y – x1), m=(y22-y-y11)/(x)/(x22-x-x11))

• (x(x11, y, y11), (x), (x22, y, y22) intersect with horizontal edge at y) intersect with horizontal edge at ybottombottom

– xxintersectintersect = x = x11 + (y + (ybottombottom – y1)/m, m=(y – y1)/m, m=(y22-y-y11)/(x)/(x22-x-x11))

• (x(x11, y, y11), (x), (x22, y, y22) intersect with vertical edge at x) intersect with vertical edge at xrightright

– yyintersectintersect = y = y11 + m(x + m(xrightright – x1), m=(y – x1), m=(y22-y-y11)/(x)/(x22-x-x11))

• (x(x11, y, y11), (x), (x22, y, y22) intersect with horizontal edge at y) intersect with horizontal edge at ybottombottom

– xxintersectintersect = x = x11 + (y + (ybottombottom – y1)/m, m=(y – y1)/m, m=(y22-y-y11)/(x)/(x22-x-x11))

Page 18: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Cohen-Sutherland Review

• Use opcodes to quickly eliminate/include linesUse opcodes to quickly eliminate/include lines

– Best algorithm when trivial accepts/rejects are commonBest algorithm when trivial accepts/rejects are common

• Must compute viewport clipping of remaining linesMust compute viewport clipping of remaining lines

– Non-trivial clipping costNon-trivial clipping cost

– Redundant clipping of some linesRedundant clipping of some lines

More efficient algorithms existMore efficient algorithms exist

• Use opcodes to quickly eliminate/include linesUse opcodes to quickly eliminate/include lines

– Best algorithm when trivial accepts/rejects are commonBest algorithm when trivial accepts/rejects are common

• Must compute viewport clipping of remaining linesMust compute viewport clipping of remaining lines

– Non-trivial clipping costNon-trivial clipping cost

– Redundant clipping of some linesRedundant clipping of some lines

More efficient algorithms existMore efficient algorithms exist

Page 19: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Solving Simultaneous Equations

Equation of a lineEquation of a line

• Slope-intercept (explicit equation): y = mx + bSlope-intercept (explicit equation): y = mx + b

• Implicit Equation: Ax + By + C = 0Implicit Equation: Ax + By + C = 0

• Parametric Equation: Line defined by two points, PParametric Equation: Line defined by two points, P00 and P and P11

– PP(t) = (t) = PP00 + ( + (PP11 - - PP00) t, where ) t, where PP is a vector [x, y] is a vector [x, y]TT

– x(t) = xx(t) = x00 + (x + (x1 1 - x- x00) t) t

– y(t) = xy(t) = x00 + (y + (y1 1 - y- y00) t) t

Equation of a lineEquation of a line

• Slope-intercept (explicit equation): y = mx + bSlope-intercept (explicit equation): y = mx + b

• Implicit Equation: Ax + By + C = 0Implicit Equation: Ax + By + C = 0

• Parametric Equation: Line defined by two points, PParametric Equation: Line defined by two points, P00 and P and P11

– PP(t) = (t) = PP00 + ( + (PP11 - - PP00) t, where ) t, where PP is a vector [x, y] is a vector [x, y]TT

– x(t) = xx(t) = x00 + (x + (x1 1 - x- x00) t) t

– y(t) = xy(t) = x00 + (y + (y1 1 - y- y00) t) t

Page 20: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Parametric Line Equation

Describes a finite lineDescribes a finite line

Works with vertical lines (like the viewport edge)Works with vertical lines (like the viewport edge)

0 <=t <= 10 <=t <= 1

• Defines line between PDefines line between P00 and P and P11

t < 0t < 0

• Defines line before PDefines line before P00

t > 1t > 1

• Defines line after PDefines line after P11

Describes a finite lineDescribes a finite line

Works with vertical lines (like the viewport edge)Works with vertical lines (like the viewport edge)

0 <=t <= 10 <=t <= 1

• Defines line between PDefines line between P00 and P and P11

t < 0t < 0

• Defines line before PDefines line before P00

t > 1t > 1

• Defines line after PDefines line after P11

Page 21: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Parametric Lines and Clipping

Define each line in parametric form: Define each line in parametric form:

• PP00(t)…P(t)…Pn-1n-1(t)(t)

Define each edge of viewport in parametric form:Define each edge of viewport in parametric form:

• PPLL(t), P(t), PRR(t), P(t), PTT(t), P(t), PBB(t)(t)

Could perform Cohen-Sutherland intersection Could perform Cohen-Sutherland intersection tests using appropriate viewport edge and linetests using appropriate viewport edge and line

Define each line in parametric form: Define each line in parametric form:

• PP00(t)…P(t)…Pn-1n-1(t)(t)

Define each edge of viewport in parametric form:Define each edge of viewport in parametric form:

• PPLL(t), P(t), PRR(t), P(t), PTT(t), P(t), PBB(t)(t)

Could perform Cohen-Sutherland intersection Could perform Cohen-Sutherland intersection tests using appropriate viewport edge and linetests using appropriate viewport edge and line

Page 22: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Line / Edge Clipping EquationsFaster line clippers use parametric equationsFaster line clippers use parametric equations

Line 0:Line 0:

• xx00 = x = x0000 + (x + (x00

11 - x - x0000) t) t00

• yy00 = y = y0000 + (y + (y00

11 - y - y0000) t) t00

Viewport Edge L:Viewport Edge L:

• xxLL = x = xLL00 + (x + (xLL

11 - x - xLL00) t) tLL

• yyLL = y = yLL00 + (y + (yLL

11 - y - yLL00) t) tLL

xx0000 + (x + (x00

11 - x - x0000) t) t0 = 0 = xxLL

00 + (x + (xLL11 - x - xLL

00) t) tLL

yy0000 + (y + (y00

11 - y - y0000) t) t0 = 0 = yyLL

00 + (y + (yLL11 - y - yLL

00) t) tLL

• Solve for tSolve for t00 and/or t and/or tLL

Faster line clippers use parametric equationsFaster line clippers use parametric equations

Line 0:Line 0:

• xx00 = x = x0000 + (x + (x00

11 - x - x0000) t) t00

• yy00 = y = y0000 + (y + (y00

11 - y - y0000) t) t00

Viewport Edge L:Viewport Edge L:

• xxLL = x = xLL00 + (x + (xLL

11 - x - xLL00) t) tLL

• yyLL = y = yLL00 + (y + (yLL

11 - y - yLL00) t) tLL

xx0000 + (x + (x00

11 - x - x0000) t) t0 = 0 = xxLL

00 + (x + (xLL11 - x - xLL

00) t) tLL

yy0000 + (y + (y00

11 - y - y0000) t) t0 = 0 = yyLL

00 + (y + (yLL11 - y - yLL

00) t) tLL

• Solve for tSolve for t00 and/or t and/or tLL

Page 23: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Cyrus-Beck Algorithm

We wish to optimize line/line intersectionWe wish to optimize line/line intersection

• Start with parametric equation of line:Start with parametric equation of line:

– P(t) = PP(t) = P00 + (P + (P1 1 - P- P00) t) t

• And a point and normal for each edgeAnd a point and normal for each edge

– PPLL, N, NLL

We wish to optimize line/line intersectionWe wish to optimize line/line intersection

• Start with parametric equation of line:Start with parametric equation of line:

– P(t) = PP(t) = P00 + (P + (P1 1 - P- P00) t) t

• And a point and normal for each edgeAnd a point and normal for each edge

– PPLL, N, NLL

Page 24: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Cyrus-Beck AlgorithmFind t such thatFind t such that

NNLL [P(t) - P [P(t) - PLL] = 0] = 0

Substitute line equation for P(t)Substitute line equation for P(t)

Solve for tSolve for t

• t = Nt = NLL [P [PLL – P – P00] / -N] / -NLL [P [P11 - P - P00]]

Find t such thatFind t such that

NNLL [P(t) - P [P(t) - PLL] = 0] = 0

Substitute line equation for P(t)Substitute line equation for P(t)

Solve for tSolve for t

• t = Nt = NLL [P [PLL – P – P00] / -N] / -NLL [P [P11 - P - P00]]

PL

NL

P(t)Inside

P0

P1

Page 25: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Cyrus-Beck AlgorithmCompute t for line intersection with all four edgesCompute t for line intersection with all four edges

Discard all (t < 0) and (t > 1)Discard all (t < 0) and (t > 1)

Classify each remaining intersection asClassify each remaining intersection as

• Potentially Entering (PE)Potentially Entering (PE)

• Potentially Leaving (PL)Potentially Leaving (PL)

NNLL [P [P11 - P - P00] > 0 implies PL] > 0 implies PL

NNLL [P [P11 - P - P00] < 0 implies PE] < 0 implies PE

• Note that we computed this term when computing tNote that we computed this term when computing t

Compute t for line intersection with all four edgesCompute t for line intersection with all four edges

Discard all (t < 0) and (t > 1)Discard all (t < 0) and (t > 1)

Classify each remaining intersection asClassify each remaining intersection as

• Potentially Entering (PE)Potentially Entering (PE)

• Potentially Leaving (PL)Potentially Leaving (PL)

NNLL [P [P11 - P - P00] > 0 implies PL] > 0 implies PL

NNLL [P [P11 - P - P00] < 0 implies PE] < 0 implies PE

• Note that we computed this term when computing tNote that we computed this term when computing t

Page 26: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Compute PE with largest tCompute PE with largest t

Compute PL with smallest tCompute PL with smallest t

Clip to these two pointsClip to these two points

Cyrus-Beck Algorithm

PE

PLP1

PL

PE

P0

Page 27: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Cyrus-Beck Algorithm

Because of horizontal and vertical clip lines:Because of horizontal and vertical clip lines:• Many computations reduceMany computations reduce

Normals: (-1, 0), (1, 0), (0, -1), (0, 1)Normals: (-1, 0), (1, 0), (0, -1), (0, 1)

Pick constant points on edgesPick constant points on edges

solution for t:solution for t:• -(x-(x00 - x - xleftleft) / (x) / (x11 - x - x00))

• (x(x00 - x - xrightright) / -(x) / -(x11 - x - x00))

• -(y-(y00 - y - ybottombottom) / (y) / (y11 - y - y00))

• (y(y00 - y - ytoptop) / -(y) / -(y11 - y - y00))

Because of horizontal and vertical clip lines:Because of horizontal and vertical clip lines:• Many computations reduceMany computations reduce

Normals: (-1, 0), (1, 0), (0, -1), (0, 1)Normals: (-1, 0), (1, 0), (0, -1), (0, 1)

Pick constant points on edgesPick constant points on edges

solution for t:solution for t:• -(x-(x00 - x - xleftleft) / (x) / (x11 - x - x00))

• (x(x00 - x - xrightright) / -(x) / -(x11 - x - x00))

• -(y-(y00 - y - ybottombottom) / (y) / (y11 - y - y00))

• (y(y00 - y - ytoptop) / -(y) / -(y11 - y - y00))

Page 28: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

ComparisonCohen-SutherlandCohen-Sutherland

• Repeated clipping is expensiveRepeated clipping is expensive

• Best used when trivial acceptance and rejection is possible for most linesBest used when trivial acceptance and rejection is possible for most lines

Cyrus-BeckCyrus-Beck

• Computation of t-intersections is cheapComputation of t-intersections is cheap

• Computation of (x,y) clip points is only done onceComputation of (x,y) clip points is only done once

• Algorithm doesn’t consider trivial accepts/rejectsAlgorithm doesn’t consider trivial accepts/rejects

• Best when many lines must be clippedBest when many lines must be clipped

Liang-Barsky: Optimized Cyrus-BeckLiang-Barsky: Optimized Cyrus-Beck

Nicholl et al.: Fastest, but doesn’t do 3DNicholl et al.: Fastest, but doesn’t do 3D

Cohen-SutherlandCohen-Sutherland

• Repeated clipping is expensiveRepeated clipping is expensive

• Best used when trivial acceptance and rejection is possible for most linesBest used when trivial acceptance and rejection is possible for most lines

Cyrus-BeckCyrus-Beck

• Computation of t-intersections is cheapComputation of t-intersections is cheap

• Computation of (x,y) clip points is only done onceComputation of (x,y) clip points is only done once

• Algorithm doesn’t consider trivial accepts/rejectsAlgorithm doesn’t consider trivial accepts/rejects

• Best when many lines must be clippedBest when many lines must be clipped

Liang-Barsky: Optimized Cyrus-BeckLiang-Barsky: Optimized Cyrus-Beck

Nicholl et al.: Fastest, but doesn’t do 3DNicholl et al.: Fastest, but doesn’t do 3D

Page 29: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Clipping Polygons

Clipping polygons is more complex than clipping Clipping polygons is more complex than clipping the individual linesthe individual lines

• Input: polygonInput: polygon

• Output: original polygon, new polygon, or nothingOutput: original polygon, new polygon, or nothing

When can we trivially accept/reject a polygon as When can we trivially accept/reject a polygon as opposed to the line segments that make up the opposed to the line segments that make up the polygon?polygon?

Clipping polygons is more complex than clipping Clipping polygons is more complex than clipping the individual linesthe individual lines

• Input: polygonInput: polygon

• Output: original polygon, new polygon, or nothingOutput: original polygon, new polygon, or nothing

When can we trivially accept/reject a polygon as When can we trivially accept/reject a polygon as opposed to the line segments that make up the opposed to the line segments that make up the polygon?polygon?

Page 30: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

What happens to a triangle during clipping?What happens to a triangle during clipping?

Possible outcomesPossible outcomes::

What happens to a triangle during clipping?What happens to a triangle during clipping?

Possible outcomesPossible outcomes::

triangle triangle

Why Is Clipping Hard?

triangle quad triangle 5-gon

How many sides can a clipped triangle have?How many sides can a clipped triangle have?

Page 31: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

How many sides?

Seven…Seven…Seven…Seven…

Page 32: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

A really tough case: A really tough case: A really tough case: A really tough case:

Why Is Clipping Hard?

Page 33: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

A really tough case: A really tough case: A really tough case: A really tough case:

Why Is Clipping Hard?

concave polygon multiple polygons

Page 34: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman Clipping

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the viewport edge’s equationClip the polygon against the viewport edge’s equation

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the viewport edge’s equationClip the polygon against the viewport edge’s equation

Page 35: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman ClippingBasic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Page 36: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman ClippingBasic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Page 37: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman ClippingBasic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Page 38: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman ClippingBasic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Page 39: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman ClippingBasic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Page 40: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman ClippingBasic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Page 41: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman ClippingBasic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Page 42: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman ClippingBasic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Page 43: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman Clipping:The AlgorithmBasic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Page 44: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman Clipping

Input/output for algorithm:Input/output for algorithm:

• Input: list of polygon vertices in order Input: list of polygon vertices in order

• Output: list of clipped poygon vertices consisting of Output: list of clipped poygon vertices consisting of old vertices (maybe) and new vertices (maybe)old vertices (maybe) and new vertices (maybe)

Note: this is exactly what we expect from the Note: this is exactly what we expect from the clipping operation against each edgeclipping operation against each edge

Input/output for algorithm:Input/output for algorithm:

• Input: list of polygon vertices in order Input: list of polygon vertices in order

• Output: list of clipped poygon vertices consisting of Output: list of clipped poygon vertices consisting of old vertices (maybe) and new vertices (maybe)old vertices (maybe) and new vertices (maybe)

Note: this is exactly what we expect from the Note: this is exactly what we expect from the clipping operation against each edgeclipping operation against each edge

Page 45: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman Clipping

Sutherland-Hodgman basic routine:Sutherland-Hodgman basic routine:

• Go around polygon one vertex at a timeGo around polygon one vertex at a time

• Current vertex has position Current vertex has position pp

• Previous vertex had position Previous vertex had position ss, and it has been added to , and it has been added to the output if appropriatethe output if appropriate

Sutherland-Hodgman basic routine:Sutherland-Hodgman basic routine:

• Go around polygon one vertex at a timeGo around polygon one vertex at a time

• Current vertex has position Current vertex has position pp

• Previous vertex had position Previous vertex had position ss, and it has been added to , and it has been added to the output if appropriatethe output if appropriate

Page 46: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman Clipping

Edge from Edge from ss to to pp takes one of four cases:takes one of four cases:

(Orange line can be a line or a plane)(Orange line can be a line or a plane)

Edge from Edge from ss to to pp takes one of four cases:takes one of four cases:

(Orange line can be a line or a plane)(Orange line can be a line or a plane)

inside outside

s

p

p output

inside outside

s

p

no output

inside outside

sp

i output

inside outside

sp

i outputp output

Page 47: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Sutherland-Hodgeman ClippingFour cases:Four cases:

• ss inside plane and inside plane and pp inside plane inside plane

– Add Add pp to output to output

– Note: Note: ss has already been added has already been added

• ss inside plane and inside plane and pp outside plane outside plane

– Find intersection point Find intersection point ii

– Add Add ii to output to output

• ss outside plane and outside plane and pp outside planeoutside plane

– Add nothingAdd nothing

• ss outside plane and outside plane and pp inside plane inside plane

– Find intersection point Find intersection point ii

– Add Add ii to output, followed by to output, followed by pp

Four cases:Four cases:

• ss inside plane and inside plane and pp inside plane inside plane

– Add Add pp to output to output

– Note: Note: ss has already been added has already been added

• ss inside plane and inside plane and pp outside plane outside plane

– Find intersection point Find intersection point ii

– Add Add ii to output to output

• ss outside plane and outside plane and pp outside planeoutside plane

– Add nothingAdd nothing

• ss outside plane and outside plane and pp inside plane inside plane

– Find intersection point Find intersection point ii

– Add Add ii to output, followed by to output, followed by pp

Page 48: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Point-to-Plane testA very general test to determine if a point A very general test to determine if a point pp is “inside” is “inside”

a plane a plane PP, defined by , defined by qq and and nn::

((pp - - qq) • ) • nn < 0: < 0: pp inside inside PP

((pp - - qq) • ) • nn = 0: = 0: pp on on PP

((pp - - qq) • ) • nn > 0: > 0: pp outside outside PP

Remember: p Remember: p • n = |p| |n| cos (• n = |p| |n| cos ())

= angle between p and n= angle between p and n

A very general test to determine if a point A very general test to determine if a point pp is “inside” is “inside” a plane a plane PP, defined by , defined by qq and and nn::

((pp - - qq) • ) • nn < 0: < 0: pp inside inside PP

((pp - - qq) • ) • nn = 0: = 0: pp on on PP

((pp - - qq) • ) • nn > 0: > 0: pp outside outside PP

Remember: p Remember: p • n = |p| |n| cos (• n = |p| |n| cos ())

= angle between p and n= angle between p and n

P

np

q

P

np

q

P

np

q

Page 49: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Finding Line-Plane Intersections

Edge intersects plane Edge intersects plane PP where where EE(t) (t) is on is on PP

• q q is a point on is a point on PP

• nn is normal to is normal to PP

((LL(t)(t) - q - q) • ) • nn = 0 = 0

tt = [( = [(q - Lq - L00) • ) • nn] / [(] / [(LL11 - L - L00) • ) • nn]]

• The intersection point The intersection point i = Li = L(t)(t) for this value of for this value of tt

Edge intersects plane Edge intersects plane PP where where EE(t) (t) is on is on PP

• q q is a point on is a point on PP

• nn is normal to is normal to PP

((LL(t)(t) - q - q) • ) • nn = 0 = 0

tt = [( = [(q - Lq - L00) • ) • nn] / [(] / [(LL11 - L - L00) • ) • nn]]

• The intersection point The intersection point i = Li = L(t)(t) for this value of for this value of tt

Page 50: CS 445 / 645 Introduction to Computer Graphics Lecture 9 Clipping Clipping.

Line-Plane Intersections

Again, lots of opportunity for optimization Again, lots of opportunity for optimization Again, lots of opportunity for optimization Again, lots of opportunity for optimization