11

Click here to load reader

Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

Embed Size (px)

Citation preview

Page 1: Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

CS527 Computer Graphics 1

Note 4: Line Drawing / Line & Area Clipping Reference: textbook 7.1-7.5, 7.8-7.9 1. Introduction Line drawing is our first adventure into the area of scan conversion. The need for scan conversion, or rasterization, techniques is a direct result of scanning nature of raster displays (thus the names). Remember the following tasks of interest: Modeling -> Rendering -> Displaying The topics of the next few lectures have to do with rendering / displaying. Lesson: (1) line drawing details (2) optimization problem 2. Line Drawing On raster system, lines are plotted with pixels, and step size in the horizontal and vertical directions are constrained by pixel separations. That is, we must �sample� a line at discrete positions and determine the nearest pixels to the line at each sampled position. First Attempt: Straightforward (Slope-Intercept Algorithm) Given 2 points: (x0, y0), (x1, y1), we can get the gradient m, and intercept b of the line passing through the points. Algorithm:

m=(y1 � y0)/(x1 � x0); b = y0 � m x0 ;

setPixel( x0, y0); while ( x0 != x1 ) {

x0 ++; y0 = round (m*x0 + b); setPixel (x0, y0); } Note: (1) slope < 1 (2) rounding is not inexpensive

Page 2: Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

CS527 Computer Graphics 2

Second Attempt: Digital Differential Analyzer (DDA) Idea: Use previous value to compute the next value yk+1 = xk+1 m + b = (xk + 1) m + b = xk m + b + m = yk + m Algorithm:

m=(y1 � y0)/(x1 � x0); b = y0 � m x0 ; setPixel( x0, y0); // offset for rounding t = 0.5 + y0;

while ( x0 != x1 ) { x0 ++; t += m; setPixel (x0, int(t)); }

We can further improve the above on the following points:

- remove floating point calculation in favour of integer operation (t) - remove single divide operation - normalize test for zeros

This improvement is our Bresenham�s algorithm

Page 3: Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

CS527 Computer Graphics 3

Third Attempt: Bresenham�s Algorithm (midpoint algorithm) Assuming we have determined that the pixel at (xk, yk ) is to be displayed, we next need to decide which pixel to plot in column xk+1. Our choices are the pixels at positions (xk+ 1, yk) and (xk + 1, yk + 1). At sampling position xk+1, we label vertical pixel separations from the mathematical line path as d1 and d2. The y coordinate on the mathematical line at pixel column position xk+1 is calculated as y = m(xk + 1) + b.

yk

y

yk + 1

xk+1

d1

d2

d1= y - yk

= m ( xk + 1) + b - yk

d2 = (yk + 1) - y = yk + 1 � m (xk + 1) � b

d1 � d2 = 2 m (xk + 1) � 2 yk + 2 b - 1

A decision parameter pk for the kth step in the line algorithm can be obtained by rearranging the equation so that it involves only integer calculations. We accomplish this by substituting m = ∆y / ∆x, where ∆y and ∆x are the vertical and horizontal separations of the endpoint positions, and defining: pk = ∆x (d1 � d2) = 2 ∆y . xk - 2 ∆x . yk + 2 ∆y + (2b � 1) ∆x The sign of pk is the same as the sign of d1 � d2, since ∆x > 0 for our example. We can get p0 = 2 ∆y . x0 � 2 ∆x . y0 + 2 ∆y + (2b - 1) ∆x = 2 ∆y . x0 � 2 ∆x . y0 + 2 ∆y + (2 y0 � 2 x0 . ∆y/∆x � 1) ∆x = 2 ∆y - ∆x Also, pk+1 = 2 ∆y ( xk+1 + 1) � 2 yk+1 ∆x + (2b � 1) ∆x so, pk+1 � pk = 2 ∆y � 2 ∆x (yk+1 � yk) where (yk+1 � yk) is either 0 or 1, depending on the sign of parameter pk.

Bresenham�s Line-Drawing Algorithm for |m| < 1 Step 1: input the two line endpoints and store the left endpoint in (x0, y0)

Page 4: Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

CS527 Computer Graphics 4

Step 2: load (x0, y0) into the frame buffer; that is, plot the first point. Step 3: Calculate constants, ∆x, ∆y, 2 ∆y, and 2 ∆y � 2 ∆x, and obtain the starting value for the decision parameter as p0 = 2 ∆y - ∆x Step 4. At each xk along the line, starting at k = 0, perform the following test: If pk < 0, the next point to plot is (xk + 1, yk) and pk+1 = pk + 2 ∆y (as yk+1 = yk) otherwise, the next point to plot is (xk + 1, yk + 1) and pk+1 = pk + 2 ∆y - 2 ∆x (as yk+1 = yk + 1) Step 5: repeat step 4 ∆x times. Note: (1) Bresenham�s circle algorithm

