CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai
OpenGL Geometric Primitives All geometric primitives are specified by vertices GL_LINE_STRIP GL_LINE_LOOP GL_POLYGON GL_LINES GL_POINTS GL_TRIANGLE_STRIP GL_QUAD_STRIP GL_TRIANGLES GL_QUADS GL_TRIANGLE_FAN
Why Clip? We do not want to waste time drawing objects that are outside of viewing window (or clipping window)
Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x,y) (xmin,ymin)
Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x,y) xmin=<x<=xmax? (xmin,ymin) ymin=<y<=ymax?
Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x2,y2) (x1,y1) xmin=<x<=xmax? (xmin,ymin) ymin=<y<=ymax?
Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x2,y2) (x1,y1) xmin=<x1<=xmax Yes (xmin,ymin) ymin=<y1<=ymax Yes
Clipping Points Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn (xmax,ymax) (x2,y2) (x1,y1) xmin=<x2<=xmax No (xmin,ymin) ymin=<y2<=ymax Yes
Clipping Lines
Clipping Lines
Clipping Lines Given a line with end-points (x0, y0), (x1, y1) and clipping window (xmin, ymin), (xmax, ymax), determine if line should be drawn and clipped end-points of line to draw. (xmax,ymax) (x1, y1) (x0, y0) (xmin,ymin)
Clipping Lines
Outline Simple line clipping algorithm Cohen-Sutherland Liang-Barsky Required readings: HB 8-5, 8-6, 8-7 and 8-9
Clipping Lines
Clipping Lines – Simple Algorithm If both end-points inside rectangle, draw line If one end-point outside, intersect line with all edges of rectangle clip that point and repeat test
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Intersecting Two Lines
Intersecting Two Lines
Intersecting Two Lines
Intersecting Two Lines
Intersecting Two Lines
Intersecting Two Lines Substitute t or s back into equation to find intersection
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm
Clipping Lines – Simple Algorithm Lots of intersection tests makes algorithm expensive Complicated tests to determine if intersecting rectangle Is there a better way?
Trivial Accepts Big Optimization: trivial accepts/rejects How can we quickly decide whether line segment is entirely inside window Answer: test both endpoints
Trivial Accepts Big Optimization: trivial accepts/rejects How can we quickly decide whether line segment is entirely inside window Answer: test both endpoints
Trivial Rejects How can we know a line is outside of the window Answer:
Trivial Rejects How can we know a line is outside of the window Answer: Wrong side
Trivial Rejects How can we know a line is outside of the window Answer: both endpoints on wrong side of same edge, can trivially reject the line
Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments
Cohen-Sutherland Algorithm Every end point is assigned to a four-digit binary value, i.e. Region code Each bit position indicates whether the point is inside or outside of a specific window edges bit 4 bit 3 bit 2 bit 1 top bottom right left
Cohen-Sutherland Algorithm bit 4 bit 3 bit 2 bit 1 top bottom right left top right left Region code? bottom
Cohen-Sutherland Algorithm bit 4 bit 3 bit 2 bit 1 top bottom right left top right left 0010 bottom ?
Cohen-Sutherland Algorithm bit 4 bit 3 bit 2 bit 1 top bottom right left top right left 0010 bottom 0100
Cohen-Sutherland Algorithm
Cohen-Sutherland Algorithm
Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject because both points located the wrong side of one edge. If , trivially accept because both points are not in the wrong side of any edge. Otherwise reduce to trivial cases by splitting into two segments
Cohen-Sutherland Algorithm Bitwise and
Cohen-Sutherland Algorithm Bitwise and Bitwise or
Cohen-Sutherland Algorithm Bitwise and Bitwise or
Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments
Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments Line is outside the window! reject
Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments Line is outside the window! reject
Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments Line is inside the window! draw
Cohen-Sutherland Algorithm Classify p0, p1 using region codes c0, c1 If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting into two segments
Window Intersection (x1, y1), (x2, y2) intersect with vertical edge at xright yintersect = y1 + m(xright – x1) where m=(y2-y1)/(x2-x1) (x1, y1), (x2, y2) intersect with horizontal edge at ybottom xintersect = x1 + (ybottom – y1)/m
Example 1
Example 1
Example 1
Example 1
Example 2
Example 2
Example 2
Example 2
Example 2
Example 2
Example 2
Example 3
Example 3
Example 3
Example 3
Cohen-Sutherland Algorithm Extends easily to 3D line clipping 27 regions 6 bits
Cohen-Sutherland Algorithm Use region codes to quickly eliminate/include lines Best algorithm when trivial accepts/rejects are common Must compute viewing window clipping of remaining lines Non-trivial clipping cost More efficient algorithms exist
Liang-Barsky Algorithm Parametric definition of a line: x = x1 + uΔx y = y1 + uΔy Δx = (x2-x1), Δy = (y2-y1), 0<=u<=1 Lines are oriented: classify lines as moving inside to out or outside to in Goal: find range of u for which x and y both inside the viewing window
Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) (-.5,-.5)
Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) (-.5,-.5)
Liang-Barsky Algorithm For lines starting outside of boundary, update its starting point (u1) For lines starting inside of boundary, update end point (u2) For lines paralleling the boundaries and outside window, reject it.
Liang-Barsky Algorithm For lines starting outside of boundary, update its starting point (u1) For lines starting inside of boundary, update end point (u2) For lines paralleling the boundaries and outside window, reject it.
Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4
Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) // left edge 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4
Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) // right edge 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4
Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) // bottom edge 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4
Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) // top edge gen: u*(pk) <= (qk), k=1,2,3,4
Liang-Barsky Algorithm Mathematically: xmin <= x1 + uΔx <= xmax ymin <= y1 + uΔy <= ymax Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) Gen: u*(pk) <= (qk), k=1,2,3,4
Liang-Barsky Algorithm Rules: pk = 0: the line is parallel to boundaries If for that same k, qk < 0, it’s outside Otherwise it’s inside pk < 0: the line starts outside this boundary rk = qk/pk u1 = max(0, rk, u1) /**update starting point**/ pk > 0: the line starts inside the boundary u2 = min(1, rk, u2) /** update end point**/ If u1 > u2, the line is completely outside
Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) (-.5,-.5)
Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 r1 = q1/p1=0.2
Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 line starts outside this boundary r1 = q1/p1=0.2
Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 r1 = q1/p1=0.2
Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 r1 = q1/p1=0.2
Liang-Barsky Algorithm Current clipped line (u1=0,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p1 = -Δx =-2.5<0 (-.5,-.5) q1 = (x1-xmin)=-.5 u1 = max(0, r1, u1) r1 = q1/p1=0.2
Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=1) (2,1) u1<u2? yes: continue no: the line is completely outside window (-.5,-.5)
Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=1) (2,1) (-.5,-.5)
Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p2 = Δx =2.5>0 (-.5,-.5) q2 = (xmax-x1)=1.5 r2 = q2/p2=0.6
Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=1) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p2 = Δx =2.5>0 (-.5,-.5) q2 = (xmax-x1)=1.5 u2 = min(1, r2, u2) r2 = q2/p2=0.6
Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p2 = Δx =2.5>0 (-.5,-.5) q2 = (xmax-x1)=1.5 r2 = q2/p2=0.6
Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=0.6) (2,1) u1<u2? yes: continue no: the line is completely outside window (-.5,-.5)
Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 r3 = q3/p3=0.333
Liang-Barsky Algorithm Current clipped line (u1=0.2,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 u1 = max(0, r3, u1) r3 = q3/p3=0.333
Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 r3 = q3/p3=0.333
Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) (-.5,-.5)
Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p3 = -Δy =-1.5<0 (-.5,-.5) q3 = (y1-ymin)=0.5 r3 = q3/p3=0.333
Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p4 = Δy =1.5>0 (-.5,-.5) q4 = (ymax-y1)=1.5 r4 = q4/p4=1
Liang-Barsky Algorithm Current clipped line (u1=0.333,u2=0.6) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p4 = Δy =1.5>0 (-.5,-.5) q4 = (ymax-y1)=1.5 u2 = min(1, r4, u2) r4 = q4/p4=1
Liang-Barsky Algorithm Check the left edge (u1=0.333,u2=0.6) (2,1) u1<u2 (-.5,-.5) xnew1=x1+Δx*u1 xnew2=x2+Δx*u2 ynew1=y1+Δy*u1 ynew2=y2+Δy*u2
Liang-Barsky Algorithm Faster than Cohen-Sutherland Extension to 3D is easy - Parametric representation for 3D lines - Compute u1,u2 based on the intersection between line and plane
Comparison Cohen-Sutherland Liang-Barsky Repeated clipping is expensive Best used when trivial acceptance and rejection is possible for most lines Liang-Barsky Computation of t-intersections is cheap (only one division) Computation of (x,y) clip points is only done once Algorithm doesn’t consider trivial accepts/rejects Best when many lines must be clipped
Curve Clipping
Curve Clipping
Curve Clipping Approximate a curve using a set of straight-line segments Apply line clipping for curve clipping
Next Lecture Polygon fill-area clipping
Next Lecture Polygon fill-area clipping