143
Chapter 6 2D Viewing Transformation

2d viewing.pptx

Embed Size (px)

DESCRIPTION

computer graphics

Citation preview

Chapter 6

Chapter 62D Viewing Transformation2D Viewing TransformationDefinition2D Viewing PipelineApproachesAspect RatioBasic W-to-V mapping steps:Define a world windowDefine a viewportCompute a mapping from window to viewportWindow or Clipping window is the selected section of a scene that is displayed on a display window. It is a finite region from World Coordinate System(area of picture thats selected for viewing/display).

View port is the window where the object is viewed on the output device. It is a finite region from Device Coordinate System to which window is mapped(area where picture is to be displayed).World Coordinate System(Object Space):The space in which the application model is defined. The representation of an object is measured in some abstract units.

Screen Coordinate System(Image Space): The space in which the image is displayed. Usually measured in pixels.

2D Viewing Transformation

When we display a scene only those objects within a particular window are displayed,everything outside the window is clipped.1. Definitions: It is defined as a process for displaying views of a two-dimensional picture on an output device:Specify which parts of the object to display (clipping window, or world window, or viewing window)Where on the screen to display these parts (view port).

2D Viewing TransformationThe mapping of the window (modeling coordinates) to viewport (device coordinates) is a 2D viewing transformation

6A world-coordinate area selected for display is called a window (defines what is to be viewed).An area on a display device to which a window is mapped is called a viewport (defines where it is to be displayed). Often, windows and viewports are rectangles in standard position, with the rectangle edges parallel to the coordinate axes. Other window or viewport geometries, such as general polygon shapes and circles, are used in some applications, but these shapes take longer to process.

2D viewing transformation is simply referred to as the window-to-viewport transformation or the windowing transformation2D Viewing TransformationDefinition2D Viewing PipelineApproachesAspect Ratio2. 2D viewing pipeline Construct world-coordinate scene using modeling-coordinate transformationsConvert world-coordinates to viewing coordinatesTransform viewing-coordinates to normalized-coordinates (ex: between 0 and 1, or between -1 and 1)Map normalized-coordinates to device-coordinates.CONSTRUCT WORLD COORDINATE SCENECONVERT WORLD CORD TOVIEW CORDMAP VIEW CORDTONORM VIEW COMAP NORM VIEW CORDTO DEVICE CORDMCWCVCNCDC2D Viewing Transformation

Bring window to origin first

2D Viewing TransformationDefinition2D Viewing PipelineApproachesAspect RatioFebruary 28, 2014Computer Graphics123. ApproachesTwo main approaches to 2D viewing Transformation areDirect Approach Normalized Approach2D Viewing TransformationFebruary 28, 2014Computer Graphics133.1.Direct Approach Mapping the clipping window into a view port which may be normalizedIts involves translating the origin of the clipping window to that of the view portscaling the clipping window to the size of the view portWe can drive it by three methods

2D Viewing TransformationFebruary 28, 2014Computer Graphics14

(from Donald Hearn and Pauline Baker)2D Viewing TransformationFebruary 28, 2014Computer Graphics152D Viewing TransformationMethod 1: Let P (xw,yw) be any point in the window, which is to be mapped to P (xv,yv) in the associated view port. The two steps required are:Translation Tv that takes (xwmin,ywmin) to (xvmin,yvmin) ,

where v = (xvmin xwmin)I + (yvmin ywmin)J

Scaling by following scaling factors about point (xvmin,yvmin)

To convert window area into the viewport area: Perform a scaling using a fixed-point position of (xwmin, ywmin) that scales the window area to the size of the viewport. Translate the scaled window area to the position of the viewport.Relative proportions of the objects are maintained if the scaling factors are the same (sx = sy). Otherwise world objects will be stretched or contracted in either the x or y direction when displayed on the output device.

February 28, 2014Computer Graphics172D Viewing Transformation

MethodStep1: Translate1(vxmin)-2(wxmin)=-11-2=-1

Step2:Then Scale about fixed point

2,21,1February 28, 2014Computer Graphics192D Viewing TransformationMethod 2: We get the same result if we perform

2D Viewing Transformation

20February 28, 2014212D Viewing TransformationMethod 3: Let P (xw,yw) be any point in the window, which is to be mapped to P (xv,yv) in the associated view port. To maintain the same relative placement in the view port as in window, we require that

22The Viewing PipelinedWindow to Viewport Coordinate TransformationSolving these expressions for the viewport position (xv, yv), we have

