Sim Clipping Final

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 51

1

Clipping Basics
Goal:only render what will be visible in the drawing window; eliminate
from the rendering pipeline all those primitives which will not appear
in the final viewport
Clipping Approaches
Individual Pixel Tests:
Render at the pixel level
Check pixel for containment in the target region
Maintain Large Drawing Area:
Render the entire set of primitives in the WCS
Display only the portion of the set which overlaps the viewport
Analytical Methods:
Compute the intersections of primitives with the clip region
Eliminate outlying portions of the primitives
Clipping
General criteria for a good clipping algorithm:
Quickly identify lines (or polygon edges) that need not be

drawn at all
- These can be skipped and discarded
Quickly identify lines (or polygon edges) that are completely

contained within the drawing bounds


- These can be drawn without modification
For rest of the lines(that cross display boundaries),calculate

their intersections as quickly as possible


- The less of these we do, the better

3
Trivial Accepts & Rejects
Big optimization: trivial accept/rejects
How can we quickly determine whether a line segment is entirely

inside the viewport?


Test both endpoints

- if both endpoints on wrong side of same edge, trivially reject line


- if both endpoints inside boundary, trivially accept line
- else find boundary intersection
xmin xmax

ymax

ymin
Clipping Primitives
Different primitives can be handled in different
ways
Points
Lines
Polygons
Point Clipping
This one is easy
Determine if a point (x,y) is in the clip

window, check for value of x and y


against each boundary of clip window
( L = xwmin, R = xwmax,
B = ywmin, T = ywmax )
Display P = (x,y) if
( xwmin x xwmax ) &&
( ywmin y ywmax )
Line Clipping
What happens when a line passes out of the viewing
volume/plane?
Part is visible, part is not

Need to find the entry/exit points, and shorten the line

The shortened line is what gets passed to rasterization


Line Clipping Example

Lets do 2D first
Clip a line against 1 edge (x1,y1)
of the viewport C
(x,y)???
What do we know?
A D
Similar triangles

A/B=C/D
B = (x2 - x1) (x2,y2)
A = (y2 - y1) B
C = (y1 - ymax)

D = BC / A

(x, y) = (x1 - D, ymax)


Line Clipping
Advantages
The other cases are handled similarly
The algorithm extends easily to 3D
Disadvantages
Too expensive! (these numbers are for 2D)
3 floating point subtracts
2 floating point multiplies
1 floating point divide
4 times! (once for each edge)
need to do better
Cohen-Sutherland Line Clipping
Oldest and most commonly used
Divide view plane into regions defined by viewport edges

Assign each region a 4-bit outcode:


T B R L
L= xmin R = xmax B4 B3 B2 B1

1001 1000 1010


T = ymax

0001 0000 0010

B = ymin

0101 0100 0110


Cohen-Sutherland Line Clipping Algorithm

1. Assign each endpoint a 4-bit outcode or tag by testing against


boundaries (top, bottom, right, left)
x < x
min (B1 = 1, else 0)
x > x
max (B2 = 1, else 0)
y < y
min (B3 = 1, else 0) T B R L
y > y
max (B4 = 1, else 0) B4 B3 B2 B1
xmin xmax

1001 1000 1010


ymax

0001 0000 0010


ymin
0101 0100 0110
Cohen-Sutherland Line Clipping
2. Determine which lines are completely inside and completely
outside
a. if (code1== code2 == 0000) bitwise OR vertex outcodes
trivial accept
b. if ((code1 & code2) 0) ie bitwise AND vertex outcodes
trivial reject
(as those lines lie on one side of the boundary)
3. If line cannot be trivially accepted or rejected, clip the line
against an edge (where both bits are nonzero) and find the
intersection point using intersection algorithm
Cohen-Sutherland Line Clipping Algo..
4. Intersection algorithm:
a. Let (x,y) be intersection point between P1(x1,y1) and P2(x2,y2)
b. Slope m=(y2 - y1) /(x2 - x1)
c. Check against each boundary
If B1=1, line intersect L = xmin
y = y1 + m(x - x1) ; x = xmin
If B2=1, line intersect R = xmax;
y = y1 + m(x - x1) ; x = xmax
If B3=1, line intersect B = ymin
x = x1 + (y - y1)/ m ; y = ymin
If B4=1, line intersect T = ymax
x = x1 + (y - y1)/ m ; y = ymax
d. Discard portion on wrong side of edge and assign outcode to
new vertex (x,y)
5. Repeat steps 2 onwards until line is completely accepted or
rejected
Example-Cohen-Sutherland
Case 1: Line segment P1P2
a. Calculate clip codes
P = 0 0 0 0
1

P2 = 0 0 0 0
b. Bitwise OR= 0 0 0 0
=>accept segment P1P2
(trivial accept since both end
points are inside the
window)
ifc0|c1=0000
accept(draw)
elseifc0&c10000
reject(dontdraw)
elseclipandretest
Example-Cohen-Sutherland
Case 2: Line segment P3P4
a. Calculate clip codes
P = 1 0 0 1
3

