Anda di halaman 1dari 73

CLIPPING ALGORITHMS

Window vs. Viewport

Window

World-coordinate area selected for display What is to be viewed


Area on the display device to which a window is mapped Where it is to be displayed

Viewport

Viewing in 2D - Viewport
250

45

Window in world coordinates.

250 250 xx 250 250

Viewport in
Device coords

Pixels. Pixels.

2-D Viewing Pipeline

The mapping of a 2D world-coordinate scene description to device coordinates is called a 2D viewing transformation or window-toviewport transformation

Clipping Window

The clipping window selects what we want to see; the viewport indicates where it is to be viewed on the output device.

Window to Viewport Transformation

To transform the world-coordinate point into the same relative position within the viewport, we require that

Clipping

Any procedure that identifies those portions of a picture that are either inside or outside of a specified region of space is referred to as clipping.

The region against which object is to clipped is called clip window.

It include extracting part of defined scene for


o o

Viewing Identifying visible surfaces in 3D views

o
o

Displaying a multi window environment


Drawing and painting operations for copying, moving, erasing or duplicating

2-D Clipping

Point clipping Line clipping


o o o

Cohen-Sutherland Nicholl Lee - Nicholl Liang-Barsky

Fill-area clipping
o o

Sutherland-Hodgeman Weiler-Atherton

Curve clipping Text clipping

Point Clipping

Is Point(x,y) Inside the Clip Window?


Inside = (x>=wx1) && (x<=wx2) && (y>=wy1) && (y<=wy2);

wy2

(x, y)

wy1 wx1

wx2

Point Clipping

Assume that clip window is rectangle. Save a point P(x,y) for display if xwmin x xwmax ywmin y ywmax

Edges of clip window (xwmin, xwmax, ywmin, ywmax) can be


either world coordinate window boundaries or viewport boundaries.

The point is clipped if not satisfy the above condition.

Line Clipping

Find the Part of a Line Inside the Clip Window


P7 P1

P4
P3 P2

P8

P6
P5 P9 Before Clipping

P10

Line Clipping

Find the Part of a Line Inside the Clip Window


P7

P4
P3

P8

P6
P5 After Clipping

Cohen-Sutherland Line Clipping I

Split plane into 9 regions Assign each a 4 bit value

1001

1000

1010

(above,below,right,left)

Assign the vertices of line 4 bit value

0001

0000
Window

0010

0101

0100

0110
14

Trivially accepted if (both region codes = 0000) Trivially rejected

if(AND of region codes 0000)


Otherwise, divide line into two segments Intersection of line with window edge is determined and trivial accept/ reject test is repeated. The process is repeated until line is accepted.

1. Given a line segment with endpoint P1 = (x1, y1) and P2 = (x2, y2) 2. Compute the 4 bit code for each endpoint. If both codes are 0000 (bitwise OR of codes yields 0000) line lies completely inside the window: pass

endpoints to draw routine.


If both codes have a 1 in same bit position (bitwise AND of codes is not 0000) the line lies outside the window. It can be trivially rejected. 3. If a line cannot be trivially accepted or rejected, at least one of two endpoints must lie outside window and line segment crosses a window edge.

This line must be clipped at window edge before being passed to drawing routine. 4. Examine one of the endpoints say P1 = (x1, y1), read P1s 4 bit code in order Left to Right, Bottom to Up. 5. When a set bit (1) is found, compute intersection I of

corresponding window edge with line from P1 to P2. Replace P1 with I and repeat the algorithm.

Cohen-Sutherland Line Clipping

Use Simple Tests to Classify Easy Cases First


P7 P1

P4
P3 P2

P8

P6
P5 P9

P10

Cohen-Sutherland Line Clipping

Classify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0) P7 1000 1001 1010 Bit 4 P1

P4 0001
P2 0101 P5 Bit 1 P3

0000 P6
0100 P9

P8 0010

P10 0110 Bit 2

Bit 3

Cohen-Sutherland Line Clipping

Classify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0) P7 1000 1001 1010 Bit 4 P1

P4 0001
P2 0101 P5 Bit 1 P3

0000 P6
0100 P9

P8 0010

P10 0110 Bit 2

Bit 3

Cohen-Sutherland Line Clipping

Classify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0) P7 1000 1001 1010 Bit 4

