Upload
flora-hodge
View
261
Download
1
Tags:
Embed Size (px)
Citation preview
Chapter 6
Windowing and clipping
Window - The rectangle defining the part of the world we wish to display.
Viewport - The rectangle on the raster graphics screen (or interface window for “window” displays) defining where the image will appear. (Default is usually entire screen or interface window.)
Chapter 6
Windowing & clipping
Windowing is showing on the viewport parts of the real seen that appears from the window.
Clipping is not showing on the viewport parts of the real seen outside the window boundaries.
Clipping needs to be fast, so often implemented in hardware. There are techniques for clipping primitive operations.
Chapter 6
TerminologyWorld Coordinate System (Object Space) -
Representation of an object measured in some physical units.
Screen Coordinate System (Image Space) - The space within the image is displayed
Interface Window - The visual representation of the screen coordinate system for “window” displays (coordinate system moves with interface window).
Viewing Transformation - The process of going from a window in world coordinates to a viewport in screen coordinates.
Chapter 6
Windows and ViewportsWindow
Interface Window
Viewport
Information outsidethe viewport isclipped away
Chapter 6
Viewing Transformation
Choose Window inWorld Coordinates
Clip to sizeof Window
Translate toorigin
Scale to size of Viewport Translate to proper position on screen (Interface Window)
Chapter 6
Notes on Viewing TransformationPanning - Moving the window about the worldZooming - Reducing the window sizeAs the window increases in size, the image in
the viewport decreases in size and vice versa
Beware of aspect ratio.
Chapter 6
Viewing Transformation Example(10, 30) (50, 30)
(10, 5) (50, 5)
(0,0)
(0, 1)
Viewportwanted
(0.5, 0.5)
(1, 0)
(0.5, 1)
(0, 0.5)
1 0 -100 1 -50 0 1
1) Translate window to origin
1 0 00 1 0.50 0 1
3) Translate to proper coordinates
1/80 0 00 1/50 00 0 1
2) Scale to correct size
X scale = 0.5/40 = 1/80
Chapter 6
Clipping Points to a WindowClipping Points to a Window
Notice P is inside and q is outside, so they q will be clipped
Suppose the window has (xmin, ymin) bottom left vertex & (xmax,ymax) as its upper right vertex, then a point (x,y) is
VISIBLE IF xmin < x < xmax; ymin < y < ymax
.
PQ
.
xmin
xmax
ymin
ymax
Chapter 6
Clipping Lines to a WindowClipping Lines to a Window
A
B
C
D
E
F G
H
I
J
Can we quickly recognise lines which need clipping?
Chapter 6
Clipping to a WindowClipping to a WindowLooking at end-points gives us a quick
classification:–Both ends visible => line visible (AB)–One end visible, other invisible => line partly
visible (CD)–Both ends invisible:
• If both end-points lie to same side of window edge, line is invisible (EF)
• Otherwise, line may be invisible (IJ) or partially visible (GH)
Chapter 6
Line Clipping Algorithms
Brute Force Method - Solve simultaneous equations for intersections of lines with window edges is impractical.
(xmax, ymax)
(xmin, ymin)
Chapter 6
Cohen-Sutherland AlgorithmRegion 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.
Chapter 6
Cohen-Sutherland Line Clipping AlgorithmCohen-Sutherland Line Clipping Algorithm
Each end-point is coded according to its position relative to the window–Four-bit code assigned as follows:
Bit 1 Set if x < xmin
Bit 2 Set if x > xmax
Bit 3 Set if y < ymin
Bit 4 Set if y > ymax
1001 1000 1010
0001 0000 0010
0101 0100 0110
Chapter 6
Cohen-Sutherland Line Clipping AlgorithmCohen-Sutherland Line Clipping Algorithm
Notice: if – Both end-point codes 0000 => VISIBLE (trivially
accepted) – Logical AND = NOT 0000 => INVISIBLE (trivially
rejected)– Logical AND = 0000 => INVISIBLE or PART VISIBLE
To clip P1P2:– Check if P1P2 totally visible or invisible– If not, for each edge in turn (left/right/bottom/top):(i) Is edge crossed ? (the corresponding bit is set for ONE
of the points, say P1)(ii) If so, replace P1 with intersection with edge.
Chapter 6
ExampleExample
Clip againstleft, right,bottom, topboundaries in turn.
P1: 1001P2: 0100
P1
P2
x=xmin
P1’
First clip to leftedge, givingP1’P2
Chapter 6
ExampleExample
P2
x=xmin
P1’ P1’: 1000P2 : 0100
No need to clipagainst right edge
Clip againstbottom gives P1’P2’
Clip against topgives P1’’P2’
P2’
P1’’
Chapter 6
Calculating the IntersectionCalculating the IntersectionTo calculate intersection of P1P2 with, say left
edge:
Left edge: x = xmin
Line : y - y2 = m (x-x2)
where m = (y2 - y1) / (x2 -x1)
Thus intersection is (xmin, y*)
where y* = y2 + m (xmin - x2)
P1
P2
Chapter 6
Other Line ClippersOther Line ClippersCohen-Sutherland is efficient for quick
acceptance or rejection of lines.
Less so when many lines need clipping.
Other algorithms are:–Liang-Barsky–Nicholl-Lee-Nicholl
see:Hearn and Baker for details
Chapter 6
Line Parametric Equation Parametric equation of a line between twp points
(x1,y1) (x2,y2)
x = x1 + u (x2−x1) & y = y1 + u (y2−y1)
Where u Є [0, 1]. When u = 0 the point is the first (x1, y1) & if u =1 the point is (x2, y2)
Chapter 6
Line Parametric Equation
Find the range of the parameter [u1, u2] so that:xmin <= x <= xmax −> xmin <= x1+u(x2−x1) <=xmax
ymin <= y <= ymax −> ymin <= y1+u(y2−y1) <= ymax
0<= u <=1
The above inequalities allow us to find the final range of the parameter u −> [u1,u2] !!
Liang−Barsky algorithm can do it fast.
Chapter 6
Liang-Barsky Line Clipping I
Let dx = x2 − x1, dy = y2 − y1, Then the inequalities:x1+u(x2−x1)>= xmin => u*(−dx) <= x1 − xminx1+u(x2−x1)<= xmax => u*( dx) <= xmax − x1y1+u(y2−y1)>= ymin => u*(−dy) <= y1 − yminy1+u(y2−y1)<= ymax => u*( dy) <= ymax − y1
We can rewrite the above equations as:u. pk <= qk
where k corresponds to window boundaries 0,2,3,4 (left, right, bottom, top)
p0 = −dx, q0 = x1 − xminp1 = dx, q1 = xmax − x1p2 = −dy, q2 = y1 − yminp3 = dy, q3 = ymax − y1
Chapter 6
Liang-Barsky Line Clipping II
If pk <0, u >= qk/pk , so we know the line proceeds from outside to inside of the window boundary k, thus
==> update u1 !!if pk > 0 u <= qk / pk, so we know the line
proceeds from inside to outside of the window boundary k, thus
==> update u2 !!else if pk=0 (which means dx or dy = 0)The line is parallel to the window boundaryif qk <0 trivially reject the line
Chapter 6
Clipping AlgorithmNotice that u1 can’t be greater than u2, so after
updating u1 or u2 if u1 > u2, reject the line.
Cliptest(p, q, *u1, *u2)
if p < 0
r = q / p;
if r > u2 return false
else if r > u1 *u1 = r
else if p > 0
r = q / p;
if r < u1 return false
else if r < u2 *u2 = r else if q < 0 return falseReturn true
Clipline(p1, p2)
u1 = 0; u2 = 1; dx=p2.x–p1.x; dy =p2.y-p1.y;
If cliptest(−dx, x1 − xmin, &u1, &u2)
If cliptest(dx, xmax − x1, &u1, &u2) If cliptest(−dy, y1 − ymin, &u1, &u2) If cliptest(dy, ymax − y1, &u1, &u2) if u2 < 1
p2.x = p1.x + u2*dx p2.y = p1.y + u2*dy
else u1 > 0 p1.x + = u1 * dx p1.y + = u1 * dyReturn updated values of p1 & p2
Chapter 6
Polygon ClippingPolygon ClippingBasic idea: clip each polygon side - but
care needed to ensure clipped polygon is closed.
A
B
C
D
E
F
Chapter 6
Sutherland-Hodgman AlgorithmSutherland-Hodgman AlgorithmThis algorithm clips a polygon against
each edge of window in turn, ALWAYS keeping the polygon CLOSED
Points pass through as in a pipeline
INPUT: List of polygon vertices
OUTPUT: List of polygon vertices on visible side of window edge
Chapter 6
Sutherland-Hodgman AlgorithmSutherland-Hodgman AlgorithmConsider a polygon side: starting vertex S, end vertex P and window edge x = xmin.
What vertices are output?
xmin xmin xmin xmin
S
P
S
P
S
P
S
P
I I
OUTPUT: - I, P I P
Chapter 6
Example - Sutherland-Hodgman AlgorithmExample - Sutherland-Hodgman Algorithm
Take each edge in turn start with left edge.
Take each point in turn:
(i) Input point and call it P- thus P = A
(ii) If P is first point:- store it as P1
- output if visible (not inthis particular example)- let S = P
A
B
C
D
E
F
Input: A B C D E F
Chapter 6
Example - Sutherland-Hodgman AlgorithmExample - Sutherland-Hodgman Algorithm
A
B
C
D
E
F
Input: A B C D E F
(iii) If P not first point,then if SP crosses windowedge:- compute intersection I- output Ioutput P if visible(iv) let S = P
Output: A’ B C D E F
A’
Chapter 6
Example - Sutherland-Hodgman AlgorithmExample - Sutherland-Hodgman Algorithm
Finally, if some pointshave been output, thenif SP1 crosses windowedge:- compute intersection I- output I
A
B
C
D
E
F
Input: A B C D E F
A’
G
Output: A’ B C D E F G
Chapter 6
Example - after clipping to left edgeExample - after clipping to left edge
B
C
D
E
F
A’
G
The result of clippingagainst the left edge
Chapter 6
Example - clip against right edgeExample - clip against right edge
B
C
D
E
F
A’
G
E’
E’’
B
C
D
F
A’
G E’
E’’
INPUT: A’ B C D E F G OUTPUT: A’ B C D E’ E’’ F G
Chapter 6
Example - clip against bottom edgeExample - clip against bottom edge
B
C
D
F
A’
G E’
E’’E’’’
F’
B
C
D
A’
G E’
E’’’F’
INPUT: A’ B C D E E’ E’’ F G OUTPUT: A’ B C D E’ E’’’ F’ G
Chapter 6
Example - clip against top edgeExample - clip against top edge
B
C
D
A’
G E’
E’’’F’
C
D
A’
G E’
E’’’F’
A’’ B’ A’’ B’
INPUT: A’ B C D E E’ E’’’ F’ G OUTPUT: A’ A’’ B’ C D E’ E’’’ F’ G
Chapter 6
Polygon clipping algorithm typedef enum { Left, Right, Bottom, Top} Edge; # define N_EDGE 4int clipPolygon(dcpt wmin,dcpt wmax,int n,
wcpt2 * pin,wcpt2 * pout){ wcpt2 * first [N_EDGE] = {0,0,0,0}, // first point clipped
s[N_EDGE];int I, cnt=0;for(i=0;i<n;i++) clippoint (pin[i],left,wmin,wmax,pout,&cnt,first,s);closeclip (wmin,wmax,pout,&cnt,first ,s);return cnt;}
Chapter 6
Clip point functionvoid clippoint (wcPt2 p, Edge b, dcPt
wMin, dcPt wMax, wcPt2 * pOut, int * cnt, wcPt2 * first[]., wcPt2 * s)
{ wcPt2 ipt;// If no previous point exists for this edge,
save this point. if (!first [b]) first[b] = &p;
Else // Previous point exists. If 'p' and
previous point cross edge,find intersection. Clip against next boundary, if any. Ifno more edges, add intersection to output list
if (cross (p, s[b]., b, wMin, wMax)) { iPt = intersect (p, s[b], b, wMin,
wMax);
if (b < Top) clipPoint (iPt, b+l, wMin, wMax,
pOut, cnt, first, s);else { pOut [*cnt] = iPt; (*cnt)++; }}s[b] = p; // Save 'p' as most recent
point for this edge// For all, if point is ‘inside' proceed to
next clip edge, if any. If no more edges, add intersection to output list
if (inside (p, b, wMin, wMax)) if (b < Top) clippoint (p, b+l, wMin, wMax,
pOut, cnt, first, s); else{
pOut [*cnt]= p; (*cnt(++; }
}
Chapter 6
Insideint inside (wcPt2 p, Edge b, dCPt wMin, dcPt wMax){ switch (b) {
case Left: if (p.x < wMin.x) return (FALSE); break;
case Right: if (p.x > wMax.x) return (FALSE); break;
case Bottom: if (p.y < wMin.y) return (FALSE); break;
case Top: if (p.y > wMax.y) return (FALSE); break;
}return (TRUE(;}
Chapter 6
Cross: Line p1 p2 with edge b
int cross (wcPt2 pI, wcPt2 p2, Edge b, dCPt wMin, dcPt wMax(
{// both end points are inside edge bif (inside (pI, b, wMin, wMax) ==
inside (p2, b, wMin, wMax)) return (FALSE(;else return (TRUE(;}
Chapter 6
Point of Intersection wcPt2 intersect (wcPt2 pI, wcPt2 p2,
Edge b, dcPt wMin, dcPt wMax(
{ wcPt2 iPt; float m ;if (pl. x != p2.x)
m = (pl.y -p2.y) / (pl. x -p2.x);switch) b){case Left:
iPt.x = wMin.x;iPt.y = p2.y + (wMin.x -
p2.x) * m;break;
case Right:iPt.x = wMax.x;iPt.y = p2.y + (wMax.x -
p2.x) * m;
break;
case Bottom:iPt.y = wMin.y; if (pl.x != p2.x) iPt.x = p2.x + (wMin.y -
p2.y) / m ;else iPt.x = p2.x; break;
case top:ipt.y = wMax.y;if (pl.x != p2.x) iPt..x = p2.x + (wMax.y -
p2.y) / m;
else iPt.x = p2.x;break;
}return (iPt);}
Chapter 6
Close clipvoid closeClip (dcPt wMin, dcPt wMax, wcPt2 * pOUt,int * cnt, wcPt2 *
first[], wcPt2 * s){ wcPt2 I; Edge b; for (b = Left; b <= Top; b++){
if (cross (s[b], *first[b], b, wMin, wMax)){ i = intersect (s [b], *first [b], b, wMin, wMax);
if (b < Top( clippoint (i, b+l, wMin, wMax,POut, cnt, first, s); else{ pOut [*cnt] = i;
(*cnt)++; }
} }
}
Chapter 6
Text Clipping
All or none string clipping All or none character clipping Clip the components of the individual
characters