0% found this document useful (0 votes)
35 views38 pages

Clipping Techniques in Computer Graphics

Uploaded by

Ivy Wanjiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views38 pages

Clipping Techniques in Computer Graphics

Uploaded by

Ivy Wanjiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

BIT 2301

Computer Graphics

Lesson 4: Clipping

Lucy W. Mburu, Ph.D


Overview: Clipping
 We’ve been assuming that all primitives (lines,
triangles, polygons) lie entirely within the
viewport
 In general, this assumption will not hold, we
sometimes need to extract data/primitives inside
a region of interest and discard (parts of)
primitives outside window.

4.2
Overview: 2D Viewing
 Converts World coordinates to Viewing coordinates
 Image moves from window to viewport
 Window: a region of the scene selected for viewing (also
called a clipping window)
 Viewport: a region on a display device for mapping to a
window.

 Graphics packages commonly allow only rectangular


clipping windows aligned with the x- and y-axes

4.3
Clipping Window vs. Viewport

 The clipping window selects what we want to see in


our virtual 2D world
 The Viewport indicates where it is to be viewed on
the output device (or within the display window)
 By default, the viewport has the same location and
dimensions as the GLUT display window that you
create
 But it can be modified so that only part of the display
window is used for OpenGL display

4.4
Clipping Window vs. Viewport

 A clipping window and associated viewport,


specified and rectangles aligned with the coordinate
axes

4.5
(Clipping) Window to Viewport
Transformation
 Zooming
 is successive mapping of different sized clipping
windows to the viewport
 reducing clip window : zoom in on part of scene
 increase clip window : zoom out
 Panning
 moving a fixed-size clipping window across the
scene

4.6
Two-Dimensional Clipping
 Point clipping – trivial
 Line clipping
 Cohen-Sutherland
 Liang-Barsky
 Nicholl-Lee-Nicholl
 Fill-area clipping
 Sutherland-Hodgman
 Weiler-Atherton
 Text clipping

4.7
Clipping Algorithms
 These algorithms will typically identify the parts
of the object that will be inside the window
 Everything outside the clipping window is
eliminated from the scene description (i.e., not
converted) for efficiency.
 Point Clipping: Remove points outside
window.
 A point is either entirely inside the window or
not
(xR, yT)
Q
P

(xL, yB) 4.8


Line Clipping-Cohen Sutherland
 The algorithm divides a two-dimensional space into
9 regions. And assigns outcodes to these regions.
 It then efficiently determines the lines and portions of
lines that are visible in the central region of interest
(the viewport).
 The bits in the 2D outcode represent: top, bottom,
right, left. E.g., the outcode 1010 represents a point
that is top-right of the viewport.

4.9
Line Clipping-Cohen Sutherland
 The algorithm uses a divide-and-conquer strategy.
 To clip a line, we need to consider only its endpoints. If
both endpoints of a line lie inside the window, the
entire line lies inside the window. It is trivially
accepted and needs no clipping. On the other hand, if
both endpoints of a line lie entirely to one side of the
window, the line must lie entirely outside of the
window. It is trivially rejected and needs to be neither
clipped nor displayed.
 If the line cannot be trivally accepted or rejected, an
intersection of the line with a window edge is
determined and the trivial reject/accept test is
repeated.
 This process is continued until the line is accepted.
4.10
Line Clipping-Cohen Sutherland
 Once the codes for each endpoint of a line are
determined, the logical AND operation of the codes
determines if the line is completely outside of the
window.
 If the logical AND of the endpoint codes is not zero,
the line can be trivally rejected. E.g, if an endpoint has
a code of 1001 while the other endpoint has a code of
1010, the logical AND will be 1000 which indicates the
line segment lies outside of the window.
 However, if the endpoints have codes of 1001 and
0110, the logical AND would be 0000, and the line
cannot not be trivally rejected.

4.11
Line Clipping-Cohen Sutherland
 The logical OR of the endpoint codes determines if the
line is completely inside the window. If the logical OR
is zero, the line can be trivally accepted.
 E.g, if the endpoint codes are 0000 and 0000, the
logical OR is 0000 - the line can be trivally accepted. If
the endpoint codes are 0000 and 0110, the logical OR
is 0110 and the line can not be trivally accepted.

