Polygon Filling - cg.tuwien.ac.at filePolygon Filling Werner Purgathofer. Werner Purgathofer / Einf....

57
Einführung in Visual Computing 186.822 Polygon Filling Werner Purgathofer

Transcript of Polygon Filling - cg.tuwien.ac.at filePolygon Filling Werner Purgathofer. Werner Purgathofer / Einf....

Einführung in Visual Computing186.822

Polygon Filling

Werner Purgathofer

Werner Purgathofer / Einf. in Visual Computing 2

flexible data structure

Linked Lists

…x1 x2

data pointer(link to next element)

…x1 x2 …x1 x2 …x1 x2

list

list^.next list^.next^.next

list^ list^.next^

Werner Purgathofer / Einf. in Visual Computing 3

Linked Lists: Removal of First Element

1 2 3 4 5list

list = list^.next

Werner Purgathofer / Einf. in Visual Computing 4

Linked Lists: Inserting New First Element

1 2 3 4 5

0new

new^.next = list

list

Werner Purgathofer / Einf. in Visual Computing 5

Linked Lists: Inserting New First Element

1 2 3 4 5

0new

new^.next = listlist = new

list

Werner Purgathofer / Einf. in Visual Computing 6

Linked Lists: Exchanging Elements 2 & 4

1 2 3 4 5

last1 last2

list

help = last1^.next^.next

help

Werner Purgathofer / Einf. in Visual Computing 7

Linked Lists: Exchanging Elements 2 & 4

1 2 3 4 5

last1 last2

list

help = last1^.next^.nextlast1^.next^.next = last2^.next^.next

help

Werner Purgathofer / Einf. in Visual Computing 8

Linked Lists: Exchanging Elements 2 & 4

1 2 3 4 5

last1 last2

list

help = last1^.next^.nextlast1^.next^.next = last2^.next^.nextlast2^.next^.next = help

help

Werner Purgathofer / Einf. in Visual Computing 9

Linked Lists: Exchanging Elements 2 & 4

1 2 3 4 5

last1 last2

list

help = last1^.next^.nextlast1^.next^.next = last2^.next^.nextlast2^.next^.next = helphelp = last1^.next

help

Werner Purgathofer / Einf. in Visual Computing 10

Linked Lists: Exchanging Elements 2 & 4

1 2 3 4 5

last1 last2

list

help = last1^.next^.nextlast1^.next^.next = last2^.next^.nextlast2^.next^.next = helphelp = last1^.nextlast1^.next = last2^.next

help

Werner Purgathofer / Einf. in Visual Computing 11

Linked Lists: Exchanging Elements 2 & 4

1 2 3 4 5

last1 last2

list

help = last1^.next^.nextlast1^.next^.next = last2^.next^.nextlast2^.next^.next = helphelp = last1^.nextlast1^.next = last2^.nextlast2^.next = help

help

Werner Purgathofer / Einf. in Visual Computing 12

odd-even rule nonzero-winding- number rule

Repetition: What is Inside a Polygon?

???

Werner Purgathofer / Einf. in Visual Computing 13

area-filling algorithms“interior”, “exterior” for self-intersecting polygons?odd-even rulenonzero-winding-number rulesame result for simple polygons

Inside-Outside Tests

Werner Purgathofer / Einf. in Visual Computing 14

inside/outside switches at every edgestraight line to the outside:

even # edge intersections = outside odd # edge intersections = inside

What is Inside?: Odd-Even Rule

1234

1234

123

12

1

Werner Purgathofer / Einf. in Visual Computing 15

point is inside if polygon surrounds itstraight line to the outside:

same # edges up and down = outsidedifferent # edges up and down = inside

What is Inside?: Nonzero Winding Number

Werner Purgathofer / Einf. in Visual Computing 16

for polygon area (solid-color, patterned)scan-line polygon fill algorithm

intersection points located and sortedconsecutive pairs define interior spanattention with vertex intersectionsexploit coherence (incremental calculations)

flood fill algorithm

Fill-Area Primitives

Werner Purgathofer / Einf. in Visual Computing 17

Scan-Line Fill: Sorted Edge Table

The sorted edge table contains all polygon edges sorted by lowest y-value

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

ymax xstart 1/m

