1 / 26

Clipping

Clipping. Clipping is the removal of all objects or part of objects in a modelled scene that are outside the real-world window. Doing this on a pixel-by-pixel basis would be very slow, especially if most objects in the scene are outside the window

astro
Download Presentation

Clipping

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Clipping • Clipping is the removal of all objects or part of objects in a modelled scene that are outside the real-world window. • Doing this on a pixel-by-pixel basis would be very slow, especially if most objects in the scene are outside the window • More practical techniques are necessary to speed up the task window

  2. window Line Clipping • Lines are defined by their endpoints, so it should be possible just to examine these, and not every pixel on the line • 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 • Hence, most lines may be either trivially accepted or rejected

  3. window Cohen-Sutherhland • The Cohen-Sutherland line-clipping algorithm is particularly fast for “trivial” cases • i.e. lines completely inside or outside the window • 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

  4. xI - xP xQ - xP = yI - yP yQ - yP Computing intersection points (1st case) Q yQ I ymax yI yP P A B ymin xmin xP xI xQ xmax As the triangles PAI and PBQ are similar we can write

  5. xI - xP xQ - xP = yI - yP yQ - yP (ymax - yP)(xQ - xP) xI xP = + yQ - yP yI ymax = Computing intersection points • We have • After replacing yI with ymax and multiplying both sides of this equation by ymax – yP, we get

  6. Computing intersection points • If segment PQ intersects one of the other sides of the rectangle, coordinates are calculated with similar considerations Q’’ ymax Q I’’ I Q’ P P’’ ymin I’ P’ xmin xmax

  7. yI - yP yQ - yP = xI - xP xQ - xP Computing intersections: 2nd case ymax Q yQ yI I P yP A B ymin xP xmin xI xQ xmax As the triangles PAQ and PBI are similar we can write

  8. (xmin - xP)(yQ - yP) yI yP = + xQ - xP xI xmin = yI - yP yQ - yP = xI - xP xQ - xP Computing intersection points • We have • After replacing xI with xmin and multiplying both sides of this equation by xmin – xP, we get

  9. Cohen Sutherland cont. • Having decided that a line is non-trivial, it must be clipped • We “push” each end-point of the line to its “nearest” window boundary 1 2 3 4

  10. 1001 0001 0101 1000 0000 0100 1010 0010 0110 Cohen-Sutherhland • The window edges are assumed to be extended so that the whole picture is divided into 9 regions • Each region is assigned a four-bit code, called an outcode Left Right Below Above

  11. Xmin Xmax 1001 0001 0101 Q Ymax Q` 1000 0000 0100 Ymin P`` P` 1010 0010 0110 P Example • Since P lies to the left of the left rectangle edge, it is replaced with P` • Since P` lies below the lower rectangle edge, it is replaced with P`` • Since Q lies to the right of the rectangle edge, it is replaced with Q` • Line segment P`` Q` (the clipped segment) can now be drawn

  12. Example • Steps 1, 2, 3 loop terminated as follows: • If the four bit code of P and Q are equal to zero • Draw line • 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 Xmin Xmax 1001 0001 0101 Q Ymax Q` 1000 0000 0100 Ymin P`` P` 1010 0010 0110 P

  13. Sample Java Code • 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 < xmin ? 8 : 0) | (x > xmax ? 4 : 0) | (y < ymin ? 2 : 0) | (y > ymax ? 1 : 0)); } Note: The expression: condition ? value1 : value2 evaluates value1 if condition is true value2 if condition is false

  14. 1001 0001 0101 1000 0000 0100 1010 0010 0110 Sample Java Code • 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 < xmin ? 8 : 0) | (x > xmax ? 4 : 0) | (y < ymin ? 2 : 0) | (y > ymax ? 1 : 0)); } 1000 = 8 0100 = 4 0010 = 2 0001 = 1 x < xminLeft x > xmaxRight y < yminBelow y > ymaxAbove

  15. 1001 0001 0101 1000 0000 0100 1010 0010 0110 Sample Java Code • The expression ((x < xmin ? 8 : 0) | (x > xmax ? 4 : 0) | (y < ymin ? 2 : 0) | (y > ymax ? 1 : 0)); Is an OR of boolean values (bit by bit) Example: 1000 | 0110 = 1110 1st and 2nd bits are mutually exclusive (cannot be both 1 but they can be both 0) Same for 3rd and 4th bits x < xminLeft x > xmaxRight y < yminBelow y > ymaxAbove

  16. 1001 0001 0101 1000 0000 0100 1010 0010 0110 P x < xminLeft x > xmaxRight y < yminBelow y > ymaxAbove Sample Java Code • The expression ((x < xmin ? 8 : 0) | (x > xmax ? 4 : 0) | (y < ymin ? 2 : 0) | (y > ymax ? 1 : 0)); Example: point P 0000 | 0100 | 0010 | 0000 == 0110

  17. 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 forpoint 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;

  18. Trivial cases Xmin Xmax CP&CQ==4 (0100) CP&CQ==1 (0001) 1001 0001 0101 Ymax 1000 0000 0100 Ymin 1010 0010 0110 CP&CQ==8 (1000) CP&CQ==2 (0010)

  19. More trivial cases Xmin Xmax 1001 0001 0101 Ymax 1000 0000 0100 Ymin 1010 0010 0110 CP&CQ==0 However PQ does not intersect the window: must run a few steps

  20. More trivial cases (cont.d) Xmin Xmax 1001 0001 0101 Q’ Ymax Now: CP’&CQ’==1 1000 0000 0100 Ymin P’ 1010 0010 0110

  21. 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

  22. elseif (cQ != 0) { if ((cQ & 8) == 8) { yQ += (xmin-xQ) * dy / dx; xQ = xmin; } elseif ((cQ & 4) == 4) { yQ += (xmax-xQ) * dy / dx; xQ = xmax; } elseif ((cQ & 2) == 2) { xQ += (ymin-yQ) * dx / dy; yQ = ymin; } elseif ((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

  23. Other Clipping algorithms • The Cohen-Sutherland algorithm requires the window to be a rectangle, with edges aligned with the co-ordinate axes • It is sometimes necessary to clip to any convex polygonal window, e.g. triangular, hexagonal, or rotated. • 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)

  24. Polygon Clipping • A polygon is usually defined by a sequence of vertices and edges • If the polygons are un-filled, line-clipping techniques are sufficient

  25. Polygon Clipping • However, if the polygons are filled, the process in more complicated • A polygon may be fragmented into several polygons in the clipping process, and the original colour associated with each one

  26. Polygon Clipping Algorithms • The Sutherland-Hodgeman clipping algorithm clips any polygon against a convex clip polygon. • The Weiler-Atherton clipping algorithm will clip any polygon against any clip polygon. The polygons may even have holes

More Related