xv = xvmin + (xw xwmin)sxyv = yvmin + (yw ywmin)sy Equation 1where the scaling factors aresx = xvmax xvmin / xwmax xwminsy = yvmax yvmin / ywmax ywmin

23The Viewing PipelinedWindow to Viewport Coordinate TransformationEquation 1 can also be derived with a set of transformations that coverts the window area into the viewport area: Perform a scaling using a fixed-point position of (xwmin, ywmin) that scales the window area to the size of the viewport. Translate the scaled window area to the position of the viewport.xv = xvmin + (xw xwmin)sx= xvmin + sx.xw xwmin*sx= sx.xw + xvmin xwmin*sx where tx= xvmin xwmin*sx

Similarly,yv = yvmin + (yw ywmin)sy= sy.yw + yvmin ywmin*sy where ty= yvmin ywmin*sy

xw yw 1Xv sx 0 xvmin xwmin*sxYv 0 sy yvmin ywmin*sy1 0 0 12D Viewing Transformation3.2 Normalized Approach(workstation transformation)Mapping the clipping window into a normalized squareIt involvestransforming the clipping window into a normalized squareThen clipping in normalized coordinates (ex: -1 to 1) or (ex: 0 to 1) Then transferring the scene description to a view port specified in screen coordinates. Finally positioning the view port area in the display window. Thus V = W.N, where N is a transformation that maps window to normalized view port and W maps Normalized points to view port.Mapping selected parts of a scene in normalized coordinates to different video monitors with workstation transformations

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 iscalled workstation transformation.

(from Donald Hearn and Pauline Baker)2D Viewing Transformation

(from Donald Hearn and Pauline Baker)2D Viewing Transformation2D Viewing TransformationDefinition2D Viewing PipelineApproachesAspect Ratio4. Aspect RatioSince viewing involves scaling, so undesirable distortions may be introduced when sx sy Aspect ratio is defined as (ymax-ymin)/ (xmax-xmin)If aw= av => no distortionIf aw> av => Horizontal spanningIf aw< av => Vertical spanning2D Viewing TransformationWindow (aw = 1)Centered Sub view(av=3/4)

Exercise 1Find the normalization transformation that maps a window defined by (1,1) to (3,5) on to The view port that is entire normalized device.

Has lower left corner (0,0) and upper right corner as (1/2,1/2)

Exercise 2Find the complete viewing transformation that First maps a window defined by (1,1) to (10,10) on to a view port of size(1/4,0) to (3/4,1/2) in normalized device spaceThen maps a window of (1/4,1/4) to (1/2,1/2) in normalized device space to view port of (1,1) to (10,10)

Exercise 2First maps a window defined by (1,1) to (10,10) on to a view port of size(1/4,0) to (3/4,1/2) in normalized device space

(1,1)(10,10)P(x,y)(1,1)(0,0)

1/4.,0,1/2Exercise 2Then maps a window of (1/4,1/4) to (1/2,1/2) in normalized device space to view port of (1,1) to (10,10)

(10,10)(1,1)P(x,y)(1,1)(0,0)

Exercise 2First maps a window defined by (1,1) to (10,10) on to a view port of size(1/4,0) to (3/4,1/2) in normalized device space

Then maps a window of (1/4,1/4) to (1/2,1/2) in normalized device space to view port of (1,1) to (10,10)

2D ClippingIntroductionPoint ClippingLine ClippingPolygon/Area ClippingText Clipping2D Clipping1. Introduction: A scene is made up of a collection of objects specified in world coordinatesWorld Coordinates372D ClippingWhen we display a scene only those objects within a particular window are displayedwymaxwyminwxminwxmaxWindowWorld Coordinates382D ClippingBecause drawing things to a display takes time we clip everything outside the windowwymaxwyminwxminwxmaxWorld CoordinatesWindow392D ClippingwymaxwyminwxminwxmaxWindowWorld Coordinates401.1 Definitions:Clipping is the process of determining which elements of the picture lie inside the window and are visible.Shielding is the reverse operation of clipping where window act as the block used to abstract the view.

2D Clipping2D Clipping1.2 Example:For the image below consider which lines and points should be kept and which ones should be clipped against the clipping windowwymaxwyminwxminwxmaxWindowP1P2P3P6P5P7P10P9P4P8421.3 Applications:Extract part of a defined scene for viewing.Drawing operations such as erase, copy, move etc.Displaying multi view windows. object boundaries.Identify visible surfaces in 3D views.2D Clipping2D Clipping1.4 Types of clipping:Three types of clipping techniques are used depending upon when the clipping operation is performed

