Você está na página 1de 52

Lectures Eight to Ten

Clipping in 2D

Clipping - Overview
Clipping is the removal or scissoring of primitives against clip-regions Useful to consider rectangular clip-regions Lines - Cohen-Sutherland; Liang-Barsky
Nicholl-Lee-Nicholl refinement

Polygons - Sutherland-Hodgman Text - String, Character & Stroke


8/10 - 2

Extending the Boundaries


A B C y = ClipYT

y = ClipYB
8/10 - 3

x = ClipXL

x = ClipXR

The 9-Region Coding Method


[Top, Left] [Top] [Top, Right] y = ClipYT [Left] [ ] [Right]

y = ClipYB
[Bottom, Left] [Bottom] [Bottom, Right]

x = ClipXL

x = ClipXR

8/10 - 4

Coding of a point P = (x, y)


Code := [ ]; IF (x < ClipXL) THEN Code := [Left] ELSE IF (x > ClipXR) THEN Code := [Right];

IF (y < ClipYB) THEN Code := Code + [Bottom] ELSE IF (y > ClipYT) THEN Code := Code + [Top];
8/10 - 5

Some Example Lines

8/10 - 6

Reasons for Coding


If we code the 2 end-points (say, P and Q) of a straight line segment then, by examining these codes, we can quickly establish whether the line: Case (i) lies totally on the screen Case (ii) lies totally off the screen Case (iii) lies partially on and partially off the screen The first 2 cases are easy to establish while the third case is more difficult.
8/10 - 7

Both Associated Codes are [] so Line is Completely Visible

8/10 - 8

Completely Invisible Lines Test Set Intersections


P

Q
8/10 - 9

Line segments with same associated Codes


Q
R E D P Display Region

8/10 - 10

Clipping Against the ClipXL Boundary


Q = (X2, Y2)

R c q (X1, Y1) = P b a

X = ClipXL

8/10 - 11

Clipping against the ClipYB Boundary


X = ClipXL

(X1, Y1) = A

B = (X2, Y2)

c a

b q

Y = ClipYB
8/10 - 12

The Liang-Barsky Approach to Line Clipping in 2D


Mathematical Methodology Simple Principals Easily Extensible to 3D Quick Efficient Method

8/10 - 13

Straight Lines
P1 = (x1, y1) Any point (x, y) lying on this line may be expressed as follows: x = x0 + dx.t and y = y0 + dy.t P0 = (x0, y0) where: dx = (x1 - x0) dy = (y1 - y0) and 0 <= t <= 1
8/10 - 14

The Line Segment V0V1


Consider a straight line segment V0V1 We can think of the line as being traced out as t varies in value from 0 to 1. At t = 0 the start point of the line (V0) is drawn At t = 1 the finish point of the line (V1) is drawn An intermediate value of t (such as 0.3, 0.75 etc.) thus represents some point along this straight line segment Values of t outside the range 0 to 1 inclusive, merely represent points along V0V1 extended
8/10 - 15

Liang-Barsky Principals
This algorithm defines the clip region to be such that: xleft and ybottom <= y <= ytop <= x <= xright

It considers a typical line segment with end-points: V0 and V1 = (x1, y1)


8/10 - 16

(x0, y0)

A Line Segment V0V1


V1 = (x1, y1)
y = y TOP

Clip Region

V0 = (x0, y0)
x = x LEFT x = x RIGHT

y = y BOTTOM

8/10 - 17

Extended Boundaries - Visibility and Invisibility


The Liang-Barsky approach views the plane as follows: Extend any of the given boundaries / clip line (such as x LEFT ) to infinity in each direction This effectively divides the plane into 2 regions - the visible region and the invisible region. Define the visible region to be that side of the extended boundary line on which the viewport or clip region lies. The invisible region is deemed to be that side of the extended boundary line on which the viewport does not lie.
8/10 - 18

The x LEFT Boundary Extends to Infinity in Both Directions


V1 = (x1, y1)
With respect to the x LEFT boundary the direction of V0V1 is from the Invisible side to the Visible side

Invisible Side

Visible Side

