University College Dublin1 Clipping u Clipping is the removal of all objects or part of objects in a modelled scene that are outside the real-world window. u Doing this on a pixel-by-pixel basis would be very slow, especially if most objects in the scene are outside the window u More practical techniques are necessary to speed up the task window
University College Dublin2 Line Clipping u Lines are defined by their endpoints, so it should be possible just to examine these, and not every pixel on the line u Most applications of clipping use a window that is either very large – i.e. nearly the whole scene fits inside, or very small – i.e. most of the scene lies inside the window u Hence, most lines may be either trivially accepted or rejected window
University College Dublin3 Cohen-Sutherhland u The Cohen-Sutherland line-clipping algorithm is particularly fast for “trivial” cases – i.e. lines completely inside or outside the window u Non-trivial lines such as those that cross a boundary of the window, are clipped by computing the coordinates of the new boundary endpoint of the line where it crosses the edge of the window window
University College Dublin4 Computing intersection points (1 st case) x min y max y I y min x max AB Q P I xIxI = y I - y P x I - x P y Q - y P x Q - x P As the triangles PAI and PBQ are similar we can write xQxQ yPyP yQyQ xPxP
University College Dublin5 Computing intersection points u We have After replacing y I with y max and multiplying both sides of this equation by y max – y P, we get = y I - y P x I - x P y Q - y P x Q - x P = y Q - y P (y max - y P )(x Q - x P ) xPxP + xIxI = y max yIyI
University College Dublin6 Computing intersection points u If segment PQ intersects one of the other sides of the rectangle, coordinates are calculated with similar considerations y max x min y min x max Q P I P’P’ Q’ I’I’ P’’ Q’’ I’’
University College Dublin7 y max Computing intersections: 2 nd case x min y min x max A B Q P I xIxI = x I - x P y I - y P x Q - x P y Q - y P As the triangles PAQ and PBI are similar we can write xQxQ yPyP yQyQ xPxP yIyI
University College Dublin8 Computing intersection points u We have After replacing x I with x min and multiplying both sides of this equation by x min – x P, we get = x Q - x P (x min - x P )(y Q - y P ) yPyP + yIyI = x min xIxI = x I - x P y I - y P x Q - x P y Q - y P
University College Dublin9 Cohen Sutherland cont. u Having decided that a line is non-trivial, it must be clipped u We “push” each end-point of the line to its “nearest” window boundary
University College Dublin10 Cohen-Sutherhland u The window edges are assumed to be extended so that the whole picture is divided into 9 regions u Each region is assigned a four-bit code, called an outcode Left Right Below Above
University College Dublin11 Example 1. Since P lies to the left of the left rectangle edge, it is replaced with P` 2. Since P` lies below the lower rectangle edge, it is replaced with P`` 3. Since Q lies to the right of the rectangle edge, it is replaced with Q` 4. Line segment P`` Q` (the clipped segment) can now be drawn 1010 Q P` P`` Q` P Y min Y max X max X min
University College Dublin12 Example u Steps 1, 2, 3 loop terminated as follows: u If the four bit code of P and Q are equal to zero –Draw line u If the two four bit codes contain a 1 in the same bit position –P and Q are on the same side of rectangle nothing to be drawn 1010 Q P` P`` Q` P Y min Y max X max X min
University College Dublin13 Sample Java Code u Method for generating outcode for each end of the line is computed, giving codes cP and cQ int clipCode(float x, float y) { return ((x xmax ? 4 : 0) | (y ymax ? 1 : 0)); } Note: The expression: condition ? value1 : value2 evaluates value1 if condition is true value2 if condition is false
University College Dublin14 Sample Java Code u Method for generating outcode for each end of the line is computed, giving codes cP and cQ int clipCode(float x, float y) { return ((x xmax ? 4 : 0) | (y ymax ? 1 : 0)); } 1000 = = = = x < xmin Left x > xmax Right y < ymin Below y > ymax Above
University College Dublin15 Sample Java Code u The expression ((x xmax ? 4 : 0) | (y ymax ? 1 : 0)); Is an OR of boolean values (bit by bit) Example: 1000 | 0110 = st and 2 nd bits are mutually exclusive (cannot be both 1 but they can be both 0) Same for 3 rd and 4 th bits x < xmin Left x > xmax Right y < ymin Below y > ymax Above
University College Dublin16 Sample Java Code u The expression ((x xmax ? 4 : 0) | (y ymax ? 1 : 0)); Example: point P 0000 | 0100 | 0010 | 0000 == x < xmin Left x > xmax Right y < ymin Below y > ymax Above P
University College Dublin17 void clipLine (Graphics g, float xP, float yP, float xQ, float yQ, float xmin, float ymin, float xmax, float ymax) { int cP = clipCode(xP, yP); // Generate clip code for point P int cQ = clipCode(xQ, yQ); // Generate clip code for point Q float dx, dy; while ((cP | cQ) != 0) { if ((cP & cQ) != 0) return; // Both points are outside area so ignore dx = xQ - xP; dy = yQ - yP;
University College Dublin18 Trivial cases 1010 Y min Y max X max X min CP&CQ==4 (0100) CP&CQ==1 (0001) CP&CQ==2 (0010) CP&CQ==8 (1000)
University College Dublin19 More trivial cases 1010 Y min Y max X max X min CP&CQ==0 However PQ does not intersect the window: must run a few steps
University College Dublin20 More trivial cases (cont.d) 1010 Y min Y max X max X min Now: CP’&CQ’==1 P’ Q’
University College Dublin21 if (cP != 0) { if ((cP & 8) == 8) /* i.e. cP & 1000 == 1000 (x<xmin) */ { yP += (xmin-xP) * dy / dx; xP = xmin; } else if ((cP & 4) == 4) { yP += (xmax-xP) * dy / dx; xP = xmax; } else if ((cP & 2) == 2) { xP += (ymin-yP) * dx / dy; yP = ymin; } else if ((cP & 1) == 1) { xP += (ymax-yP) * dx / dy; yP = ymax; } cP = clipCode(xP, yP) } // end outer if
University College Dublin22 else if (cQ != 0) { if ((cQ & 8) == 8) { yQ += (xmin-xQ) * dy / dx; xQ = xmin; } else if ((cQ & 4) == 4) { yQ += (xmax-xQ) * dy / dx; xQ = xmax; } else if ((cQ & 2) == 2) { xQ += (ymin-yQ) * dx / dy; yQ = ymin; } else if ((cQ & 1) == 1) { xQ += (ymax-yQ) * dx / dy; yQ = ymax;} cQ = clipCode(xQ, yQ); } // end else if } //end while drawLine(g, xP, yP, xQ, yQ); } // end method
University College Dublin23 Other Clipping algorithms u The Cohen-Sutherland algorithm requires the window to be a rectangle, with edges aligned with the co-ordinate axes u It is sometimes necessary to clip to any convex polygonal window, e.g. triangular, hexagonal, or rotated. u The Cyrus-Beck, and Liang-Barsky line clippers use the concept of inside/outside half spaces generated by edges (polygons are seen as intersection of half (2D) spaces (planes)
University College Dublin24 Polygon Clipping u A polygon is usually defined by a sequence of vertices and edges u If the polygons are un-filled, line-clipping techniques are sufficient
University College Dublin25 Polygon Clipping u However, if the polygons are filled, the process in more complicated u A polygon may be fragmented into several polygons in the clipping process, and the original colour associated with each one
University College Dublin26 Polygon Clipping Algorithms u The Sutherland-Hodgeman clipping algorithm clips any polygon against a convex clip polygon. u The Weiler-Atherton clipping algorithm will clip any polygon against any clip polygon. The polygons may even have holes