Werner Purgathofer / Einf. in Visual Computing 18

sort all edges by smallest y-valueedge entry:

[max. y-value, x-intercept, inverse slope]

active-edge listfor each scan linecontains all edges crossed by that scan lineincremental update

consecutive intersection pairs (spans) filled

Scan-Line Fill: Sorted Edge Table

yB xC 1/mCB yD xC 1/mCD

Werner Purgathofer / Einf. in Visual Computing 19

Sorted Edge Table

The sorted edge table contains all polygon edges sorted by lowest y-value

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

ymax xstart 1/m

Werner Purgathofer / Einf. in Visual Computing 20

Sorted Edge Table / Active Edge List

Active Edge List When processing from bottom to top, keep a list of all active edges

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 21

Active Edge List

Sorted Edge Table / Active Edge List

incremental update!yF xA 1/mAF yB xA 1/mAB

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 22

Active Edge List

Sorted Edge Table / Active Edge List

yF xA 1/mAF yB xA 1/mAB

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 23

Active Edge List

Sorted Edge Table / Active Edge List

yE xF 1/mFE yB xA 1/mAB

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 24

Active Edge List

Sorted Edge Table / Active Edge List

yE xF 1/mFE yB xA 1/mAB

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 25

Active Edge List

Sorted Edge Table / Active Edge List

yE xF 1/mFE yB xA 1/mAB

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 26

Active Edge List

Sorted Edge Table / Active Edge List

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

yE xF 1/mFE yB xA 1/mAB yB xC 1/mCB yD xC 1/mCD

Werner Purgathofer / Einf. in Visual Computing 27

Active Edge List

Sorted Edge Table / Active Edge List

yE xF 1/mFE yB xA 1/mAB yB xC 1/mCB yD xC 1/mCD

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 28

Active Edge List

Sorted Edge Table / Active Edge List

yE xF 1/mFE yD xC 1/mCD

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 29

Active Edge List

Sorted Edge Table / Active Edge List

yE xF 1/mFE yD xC 1/mCD

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 30

Active Edge List

Sorted Edge Table / Active Edge List

yE xF 1/mFE yE xD 1/mDE

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 31

Active Edge List

Sorted Edge Table / Active Edge List

yE xF 1/mFE yE xD 1/mDE

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 32

Active Edge List

Sorted Edge Table / Active Edge List

A

B

C

D

E

F

yF xA 1/mAF yB xA 1/mAB

yE xF 1/mFE

yB xC 1/mCB yD xC 1/mCD

yE xD 1/mDE

Werner Purgathofer / Einf. in Visual Computing 33

incremental update of intersection point

1+=kk+1

yymxx kk+11+=

slope of polygon boundary line: m

(for 2 successive scanlines)

Scan-Line Fill: Incremental Update

yk + 1yk(xk , yk)

(xk+1, yk+1)

Werner Purgathofer / Einf. in Visual Computing 34

intersection points along scan lines that intersect polygonvertices

→ either special handling (1 or 2 intersections?)

→ or move vertices up or down by ε

Scan-Line Fill: Intersecting Vertices

1

1

2

2

2

Werner Purgathofer / Einf. in Visual Computing 35

pixel filling of areaareas with no single color boundarystart from interior point“flood” internal region4-connected, 8-connected areasreduce stack size by eliminating several recursive calls

Flood-Fill Algorithm

Werner Purgathofer / Einf. in Visual Computing 36

area must be distinguishable from boundaries

example: area defined within multiple color boundaries

Flood-Fill: Boundary and Seed Point

seed point

Werner Purgathofer / Einf. in Visual Computing 37

Definition: 4-connected means, that a connection is only valid in these 4 directions

Definition: 8-connected means, that a connection is valid in these 8 directions

Flood-Fill: Connectedness

Werner Purgathofer / Einf. in Visual Computing 38

has an 8-connected borderan 8-connected areahas a 4-connected border

a 4-connected area

Examples for 4-connected and 8-connected

Werner Purgathofer / Einf. in Visual Computing 39

Simple Flood-Fill Algorithm

void floodFill4 (int x, int y, int new, int old){ int color;

/* set current color to new */getPixel (x, y, color);if (color = old) {

setPixel (x, y);floodFill4 (x–1, y, new, old); /* left */floodFill4 (x, y+1, new, old); /* up */floodFill4 (x+1, y, new, old); /* right */floodFill4 (x, y–1, new, old) /* down */

}}