4.12
Cohen Sutherland: The Algorithm
1. Given a line segment with endpoint p1=(x1,y1) and p2=(x2,y2)
2. Compute the 4-bit codes for each endpoint:
If both codes are 0000,(bitwise OR of the codes yields 0000 ) line lies
completely inside the window: pass the endpoints to the draw routine.
If both codes have a 1 in the same bit position (bitwise AND of the 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 the two endpoints must lie outside the window and the
line segment crosses a window edge. This line must be
clipped at the window edge before being passed to the
drawing routine.
4. Examine one of the endpoints, say p1=(x1,y1). Read p1's 4-bit
code in order: Left-to-Right, Bottom-to-Top.
5. When a set bit (1) is found, compute the intersection I of the
corresponding window edge with the line from p1 to p2.
Replace p1 with I and repeat the algorithm.
4.13
Cohen-Sutherland- Example
 To clip the following lines:

1. Consider the line segment AD. Point A has an outcode


of 0000 and point D has 1001. The logical AND of these
outcodes is zero, hence the line cannot be trivally rejected. Also,
the logical OR of the outcodes is not zero; therefore, the line
cannot be trivally accepted. The algorithm then chooses D as the
outside point (its outcode contains 1's). By our testing order, we
first use the top edge to clip AD at B. The algorithm then
recomputes B's outcode as 0000. With the next iteration of the
algorithm, AB is tested and is trivially accepted and displayed.

4.14
Cohen-Sutherland- Example
2. Consider the line segment EI. Point E has 0100 and point I
has 1010. The results cannot be trivally rejected or accepted.
Point E is determined to be an outside point, so the algorithm clips the
line against the bottom edge of the window.
Now line EI has been clipped to be line FI. Line FI is tested and
cannot be trivially accepted or rejected.
Point F has 0000, so the algorithm chooses point I as an outside point
since its outcode is 1010. The line FI is clipped against the
window's top edge, yielding a new line FH. Line FH cannot be
trivally accepted or rejected.
Since H's outcode is 0010, the next iteration clips against the
window's right edge, yielding line FG. The next iteration tests FG,
and it is trivially accepted and displayed.

4.15
Cohen-Sutherland- Example
 After clipping the segments AD and EI, the result is that
only the line segment AB and FG can be seen in the
window.

4.16
Cohen-Sutherland Line Clipping

 This algorithm can be very efficient if it can accept and


reject primitives trivially
 If clip window is larger than the scene data
 Most primitives are accepted trivially
 If clip window is much smaller than scene data
 Most primitives are rejected trivially

 Good for hardware implementation

4.17
Liang-Barsky Line Clipping
Clipping: Overview of Steps
 Express line segments in parametric form
 Derive equations for testing if a point is inside the
window
 Compute new parameter values for visible portion of
line segment, if any
 Display visible portion of line segment

 The relative speed improvement over Sutherland-


Cohen algorithm is as follows:
 36% for 2D lines
40% for 3D lines
70% for 4D lines

4.18
Liang-Barsky Clipping

 Parametric clipping - view line in parametric form and


reason about the parameter values
 More efficient, as not computing the coordinate values at
irrelevant vertices
 Clipping conditions on parameter: Line is inside clip
region for values of t such that:

xmin  x1  tx  xmax x  x2  x1


ymin  y1  ty  ymax y  y2  y1

4.19
Liang-Barsky (2)

 Infinite line intersects clip region edges when:

p1  x q1 x 1  x min
qk
tk  where p 2 x q 2 x max  x 1
pk
p 3  y q 3 y1  y min
p 4 y q 4  y max  y1

4.20
Liang-Barsky (3)

 When pk<0, as t increases line goes from outside to


inside - enter
 When pk>0, line goes from inside to outside - leave
 When pk=0, line is parallel to an edge (clipping is
easy)
 If there is a segment of the line inside the clip region,
sequence of infinite line intersections must go: enter,
enter, leave, leave

4.21
Liang-Barsky (4)

Leave
Enter
Leave Leave
Leave

Enter

Enter
Enter

4.22
Liang-Barsky - Algorithm
 Compute entering t values, which are qk/pk for each pk<0

 Compute leaving t values, which are qk/pk for each pk>0

 Parameter value for small t end of line is:tsmall= max(0,


entering t’s)
 parameter value for large t end of line is: tlarge=min(1,
leaving t’s)
 if tsmall<tlarge, there is a line segment - compute endpoints
by substituting t values

4.23
Nicholl-Lee-Nicholl Line Clipping

 Creates more testing regions around the clipping


window
 Avoids multiple line-intersection calculations
 Initial testing to determine if a line segment is
completely inside the clipping window can be done
using previous methods
 If trivial acceptance or rejection is not possible the
NLN algorithm sets up additional regions

4.24
Nicholl-Lee-Nicholl Line Clipping
P0

P0 P0

For line with endpoints P0Pend, there are three different


positions to consider - all others can be derived from
these by symmetry considerations
For each case, we generate specialized test regions for
other endpoint Pend, which use simple tests (slope, >,
<), and tells us which edges to clip against.
4.25
Case 1

T
Find which of the four
regions Pend lies in,
L P0 R then calculate the line
intersection with the
B corresponding
boundary

4.26
Case 2

LT

L
P0 L LR
L

LB

Find which of the four regions Pend lies in, then


calculate the line intersection with the
corresponding boundary
4.27
Case 3 : Two Possibilities

P0 P0
T T
TR T TR
L L L
LB LR TB
LB

Find which of the five regions Pend lies in, then


calculate the line intersection with the
corresponding boundary
4.28
N-L-N Line clipping
 To determine in which region
Pend lies we compare the
LT
slope of PendPo to the slopes
of the boundaries of the NLN
regions
L
P0 L LR
L

LB

Number of cases explodes in 3D, making


algorithm unsuitable
4.29
Polygon Clipping

 Clipping a polygon fill area needs more than line-


clipping of the polygon edges
 would produce and unconnected set of lines
 Must generate one or more closed polylines, which
can be filled with the assigned colour or pattern

4.30
Polygon Clipping

 Find the vertices of the new polygon(s) inside the


window
 Sutherland-Hodgman polygon clipping:
 Check each edge of the polygon against all window
boundaries
 Modify the vertices based on the transitions
 Transfer the new edges to the next clipping boundary

4.31
Polygon Clipping- Illustration

 Consider the following image that needs to be clipped


using the Sutherland-Hodgman algorithm:

4.32
Sutherland-Hodgman Polygon Clipping
 Step 1: Clip against the left border
 V1 V2 both inside V1, V2
 V2 V3 both inside V2, V3
 V3 V4 both inside V3, V4
 V4 V5 both inside V4, V5
 V5 V6 both inside V5, V6
 V6 V1 both inside V6, V1
 Return V1, V2, V3, V4, V5, V6, V1

4.33
Sutherland-Hodgman Polygon Clipping
 Step 2: Clip against the bottom border
 V1 V2 both inside V1, V2
 V2 inside V3 outside V2, V23 (we leave out V3)
 V3 V4 both outside (we leave both out)
 V4 V5 both outside (we leave both out)
 V5 Outside V6 inside V56, V6 (we leave out V5)
 V6 V1 both inside V6, V1
 Return V1, V2, V23, V56, V6, V1

4.34
Sutherland-Hodgman Polygon Clipping
 Step 3: Clip against the right border
 V1 inside V2 outside V1, V12 (we leave out V2)
 V2 outside V23 inside V223, V23 (we leave out V2)
 V23 V56 both inside V23, V56
 V56 V6 both inside V56, V6
 V6 V1 both inside V6, V1
 Return V1, V12, V223, V23, V56, V6, V1

4.35
Sutherland-Hodgman Polygon Clipping
 Step 4: Clip against the top border
 V1 inside V12 both outside (we leave both out)
 V12 outside V223 inside V12223, V223 (we leave out V12)
 V223 V23 both inside V223, V23
 V23 V56 both inside V23, V56
 V56 V6 both inside V56, V6
 V6 inside V1 outside V6, V16
(we leave out V1)

 Return V12223, V223, V23, V56, V6, V16, V12223

4.36
Sutherland-Hodgman Polygon Clipping
 Before clipping
V1, V2, V3, V4, V5, V6, V1

 After clipping
V12223, V223, V23, V56, V6,
V16, V12223

4.37
Text Clipping
 All-or-none text clipping
 Using boundary box for the entire text
 All-or-none character clipping
 Using boundary box for each individual character
 Character clipping
 Vector font: Clip boundary polygons or curves
 Bitmap font: Clip individual pixels

4.38

You might also like