Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons...

28
Polygons UNIT - III

Transcript of Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons...

Page 1: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Polygons UNIT - III

Page 2: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Agenda

Polygon Terminology

Types of polygons

Inside Test

Polygon Filling Algorithms

Scan-Line Polygon Fill Algorithm

Flood-Fill Algorithm

Page 3: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

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

Page 4: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Polygons in graphics

The main geometric object used for interactive

graphics.

Different types of Polygons

Simple Convex

Simple Concave

Non-simple : self-intersecting

Convex Concave Self-intersecting

Page 5: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

5

Polygon Representation and Entering

Page 6: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Polygon Representation

Some graphics packages store polygon as a whole unit.

Some graphics packages provide trapezoid primitive:

means polygons are drawn in the form of trapezoids. And

polygon is considered as a collection of trapezoids.

Sometimes we need to get information of vertices and

polygon is drawn by using series of lines. Here polygon

information is stored in the Display File. The information

is in the form of commands.

Opcode 1 move

2 line

above 3 for no. of vertices of polygon

Page 7: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Inside Test

Why ?

Want to fill in (color) only pixels inside a polygon

What is “inside” of a polygon ?

Methods

1. Even –odd method

2. Winding no. method

Page 8: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Even –odd method

Case :1

Count number of intersections with polygon edges

If N is odd, point is inside

If N is even, point is outside

Case:2

P

P

Q

Q

P Q

P Q

Odd

even

Page 9: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

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

Page 10: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Polygon Filling

10

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

Page 11: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

11

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

Page 12: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

12

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:

Page 13: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

13

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); } }

Page 14: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

14

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

Page 15: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

15

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); } }

Page 16: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Problems with 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.

Page 17: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

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.

Exploit coherence: pixels that are nearby each other tend to share common attributes (color, illumination, normal vectors, texture, etc.).

Span coherence: Pixels in the same scan line tend to be similar.

Scan-line coherence: Pixels in adjacent scan line tend to be similar.

Page 18: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Example

The boundary of a

polygon: (In

practice, a polygon

is defined as a list of

vertices.)

Page 19: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Basic Scan-Fill 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-coordinate.

3. Fill in all pixels between pairs of

intersections.

Page 20: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Edge Coherence

Computing the intersections between scan lines and edges can be costly

Use a method similar to the midpoint 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

Page 21: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

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 A Case B

Page 22: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

22

Spacial Handling (cont.)

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.

Case 1

Page 23: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Spacial Handling (cont.)

However, in this case we count p1 once (p0,p1,p2,p3),

Case 2

Page 24: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

To increase the efficiency of the algorithm special data structures are maintained in Scan line Algorithm.

Active Edge list and Active Edge Table.

Each non-horizontal edge occupies one row/record .

The rows are sorted according to ymin.

Page 25: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Active Edges

Polygon edges are sorted according to their minimum Y.

When the current scan line y value matches the ymin of an edge that edge becomes active.

When the current scan line moves above the upper endpoint, the edge becomes inactive.

Only Active edges are involved in intersection computation.

Page 26: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

Active Edge Table

Active edges are sorted according to increasing X.

Page 27: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill

v1

v2

v3

v4

v5 v6

v7 E1

E2

E3

E4

E5

E6

E7

Edge Ymin Ymax X-coor of vertex with y=ymin

E1 Y1 Y2-1 X1

E7 Y1 Y7 X1

E4 Y5 Y4-1 X5

E6 Y6 Y7 X6

E2 Y2 Y3 X2

E3 Y4 Y3 X4

An Edge List

Page 28: Polygons - computergraphics2015.files.wordpress.com fileAgenda Polygon Terminology Types of polygons Inside Test Polygon Filling Algorithms Scan-Line Polygon Fill Algorithm Flood-Fill