1 CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai
2/135 OpenGL Geometric Primitives All geometric primitives are specified by vertices GL_QUAD_STRIP GL_POLYGON GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_POINTS GL_LINES GL_LINE_LOOP GL_LINE_STRIP GL_TRIANGLES GL_QUADS
3/135 Review: Drawing General Polygons How to draw every interior pixels? ? Scanline conversion of polygons!
4/135 Review: Curved Boundaries How to deal with curved boundaries? Boundary fill algorithm!
5/135 Review: How to deal with this? Multiple color boundaries? Flood fill algorithm!
6/135 Why Clip? We do not want to waste time drawing objects that are outside of viewing window (or clipping window)
7/135 Clipping Points Given a point (x, y)and clipping window (x min, y min ), (x max, y max ), determine if the point should be drawn (x min, y min ) (x max, y max ) (x,y)
8/135 Clipping Points Given a point (x, y)and clipping window (x min, y min ), (x max, y max ), determine if the point should be drawn (x min, y min ) (x max, y max ) (x,y) x min =<x<=x max ? y min =<y<=y max ?
9/135 Clipping Points Given a point (x, y)and clipping window (x min, y min ), (x max, y max ), determine if the point should be drawn ( x 1, y 1) ( x 2, y 2) (x min, y min ) (x max, y max ) x min =<x<=x max ? y min =<y<=y max ?
10/135 Clipping Points Given a point (x, y)and clipping window (x min, y min ), (x max, y max ), determine if the point should be drawn ( x 1, y 1) ( x 2, y 2) (x min, y min ) (x max, y max ) x min =<x 1 <=x max Yes y min =<y 1 <=y max Yes
11/135 Clipping Points Given a point (x, y)and clipping window (x min, y min ), (x max, y max ), determine if the point should be drawn ( x 1, y 1) ( x 2, y 2) (x min, y min ) (x max, y max ) x min =<x 2 <=x max No y min =<y 2 <=y max No
12/135 Clipping Lines
13/135 Clipping Lines
14/135 Clipping Lines Given a line with end-points (x 0, y 0 ), (x 1, y 1 ) and clipping window (x min, y min ), (x max, y max ), determine if line should be drawn and clipped end-points of line to draw. (x 0, y 0 ) (x 1, y 1 ) (x min, y min ) (x max, y max )
15/135 Clipping Lines
16/135 Outline Simple line clipping algorithm Cohen-Sutherland Liang-Barsky
17/135 Clipping Lines
18/135 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
19/135 Clipping Lines – Simple Algorithm
20/135 Clipping Lines – Simple Algorithm
21/135 Intersecting Two Lines
22/135 Intersecting Two Lines
23/135 Intersecting Two Lines
24/135 Intersecting Two Lines
25/135 Intersecting Two Lines
26/135 Intersecting Two Lines Substitute t or s back into equation to find intersection
27/135 Clipping Lines – Simple Algorithm
28/135 Clipping Lines – Simple Algorithm
29/135 Clipping Lines – Simple Algorithm
30/135 Clipping Lines – Simple Algorithm
31/135 Clipping Lines – Simple Algorithm
32/135 Clipping Lines – Simple Algorithm
33/135 Clipping Lines – Simple Algorithm
34/135 Clipping Lines – Simple Algorithm
35/135 Clipping Lines – Simple Algorithm
36/135 Clipping Lines – Simple Algorithm
37/135 Clipping Lines – Simple Algorithm
38/135 Clipping Lines – Simple Algorithm
39/135 Clipping Lines – Simple Algorithm
40/135 Clipping Lines – Simple Algorithm
41/135 Clipping Lines – Simple Algorithm
42/135 Clipping Lines – Simple Algorithm Lots of intersection tests makes algorithm expensive Complicated tests to determine if intersecting rectangle Is there a better way?
43/135 Trivial Accepts Big Optimization: trivial accepts/rejects How can we quickly decide whether line segment is entirely inside window Answer: test both endpoints
44/135 Trivial Accepts Big Optimization: trivial accepts/rejects How can we quickly decide whether line segment is entirely inside window Answer: test both endpoints
45/135 Trivial Rejects How can we know a line is outside of the window Answer:
46/135 Trivial Rejects How can we know a line is outside of the window Answer: Wrong side
47/135 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
48/135 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments
49/135 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 4bit 3bit 2bit 1 leftrightbottomtop
50/135 Cohen-Sutherland Algorithm bit 4bit 3bit 2bit 1 leftrightbottomtop right left bottom Region code?
51/135 Cohen-Sutherland Algorithm bit 4bit 3bit 2bit 1 leftrightbottomtop right left bottom 0010 ?
52/135 Cohen-Sutherland Algorithm bit 4bit 3bit 2bit 1 leftrightbottomtop right left bottom
53/135 Cohen-Sutherland Algorithm
54/135 Cohen-Sutherland Algorithm
55/135 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments
56/135 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments
57/135 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments Line is outside the window! reject
58/135 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments Line is outside the window! reject
59/135 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments Line is inside the window! draw
60/135 Cohen-Sutherland Algorithm Classify p 0, p 1 using region codes c 0, c 1 If, trivially reject If, trivially accept Otherwise reduce to trivial cases by splitting into two segments
61/135 Window Intersection (x 1, y 1 ), (x 2, y 2 ) intersect with vertical edge at x right y intersect = y 1 + m(x right – x1) where m=(y 2 -y 1 )/(x 2 -x 1 ) (x 1, y 1 ), (x 2, y 2 ) intersect with horizontal edge at y bottom x intersect = x 1 + (y bottom – y1)/m where m=(y 2 -y 1 )/(x 2 -x 1 )
62/135 Example 1
63/135 Example 1
64/135 Example 1
65/135 Example 1
66/135 Example 2
67/135 Example 2
68/135 Example 2
69/135 Example 2
70/135 Example 2
71/135 Example 2
72/135 Example 2
73/135 Example 3
74/135 Example 3
75/135 Example 3
76/135 Example 3
77/135 Cohen Sutherland Java applet: click herehere
78/135 Cohen-Sutherland Algorithm Extends easily to 3D line clipping 27 regions 6 bits
79/135 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
80/135 Liang-Barsky Algorithm Parametric definition of a line: x = x 1 + u Δ x y = y 1 + u Δ y Δ x = (x 2 -x 1 ), Δ y = (y 2 -y 1 ), 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
81/135 Liang-Barsky Algorithm Current clipped line (u 1 =0,u 2 =1) (-.5,-.5) (2,1)
82/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.333,u 2 =0.6) (-.5,-.5) (2,1)
83/135 Liang-Barsky Algorithm For lines starting outside of boundary, update its starting point (u 1 ) For lines starting inside of boundary, update end point (u 2 ) For lines paralleling the boundaries and outside window, reject it.
84/135 Liang-Barsky Algorithm For lines starting outside of boundary, update its starting point (u 1 ) For lines starting inside of boundary, update end point (u 2 ) For lines paralleling the boundaries and outside window, reject it.
85/135 Liang-Barsky Algorithm Mathematically: x min <= x 1 + u Δ x <= x max y min <= y 1 + u Δ y <= y max Rearranged 1:u*(- Δ x) <= (x 1 – x min ) 2:u*( Δ x) <= (x max – x 1 ) 3:u*(- Δ y) <= (y 1 – y min ) 4:u*( Δ y) <= (y max – y 1 ) gen:u*(p k ) <= (q k ), k=1,2,3,4
86/135 Liang-Barsky Algorithm Rules: 1) p k = 0: the line is parallel to boundaries If for that same k, q k < 0, it’s outside Otherwise it’s inside 2) p k < 0: the line starts outside this boundary r k = q k /p k u 1 = max(0, r k, u 1 ) /**update starting point**/ p k > 0: the line starts inside the boundary r k = q k /p k u 2 = min(1, r k, u 2 ) /** update end point**/ If u 1 > u 2, the line is completely outside
87/135 Liang-Barsky Algorithm Current clipped line (u 1 =0,u 2 =1) (-.5,-.5) (2,1)
88/135 Liang-Barsky Algorithm Current clipped line (u 1 =0,u 2 =1) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 1 = -Δx =-2.5<0 q 1 = (x 1 -x min )=-.5 r 1 = q 1 /p 1 =0.2
89/135 Liang-Barsky Algorithm Current clipped line (u 1 =0,u 2 =1) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 1 = -Δx =-2.5<0 q 1 = (x 1 -x min )=-.5 r 1 = q 1 /p 1 =0.2 line starts outside this boundary
90/135 Liang-Barsky Algorithm Current clipped line (u 1 =0,u 2 =1) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 1 = -Δx =-2.5<0 q 1 = (x 1 -x min )=-.5 r 1 = q 1 /p 1 =0.2
91/135 Liang-Barsky Algorithm Current clipped line (u 1 =0,u 2 =1) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 1 = -Δx =-2.5<0 q 1 = (x 1 -x min )=-.5 r 1 = q 1 /p 1 =0.2
92/135 Liang-Barsky Algorithm Current clipped line (u 1 =0,u 2 =1) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 1 = -Δx =-2.5<0 q 1 = (x 1 -x min )=-.5 r 1 = q 1 /p 1 =0.2u 1 = max(0, r 1, u 1 )
93/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.2,u 2 =1) (-.5,-.5) (2,1) u 1 <u2? - yes: continue - no: the line is completely outside window
94/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.2,u 2 =1) (-.5,-.5) (2,1)
95/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.2,u 2 =1) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 2 = Δx =2.5>0 q 2 = (x max -x 1 )=1.5 r 2 = q 2 /p 2 =0.6
96/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.2,u 2 =1) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 2 = Δx =2.5>0 q 2 = (x max -x 1 )=1.5 r 2 = q 2 /p 2 =0.6u 2 = min(1, r 2, u 2 )
97/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.2,u 2 =0.6) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 2 = Δx =2.5>0 q 2 = (x max -x 1 )=1.5 r 2 = q 2 /p 2 =0.6
98/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.2,u 2 =0.6) (-.5,-.5) (2,1) u 1 <u2? - yes: continue - no: the line is completely outside window
99/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.2,u 2 =0.6) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 3 = -Δy =-1.5<0 q 3 = (y 1 -y min )=0.5 r 3 = q 3 /p 3 =0.333
100/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.2,u 2 =0.6) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 3 = -Δy =-1.5<0 q 3 = (y 1 -y min )=0.5 r 3 = q 3 /p 3 =0.333u 1 = max(0, r 3, u 1 )
101/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.333,u 2 =0.6) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 3 = -Δy =-1.5<0 q 3 = (y 1 -y min )=0.5 r 3 = q 3 /p 3 =0.333
102/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.333,u 2 =0.6) (-.5,-.5) (2,1)
103/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.333,u 2 =0.6) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 3 = -Δy =-1.5<0 q 3 = (y 1 -y min )=0.5 r 3 = q 3 /p 3 =0.333
104/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.333,u 2 =0.6) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 4 = Δy =1.5>0 q 4 = (y max -y 1 )=1.5 r 4 = q 4 /p 4 =1
105/135 Liang-Barsky Algorithm Current clipped line (u 1 =0.333,u 2 =0.6) (-.5,-.5) (2,1) Δx = (x2-x1)=2.5 Δy = (y2-y1)=1.5 p 4 = Δy =1.5>0 q 4 = (y max -y 1 )=1.5 r 4 = q 4 /p 4 =1u 2 = min(1, r 4, u 2 )
106/135 Liang-Barsky Algorithm Check the left edge (u 1 =0.333,u 2 =0.6) (-.5,-.5) (2,1) u 1 <u 2 x new1 =x 1 +Δx*u 1 y new1 =y 1 +Δy*u 1 x new2 =x 2 +Δx*u 2 y new2 =y 2 +Δy*u 2
107/135 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
108/135 Comparison Cohen-Sutherland 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
109/135 Curve Clipping
110/135 Curve Clipping
111/135 Curve Clipping Approximate a curve using a set of straight- line segments Apply line clipping for curve clipping
112/135 Next Lecture Polygon fill-area clipping
113/135 Next Lecture Polygon fill-area clipping