a. Analytical clippingClip it before you scan convert itused mostly for lines, rectangles, and polygons, where clipping algorithms are simple and efficient

2D Clippingb.ScissoringClip it during scan conversiona brute force techniquescan convert the primitive, only write pixels if inside the clipping regioneasy for thick and filled primitives as part of scan line fillif primitive is not much larger than clip region, most pixels will fall insidecan be more efficient than analytical clipping.2D Clippingc. Raster Clipping Clip it after scan conversionrender everything onto a temporary canvas and copy the clipping regionwasteful, but simple and easy, often used for text1.5 Levels of clipping:Point ClippingLine ClippingPolygon ClippingArea ClippingText ClippingCurve Clipping

2D Clipping2D ClippingIntroductionPoint ClippingLine ClippingPolygon/Area ClippingText ClippingPoint ClippingSimple and Easy a point (x,y) is not clipped if:wxmin x wxmax & wymin y wymax

otherwise it is clippedwymaxwyminwxminwxmaxWindowP1P2P5P7P10P9P4P8ClippedPoints Within the Window are Not ClippedClippedClippedClipped492D ClippingIntroductionPoint ClippingLine ClippingPolygon/Area ClippingText ClippingLine ClippingIt is Harder than point clippingWe first examine the end-points of each line to see if they are in the window or notBoth endpoints inside, line trivially acceptedOne in and one out, line is partially insideBoth outside, might be partially inside

yminymaxxminxmax51Line ClippingSituationSolutionExampleBoth end-points inside the windowDont clipOne end-point inside the window, one outsideMust clipBoth end-points outside the windowDont know!522D Cohen Sutherland Line Clipping AlgorithmsCohen-Sutherland Line Clipping An efficient line clipping algorithmThe key advantage of the algorithm is that it vastly reduces the number of line intersections that must be calculated.Dr. Ivan E. Sutherland co-developed the Cohen-Sutherland clipping algorithm. Sutherland is a graphics giant and includes amongst his achievements the invention of the head mounted display.

Cohen-Sutherland Line ClippingTwo phases Algorithm

Phase I: Identification PhaseAll line segments fall into one of the following categoriesVisible: Both endpoints lies insideInvisible: Line completely lies outsideClipping Candidate: A line neither in category 1 or 2

Phase II: Perform ClippingCompute intersection for all lines that are candidate for clipping.55Cohen-Sutherland Line ClippingPhase I: Identification Phase: 2d World space is divided into 9 regions(3d into 27 regions,6 bits) based on the window boundariesEach region has a unique four bit region code that identifies the location of the point relative to the boundaries of the clipping rectangle.Region codes indicate the position of the regions with respect to the window10011000101000010000Window0010010101000110abovebelowrightleft3,ymax2,ymin

1,xmax

0,xmin

Region Code Legend56abovebelowrightleft3,ymax2,ymin

1,xmax

0,xmin

0 means point is left of viewport

1 means point is right of viewport

2 means point is below viewport

3 means point is labove viewport

01x minrightleftx maxleftrighty minabove bottomy maxbottomaboveAssigning Codes Let point (x,y) is be given code b3b2b1b0:bit 3 = 1 (1000) if y> wymax bit 2 = 1 (0100) if ywxmax bit 0 = 1(0001) if x< wxmin

Cohen-Sutherland Line ClippingEvery end-point is labelled with the appropriate region codewymaxwyminwxminwxmaxWindowP3 [0001]P6 [0000]P5 [0000]P7 [0001]P10 [0100]P9 [0000]P4 [1000]P8 [0010]P12 [0010]P11 [1010]P13 [0101]P14 [0110]593 casesCase 1(code1 | code2) =0If Bitwise OR is 0,trivially accept and get out of loop.Case 2(code1 & code2) = non-0If Bitwise AND( atleast 1 code contain 1 bit) is 0,trivially reject and get out of loop.Case 3failed both tests, so calculate the line segment to clip from an outside point to an intersection with clip edge. If the line is not completely inside or completely outside, perform intersection calculations with one or more clipping boundaries.

