1 / 61

Polygon Filling

Polygon Filling. 09/06/13 09/07/13. Prepared by Narendra V G CSE MIT. 1. Common Types of Polygon. Concave Convex • Triangles • Trapezoids • Quadrilaterals • Self-intersecting Multiple loops Holes. 09/06/13 09/07/13. Prepared by Narendra V G CSE MIT. 2. Convex. Hole.

Download Presentation

Polygon Filling

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. Polygon Filling 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 1

  2. Common Types of Polygon • Concave • Convex • Triangles • Trapezoids • Quadrilaterals • Self-intersecting • Multiple loops • Holes 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 2

  3. Convex Hole Common Types of Polygon • Concave • Convex • Triangles • Trapezoids • Quadrilaterals • Self-intersecting • Multiple loops • Holes Concave 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 3

  4. Convexity/Concavity Definition • A polygon is convex if: for all edges, all other vertices lie on the same side of the edge • Otherwise it is concave. • Concave polygons often difficult to process (algorithms get stuck or miss bits) Concave Convex 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 4

  5. Polygon Fill Areas Polygon Classifications convex: no interior angle > 180° concave: not convex Splitting Concave Polygons Vector Method All vector cross products have the same sign => convex Rotational Method Rotate polygon-edges onto x-axis, always same direction => convex 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 5

  6. Fill Algorithms • Given the edges defining a polygon, and a color for the polygon, we need to fill all the pixels inside the polygon. • Three different algorithms: • 1. Scan-line fill • 2. Boundary fill • 3. Flood fill 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 6

  7. Some Problems 1. Which pixels should be filled in? 2. Which happened to the top pixels? To the rightmost pixels? 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 7

  8. Some Remarks to the 2nd problem Why is the 2nd problem such a big deal? What would happen if we fill the top and right most pixels? Because this will cause “double-fill” when two rectangles are adjacent. “Double-filling” brings the following two disadvantages: 1). Inefficient 2). If polygons have different colour, then final colour depends on the order in which the polygons are drawn, and may lead to “flicker”. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 8

  9. General Ideas about Polygon Filling • Rules for shared edges: • A shared vertical edge belongs to the rightmost of the two sharing shapes. (Therefore, the right edge of rectangles will not be filled). • A shared non-vertical edge belongs to the upper shape. (Therefore, the top edge of rectangles will not be filled). • Fill in polygons by computing intersections of boundaries with scan lines, and filling between pairs of intersections • This is the actual algorithm! 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 9

  10. scan lines span span span span Illustration of the Ideas A span is the collection of adjacent pixels on a single scan line which lie inside the primitive. Coherence literally means to be logically consistent or connected.Spatial coherence means that primitives don't change an awful lot if at all from pixel to pixel within a scan line or from scan line to scan line. This allows us to optimise our algorithms. Edge coherence means that most of the edges that intersect scan line i also intersect scan line i+1. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 10

  11. Filling the Spans Span-filling is an important step in the whole polygon-filling algorithm, and i is implemented by a three-step process: 1. Find the intersections of the scan line with all edges of the polygon. 2. Sort the intersections by increasing x coordinates. 3. Fill in all pixels between pairs of intersections that lie interior to the polygon. Now more questions arise: • How do we find and sort the intersections efficiently? • How do we judge whether a pixel lying inside or outside the polygon? 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 11

  12. Parity (Odd-Even) Rule Begin from a point outside the polygon, increasing the x value, counting the number of edges crossed so far, a pixel is inside the polygon if the number of edges crossed so far (parity) is odd, and outside if the number of edges crossed so far (parity) is even. This is known as the parity, or the odd-even, rule. It works for any kind of polygons. Parity starting from even even even odd odd odd even odd 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 12

  13. : intersection points between scan lines and edges (span extrema). : interior pixels Implementation of the Ideas 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 13

  14. Implementation of the Ideas (cont.) Filled pixels using the strictly inside principle. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 14

  15. Four Elaborations to the 2nd Question Q1: Given an intersection with an arbitrary, fractional x value, how do we determine which pixel on either side of that intersection is interior? A: The strictly inside rule: 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 15

  16. Four Elaborations (cont.) Q2: How do we deal with the special case of intersections at integer pixel coordinates? A: Use the criterion we used for avoid conflicting between shared edges: if the leftmost pixel in a span has integer x coordinate, we define it to be interior; if the rightmost pixel has integer x coordinate, we define it to be exterior. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 16

  17. A A C ymin ymin ymin B B ymax C Four Elaborations (cont.) Q3: How do we deal with the special case for shared vertices? A: We count the ymin vertex of an edge in the parity calculation but not the ymax vertex. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 17

  18. Four Elaborations (cont.) Q4: How do we deal with the special case in which the vertices define a horizontal edge? A: Bottom edges are drawn but top edges are not. Bottom edges are drawn Top edges are not 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 18

  19. G F G F H H I I E D C D C J J B A B A Four Elaborations (cont.) E 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 19

  20. Four Elaborations (cont.) We deal properly with the horizontal edges by not counting their vertices. For the figure in the last slide, consider bottom edge AB. Vertex A is a ymin vertex for edge JA, and AB does not contribute. Therefore, the parity is odd and the span AB is drawn. Vertical edge BC has its ymin at B, but again AB does not contribute. The parity becomes even, and the span is terminated. At vertex J, edge IJ has a ymin vertex but edge JA does not, so the parity becomes odd and the span is drawn to edge BC. The span that starts at edge IJ and hits C sees no change at C because C is a ymax vertex for BC, so the span continues along bottom edge CD; at D, however, edge DE has a ymin vertex, so the parity is reset to even and the span ends. At I, edge IJ has its ymax vertex and edge HI also does not contribute, so parity stays even and the top edge IH is not drawn. At H, however, edge GH has a ymin vertex, the parity becomes odd, and the span is drawn from H to the pixel to the left of the intersection with edge EF. Finally, there is no ymin vertex at G, nor is there one at F, so top edge FG is not drawn. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 20

  21. Catching Edge/Scan line intersections • Catch the intersection information in a table • Edge Table (ET) with all edges sorted by ymin • Active Edge Table (AET) containing • The edges that intersect the current scan line • Their points of intersection • Sorted by x-coordinate, left to right 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 21

  22. D F even b even c d a odd odd E C A B Example Let’s apply the rules to scan line 8 below. We fill in the pixels from point a, pixel (2, 8), to the first pixel to the left of point b, pixel (4, 8), and from the first pixel to the right of point c, pixel (9, 8), to one pixel to the left of point d, pixel (12, 8). For scan line 3, vertex A counts once because it is the ymin vertex of edge FA, but the ymax vertex of edge AB; this causes odd parity, so we draw the span from there to one pixel to the left of the intersection with edge CB. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 22

  23. 11  10  11 7 6/4  DE 9  y coordinate 8  9 2 -5/2 EF 7 6  11 13 0  CD 5 4  9 2 0  FA 3 2  3 2 -5/2 AB 1 0  5 7 6/4  BC ymax xmin 1/m An Example for the Global Edge Table Global edge table (ET): make the addition of edges to the AET efficient. It contains all edges sorted by their smaller y coordinate. The ET is typically built by using a bucket sort with as many buckets as there are scan-lines. Within each bucket, edges are kept in order of increasing x coordinate of the lower endpoint. Each entry in the ET contains the ymax coordinate of the edge, the x coordinate of the bottom endpoint (xmin), and 1/m. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 23

  24. D F E C A B An Example of the Active Edge Table AET pointer FA EF DE As each new scan-line y+1 is encounter, update AET: 1). Remove edges not intersected by y+1 (ymax=y) 2). Add edges intersected by y+1 (ymin=y+1) 3). Calculate new x intersections. CD Ymaxx 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 24

  25. Scan-Line Algorithm • Two sub-problems for polygon filling • Find and sort intersections • Fill the spans. AET = active-edge table store all edges intersected by a scan-line y sorted by x intersection. Each entry in AET contains the ymaxcoordinate of the edge, the x coordinate of the intersection point, and 1/m. ET = Global Edge Table 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 25

  26. Edge Coherence In order to calculate intersections between scan lines and edges, we must avoid the brute-force technique of testing each polygon edge for intersection with each new scan line – it is inefficient and slow. Clever Solution: if an edge intersects with a scan line, and the slope of the edge is m, then successive scan line intersections can be found from: xi+1 = xi + 1/m where i is the scan line count. Given that 1/m = (x1 – x0)/(y1 – y0) the floating-point arithmetic can be avoided by storing the numerator, comparing to the denominator, and incrementing x when it overflows. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 26

  27. Edge Coherence (cont.) 1/m = 2/5 xmin = 3, the sequence is: numerator: denominator: 2 4 1 5 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 27

  28. Scan-Line Algorithm (cont.) • Set y to the smallest y coordinate that has an entry in the ET, that is, y for the first nonempty bucket. • Initialise the AET to be empty. • Repeat until the AET and ET are empty. 3.1 Move from ET bucket y to the AET those edges whose ymin= y (entering edges). 3.2 Remove from the AET those entries for which y=ymax (edges not involved in the next scan line), then sort the AET on x (made easier because ET is pre-sorted). 3.3 Fill in desired pixel values on scan line y by using pairs of x coordinates from the AET. 3.4 Increment y by 1 (to the coordinate of the next scan line). 3.5 For each non-vertical edge remaining in the AET, update x for the new y. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 28

  29. Scan Line Algorithm • Advantages: • Simple • Potentially fewer quantization errors (more bits available for depth) • Don’t over-render (each pixel only drawn once) • Filter anti-aliasing can be made to work (have information about all polygons at each pixel) • Disadvantages: • Invisible polygons clog AET, ET • Non-intersection criteria may be hard to meet 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 29

  30. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 30

  31. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 31

  32. Boundary-Fill Algorithm 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 32

  33. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 33

  34. Polygon Filling:Boundary-Fill Algorithm 2 2 1 1 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 34

  35. Polygon Filling:Boundary-Fill Algorithm 3 3 1 1 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 35

  36. Polygon Filling:Boundary-Fill Algorithm 5 6 6 5 4 1 4 1 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 36

  37. Polygon Filling:Boundary-Fill Algorithm 5 5 4 1 4 1 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 37

  38. Polygon Filling:Boundary-Fill Algorithm 4 1 4 1 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 38

  39. Polygon Filling:Boundary-Fill Algorithm 7 1 7 1 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 39

  40. Polygon Filling:Boundary-Fill Algorithm 8 1 1 8 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 40

  41. Polygon Filling:Boundary-Fill Algorithm 9 1 1 9 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 41

  42. Polygon Filling:Boundary-Fill Algorithm 1 1 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 42

  43. Boundary fill across pixel spans for a 4-connected area. (a) The filled initial pixel span, showing the position of the initial point (open circle) and the stacked positions for pixel spans on adjacent scan lines. (b) Filled pixel span on the first scan line above the initial scan Line and the current contents of the stack. (c) Filled pixel spans on the first two scan lines above the initial scan line and the current contents of the stack. (d) Completed pixel spans for the upper-right portion of the defined region and the remaining stacked positions to be processed. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 43

  44. Boundary Fill • Suppose that the edges of the polygon has already been colored. • Suppose that the interior of the polygon is to be colored a different color from the edge. • Suppose we start with a pixel inside the polygon, then we color that pixel and all surrounding pixels until we meet a pixel that is already colored. void boundaryFill(int x, int y, int fillColor, int borderColor) { int interiorColor; getPixel(x,y,interiorColor); if ((interiorColor!=borderColor)&&(interiorColor!=fillColor)) { setPixel(x,y,fillColor); boundaryFill(x+1,y,fillColor,borderColor); boundaryFill(x-1,y,fillColor,borderColor); boundaryFill(x,y+1,fillColor,borderColor); boundaryFill(x,y-1,fillColor,borderColor); } } 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 44

  45. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 45

  46. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 46

  47. Flood Fill • Suppose we want to color the entire area whose original color is interiorColor, and replace it with fillColor. • Then, we start with a point in this area, and then color all surrounding points until we see a pixel that is not interiorColor. void floodFill(int x, int y, int fillColor, int interiorColor) { int color; getPixel(x,y,color) if (color==interiorColor) { setPixel(x,y,fillColor); floodFill(x+1,y,fillColor,interiorColor); floodFill(x-1,y,fillColor,interiorColor); floodFill(x,y+1,fillColor,interiorColor); floodFill(x,y-1,fillColor,interiorColor); } } 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 47

  48. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 48

  49. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 49

  50. 09/06/13 09/07/13 Prepared by Narendra V G CSE MIT 50

More Related