CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon...

18
8/23/2017 1 CSC 210 1.0 Computer Graphics [email protected] Department of Computer Science University of Sri Jayewardanepura Lecture 02 Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside Test Odd-Even Rule Nonzero Winding Number Rule Marching Cubes 8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 2 So we can figure out how to draw lines and circles How do we go about drawing polygons? We use an incremental algorithm known as the scan-line algorithm 8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 4 2 4 6 8 10 Scan Line 0 2 4 6 8 10 12 14 16 8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 5 2 4 6 8 10 12 14 0 2 4 6 8 10 12 14 0

Transcript of CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon...

Page 1: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

1

CSC 210 1.0

Computer Graphics

[email protected]

Department of Computer Science

University of Sri Jayewardanepura

1

Lecture 02

Polygon Filling

Scan-Line Polygon Fill Algorithm

Boundary Fill Algorithm

▪ Span Flood-Fill Algorithm

Inside-outside Test

▪ Odd-Even Rule

▪ Nonzero Winding Number Rule

Marching Cubes

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 2

So we can figure out how to draw lines and circlesHow do we go about drawing polygons?We use an incremental algorithm known as the scan-line algorithm

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 4

2

4

6

8

10 Scan Line

02 4 6 8 10 12 14 16

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 5

2 4 6 8 10 12 140

2

4

6

8

10

12

14

0

Page 2: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

2

2 4 6 8 10 12 140

2

4

6

8

10

12

14

0

Edge Table (ET) Entry

maxyminx

slope

1

2 4 6 8 10 12 140

2

4

6

8

10

12

14

0

AB

C

D

E

F

0

1

2

3

4

5

6

7

8

9

10

11

3 7 5 7 6/4

9 2 0

12 13 0

9 7 -5/2 12 7 6/4

-5/2

N

N

N

N

N

N

N

N

N

N

N

N

Scan Line

AB BC

FA

CD

EF DE

10

The Scan-Line Polygon Fill Algorithm

(Example) Polygon = {A, B, C, D, E, F, G}

Polygon = {(2, 7), (4, 12), (8,15), (16, 9), (11, 5), (8, 7), (5, 5)}

11

The Scan-Line Polygon Fill Algorithm

(Example)

12

The Scan-Line Polygon Fill Algorithm

Dealing with vertices

Page 3: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

3

The basic scan-line algorithm is as follows:

Find the intersections of the scan line with all edges of the polygon

Sort the intersections by increasing x coordinate

Fill in all pixels between pairs of intersections that lie interior to the polygon

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 13 8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 14

Over the last couple of lectures we have looked at the idea of scan converting linesThe key thing to remember is this has to be FASTFor lines we have either DDA or BresenhamFor circles the mid-point algorithm

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 15 8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 16

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 17

9

7

6

5

4

3

2

1

0

8

976543210 8 10

10

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 18

Page 4: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

4

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 19 8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 20

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 21

Another approach to area filling is to start

at a point inside a region and paint the

interior outward toward the boundary.

• If the boundary is specified in a singlecolor, the fill algorithm processed outwardpixel by pixel until the boundary color isencountered.

• A boundary-fill procedure accepts asinput the coordinate of the interior point(x, y), a fill color, and a boundary color.

22

The recursive boundary-fill algorithm:

1. Start from an interior point. 2. If the current pixel is not already filled and

if it is not an edge point, then set the pixel with the fill color, and store its neighboring pixels (4 or 8-connected) in the stack for processing. Store only neighboring pixel that is not already filled and is not an edge point.

3. Select the next pixel from the stack, and continue with step 2.

23

The order of pixels that should be added

to stack using 4-connected is above,

below, left, and right. For 8-connected is

above, below, left, right, above-left, above-

right, below-left, and below-right.

24

Page 5: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

5

25

Start Position

Boundary Fill Algorithm

4-connected (Example)

26

3

2

11

2 3

Boundary Fill Algorithm

4-connected (Example)

27

1 4

2

4

2

1

Boundary Fill Algorithm

4-connected (Example)

28

1

2

2

1

Boundary Fill Algorithm

4-connected (Example)

29

5 15

1

Boundary Fill Algorithm

4-connected (Example)

30

11

Boundary Fill Algorithm

4-connected (Example)

Page 6: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

6

31

Boundary Fill Algorithm

4-connected (Example)

32

Start Position

Boundary Fill Algorithm

8-connected (Example)

33

4 1 5

2 35

4

3

2

1

Boundary Fill Algorithm

8-connected (Example)

34

Boundary Fill Algorithm

8-connected (Example)

6

4 1

2 36

4

3

2

1

35

Boundary Fill Algorithm

8-connected (Example)

7 8

4 1

2 3

8

7

4

3

2

1

36

Boundary Fill Algorithm

8-connected (Example)

11 9 12

7 10

4 1

2 3

12

11

10

9

7

4

3

2

1

Page 7: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

7

37

Boundary Fill Algorithm

8-connected (Example)

11 9

7 10

4 1

2 3

11

10

9

7

4

3

2

1

38

Boundary Fill Algorithm

8-connected (Example)

9

7 10

