cs 551 / 645: introductory computer graphics clipping lines and polygons

43
CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Upload: tamsyn-campbell

Post on 17-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

CS 551 / 645: Introductory Computer Graphics

Clipping Lines and Polygons

Page 2: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Know your role...

Brian talks about assignment 1 Clipping lines to viewports Clipping lines to arbitrary polygons Clipping polygons

Page 3: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Clipping Lines To Viewport

Discard segments of lines outside viewport– Trivially accept lines with both endpoints inside all edges of

the viewport– Trivially reject lines with both endpoints outside the same

edge of the viewport– Otherwise, reduce to trivial cases by splitting into two

segments

Page 4: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Solving Simultaneous Equations

Equation of a line– Slope-intercept: y = mx + b– Implicit Equation: Ax + By + C = 0

– Parametric: Line defined by two points, P0 and P1

P(t) = P0 + (P1 - P0) t

x(t) = x0 + (x1 - x0) t

y(t) = x0 + (y1 - y0) t

Page 5: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Parametric Line Equation

Describes a finite line Works with vertical lines (like the viewport) 0 <=t <= 1

– Defines line between P0 and P1

t < 0– Defines line before P0

t > 1– Defines line after P1

Page 6: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Parametric Lines and Clipping

Define each line in parametric form: – P0(t)…Pn-1(t)

Define each edge of viewport in parametric form:– PL(t), PR(t), PT(t), PB(t)

For each line, compute intersection with all viewport edges

Page 7: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Computing Intersections

Line 0:– x0 = x0

0 + (x01 - x0

0) t0

– y0 = y00 + (y0

1 - y00) t0

Edge L:– xL = xL

0 + (xL1 - xL

0) tL

– yL = yL0 + (yL

1 - yL0) tL

x00 + (x0

1 - x00) t0 = xL

0 + (xL1 - xL

0) tL

y00 + (y0

1 - y00) t0 = yL

0 + (yL1 - yL

0) tL

– Solve for t0 and tL

Page 8: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

What’s wrong with this?

Page 9: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Cohen-Sutherland Line Clipping

Divide viewplane into regions defined by viewport edges

Assign each region a 4-bit outcode:

0000 00100001

1001

0101 0100

1000 1010

0110

Page 10: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Cohen-Sutherland Line Clipping

Set bits with simple testsx > xmax y < ymin etc.

Assign an outcode to each vertex of line– If both outcodes = 0, trivial accept– bitwise AND vertex outcodes together– If result 0, trivial reject

Page 11: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Cohen-Sutherland Line Clipping

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

Pick an edge that the line crosses

A

B

D E

C

Page 12: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Cohen-Sutherland Line Clipping

Intersect line with edge (how?)

A

B

D E

C

Page 13: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

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

Apply trivial accept/reject tests and repeat if necessary

Cohen-Sutherland Line Clipping

A

B

DC

Page 14: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Cohen-Sutherland Line Clipping

Outcode tests and line-edge intersects are quite fast

But some lines require multiple iterations (see F&vD figure 3.40)

Fundamentally more efficient algorithms: – Cyrus-Beck uses parametric lines– Liang-Barsky optimizes this for upright volumes– F&vD for more details

Page 15: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Cyrus-Beck Algorithm

Use parametric equations of lines Optimize We saw that this could be expensive…

Start with parametric equation of line:– P(t) = P0 + (P1 - P0) t

And a point and normal for each edge– PL, NL

Page 16: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Cyrus-Beck Algorithm

NL [P(t) - PL] = 0

Substitute line equation for P(t) Solve for t

– t = NL [P0 - PL] / -NL [P1 - P0]

PL

NL

P(t)Inside

P0

P1

Page 17: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Cyrus-Beck Algorithm

Compute t for line intersection with all four edges Discard all (t < 0) and (t > 1) Classify each remaining intersection as

– Potentially Entering (PE)– Potentially Leaving (PL)

NL [P1 - P0] > 0 implies PL

NL [P1 - P0] < 0 implies PE– Note that we computed this term in when computing t

Page 18: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Compute PE with largest t Compute PL with smallest t Clip to these two points

Cyrus-Beck Algorithm

PE

PLP1

PL

PE

P0

Page 19: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Cyrus-Beck Algorithm

Because of horizontal and vertical clip lines:– Many computations reduce

Normals: (-1, 0), (1, 0), (0, -1), (0, 1) Pick constant points on edges solution for t:

– -(x0 - xleft) / (x1 - x0)

– (x0 - xright) / -(x1 - x0)