P4 0001
P3

0000 P6

P8 0010

P10 P9 0110 Bit 2

Bit 3

0101 P5 Bit 1

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010

Bit 4

P4 0001
P3

0000 P6

P8 0010

P10 P9 0110 Bit 2

Bit 3

0101 P5 Bit 1

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010

Bit 4

P4 0001
P3

0000 P6

P8 0010

P10 P9 0110 Bit 2

Bit 3

0101 P5 Bit 1

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010

Bit 4

P4 0001
P3

0000 P6

P8 0010

P10 P9 0110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010

Bit 4

P4 0001
P3

0000 P6

P8 0010

P10 P9 0110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010

Bit 4

P4 0001
P3

0000 P6

P8 0010

P10 P9 0110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly P7 1000 1001 1010

Bit 4

P4 0001
P3

0000 P6

P8 0010

P10 P9 0110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P

Bit 4

P4 0001
P3

0000 P6

P8 0010

P10 P9 0110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7

Bit 4

P4 0001
P3

0000 P6

P8 0010

P10 P9 0110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7

Bit 4

P4 0001
P3

P8 0010

0000 P6

P10 P9 0110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7

Bit 4

P4 0001
P3

P8 0010

0000 P6

P10 P9 0110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7

Bit 4

P4 0001
P3

P8 0010

0000 P6

P10 P9 0110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7

Bit 4

P4 0001
P3

P8 0010

0000 P6

P10 P9 0110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P

Bit 4

P4 0001
P3

P8 0010

0000 P6

P10 P90110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7

Bit 4

P4 0001
P3

P8 0010

0000 P6

P10 P90110 Bit 2

P5 0101 Bit 1

Bit 3

0100

Cohen-Sutherland Line Clipping

Compute Intersections with Window Boundary for Lines That Cant be Classified Quickly 1001 1000 1010 P7

Bit 4

P4 0001
P3

P8 0010

0000 P6

P5 0101 Bit 1

Bit 3 0100 Bit 2 0110

Line Intersection and Clipping


For left window edge, Intersection point will be (xL, y)

y = m (xL x1 ) + y1
For right window edge, Intersection point will be (xR, y)

y = m (xR x1 ) + y1
For top window edge, Intersection point will be (x, yT)

x = x1 + (yT y1 ) / m
For bottom window edge, Intersection point will be (x, yB)

x = x1 + (yB y1 ) / m

Liang-Barsky Line Clipping

Parametric definition of a line:


x = x1 + ux y = y1 + uy x = (x2-x1), y = (y2-y1), 0<=u<=1

Goal: find range of u for which x and y both inside the viewing window

Liang-Barsky Line Clipping II

Mathematically:

xmin <= x1 + ux <= xmax ymin <= y1 + uy <= ymax u*(-x) <= (x1 xmin) u*(x) <= (xmax x1) u*(-y) <= (y1 ymin) u*(y) <= (ymax y1)

Rearranged

1: 2: 3: 4:

u*(pk) <= (qk)

k=1,2,3,4

Parameter p and q are defined as

p1 = - x p2 = x p3 = - y p4 = y

q1 = x1 xmin q2 = xmax x1 q3 = y1 ymin q4 = ymax y1

Observations
1)

If pk = 0, line is parallel to corresponding boundary


a)

If qk < 0, line is completely outside the boundary and can be eliminated If qk > 0, the line is inside the boundary and needs further consideration

b)

2)

If pk < 0, line proceeds from outside to inside of corresponding boundary line. If pk > 0, line proceeds from inside to outside of corresponding boundary line. If pk 0, calculate value of u as u = qk / pk

3)

4)

Algorithm

Rules:
1) pk = 0: the line is parallel to boundaries

If for that same k, qk < 0, its outside Otherwise its inside rk = qk/pk u1 = max(0, rk, u1) rk = qk/pk u2 = min(1, rk, u2)

2) pk < 0: the line proceeds from outside to inside


3) pk > 0: the line proceeds from inside to outside


4) If u1 > u2, the line is completely outside

Algorithm
a)