4 1

2 3

10

9

7

4

3

2

1

39

Boundary Fill Algorithm

8-connected (Example)

9

7

4 1

2 3

9

7

4

3

2

1

40

Boundary Fill Algorithm

8-connected (Example)

7

4 1

2 37

4

3

2

1

41

Boundary Fill Algorithm

8-connected (Example)

4 1

2 34

3

2

1

42

Boundary Fill Algorithm

8-connected (Example)

1

2 3

3

2

1

Page 8: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

8

43

Boundary Fill Algorithm

8-connected (Example)

1

2

2

1

44

Boundary Fill Algorithm

8-connected (Example)

1

1

45

Boundary Fill Algorithm

8-connected (Example)

46

Boundary Fill Algorithm

Since the previous procedure requires

considerable stacking of neighboring pixels, more

efficient methods are generally employed.

These methods (Span Flood-Fill) fill horizontal

pixel spans across scan lines, instead of

proceeding to 4-connected or 8-connected

neighboring pixels.

Then we need only stack a beginning position for

each horizontal pixel spans, instead of stacking all

unprocessed neighboring positions around the

current position.

47

Span Flood-Fill Algorithm

The algorithm is summarized as follows:

• Starting from the initial interior pixel, then fill in the

contiguous span of pixels on this starting scan line.

• Then locate and stack starting positions for spans

on the adjacent scan lines, where spans are defined

as the contiguous horizontal string of positions

bounded by pixels displayed in the area border

color.

• At each subsequent step, unstack the next start

position and repeat the process.

48

11

10

9

8

7

6

5 S

4

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Span Flood-Fill Algorithm (example)

Page 9: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

9

49

11

10

9

8

7

6 2

5 S

4 1

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

2

1

Span Flood-Fill Algorithm (example)

50

11

10

9

8

7 3

6 2

5 S

4 1

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

3

1

Span Flood-Fill Algorithm (example)

51

11

10

9

8 5 6

7 3

6 4 2

5 S

4 1

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

6

5

4

1

Span Flood-Fill Algorithm (example)

52

11

10

9 7

8 5 6

7 3

6 4 2

5 S

4 1

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

7

5

4

1

Span Flood-Fill Algorithm (example)

53

11

10

9 7

8 5 6

7 3

6 4 2

5 S

4 1

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

5

4

1

Span Flood-Fill Algorithm (example)

54

11

10

9 8 7

8 5 6

7 3

6 4 2

5 S

4 1

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

8

4

1

Span Flood-Fill Algorithm (example)

Page 10: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

10

55

11

10

9 8 7

8 5 6

7 3

6 4 2

5 S

4 1

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

4

1

Span Flood-Fill Algorithm (example)

56

11

10

9 8 7

8 5 6

7 3

6 4 2

5 9 S

4 1

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

9

1

Span Flood-Fill Algorithm (example)

57

11

10

9 8 7

8 5 6

7 3

6 4 2

5 9 S

4 10 1

3

2

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

10

1

Span Flood-Fill Algorithm (example)

58

11

10

9 8 7

8 5 6

7 3

6 4 2

5 9 S

4 10 1

3 11

2

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

11

1

Span Flood-Fill Algorithm (example)

59

11

10

9 8 7

8 5 6

7 3

6 4 2

5 9 S

4 10 1

3 11

2 12

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

12

1

Span Flood-Fill Algorithm (example)

60

11

10

9 8 7

8 5 6

7 3

6 4 2

5 9 S

4 10 1

3 11

2 12

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

1

Span Flood-Fill Algorithm (example)

Page 11: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

11

61

11

10

9 8 7

8 5 6

7 3

6 4 2

5 9 S

4 10 1

3 11

2 12

1

0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Span Flood-Fill Algorithm (example)

62

Flood Fill AlgorithmSometimes we want to fill in (recolor) an area that is

not defined within a single color boundary.

We paint such areas by replacing a specified interior

color instead of searching for a boundary color

value.

This approach is called a flood-fill algorithm.

63

Flood Fill Algorithm

We start from a specified interior pixel (x, y) and

reassign all pixel values that are currently set to a

given interior color with the desired fill color.

If the area has more than one interior color, we

can first reassign pixel values so that all interior

pixels have the same color.

Using either 4-connected or 8-connected

approach, we then step through pixel positions

until all interior pixels have been repainted.

Counting Number Methods

Odd-Even Rule

Nonzero winding number rule

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Count the edge crossing along the line from any point (x,y) to infinity.

If the number of interactions is odd, then the point (x,y) is an interior point;

Else, it is an exterior point.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 65

Give directions to all the edges of the polygon.

1 to all upward edges and -1 for others.

Check the sum of scan line.

If it is non-zero, then it is an interior point, otherwise it is an exterior point.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 66

Page 12: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

12

Vertices and Edges

Surface in Euclidean space defined by a parametric equation with two parameters

A set of weighted control points determine the location of individual surface points

The come in several flavors including Bezier, B-Spline, NURBS

Page 13: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

13

Marching Cubes (Geometric)

BONO - branch-on-need octree (Geometric)

