250 likes | 256 Vues
Agenda. Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill Algorithm. A Polygon. Vertex = point in space (2D or 3D) Polygon = ordered list of vertices Each vertex connected with the next in the list
E N D
Agenda • Polygon Terminology • Types of polygons • Inside Test • Polygon Filling Algorithms • Scan-Line Polygon Fill Algorithm • Flood-Fill Algorithm
A Polygon • Vertex = point in space (2D or 3D) • Polygon = ordered list of vertices • Each vertex connected with the next in the list • Last is connected with the first • May contain self-intersections • Simple polygon – no self-intersections • These are of most interest in CG
Polygons in graphics • The main geometric object used for interactive graphics. • Different types of Polygons • Simple Convex • Simple Concave • Non-simple : self-intersecting Self-intersecting Convex Concave
Inside Test • Why ? • Want to fill in (color) only pixels inside a polygon • What is “inside” of a polygon ? • Methods • Even –odd method • Winding no. method
Even –odd method Case :1 Case:2 • Count number of intersections with polygon edges • If N is odd, point is inside • If N is even, point is outside P Q Odd P Q P even Q P Q
Winding no. Method • Every side has given a no. called winding no. • Total of this winding no. is called net winding. • If net winding is zero then point is outside otherwise it is inside. +1 -1
Polygon Filling • Seed Fill Approaches • 2 algorithms: Boundary Fill and Flood Fill • works at the pixel level • suitable for interactive painting apllications • Scanline Fill Approaches • works at the polygon level • better performance
Seed Fill Algorithms: Connectedness • 4-connected region: From a given pixel, the region that you can get to by a series of 4 way moves (N, S, E and W) • 8-connected region: From a given pixel, the region that you can get to by a series of 8 way moves (N, S, E, W, NE, NW, SE, and SW) 4-connected 8-connected
Boundary Fill Algorithm • Boundary-defined region • Start at a point inside a region • Paint the interior outward to the edge • The edge must be specified in a single color • Fill the 4-connected or 8-connected region • 4-connected fill is faster, but can have problems:
Boundary Fill Algorithm (cont.) void BoundaryFill(int x, int y, newcolor, edgecolor) { int current; current = ReadPixel(x, y); if(current != edgecolor && current != newcolor) { putpixel(x,y, newcolor); BoundaryFill(x+1, y, newcolor, edgecolor); BoundaryFill(x-1, y, newcolor, edgecolor); BoundaryFill(x, y+1, newcolor, edgecolor); BoundaryFill(x, y-1, newcolor, edgecolor); } }
Flood Fill Algorithm • Interior-defined region • Used when an area defined with multiple color boundaries • Start at a point inside a region • Replace a specified interior color (old color) with fill color • Fill the 4-connected or 8-connected region until all interior points being replaced
Flood Fill Algorithm (cont.) void FloodFill(int x, int y, newcolor, oldColor) { if(ReadPixel(x, y) == oldColor) { putpixel(x,y, newcolor); FloodFill(x+1, y, newcolor, oldColor); FloodFill(x-1, y, newcolor, oldColor); FloodFill(x, y+1, newcolor, oldColor); FloodFill(x, y-1, newcolor, oldColor); } }
Problems with Seed Fill Algorithm • Recursive seed-fill algorithm may not fill regions correctly if some interior pixels are already displayed in the fill color. • This occurs because the algorithm checks next pixels both for boundary color and for fill color. • Encountering a pixel with the fill color can cause a recursive branch to terminate, leaving other interior pixels unfilled. • This procedure requires considerable stacking of neighboring points, more efficient methods are generally employed.
Scan line polygon Filling Interior Pixel Convention • Pixels that lie in the interior of a polygon belong to that polygon, and can be filled. • Pixels that have centers that fall outside the polygon, are said to be exterior and should not be drawn and filled. Basic Scan Line Algorithm For each scan line: 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.
Basic Scan-Fill Algorithm For scan line number 8 the sorted list of x-coordinates is (2,4,9,13) (b and c are initially no integers) Therefore fill pixels with x- coordinates 2-4 and 9-13.
What happens at edge end-point? Intersection points along the scan lines that intersect polygon vertices. Scan line y’ has odd intersection points and intersects four polygon edges Scan line y has even intersection points and intersects five polygon edges
What happens at edge end-point? • Edge endpoint is duplicated. • In other words, when a scan line intersects an edge endpoint, it intersects two edges. • Two cases: • Case A: edges are on opposite side of the scan line • Case B: edges are on the same side of current scan line • In Case A, we should consider this as only ONE edge intersection • In Case B, we should consider this as TWO edge intersections Scan-line Scan-line Case B Case A
Spacial Handling (cont.) Case 1 Intersection points: (p0, p1, p2) ??? ->(p0,p1,p1,p2) so we can still fill pairwise ->In fact, if we compute the intersection of the scanline with edge e1 and e2 separately, we will get the intersection point p1 twice. Keep both of the p1.
Spacial Handling (cont.) Case 2 However, in this case we count p1 once (p0,p1,p2,p3),
What happens at edge end-point? For Scan line y’ (1,2,2,3) For Scan line y (1,2,3,4)
Coherence • Coherence: pixels that are nearby each other tend to share common attributes (color, illumination, normal vectors, texture, etc.). Types of Coherence • Span coherence: Pixels in the same scan line tend to be similar. • Scan-line coherence: Pixels in adjacent scan line tend to be similar. • Edge coherence means that most of the edges that intersect scan line i also intersect scan line i+1. (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.)
EdgeCoherence Problem: Calculating intersections is slow. Solution: Incremental computation / coherence • Computing the intersections between scan lines and edges can be costly • Use a method similar to the midpoint line drawing algorithm y = mx + b, x = (y – b) / m At y = s, xs = (s – b ) / m At y = s + 1, xs+1 = (s+1 - b) / m = xs + 1 / m Incremental calculation: xs+1 = xs+ 1 / m
Active Edge Table • A table of edges that are currently used to fill the polygon • Scan lines are processed in increasing Y order. • Polygon edges are sorted according to their minimum Y. • When the current scan line reaches the lowerendpoint of an edge itbecomes active. • When the current scan line moves above the upper endpoint,the edge becomes inactive.
Active Edge Table • Active edges are sorted according to increasing X. • Filling in pixels between leftmost edge intersection and stops at the second. • Restarts at the third intersection and stops at the fourth.