Mod 2 Lecture 1_2 CG windowing clipping
Mod 2 Lecture 1_2 CG windowing clipping
Computer Graphics
In each of these cases, work is not just done with the base entity data, but with a
display file that stores the displayable vectors for an entity.
For example, in the selection of a surface without use of a display file it would be
necessary to recompute the surface display in order to identify which path is
nearest to the cursor. With a display file it is only necessary to find the nearest
displayed vector and to cross-reference back to the entity.
Display files also lend themselves to fast image manipulation or zoom facilities.
Instead of recomputing the entire image for a zoom within existing window
boundary, the display file vectors are used- leading to some loss in display
resolution for curves, but generally faster display control.
The display file is related to the rest of the data by cross-referencing between the
display file and the entity table.
• Windowing Concepts
• Clipping
– Introduction
– Brute Force
– Cohen-Sutherland Clipping Algorithm
• Area Clipping
– Sutherland-Hodgman Area Clipping Algorithm
Clipping
- is used in extracting the part of a scene and for
identifying visible surfaces in 3D views
- Displaying multi window environments
- Selecting object that can be applied with
necessary Geometric Transformations
(Zooming and panning of image is applied on
the necessary geometry)
Windowing
• A scene is made up of a collection of objects
specified in world coordinates
World Coordinates
Windowing
• When we display a scene only those objects
within a particular window are displayed
Window
wymax
wymin
wxmin wxmax
World Coordinates
Windowing
• Because drawing things to a display takes time
we clip everything outside the window
Window
wymax
wymin
wxmin wxmax
World Coordinates
Clipping
• For the image below consider which lines and
points should be kept and which ones should
be clipped
P4
Window P2
wymax
P6
P3
P1
P7 P5
P9
P8
wymin
P10
wxmin wxmax
Point Clipping
- a point (x,y) is not clipped if:
• wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax
• otherwise it is clipped
P4 Clipped
Clipped
Window P2
wymax
Clipped
P5
P1
P7 Points Within the Window
are Not Clipped
P9 P8
wymin
Clipped P10
wxmin wxmax
Line Clipping
• examine the end-points of each line to see if
they are in the window or not
Situation Solution Example
Both end-points
Don’t know!
outside the window
Brute Force Line Clipping
• Brute force line clipping can be performed as
follows:
– Don’t clip lines with both
end-points within the
window
– For lines with one end-
point inside the window
and one end-point
outside, calculate the
intersection point (using the equation of the line)
and clip from this point out
Brute Force Line Clipping (cont…)
– For lines with both end-
points outside the window
test the line for
intersection with all of the
window boundaries, and
clip appropriately
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
Cohen-Sutherland Algorithm
• By numbering the bit positions in the region code as 1
through 4 from right to left, the coordinate regions can
be correlated with the bit positions as
» bit 1: left
» bit 2: right
» bit 3: below
» bit 4: above
• A value of 1 in any bit position indicates that the point
is in that relative position otherwise, the bit position is
set to 0.
• If a point is within the clipping rectangle, the region
code is 0000. A point that is below and to the left of
the rectangle has a region code of 0101.
Cohen-Sutherland Algorithm
S R(10,9)
F(11,7)
Q
P(3,4)
B(9,2)
Cohen-Sutherland: Labelling
• Every end-point is labelled with the
appropriate region code
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
Cohen-Sutherland: Lines In The Window
Lines completely contained within the window
boundaries have region code [0000] for both
end-points so are not clipped
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
Cohen-Sutherland: Lines Outside The Window
Any lines with a common set bit in the region
codes of both end-points can be clipped
– The AND operation can efficiently check this
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
Cohen-Sutherland: Other Lines
• Lines that cannot be identified as completely
inside or outside the window may or may not
cross the window interior
• These lines are processed as follows:
– Compare an end-point outside the window to a
boundary (choose any order in which to consider
boundaries e.g. left, right, bottom, top) and
determine how much can be discarded
– If the remainder of the line is entirely inside or
outside the window, retain it or clip it respectively
Cohen-Sutherland: Other Lines (cont…)
– Otherwise, compare the remainder of the line against
the other window boundaries
– Continue until the line is either discarded or a
segment inside the window is found
• We can use the region codes to determine which
window boundaries should be considered for
intersection
– To check if a line crosses a particular boundary we
compare the appropriate bits in the region codes of its
end-points
– If one of these is a 1 and the other is a 0 then the line
crosses the boundary
Cohen-Sutherland Examples
• Consider the line P9 to P10 below
– Start at P10
Window
– From the region codes wymax
of the two end-points we
know the line doesn’t
cross the left or right wymin
P [0000]
9
P ’ [0000]
boundary 10
– Calculate the
P [0100]
10
wxmin wxmax
intersection of the line with the bottom boundary to
generate point P10’
– The line P9 to P10’ is completely inside the window so
is retained
Cohen-Sutherland Examples (cont…)
Window
wymax
wymin
wxmin wxmax
Calculating Line Intersections
• Intersection points with the window
boundaries are calculated using the line-
equation parameters
– Consider a line with the end-points (x1, y1) and (x2,
y2)
– The y-coordinate of an intersection with a vertical
window boundary can be calculated using:
y = y1 + m (xboundary - x1)
where xboundary can be set to either wxmin or wxmax
Calculating Line Intersections (cont…)
boundary in turn
Original Area Clip Left Clip Right Clip Top Clip Bottom
Sutherland-Hodgman Polygon Clipping:
Four possible scenarios at each clipper
v2 v2 v2 v2
v1’ v1’
v1 v1
v1 v1
v2’ v2
v3
v2’’
v1’
v3’
Left Right Bottom Top
v1 Clipper Clipper Clipper Clipper
boundary in
question
S
No Points Saved Save Points I & P
Other Area Clipping Concerns
• Clipping concave areas can be a little more tricky
as often superfluous lines must be removed
Window Window
Window Window
• Clipping curves requires more work
– For circles we must find the two intersection points on
the window boundary
Summary
• Objects within a scene must be clipped to
display the scene in a window
• Because there are can be so many objects
clipping must be extremely efficient
• The Cohen-Sutherland algorithm can be used
for line clipping
• The Sutherland-Hodgman algorithm can be
used for area clipping