Presentation is loading. Please wait.

Presentation is loading. Please wait.

Windowing and clipping

Similar presentations


Presentation on theme: "Windowing and clipping"— Presentation transcript:

1 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

2 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

3 Terminology World 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

4 Windows and Viewports Window Interface Window Viewport
Information outside the viewport is clipped away Chapter 6

5 Viewing Transformation
Choose Window in World Coordinates Clip to size of Window Translate to origin Scale to size of Viewport Translate to proper position on screen (Interface Window) Chapter 6

6 Notes on Viewing Transformation
Panning - Moving the window about the world Zooming - Reducing the window size As the window increases in size, the image in the viewport decreases in size and vice versa Beware of aspect ratio. Chapter 6

7 Viewing Transformation Example
(10, 30) (50, 30) (10, 5) (50, 5) (0,0) (0, 1) Viewport wanted (0.5, 0.5) (1, 0) (0.5, 1) (0, 0.5) 1/ / 2) Scale to correct size X scale = 0.5/40 = 1/80 3) Translate to proper coordinates 1) Translate window to origin Chapter 6

8 Clipping Points to a Window
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 . P Q xmin xmax ymin ymax Notice P is inside and q is outside, so they q will be clipped Chapter 6

9 Clipping Lines to a Window
Can we quickly recognise lines which need clipping? E F A B C D G H I J Chapter 6

10 Clipping to a Window Looking 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

11 Line Clipping Algorithms
(xmax, ymax) (xmin, ymin) Brute Force Method - Solve simultaneous equations for intersections of lines with window edges is impractical. Chapter 6

12 Cohen-Sutherland Algorithm
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. Chapter 6

13 Cohen-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

14 Cohen-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

15 Example P1 Clip against left, right, bottom, top P1’
boundaries in turn. P1’ P1: 1001 P2: 0100 First clip to left edge, giving P1’P2 P2 x=xmin Chapter 6

16 Example P1’: 1000 P2 : 0100 P1’ P1’’ No need to clip
against right edge Clip against bottom gives P1’P2’ Clip against top gives P1’’P2’ P2’ P2 x=xmin Chapter 6

17 Calculating the Intersection
To 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) P2 P1 Chapter 6

18 Other Line Clippers Cohen-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

19 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

20 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

21 Liang-Barsky Line Clipping I
Let dx = x2 − x1, dy = y2 − y1, Then the inequalities: x1+u(x2−x1)>= xmin => u*(−dx) <= x1 − xmin x1+u(x2−x1)<= xmax => u*( dx) <= xmax − x1 y1+u(y2−y1)>= ymin => u*(−dy) <= y1 − ymin y1+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 − xmin p1 = dx, q1 = xmax − x1 p2 = −dy, q2 = y1 − ymin p3 = dy, q3 = ymax − y1 Chapter 6

22 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 boundary if qk <0 trivially reject the line Chapter 6

23 Clipping Algorithm Notice 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 if r < u1 return false else if r < u2 *u2 = r else if q < 0 return false Return 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 * dy Return updated values of p1 & p2 Chapter 6

24 Polygon Clipping Basic idea: clip each polygon side - but care needed to ensure clipped polygon is closed. B C A D E F Chapter 6

25 Sutherland-Hodgman Algorithm
This 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

26 Sutherland-Hodgman Algorithm
Consider a polygon side: starting vertex S, end vertex P and window edge x = xmin. What vertices are output? xmin xmin xmin xmin P P P P I I S S S S OUTPUT: I, P I P Chapter 6

27 Example - 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 in this particular example) - let S = P B C A D E F Input: A B C D E F Chapter 6

28 Example - Sutherland-Hodgman Algorithm
B (iii) If P not first point, then if SP crosses window edge: - compute intersection I - output I output P if visible (iv) let S = P A’ C A D E F Output: A’ B C D E F Input: A B C D E F Chapter 6

29 Example - Sutherland-Hodgman Algorithm
B Finally, if some points have been output, then if SP1 crosses window edge: - compute intersection I - output I A’ C A D G E F Output: A’ B C D E F G Input: A B C D E F Chapter 6

30 Example - after clipping to left edge
B The result of clipping against the left edge A’ C D G E F Chapter 6

31 Example - clip against right edge
B B A’ A’ C C D E’ D E’ G G E E’’ E’’ F F INPUT: A’ B C D E F G OUTPUT: A’ B C D E’ E’’ F G Chapter 6

32 Example - clip against bottom edge
F’ F’ F INPUT: A’ B C D E E’ E’’ F G OUTPUT: A’ B C D E’ E’’’ F’ G Chapter 6

33 Example - clip against top edge
B A’’ B’ A’’ B’ A’ A’ C C D D E’ E’ G G E’’’ E’’’ F’ F’ INPUT: A’ B C D E E’ E’’’ F’ G OUTPUT: A’ A’’ B’ C D E’ E’’’ F’ G Chapter 6

34 Polygon clipping algorithm
typedef enum { Left, Right, Bottom, Top} Edge; # define N_EDGE 4 int 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

35 Clip point function void 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. If no 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)) clippoint (p, b+l, wMin, wMax, pOut, cnt, first, s); else{ pOut [*cnt]= p; (*cnt(++; Chapter 6

36 Inside int 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); case Bottom: if (p.y < wMin.y) return (FALSE); case Top: if (p.y > wMax.y) return (FALSE); } return (TRUE(; Chapter 6

37 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 b if (inside (pI, b, wMin, wMax) == inside (p2, b, wMin, wMax)) return (FALSE(; else return (TRUE(; } Chapter 6

38 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; 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; iPt..x = p2.x + (wMax.y - p2.y) / m; } return (iPt); Chapter 6

39 Close clip void 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

40 Text Clipping All or none string clipping
All or none character clipping Clip the components of the individual characters Chapter 6


Download ppt "Windowing and clipping"

Similar presentations


Ads by Google