(2) Many books would have you believe that the development of line drawing algorithms ended with Bresenham�s famous algorithm, But there has been some significant work of 2-step algorithm by Xiaolin Wu. It takes the interesting approach of treating line drawing as a automaton, or finite state machine.

Page 5: Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

CS527 Computer Graphics 5

Clipping: 1. Introduction Transformation sequence: (1) modeling coordinate ( into Modeling Transformation ) (2) object/world coordinate ( into Viewing Transformation ) (3) eye/camera coordinate ( into Projection Transformation ) (4) clip coordinate ( into Normalization ) (5) normalized device coordinate ( into viewport transformation ) (6) raster (or screen) coordinate After the mapping of the view volume (a frustum for perspective views and parallelepiped for parallel views) to a centered cube with sides of length 2. The points are now represented in clip coordinates, i.e. things not within the cube of 2x2x2 are to be out. 2. Basic Principles on Clipping Clipping: the process of determining which primitives, and parts of primitives, fit within the clipping volume defined by the application program. Primitives that fit within the specified view volume pass through the clipper, or are accepted. Primitives that cannot appear on the display are eliminated, or rejected or culled. Primitives that are only partially within the view volume must be clipped so that any part lying outside the volume is removed.

Have following possibilities: - line completely inside window, completely outside, intersects window edges Clipping Algorithm must: - identify and save lines completely inside (e.g. AB) - identify and discard lines completely outside (e.g. GH, EF) - find intersections�and save inside line sections (e.g. IJ, CD) Trivial solution: - solve the line equations to find out where it intersects the window boundary

A

B

G

H

J

I

D

EC

F

A

B

J�

I�D�

C

Page 6: Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

CS527 Computer Graphics 6

3. Cohen-Sutherland Line-Clipping Algorithm

Idea: to rapidly identify inside and outside lines assign a four-bit region code to line endpoints

• design to identify efficiently those lines that can be trivially accepted or rejected. • especially efficient in cases where most lines can be trivially accepted or rejected. Define a 4-bit outcode bits are set to true or 1 as follows:

bit 1 => if point above of window bit 2 => if point below of window bit 3 => if point right of window bit 4 => if point left of window

Thus: bit 1 is the sign bit of (ymax - y), or bit_1 = y > ymax , bit 2 is the sign bit of ( y - ymin ), or bit_2 = y < ymin , bit 3 is the sign bit of (xmax - x), or bit_3 = x > xmax , bit 4 is the sign bit of ( x - xmin ), or bit_4 = x < xmin ,

3 cases to consider: 1. A line trivially accepted if outcodes of both endpoints are 0000 2. A line can be trivially rejected if

not case 1, and the logical AND of outcodes of endpoints is NOT 0000 . ( line is entirely beyond at least ONE boundary line )

3. Otherwise further processing is needed since line cannot be trrejected.

0000

1000

0100

0001

1001

0101

xmin xma

x x x x

bit: 1 2 3 4

.

ivially accepted nor

0010

1010

0110 x

ymax

ymin

Page 7: Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

CS527 Computer Graphics 7

Example: 1. AB is trivially accepted.

2. CD is trvially rejected

D: 0110 C: 0101

AND = 0100 ⇒ entire line is below ymin 3. EF needs further processing:

F: 1000 E: 0001

AND = 0000 clip against upper window boundary.

E� = [ xE + (ymax � yE)/ (yF - yBoth EE� and E�F are trivially rejected.

4. GH : clipped against upper boundary to get H�

H�H is trivially rejected, GH� needs further processing,

clipped against left boundary to get G� GG� is trivially rejected, G�H� is trivially accepted.

G� B

E 0001

F H H�

0010

1010

G

1001 E�

E) • (xF � xE ) , ymax ]

A 0000

0100 0110 D

0101 C

Page 8: Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

CS527 Computer Graphics 8

