Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights...

28
Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    0

Transcript of Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights...

Page 1: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Scan Conversion II

Revision 1.42/13/07

Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved

Page 2: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Polygons

A polygon is a many-sided planar figure composed of vertices and edges. A polygon is bounded (finite area) and closed (includes boundary).

Vertices are represented by points (x,y).

Edges are represented as line segments which connect two points, (x1,y1) and (x2,y2).

P = { (xi , yi ) } i=1,n

E3

(x3,y3)

E2

(x2,y2)E1(x1,y1)

Larry Hodges

Page 3: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Polygons: Complex vs Simple

Zachary Wartell

• A simple polygon – edges only intersect a vertices, no coincident vertices

• A complex polygon – edges intersect and/or coincident vertices

A

B

C

C

EF

G

H

A B,E

C

D

F

A

B

C

A B

C

D

E

Page 4: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Simple Polygons: Convex and Concave

Convex Polygon - For any two points P1, P2 inside the polygon, all points on the line segment which connects P1 and P2 are inside the polygon.

– All points P = uP1 + (1-u)P2, u in [0,1] are inside the polygon provided that P1 and P2 are inside the polygon.

Concave Polygon - A polygon which is not convex.

Larry Hodges, Zachary Wartell

Page 5: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Inside vs Outside Polygon: Odd-Parity Test

Larry Hodges, Zachary Wartell

-Is point P inside polygon?

-Count # times a ray from P intersects edge of polygon

-odd count implies inside

-When crossing a vertex, if the vertex’s two edges are on same side of line then count it twice, else count it once.

Count onceCount twice

oror

Page 6: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Filling Concave Polygons

Fill the polygon 1 scanline at a time

Determine which pixels on each scanline are inside the polygon and set those pixels to the appropriate value.

Look only for those pixels at which changes occur.

Larry Hodges, Zachary Wartell

Page 7: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Scan-Line Algorithm

Larry Hodges, Zachary Wartell

For each scan-line:

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

Possible Problems

1. Horizontal edges ==> Ignore

2. Vertices ==> If local max or min, then count twice, else count once. (This is implemented by shortening one edge by one pixel.)

3. Calculating intersections is slow.

2

4

6

8

For scan-line number 7 the sorted list of x-coordinates is (1,3,7,9)

Therefore fill pixels with x-coordinates 1-3 and 7-9.

Page 8: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Edge Coherence

Larry Hodges, Zachary Wartell

-Not all polygon edges intersect each scanline.

-Many edges intersected by scanline yk will also be intersected by scanline yk+1

-Nearby pixels on given edge and span have similar coordinates, attributes and equation components

Page 9: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

“scan-line aligned” trapezoids

Zachary Wartell

T1

T2

T3

T4

T5T6

-break up problem into simpler ”scan-line aligned” trapezoids- only two side edges: left, right

left edgeof T2

right edgeof T2

Page 10: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Edge Table

Zachary Wartell

T1

T2

T3

T4

T5T6

-sort vertices by y coordinate building Edge Table (ET)

ymax

y=0A

B

CD

F

E

G

H

yD,C

yG

yE

yB

yH

yA AH AB

HG

BC

EF ED

GF

ET

not needed

Page 11: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Scan AET with AEL

Zachary Wartell

T1

T2

T3

T4

T5T6

-scan up through ET and track Active Edge Table (AET). At each scan-line fill in span of pixel for each edge pair.

ymax

y=0A

CD

F

E

G

H

yG

yE

yB

yH

yA AH AB

HG

BC

EF ED

GF

ET AETk

(k = 1..10)

[(AH,AB)]

[(HG,AB)]

[(HG,BC)]

[(HG,EF),(DE,BC)]

[(GF,EF),(DE,BC)]

[(DE,BC)]

B

Page 12: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

How do we fill trapezoids?

Zachary Wartell

-Need to generate pixel coordinates of left and right edges of edge pair-- (lx,ly) and (rx,ry)-Need to fill pixel in span between current

A

CD

F

E

G

H

B

A

CD

F

E

G

H

B

rx0, lx0

y0

rx2rx3

lx1,

lx2,

lx3

H

G

y1

y2

y3

rx1

Page 13: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Rasterize Edge

Zachary Wartell

-From y=mx + b we have: yk+1= yk+1,

xk+1= xk+ 1 / m = xk+ Δx / Δy - fractions yuck!

-So split into integer and fraction and look at fraction numerator: xk+1= xk+ (Δx div Δy) + (Δx mod Δy) / Δy

integer fraction

xk = xik + xnk / Δ y, xi is integer component xn is x’s fraction’s numerator

xk=0

yk=0

yk

yk+1

xk xk+1 …

B

Page 14: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Rasterize Edge

Larry Hodges, Zachary WartellLarry Hodges, Zachary Wartell

-Base Case: xi = x0 , xn = 0-Iteration Case: xi is integer component, xn is fraction numerator

xi := xi + (Δx div Δy)xn := xn + (Δx mod Δy)if xn ≥ Δy then

xi := xi + 1 xn := xn – Δyendif

A=(6,1)

B=(13,4)

Δx=7Δy=3Δx div Δy= 2Δx mod Δy= 1 k y xi xn x (=xi+xn/Δy)

0 1 6 0 61 2 8 1 8 1/32 3 10 2 10 2/33 4 13 0 13 0/3

(8,2)(10,3)

Warning: H&B doesn’t handle Δx > Δy

Page 15: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Rasterization Special Cases

Larry Hodges, Zachary Wartell

