Computer Graphics Lecture 14 CLIPPING I Taqdees A

  • Slides: 57
Download presentation
Computer Graphics Lecture 14 CLIPPING I Taqdees A. Siddiqi cs 602@vu. edu. pk

Computer Graphics Lecture 14 CLIPPING I Taqdees A. Siddiqi cs 602@vu. edu. pk

CLIPPING I Taqdees A. Siddiqi cs 602@vu. edu. pk

CLIPPING I Taqdees A. Siddiqi cs 602@vu. edu. pk

Concept Restricting the effect of graphics primitives to a subregion of the canvas Protecting

Concept Restricting the effect of graphics primitives to a subregion of the canvas Protecting other portions of the canvas

All primitives are clipped to the boundaries of this clipping rectangle; Primitives lying outside

All primitives are clipped to the boundaries of this clipping rectangle; Primitives lying outside the clip rectangle are not drawn.

The default clipping rectangle is the full canvas (the screen); We cannot see any

The default clipping rectangle is the full canvas (the screen); We cannot see any graphics primitives outside the screen

Example This is a simple example of line clipping: the display window is the

Example This is a simple example of line clipping: the display window is the canvas and also the default clipping rectangle, thus all line segments inside the canvas are drawn.

Point Clipping xmin <= xmax ymin <= ymax

Point Clipping xmin <= xmax ymin <= ymax

Line Clipping of lines against rectangles

Line Clipping of lines against rectangles

Clipping Individual Points Simpler problem Xmin < Xmax And Ymin < Ymax

Clipping Individual Points Simpler problem Xmin < Xmax And Ymin < Ymax

Trivial Accept – save a line with both endpoints inside all clipping boundaries

Trivial Accept – save a line with both endpoints inside all clipping boundaries

Trivial Reject – discard a line with both endpoints outside the clipping boundaries

Trivial Reject – discard a line with both endpoints outside the clipping boundaries

For all other lines – compute intersections of line with clipping boundaries

For all other lines – compute intersections of line with clipping boundaries

Parametric Representation Line x = x 1 + u (x 2 - x 1)

Parametric Representation Line x = x 1 + u (x 2 - x 1) y = y 1 + u (y 2 - y 1) and 0 <= u <= 1

If 0 <= u <= 1 then the line does enter the interior of

If 0 <= u <= 1 then the line does enter the interior of the window at that boundary otherwise, the line does not enter the interior of the window at that boundary.

Solve Simultaneous Equations Need to consider only endpoints of a line: If both endpoints

Solve Simultaneous Equations Need to consider only endpoints of a line: If both endpoints lie inside the clip rectangle; it can be trivially accepted.

If one endpoint lies inside and one outside, the line intersects the clip rectangle

If one endpoint lies inside and one outside, the line intersects the clip rectangle and we must compute the intersection point.

If both endpoints are outside the clip rectangle, we need to perform further calculations

If both endpoints are outside the clip rectangle, we need to perform further calculations to determine whethere any intersections.

The Brute Force Approach Intersect that line with each of the four clip rectangle

The Brute Force Approach Intersect that line with each of the four clip rectangle edges. if so, the line cuts the clip rectangle and is partially inside.

For each line and clip-rectangle edge, we therefore take the two mathematically infinite lines

For each line and clip-rectangle edge, we therefore take the two mathematically infinite lines that contain them and intersect them.

Test whether this intersection point is "interior" -- that is, whether it lies within

Test whether this intersection point is "interior" -- that is, whether it lies within both the clip rectangle edge and the line.

If so, there is an intersection with the clip rectangle.

If so, there is an intersection with the clip rectangle.

Cohen Sutherland Line Clipping Algorithm More efficient; Performs initial tests on a line to

Cohen Sutherland Line Clipping Algorithm More efficient; Performs initial tests on a line to determine whether intersection calculations can be avoided.

End-point pairs are checked using the out-code. If not divided into two segments at

End-point pairs are checked using the out-code. If not divided into two segments at a clip edge, iteratively clipped by testing trivialacceptance or trivial-rejection.

Trivial Accept / Reject Test

Trivial Accept / Reject Test

Each bit in the out-code is set to either 1 (true) or 0 (false);

Each bit in the out-code is set to either 1 (true) or 0 (false); The 4 bits in the code correspond to the following conditions:

Bit 1: outside half-plane of top edge, above top edge Y > Ymax Bit

Bit 1: outside half-plane of top edge, above top edge Y > Ymax Bit 2: outside half-plane of bottom edge, below bottom edge Y < Ymin

Bit 3: outside half-plane of right edge, to the right of right edge X

Bit 3: outside half-plane of right edge, to the right of right edge X > Xmax Bit 4: outside half-plane of left edge, to the left of left edge X < Xmin