V0 = (x0, y0)

x = x LEFT

y = y BOTTOM
8/10 - 19

The value of t at the Point of Intesection with a Boundary


If we extend the line segment represented by V0V1 to be a line of infinite length, then each of the 4 identified inequalities provides the range of values of the parameter t for which this extended line is on the visible side of the corresponding boundary line. Moreover, when t = qi / pi we have the appropriate parametric value for the point of intersection with the corresponding boundary.
8/10 - 20

Where does V0 lie with respect to a given boundary? Inspect qi


Examine qi in order to establish where V0 lies with respect to the boundary qi in this case is (x0 - xleft) BUT in general relates to some value based upon a co-ordinate associated with the first end-point V0 and one of the clip boundary Tells us the relative positioning (in terms of visible side, invisible side or actually on the boundary) of the first end-point (V0)
8/10 - 21

Is qi > 0?
(i) If (qi > 0) Then V0 is on the visible side of the corresponding boundary

Eg
==>

If

q1

>
>

0
0

x0 - xleft

and thus

x0

>

xleft

V0 (end point whose x coord is x0) lies on the visible side of the xleft boundary.
8/10 - 22

Is qi < 0?
(ii) If (qi < 0) Then V0 is on the invisible side of the corresponding boundary

Eg
==>

If

q1

<
<

0
0

x0 - xleft

and thus

x0

<

xleft

V0 (end point whose x coord is x0) lies to the invisible side of the xleft boundary
8/10 - 23

Is qi = 0?
(iii) If (qi = 0) Then V0 lies on the corresponding boundary

Eg
==>

If

q1

=
=

0
0

xo - xleft

and thus

x0

x left.

V0 (the end point whose x coordinate is x0) lies on the xleft boundary
8/10 - 24

Thedirection of V0 V1 w.r.t. the Boundary. Inspect Pi


We examine Pi in order to establish the direction of the line with respect to the boundary.

This tells us whether the direction of the line is: visible side of the boundary to invisible side invisible side of the boundary to visible side parallel to (or indeed ON) the actual boundary

8/10 - 25

Meaning of the Relationship Between Pi and 0


(i) If Pi < 0 this implies that the line from V0 to V1 is from the invisible side to the visible side

Eg

P1 < 0

==> ==> ==>

- dx is negative dx is positive (x1 - x0) is positive

so x1 > x0
V1 is thus to the right of V0 and hence with respect to the xleft boundary the line from V0 to V1 is from the invisible side to the visible side.
8/10 - 26

A Max/Min Problem
We ignore decreases to t0 and increases to t1

Such t values represent the parts of the extended line segment beyond its natural end-points. Analysis reduces to solving a max/min problem:

t0 = MAX ({qi / Pi | Pi < 0, i = 1, 2, 3, 4} {0} )


and t1 = MIN ({qi / Pi | Pi > 0, i = 1, 2, 3, 4} {1} )
8/10 - 27

Visibility and Partial Visibility


If there is a visible segment then: t0 <= t <= t1 A necessary, but not a sufficient condition for a line segment to be at least partially visible is thus: t0 <= t1
8/10 - 28

Nicholl-Lee-Nicholl Refinement to Cohen-Sutherland


N-L-N considers an apparant inefficiency in Cohen-Sutherland Attempts to eliminate the need for (unnecessary) multiple clipping Utilises C-Ss Point Coding Methodology Represents an attractive and simple to implement approach Only available in 2D
8/10 - 29

The Problem of 1 OR 2 Clips


X = ClipXL X = ClipXR
P Y = ClipYT Display Region B One Clip

Two Clips Y = ClipYB (first A then B)

A Q
8/10 - 30

Case 1 - P inside Clip Window


Top P Left Right

Bottom
8/10 - 31

Multiple or Single Clipping?


Q1

Q2
One Clip A

Two Clips

Q3
8/10 - 32

Case 2 - P in the Left region


LT L

Q4

P
L L LB Q3
8/10 - 33

LR Q1 Q2

Case 3 (i) P in the Upper Right Region


P

T T L LB TB

TR

8/10 - 34

