06-Clipping

advertisement
CSE 423 Computer Graphics
Clipping
•
•
•
•
Cohen Sutherland Algorithm (Line)
Cyrus-Back Algorithm (Line)
Sutherland-Hodgeman Algorithm (Polygon)
Cohen Sutherland Algorithm (3d)
Point Clipping
clip
rectangle
y = ymax
(xmax, ymax)
x = xmin
(xmin , ymin )
x = xmax
y = ymin
For a point (x,y) to be inside the clip rectangle:
xmin  x  xmax
ymin  y  ymax
Point Clipping
clip
rectangle
y = ymax
(xmax, ymax)
(x1, y1)
x = xmin
(xmin , ymin )
x = xmax
y = ymin
For a point (x,y) to be inside the clip rectangle:
xmin  x  xmax
ymin  y  ymax
Line Clipping
clip
rectangle
Cases for clipping lines
Line Clipping
B
B
A
A
clip
rectangle
Cases for clipping lines
Line Clipping
D
D'
D'
C
C
B
B
A
A
clip
rectangle
Cases for clipping lines
Line Clipping
F
D
D'
D'
C
C
B
B
A
A
E
clip
rectangle
Cases for clipping lines
Line Clipping
F
D
D'
D'
C
C
B
H
B
E
H'
A
G'
clip
rectangle
H'
A
G
Cases for clipping lines
G'
Line Clipping
F
D
D'
D'
C
C
B
H
B
E
H'
J
A
G'
G'
J'
clip
rectangle
H'
A
G
I'
I
Cases for clipping lines
Line Clipping
Clipping Lines by Solving Simultaneous Equations
(x1, y1)
(x, y)
(x1, y1)
(xb, yb)
(xa, ya)
(xb, yb)
(x0, y0)
(x, y)
(xa, ya)
(x0, y0)
(xc, yc)
(xd, yd)
clip
rectangle
x  x0  tline  x1  x0 ,
x  xa  tedge  xb  xa ,
(xc, yc)
(xd, yd)
clip
rectangle
y  y0  tline  y1  y0 
y  ya  tedge  yb  ya 
Cohen-Sutherland Algorithm
The Cohen-Sutherland Line-Clipping
Algorithm performs initial tests on a line to
determine whether intersection calculations
can be avoided.
1.
2.
3.
First, end-point pairs are checked for Trivial
Acceptance.
If the line cannot be trivially accepted, region
checks are done for Trivial Rejection.
If the line segment can be neither trivially
accepted or rejected, it is divided into two
segments at a clip edge, so that one segment
can be trivially rejected.
These three steps are performed iteratively
until what remains can be trivially accepted or
rejected.
Cohen-Sutherland Algorithm
1001
bit 0 : y  ymax
bit 1 : y  ymin
1000
1010
0001
0000
0010
0101
0100
0110
clip
rectangle
Region outcodes
bit 2 : x  xmax
bit 3 : x  xmin
Cohen-Sutherland Algorithm
1. A line segment can be trivially
accepted if the outcodes of both the
endpoints are zero.
2. A line segment can be trivially
rejected if the logical AND of the
outcodes of the endpoints is not zero.
3. A key property of the outcode is that
bits that are set in nonzero outcode
correspond to edges crossed.
Cohen-Sutherland Algorithm
E
D
clip
rectangle
C
B
A
An Example
Cohen-Sutherland Algorithm
E
D
clip
rectangle
C
B
An Example
Cohen-Sutherland Algorithm
D
clip
rectangle
C
B
An Example
Cohen-Sutherland Algorithm
clip
rectangle
C
B
An Example
Parametric Line-Clipping
(1) This fundamentally different (from CohenSutherland algorithm) and generally more
efficient algorithm was originally published by
Cyrus and Beck.
(2) Liang and Barsky later independently
developed a more efficient algorithm that is
especially fast in the special cases of upright
2D and 3D clipping regions.They also
introduced more efficient trivial rejection tests
for general clip regions.
The Cyrus-Back Algorithm
Outside of clip region
Inside of clip rectangle
Edge Ei
PEi
Pi t   PEi
P1


N i  Pt   PEi  0

P0


N i  Pt   PEi  0

N i  Pt   PEi  0
Ni
Line P0 P1 : Pt   P0  P1  P0 t


N i  Pt   PEi  0


 N  P  P  P t  P   0
N  P  P 
t 
 N i  P0  P1  P0 t  PEi  0
i
0
1
i
0
0
Ei
Ei
 N i  P0  P1 
t 

N i  P0  PEi
 Ni  D
, D  P  P
0
1
The Cyrus-Back Algorithm
Outside of clip region
Inside of clip rectangle
Edge Ei
PEi
Pi t   PEi
P1


N i  Pt   PEi  0



N i  Pt   PEi  0
P0

N i  Pt   PEi  0
Ni
t

N i  P0  PEi
 Ni  D

t exists when
1 N i  0
2
3
D  0  P0  P1
Ni  D  0
The Cyrus-Back Algorithm
P1
t=1
Line 1
PE
P1
t=1
Line 2
P1
t=1
PL
PL
PL
P0
PE
t=0
PL
Line 3
P0
t=0
PE
PE
P0
t=0
PE = Potentially Entering
Clip
rectangle
PL = Potentially Leaving
N i  D  0  PE
N i  D  0  PL
 Angle  90
 Angle  90
The Cyrus-Back Algorithm
Precalculate Ni and PEi for each edge
for (each line segment to be clipped) {
if (P1 == P0)
line is degenerated, so clip as a point;
else {
tE = 0; tL = 1;
for (each candidate intersection with a clip edge) {
if (Ni • D != 0) { /* Ignore edges parallel to line */
calculate t;
use sign of Ni • D to categorize as PE or PL;
if (PE) tE = max(tE , t);
if (PL) tL = min(tL , t);
}
}
if (tE > tL) return NULL;
else return P(tE) and P(tL) as true clip intersection;
}
}
Polygon Clipping
Example
Polygon Clipping
Example
Polygon Clipping
Example
Sutherland-Hodgeman Algo.
Clip
Clip
ClipAgainst
Against
Against
TheBottom
Clipped
Right
Left
Top Clipping
Clipping
Clipping
Polygon
Boundary
Boundary
Boundary
Initial Condition
4 Cases of Polygon Clipping
Inside
p:second output
s
Outside
i:first output
p
s
Polygon
being
clipped
p
s
s
p:output
(no output)
i:output
Clip
boundary
Case 4321
Algorithm
Input vertex P
No
Close Polygon entry
First Point
Does SF
intersect E?
Yes
Yes
F=P
Does SP intersect
E?
Compute
Intersection I
No
Output
vertex I
Yes
Compute
Intersection Point
I
Exit
Output
vertex I
S=P
Yes
Is S on left
side of E?
Output
vertex S
NO
Exit
No
3D Clipping
• Both the Cohen-Sutherland and Cyrus-Beck clipping algorithm
readily extend to 3D.
• For Cohen-Sutherland algorithm use two extra-bit in outcode for
incorporating z < zmin and z > zmax regions
Thank You
Download