CLIPPING ALGORITHMS
Nov 25, 2015
CLIPPING ALGORITHMS
Window vs. ViewportWindowWorld-coordinate area selected for displayWhat is to be viewedViewportArea on the display device to which a window is mappedWhere it is to be displayed
Viewing in 2D - ViewportWindow in world coordinates.45250
Viewport inDevice coords250 x 250Pixels.250 x 250Pixels.
2-D Viewing PipelineThe mapping of a 2D world-coordinate scene description to device coordinates is called a 2D viewing transformation or window-to-viewport transformation
Clipping WindowThe clipping window selects what we want to see; the viewport indicates where it is to be viewed on the output device.
Window to Viewport TransformationTo transform the world-coordinate point into the same relative position within the viewport, we require that
ClippingAny procedure that identifies those portions of a picture that are either inside or outside of a specified region of space is referred to as clipping.The region against which object is to clipped is called clip window.It include extracting part of defined scene for ViewingIdentifying visible surfaces in 3D viewsDisplaying a multi window environmentDrawing and painting operations for copying, moving, erasing or duplicating
2-D ClippingPoint clippingLine clippingCohen-SutherlandNicholl Lee - NichollLiang-BarskyFill-area clippingSutherland-HodgemanWeiler-AthertonCurve clippingText clipping
Point Clipping Is Point(x,y) Inside the Clip Window?
(x, y)wx2wx1wy1wy2Inside = (x>=wx1) && (x=wy1) && (y
Point ClippingAssume that clip window is rectangle.Save a point P(x,y) for display ifxwmin x xwmaxywmin y ywmaxEdges of clip window (xwmin, xwmax, ywmin, ywmax) can be either world coordinate window boundaries or viewport boundaries.The point is clipped if not satisfy the above condition.
Line ClippingFind the Part of a Line Inside the Clip Window
P7P8P10P9P1P2P5P4P3P6Before Clipping
Line ClippingFind the Part of a Line Inside the Clip Window
After ClippingP4P3P6P8P7P5
0000WindowCohen-Sutherland Line Clipping ISplit plane into 9 regionsAssign each a 4 bit value(above,below,right,left)Assign the vertices of line 4 bit value10001010100101000110010100010010*
Trivially acceptedif (both region codes = 0000)
Trivially rejected
if(AND of region codes 0000)
Otherwise, divide line into two segments
Intersection of line with window edge is determined and trivial accept/ reject test is repeated.
The process is repeated until line is accepted.
Given a line segment with endpoint P1 = (x1, y1) and P2 = (x2, y2)Compute the 4 bit code for each endpoint.If both codes are 0000 (bitwise OR of codes yields 0000) line lies completely inside the window: pass endpoints to draw routine.If both codes have a 1 in same bit position (bitwise AND of codes is not 0000) the line lies outside the window. It can be trivially rejected.If a line cannot be trivially accepted or rejected, at least one of two endpoints must lie outside window and line segment crosses a window edge.
This line must be clipped at window edge before being passed to drawing routine.Examine one of the endpoints say P1 = (x1, y1), read P1s 4 bit code in order Left to Right, Bottom to Up.When a set bit (1) is found, compute intersection I of corresponding window edge with line from P1 to P2. Replace P1 with I and repeat the algorithm.
Cohen-Sutherland Line ClippingUse Simple Tests to Classify Easy Cases FirstP7P8P10P9P1P2P5P4P3P6
Cohen-Sutherland Line ClippingClassify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0)P10P5P6P91000P7P81010001001100100000001010001P1P21001P4P3Bit 4Bit 3Bit 2Bit 1
Cohen-Sutherland Line ClippingClassify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0)P10P5P6P91000P7P81010001001100100000001010001P1P21001P4P3Bit 4Bit 3Bit 2Bit 1
Cohen-Sutherland Line ClippingClassify Some Lines Quickly by AND of Bit Codes Representing Regions of Two Endpoints (Must Be 0)P10P5P6P91000P7P810100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified QuicklyP10P5P6P91000P7P810100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified Quickly
P10P5P6P91000P7P810100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified Quickly
P10P6P91000P7P810100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified Quickly
P10P6P91000P7P810100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified QuicklyP10P6P91000P7P810100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified Quickly
P10P6P91000P7P810100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified Quickly
P10P6P91000P810100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5P
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified Quickly
P10P6P91000P810100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5P7
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified QuicklyP10P6P9100010100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5P7P8
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified QuicklyP10P6P9100010100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5P7P8
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified Quickly
P10P6P9100010100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5P7P8
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified Quickly
P10P6P9100010100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5P7P8
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified QuicklyP10P6100010100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5PP8P9
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified Quickly
P6100010100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5P7P8P10P9
Cohen-Sutherland Line ClippingCompute Intersections with Window Boundary for Lines That Cant be Classified QuicklyP6100010100010011001000000010100011001P4P3Bit 4Bit 3Bit 2Bit 1P5P7P8
Line Intersection and ClippingFor left window edge, Intersection point will be (xL, y) y = m (xL x1 ) + y1 For right window edge, Intersection point will be (xR, y) y = m (xR x1 ) + y1 For top window edge, Intersection point will be (x, yT) x = x1 + (yT y1 ) / m For bottom window edge, Intersection point will be (x, yB) x = x1 + (yB y1 ) / m
Parameter p and q are defined as
p1 = - x q1 = x1 xmin p2 = x q2 = xmax x1 p3 = - y q3 = y1 ymin p4 = y q4 = ymax y1
Observations
If pk = 0, line is parallel to corresponding boundaryIf qk < 0, line is completely outside the boundary and can be eliminatedIf qk > 0, the line is inside the boundary and needs further considerationIf pk < 0, line proceeds from outside to inside of corresponding boundary line.If pk > 0, line proceeds from inside to outside of corresponding boundary line.If pk 0, calculate value of u as u = qk / pk
Algorithm
Rules:1) pk = 0: the line is parallel to boundariesIf for that same k, qk < 0, its outsideOtherwise its inside2) pk < 0: the line proceeds from outside to insiderk = qk/pku1 = max(0, rk, u1)3) pk > 0: the line proceeds from inside to outsiderk = qk/pku2 = min(1, rk, u2)4) If u1 > u2, the line is completely outside
AlgorithmIf pk = 0 and qk < 0, eliminate the line and stop, otherwise proceed to next step.For all k such that pk < 0, calculate rk = qk / pk. Let u1 be maximum of set containing 0 and various values of r.For all k such that pk > 0, calculate rk = qk / pk. Let u2 be minimum of set containing 1 and various values of r.If u1 > u2, eliminate line. It is completely outside the window, otherwise use u1 and u2 to calculate the endpoints of clipped line.
Example
ConclusionMore efficient than Cohen Sutherland. Intersection calculations are reduced.Each update of u1 and u2 requires only one division.Window intersections of line are computed only once when final value of u1 and u2 have been computed.
Polygon ClippingFind the Part of a Polygon Inside the Clip Window?Before Clipping
Polygon ClippingFind the Part of a Polygon Inside the Clip Window?After Clipping
Sutherland-Hodgeman Polygon ClippingClip to Each Window Boundary One at a Time
Sutherland-Hodgeman Polygon ClippingClip to Each Window Boundary One at a Time
Sutherland-Hodgeman Polygon ClippingClip to Each Window Boundary One at a Time
Sutherland-Hodgeman Polygon ClippingClip to Each Window Boundary One at a Time
Sutherland-Hodgeman Polygon ClippingClip to Each Window Boundary One at a Time
Clipping to a BoundaryDo Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window BoundaryP1P2P3P4P5InsideOutsideWindow Boundary
Clipping to a BoundaryDo Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window BoundaryP1P2P3P4P5InsideOutsideWindow Boundary
Clipping to a BoundaryDo Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window BoundaryP1P2P3P4P5InsideOutsideWindow Boundary
Clipping to a BoundaryDo Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary
P1P2P3P4P5InsideOutsideWindow Boundary
Clipping to a BoundaryDo Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window BoundaryP1P2P3P4P5InsideOutsideWindow BoundaryP
Clipping to a BoundaryDo Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary
P1P2P3P4P5InsideOutsideWindow BoundaryP
Clipping to a BoundaryDo Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window BoundaryP1P2P3P4P5InsideOutsideWindow BoundaryP
Clipping to a BoundaryDo Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary
P1P2P3P4P5InsideOutsideWindow BoundaryPP
Clipping to a BoundaryDo Inside Test for Each Point in Sequence, Insert New Points When Cross Window Boundary, Remove Points Outside Window Boundary
P1P2InsideOutsideWindow BoundaryPP
Polygon Clipping INote the difference between clipping lines and polygons:NOTE!
Polygon Clipping IISome difficulties:Maintaining correct inside/outsideVariable number of verticesHandle screen cornerscorrectly
Sutherland-Hodgeman Polygon Clipping ISimplify via separationClip whole polygon against one edgeRepeat with output for other 3 edgesSimilar for 3DYou can create intermediate vertices that get thrown out
Sutherland-Hodgeman Polygon Clipping IIExample Iout insave new clip vertand ending vertin insave ending vert
in outsave new clip vert
out outsave nothing
Sutherland-Hodgeman Polygon Clipping IIExample 2StartLeftRightBottomTop
Sutherland-Hodgeman Polygon Clipping Step-1: Initialize the i/p list & o/p list such that i/p list contains set of all consecutive vertices of the polygon & the o/p list is empty.Step-2: Against each window edge L,R,B,T perform the following steps:If V1 and V2 are inside the window then save V2 in o/p list.If V1 and V2 are outside the window then no operation is performed.If V1 is inside and V2 is outside then find the intersection point at window edge and save the intersection point in the o/p list
If V1 is outside & V2 is inside then find the intersection point at the window edge and save the intersection point V2 in the o/p list.Step-3: When the next edge is taken the o/p list becomes the i/p list for that edge.Step-4: the o/p list contains the set of vertices which forms the clipped polygon area.
Weiler-Atherton Polygon Clipping IProblem with Sutherland-Hodgeman:Concavities can end up linked
Weiler-Atherton creates separate polygons in such cases
Weiler-Atherton Polygon Clipping IIExampleadd clip pt.and end pt.add end pt.add clip pt.cache old dir.follow clip edge untilnew crossing foundb) reach pt. already added
Weiler-Atherton Polygon Clipping IIIExample (cont)continue fromcached locationadd clip pt.and end pt.add clip pt.cache dir.follow clip edge untila) new crossing foundb) reach pt. already added
Weiler-Atherton Polygon Clipping IVExample (concluded)continue fromcached locationnothing addedfinishedFinal result:Two unconnectedpolygons
**NOTE! 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.