Case 3 (ii) P in the Upper Right Region


P

T TR L L

LB

LR
8/10 - 35

How do we determine which region Q is in?


Q
TL TR

BL

BR

8/10 - 36

Nicholl-Lee-Nicholl Case 1 Summary


Case 1
The point P lies in the window and Q lies outside the window in some region N: Save the line segment XP: where and P is the start end-point X is the intercept with the appropriate boundary

8/10 - 37

Nicholl-Lee-Nicholl Case 2 Summary (1)


Case 2 - There are 2 different situations:

(i) P lies to the left of the window and Q lies in region L


Save the line segment LQ where L is the intersection with the left boundary and Q is the finishing end-point
8/10 - 38

Nicholl-Lee-Nicholl Case 2 Summary (2)


Case 2 (II) P lies to the left of the window and Q lies in some region LX

Save the line segment LX (where X is T, R or B)


where L is the intercept with the Left boundary and X is the intercept with the X boundary
8/10 - 39

Nicholl-Lee-Nicholl Case 3 (i) Summary


Case 3 (I) Region in which Q lies T L LB TR TB Save the line segment TQ LQ LB TR TB

where Q is the finishing end-point and L, T, B, R is the intercept with the Left, Top, Bottom, Right boundary

8/10 - 40

Nicholl-Lee-Nicholl Case 3 (ii) Summary


Situation II Region in which Q lies T L TR LR LB where and Save line segment TQ LQ TR LR LB

Q is the finishing end-point L, T, B, R is the intercept with the 8/10 - 41 Left, Top, Bottom, Right boundary

Polygon Clipping

Cohen-Sutherland Approach

Solid and Hollow Polygons


There is a difference between clipping hollow and solid polygons We can consider Hollow polygons as being comprised of a number of straight line segments each individual line segment is thus clipped Solid polygons define a fill area and the clipping of a solid polygon also returns a fill area. More difficult.
8/10 - 43

Sample (Hollow) Polygon Clipping


A
r s

p E q

t D B u w C v
8/10 - 44

Solid Polygon Clipping Producing Multiple Polygons


Small Polygon

Interior angles greater than 180

Clip Region

Small Polygon
8/10 - 45

The Sutherland-Hodgman Approach


The Sutherland-Hodgman approach to polygon clipping considers a solid polygon to be composed of a number of vertices The clipped polygon is also described in terms of a number of vertices. Edges connect 2 vertices in the polygon thus:
Tail Vertex Head Vertex
8/10 - 46

The 4 Different Cases


Case 4 Tail Head Tail Case 3 Case 2 Tail Case 1 Head

Head
Tail Head

Invisible Side

Visible Side

8/10 - 47

Methodology
Traverse all of the vertices in turn against a given clip boundary to produce a set of clipped points (orvertices) which define the clipped polygon WRT that clip boundary. We start with a sequence of N vertices and end up with a sequence of vertices (unlikely to be N - may be fewer or more) Repeat this exercise for each of the other clip boundaries in turn. 8/10 - 48

Order of the Traversal


We consider each vertex (1 to N) in turn. N is the number of vertices describing the polygon. When we consider the first vertex (Vertex 1) previous vertex is the last vertex - vertex N. We are really consider the edge whose Head is Vertex 1 and whose Tail is Vertex N. At all other times we consider the edge whose Head vertex is I and whose Tail vertex is I 8/10 - 49 -1

Associated Functions/Procedures
The algorithm uses the following functions and procedures. Details not required A Boolean function Visible which accepts as parameter some vertex and a clip boundary and returns a true value if the vertex is visible WRT that clip boundary and false otherwise.

A procedure Calculate which calculates the intersection of the edge under consideration with the current (appropriate) clip boundary. 8/10 - 50

Text Clipping

Stroke, String & Character Clipping

Character Clipping
3 methods
Stroke
Highly precise, most accurate, most expensive. Clip unit is the composite strokes of the character Clip Unit is the entire string which is either fully displayed or not displayed at all Clip Unit is the individual character which is either fully displayed or not displayed at all
8/10 - 52

String

Character

Você também pode gostar