Title: Clipping
1Week 11 Line and Polygon Clipping
2Introduction
- The view volume in the graphics pipeline.
- Polygons that are completely inside the view
volume are processed for display. - Polygons that are completely outside the view
volume are culled. - Polygons that intersect the boundary of the view
volume are clipped against the boundary, and
then processed for display. Clipping can be done
in 3D space or image space the algorithms are
nearly identical.
3Line clipping against a rectangle
- Keep only the portions that are inside the
rectangle. - Lines lying on the clipping rectangles border
are considered inside and hence are displayed. - Clipping a line against a rectangle results in a
single line segment.
4Review Parametric Equations for Lines
- Parametric equation of a line connecting P0 and
P1 - Parametric equation through interpolation
(variant on above) - Why parametric equations?
- good for representing a line segment, not an
infinitely long line - good for representing a line with infinite slope
t1
example
t0
P(t) tP1 (1-t)P0 P0 (P1-P0)t
where 0t1
t0
t1
t
1-t
P0(x0,y0)
P1(x1,y1)
5Computing intersection using parametric equations
- Intersection between two parametric equations
- Intersection of a line segment and an edge of the
clip rectangle
t1
x(t) x(s) y(t) y(s)
s0
P2(x2,y2)
P3(x3,y3)
Solve for s and t
s1
t0
x(t) 0 t(2-0) 2t y(t) -1
t(1-(-1)) 2t - 1
clip rectangle
y
(2,1)
y(t)2t-10 ? t 1/2
x
Get intersection (x(1/2),0)(1,0)
(0,-1)
6 Brute-force line clipping algorithm
- For a point p(x,y) to be inside a rectangle ltt,
b, r, lgt, it should be guaranteed that l ? x ? r
and b ? y ? t. - For a line segment L connecting p0 and p1,
- ? If both p0 and p1 are inside the rectangle,
trivially accept L. - ? Otherwise, compute intersections with L and all
4 edges of the rectangle and then get the clipped
version.
?
?
7Cohen-Sutherland clipping algorithm
- The planar area is divided into 9 regions, and
4-bit code ltt, b, r, lgt is assigned to each
region. -
-
- Each edge of the rectangle partitions the whole
2D plane into two half-planes. - Each bit of ltt, b, r, lgt is for an edge of the
rectangle - 0 if the region is on or in the rectangle-side
half-plane - 1 otherwise.
- An end point of a line segment is assigned a code
of the region where it is.
t b r l
bit 1
top
left
right
bit 0
bottom
8Cohen-Sutherland clipping algorithm (contd)
- Case 1 If both p0 and p1 have 0000 codes,
trivially accept. - Case 2 If their bitwise-AND is not zero(0000),
trivially reject. -
-
-
- Case 3 Otherwise. See the next slide.
P0
P1
i.e. Both vertices are outside. e.g.
1001
t b r l
0101
9Cohen-Sutherland clipping algorithm (contd)
- Note that Case 3 can be reached only after
trivial-rejection fails. - Suppose we see a 1-bit on one end point. Then the
corresponding bit on the other end must be 0.
(Otherwise, they must have been caught at Case
2.) The line segment crosses the edge associated
with the bit. -
-
t b r l
The edge crosses top and left
Choose any endpoint (which is outside of the
rectangle, i.e. which is not 0000) and scan the
code until 1 is met.
10Cohen-Sutherland clipping algorithm (contd)
Loop Test for trivially accepted/rejected. Find
an endpoint outside the rectangle. Scan the code
and find the edge that might be crossed. Compute
the intersection (as we did in the brute-force
algorithm). Clip off the line segment from the
outside endpoint to the intersection
point. Update the code of the new endpoint.
example2
example1
a
a
1000
0000
0000
1000
a
a
0010
a
b
0010
b
b
b
0000
0010
0010
trivially rejected
11Line against polygon
A test for a relation between a point and a
line The left and right sides are relative to a
viewer standing at P1 and looking towards
P2. If det(A, P1, P2) 0 then A is on the line
P1P2 If det(A, P1, P2) gt 0 then A is to the left
of P1P2 If det(A, P1, P2) lt 0 then A is to the
right of P1P2
12Line against polygon
The determinant det(A, P1, P2) is interpreted
with A, P1, P2 as row vectors
13Line against polygon (contd)
A function to compute line intersections Let R
denote the intersection of two lines P1P2 and
P3P4 R belongs to both P1P2 and P3P4 Solve the
system Note If the equations are
linearly dependent, then an infinity of solutions
exist
14Line against polygon (contd)
Let B1, B2,..., Bn be a convex polygon and let
P1P2 be a line segment. Determine the line
segment Q1Q2 that is part of P1P2 and lies inside
the convex polygon. Let Ti det(Bi, P1, P2) Step
1 Eliminate non-intersecting cases We
distinguish three cases Case 1 If Ti gt 0 or Ti
lt 0 for all i 1, 2,..., n Then all Bis are
on one side of line segment P1P2 (i.e. entire
polygon is on one side of P1P2) Thus Q1Q2
Ø
15Line against polygon (contd)
Step 1 Eliminate non-intersecting cases Case 2
If there exists a Bi s.t Ti 0 and the rest of
the Bjs are on the same side of P1P2 Then Q1Q2
Ø
16Line against polygon (contd)
Step 1 Eliminate non-intersecting cases Case 3
If there exist Bi, Bj s.t Ti Tj 0 and the
rest of the Bjs are on the same side of
P1P2 Then Q1Q2 Ø
17Line against polygon (contd)
Step 2 Find the line segments in the convex
polygon that intersect P1P2 If the line P1P2
crosses one side of the polygon then it crosses
two sides If Bj is on one side, or on, P1P2 and
B(j1) is on the other side of P1P2 i.e. Tj gt
0, T(j1) lt 0 or Tj lt 0, T(j1) gt 0 If Bk (k
! j) is on one side, or on, P1P2 and B(k1) is
on the other side of P1P2 i.e. Tk gt 0,
T(k1) lt 0 or Tklt 0, T(k1) gt 0 Then P1P2 must
cross between these pairs of points
18Line against polygon (contd)
Step 3 Classify the end points of P1P2 against
the lines BjB(j1) and BkB(k1) Compute the
sides of BjB(j1) and BkB(k1) that each of P1
and P2 lie upon If sgn(P1, Bj, B(j1)) then
P1 is to the left of BjB(j1) If sgn(P1, Bj,
B(j1)) - then P1 is to the right of
BjB(j1) In this step - Need to determine
sgn(P1, Bj, B(j1)) sgn(P2, Bj, B(j1))
sgn(P1, Bk, B(k1)) sgn(P2, Bk, B(k1))
Where sgn is the sign of the determinant. Step
4 Compute Q1Q2 from the classification in Step
3 We have 9 possibilities see next slide
19 20 Polygon Clipping
If line clipping was used
Instead, we need
Pipeline of boundary clipping
bottom clipper
right clipper
top clipper
left clipper
21Sutherland-Hodgeman Algorithm
e.g. right clipping Input is v1 ? v2 ? v3 ?
v4 Output should be v11 ? v2 ? v3 ? v33
While moving from vi to vj, ? determine if we
have to save vj, and ? determine if we have to
add a new vertex. ? out ? in (v1 ? v2) add v11,
and save v2 ? in ? in (v2 ? v3) save v3, and add
none ? in ? out (v3 ? v4) save none, and add
v33 ? out ? out (v4 ? v1) save none, and add
none
add, and then save
save, and then add
22Sutherland-Hodgeman Algorithm - Example
top clipping
Input 1 ? 2 ? 3 ? 4
11 ? 2 ? 3 ? 4 ? 44
right clipping
2 ? 22 ? 33 ? 4 ? 44 ? 11
23A problem in Sutherland-Hodgeman algorithm
- Recall A polygon is convex if, given any 2
points in its interior, the line segment joining
them is also in the interior. - Concave polygons are not always correctly
clipped. Apply Sutherland-Hodgeman algorithm to
the example, see what happens. - Convex polygons are correctly clipped by
Sutherland-Hodgeman algorithm. Fortunately its
reasonable to restrict polygons to be clipped
only to convex ones.