Polygon Filling
Post on 31-Dec-2015
58 Views
Preview:
DESCRIPTION
Transcript
Polygon Filling
09/06/13 09/07/13 1Prepared by Narendra V G CSE MIT
Common Types of Polygon
• Concave• Convex
• Triangles
• Trapezoids
• Quadrilaterals
• Self-intersecting• Multiple loops• Holes
09/06/13 09/07/13 2Prepared by Narendra V G CSE MIT
Common Types of Polygon
• Concave• Convex
• Triangles
• Trapezoids
• Quadrilaterals
• Self-intersecting• Multiple loops• Holes
Concave
Convex
Hole
09/06/13 09/07/13 3Prepared by Narendra V G CSE MIT
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)
ConcaveConvex
09/06/13 09/07/13 4Prepared by Narendra V G CSE MIT
� 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
Polygon Fill Areas
09/06/13 09/07/13 5Prepared by Narendra V G CSE MIT
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 6Prepared by Narendra V G CSE MIT
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 7Prepared by Narendra V G CSE MIT
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 8Prepared by Narendra V G CSE MIT
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 9Prepared by Narendra V G CSE MIT
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.
Illustration of the Ideas
scan lines
spanspan
span
span
09/06/13 09/07/13 10Prepared by Narendra V G CSE MIT
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 11Prepared by Narendra V G CSE MIT
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
oddodd
odd
odd
even
even
even
09/06/13 09/07/13 12Prepared by Narendra V G CSE MIT
Implementation of the Ideas
: intersection points between scan lines and edges (span extrema).
: interior pixels
09/06/13 09/07/13 13Prepared by Narendra V G CSE MIT
Implementation of the Ideas (cont.)
Filled pixels using the strictly inside principle.
09/06/13 09/07/13 14Prepared by Narendra V G CSE MIT
Q1: Given an intersection with an arbitrary, fractional x value, how do we determine which pixel on either side of that intersection is interior?
Four Elaborations to the 2nd Question
A: The strictly
inside rule:
09/06/13 09/07/13 15Prepared by Narendra V G CSE MIT
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.
Four Elaborations (cont.)
09/06/13 09/07/13 16Prepared by Narendra V G CSE MIT
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.
Four Elaborations (cont.)
A
ymin
ymax
B
C
A
ymin ymin
B
C
09/06/13 09/07/13 17Prepared by Narendra V G CSE MIT
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.
Four Elaborations (cont.)
Bottom edges are drawn Top edges are not
09/06/13 09/07/13 18Prepared by Narendra V G CSE MIT
Four Elaborations (cont.)
A B
C D
E
FG
HI
J
E
A B
C D
FG
HI
J
09/06/13 09/07/13 19Prepared by Narendra V G CSE MIT
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 20Prepared by Narendra V G CSE MIT
• 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
Catching Edge/Scan line intersections
09/06/13 09/07/13 21Prepared by Narendra V G CSE MIT
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.
oddodd
even evena b c d
A
B
C
D
E
F
09/06/13 09/07/13 22Prepared by Narendra V G CSE MIT
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.
An Example for the Global Edge Table
11 13 0 CD
9 2 0 FA
3 2 -5/2 AB
5 7 6/4 BC
9 2 -5/2 EF
11 7 6/4 DE
11
10
9
8
7
6
5
4
3
2
1
0
y co
ordi
nate
ym
ax
xm
in
1/m
09/06/13 09/07/13 23Prepared by Narendra V G CSE MIT
An Example of the Active Edge Table
AB
C
F
E
D
2
5
4
611 10
11 13 0
09 2
9 2
AET pointer
FA
EF
DE
CDAs 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.
Ymax x m
1
09/06/13 09/07/13 24Prepared by Narendra V G CSE MIT
• 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 ymax coordinate of the edge, the x coordinate of the intersection point, and 1/m.
ET = Global Edge Table
Scan-Line Algorithm
09/06/13 09/07/13 25Prepared by Narendra V G CSE MIT
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.
Edge Coherence
09/06/13 09/07/13 26Prepared by Narendra V G CSE MIT
1/m = 2/5
xmin = 3,
the sequence is:
numerator:
denominator:
Edge Coherence (cont.)
2
5
35
16
m
,...5
14
5
63,
5
43,
5
23
5
23
5
43
5
14
2
5
4 1
09/06/13 09/07/13 27Prepared by Narendra V G CSE MIT
1. Set y to the smallest y coordinate that has an entry in the ET, that is, y for the first nonempty bucket.
2. Initialise the AET to be empty.
3. 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.
Scan-Line Algorithm (cont.)
09/06/13 09/07/13 28Prepared by Narendra V G CSE MIT
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 29Prepared by Narendra V G CSE MIT
09/06/13 09/07/13 30Prepared by Narendra V G CSE MIT
09/06/13 09/07/13 31Prepared by Narendra V G CSE MIT
Boundary-Fill Algorithm
09/06/13 09/07/13 32Prepared by Narendra V G CSE MIT
09/06/13 09/07/13 33Prepared by Narendra V G CSE MIT
Polygon Filling:Boundary-Fill Algorithm
1
2
21
09/06/13 09/07/13 34Prepared by Narendra V G CSE MIT
Polygon Filling:Boundary-Fill Algorithm
1
3
31
09/06/13 09/07/13 35Prepared by Narendra V G CSE MIT
Polygon Filling:Boundary-Fill Algorithm
14
6541
5 6
09/06/13 09/07/13 36Prepared by Narendra V G CSE MIT
Polygon Filling:Boundary-Fill Algorithm
14 5
41
5
09/06/13 09/07/13 37Prepared by Narendra V G CSE MIT
Polygon Filling:Boundary-Fill Algorithm
1 41
4
09/06/13 09/07/13 38Prepared by Narendra V G CSE MIT
Polygon Filling:Boundary-Fill Algorithm
1 71
7
09/06/13 09/07/13 39Prepared by Narendra V G CSE MIT
Polygon Filling:Boundary-Fill Algorithm
1 81
8
09/06/13 09/07/13 40Prepared by Narendra V G CSE MIT
Polygon Filling:Boundary-Fill Algorithm
1 919
09/06/13 09/07/13 41Prepared by Narendra V G CSE MIT
Polygon Filling:Boundary-Fill Algorithm
1
1
09/06/13 09/07/13 42Prepared by Narendra V G CSE MIT
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 43Prepared by Narendra V G CSE MIT
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 44Prepared by Narendra V G CSE
MIT
09/06/13 09/07/13 45Prepared by Narendra V G CSE MIT
09/06/13 09/07/13 46Prepared by Narendra V G CSE MIT
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 47Prepared by Narendra V G CSE
MIT
09/06/13 09/07/13 48Prepared by Narendra V G CSE MIT
09/06/13 09/07/13 49Prepared by Narendra V G CSE MIT
09/06/13 09/07/13 50Prepared by Narendra V G CSE MIT
09/06/13 09/07/13 51Prepared by Narendra V G CSE MIT
Flood
-Fill A
lgorith
mB
oun
dary
-Fill A
lgorith
m
09/06/13 09/07/13 52Prepared by Narendra V G CSE MIT
09/06/13 09/07/13 53Prepared by Narendra V G CSE MIT
Example
• Given a sequence of ordered vertices (10,10) (10,16) (16,20) (28,10) (28,16) (22,10)
(10,10)
(10,16)
(16,20)
(22,10) (28,10)
(28,16)
Edge 1
Edge 2
Edge 3
Edge 4
Edge 5
Edge 6
09/06/13 09/07/13 54Prepared by Narendra V G CSE MIT
Edge Table vs Global Edge Table (GET)
09/06/13 09/07/13 55Prepared by Narendra V G CSE MIT
Global Edge Table vs Active Edge Table
09/06/13 09/07/13 56Prepared by Narendra V G CSE MIT
Filling the polygon
Ymin = 10
Result
update x = x +1/m
Sorted by x-max
09/06/13 09/07/13 57Prepared by Narendra V G CSE MIT
Filling the Polygon – 2nd scanline to scanline 15
Y-max reaches the scanline
09/06/13 09/07/13 58Prepared by Narendra V G CSE MIT
Filling the polygon -- cont
Combine
reorder
Final Result
09/06/13 09/07/13 59Prepared by Narendra V G CSE MIT
Examples:
09/06/13 09/07/13 60Prepared by Narendra V G CSE MIT
Solutions:
09/06/13 09/07/13 61Prepared by Narendra V G CSE MIT
top related