P4 = 1 0 0 0
b. Bitwise AND = 1 0 0 0
=> reject segment P3P4
( trivial reject since both end
points are out of the window)
Example-Cohen-Sutherland
Case 3: Line segments P5P6 , P7P8
a.Calculate clip codes
P5 = 0 0 0 1

P6 = 1 0 0 0

b. Bitwise AND = 0 0 0 0
P7 = 0 0 0 1

P8 = 1 0 0 0

b. Bitwise AND = 0 0 0 0
Example-Cohen-Sutherland
c. Clip against left
P'5 = ( xwmin, y5 + m( xwmin - x5))

P'7 = ( xwmin, y7 + m( xwmin - x7))

Line segments P'5 P6 , P'7 P8


Example-Cohen-Sutherland
Line segments P'5 P6 , P'7 P8
d. Update clip codes
P'5 = 1 0 0 0

P6 = 1 0 0 0

e. Bitwise AND = 1 0 0 0
=> reject P'5P6
P'7 = 0 0 0 0

P8 = 1 0 0 0

e. Bitwise AND = 0 0 0 0
Example-Cohen-Sutherland
Line segment P'7 P8
f. Clip against right
g. Clip against bottom
h. Clip against top
P'8 = (x7 + (ywmax - y7) / m, ywmax)

Line segment P'7 P'8


Example-Cohen-Sutherland
Line segment P'7 P'8
i. Update clip codes
P' = 0 0 0 0
7

P'8 = 0 0 0 0
j. Bitwise OR = 0 0 0 0
=> accept P'7 P'8
Cohen-Sutherland Summary
Advantages:
Simple to implement

Oriented for most simple window/viewport systems

Extends to 3-D cubic volumes

Outcode tests and line-edge intersects are quite fast

Efficient when most of the lines to be clipped are either

rejected or accepted (not so many subdivisions).


Easy to program.

Parametric clipping are more efficient.

21
Cohen-Sutherland Summary
Disadvantages:
Fixed-order decision can do needless work

Can generate more efficient rejection tests

Will do unnecessary clipping.

Not the most efficient.

Clipping window must be rectangular

But some lines require multiple iterations:

Clip top, Clip left, Clip bottom, Clip right

Fundamentally more efficient algorithms:


Cyrus-Beck uses parametric lines

Liang-Barsky optimizes this for upright

volumes
Liang-Barsky Line Clipping
Consider the parametric definition of a line:
x = x1 + ux y = y1 + uy
x = (x2 - x1) y = (y2 - y1) 0 (u) 1
Substituting in point clipping condition in parametric form:

xmin x1 + ux xmax ymin y1 + uy ymax


On rearranging the four inequalities,

-ux (x1 - xmin) ux (xmax - x1)


-uy (y1 - ymin) uy (ymax - y1)
In general: u * pk qk , k= 1,2,3,4 ie.(L,R,B,T) and

p1 = -x q1 = (x1 - xmin)
p2 = x q2 = (xmax - x1)
p3 = -y q3 (y1 - ymin)
p4 = y q4 (ymax - y1)
Liang-Barsky Line Clipping Algorithm
1. Input endpoint coordinates of line (x1, y1) and (x2, y2)
2. For each boundary k=(1,2,3,4) for (L,R,B,T) calculate pk, qk
p
1 = -x q1 = (x1 - xmin)
p2 = x q2 = (xmax - x1)
p3 = -y q3 (y1 - ymin)
p4 = y q4 (ymax - y1)
3. Check the following cases:
a. pk = 0 (Line is parallel to boundary k)
If for the same k, qk < 0, reject
Else if qk 0 line is inside boundary, accept

b. pk < 0 (Line proceeds from outside boundary k to inside)


rk = qk / pk
u1 = max(0, rk)
c. pk > 0 (Line proceeds from inside boundary k to outside)
rk = qk / pk
u2 = min(1, rk)
Liang-Barsky Line Clipping
4. If (u1 > u2), the line is completely outside boundary,
reject the line
else if (u1 < u2) calculate intersection points (x1, y1) and
(x2,y2)
x1 = x1 + x u1
y1 = y1 + y u1
x2 = x1 + x u2
y2 = y1 + y u2
Example: Liang-Barsky Line Clipping
Let P1 (-1, -2) and P2 (2, 4) be endpoints of line
XL = 0, XR = 1, YB = 0, YT = 1
x = 2 - (-1) = 3 y = 4 - (-2) = 6
p1 = - x = -3 q1 = x1 - XL = -1 - 0 = -1
r1 = (q1/p1) = 1/3
p2 = x = 3 q2 = XR - x1 = 1 - (-1) = 2
r2 = (q2/p2) = 2/3
p3 = - y = -6 q3 = y1 - YR = -2
r3 = (q3/p3) = 1/3
p4 = y = 6 q4 = YT - y1 = 3
r4 = (q4/p4) = 1/2
for (pi < 0) u1 = MAX(1/3, 1/3, 0)= 1/3
for (pi > 0) u2 = MIN (2/3, 1/2, 1) = 1/2
Since u1 < u2 there is a visible section
Example: Liang-Barsky Line Clipping.