ISSUE - Isosurfacing in Span Space with Utmost Efficiency (Span Space)

Interval Tree – (Span Space)

Fig 5.7. Relationship between color banding and contouring

Contour line (isoline): the same scalar value, or isovalue

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 75

A contour C is defined as all points p, in a dataset D, that have the same scalar value x: s(p) = x.

For 2D dataset: contour line (isoline); For 3D dataset: contour surface (iso-surface)

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Fig 5.8. Isoline properties

Contour properties: Closed curve or open

curves Never stop inside the

dataset itself Never intersects itself No intersect of an isoline

with another scalar value

Contours are perpendicular to the gradient of the contoured function

(Fig 5.9)

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 77

Fig 5.9. The gradient of a scalar field is perpendicular to the field's contours

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Page 14: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

14

Fig 5.10. Constructing the isoline for the scalar value v = 0.48. The figure indicates scalar values at the grid vertices.

Given a discrete, sampled dataset, compute contours

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 79

Contouring need

At least piecewise linear, C1 dataset

The complexity of computing contours

The most popular method

2D: Marching Squares (§5.3.1)

3D: Marching Cubes (§5.3.2)

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Fig5.12. Topological states of a quad cell (marching squares algorithm). Red indicates "inside" vertices. Bold indices mark ambiguous cases.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Listing 5.2. Marching squares pseudocode

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Similar to Marching Squares but 3D versus 2D 28= 256 different topological cases; Reduced to only 15 by symmetry

considerations

16 topological states (Fig 5.13)

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

William E. Lorensen, Harvey E. Cline: Marching Cubes: A high resolution 3D surface construction algorithm. In: Computer Graphics, Vol. 21, Nr. 4, July 1987

International Conference on Computer Graphics and Interactive Techniques (ACM/SIGGRAPH 1987)

Computes polygons where the isosurfacepasses through eight nearest neighbors

Gradient of scalar value at each grid point used for surface normal

Other algorithms are always compared to Marching Cubes

Page 15: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

15

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Marching Cubes is an algorithm which “creates triangle models of constant density surfaces from 3D medical data.”

Computed Tomography (CT) Magnetic Resonance (MRI) Single-Photon Emission Computed

Tomography (SPECT)

Each scanning process results in two dimensional “slices” of data.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Construction/Reconstruction of scanned surfaces or objects.

Problem of interpreting/interpolating 2D data into 3D visuals.

Marching Cubes provides a new method of creating 3D surfaces.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

High resolution surface construction algorithm.

Extracts surfaces from adjacent pairs of data slices using cubes.

Cubes “march” through the pair of slices until the entire surface of both slices has been examined.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Load slices. Create a cube from pixels on adjacent slices. Find vertices on the surfaces. Determine the intersection edges. Interpolate the edge intersections. Calculate vertex normals. Output triangles and normals.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Uses identical squares of four pixels connected between adjacent slices.

Each cube vertex is examined to see if it lies on or off of the surface.

Page 16: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

16

There are 28=256 ways the surface may intersect the cube

Triangulate each case

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

using the symmetries reduces those 256 cases to 15 patterns

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

15 Unique cube configurations that can be rotated and reflected to 256 configurations

Fig 5.13. Topological states of a hex cell (marching cubes algorithm).

Red indicates "inside" vertices. Bold indices mark ambiguous cases.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 95

Fig 5.14. Ambiguous cases for marching cubes. Each case has two contouring variants.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP 96

Page 17: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

17

• Pixels on the slice surfaces determine 3D surfaces.

• 256 surface permutations, but only 14 unique patterns.

• A normal is calculated for each triangle vertex for rendering.

8/23/2017

[email protected] - Faculty of Applied Sciences of USJP 97

Pros:

Simple rendering and manipulation

High resolution

Cons:

Possible holes in the model

Model complexity

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Determine triangles contained by a cube. Determine which cube edges are intersected. Interpolate intersection point using pixel

density. Calculate unit normals for each triangle

vertex using the gradient vector.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Variations can increase/decrease surface density.

Utilizes pixel, line and slice coherency to minimize the number of calculations.

Can provide solid modeling. Can use conventional rendering techniques

and hardware. No user interaction is necessary. Enables selective displays. Can be used with other density values.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Page 18: CSC 210 1.0 Computer Graphics Scan-Line Polygon Fill ...€¦ · Polygon Filling Scan-Line Polygon Fill Algorithm Boundary Fill Algorithm Span Flood-Fill Algorithm Inside-outside

8/23/2017

18

Fig 5.15. Ringing artifacts on isosurface. (a) Overview. (b) Detail mesh.

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

MRI scan data

“wavy” pattern:

Caused by subsampling

General rule: most isosurface details that are under or around the size of the resolution of the iso-surfaced dataset can be

either actual data or artifact

should be interpreted with great care

Can draw more than a single iso-surface of the same dataset in one visualization (Fig 5.16)

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Fig 5.16. Two nested isosurfaces of a tooth scan dataset

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Fig 5.17. Isosurfaces, isolines, and slicing

8/23/2017 [email protected] - Faculty of Applied Sciences of USJP

Isosurfaces and isolines are strongly related