1
Rasterization and Clipping Computer Graphics Course 2006 Todays - - PDF document
Rasterization and Clipping Computer Graphics Course 2006 Todays - - PDF document
1 Rasterization and Clipping Computer Graphics Course 2006 Todays agenda We will learn about basic low-level algorithms dealing with rasterization and clipping. This algorithms will be implemented in most of the graphics packages
2
Today’s agenda
We will learn about basic low-level algorithms
dealing with rasterization and clipping.
This algorithms will be implemented in most of
the graphics packages you will deal with.
Basic ideas outlined here are important outside
- f the specific domain of the clipping and
rasterization.
The constants hidden in O(n) algorithms are what all
this is about.
3
Today’s agenda
So what is clipping? rasterization? Lets start with the line clipping
4
Line Clipping Algorithms
Goal: avoid drawing primitives that are
- utside the viewing window.
The most common case: clipping line
segments against a rectangular window
5
Line Clipping Algorithms
Three cases:
Segment is entirely inside the window -
Accept
Segment is entirely outside the window -
Reject
Segment intersects the boundary - Clip
6
Point Classification
How can we tell if a point is inside a
rectangular window?
xmin xmax ymax ymin
max min max min
y y y x x x ≤ ≤ ≤ ≤
max min max min
y y y x x x ≤ ≤ ≤ ≤
7
Line Segment Clipping
If both endpoints are inside the window,
the entire segment is inside: trivial accept
If one endpoint is inside, and the other is
- utside, line segment must be split.
What happens when both endpoints are
- utside? In what situation can we claim
for a trivial reject?
8
Cohen-Sutherland Algorithm
Assign a 4-digit binary outcode to each of
the 9 regions defined by the window:
xmin xmax ymax ymin 0000 0010 0001 0110 1010 1000 0100 1001 0101
9
Cohen-Sutherland Algorithm
xmin xmax ymax ymin 0000 0010 0001 0110 1010 1000 0100 1001 0101 bit bit 1 1 2 2 3 3 4 4 condition condition y > y > y ymax
max
y < y < y ymin
min
x > x > x xmax
max
x < x < x xmin
min
10
Cohen-Sutherland Algorithm
Compute the outcodes C1 and C2
corresponding to both segment endpoints.
If ((C1 | C2) = = 0): Trivial Accept If ((C1 & C2) != 0): Trivial Reject Otherwise, split segment into two parts.
Reject one part, and repeat the procedure
- n the remaining part.
What edge should be intersected ?
11
Example
xmin xmax ymax ymin A B C D Outcode(A Outcode(A) = 0000 ) = 0000 Outcode(D Outcode(D) = 1001 ) = 1001 Clip (A,D) with Clip (A,D) with y = y = y ymax
max, splitting it into (A,B) and (B,D)
, splitting it into (A,B) and (B,D) Reject (B,D) Reject (B,D) Proceed with (A,B) Proceed with (A,B) No trivial accept/reject No trivial accept/reject
12
Example
xmin xmax ymax ymin A B C D Outcode(A Outcode(A) = 0100 ) = 0100 Outcode(E Outcode(E) = 1010 ) = 1010 Clip (A,E) with Clip (A,E) with y = y = y ymax
max, splitting it into (A,D) and (D,E)
, splitting it into (A,D) and (D,E) Reject (D,E) Reject (D,E) Proceed with (A,D) Proceed with (A,D) No trivial accept/reject No trivial accept/reject E
13
Example
xmin xmax ymax ymin A B C D Outcode(A Outcode(A) = 0100 ) = 0100 Outcode(D Outcode(D) = 0010 ) = 0010 Clip (A,D) with Clip (A,D) with y = y = y ymin
min, splitting it into (A,B) and (B,D)
, splitting it into (A,B) and (B,D) Reject (A,B) Reject (A,B) Proceed with (B,D) Proceed with (B,D) No trivial accept/reject No trivial accept/reject
14
Example
xmin xmax ymax ymin B C D Outcode(B Outcode(B) = 0000 ) = 0000 Outcode(D Outcode(D) = 0010 ) = 0010 Clip (B,D) with Clip (B,D) with x = x = x xmax
max, splitting it into (B,C) and (C,D)
, splitting it into (B,C) and (C,D) Reject (C,D) Reject (C,D) Proceed with (B,C) Proceed with (B,C) No trivial accept/reject No trivial accept/reject
15
Parametric Line Clipping (Cyrus-Beck Algorithm)
Represent line segment parametrically as Solve the equation
Dt P t P P P t P + = − + =
1
) ( ) ( Dt P t P P P t P + = − + =
1
) ( ) (
( )
) ( = − ⋅ E t P N (
)
) ( = − ⋅ E t P N
( ) ( ) = ⋅ + − ⋅ ⇔ = − + ⋅ ⇔ Dt N E P N E Dt P N ( ) ( ) = ⋅ + − ⋅ ⇔ = − + ⋅ ⇔ Dt N E P N E Dt P N ( )
D N E P N t ⋅ − − ⋅ =
( )
D N E P N t ⋅ − − ⋅ = N N P P0 P P1
1
E E
16
Intersection Classification (Cyrus-Beck Algorithm)
Clip the line to a rectangle. Clip the line to a rectangle. Edges of rectangle are defined by point and normal. Edges of rectangle are defined by point and normal.
p0 p1
N1 N2 N4 N3 E1 E2 E3 E4
17
Intersection Classification (Cyrus-Beck Algorithm)
- The segment is potentially entering
(PE) the clip region. (remember D=P1-P0)
- The segment is potentially leaving
(PL) the clip region. We want the entry with the highest t1 (or zero) and the lowest leaving t2 (or one). Since those t’s will define the smallest segment which is, when valid, fully included in the clipping region. The range [t1, t2] defines the clipped segment.
( )⇒
< ⋅ D Ni
( )⇒
< ⋅ D Ni
( )⇒
> ⋅ D Ni
( )⇒
> ⋅ D Ni
18
Intersection Classification (Cyrus-Beck Algorithm)
p0 p1 N1 N2 N3 N4 E1 E2 E3 E4
PL PL PE PE
Lets consider several cases: t1>t2 - means that on the line P1-P2 we have first left and then entered the clipping
- region. As in
(But this is not the case as in) Such case should be clipped completely. Another case is when the segment is parallel to on of the clipping edges, as in
PL PE PE PL PE
19
Cyrus-Beck Algorithm
t1=-oo t2 = oo
For each edge i with normal Ni and point Ei compute t.
If return NIL else continue ;
If t2=min(t,t2) else t1=max(t,t1)
If (t2 < t1 or t1>1 or t2<0) then return NIL t1=max(t1,0) t2=min(t2,1) Return [t1,t2]
( )
> ⋅ D Ni
( )
> ⋅ D Ni
) ) ( and (
1 /
> − ⋅ = ⋅
i i i
E P N D N ) ) ( and (
1 /
> − ⋅ = ⋅
i i i
E P N D N
20
Today’s agenda
Now lets see how the rasterization of the
simple line is done
21
Rasterization
Clip primitives to viewing window. Transform clipped primitives to device
coordinates
Points: round floating point coordinates to
nearest pixel coordinates.
Lines: determine the coordinates of all pixels
that “lie” on the line.
22
Rasterization
So how would you draw a simple line?
23
An Incremental Algorithm
Input: endpoints and Compute Line equation for x := x1 to x2
y := Round( ax + b ) DrawPixel( x, y )
) , (
1 1 y
x ) , (
2 2 y
x
( ) ( )
1 2 1 2
x x y y a − − =
b ax ax y ax y + = − + = ) (
1 1
24
Incremental Algorithm
Note: when x is incremented by 1, y is
incremented by a
DrawPixel(x1, y1) y := y1 for x := x1+ 1 to x2
y := y + a DrawPixel( x, Round(y) )
So is there something else left to optimize?
25
The Midpoint Algorithm (Bresenham)
Assumption: line has slope ‘a' between 0
and 1
Idea: choose the next pixel by checking if
the midpoint is above or below the line.
) , ( y x ) , ( y x ) , 1 ( y x + ) , 1 ( y x + ) 1 , 1 ( + + y x ) 1 , 1 ( + + y x ) 2 1 , 1 ( + + y x ) 2 1 , 1 ( + + y x
26
The Midpoint Algorithm
Each line can be written using the line
equation:
The relation between any point (x,y) and
the line can be determined by the sign of F(x,y):
F(x,y) = = 0 for points ON the line F(x,y) < 0 for points ABOVE the line F(x,y) > 0 for points BELOW the line
) , ( = + + = C By Ax y x F ) , ( = + + = C By Ax y x F
27
The Midpoint Algorithm
Given a chosen pixel (x,y), the next pixel will
be:
(x+ 1,y) if F(M) < = 0 (denote this pixel by E ) (x+ 1,y+ 1) if F(M) > 0 (denote this pixel by NE )
) , ( y x ) , ( y x ) , 1 ( y x + ) , 1 ( y x + ) 1 , 1 ( + + y x ) 1 , 1 ( + + y x
) 2 1 , 1 ( + + = y x M ) 2 1 , 1 ( + + = y x M NE E
28
The Midpoint Algorithm
For each pixel compute: Make a decision based on sign of d Incrementally update M and d
) , ( y x ) , ( y x ) , 1 ( y x + ) , 1 ( y x + ) 1 , 1 ( + + y x ) 1 , 1 ( + + y x
) 2 1 , 1 ( + + = y x M ) 2 1 , 1 ( + + = y x M
C y B x A d + + + + = ) 2 1 ( ) 1 (
NE E
29
The Midpoint Algorithm
If d < = 0, we choose (x+ 1, y) (E)
M = M + (1,0) = > d = d + A
If d > 0, we choose (x+ 1, y+ 1) (NE)
M = M + (1,1) = > d = d + (A + B)
Each iteration we compute d by adding
either A or (A+ B), based on the sign of d
30
The Midpoint Algorithm
What should the initial value of d be? To avoid division, we’ll multiply everything
by 2, and result with the following algorithm:
) ( ) ( ) , ( ) ( ) ( ) 1 ( ) , 1 (
2 2 1 1 2 1 1 2 1 1 1 2 1 1 1 B B B
A A y x F A C By Ax C y B x A y x F + = + + = + + + + = + + + + = + +
31
The Midpoint Algorithm
Refering to the implicit line equation: y=(dy/dx)x+b => F(x,y) = Ax + By + C == (dy)x - (dx)y + C = 0 , the Midpoint algorithm is as follows: dE = 2*A /* In order to avoid fractions we’ll dNE = 2*(A+B) /* multiply the equations by 2 x = x0, y = y0 d = 2*A+B DrawPixel( x0, y0 ) while ( x < x1 ) { if ( d <= 0 ) { d = d + dE, x++ } else { d = d + dNE, x++, y++ } DrawPixel( x, y ) }
32
Today’s agenda
And now to the polygon filling (it is
REALLY the last one for today)
33
Point-in-polygon test
How do we tell if a point is inside or
- utside a polygon?
34
Point-in-polygon test
Odd-even rule: count the number of
times a line from the point of interest to a point known to be outside crosses the edges of the polygon.
Odd = inside Even = outside
35
Point-in-polygon test
- Non
Non-
- zero winding number rule:
zero winding number rule: Consider the Consider the number of times the polygon edges wind number of times the polygon edges wind around the point of interest (counter around the point of interest (counter-
- clockwise direction).
clockwise direction).
- Define edge direction to be counter
Define edge direction to be counter-
- clockwise.
clockwise.
- Define edge normal as the clockwise normal.
Define edge normal as the clockwise normal.
- Choose a
Choose a “ “far far” ” point, outside polygon, and cast a point, outside polygon, and cast a ray from the point of interest to that point. ray from the point of interest to that point.
- Calculate a winding number: for each edge crossed:
Calculate a winding number: for each edge crossed:
- If
If angle(ray, normal) < 90 angle(ray, normal) < 90
- increase counter by 1.
increase counter by 1.
- If
If angle(ray, normal) > 90 angle(ray, normal) > 90
- decrease the count by
decrease the count by 1. 1.
- If the winding number is non
If the winding number is non-
- zero the point is
zero the point is inside the polygon. inside the polygon.
36
Point-in-polygon test
Which interior corresponds to which rule?
37
Point-in-polygon test
Which interior corresponds to which rule?
Odd-even rule Winding number
38
Scan-Line Polygon Fill
Two scan-line spans:
from x= 2 through 4 from x= 9 through 13
The scanline “span”:
A span is a group of adjacent pixels on a scanline, that are considered to be interior to the polygon.
39
Polygon Fill-
Scan-line algorithm
For each scan-line crossing the polygon, find
intersections with polygon edges, sort from left to right, and fill interior spans.
40
Polygon Fill-
Scan-line algorithm steps
Find the intersection of the scan line with all
edges of the polygon.
Sort the intersections by increasing x
coordinate.
Fill all pixels between pairs of intersections
that lie interior to the polygon, using the odd- parity rule:
Parity is initially even, and each intersection
inverse the parity bit.
Draw when parity is odd. Do not draw when parity is even.
41
Scan-Line Polygon Fill:
span extrema
.
(a) MidPoint algorithm edge pixels: (b) Only Interior pixels.
42
Polygon Fill - scanline algorithm
Special cases
Q: Given an intersection with an arbitrary
fractional x value, which pixel on either side
- f intersection is interior ?
A: If we approach a fractional intersection to
the right and are inside polygon, we round down the x coordinate to be inside polygon; and vice versa.
43
Polygon Fill - scanline algorithm
Special cases
Q: How do we deal with intersection at integer
pixel coordinate (think of 2 polygons sharing such pixel - to whom does it belong) ?
A:
Leftmost pixels of a span are considered to be interior. Rightmost pixels, are considered to be exterior.
44
Polygon Fill - scanline algorithm
Special cases
Q: How do we deal with intersection at integer pixel
coordinate which is also a shared vertex ? A: We will count only the Ymin vertex of an edge in the parity calculation, but not the Ymax.
Q: How do we deal with intersection at integer pixel
coordinate where the vertices also define a horizontal line ? A: We ignore horizontal edges in intersection calculations.
45
Which edges will be drawn in the polygon
below?
46
Scan-Line Coherence
Scan-line coherence means that the interior spans
corresponding to two adjacent scan lines are usually very similar.
We use scan-line coherence in order to compute
these spans incrementally, rather than intersecting each scan line with the polygon.
In particular, if a polygon edge intersects a scan line
y= k at xk, the intersection with y= k+ 1 is
m x x
k k
1
1
+ =
+
m x x
k k
1
1
+ =
+ (where m is the slope of the edge).
47
Scan-Line Algorithm-
Data structures
Edge Table (ET):
An entry for each scan line crossing the polygon. Each entry contains a list of all polygon edges whose
lower end (Ymin) is on this scan line.
For each edge we store the following information:
xmin, ymax, and slope (dx and dy or m).
The edges in each list are sorted from left to right
(according to their xmin).
48
Scan-Line Algorithm-
Data structures
Edge-Table Example:
49
Scan-Line Algorithm-
Data structures
Active Edge Table (AET):
Maintain an Active Edge list that contains only the list
- f edges crossing the current scan line.
Therefore, the edges held by the AET are updated each new scanline.
In this table each edge element holds the following
information: Ymax, slope, (all taken from the ET), and the x coordinate of intersection point between the edge and current scanline (this should be updated for each new scanline).
The edges in this table are sorted by the x coordinate
- f the intersection points (left to right).
50
Scan-Line Algorithm
ET+ AET Example (AET is given for scanlines 9 & 10): AET:
Scanlines 9: 10:
ET:
Note that this AET example is general, and don’t handle special cases (such as the intersection with edge DC, that according to our rules should be taken as x=12 rather than 13).
51
Scan-Line Algorithm steps
Initialize the ET. Set ‘y’ (the current scanline) to the first non-empty
entry in the ET.
Repeat until the AET and ET are empty:
Move new edges from ET to AET: take all edges in entry y of
ET (recall that these edges has ymin = = y).
Update the x coordinate (intersection point with current
scnline) of each edge in the AET.
Re-sort the AET list, if necessary. Fill interior spans according to the edges on the AET list. Remove from AET those edges with ymax = = y (will not
intersect the next scanline).
Increment y by 1 (move to next scanline).
52
Scan-Line Algorithm
Exercise: Run the algorithm to fill the polygon below:
1 2 3 4 5 6 7 8 1 2 3 4 5 6 1 2 3 4 5 6 7 8 1 2 3 4 5 6