Cohen-Sutherland Line ClippingVisible Lines: Lines completely contained within the window boundaries have region code [0000] for both end-points so are not clippedwymaxwyminwxminwxmaxWindowP3 [0001]P6 [0000]P5 [0000]P7 [0001]P10 [0100]P9 [0000]P4 [1000]P8 [0010]P12 [0010]P11 [1010]P13 [0101]P14 [0110]61Cohen-Sutherland Line ClippingInvisible Lines: Any line with a common set bit in the region codes of both end-points can be clipped completelyThe AND operation can efficiently check this Non Zero means InvisiblewymaxwyminwxminwxmaxWindowP3 [0001]P6 [0000]P5 [0000]P7 [0001]P10 [0100]P9 [0000]P4 [1000]P8 [0010]P12 [0010]P11 [1010]P13 [0101]P14 [0110]62Cohen-Sutherland Line ClippingClipping Candidates: Lines that cannot be identified as completely inside or outside the window may or may not cross the window interior. These lines are processed in Phase II. If AND operation result in 0 the line is candidate for clippingwymaxwyminwxminwxmaxWindowP3 [0001]P6 [0000]P5 [0000]P7 [0001]P10 [0100]P9 [0000]P4 [1000]P8 [0010]P12 [0010]P11 [1010]P13 [0101]P14 [0110]63Cohen-Sutherland Line ClippingWe can use the region codes to determine which window boundaries should be considered for intersectionTo check if a line crosses a particular boundary we compare the appropriate bits in the region codes of its end-pointsIf one of these is a 1 and the other is a 0 then the line crosses the boundary.64Cohen-Sutherland Line ClippingNote: p3,p4 is a special case and is solved in same way as we solve case 3(intersection lines that crosses clipping edges).

so calculate the line segment to clip from an outside point to an intersection with clip edge

wymaxwyminwxminwxmaxWindowP3 [0001]P7 [0001]P10 [0100]P9 [0000]P4 [1000]P8 [0010]65Cohen-Sutherland Clipping AlgorithmPhase II: Clipping Phase: Lines that are in category 3 are now processed as follows:Compare an end-point outside the window to a boundary (choose any order in which to consider boundaries e.g. left, right, bottom, top) and determine how much can be discardedIf the remainder of the line is entirely inside or outside the window, retain it or clip it respectivelyOtherwise, compare the remainder of the line against the other window boundariesContinue until the line is either discarded or a segment inside the window is found66To check if a line crosses a particular boundary we compare the appropriate bits in the region codes of its end-pointsIf one of these is a 1 and the other is a 0 then the line crosses the boundary

Cohen-Sutherland Line ClippingIntersection points with the window boundaries are calculated using the line-equation parametersConsider a line with the end-points (x1, y1) and (x2, y2)The y-coordinate of an intersection with a vertical window boundary can be calculated using:y = y1 + m (xboundary - x1)where xboundary can be set to either wxmin or wxmaxThe x-coordinate of an intersection with a horizontal window boundary can be calculated using:x = x1 + (yboundary - y1) / mwhere yboundary can be set to either wymin or wymax68

y = 2 + 1/3 (5 4) = 2.33Intersection point at xwmax is (5, 2.33)If the value of m for an intersection with a rectangle boundary edge is outside the range 0 to 1, the line does not intersect with that boundary. If the value of m is within the range from 0 to 1, the line segment intersect with that boundary.Cohen-Sutherland Line ClippingExample1: Consider the line P9 to P10 belowStart at P10From the region codes of the two end-points we know the line doesnt cross the left or right boundaryCalculate the intersection of the line with the bottom boundary to generate point P10The line P9 to P10 is completely inside the window so is retainedwymaxwyminwxminwxmaxWindowP10 [0100]P9 [0000]P10 [0000]P9 [0000]70Cohen-Sutherland Line ClippingExample 2: Consider the line P3 to P4 belowStart at P4From the region codes of the two end-points we know the line crosses the left boundary so calculate the intersection point to generate P4

The line P3 to P4 is completely outside the window so is clippedwymaxwyminwxminwxmaxWindowP4 [1001]P3 [0001]P4 [1000]P3 [0001]71Cohen-Sutherland Line ClippingExample 3: Consider the line P7 to P8 belowStart at P7From the two region codes of the two end-points we know the line crosses the left boundary so calculate the intersection point to generate P7wymaxwyminwxminwxmaxWindowP7 [0000]P7 [0001]P8 [0010]P8 [0000]72Cohen-Sutherland Line ClippingExample 4: Consider the line P7 to P8Start at P8 Calculate the intersection with the right boundary to generate P8P7 to P8 is inside the window so is retainedwymaxwyminwxminwxmaxWindowP7 [0000]P7 [0001]P8 [0010]P8 [0000]73Illustration of Line ClippingBefore Clipping