12

43

recursionsequence

Werner Purgathofer / Einf. in Visual Computing 40

Bad Behavior of Simple Flood-Fill

12

43

recursionsequence

Werner Purgathofer / Einf. in Visual Computing 41

floodFill4 produces too high stacks (recursion!)solution:

incremental horizontal fill (left to right)recursive vertical fill (first up then down)

Span Flood-Fill Algorithm

Werner Purgathofer / Einf. in Visual Computing 42

Good Behavior of Span Flood-Fill

1

2recursionsequence

Werner Purgathofer / Einf. in Visual Computing 43

Span Flood-Fill Example

Stack:x

Werner Purgathofer / Einf. in Visual Computing 44

Span Flood-Fill Example

B

A1 2 3

Stack:xAB

Werner Purgathofer / Einf. in Visual Computing 45

Span Flood-Fill Example

A1 2 3

Stack:ABCDEF

F E

CD4 5 6 7 8 9 10 11

Werner Purgathofer / Einf. in Visual Computing 46

Span Flood-Fill Example

A1 2 3

Stack:ACDEFG

GE

CD4 5 6 7 8 9 10 11

12 13 14

Werner Purgathofer / Einf. in Visual Computing 47

Span Flood-Fill Example

A1 2 3

Stack:ACDEGH

E=H

CD4 5 6 7 8 9 10 11

12 13 1415 16 17 18

Werner Purgathofer / Einf. in Visual Computing 48

Span Flood-Fill Example

A1 2 3

Stack:ACDEHCD

4 5 6 7 8 9 10 1112 13 14

15 16 17 1819 (E)

Werner Purgathofer / Einf. in Visual Computing 49

Span Flood-Fill Example

A1 2 3

Stack:ACDIJC

I

4 5 6 7 8 9 10 1112 13 14

15 16 17 1819

J20 21 22

Werner Purgathofer / Einf. in Visual Computing 50

Span Flood-Fill Example

A1 2 3

Stack:ACIJ

CI

4 5 6 7 8 9 10 1112 13 14

15 16 17 1819

20 21 2223

Werner Purgathofer / Einf. in Visual Computing 51

Span Flood-Fill Example

A1 2 3

Stack:ACIK

C

K

4 5 6 7 8 9 10 1112 13 14

15 16 17 1819

20 21 2223

24 25

Werner Purgathofer / Einf. in Visual Computing 52

Span Flood-Fill Example

A=N1 2 3

Stack:ACKL

MN

C

M

4 5 6 7 8 9 10 1112 13 14

15 16 17 1819

20 21 2223

24 2526 27 28 29 30

L

Werner Purgathofer / Einf. in Visual Computing 53

Span Flood-Fill Example

(A)1 2 3

Stack:ACLMNC

M

4 5 6 7 8 9 10 1112 13 14

15 16 17 1819

20 21 2223

24 2526 27 28 29 30

L

31 32 33

Werner Purgathofer / Einf. in Visual Computing 54

Span Flood-Fill Example

1 2 3

Stack:CLM

C4 5 6 7 8 9 10 11

12 13 1415 16 17 18

19

20 21 2223

24 2526 27 28 29 30

L

31 32 33

34 35

Werner Purgathofer / Einf. in Visual Computing 55

Span Flood-Fill Example

1 2 3

Stack:CL

C4 5 6 7 8 9 10 11

12 13 1415 16 17 18

19

20 21 2223

24 2526 27 28 29 30

31 32 33

34 35 36 37

Werner Purgathofer / Einf. in Visual Computing 56

Span Flood-Fill Example

1 2 3

Stack:C

4 5 6 7 8 9 10 1112 13 14

15 16 17 1819

20 21 2223

24 2526 27 28 29 30

31 32 33

34 35 36 37

38

finished!

3D Polygons

normally surface elements of 3D objectsattributes are surface attributes

color, material, transparency, texture, geometric microstructure, reflection properties, …

attributes and normal vectors often at the verticestriangles: linear interpolation with barycentric coordinates

Werner Purgathofer / Einf. in Visual Computing 57