Two Dimensional Viewing Graphics Pipeline 5 Graphics Pipeline Window-to-Viewport Mapping 5 Viewing Transformation Clipping Clipping Points Clipping Lines Clipping Polygons Cohen-Sutherland Algorithm Liang-Barsky Algorithm Sutherland-Hodgeman Algorithm Weiler-Atherton Algorithm Text Clipping Computer Graphics – Lecture 5 Computer Graphics – Lecture 5 1 5 Position of a Viewport within a Display Window Window-to-Viewport Mapping (1) We need to map from the clipping window to viewport Clipping window specified in the World Coordinate System Viewport specified in the Device (Screen) Coordinate System Computer Graphics – Lecture 5 3 Computer Graphics – Lecture 5 2 5 4 1 Window-to-Viewport Mapping (2) 5 5 Clipping Clipping Rectangle defined by xl, xr, yt, yb Mapping done by using geometric transformations: 1. Translation: bring bottom-left corner of clipping window to origin => Multiply by Translation Matrix T(–xc, –yc) 2. Scaling: Resize clipping window to size of viewport => Multiply by Scaling Matrix S(wv / wc, hv / hc) 3. Translation: bring bottom-left corner of clipping window to bottom-left corner of viewport => Multiply by Translation Matrix T(xv, yv) Clipping Points: easy! Clipping Lines: more difficult Composite Transformation Matrix: M = T(xv, yv) · S(wv / wc, yv / yc) · T(–xc, –yc) Computer Graphics – Lecture 5 Cohen-Sutherland Line Clipping Algorithm (1) 5 5 Computer Graphics – Lecture 5 Cohen-Sutherland Line Clipping Algorithm (2) 6 5 Assign a 4 bit region code to each region: b4b3b2b1 b1 = 1, if region is to the left of the left boundary b2 = 1, if region is to the right of the right boundary b3 = 1, if region is below bottom boundary b4 = 1, if region is above top boundary Computer Graphics – Lecture 5 7 Computer Graphics – Lecture 5 8 2 Cohen-Sutherland Line Clipping Algorithm (3) A. B. C. Cohen-Sutherland Line Clipping Algorithm (5) The worst case requires four clips Cohen-Sunderland is not the most efficient algorithm as it can end up doing needless clippings Still widely used since widely known Extension to 3D straightforward Find Region Codes (C1 and C2) G for line Remove line from input list If C1 OR C2 = 0000, then add line to output list Else If C1 AND C2 = 0000, then How to trivially reject line below, to right, and to left: C1 AND C2 = C1 AND C2 = C1 AND C2 = How to generalize these four cases? C1 AND C2 is not equal to 0000 The rest are difficult: C1 AND C2 = 0000 5 Cohen-Sutherland Line Clipping Algorithm (4) Start with an input list of lines (endpoints) While input list is not empty If C1 OR C2 = 0000, then trivially accept How to trivially reject a line that has both points above the top? C1 AND C2 = 1xxx Computer Graphics – Lecture 5 5 E F I H J Find intersection of line with an edge (top, bottom, left, right order) Add intersection point and interior point to input list 9 5 End Computer Graphics – Lecture 5 Cyrus-Beck and Liang-Barsky Parametric Line Clipping Algorithms 10 5 Cyrus-Beck Algorithm A More efficient Can clip against convex polygon clip region Liang-Barsky Like Cyrus-Beck, but faster for rectangular clip regions B Computer Graphics – Lecture 5 11 Computer Graphics – Lecture 5 12 3 5 Liang-Barsky Line Clipping Algorithm (1) Liang-Barsky Line Clipping Algorithm (2) 5 xl ≤ x1 + uΔx ≤ xr yb ≤ y1 + uΔy ≤ yt Based on the parametric representation of a line Rewrite as four inequalities: (x1,y1) upk ≤ qk, where k = 1, 2, 3, 4 p1 = –Δx, p2 = Δx, p3 = –Δy, p4 = Δy, (x2,y2) Computer Graphics – Lecture 5 Liang-Barsky Line Clipping Algorithm (3) Computer Graphics – Lecture 5 13 5 Each value of k corresponds to one boundary: k = 1 corresponds to left boundary k = 2 corresponds to right boundary k = 3 corresponds to bottom boundary k = 4 corresponds to top boundary If qk < 0, then p1 is outside kth boundary If qk ≥ 0, then p1 is inside or on kth boundary If pk < 0, then line goes in the direction from outside to inside the boundary If pk > 0, then line goes in the direction from inside to outside the boundary Computer Graphics – Lecture 5 q 1 = x 1 – xl q 2 = x r – x1 q3 = y1 – yb q4 = yt – y1 Liang-Barsky Line Clipping Algorithm (4) 14 5 If pk ≠ 0, then the intersection of the line with the kth boundary is at rk = qk / pk 15 For each line we want to find u1 and u2 that lie in the clip region Computer Graphics – Lecture 5 16 4 5 Liang-Barsky Line Clipping Algorithm (5) For each line segment: u1 = 0; u2 = 1 (start with original endpoints) k = 1 While still need to clip and k ≤ 4 Compute pk and qk If pk = 0 and qk < 0, then reject line and stop clipping Else If u1 > u2 then reject line and stop clipping k = k + 1; End If line not rejected, u1 and u2 are endpoints of clipped line 5 Liang-Barsky computes rk = qk / pk for a line needing clipping, which is faster than computing the intersection of lines in Cohen-Sutherland Liang-Barsky does not have a trivial accept or reject If most lines can be trivially accepted or rejected Example Run: Computer Graphics – Lecture 5 17 Liang-Barsky versus Cohen-Sutherland 5 u1 = 0, u2 = 1, p1 < 0, q1 < 0 u1 = r1, u2 = 1, p2 > 0, q2 > 0 u1 = r1, u2 = 1, p3 > 0, q3 > 0 u1 = r1, u2 = r3, p4 < 0, q4 > 0 u1 = r1, u2 = r3 rk = qk / pk If pk < 0, u1 = max{u1, rk} Else u2 = min{u2, rk} Computer Graphics – Lecture 5 Liang-Barsky Line Clipping Algorithm (6) Use Cohen-Sutherland Line Clipping Using Nonrectangular Polygon Clip Windows 18 5 Cyrus-Beck and Liang-Barsky can be extended to clip lines against convex polygon windows For concave-polygon clipping regions, add dummy edges to modify clipping regions to convex-polygon shape Else Use Liang-Barsky Computer Graphics – Lecture 5 19 Computer Graphics – Lecture 5 20 5 5 Clipping Polygons Want to clip polygons 5 Sutherland-Hodgeman Polygon Clipper Algorithm (1) Divide and Conquer algorithm Clipping against clip region implemented by clip against each region edge Suitable for parallel and pipeline implementation If just clip lines top Computer Graphics – Lecture 5 21 5 Sutherland-Hodgeman Polygon Clipper Algorithm (2) Consider a polygon described v3 by a sequence of vertices V = (v1, v2, v3, v4, …, vn) Want to clip against clip region v7 to get clipped polygon v2 C = (c1, c2, c3, …, cm) Let E be the edge list of V E = {sp | s = vi, p = vi+1} U {vnv1} Want to get updated edge list: K = {sp | s = ci, p = ci+1} U {cnc1} Computer Graphics – Lecture 5 Computer Graphics – Lecture 5 Sutherland-Hodgeman Polygon Clipper Algorithm (3) 22 5 v4 v6 v5 v1 23 Computer Graphics – Lecture 5 24 6 Sutherland-Hodgeman Polygon Clipper Algorithm – Example 5 Weiler-Atherton Polygon Clipping Algorithm (1) V = (a, b, c, d, e, f, g) E = (ab, bc, cd, de, ef, fg, ga) Updated E after clipping by: Right boundary E= Bottom boundary E= Left boundary E= Top boundary E= Computer Graphics – Lecture 5 Weiler-Atherton Polygon Clipping Algorithm (2) Computer Graphics – Lecture 5 Weiler-Atherton Polygon Clipping Algorithm – Pseudocode 26 5 Begin position = start save = off trace-subject(position) End Trace-subject(position) If hit clip if (entering clip and subject not saved) save = on trace-subject(current) else if (leaving clip & clip right not Trace-clip(position) saved) If hit subject save = on if subject right not saved push current onto stack turn on save trace-clip(current) trace-subject(current) else else save = off turn off save if stack empty then stop output region just completed else popstack(current) if stack empty then stop trace-subject(current) else popstack(current) end trace-subject(current) end While traversing the subject polygon: When going from outside the clip polygon to inside, follow the subject polygon boundary in clockwise direction When going from inside the clip polygon to outside, follow the clip polygon boundary in clockwise direction Computer Graphics – Lecture 5 Very general Can clip convex polygon by convex polygon Can clip concave polygon by a convex polygon Clip region is called clip polygon Polygon to be clipped is called subject polygon Traces clockwise around the polygons 25 5 5 27 Computer Graphics – Lecture 5 28 7 Weiler-Atherton Polygon Clipping Algorithm – Example (1) 5 Weiler-Atherton Polygon Clipping Algorithm – Example (2) 5 Counterclockwise processing Computer Graphics – Lecture 5 Text Clipping 29 Computer Graphics – Lecture 5 30 5 All-or-none string-clipping strategy All-or-none character-clipping strategy Clip the components of individual characters Computer Graphics – Lecture 5 31 8