Consider the line segment AD. Point A has an outcode of 0000 and point D has an outcode of 1001. The logical AND of these outcodes is zero; therefore, the line cannot be trivially rejected. Also, the logical OR of the outcodes is not zero;therefore, the line cannot be trivally accepted.

The algorithm then chooses D as the outside point (its outcode contains 1's). By our testing order, we first use the top edge to clip AD at B. The algorithm then recomputes B's outcode as 0000. With the next iteration of the algorithm, AB is tested and is trivially accepted and displayed.74Consider the line segment EI Point E has an outcode of 0100, while point I's outcode is 1010.

The results of the trivial tests show that the line can neither be trivally rejected or accepted.

Point E is determined to be an outside point, so the algorithm clips the line against the bottom edge of the window.

Now line EI has been clipped to be line FI. Line FI is tested and cannot be trivially accepted or rejected.

Point F has an outcode of 0000, so the algorithm chooses point I as an outside point since its outcode is1010.

The line FI is clipped against the window's top edge, yielding a new line FH. Line FH cannot be trivally accepted or rejected.

Since H's outcode is 0010, the next iteration of the algorthm clips against the window's right edge, yielding line FG. The next iteration of the algorithm tests FG, and it is trivially accepted and display.

After ClippingAfter clipping the segments AD and EI, the result is that only the line segment AB and FG can be seen in the window.

Questions Let R be the rectangular window whose lower left-hand corner is at L(-3,1) and upper right-hand corner is at R(2,6). Clip the line segments AB and CD where A(-4,2); B(-1,7); C(-1,5) and D(3,8), using cohen-sutherland line clipping algorithm. Clip line AB having coordinates A (7, 2),B (10, 1) against window A(5,1) , B(8,1) , C(8,4) , D(5,4) using Cohen Sutherland line clipping algorithm.Use the Cohen-Sutherland algorithm to clip the line whose end points are (-4, 2) and (-1, 7) against the rectangular window whose lower left-hand corner is at (-3, 1) and upper right-hand corner is at (2, 6).

2D ClippingIntroductionPoint ClippingLine ClippingPolygon / Area ClippingText ClippingThe polygon to be clipped is called the SUBJECT polygon and the polygon defining the clipping area is called the CLIP polygon.Polygon ClippingArea Clipping (polygons) To clip a polygon, we cannot directly apply a line-clipping method to the individual polygon edges because this approach would produce a series of unconnected line segments as shown in figure . Note the difference between clipping lines and polygons:80NOTE! Some clipping algorithms leave the pieces connected by a line, others do not. The major difference is that you want to know what is inside and what is outside of the polygon.Polygon ClippingSome difficulties:Maintaining correct inside/outsideVariable number of verticesHandle screen cornerscorrectly81Sutherland-Hodgman Area ClippingA technique for clipping areas developed by Sutherland & HodgmanPut simply the polygon is clipped by comparing it against each boundary in turnOriginal AreaClip LeftClip RightClip TopClip BottomSutherland turns up again. This time with Gary Hodgman with whom he worked at the first ever graphics company Evans & Sutherland

At each step, a new sequence of output vertices is generated and passed to the next window boundary clipper82

1. Basic Concept:Simplify via separationClip whole polygon against one edgeRepeat with output for other 3 edgesSimilar for 3DYou can create intermediate vertices that get thrown outSutherland-Hodgeman Polygon Clipping84Sutherland-Hodgeman Polygon ClippingExampleStartLeftRightBottomTopNote that the point one of the points added when clipping on the right gets removed when we clip with bottom852. Algorithm:Let (P1, P2,. PN) be the vertex list of the Polygon to be clipped and E be the edge of +vely oriented, convex clipping window.We clip each edge of the polygon in turn against each window edge E, forming a new polygon whose vertices are determined as follows:Sutherland-Hodgeman Polygon Clipping86

Four Cases of polygon clipping against one edge

The clip boundary determines a visible and invisible region. The edges from vertex i to vertex i+1 can be one of four types:Case 1 : Wholly inside visible region - save endpoint Case 2 : Exit visible region - save the intersection Case 3 : Wholly outside visible region - save nothing Case 4 : Enter visible region - save intersection and endpoint Because clipping against one edge is independent of all others, it is possible to arrange the clipping stages in a pipeline. The input polygon is clipped against one edge and any points that are kept are passed on as input to the next stage of the pipeline. This way four polygons can be at different stages of the clipping process simultaneously.Polygons can be clipped against each edge of the window one at a time. Windows/edge intersections, if any, are easy to find since the X or Y coordinates are already known. Vertices which are kept after clipping against one window edge are saved for clipping against the remaining edges. Note that number of vertices usually changes and will often increases.

Four casesInside: If both Pi-1 (1)and Pi (2)are to the left of window edge vertex then Pi (2)is placed on the output vertex list.Entering: If Pi-1(1) is to the right of window edge and Pi (2)is to the left of window edge vertex then intersection (I) of Pi-1 Pi (1,2) with edge E and Pi (2)are placed on the output vertex list.Leaving: If Pi-1 (1)is to the left of window edge and Pi (2)is to the right of window edge vertex then only intersection (I) of Pi-1 Pi (1,2)with edge E is placed on the output vertex list.Outside: If both Pi-1 (1)and Pi (2)are to the right of window edge nothing is placed on the output vertex list.Sutherland-Hodgeman Polygon Clipping8990Sutherland-Hodgman Polygon Clipping If the first vertex is outside the window boundary and the second vertex is insideThen , both the intersection point of the polygon edge with the window boundary and the second vertex are added to the output vertex list.

91Sutherland-Hodgman Polygon Clipping If both input vertices are inside the window boundary.Then, only the second vertex is added to the output vertex list.

92Sutherland-Hodgman Polygon Clipping If the first vertex is inside the window boundary and the second vertex is outside.Then, only the edge intersection with the window boundary is added to the output vertex list.

93Sutherland-Hodgman Polygon Clipping If both input vertices are outside the window boundary.Then, nothing is added to the output vertex list.

94Sutherland-Hodgman Polygon Clipping (Example) We illustrate this algorithm by processing the area in figure against the left window boundary.

Vertices 1 and 2 are outside of the boundary. Vertex 3, which is inside, 1' and vertex 3 are saved. Vertex 4 and 5 are inside, and they also saved. Vertex 6 is outside, 5' is saved. Using the five saved points, we would repeat the process for the next window boundary.95Sutherland-Hodgman Polygon ClippingThe Sutherland-Hodgman algorithm correctly clips convex polygons, but concave polygons may be displayed with extraneous lines as demonstrated in figure. Since there is only one output vertex list, the last vertex in the list is always joined to the first vertex.

Sutherland-Hodgeman Polygon ClippingCreating New Vertex Listout outsave nothingOutside(0 output)Pi-1Piin insave ending vert Inside(1 output)Pi-1Piout insave new clip vertand ending vertEntering(2 outputs)PiPi-1Piin outsave new clip vertLeaving(1 output)Pi-196Sutherland-Hodgman Polygon ClippingEach example shows the point being processed (P) and the previous point (S)

Saved points define area clipped to the boundary in questionSPSave Point PSPSave Point IIPSNo Points SavedSPSave Points I & PI97

STARTINPUT VERTEX LIST(P1, P2........, PN)IF PiPi+1 INTERSECT E ?FOR i =1 TO (N-1) DOCOMPUTE IOUTPUT I IN VERTEX LISTIF Pi TO LEFT OF E ?YESNOYESOUTPUT Pi IN VERTEX LISTNOi = i+1Flow ChartSpecial case for first Vertex99Flow ChartSpecial case for first VertexIF PNP0 INTERSECT E ?COMPUTE IOUTPUT I IN VERTEX LISTYESNOENDYOU CAN ALSO APPEND AN ADDITIONAL VERTEX PN+1 = P1 AND AVOID SPECIAL CASE FOR FIRST VERTEX100Sutherland-Hodgeman Polygon ClippingInside/Outside Test: Let P(x,y) be the polygon vertex which is to be tested against edge E defined form A(x1, y1) to B(x2, y2). Point P is to be said to the left (inside) of E or AB iff

or C = (x2 x1) (y y1) (y2 y1)(x x1) > 0 otherwise it is said to be the right/Outside of edge E

101Weiler-Atherton AlgorithmWhen we have non-convex polygons then the algorithm above might produce polygons with coincident edgesThis is sometimes OK for rendering but is not for other applications (e.g. shadows)The Weiler-Atherton algorithm produces separate polygons for each visible fragmentWeiler-Atherton Polygon ClippingProblem with Sutherland-Hodgeman:Concavities can end up linked

Weiler-Atherton creates separate polygons in such casesA different clipping algorithm, the Weiler-Atherton algorithm, creates separate polygons

103Weiler-Atherton Polygon ClippingWhen using Sutherland-Hodgeman, concavities can end up linked

A different clipping algorithm, the Weiler-Atherton algorithm, creates separate polygonsRemember this?Weiler-Atherton Polygon ClippingExampleadd clip pt.and end pt.add end pt.add clip pt.cache old dir.follow clip edge untilnew crossing foundb) reach pt. already added105Weiler-Atherton Polygon ClippingExample (cont)continue fromcached locationadd clip pt.and end pt.add clip pt.cache dir.follow clip edge untila) new crossing foundb) reach pt. already added106Weiler-Atherton Polygon ClippingExample (concluded)continue fromcached locationnothing addedfinishedFinal result:Two unconnectedpolygons1072D ClippingIntroductionPoint ClippingLine ClippingPolygon/Area ClippingText ClippingSutherland-Hodgman Area ClippingA technique for clipping areas developed by Sutherland & HodgmanPut simply the polygon is clipped by comparing it against each boundary in turnOriginal AreaClip LeftClip RightClip TopClip BottomSutherland turns up again. This time with Gary Hodgman with whom he worked at the first ever graphics company Evans & Sutherland

