1 Rasterization and Clipping Computer Graphics Course 2006
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 of the specific domain of the clipping and rasterization. � The constants hidden in O(n) algorithms are what all this is about. 2
Today’s agenda � So what is clipping? rasterization? � Lets start with the line clipping 3
Line Clipping Algorithms � Goal: avoid drawing primitives that are outside the viewing window. � The most common case: clipping line segments against a rectangular window 4
Line Clipping Algorithms � Three cases: � Segment is entirely inside the window - Accept � Segment is entirely outside the window - Reject � Segment intersects the boundary - Clip 5
Point Classification � How can we tell if a point is inside a rectangular window? y max ≤ ≤ ≤ ≤ x x x x x x min min max max ≤ ≤ ≤ ≤ y y y y y y min max min max y min x min x max 6
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 outside, line segment must be split. � What happens when both endpoints are outside? In what situation can we claim for a trivial reject? 7
Cohen-Sutherland Algorithm � Assign a 4-digit binary outcode to each of the 9 regions defined by the window: 1001 1000 1010 y max 0001 0000 0010 y min 0101 0100 0110 x min x max 8
Cohen-Sutherland Algorithm 1001 1000 1010 bit condition bit condition y max 1 y > y y max 1 y > max 0001 0000 0010 2 y < y y min 2 y < min y min 3 x > x x max 3 x > max 0101 0100 0110 4 x < x x min 4 x < min x min x max 9
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 on the remaining part. � What edge should be intersected ? 10
Example D C y max B A Outcode(A) = 0000 ) = 0000 Outcode(A y min Outcode(D) = 1001 ) = 1001 Outcode(D No trivial accept/reject No trivial accept/reject x min x max Clip (A,D) with y = y = y y max , splitting it into (A,B) and (B,D) Clip (A,D) with max , splitting it into (A,B) and (B,D) Reject (B,D) Reject (B,D) Proceed with (A,B) Proceed with (A,B) 11
Example E y max D C Outcode(A) = 0100 ) = 0100 Outcode(A B y min Outcode(E) = 1010 ) = 1010 Outcode(E A No trivial accept/reject No trivial accept/reject x min x max Clip (A,E) with y = y = y y max , splitting it into (A,D) and (D,E) Clip (A,E) with max , splitting it into (A,D) and (D,E) Reject (D,E) Reject (D,E) Proceed with (A,D) Proceed with (A,D) 12
Example y max D C Outcode(A) = 0100 ) = 0100 Outcode(A B y min Outcode(D) = 0010 ) = 0010 Outcode(D A No trivial accept/reject No trivial accept/reject x min x max Clip (A,D) with y = y = y y min , splitting it into (A,B) and (B,D) Clip (A,D) with min , splitting it into (A,B) and (B,D) Reject (A,B) Reject (A,B) Proceed with (B,D) Proceed with (B,D) 13
Example y max D C Outcode(B) = 0000 ) = 0000 Outcode(B B y min Outcode(D) = 0010 ) = 0010 Outcode(D No trivial accept/reject No trivial accept/reject x min x max Clip (B,D) with x = x = x x max , splitting it into (B,C) and (C,D) Clip (B,D) with max , splitting it into (B,C) and (C,D) Reject (C,D) Reject (C,D) Proceed with (B,C) Proceed with (B,C) 14
Parametric Line Clipping (Cyrus-Beck Algorithm) � Represent line segment parametrically as = + − = + = + − = + P ( t ) P ( P P ) t P Dt P ( t ) P ( P P ) t P Dt 0 1 0 0 0 1 0 0 � Solve the equation N N N ( ) ( ) ⋅ − = ⋅ − = N P ( t ) E 0 P 1 P ( t ) E 0 P 1 N ( ) ( ) ⇔ ⋅ + − = ⇔ ⋅ + − = N P Dt E 0 P Dt E 0 0 0 ( ) ( ) ⇔ ⋅ − + ⋅ = ⇔ ⋅ − + ⋅ = N P E N Dt 0 N P E N Dt 0 0 0 ( ) ( ) ⋅ − ⋅ − N P E P 0 N P E P = = 0 E 0 E 0 t t − ⋅ − ⋅ N D N D 15
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. N4 p0 E4 E1 E3 N3 N1 E2 p1 N2 16
Intersection Classification (Cyrus-Beck Algorithm) ( ) ⇒ ( ) ⇒ ⋅ < ⋅ < � N i D 0 N i D 0 The segment is potentially entering (PE) the clip region. (remember D=P1-P0) ( ) ⇒ ( ) ⇒ ⋅ > � ⋅ > The segment is potentially leaving N i D 0 N i D 0 (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. 17
Intersection Classification (Cyrus-Beck Algorithm) � Lets consider several cases: � t1>t2 - means that on the line N4 P1-P2 we have first left and PE p0 then entered the clipping E4 region. As in PE E1 � (But this is not the case as in) E3 N3 N1 � Such case should be clipped completely. PE PL PE PL � Another case is when the E2 PL segment is parallel to on of p1 N2 the clipping edges, as in PL PE 18
Cyrus-Beck Algorithm � t1=-oo t2 = oo � For each edge i with normal N i and point E i compute t. ⋅ ⋅ = = ⋅ ⋅ − − > > � If return NIL else ( N D 0 and N ( P E ) 0 ) ( N D 0 and N ( P E ) 0 ) i i 0 / 1 i i i 0 / 1 i continue ; ( ) ( ) ⋅ D > � If t2=min(t,t2) else t1=max(t,t1) ⋅ D > N i 0 N i 0 � If (t2 < t1 or t1>1 or t2<0) then return NIL � t1=max(t1,0) t2=min(t2,1) � Return [t1,t2] 19
Today’s agenda � Now lets see how the rasterization of the simple line is done 20
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. 21
Rasterization � So how would you draw a simple line? 22
An Incremental Algorithm � Input: endpoints and ( x 2 y , ) ( x 1 y , ) 2 1 ( ) − y y = � Compute 2 1 a ( ) − x x 2 1 = + − = + � Line equation ( ) y ax y ax ax b 1 1 � for x := x1 to x2 � y := Round( ax + b ) � DrawPixel( x, y ) 23
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? 24
The Midpoint Algorithm (Bresenham) � Assumption: line has slope ‘a' between 0 + + + + and 1 ( x 1 , y 1 ) ( x 1 , y 1 ) 1 1 + + + + ( x 1 , y ) ( x 1 , y ) 2 2 x + x + ( ( x , , y ) ) x y ( 1 , y ) ( 1 , y ) � Idea: choose the next pixel by checking if the midpoint is above or below the line. 25
The Midpoint Algorithm � Each line can be written using the line equation: = + + = = + + = F ( x , y ) Ax By C 0 F ( x , y ) Ax By C 0 � 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 26
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 ) NE + + + + ( x 1 , y 1 ) ( x 1 , y 1 ) 1 1 = + + = + + M ( x 1 , y ) M ( x 1 , y ) 2 2 x + x + ( x , y ) ( x , y ) E ( 1 , y ) ( 1 , y ) 27
The Midpoint Algorithm 1 = + + + + � For each pixel compute: d A ( x 1 ) B ( y ) C 2 � Make a decision based on sign of d � Incrementally update M and d NE + + + + ( x 1 , y 1 ) ( x 1 , y 1 ) 1 1 = = + + + + M ( x 1 , y ) M ( x 1 , y ) 2 2 x + x + ( x , y ) ( x , y ) ( 1 , y ) E ( 1 , y ) 28
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 29
The Midpoint Algorithm � What should the initial value of d be? + + = + + + + 1 1 F ( x 1 , y ) A ( x 1 ) B ( y ) C 1 1 1 1 2 2 = + + + + B Ax By C ( A ) 1 1 2 = + + ( , ) ( B ) F x y A 1 1 2 = + B ( A ) 2 � To avoid division, we’ll multiply everything by 2, and result with the following algorithm: 30
Recommend
More recommend