If pk = 0 and qk < 0, eliminate the line and stop, otherwise proceed to next step. For all k such that pk < 0, calculate rk = qk / pk. Let u1 be maximum of set containing 0 and various values of r. For all k such that pk > 0, calculate rk = qk / pk. Let u2 be minimum of set containing 1 and various values of r. If u1 > u2, eliminate line. It is completely outside the window, otherwise use u1 and u2 to calculate the endpoints of clipped line.

b)

c)

d)

Example
H (8,9) y I (-1,7) G (6,6) A (11,6) F (8,4)

B (11,10)
ymax = 8

E (2,3) J (11,1) x xmax = 9

ymin = 2

xmin = 1

Conclusion

More efficient than Cohen Sutherland.

Intersection calculations are reduced.


Each update of u1 and u2 requires only one division. Window intersections of line are computed only once when final value of u1 and u2 have

been computed.

Polygon Clipping

Find the Part of a Polygon Inside the Clip Window?

Before Clipping

Polygon Clipping

Find the Part of a Polygon Inside the Clip Window?

After Clipping

Sutherland-Hodgeman Polygon Clipping

Clip to Each Window Boundary One at a Time

Sutherland-Hodgeman Polygon Clipping

Clip to Each Window Boundary One at a Time

Sutherland-Hodgeman Polygon Clipping

Clip to Each Window Boundary One at a Time

Sutherland-Hodgeman Polygon Clipping

Clip to Each Window Boundary One at a Time

Sutherland-Hodgeman Polygon Clipping

Clip to Each Window Boundary One at a Time

Clipping to a Boundary

Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P1 Inside

P3
P4

P5

Outside

Clipping to a Boundary

Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P1 Inside

P3
P4

P5

Outside

Clipping to a Boundary

Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P1 Inside

P3
P4

P5

Outside

Clipping to a Boundary

Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P1 Inside

P3
P4

P5

Outside

Clipping to a Boundary

Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P P1 Inside

P3
P4

P5

Outside

Clipping to a Boundary

Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P P1 Inside

P3
P4

P5

Outside

Clipping to a Boundary

Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P P1 Inside

P3
P4

P5

Outside

Clipping to a Boundary

Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P P1 P Inside Outside

P3
P4

P5

Clipping to a Boundary

Do Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary P2 Window Boundary P P1 P Inside Outside

Polygon Clipping I

Note the difference between clipping lines and polygons:

NOTE!

Polygon Clipping II

Some difficulties:

Maintaining correct inside/outside Variable number of vertices Handle screen corners correctly

Sutherland-Hodgeman Polygon Clipping I

Simplify via separation Clip whole polygon against one edge


Repeat with output for other 3 edges Similar for 3D

You can create intermediate vertices that get thrown out

Sutherland-Hodgeman Polygon Clipping II

Example I

out in save new clip vert and ending vert

in in save ending vert

in out save new clip vert

out out save nothing

Sutherland-Hodgeman Polygon Clipping II

Example 2

Start

Left

Right

Bottom

Top

Sutherland-Hodgeman Polygon Clipping

Step-1: Initialize the i/p list & o/p list such that i/p list contains set of all consecutive vertices of the polygon & the o/p list is empty. Step-2: Against each window edge L,R,B,T perform the following steps:

If V1 and V2 are inside the window then save V2 in o/p list. If V1 and V2 are outside the window then no operation is performed. If V1 is inside and V2 is outside then find the intersection point at window edge and save the intersection point in the o/p list

If V1 is outside & V2 is inside then find the intersection point at the window edge and save the intersection point V2 in the o/p list.

Step-3: When the next edge is taken the o/p list becomes the i/p list for that edge. Step-4: the o/p list contains the set of vertices which forms the clipped polygon area.

Weiler-Atherton Polygon Clipping I

Problem with Sutherland-Hodgeman:

Concavities can end up linked

Weiler-Atherton creates separate polygons in such cases

Weiler-Atherton Polygon Clipping II

Example

add clip pt.


and end pt.

add end pt.

add clip pt.

follow clip edge until


found b) reach pt. already

cache old dir. a) new crossing

Weiler-Atherton Polygon Clipping III

Example (cont)

continue from
cached location

add clip pt.


and end pt.

add clip pt.


cache dir.

follow clip edge until


a) new crossing found b) reach pt. already added

Weiler-Atherton Polygon Clipping IV

Example (concluded)

continue from
cached location

nothing added
finished

Final result: Two unconnected polygons

Anda mungkin juga menyukai