compute new endpoints by intersection


with boundary as:
u1 = 1/3
x1' = x1 + x.u1 = -1 + (3 x 1/3) = 0
y1' = y1 + y.u1 = -2 + (6 x 1/3) = 0
u2 = 1/2
x2' = x1 + x.u2 = -1 + (3 x 1/2) = 1/2
y2' = y1 + y.u2 = -1 + (6 x 1/2) = 1
So (x1,y1) = (0,0) and (x2,y2) = (1/2,1)
are new endpoints of the line
Liang-Barsky Line Clipping
Liang-Barsky's Line Clipping Algorithm uses the parametric
equations for a line and solves four inequalities to find the range
of the parameter for which the line is in intersection with
rectangle.
Also extends to 3D
Just add equations for z = z 1 + uz

2 more ps and qs
In most cases, Liang-Barsky is slightly more efficient
Avoids unnecessary intersection calculations

Avoids multiple shortenings of line segments

However, Cohen-Sutherland is much easier to understand


Clipping Polygons
We know how to clip a single line segment
How about a polygon in 2D?

How about in 3D?

Its the process of finding the exact part of a polygon lying


inside the view volume
To maintain consistency, clipping of a polygon should result
in a polygon, not a sequence of partially unconnected lines
first look at different 2D solutions, then extend one to 3D
Clipping polygons is more complex than clipping the
individual lines
Input: polygon

Output: polygon, or nothing


Why Is Clipping Hard?
What happens to a triangle during clipping?
Possible outcomes:

Triangletriangle Trianglequad Triangle5-gon


How many sides can a clipped triangle have?

30
Why Is Clipping Hard?
A really tough case:

concave polygon multiple polygons


Sutherland-Hodgeman Polygon Clipping

Simplify via separation


Need to create clipped polygons from the original polygons

Clip the entire polygon with one edge

Clip the output polygon against the next edge

Repeat for all edges

Do inside test for each point in sequence,

Insert new points when they cross window boundary,

Remove points outside window boundary


Sutherland-Hodgeman Polygon Clipping

Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

33
Sutherland-Hodgeman Polygon Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

34
Sutherland-Hodgeman Polygon Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

35
Sutherland-Hodgeman Polygon Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

36
Sutherland-Hodgeman Polygon Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

37
Sutherland-Hodgeman Polygon Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

38
Sutherland-Hodgeman Polygon Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

39
Sutherland-Hodgeman Polygon Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

40
Sutherland-Hodgeman Polygon Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

41
Sutherland-Hodgeman Polygon Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped
Will this work for non-rectangular clip regions?
What would
3-D clipping
involve?

42
Sutherland-Hodgeman Polygon Clipping
Input/output for algorithm:
Input: list of polygon vertices in order
Output: list of clipped polygon vertices consisting of old
vertices (maybe) and new vertices (maybe)
Note: clipping operation done against each edge
Sutherland-Hodgeman basic routine:

Go around polygon one vertex at a time


Current vertex has position p
Previous vertex had position s, and it has been added to
the output if appropriate

43
Sutherland-Hodgeman Clipping
Edge from s to p takes one of four cases:
(Purple line can be a line or a plane)

inside outside inside outside inside outside inside outside


p p s
s

p s
p s

p output i output no output i output


p output
44
Sutherland-Hodgeman Clipping
Four cases:
s inside plane and p inside plane

Add p to output

Note: s has already been added
s inside plane and p outside plane
Find intersection point i

Add i to output
s outside plane and p outside plane

Add nothing
s outside plane and p inside plane
Find intersection point i

Add i to output, followed by p

45
Example: Sutherland - Hodgeman

Input Vertices
in ->
P1
P2
P3
P4
Example: Sutherland - Hodgeman

Left Clipping
in -> [clip left] ->
P1 - in to in -> P1
P2 - in to out -> P'2
P3 - out to out -> x
P4 - out to in -> P'4
-> P4
Example: Sutherland - Hodgeman

Right Clipping
-> [clip right] ->
P1 - in to in -> P1
P'2 - in to in -> P'2
x
P'4 - in to in -> P'4
P4 - in to in -> P4
Example: Sutherland - Hodgeman

Bottom Clipping
-> [clip bottom]->
P1 - in to in -> P1
P'2 - in to out -> P''2
P'4 - out to in -> P''4
-> P'4
P4 - in to in -> P4
Example: Sutherland - Hodgeman

Top Clipping
-> [clip top] -> out
P1 - in to in -> P1
P''2 - in to in -> P''2
P''4 - in to in -> P''4
P'4 - in to in -> P'4
P4 - in to in -> P4
Sutherland-Hodgeman Summary
Coded recursively - efficient hardware implementation.
Quite general -

clip region can be any convex polygon in 2D,


or any convex polyhedron in 3D.

51

You might also like