– -(y0 - ybottom) / (y1 - y0)

– (y0 - ytop) / -(y1 - y0)

Page 20: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Comparison

Cohen-Sutherland– Repeated clipping is expensive– Best used when trivial acceptance and rejection is

possible for most lines

Cyrus-Beck– Computation of t-intersections is cheap– Computation of (x,y) clip points is only done once– Algorithm doesn’t consider trivial accepts/rejects– Best when many lines must be clipped

Liang-Barsky: Optimized Cyrus-Beck Nicholl et al.: Fastest, but doesn’t do 3D

Page 21: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Clipping Polygons

Clipping polygons is more complex than clipping the individual lines– Input: polygon– Output: polygon, or nothing

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

Page 22: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

What happens to a triangle during clipping? Possible outcomes:

triangletriangle

Why Is Clipping Hard?

trianglequad triangle5-gon

How many sides can a clipped triangle have?

Page 23: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

A really tough case:

Why Is Clipping Hard?

Page 24: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

A really tough case:

Why Is Clipping Hard?

concave polygonmultiple polygons

Page 25: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Basic idea:– Consider each edge of the viewport individually– Clip the polygon against the edge equation

Page 26: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Basic idea:– Consider each edge of the viewport individually– Clip the polygon against the edge equation– After doing all planes, the polygon is fully clipped

Page 27: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Basic idea:– Consider each edge of the viewport individually– Clip the polygon against the edge equation– After doing all planes, the polygon is fully clipped

Page 28: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Basic idea:– Consider each edge of the viewport individually– Clip the polygon against the edge equation– After doing all planes, the polygon is fully clipped

Page 29: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Basic idea:– Consider each edge of the viewport individually– Clip the polygon against the edge equation– After doing all planes, the polygon is fully clipped

Page 30: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Basic idea:– Consider each edge of the viewport individually– Clip the polygon against the edge equation– After doing all planes, the polygon is fully clipped

Page 31: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Basic idea:– Consider each edge of the viewport individually– Clip the polygon against the edge equation– After doing all planes, the polygon is fully clipped

Page 32: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Basic idea:– Consider each edge of the viewport individually– Clip the polygon against the edge equation– After doing all planes, the polygon is fully clipped

Page 33: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Basic idea:– Consider each edge of the viewport individually– Clip the polygon against the edge equation– After doing all planes, the polygon is fully clipped

Page 34: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping:The Algorithm Basic idea:

– Consider each edge of the viewport individually– Clip the polygon against the edge equation– After doing all planes, the polygon is fully clipped

Will this work for non-rectangular clip regions? What would 3-D

clipping involve?

Page 35: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Input/output for algorithm:– Input: list of polygon vertices in order – Output: list of clipped poygon vertices consisting of

old vertices (maybe) and new vertices (maybe)

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

Page 36: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Sutherland-Hodgman basic routine:– Go around polygon one vertex at a time– Current vertex has position p – Previous vertex had position s, and it has been

added to the output if appropriate

Page 37: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Edge from s to p takes one of four cases:(Purple 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 38: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Sutherland-Hodgman Clipping

Four cases:– s inside plane and p inside plane

Add p to output Note: s has already been added

– s inside plane and p outside plane Find intersection point i Add i to output

– s outside plane and p outside plane Add nothing

– s outside plane and p inside plane Find intersection point i Add i to output, followed by p

Page 39: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Point-to-Plane test

A very general test to determine if a point p is “inside” a plane P, defined by q and n:

(p - q) • n < 0: p inside P

(p - q) • n = 0: p on P

(p - q) • n > 0: p outside P

P

np

q

P

np

q

P

np

q

Page 40: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Point-to-Plane Test

Dot product is relatively expensive– 3 multiplies– 5 additions– 1 comparison (to 0, in this case)

Think about how you might optimize or special-case this

Page 41: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Finding Line-Plane Intersections

Use parametric definition of edge:L(t) = L0 + (L1 - L0)t

– If t = 0 then L(t) = L0

– If t = 1 then L(t) = L1

– Otherwise, L(t) is part way from L0 to L1

Page 42: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Finding Line-Plane Intersections

Edge intersects plane P where E(t) is on P– q is a point on P– n is normal to P

(L(t) - q) • n = 0

t = [(q - L0) • n] / [(L1 - L0) • n]

– The intersection point i = L(t) for this value of t

Page 43: CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons

Line-Plane Intersections

Note that the length of n doesn’t affect result: Again, lots of opportunity for optimization