At each step, a new sequence of output vertices is generated and passed to the next window boundary clipper109

1. Basic Concept:Simplify via separationClip whole polygon against one edgeRepeat with output for other 3 edgesSimilar for 3DYou can create intermediate vertices that get thrown outSutherland-Hodgeman Polygon Clipping111Sutherland-Hodgeman Polygon ClippingExampleStartLeftRightBottomTopNote that the point one of the points added when clipping on the right gets removed when we clip with bottom1122. Algorithm:Let (P1, P2,. PN) be the vertex list of the Polygon to be clipped and E be the edge of +vely oriented, convex clipping window.We clip each edge of the polygon in turn against each window edge E, forming a new polygon whose vertices are determined as follows:Sutherland-Hodgeman Polygon Clipping113

115Sutherland-Hodgman Polygon Clipping There are four possible cases when processing vertices in sequence around the perimeter of a polygon. As each pair of adjacent polygon vertices is passed to a next window boundary clipper, we make the following tests:116Sutherland-Hodgman Polygon Clipping If the first vertex is outside the window boundary and the second vertex is insideThen , both the intersection point of the polygon edge with the window boundary and the second vertex are added to the output vertex list.

117Sutherland-Hodgman Polygon Clipping If both input vertices are inside the window boundary.Then, only the second vertex is added to the output vertex list.

