Overview Lecture 5 Clipping Cohen-Sutherland line clipping algorithm Liang-Barsky line clipping algorithm Sutherland-Hogeman polygon clipping Line and Polygon Clipping Computer Graphics & Visualization 2 Clipping Algorithms Computer Graphics & Visualization Cohen-Sutherland Line-Clipping Line Clipping: 1. 1000 1010 0001 0000 0010 If C0 ∧ Cend ≠ 0 then P0Pend is trivially rejected 0101 0100 0110 3. If C0 ∨ Cend = 0 then P0Pend is trivially accepted C0 = 4. Otherwise subdivide and go to step 1 with new segment. - Oldest and most commonly used Nicholl-Lee-Nicholl (encoding) (more efficient) Liang-Barsky (parametric) 2. - More efficient than Cohen-Sutherland Polygon Clipping: Sutherland-Hodgeman (divide and conquer strategy) Weiler-Atherton (modified for concave polygons) Computer Graphics & Visualization Pedher Johansson Department of Computing Science, Umeå University 4 Cohen-Sutherland Line-Clipping 1) 2) 3) 4) 5 A3D3 A3C3 A3B3 accept 1) 2) 3) 4) 5) A2E2 B2E2 1001 B2D2 B2C2 accept A1 0001 C1 1010 D2 C2 0010 B2 0000 A3 A2 0101 C3 B3 0100 0110 D3 Computer Graphics & Visualization Pedher Johansson Department of Computing Science, Umeå University Cend = Bit code of Pend Pedher Johansson Department of Computing Science, Umeå University Will do unnecessary clipping. Not the most efficient. Clipping and testing are done in fixed order. Efficient when most of the lines to be clipped are either rejected or accepted (not so many subdivisions). Easy to program. Parametric clipping are more efficient. E2 B1 1000 Computer Graphics & Visualization Bit code of P0 Cohen-Sutherland Line-Clipping Clip order: Left, Right, Bottom, Top 1) A1C1 2) B1C1 3) reject Encode end points Bit 0 = point is left of window Bit 1 = point is right of window Bit 2 = point is below window Bit 3 = point is above window 1001 Cohen-Suterland (encoding) 3 Pedher Johansson Department of Computing Science, Umeå University 6 Computer Graphics & Visualization Pedher Johansson Department of Computing Science, Umeå University 1 Parametric form Liang-Barsky Line-Clipping A line segment with endpoints More efficient than Cohen-Sutherland (x0, y0) and (xend, yend) A line is inside the clipping region for values of u such that: Δ x = xend – x0 xwmin ≤ x0 + uΔx ≤ xwmax ywmin ≤ y0 + uΔy ≤ ywmax Δ y = yend – y0 we can describe in the parametric form x = x0 + uΔx y = x0 + uΔy 0≤u≤1 where Can be described as u pk ≤ qk, k = 1, 2, 3, 4 Δx = xend – x0 Δy = yend – y0 7 Computer Graphics & Visualization Pedher Johansson Department of Computing Science, Umeå University 8 Liang-Barsky Line-Clipping Computer Graphics & Visualization Pedher Johansson Department of Computing Science, Umeå University Liang-Barsky Line-Clipping The infinitely line intersects the clip region edges when: When pk < 0, as u increases - line goes from outside to inside - entering When pk > 0, p1 = −Δx q1 = x0 − xwmin p2 = Δx q2 = xwmax − x0 q uk = k where p3 = −Δy q3 = y0 − ywmin pk p4 = Δy q4 = ywmax − y0 9 Computer Graphics & Visualization - line goes from inside to outside - exiting Left boundary Right boundary Bottom boundary Top boundary Pedher Johansson Department of Computing Science, Umeå University When pk = 0, - line is parallel to an edge If there is a segment of the line inside the clip region, a sequence of infinite line intersections must go: entering, entering, exiting, exiting 10 Liang-Barsky Line-Clipping Enter Exit Enter 11 1. Set umin = 0 and umax = 1. 2. Calculate the u values: 3. If u < umin or u > umax ignore it. Otherwise classify the u values as entering or exiting. 4. If umin < umax then draw a line from: Exit Clip region Enter ( x0 + Δx · umin, y0 + Δy · umin ) to ( x0 + Δx · umax, y0 + Δy · umax ) Enter Computer Graphics & Visualization Pedher Johansson Department of Computing Science, Umeå University Liang-Barsky Line-Clipping Exit Exit Computer Graphics & Visualization Pedher Johansson Department of Computing Science, Umeå University 12 Computer Graphics & Visualization Pedher Johansson Department of Computing Science, Umeå University 2 Example Liang-Barsky 10,10 0,10 Liang-Barsky Line-Clipping Pend(15,9) P0(-5,3) We have umin = 1/4 and umax = 3/4 0,0 uleft = uright 10,0 q1 x0 − xwmin 1 −5−0 = = = p1 − (15 − (−5)) 4 − Δx xw − x 10 − (−5) 3 q = 2 = max 0 = = p2 15 − (−5) 4 Δx Exiting u < 0 then ignore q4 ywmax − y0 10 − 3 7 = = = 9−3 6 Δy p4 u > 1 then ignore 13 Pedher Johansson Computer Graphics & Visualization Example Liang-Barsky 0,10 Δx Department of Computing Science, Umeå University Pend(2,14) - compute endpoints by substituting u values Draw a line from (-5+(20)·(1/4), 3+(6)·(1/4)) to (-5+(20)·(3/4), 3+(6)·(3/4)) 14 10,10 Computer Graphics & Visualization We have umin = 4/5 and umax = 2/3 0,0 q1 x0 − xwmin 4 −8−0 = = = p1 − Δx − (2 − (−8)) 5 uright = q2 xwmax − x0 10 − (−8) 9 = = = p2 2 − (−8) 5 Δx ubottom = utop = 15 10,0 Entering Pend - P0 = (2+8, 14-2) = (10, 12) umin = 4/5 umin > umax , there is no line segment do draw u > 1 then ignore q3 y0 − ywmin 2−0 1 = = = − u < 0 then ignore 6 p3 − Δy − (14 − 2) q4 ywmax − y0 10 − 2 2 = = = 14 − 2 3 Δy p4 Computer Graphics & Visualization Exiting umax = 2/3 Pedher Johansson Department of Computing Science, Umeå University 16 Nicholl-Lee-Nicholl Line Clipping L B If P0 is to the left: P0 edge region If P0 is to the left and above: P0 L corner region T L P0 Find position of Pend relative to P0. Computer Graphics & Visualization R P0 LT inside Pedher Johansson Department of Computing Science, Umeå University T If P0 inside and Pend outside: P0 P0 Computer Graphics & Visualization Nicholl-Lee-Nicholl Line Clipping Avoids multiple clipping of an individual line by creating more regions. Only three regions need to be considered. 17 Pedher Johansson Department of Computing Science, Umeå University Liang-Barsky Line-Clipping P0(-8,2) uleft = Δy If umin < umax , there is a line segment umax = 3/4 q y − ywmin 3−0 1 ubottom = 3 = 0 = =− 2 p3 − Δy − (9 − 3) utop = Pend - P0 = (15+5,9-3) = (20,6) umin = 1/4 Entering LR L L LB Pedher Johansson Department of Computing Science, Umeå University 18 Computer Graphics & Visualization TR T LB TB Pedher Johansson Department of Computing Science, Umeå University 3 Nicholl-Lee-Nicholl Line Clipping Suterland-Hodgeman Polygon Clipping To determine region Four test cases: 1. 2. 3. 4. compare slopes, and boundaries of the NLN region. First vertex inside and the second outside (in-out pair) Both vertices inside clip window First vertex outside and the second inside (out-in pair) Both vertices outside the clip window Concave polygons may be displayed with extra lines. 19 Computer Graphics & Visualization Pedher Johansson Department of Computing Science, Umeå University 20 Computer Graphics & Visualization Pedher Johansson Department of Computing Science, Umeå University Weiler-Atherton Polygon Clipping Clips concave polygons correctly. Instead of always going around the polygon edges, we also, want to follow window boundaries. For an outside-to-inside pair of vertices, follow the polygon boundary. For an inside-to-outside pair of vertices, follow the window boundary in a clockwise direction. 21 Computer Graphics & Visualization Pedher Johansson Department of Computing Science, Umeå University 4