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

## Presentation on theme: "CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons."— Presentation transcript:

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

Know your role... l Brian talks about assignment 1 l Clipping lines to viewports l Clipping lines to arbitrary polygons l Clipping polygons

Clipping Lines To Viewport l 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

Solving Simultaneous Equations l Equation of a line –Slope-intercept: y = mx + b –Implicit Equation: Ax + By + C = 0 –Parametric: Line defined by two points, P 0 and P 1 n P(t) = P 0 + (P 1 - P 0 ) t n x(t) = x 0 + (x 1 - x 0 ) t n y(t) = x 0 + (y 1 - y 0 ) t

Parametric Line Equation l Describes a finite line l Works with vertical lines (like the viewport) l 0 <=t <= 1 –Defines line between P 0 and P 1 l t < 0 –Defines line before P 0 l t > 1 –Defines line after P 1

Parametric Lines and Clipping l Define each line in parametric form: –P 0 (t)…P n-1 (t) l Define each edge of viewport in parametric form: –P L (t), P R (t), P T (t), P B (t) l For each line, compute intersection with all viewport edges

Computing Intersections l Line 0: –x 0 = x 0 0 + (x 0 1 - x 0 0 ) t 0 –y 0 = y 0 0 + (y 0 1 - y 0 0 ) t 0 l Edge L: –x L = x L 0 + (x L 1 - x L 0 ) t L –y L = y L 0 + (y L 1 - y L 0 ) t L l x 0 0 + (x 0 1 - x 0 0 ) t 0 = x L 0 + (x L 1 - x L 0 ) t L l y 0 0 + (y 0 1 - y 0 0 ) t 0 = y L 0 + (y L 1 - y L 0 ) t L –Solve for t 0 and t L

What’s wrong with this?

Cohen-Sutherland Line Clipping l Divide viewplane into regions defined by viewport edges l Assign each region a 4-bit outcode: 000000100001 1001 01010100 10001010 0110

Cohen-Sutherland Line Clipping l Set bits with simple tests x > x max y < y min etc. l Assign an outcode to each vertex of line –If both outcodes = 0, trivial accept –bitwise AND vertex outcodes together –If result  0, trivial reject

Cohen-Sutherland Line Clipping l If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded l Pick an edge that the line crosses A B D E C

Cohen-Sutherland Line Clipping l Intersect line with edge (how?) A B D E C

l Discard portion on wrong side of edge and assign outcode to new vertex l Apply trivial accept/reject tests and repeat if necessary Cohen-Sutherland Line Clipping A B D C

l Outcode tests and line-edge intersects are quite fast l But some lines require multiple iterations (see F&vD figure 3.40) l Fundamentally more efficient algorithms: –Cyrus-Beck uses parametric lines –Liang-Barsky optimizes this for upright volumes –F&vD for more details

Cyrus-Beck Algorithm l Use parametric equations of lines l Optimize l We saw that this could be expensive… l Start with parametric equation of line: –P(t) = P 0 + (P 1 - P 0 ) t l And a point and normal for each edge –P L, N L

Cyrus-Beck Algorithm l N L [P(t) - P L ] = 0 l Substitute line equation for P(t) l Solve for t –t = N L [P 0 - P L ] / -N L [P 1 - P 0 ] PLPL NLNL P(t) Inside P0P0 P1P1

Cyrus-Beck Algorithm l Compute t for line intersection with all four edges l Discard all (t 1) l Classify each remaining intersection as –Potentially Entering (PE) –Potentially Leaving (PL) l N L [P 1 - P 0 ] > 0 implies PL l N L [P 1 - P 0 ] < 0 implies PE –Note that we computed this term in when computing t

l Compute PE with largest t l Compute PL with smallest t l Clip to these two points Cyrus-Beck Algorithm PE PL P1P1 PE P0P0

Cyrus-Beck Algorithm l Because of horizontal and vertical clip lines: –Many computations reduce l Normals: (-1, 0), (1, 0), (0, -1), (0, 1) l Pick constant points on edges l solution for t: –-(x 0 - x left ) / (x 1 - x 0 ) –(x 0 - x right ) / -(x 1 - x 0 ) –-(y 0 - y bottom ) / (y 1 - y 0 ) –(y 0 - y top ) / -(y 1 - y 0 )

Comparison l Cohen-Sutherland –Repeated clipping is expensive –Best used when trivial acceptance and rejection is possible for most lines l 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 l Liang-Barsky: Optimized Cyrus-Beck l Nicholl et al.: Fastest, but doesn’t do 3D

Clipping Polygons l Clipping polygons is more complex than clipping the individual lines –Input: polygon –Output: polygon, or nothing l When can we trivially accept/reject a polygon as opposed to the line segments that make up the polygon?

l What happens to a triangle during clipping? l Possible outcomes: triangle  triangle Why Is Clipping Hard? triangle  quad triangle  5-gon l How many sides can a clipped triangle have?

l A really tough case: Why Is Clipping Hard?

l A really tough case: Why Is Clipping Hard? concave polygon  multiple polygons

Sutherland-Hodgman Clipping l Basic idea: –Consider each edge of the viewport individually –Clip the polygon against the edge equation

Sutherland-Hodgman Clipping l 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

Sutherland-Hodgman Clipping l 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

Sutherland-Hodgman Clipping l 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

Sutherland-Hodgman Clipping l 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

Sutherland-Hodgman Clipping l 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

Sutherland-Hodgman Clipping l 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

Sutherland-Hodgman Clipping l 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

Sutherland-Hodgman Clipping l 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

Sutherland-Hodgman Clipping: The Algorithm l 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 l Will this work for non-rectangular clip regions? l What would 3-D clipping involve?

Sutherland-Hodgman Clipping l 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) l Note: this is exactly what we expect from the clipping operation against each edge

Sutherland-Hodgman Clipping l 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

Sutherland-Hodgman Clipping l Edge from s to p takes one of four cases: (Purple line can be a line or a plane) insideoutside s p p output insideoutside s p no output insideoutside s p i output insideoutside s p i output p output

Sutherland-Hodgman Clipping l Four cases: –s inside plane and p inside plane n Add p to output n Note: s has already been added –s inside plane and p outside plane n Find intersection point i n Add i to output –s outside plane and p outside plane n Add nothing –s outside plane and p inside plane n Find intersection point i n Add i to output, followed by p

Point-to-Plane test l 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 n p q P n p q P n p q

Point-to-Plane Test l Dot product is relatively expensive –3 multiplies –5 additions –1 comparison (to 0, in this case) l Think about how you might optimize or special-case this

Finding Line-Plane Intersections l Use parametric definition of edge: L(t) = L 0 + (L 1 - L 0 )t –If t = 0 then L(t) = L 0 –If t = 1 then L(t) = L 1 –Otherwise, L(t) is part way from L 0 to L 1

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 - L 0 ) n] / [(L 1 - L 0 ) n] –The intersection point i = L(t) for this value of t

Line-Plane Intersections Note that the length of n doesn’t affect result: l Again, lots of opportunity for optimization

Download ppt "CS 551 / 645: Introductory Computer Graphics Clipping Lines and Polygons."

Similar presentations