15 clipping
TRANSCRIPT
-
7/29/2019 15 Clipping
1/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 1/2215 Clipping
Lecture 15:
ClippingCOMP 175: Computer GraphicsNovember 1, 2011
-
7/29/2019 15 Clipping
2/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 2/2215 Clipping
Library trip
Camera assignment
problemsdebugging methods
Midterm Eval
General discussion on how we can do better
Discussions
-
7/29/2019 15 Clipping
3/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 3/2215 Clipping
Last week we discussed some simple examples of clippingin Camera
We said that OpenGL takes care of a lot of that for free
But clipping is an important part of computer graphics:
Clipping
-
7/29/2019 15 Clipping
4/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 4/2215 Clipping
Line Clipping
(x min , y min )
(x max , y max )
(x, y )
-
7/29/2019 15 Clipping
5/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 5/2215 Clipping
Parametric Line Formulation for Clipping
-
7/29/2019 15 Clipping
6/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 6/2215 Clipping
Clip Rectangle
Cohen-Sutherland Line Clipping in 2D
-
7/29/2019 15 Clipping
7/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 7/2215 Clipping
If we can neither triviallyaccept or reject, then we dodivide-and-conquerSubdivide line into twosegments and test again
Cohen-Sutherland Algorithm
Cliprectangle
DC
BA
E
F
G
H
I
Use a clip edge to cut lineUse outcodes to choose which edge is crossed
The bits that are different between outcodes will tell us which edge to examinePick an order for checking edges: top bottom right left Compute the intersection point
Clip edge will be axis-aligned, so we can fix either the x or the yCan substitute into the line equation
Iterate for the newly created line segment, might need multiple passes(e.g., E-I at H)
-
7/29/2019 15 Clipping
8/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 8/2215 Clipping
Cohen-Sutherland AlgorithmAlgorithm (float x0, y0, x1, y1)
ComputeOutCode(x0, y0, outcode0);ComputeOutCode(x1, y1, outcode1); repeat
check for trivial reject or trivial accept pick the point that is outside the clip rectangle
if TOP thenx = x0 + (x1 x0) * (ymax y0)/(y1 y0); y = ymax;
else if BOTTOM thenx = x0 + (x1 x0) * (ymin y0)/(y1 y0); y = ymin;
else if RIGHT theny = y0 + (y1 y0) * (xmax x0)/(x1 x0); x = xmax;
else if LEFT theny = y0 + (y1 y0) * (xmin x0)/(x1 x0); x = xmin;
if (x0, y0 is the outer point) then x0 = x; y0 = y; ComputeOutCode(x0, y0, outcode0)
else x1 = x; y1 = y; ComputeOutCode(x1, y1, outcode1)
until doneDrawRectangle(xmin, ymin, xmax, ymax) DrawLine (x0, y0, x1, y1)
-
7/29/2019 15 Clipping
9/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 9/2215 Clipping
Similar to 2DDivide volume into 27 regions (picture a Rubiks cube) 6-bit outcode records results of 6 bounds tests
1 st / 2 nd bits: back & front planes3 rd / 4 th bits: top & bottom planes
5 th / 6 th bits: right & left planes
The same operation as the 2D case in using outcodes
Cohen-Sutherland Algorithm in 3D
Top plane
001000 (above)000000 (below)
Back plane
000000 (in front)100000 (behind)
Right plane
000000 (to left of)
000010 (to right of)
Bottom plane
000000 (above)
000100 (below)
Front plane
010000 (in front)000000 (behind)
Left plane
000001 (to left of)
000000 (to right of)
-
7/29/2019 15 Clipping
10/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 10/2215 Clipping
Sutherland-Hodgman Polygon Clipping
-
7/29/2019 15 Clipping
11/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 11/2215 Clipping
Cyrus-Beck / Liang-Barsky Parametric Line Clipping
-
7/29/2019 15 Clipping
12/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 12/2215 Clipping
Parametric Line Clipping
-
7/29/2019 15 Clipping
13/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 13/2215 Clipping
Parametric Line Clipping, Using Dot Product
Find t Note that if t is less than 0 or greater than 1, then theintersection occurs outside of the line segment
-
7/29/2019 15 Clipping
14/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 14/2215 Clipping
Parametric Line Clipping, Solving t
-
7/29/2019 15 Clipping
15/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 15/2215 Clipping
solve for t:t = N i [P 0 P E i ] / (-N i D)
Caveats: for this equation to be true, it must be
that:N i 0 (that is, the normal should not be 0; this couldoccur only as a mistake)D 0 (that is, P 1 P 0)N
i D 0 (edge E
iand line D are not parallel; if they are,
no intersection)
The algorithm needs to check for these
Parametric Line Clipping, Solving t
-
7/29/2019 15 Clipping
16/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 16/2215 Clipping
When applied to line-clipping, there will be multiple t values (a t value for each clip edge test)
Eliminate t values outside of [0, 1]Determine if the intersection is considered potentiallyentering (PE) or potentially leaving (PL)
Problem, how can you tell??
Parametric Line Clipping, Against A Polygon
PE
PL
-
7/29/2019 15 Clipping
17/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 17/2215 Clipping
Parametric Line Clipping, Against A Polygon
-
7/29/2019 15 Clipping
18/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 18/2215 Clipping
Pre-calculate Ni and select PEi for each edge;for each line segment to be clippedif P1 = P0 then line is degenerate so clip as a point;elsebegintE = 0; tL = 1;for each candidate intersection with a clip edgeif Ni D 0 then {Ignore edges parallel to line}begincalculate t; {of line and clip edge intersection}use sign of Ni D to categorize as PE or PL; if PE then tE = max(tE,t);if PL then tL = min(tL,t);
endif tE > tL then return nilelse return P(tE) and P(tL) as true clip intersections
end
Pseudocode
-
7/29/2019 15 Clipping
19/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 19/2215 Clipping
Questions?
-
7/29/2019 15 Clipping
20/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 20/2215 Clipping
The intersection test can beextended to 3D
Here we try to find theintersection point (I) between aline segment (AC) and a cube
Problem is the same as the 2Done, and same or similaralgorithms can be applied
Other Types of Clipping
-
7/29/2019 15 Clipping
21/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 21/2215 Clipping
Clipping works on edges of triangles (see right)
New vertices might have to becreated (e.g., points a and b)
Since clipping works with linesegments, it is similar to theCyrus-Beck algorithm, but in3D
Other Types of Clipping
-
7/29/2019 15 Clipping
22/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 22/2215 Clipping
Sutherland-Hodgman Polygon Clipping
Keeps polygon info intact
-
7/29/2019 15 Clipping
23/23
COMP 175 | COMPUTER GRAPHICS
Remco Chang 23/2215 Clipping
Questions?