Conclusion Algorithm is efficient when out-code testing can be done cheaply

Conclusion Algorithm is efficient when out-code testing can be done cheaply

Liang-Barsky Algorithm Faster line clippers based on analysis of the parametric equation of a

Liang-Barsky Algorithm Faster line clippers based on analysis of the parametric equation of a line segment:

x = x 1 + u x y = y 1 + u y,

x = x 1 + u x y = y 1 + u y, where 0 <= u <= 1, x = x 2 - x 1 and y = y 2 - y 1

Following the Liang-Barsky approach, we first write the point-clipping in a parameteric way: xmin

Following the Liang-Barsky approach, we first write the point-clipping in a parameteric way: xmin <= x 1 + u x <= xmax ymin <= y 1 + u y <= ymax

These four inequalities can be expressed as u * pk <= qk, for k

These four inequalities can be expressed as u * pk <= qk, for k = 1, 2, 3, 4

Where parameters p and q are defined as: p 1 = - x, q

Where parameters p and q are defined as: p 1 = - x, q 1 = x 1 - xmin p 2 = - x, q 2 = xmax - x 1 p 3 = - y, q 3 = y 1 - ymin p 4 = - y, q 4 = ymax - y 1

Any line that is parallel to one of the clipping boundaries having pk =

Any line that is parallel to one of the clipping boundaries having pk = 0 If, for that value of k, we also find qk >= 0, the line is inside the parallel clipping boundary.

When pk < 0, the infinite extension of the line proceeds from outside to

When pk < 0, the infinite extension of the line proceeds from outside to the inside of the infinite extension of the particular clipping boundary.

If pk > 0, the line proceeds from the inside to the outside.

If pk > 0, the line proceeds from the inside to the outside.

For a nonzero value of pk, we can calculate the value of u that

For a nonzero value of pk, we can calculate the value of u that corresponds to the point where the infinitely extended line intersects the extension of boundary k as: u = qk / pk

For each line, we can calculate values for parameters u 1 and u 2

For each line, we can calculate values for parameters u 1 and u 2 which define that part of the line which lies within the clip rectangle.

The value of u 1 is determined by looking at the rectangle edges for

The value of u 1 is determined by looking at the rectangle edges for which the line proceeds from the outer-side to the inner-side (p < 0). For these edges we calculate rk = qk / pk.

The value of u 1 is taken as the largest of the set consisting

The value of u 1 is taken as the largest of the set consisting of 0 and the various values of r.

The value of u 2 is determined by examining the boundaries for which the

The value of u 2 is determined by examining the boundaries for which the line proceeds from inside to outside (p > 0).

Algorithm Line intersection parameters are initialized to the values: u 1 = 0 and

Algorithm Line intersection parameters are initialized to the values: u 1 = 0 and u 2 = 1.

For each clipping boundary, the appropriate values for p and q are calculated and

For each clipping boundary, the appropriate values for p and q are calculated and used to determine whether the line can be rejected or the intersection parameters are to be adjusted.

A value of rk is calculated for each of these boundaries and the value

A value of rk is calculated for each of these boundaries and the value of u 2 is the minimum of the set consisting of 1 and the calculated r values.

If u 1 > u 2, the line is completely outside the clip window

If u 1 > u 2, the line is completely outside the clip window and it can be rejected.

Otherwise, the end points of the clipped line are calculated from the two values

Otherwise, the end points of the clipped line are calculated from the two values of parameter u.

When p < 0, the parameter r is used to update u 1; When

When p < 0, the parameter r is used to update u 1; When p > 0, the parameter r is used to update u 2.

If updating u 1 or u 2 results in u 1 > u 2,

If updating u 1 or u 2 results in u 1 > u 2, we reject the line.

Otherwise, we update the appropriate u parameter only if the new value results in

Otherwise, we update the appropriate u parameter only if the new value results in a shortening of the line.

When p = 0 and q < 0, we can discard the line since

When p = 0 and q < 0, we can discard the line since it is parallel to and outside of this boundary.

If the line has not been rejected after all four values of p and

If the line has not been rejected after all four values of p and q have been tested, the endpoints of the clipped line are determined from values of u 1 and u 2.

Conclusion More efficient; intersection calculations are reduced.

Conclusion More efficient; intersection calculations are reduced.

Each update of parameters u 1 and u 2 requires only one division; and

Each update of parameters u 1 and u 2 requires only one division; and window intersections of the line are computed only once, when the final values of u 1 and u 2 are computed.

Cohen-Sutherland algorithm can repeatedly calculate intersections along a line path, even though the line

Cohen-Sutherland algorithm can repeatedly calculate intersections along a line path, even though the line may be completely outside the clip window, and, each intersection calculation requires both a division and a multiplication.

Computer Graphics Lecture 14

Computer Graphics Lecture 14