-Previous slide truncated to obtain integer x coordinate, alternatively use rounding

-you end up with a fraction (comparing xn to Δy/2) and must multiply xn by 2 to get all integer operations

-Often polygons share edges, to avoid writing the same pixel twice possibly:

-horizontal edge – only drawn when on top-right edge – round up-left edge – round down, if xn = 0

Page 16: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Rasterization Special Cases

Larry Hodges, Zachary Wartell

-Edge Shortening Trick:-Recall Odd-Parity Rule Problem:

-Implement “Count Once” case with edge shortening:

Count onceCount twice

oror

Count onceCount twice

oror

A

B

C

A

B

C

A

B

C

B'

A

B

C

B'

C

B

A

B

C

B

A

B'B

B'

xA,yB’,1/mAB xC,yB’,1/mCB

Page 17: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Splitting Concave Polygons – Concept

Concave can be split into Convex Polygons – this simplifies Edge Tableand Active Edge Table in polygon rasterizer (only one scan-line trapezoid is active at a time!)

Identify: look for interior angle greater than 180º - cross product signs will differ

X

YE1

E2

E3

E4

E5

E6

(E1 X E2)z > 0

(E2 X E3)z > 0

(E3 X E4)z < 0

(E4 X E5)z < 0

(E5 X E6)z > 0

(E6 X E1)z > 0

i j kx0 y0 z0x1 y1 z1

©Zachary Wartell - 1/26/2005

Page 18: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Splitting Concave Polygons

Edge Vector: Compute Edge Vectors (Ek = Vk+1- Vk) traversing polygon counter-clockwise.If cross product of consecutive edge is negative, then split polygon alongthe embedding line of first edge in cross product

Rotational: put successive edges on x-axis. If next edge has negative x coordinate split polygon on x-axis.

X

YE1

E2

E3

E4

E5

E6

X

Y E1

E2

E3

E4

E5

E6

XY

X

Y

©Zachary Wartell - 1/26/2005

Page 19: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Splitting Convex Polygon to Triangles

Triangle rasterizing is even simpler. Only 3 edges to “sort”. Only fill in 1 or 2 scan-line aligned triangles. Pretty much what all graphics hardware does.

Pick 3 vertices of convex polygon. Split off triangle. Repeat with remaining vertices. Continue until only 3 vertices left.

v0

v1

v2

v3

v4

v0

v1

v2

v3

v4

v0

v1

v2

v3

v4

A) B) C)

©Zachary Wartell - 1/26/2005

Page 20: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Filling Irregular Boundaries

Larry Hodges, Zachary Wartell

boundary fill: expand and fill region until you reach boundary color

flood fill: expand and fill region while you find interior color

Boundary Fill

Interior Fill

Page 21: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

4 vs 8 connected

Larry Hodges, Zachary Wartell

Define: 4-connected versus 8-connected,its about the neighbors

Page 22: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

4 vs 8 connected

Zachary Wartell

Fill Result: 4-connected versus 8-connected

“seed pixel”

Page 23: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Recursive Pixel Boundary Fillhttp://www.cosc.canterbury.ac.nz/people/mukundan/cogr/Fill.html

void boundaryFill4(int x, int y){  if(getPixel(x,y)!=borderColor && getPixel(x,y)!=fillColor ){   setPixel(x,y);   boundaryFill4(x+1,y);   boundaryFill4(x-1,y);   boundaryFill4(x,y+1);   boundaryFill4(x,y-1);  } }

Page 24: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

65

47

1

2

131

3

12

14156

141

5

5

7

4

Recursive Scan-line (boundary) fill

Zachary Wartell

A B

C D

Stack

Page 25: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Fill Patterns

Larry Hodges, Zachary Wartell

Fill patterns can be used to put a noticeable “texture” inside a polygon. A fill pattern can be defined in a 0-based, m x n array. A pixel (x,y) is assigned the value found in:

pattern((x mod m), (y mod n))

Pattern Pattern filled polygon

Page 26: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Halftoning

Larry Hodges, Zachary Wartell

For 1-bit (B&W) displays, fill patterns with different fill densities can be used to vary the range of intensities of a polygon. The result is a tradeoff of resolution (addressability) for a greater range of intensities and is called halftoning. The pattern in this case should be designed to avoid being noticed.

These fill patterns are chosen to minimize banding.

Page 27: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Dithering

Larry Hodges, Zachary Wartell

Another method to increasing the number of apparent intensities on a 1-bit (B&W) display is dithering. In an ordered dither the decision to turn a pixel on or off at point (x,y) depends on the desired intensity I(x,y) at that point and on an (n by n) dither matrix Dn. The dither matrix is indexed from 0 to (n-1) along its rows and columns. Each of the integers 0 to n2 -1 appears once in the matrix. For instance when n = 4, we have D4 =

0 8 2 10

12 4 14 6

3 11 1 9

15 7 13 5

To process the point at (x,y), we first compute

i = x MOD 4, j = y MOD 4

Then if I(x,y) > D4(i, j) the point (x,y) is turned on; otherwise it is not.

Page 28: Scan Conversion II Revision 1.4 2/13/07 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.

Revisions

Larry Hodges, Zachary Wartell

1.11. Removed slide 17 “Antialiasing Polygons” (putting anti-aliasing from H&B in separate .ppt)

1.2 1. Fixed error in slide 11

1.4– Added Slide 23 ‘Recursive Pixel Boundary Fill’– clarified diagram in slide 24 and noted subtly/error found in textbook.