Concepts, algorithms for clipping
Overview Clipping Point Line clipping algorithms Cohen-Sutherland Liang-Barsky Polygon clipping algorithms Hodgman-Sutherland Weiler-Atherton
What is Clipping? The process of determining the portions of a primitive lying within a region called the ‘clipping region’ Performed in 2D (against window) and 3D (against volume) Types of clipping Point clipping Line clipping Polygon clipping
Clipping The purpose of clipping? For preventing Actively in one window from affecting pixels in other windows Mathematical overflow and underflow from primitives passing behind the eye point or at great distances (in 3D) Rasterisation is computationally expensive More or less linear with number of pixels created After clipping – only rasterise that which is in the viewable region A few clipping operations will save many other operations later on
Point Clipping Okay, this one’s easy! Given (x, y) check Note: Viewport does not necessarily have to be the same as screen size
Point Clipping Example application Used in scenes that are modelled with particle systems (e.g. clouds, smoke, explosions. etc.)
Line Clipping in 2D Shortened line passed to rasterisation stage There must be no difference in the final image
Line Clipping Four possible cases A. Both points inside B. Only one point inside C. Both points outside D. Both points outside, but part of line inside Case A is easy, but what about B, C, and D? Brute force approach Determine intersection of line segment with clipping window edge
Line Clipping First determine whether line segment completely inside or outside clipping window Use point-clipping test to test endpoints against all 4 clipping window boundaries If endpoints are both inside, then keep If both outside (the same clipping boundary), then remove If test fails Means that the line segment intersects at least one clipping boundary and may or may not cross into the interior of the clipping window
Parametric Line Formulation for Clipping A line segment can be represented as x = x0 + u(xend – x0) y = y0 + u(yend – y0) 0 ≤ u ≤ 1 For each clipping window edge Test whether line crossed that edge, by assigning that edge’s coordinate value to either x or y and solve for u If u outside range 0 to 1, line does not intersect window border If u within the range 0 to 1, then part of the line is inside that border Process this until entire line is clipped
Parametric Line Clipping x = x0 + u(xend – x0) y = y0 + u(yend – y0) 0 ≤ u ≤ 1 Example: Substitute xwindow min into equation and solve for u If u range within [0,1] then line intersects border Use u to find y intersection value
Line Clipping Intersection computations are expensive! A major goal of any line clipping algorithm is to minimise the intersection calculations Cohen-Sutherland clipping algorithm One of the earliest algorithms for fast line clipping Variations of this method are widely used Processing time reduced by performing more tests before resorting to intersection calculations Can be extended to 3D
Cohen-Sutherland Clipping For each endpoint, say (x, y) of a line segment assign a 4- bit region code If (x < xleft), bit 0 = 1 If (x > xlright), bit 1 = 1 If (y < ybottom), bit 2 = 1 If (y > ytop), bit 3 = 1
Cohen-Sutherland Clipping Cohen-Sutherland clipping algorithm Then go through these steps 1. If both endpoints 0000 (i.e. code 1 || code 2 = 0), ACCEPT (|| is bitwise OR) 2. Else If (code 1 && code 2) != 0, REJECT (&& is bitwise AND) 3. Else {Clip line against one viewport boundary 4. Assign the new endpoint with a 4-bit region code 5. Goto 1}
Cohen-Sutherland Clipping Algorithm If both endpoints 0000, ACCEPT Else If (code 1 && code 2) != 0, REJECT Else, { Clip line against one viewport boundary Assign the new endpoint with a 4-bit region code Goto 1}
Cohen-Sutherland Clipping Algorithm If both endpoints 0000, ACCEPT Else If (code 1 && code 2) != 0, REJECT Else, { Clip line against one viewport boundary Assign the new endpoint with a 4-bit region code Goto 1} Line clipped and new endpoints Generated in the following order: A’, B’, A”, then B”
Cohen-Sutherland Clipping in 3D Need 6-bit region codes Clip line segment against planes
Cohen-Sutherland Clipping in 3D Similar algorithm for using 3D outcodes to clip against canonical parallel view volume: xmin = ymin = -1; xmax = ymax = 1; else if LEFT then zmin = -1; zmax = 0; y = y0 + (y1 – y0) * (xmin – x0) / (x1 – x0); z = z0 + (z1 – z0) * (xmin – x0) / (x1 – x0); ComputeOutCode(x0, y0, z0, outcode0); x = xmin; ComputeOutCode(x1, y1, z1, outcode1); else if NEAR then repeat x = x0 + (x1 – x0) * (zmax – z0) / (z1 – z0); check for trivial reject or trivial accept y = y0 + (y1 – y0) * (zmax – z0) / (z1 – z0); pick the point that is outside the clip rectangle z = zmax; if TOP then else if FAR then x = x0 + (x1 – x0) * (ymax – y0) / (y1 – y0); x = x0 + (x1 – x0) * (zmin – z0) / (z1 – z0); z = z0 + (z1 – z0) * (ymax – y0) / (y1 – y0); y = y0 + (y1 – y0) * (zmin – z0) / (z1 – z0); y = ymax; z = zmin; else if BOTTOM then x = x0 + (x1 – x0) * (ymin – y0) / (y1 – y0); if (x0, y0, z0 is the outer point) then z = z0 + (z1 – z0) * (ymin – y0) / (y1 – y0); x0 = x; y0 = y; z0 = z; y = ymin; ComputeOutCode(x0, y0, z0, outcode0) else if RIGHT then else y = y0 + (y1 – y0) * (xmax – x0) / (x1 – x0); x1 = x; y1 = y; z1 = z; z = z0 + (z1 – z0) * (xmax – x0) / (x1 – x0); ComputeOutCode(x1, y1, z1, outcode1) x = xmax; until done
Liang-Barsky Clipping Parametric definition of a line segment with endpoints (x1, y1) and (x2, y2) x = x1 + u ∆x 0 ≤ u ≤ 1 y = y1 + u ∆y ∆x = (x2 + x1) ∆y = (y2 + y1) The goal is to find range of u for which x and y are both inside the viewport
Liang-Barsky Clipping Liang-Barsky clipping algorithm (cont.) Combine parametric equations with the point clipping tests x = x1 + u ∆x y = y1 + u ∆y This gives xleft ≤ x ≤ xright and ybottom ≤ y ≤ ytop xleft ≤ x1 + u ∆x ≤ xright ybottom ≤ y1 + u ∆y ≤ ytop
Liang-Barsky Clipping These four inequalities can be expressed as xleft ≤ x1 + u ∆x ≤ xright ybottom ≤ y1 + u ∆y ≤ ytop u pk ≤ qk , for k = 0, 1, 2, 3 4 inequalities p0 = - ∆x , q0 = x1 + xleft p1 = ∆x , q1 = xright + x1 p2 = - ∆y , q2 = y1 + ybottom p3 = ∆y , q3 = ytop + y1 (from xleft ≤ x1 + u ∆x ) (from x1 + u ∆x ≤ xright ) (from ybottom ≤ y1 + u ∆y ) (from y1 + u ∆y ≤ ytop )
Line Clipping Liang-Barsky clipping algorithm (cont.) Case 1: pk = 0, line is parallel to a corresponding viewpoint boundary Where k = 0, 1, 2, 3, correspond to left, right, bottom and top boundaries For the same value of k If qk < 0, then the line is completely outside Otherwise if qk ≥ 0, then line is inside u pk ≤ qk , for k = 0, 1, 2, 3
Line Clipping Liang-Barsky clipping algorithm (cont.) Case 2: pk ≠ 0, calculate rk = qk / pk for all boundaries (rk is the intersection of the (extended) line with the (extended) boundary) pk < 0 , line starts outside this boundary u1 = max (0, rk) pk > 0 , line starts inside this boundary u2 = min (1, rk) If u1 > u2 , the line is completely outside Else, endpoints calculated from u1 and u2 u pk ≤ qk , for k = 0, 1, 2, 3
Line Clipping Both the Cohen-Sutherland and Liang-Barsky clipping algorithms can be extended to 3D There are a number of other line clipping algorithms E.g. the Nicholl-Lee-Nicholl line clipping algorithm Even faster than the above mentioned methods, however this algorithm only works well for 2D
Area Clipping in 2D Not as simple as line segment clipping Clipping a line segment yields at most one line segment Clipping a polygon can yield multiple polygons However, clipping a convex polygon can yield at most one other polygon
Area Clipping Not as simple as line segment clipping Line clipping Polygon clipping
Area Clipping Sutherland-Hodgman polygon clipping Parse all polygon edges (either in clockwise or counter- clockwise direction) Clip edges against one boundary Repeat for other 3 boundaries 4 possible cases Out In : Save intersection point and endpoint In In : Save endpoint In Out : Save intersection point Out Out : Save nothing [Note: For intersection points, create intermediate vertices (these might be replaced later on)]
Sutherland-Hodgman Polygon Clipping
Sutherland-Hodgman Clipping
Sutherland-Hodgman Polygon Clipping
Sutherland-Hodgman Area Clipping Problem: Algorithm works fine for convex polygons, however extraneous lines may be displayed when clipping concave polygons
Area Clipping Weiler-Atherton polygon clipping More general, can be used to clip either convex or concave area filled polygons Instead of just tracing the polygon’s perimeter (i.e. the previous algorithm) When exit-intersection point encountered take a detour and trace along the clipping boundary Need to remember exit-intersection point in order to continue tracing from here later Edge traversal can be clockwise or counter-clockwise but must maintain same direction
Weiler-Atherton Clipping Process edges until [in out] pair of vertices encountered From exit-intersection point, trace clipping boundary until another intersection point If point was previously processed, goto next step Else (i.e. new intersection point) continue processing edges until a previously processed vertex is encountered Form the vertex list for this section of clipped fill area Return to the exit-intersection point and continue processing the polygon edges
Weiler-Atherton Polygon Clipping
Weiler-Atherton Clipping Can also be used to clip area filled polygons with Hollow areas Against a polygon-shaped clipping window
Thanks.. Questions?