118Sutherland-Hodgman Polygon Clipping If the first vertex is inside the window boundary and the second vertex is outside.Then, only the edge intersection with the window boundary is added to the output vertex list.

119Sutherland-Hodgman Polygon Clipping If both input vertices are outside the window boundary.Then, nothing is added to the output vertex list.

120Sutherland-Hodgman Polygon Clipping (Example) We illustrate this algorithm by processing the area in figure against the left window boundary.

Vertices 1 and 2 are outside of the boundary. Vertex 3, which is inside, 1' and vertex 3 are saved. Vertex 4 and 5 are inside, and they also saved. Vertex 6 is outside, 5' is saved. Using the five saved points, we would repeat the process for the next window boundary.120121Sutherland-Hodgman Polygon ClippingThe Sutherland-Hodgman algorithm correctly clips convex polygons, but concave polygons may be displayed with extraneous lines as demonstrated in figure. Since there is only one output vertex list, the last vertex in the list is always joined to the first vertex.

Four casesInside: If both Pi-1 and Pi are to the left of window edge vertex then Pi is placed on the output vertex list.Entering: If Pi-1 is to the right of window edge and Pi is to the left of window edge vertex then intersection (I) of Pi-1 Pi with edge E and Pi are placed on the output vertex list.Leaving: If Pi-1 is to the left of window edge and Pi is to the right of window edge vertex then only intersection (I) of Pi-1 Pi with edge E is placed on the output vertex list.Outside: If both Pi-1 and Pi are to the right of window edge nothing is placed on the output vertex list.Sutherland-Hodgeman Polygon Clipping122Four Cases of polygon clipping against one edge

The clip boundary determines a visible and invisible region. The edges from vertex i to vertex i+1 can be one of four types:Case 1 : Wholly inside visible region - save endpoint Case 2 : Exit visible region - save the intersection Case 3 : Wholly outside visible region - save nothing Case 4 : Enter visible region - save intersection and endpoint

