Title: Clipping
1Clipping
2Learning outcomes for this lecture
- You will
- understand
- What is meant by clipping
- 2D clipping concepts
- 3D clipping concepts
- Different kinds of clipping
- The Cohen-Sutherland 2D region-coding clipping
technique - Be able to
- calculate Cohen-Sutherland 2D region-coding
3Clipping
- Clipping means
- Identifying portions of a scene that are inside
(or outside) a specified region - Examples
- Multiple viewports on a device
- Deciding how much of a games world the player can
see
4Clipping
- Clipping means
- Identifying portions of a scene that are inside
(or outside) a specified region - Examples
- Multiple viewports on a device
- Deciding how much of a games world the player can
see
Player cant see this far yet
5Requirements for clipping
- Is (x, y) inside or outside a given region
- For 2D graphics the region defining what is to be
clipped is called - The clip window
Clip window
6Interior and exterior clipping
- interior clipping
- what is to be saved is inside the clip window
- exterior clipping
- what is to be saved is outside clip window
Interior clipping- keep point P2
P2(x2, y2)
7Interior and exterior clipping
- We shall assume interior clipping for now
- But you must be aware of exterior clipping too
Exterior clipping- keep point P1
Clip window
P1(x1, y1)
8Overview of types of clipping
- All-or-none clipping
- If any part of object outside clip windowwhole
object is rejected - Point clipping
- Only keep points inside clip window
- Line clipping
- Only keep segment of line inside clip window
- Polygon clipping
- Only keep sub-polygons inside clip window
9Before and after POINT clipping
10Before and after LINE clipping
11Before and after POLYGON clipping
12Cohen-Sutherland 2D clipping
- 4 regions are defined outside the clip window
- TOP
- BOTTOM
- LEFT
- RIGHT
13The 4-bit codes
- A 4-bit code is assigned to each point
- This code is sometimes called a Cohen-Sutherland
region code - LEFT bit 1 binary 0001
- RIGHT bit 2 binary 0010
- BOTTOM bit 3 binary 0100
- TOP bit 4 binary 1000
- So a point coded 0001 is LEFT of the clip window,
etc.
14Cohen-Sutherland region codes
- The point (65, 50) is above and to the right of
the clip window, - Therefore gets the code 1010
15Cohen-Sutherland region codes
- 1000 indicates the TOP region
- 0010 indicates the RIGHT region
- the 4 bit Cohen-Sutherland codeis formed by a
logical OR of all region codes - 1000 TOP
- OR 0010 RIGHT
- ------------------
- 1010
16How the codes are useful
- Why bother encoding points with 4-bit
Cohen-Sutherland region codes? - Can make some quick decisions
- If code is zero (0000) point is inside window
- If code is non-zero point is outside window
- For the two endpoints of a line
- If codes for both endpoints are zero,whole line
is inside window - If (logical) AND of codes for endpoints is
non-zero,the endpoints must have a region in
common So the whole line must be outside clip
window
17Some lines and their endpoint codes
TOP BOTTOM RIGHT LEFT
- Line1 codes are 1001 and 0000
- Line2 codes are 0000 and 0000 ltlt inside
- Line3 codes are 1000 and 0100
- Line4 codes are 1010 and 0110 ltlt outside
18Clipping from region codes
- To clip a line based on region codes
- Choose an endpoint that is OUTSIDE clip window
(I.e. non-zero code) - Check first bit (TOP) if set, clip intersection
from point to TOP of clip window - Check second bit (BOTTOM) and so on
19Line Clipping Algorithms
- Goal avoid drawing primitives that are outside
the viewing window. - The most common case clipping line segments
against a rectangular window
20Line Clipping Algorithms
- Three cases
- Segment is entirely inside the window - Accept
- Segment is entirely outside the window - Reject
- Segment intersects the boundary - Clip
21Point Classification
- How can we tell if a point is inside a
rectangular window?
22Line Segment Clipping
- If both endpoints are inside the window, the
entire segment is inside trivial accept - If one endpoint is inside, and the other is
outside, line segment must be split. - What happens when both endpoints are outside?
23Cohen-Sutherland Algorithm
- Assign a 4-digit binary outcode to each of the 9
regions defined by the window
24Cohen-Sutherland Algorithm
25Cohen-Sutherland Algorithm
- Compute the outcodes C1 and C2 corresponding to
both segment endpoints. - If ((C1 C2) 0) Trivial Accept
- If ((C1 C2) ! 0) Trivial Reject
- Otherwise, split segment into two parts. Reject
one part, and repeat the procedure on the
remaining part. - What edge should be intersected ?
26Example
Outcode(A) 0000 Outcode(D) 1001
No trivial accept/reject
Clip (A,D) with y ymax, splitting it into (A,B)
and (B,D) Reject (B,D) Proceed with (A,B)
27Example
E
D
C
Outcode(A) 0100 Outcode(E) 1010
B
A
No trivial accept/reject
Clip (A,E) with y ymax, splitting it into (A,D)
and (D,E) Reject (D,E) Proceed with (A,D)
28Example
D
C
Outcode(A) 0100 Outcode(D) 0010
B
A
No trivial accept/reject
Clip (A,D) with y ymin, splitting it into (A,B)
and (B,D) Reject (A,B) Proceed with (B,D)
29Example
D
C
Outcode(B) 0000 Outcode(D) 0010
B
No trivial accept/reject
Clip (B,D) with x xmax, splitting it into (B,C)
and (C,D) Reject (C,D) Proceed with (B,C)
30Clipping a line
- Choose point P2 (code is 1000)
- Clip from point to TOP of clip window
31Clipping a line
- Choose point P1 (code is 0110)
- Bit 1 (TOP) not set
- Bit 2 (BOTTOM) set so clip bottom
32Clipping a line
- Bit 3 (RIGHT) set so clip right
- Bit 4 (LEFT) not set so clipping finished
33How to clip a line
- Need to know
- End points of line(x1, y1) (x2, y2)
- X or Y value definingclip window edge
- (similar triangles and line gradient)
- m (y2 y1) / (x2 x1) // (gradient)
- newx xwmin
- newy y1 m(xwmin x1)
xmin
(x2, y2)
(x1, y1)
(newx, newy)
34Clipping Lines
(xl, yt)
(xr, yt)
A point is visible if xl lt x lt xr and yb lt y lt yt
(x, y)
(xl, yb)
(xr, yb)
- A line is completely visible if both of its end
points are in the window. - Brute Force Method - Solve simultaneous equations
for intersections of lines with window edges.
35Cohen-Sutherland Clipping
- Region Checks Trivially reject or accept lines
and points. - Fast for large windows (everything is inside) and
for small windows (everything is outside). - Each vertex is assigned a four-bit outcode.
36Cohen-Sutherland Clipping (cont.)
Bit 1 Above Bit 2 Below Bit 3 Right Bit 4
Left
- Bit 1 1 if y gt yt, else 0
- Bit 2 1 if y lt yb, else 0
- Bit 3 1 if x gt xr, else 0
- Bit 4 1 if x lt xl, else 0
37Cohen-Sutherland Clipping (cont.)
Bit 1 Above Bit 2 Below Bit 3 Right Bit 4
Left
- A line can be trivially accepted if both
endpoints have an outcode of 0000. - A line can be trivially rejected if any
corresponding bits in the two outcodes are both
equal to 1. (This means that both endpoints are
to the right, to the left, above, or below the
window.) - if (outcode 1 outcode 2) ! 0000, trivially
reject!
38Clipping Lines Not Accepted or Rejected
- In the case where a line can be neither trivially
accepted nor rejected, the algorithm uses a
divide and conquer method.
Line AD 1) Test outcodes of A and D --gt cant
accept or reject. 2) Calculate intersection point
B, which is on the dividing line between the
window and the above region. Form new line
segment AB and discard BD because above the
window. 3) Test outcodes of A and B.
Reject. Line EH ??
D
C
B
A
H
E
F
G
39Polygon Clipping
- Polygons can be clipped against each edge of the
window one edge at a time. Window/edge
intersections, if any, are easy to find since the
X or Y coordinates are already known. - Vertices which are kept after clipping against
one window edge are saved for clipping against
the remaining edges. Note that the number of
vertices usually changes and will often increase.
40Polygon Clipping Algorithm
- The window boundary determines a visible and
invisible region. - The edge from vertex i to vertex i1 can be one
of four types - Exit visible region - save the intersection
- Wholly outside visible region- save nothing
- Enter visible region - save intersection and
endpoint - Wholly inside visible region - save endpoint
41Polygon clipping issues
- The final output, if any, is always considered a
single polygon. - The spurious edge may not be a problem since it
always occurs on a window boundary, but it can be
eliminated if necessary.
42Pipelined Polygon Clipping
Clip Top
Clip Right
Clip Bottom
Clip Left
- Because polygon clipping does not depend on any
other polygons, it is possible to arrange the
clipping stages in a pipeline. the input polygon
is clipped against one edge and any points that
are kept are passed on as input to the next stage
of the pipeline. - This way four polygons can be at different stages
of the clipping process simultaneously. This is
often implemented in hardware.
43Clipping in the openGL pipeline
- A figure illustrating the openGL graphics
pipeline
clipping is done here
44Text clipping
- strategies for dealing with text objects
- Consider following example
- Assume interiorclipping
45Student Exercise
- Draw three possible results of interior clipping
of the string ABC - Try to name what typeof clipping you have done
in each case
46Student Exercise
- All-or-none string clipping
- No text is retained after clippingsince part of
text is outside clip window - Whole String ABC is clipped as a single object
47Student Exercise
- All-or-none character clipping
- Only B and C are retained after
clippingsince part of A is outside clip window - Each character is clipped as a separate object
48Student Exercise
- Character component clipping
- All of B and C are retained, and those parts
of A inside clip window are also retained - Each component of each character is clipped as a
separate object
49Introduction to 3D clipping
- In 3D a clip volume needs to be defined
- Example for perspective 3D
- We define
- Point to project to(viewers eye)
50Introduction to 3D clipping
- In 3D a clip volume needs to be defined
- Example for perspective 3D
- We define
- Window to define directionand amount of view
51Introduction to 3D clipping
- In 3D a clip volume needs to be defined
- Example for perspective 3D
- We define
- 2 clip planes to definemax and min distanceto
viewer
52Introduction to 3D clipping
- In 3D a clip volume needs to be defined
- Example for perspective 3D
53Introduction to 3D clipping
- Result is a finite clip volume of space
- This shape is a named a frustum
54Canonical View Volume
- 3-D Extension of 2-D Cohen-Sutherland Algorithm,
Outcode of six bits. A bit is true (1) when the
appropriate condition is satisfied - Bit 1 - Point is above view volume y gt -z
- Bit 2 - Point is below view volume y lt z
- Bit 3 - Point is right of view volume x gt -z
- Bit 4 - Point is left of view volume x lt z
- Bit 5 - Point is behind view volume z lt -1
- Bit 6 - Point is in front of view volume z gt zmin
553D Line Clipping
- A line is trivially accepted if both endpoints
have a code of all zeros. - A line is trivially rejected if the bit-by-bit
logical AND of the codes is not all zeros. - Otherwise Calculate intersections.
- On the y z plane from parametric equation of
the line - y0 t( y1 - y0) z0 t( z1 z0)
- Solve for t and calculate x and y. Already know
z y
56Summary clipping in general
- Clipping means
- Identifying portions of a scene that are inside
(or outside) a specified region - Clipping is defined using a clip window
- Interior clipping keep things inside clip window
/ Exterior cl. outside
57- Types of clipping
- All-or-none (AON) clipping
- if any part of object outside clip area then
reject whole object - Point / line / polygon / text clipping (AON
string/char, component)
58- 3D clipping involves defining a clip volume
- A finite volume of space defined by front and
back clipping planes, the view plane window and
projection method(more details in lecture on 3D)
59Summary
- A 4-bit encoding for regions related to clip
window - TOP
- BOTTOM
- LEFT
- RIGHT
- Code can be used to easily
- To determine if a point in inside clip window
- If endpoints of a line mean whole lineis fully
inside or fully outside clip window - Can implement in C using char
- 8-bit, unsigned, data type