enum { TOP=0x8, BOTTOM=0x4, RIGHT=0x2, LEFT=0x1); Algorithm bealoon accept=FALSE, done=FALSE; outcode0 = CompOutCode(x0, y0, xmin, xmax, ymin, ymax); outcode1 = CompOutCode(x1, y1, xmin, xmax, ymin, ymax); do { // trivial cases if ( !(outcode0 | outcode1) ) { accept=TRUE; done=TRUE; } else if ( outcode0 & outcode1 ) done = TRUE; else { // complicated ones // 1. pick one endpoint that is outside outcodeOut = outcode0 ? outcode0 : outcode1; // 2. find intersection with TOP, BOTTOM, RIGHT, LEFT using the line equation if (outcodeOut & TOP) { // from m=(y1 � y0)/(x1 � x0), b=y0 � m x0; x = x0 + (x1 � x0) * (ymax � y0) / ( y1 � y0 ); y = ymax; } else if (outcodeOut & BOTTOM) { x = x0 + (x1 � x0) * (ymin � y0) / ( y1 � y0 ); y = ymin; } else if (outcodeOut & RIGHT) { y = y0 + (y1 � y0) * (xmax � x0) / ( x1 � x0 ); x = xmax; } else { // if (outcodeOut & LEFT) y = y0 + (y1 � y0) * (xmin � x0) / ( x1 � x0 ); x = xmin; } // 3. move outside point to intersection point to clip, and get ready for the next pass if (outcodeOut == outcode0 ) { x0 = x; y0 = y; outcode0 = CompuOutcode(x0, y0, xmin, xmax, ymin, ymax); } else { x1 = x; y1 = y; outcode1 = CompuOutcode(x1, y1, xmin, xmax, ymin, ymax); } } while (done == FALSE); if (accept) drawLine(x0, y0, x1, y1); } outcode CompOutCode(double x, double y,double xmin, double xmax, double ymin, double ymax) { outcode code = 0; if (y > ymax) code |= TOP else if (y < ymin) code |= BOTTOM; if (x > xmax) code |= RIGHT else if (x < xmin) code |= LEFT; return code; } Extension to 3D: use 6-bit outcode�the additional 2 bits are set if the point lies either in front of or behind the clipping volume. The testing strategy is virtually identical for the two- and three-dimensional cases.

Page 9: Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

CS527 Computer Graphics 9

4. Sutherland-Hodgman Polygon-Clipping Algorithm We cannot use line-clipping procedures to produce an enclosed area. Clipping individual lines of an area leads to:

Polygon clipping: process vertices in order around area boundary. Clipped polygon then defined as the new sequence of vertices input: <1, 2, 3> output: <1�, 2�, 3�, 4�, 5�, 6�> where 12, 23, 31 are edges

1

2

3

6� 1�

5� 4�

3�

2�

Note: 1. We can do: for each pixel in the polygon, decide if it is within frame-buffer boundary.

This is okay, but too inefficient. This is called scissoring (clipping on the fly during scan conversion)�scan convert entire primitive but write only visible portions.

2. One polygon may be clipped into many polygons.

Idea: process polygon against each of the 4 boundaries in turn (e.g. left, right, bottom, top) (similar spirit as the line clipping algorithm)

1

6� 1�

5� 4�

3�

2�

1

3

2

2�

3�

5� 4�

1

3

2�

3�

LEFT RIGHT BOTTOM TOP

For each window boundary, could process all polygon vertices to generate output vertex set for next window boundary.

Page 10: Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

CS527 Computer Graphics 10

Have 4 possible cases when processing from one line endpoint to the other. To illustrate, consider left boundary of window:

out to insave 1�, 2

in to insave 3

in to outsave 4�

out to outsave none

1 21�

2

3 344�

4

1

The result of clipping the object 1234 using the left boundary is 1�234�. This new set of vertices is passed to the boundary clipping (such as right bundary) etc. Note: Identify vertex positions with region codes.

12

3

I1

I4

I2

I3

left right bottom top IN

OUT

Simplified processing sequence:

1 2 3

1 I1 { I2, 3 }

1 I1 { I2, 3 }

{ I4, I2 } 3 I3

{ I4, I2 } 3 I3

From the 1st column to the second column: first we consider edge 12, which is from inside to outside, so keep intersection I1 second, we consider edge 23, which is from outside to inside, so keep intersection I2 and 3 third, we consider edge 31, which is from inside to inside, so keep 1. Final result after the top clipping: join vertices: I4 -> I2 -> 3 ->I3 ->I4

Note: Sutherland-Hodgman clips convex polygon correctly, but concave polygons may be displayed with extraneous lines:

Page 11: Note 4: Line Drawing / Line & Area Clipping - wmich.eduyang/teach/cs5270/oldnotes/note04.pdfCS527 Computer Graphics 1 Note 4: Line Drawing / Line & Area Clipping Reference: textbook

CS527 Computer Graphics 11

11� 2

34

4�

55�

66�

1� 2

34�5�

6�5

This occurs because clipped concave polygons may have multiple areas. But algorithm only generates one output vertex list.ie. in the above example, point 1� joined to point 6� along left window edge. To display concave polygons correctly: - modify sutherland-hodgman - use a more general polygon clipper. To accommodate concave polygons in Sutherland-Hodgman approach, check final vertex list for multiple vertex points along any window boundary. If multiple points are present, break single area into two or more separate areas. I.e. �connect� points along this line in pairs (careful�special case exists). 5. Clipping of Other Primitives Technique 1: Bounding Boxes (or extent) is the smallest rectangle, aligned with the window, that contains the polygon. Can avoid detailed clipping for trivial cases of bounding volume outside window or inside window. Need detailed clipping for partially inside window.

Technique 2 (for curves, surfaces) - difficult to find general algorithms for processing curves/surfaces

• need numerical computation on intersection, and number of intersection - avoid the above problem by approximating curves with line segments and surfaces

with planar polygons.