Because clipping against one edge is independent of all others, it is possible to arrange the clipping stages in a pipeline. The input polygon is clipped against one edge and any points that are kept are passed on as input to the next stage of the pipeline. This way four polygons can be at different stages of the clipping process simultaneously.

Polygons can be clipped against each edge of the window one at a time. Windows/edge intersections, if any, are easy to find since the X or Y coordinates are already known. Vertices which are kept after clipping against one window edge are saved for clipping against the remaining edges. Note that the number of vertices usually changes and will often increases.To clip an area against an individual boundary:Consider each vertex in turn against the boundaryVertices inside the boundary are saved for clipping against the next boundaryVertices outside the boundary are clippedIf we proceed from a point inside the boundary to one outside, the intersection of the line with the boundary is savedIf we cross from the outside to the inside intersection point and the vertex are saved125Sutherland-Hodgeman Polygon ClippingCreating New Vertex Listout outsave nothingOutside(0 output)Pi-1Piin insave ending vert Inside(1 output)Pi-1Piout insave new clip vertand ending vertEntering(2 outputs)PiPi-1Piin outsave new clip vertLeaving(1 output)Pi-1126Sutherland-Hodgman Polygon ClippingEach example shows the point being processed (P) and the previous point (S)

Saved points define area clipped to the boundary in questionSPSave Point PSPSave Point IIPSNo Points SavedSPSave Points I & PI127

STARTINPUT VERTEX LIST(P1, P2........, PN)IF PiPi+1 INTERSECT E ?FOR i =1 TO (N-1) DOCOMPUTE IOUTPUT I IN VERTEX LISTIF Pi TO LEFT OF E ?YESNOYESOUTPUT Pi IN VERTEX LISTNOi = i+1Flow ChartSpecial case for first Vertex129Flow ChartSpecial case for first VertexIF PNP0 INTERSECT E ?COMPUTE IOUTPUT I IN VERTEX LISTYESNOENDYOU CAN ALSO APPEND AN ADDITIONAL VERTEX PN+1 = P1 AND AVOID SPECIAL CASE FOR FIRST VERTEX130Sutherland-Hodgeman Polygon ClippingInside/Outside Test: Let P(x,y) be the polygon vertex which is to be tested against edge E defined form A(x1, y1) to B(x2, y2). Point P is to be said to the left (inside) of E or AB iff

or C = (x2 x1) (y y1) (y2 y1)(x x1) > 0 otherwise it is said to be the right/Outside of edge E

131Weiler-Atherton Polygon ClippingProblem with Sutherland-Hodgeman:Concavities can end up linked

Weiler-Atherton creates separate polygons in such casesRemember me?132Weiler-Atherton Polygon ClippingExampleadd clip pt.and end pt.add end pt.add clip pt.cache old dir.follow clip edge untilnew crossing foundb) reach pt. already added133Weiler-Atherton Polygon ClippingExample (cont)continue fromcached locationadd clip pt.and end pt.add clip pt.cache dir.follow clip edge untila) new crossing foundb) reach pt. already added134Weiler-Atherton Polygon ClippingExample (concluded)continue fromcached locationnothing addedfinishedFinal result:Two unconnectedpolygons135136Weiler-Atherton Polygon Clipping This algorithm was developed for identifying visible surfaces, and can be used to clip a fill area that is either a convex polygon or a concave polygon. The basic idea of this algorithm is that instead of proceeding around the polygon edges as vertices are processed, we will follow the window boundaries. The path we follow depends on: polygon-processing direction (clockwise or counterclockwise) The pair of polygon verticesoutside-to-inside or an inside-to-outside. 137Weiler-Atherton Polygon Clipping For clockwise processing of polygon vertices, we use the following rules: For an outside-to-inside pair of vertices, follow polygon boundaries. For an inside-to-outside pair of vertices, follow window boundaries in a clockwise direction.

Note: only in out case differs as we follow window boundaries in a clockwise direction instead of polygon boundary138Weiler-Atherton Polygon Clipping (Example)

139Text 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. 140Text Clipping All-or-none string-clipping (treat as word) If all of the string is inside a clip window, we keep it. Otherwise the string is discarded.

141Text Clipping All-or-none character-clipping(treat as character)Here we discard only those characters that are not completely inside the window

142Text Clipping Clip the components of individual characters(real case) 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

Text ClippingText clipping relies on the concept of bounding rectangle

TYPESAll or None String Clipping: The bounding rectangle is on the word.All or None Character Clipping: The bounding rectangle is on the character.

Character Clipping

STRINGS T R I N G