Chapter 6 – 2D Viewing Co-ordinate Systems. Cartesian – offsets along the x and y axis from (0.0) Polar – (r,) Graphic libraries mostly using Cartesian co-ordinates Any polar co-ordinates must be converted to Cartesian coordinates Four Cartesian co-ordinates systems in computer Graphics. 1. Modeling co-ordinates 2. World co-ordinates 3. Normalized device co-ordinates 4. Device co-ordinates 2 Modeling Co-ordinates Also known as local coordinate. Ex: where individual object in a scene within separate coordinate reference frames. Each object has an origin (0,0) So the part of the objects are placed with reference to the object’s origin. 3 World Co-ordinates The world coordinate system describes the relative positions and orientations of every generated objects. The scene has an origin (0,0). The object in the scene are placed with reference to the scenes origin. World co-ordinate scale may be the same as the modeling coordinate scale or it may be different. 4 Normalized Device Co-ordinates Output devices have their own co-ordinates. Co-ordinates values: The x and y axis range from 0 to 1 All the x and y co-ordinates are floating point numbers in the range of 0 to 1 This makes the system independent of the various devices coordinates. This is handled internally by graphic system without user awareness. 5 Device Co-ordinates Specific co-ordinates used by a device. Pixels on a monitor Points on a laser printer. mm on a plotter. The transformation based on the individual device is handled by computer system without user concern. 6 Two-Dimensional Viewing Window: •A world-coordinate area selected for display. •Window defines what is to be viewed Viewport: •An area on a display device to which a window is mapped. •Viewport defines where it is to be displayed Viewing transformation: •The mapping of a part of a world-coordinate scene to device coordinates. Most of the time, windows and viewports are usually rectangles in standard position(i.e aligned with the x and y axes). In some application, others such as general polygon shape and circles are also available However, other than rectangle will take longer time to process. 7 Window and viewports 8 Viewing Transformation In 2D (two dimensional) viewing transformation is simply referred as the window-to-viewport transformation or the windowing transformation. Mapping a window onto a viewport involves converting from one coordinate system to another. If the window and viewport are in standard position, this just involves translation and scaling. If the window and/or viewport are not in standard, then extra transformation which is rotation is required. 9 Window-To-Viewport Coordinate Transformation The sequence of transformations are: 1. Translate the window to the origin 2. Scale it to the size of the viewport 3. Translate the scaled object to the position of the viewport. 10 Window-To-Viewport Coordinate Transformation 11 Viewing Transformation y-world y-view window x-view x-world World and Viewport window 1 0 1 Normalised device 12 Window-To-Viewport Coordinate Transformation Window-to-Viewport transformation 13 Window-To-Viewport Coordinate Transformation xv - xvmin xw - xwmin = xvmax - xvmin yv – yvmin yvmax – yvmin xwmax - xwmin yw - ywmin = ywmax - ywmin YWmax From these two equations we derived xv = xvmin + (xw – xwmin)sx yv = yvmin + (yw – ywmin)sy YVmax xw,yw YWmin XWmin YVmin . xv,yv XVmin XVmax XWmax where the scaling factors are sx = xvmax – xvmin xwmax – xwmin sy = yvmax - yvmin ywmax - ywmin 14 Window-To-Viewport Coordinate Transformation Relative proportions of objects are maintained if the scaling factors are the same (sx = sy). Otherwise, world objects will be stretched or contracted in either x or y direction when displayed on output device. 15 The Viewing Pipeline 16 The Viewing Pipeline 17 Viewport-to-Normalized Device Coordinate Transformation From normalized coordinates, object descriptions can be mapped to the various display devices. When mapping window-to-viewport transformation is done to different devices from one normalized space, it is called workstation transformation. 18 Clipping • Clipping is a process of dividing an object into visible and invisible positions and displaying the visible portion and discarding the invisible portion. • Generally we have clipping algorithms for the following primitive types. – Point clipping – Line clipping – Area clipping (Polygon) Clipping Window World Coordinates ywmax The clipping window is mapped into a viewport. ywmin xwmin xwmax Viewport yvmax yvmin Viewport Coordinates xvmin xvmax 20 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 Cohen-Sutherland line clipping algorithm • This is one of the most popular line clipping algorithms. • This was introduced by Dan Cohen and Ivan Sutherland. • It was designed not only to find the endpoints very rapidly but also to reject even more rapidly any line that is clearly invisible. • This makes it a very good algorithm. Cohen-Sutherland line clipping algorithm • Assign a four bit code to all regions. • Every line end point in a picture is assigned a 4-bit binary code known as region code. • That identifies the location of the point relative to the boundaries of the clipping rectangle. • Each bit position in the region code is used to indicate one of the 4 rectangle coordinates positions of the point with respected to clipping window to the left, right, below and top. Cohen-Sutherland Clipping-Region Outcodes 1001 1000 1010 0001 0000 0010 0101 0100 0110 ymax ymin x min x max Cohen-Sutherland line clipping algorithm – – – – Bit 1 – left Bit 2 – right Bit 3 – below Bit 4 – top • A value of 1 in any bit position indicates that the point is in the relative position otherwise the bit position is set to zero. • If a point is within the clipping the region code is 0000. Cohen-Sutherland line clipping algorithm • If the code of both the endpoints are 0000 then the line is totally visible and hence draw the line. • Bit values in the region codes are determined by comparing endpoint co-ordinate values (x, y) to the clip boundaries. Cohen-Sutherland line clipping algorithm • Bit 1 is set to 1 if x < Xwmin • The other three bit values can be determined using similar comparison or calculating differences between endpoint co-ordinates and clipping boundaries. • Use the resultant sign of each differences calculation to set the corresponding value in the region code. Cohen-Sutherland line clipping algorithm – – – – • Bit 1 is the sign bit of x-xwmin Bit 2 is the sign bit of xwmax-x Bit 3 is the sign bit of y-ywmin Bit 4 is the sign bit of ywmax-y If any line have 1 in the same bit position in the region codes for each endpoint are completely outside the clipping rectangle, so we discard the line Cohen-Sutherland line clipping algorithm • For a line with end points co-ordinates (x1, y1) and (x2, y2) then the y coordinate of the intersection point with a vertical boundary can be obtained with the calculation • y=y1 + m(x-x1) (1) • Where the x value is set either to xwmin or xwmax. Cohen-Sutherland line clipping algorithm • • • Similarly if we are looking for the intersection with a horizontal boundary, the x co-ordinate can be calculated as x = x1 + 1/m (y-y1) (2) where y set either ywmin or ywmax. Cohen-Sutherland Clipping Trivial Acceptance: O(P0) = O(P1) = 0 1001 1000 ymax 1010 P1 0001 0000 0010 0100 0110 P0 ymin 0101 x min x max Cohen-Sutherland Clipping: Trivial Rejection: O(P0) & O(P1) 0 P0 1001 1000 1010 P1 ymax P0 0001 ymin 0000 0010 P0 0101 x min P1 P1 0100 0110 x max Cohen-Sutherland Clipping: 1001 O(P0) =0 , O(P1) 0 1000 1010 ymax P0 0001 0000 P1 0010 P0 P0 ymin 0100 0101 0110 P1 x min x max P1 Cohen-Sutherland Clipping: The Algorithm 1. Compute the outcodes for the two vertices 2. Test for trivial acceptance or rejection 3. Select a vertex for which outcode is not zero – There will always be one 4. Select the first nonzero bit in the outcode to define the boundary against which the line segment will be clipped. 5. Compute the intersection and replace the vertex with the intersection point 6. Compute the outcode for the new point and iterate Cohen-Sutherland Clipping A 1001 1000 1010 B ymax C 0001 0000 0010 0101 0100 0110 ymin x min x max Cohen-Sutherland Clipping A 1001 1000 1010 B ymax C 0001 0000 0010 0101 0100 0110 ymin x min x max Cohen-Sutherland Clipping: Example 1 1001 1000 1010 B ymax C 0001 0000 0010 0101 0100 0110 ymin x min x max Cohen-Sutherland Clipping 1001 1000 1010 E ymax D 0001 0000 ymin C 0010 B A 0100 0101 x min 0110 x max Cohen-Sutherland Clipping 1001 1000 1010 E ymax D 0001 0000 ymin C 0010 B 0100 0101 x min 0110 x max Cohen-Sutherland Clipping 1001 1000 1010 ymax D 0001 0000 ymin C 0010 B 0100 0101 x min 0110 x max Cohen-Sutherland Clipping 1001 1000 0001 0000 1010 ymax ymin C 0010 B 0100 0101 x min 0110 x max Example Clip the line with the boundaries (0, 0) and (15, 15) and the points are (2, 3) and (9, 10). Solution: Given (x1, y1) = (2, 3) & (x2, y2) = (9, 10) Here xwmin = 0, ywmin = 0 Xwmax = 15, ywmax = 15 Example • • • • Bit 1 is the sign bit of x-xwmin Bit 2 is the sign bit of xwmax-x Bit 3 is the sign bit of y-ywmin Bit 4 is the sign bit of ywmax-y • For (2, 3) the region code is 0000. • For (9, 10) the region code is 0000. • So the line should be drawn. Example • Clip the line with the boundaries (0, 0) and (15, 15) and the points are (2, -5) and (2, 18). • Solution: Region code for (2, -5) is 2-0=0, 15-2=0, -5-0=1, 15+5=0. So it is in 0100 region. So (2, -5) is not in the clipping region. Example • Region code for (2, 18) is 2-0=0, 15-2=0, 18-0=0, 15-18=1. The code is 1000. Clearly this point is out of clipping window. • Now (0100) & (1000) = 0000 • So we have to find the horizontal intercept point Example • Horizontal intercept: • point (x=1/m (ybottom-y1) + x1, ybottom); • m=y2-y1 / x2-x1 = 18-(-5) / 2-2 = 23/0 = ∞ • Then x= 1/∞ (0-(-5)) + 2 = 2 and y=ybottom=0. • The point is (2, 0). • So we have to draw the line from (2, 0). Example • Point is coordinate (x=1/m (ytop-y1) + x1, ytop) • m=y2-y1 / x2-x1 = ∞. • y = ytop = 15. • x= 1/∞ (15-2) + 2 =2. • The point is (2, 15). • Then draw the line between (2, 15) and (2, 0). Liang-Barsky Line Clipping Algorithm • Using Parametric equations of line, Cyrus and Beck developed an algorithm. • Later Liang and Barsky modified and make even faster parametric line-clipping algorithm. 54 Liang-Barsky Line Clipping Algorithm • Consider the parametric definition of a line: – x = x1 + u∆x – y = y1 + u∆y Where ∆x = (x2 - x1), ∆y = (y2 - y1), 0 ≤ u ≤ 1 • Mathematically, this means – xmin ≤ x1 + u∆x ≤ xmax – ymin ≤ y1 + u∆y ≤ ymax • Rearranging, we get – – – – -u∆x ≤ (x1 - xmin) u∆x ≤ (xmax - x1) -u∆y ≤ (y1 - ymin) u∆y ≤ (ymax - y1) • In general: u * pk ≤ qk p1=-∆x p2= ∆x p3= -∆y p4= ∆y q1= x1 - xmin q2= xmax - x1 q3= y1 - ymin q4= ymax - y1 55 Where K=1,2,3,4. Liang-Barsky Line Clipping: Observation 1. pk = 0 • Line is parallel to boundaries. • If for the same k, qk < 0, reject • Else, accept u>1 p2 u[0,1] p1 u<0 Liang-Barsky Line Clipping: Observation 2. pk < 0 • Line proceeds from outside to inside • rk = qk / pk u>1 • u1 = max(0, rk, u1) p2 u[0,1] iL u2 u1 p1 u<0 potential entrance potential exit Liang-Barsky Line Clipping: Observation 3. pk > 0 • Line proceeds from inside to out side • rk = qk / pk • u2 = min(1, rk, u2) u[0,1] u<0 p2 u>1 iT u2 p1 u1 potential entrance potential exit Liang-Barsky: Algorithm p2 wxmin u>1 iT wymax u[0,1] u2 u iL 1 p1 u<0 wymin potential entrance potential exit Liang-Barsky Line Clipping: Observation p2 u>1 u[0,1] If u1 > u2, the line is completely outside u<0 p1 iR u1= 0 iT u2= -1/6 Opps! u1 > u2 potential entrance potential exit Liang-Barsky Line Clipping: Observation If u1 > u2, the line is completely outside iR iT u>1 u[0,1] iB p2 iL u1=1.25 u2=1 Opps! u1 > u2 p1 potential entrance u<0 potential exit Advantage of Liang-Barsky Line Clipping This is more efficient than Cohen-Sutherland Alg, which computes intersection with clipping window borders for each undecided line, as a part of the feasibility tests. 62 Example 1 • Clipping window (1,2) and (9,8) Line end points A(11,6) and B(11,10) Solution: P1=-dx =0 q1=x1-xmin=10 P2= dx = 0 q2=xmax-x1=-2 P3=-dy =-4 q3=y1-ymin=4 P4= dy =4 q4=ymax-y1=2 P2=0 and q2<0 so discard the line 63 Liang-Barsky Line Clipping: Observation u>1 p2 u[0,1] p1 u<0 Example 2 • Clipping window (1,2) and (9,8) Line end points A(3,7) and B(3,10) Solution: P1=-dx =0 q1=x1-xmin=2 P2= dx = 0 q2=xmax-x1=6 P3=-dy =-3 q3=y1-ymin=5 P4= dy =3 q4=ymax-y1=1 u1=max(0,-5/3)=0 u2=min(1/3,1)=1/3 r3=-5/3 r4=1/3 65 Liang-Barsky Line Clipping: Observation u>1 u2=1/3 p2 u[0,1] p1 u<0 • Here u1<u2 so there is a visible section New endpoints of line: • x1’=x1+u1*dx=3 • y1’=y1+u1*dy=7 • x2’=x1+u2*dx=3 • y2’=y1+u2*dy=8. • Hence visible line will be from (3,7) to (3,8) 67 Example 3 • Clipping window (1,2) and (9,8) Line end points A(2,3) and B(8,4) Solution: P1=-dx =-6 q1=x1-xmin=1 P2= dx = 6 q2=xmax-x1=7 P3=-dy =-1 q3=y1-ymin=1 P4= dy =1 q4=ymax-y1=5 u1=max(0,-1/6,-1)=0 u2=min(7/6,5,1)=1 r1=-1/6 r2=7/6 r3=-1 r4=5 68 Liang-Barsky Line Clipping: Observation u>1 iR p2 u[0,1] p1 u<0 iT u2=1 u1=0 iL iB potential entrance potential exit • Here u1<u2 so there is a visible section • u1=0 and u2=1 • So line itself inside the clipping window. • Hence visible whole line. 70 Example 4 • Clipping window (1,2) and (9,8) Line end points A(-1,7) and B(11,1) Solution: P1=-dx =-12 q1=x1-xmin=-2 P2= dx = 12 q2=xmax-x1=10 P3=-dy =6 q3=y1-ymin=5 P4= dy =-6 q4=ymax-y1=1 u1=max(0,1/6,-1/6)=1/6 u2=min(5/6,5/6,1)=5/6 r1=1/6 r2=5/6 r3=5/6 r4=-1/6 71 • Here u1<u2 so there is a visible section New endpoints of line: • x1’=x1+u1*dx=1 • y1’=y1+u1*dy=6 • x2’=x1+u2*dx=9 • y2’=y1+u2*dy=2. • Hence visible line will be from (1,6) to (9,2) 72 Example 5 • Clip the line p1(-15,-30) , p2(30,60) against the window having diagonally opposite corners as (0,0) and (15,15) Solution: P1=-dx =-45 q1=x1-xmin=-15 r1=1/3 P2= dx = 45 q2=xmax-x1=30 r2=2/3 P3=-dy =-90 q3=y1-ymin=-30 r3=1/3 P4= dy =90 q4=ymax-y1=45 r4=1/2 u1=max(0,1/3,1/3)=1/3 u2=min(2/3,1/2,1)=1/2 73 Liang-Barsky: Algorithm p2 wxmin u>1 iR iT wymax u[0,1] u2=0.5 u =1/3 iL 1 iB p1 u<0 wymin potential entrance potential exit • Here u1<u2 so there is a visible section New endpoints of line: • x1’=x1+u1*dx=0 • y1’=y1+u1*dy=0 • x2’=x1+u2*dx=7.5 • y2’=y1+u2*dy=15. • Hence visible line will be from (0,0) to (7.5,15) 75 Nicholl-Lee-Nicholl Line Clipping Algorithm(NLN) • Generate region codes (Cohen-Suther.) & use trivial accept and reject • When trivial case fails , creates more regions around clipping window to avoid multiple line intersection calculations. • Performs fewer comparisons and divisions than Cohen-Sutherland and Liang-Barsky, but cannot be extended to 3D. 76 P0 P0 P0 • Examine first where the starting point p0 is located. • We will consider only 3 regions. • If point is in any of the other six regions then perform transformation such that that point will convert in any of this 3 regions. 77 Case 1: Inside case for P0 The location of Pend in each T L P0 B R region defines what edge the line P0 , Pend is intersecting. 78 Case 2: edge case for P0 Detecting whether Pend is in LT L P0 L LR any of regions L is immediate. L LB Else, Pend is detected for being positioned in any of LB, LR or LT, case where P0 , Pend is clipped with left border and bottom, right or top border, resp. 79 The slope of P0 , Pend is Pend LT compared to P0 , Pcorner L P0 L LR L LB for each corner to find the region of Pend . • Pend is in region LT if slop P0Ptr < slop P0Pend < slop P0Ptl Yt-Y1 Xr-X1 < Y2-Y1 < Yt-Y1 X2-X1 XL-X1 Once one of LT, LR or LB regions is found, intersection point with appropriate border is calculated.80 The slope of P0 , Pend is Pend LT compared to P0 , Pcorner L P0 L LR L LB for each corner to find the region of Pend . P0 , Pend is entierely clipped if Pend is positioned outside the regions. 81 Case 3: corner case for P0 P0 P0 T TR L T T TR L LB L LR LB TB There are two cases, depending on whether P0 is closer to left or top borders. 82 Polygon Clipping using line clipping not good enough Not a polygon Polygon Clipping • Need to maintain closed polyline boundary! Note, clipping yields 2 polygons Trivial Accept & Reject Accept Reject Need Clipping bounding box easy to compute Sutherland-Hodgman Clipping • Ivan Sutherland, Gary W. Hodgman: Reentrant Polygon Clipping. Communications of the ACM, vol. 17, pp. 32-42, 1974 • Basic idea: – Consider each edge of the viewport individually – Clip the polygon against the edge equation Sequentially Clip polygon across each side 1 3 2 clip right clip left 5 4 clip bottom clip top Sutherland-Hodgman Polygon Clipping • Input each edge (vertex pair) successively. • Output is a new list of vertices. • Each edge goes through 4 clippers. Sutherland-Hodgman Polygon Clipping: Four possible scenarios at each clipper 1. If first input vertex is outside, and second is inside, output the intersection and the second vertex 2. If both input vertices are inside, then just output second vertex 3. If first input vertex is inside, and second is outside, output is the intersection 4. If both vertices are outside, output is nothing outside inside v2 outside inside v2 v1’ outside inside v2 inside v2 v1’ v1 v1 v1 Outside to inside: Output: v1’ and v2 outside Inside to inside: Output: v2 Inside to outside: Output: v1’ v1 Outside to outside: Output: nothing Complete Example 2 2 2 2' 2' 2' 3 2' ' I – In 3' O - Out 3’ 1 Left Clip (1,2) (I/I) → {2} (2,3) (I/O) → {2'} (3,1) (O/I) → {3',1} 1 Right Clip 3’ Bottom Clip 1’ 1 Top Clip (2,2'): (I/I) → {2'} (2',3'): (I/I) → {3'} (2',3'): (I/O) → {2''} (3',1): (I/I) → {1} (3',1): (O/O) → {} (2'',1'): (I/I) → {1'} (1,2): (I/I) → {2} (1,2): (O/I) → {1',2} (1',2): (I/I) → {2} (2,2'): (I/I) → {2'} (2,2'): (I/I) → {2'} (2',2''): (I/I) → {2''} Disadvantage of Sutherland - Hodgeman • Convex polygons are correctly clipped. • Concave polygons may not be correctly clipped so we will use Weiler-Atherton. • Properties of Convex polygon. 1. Every internal angle is less than or equal to180 degrees. 2. Every line segment between two vertices remains inside or on the boundary of the polygon. 91 Weiler-Atherton Polygon Clipping • Developed as a method for identifying visible surfaces • It can be applied with arbitrary polygon-clipping • region. • Not always proceeding around polygon edges • Sometimes follows the window boundaries For clockwise processing of polygon vertices • For an outside-to-inside pair of vertices, follow the polygon boundary. • For an inside-to-outside pair of vertices, follow the window boundary in clockwise direction. 92 Weiler-Atherton Polygon Clipping 93 Weiler-Atherton Polygon Clipping • For clockwise processing of polygon vertices • For an outside-to-inside pair of vertices, follow the polygon boundary. • For an inside-to-outside pair of vertices, follow the window boundary in clockwise direction. 1 1’ 2 1 1’ 2 1 3 1’ 3’ 2 In -> In Add end vertex 1’ 3 4 Out -> In Add clip vertex Add end vertex 1 In -> Out Add clip vertex Cache old direction 2 3 4 3’ Follow clip edge until (a) new crossing found (b) reach vertex already added Weiler-Atherton Polygon Clipping • For clockwise processing of polygon vertices • For an outside-to-inside pair of vertices, follow the polygon boundary. • For an inside-to-outside pair of vertices, follow the window boundary in clockwise direction. 1 4 1’ 3’ 2 1 3 1’ 3’ 4 2 1’ 3 3’ 4’ 4’ 2 1 3 Out -> In Add clip vertex Add end vertex 1’ 6’ 6’ 6 2 3’ 4’ 5 5 Continue from cached vertex and direction 1 3 5 6 Follow clip edge until In -> Out (a) new crossing found Add clip vertex (b) reach vertex already Cache old direction added Weiler-Atherton Polygon Clipping • For clockwise processing of polygon vertices • For an outside-to-inside pair of vertices, follow the polygon boundary. • For an inside-to-outside pair of vertices, follow the window boundary in clockwise direction. 1’ 2 1 1’ 3’ 2 1 3 3’ 4’ 4’ 6 6’ 5 Continue from cached vertex and direction 1’ 6 6’ 2 3 5 Nothing added Finished 3’ 4’ 6’ 3 5 Final Result: 2 unconnected polygons Other Clipping • Curve clipping • Use bounding rectangle to test for overlap with a rectangular clip window. 97 Text Clipping • There are several techniques that can be used to provide text clipping in a graphics packages. • The choice of clipping method depends on how characters are generated and what requirements we have for displaying character strings. 98 Text Clipping All-or-none string-clipping • If all of the string is inside a clip window, we keep it. • Otherwise the string is discarded. 99 Text Clipping All-or-none character-clipping Here we discard only those characters that are not completely inside the window 100 Text Clipping Clip the components of individual characters • We treat characters in much the same way that we treated lines. • If an individual character overlaps a clip window boundary, we clip off the parts of the character that are outside the window 101 Done with Clipping • Point Clipping – Easy, just do inequalities • Line Clipping – Cohen-Sutherland Any – Liang-Barsky – Nicholl-Lee-Nicholl • Polygon Clipping – Sutherland-Hodgeman – Weiler-Atherton • Text Clipping Question…?