49
Introduction to Computer Graphics Clipping Clipping

7 Clipping

Embed Size (px)

DESCRIPTION

p

Citation preview

  • Introduction to Computer GraphicsClipping

  • Review: Polygon RasterizationFor scanline, determine all intersections of polygon edges with scanlineSort edge intersections in least to greatest orderUse parity count to determine when pixels are drawnHorizontal lines do not contribute to parity countYmin endpoints do contribute to parity countYmax endpoints do not contribute to parity countBottom edge drawn because A is min of AH. AB does not contributeNot drawn because H is max of AHAnd HG does not contributeNot drawn because D is min of EDAnd increments counter to 2.DC doesnt contribute

  • Next Topic: ClippingWeve been assuming that all primitives (lines, triangles, polygons) lie entirely within the viewport In general, this assumption will not hold:

  • ClippingAnalytically calculating the portions of primitives within the viewport

  • Why Clip?Bad idea to rasterize outside of framebuffer bounds Also, dont waste time scan converting pixels outside window

  • ClippingThe nave approach to clipping lines:for each line segment for each edge of viewport find intersection point pick nearest point if anything is left, draw itWhat do we mean by nearest?How can we optimize this?

  • Trivial Accepts Big optimization: trivial accept/rejectsHow can we quickly determine whether a line segment is entirely inside the viewport?A: test both endpoints.

  • Trivial RejectsHow can we know a line is outside viewport?A: if both endpoints on wrong side of same edge, can trivially reject line

  • Clipping Lines To ViewportCombining trivial accepts/rejectsTrivially accept lines with both endpoints inside all edges of the viewportTrivially reject lines with both endpoints outside the same edge of the viewportOtherwise, reduce to trivial cases by splitting into two segments

  • Cohen-Sutherland Line ClippingDivide viewplane into regions defined by viewport edgesAssign each region a 4-bit outcode: 000000100001100101010100100010100110

  • Cohen-Sutherland Line ClippingAssign an outcode to each vertex of line to testBit 1 = sign bit of (ymax y)If both outcodes = 0, trivial acceptbitwise ORbitwise AND vertex outcodes togetherif result 0, trivial reject

  • Cohen-Sutherland Line ClippingIf line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discardedPick an edge that the line crosses (how?)Intersect line with edge (how?)Discard portion on wrong side of edge and assign outcode to new vertexApply trivial accept/reject tests; repeat if necessary

  • Cohen-Sutherland Line ClippingIf line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discardedPick an edge that the line crossesCheck against edges in same order each timeFor example: top, bottom, right, leftABDEC

  • Cohen-Sutherland Line ClippingIntersect line with edge (how?)

  • Discard portion on wrong side of edge and assign outcode to new vertex

    Apply trivial accept/reject tests and repeat if necessary Cohen-Sutherland Line ClippingABDC

  • Viewport Intersection Code(x1, y1), (x2, y2) intersect with vertical edge at xrightyintersect = y1 + m(xright x1), m=(y2-y1)/(x2-x1)

    (x1, y1), (x2, y2) intersect with horizontal edge at ybottomxintersect = x1 + (ybottom y1)/m, m=(y2-y1)/(x2-x1)

  • Cohen-Sutherland ReviewUse opcodes to quickly eliminate/include linesBest algorithm when trivial accepts/rejects are commonMust compute viewport clipping of remaining linesNon-trivial clipping costRedundant clipping of some linesMore efficient algorithms exist

  • Solving Simultaneous EquationsEquation of a lineSlope-intercept (explicit equation): y = mx + bImplicit Equation: Ax + By + C = 0Parametric Equation: Line defined by two points, P0 and P1P(t) = P0 + (P1 - P0) t, where P is a vector [x, y]T

    x(t) = x0 + (x1 - x0) ty(t) = x0 + (y1 - y0) t

  • Parametric Line EquationDescribes a finite lineWorks with vertical lines (like the viewport edge)0
  • Parametric Lines and ClippingDefine each line in parametric form: P0(t)Pn-1(t)Define each edge of viewport in parametric form:PL(t), PR(t), PT(t), PB(t)Could perform Cohen-Sutherland intersection tests using appropriate viewport edge and line

  • Line / Edge Clipping EquationsFaster line clippers use parametric equationsLine 0:x0 = x00 + (x01 - x00) t0y0 = y00 + (y01 - y00) t0Viewport Edge L:xL = xL0 + (xL1 - xL0) tLyL = yL0 + (yL1 - yL0) tL

    x00 + (x01 - x00) t0 = xL0 + (xL1 - xL0) tLy00 + (y01 - y00) t0 = yL0 + (yL1 - yL0) tLSolve for t0 and/or tL

  • Cyrus-Beck AlgorithmWe wish to optimize line/line intersectionStart with parametric equation of line:P(t) = P0 + (P1 - P0) tAnd a point and normal for each edgePL, NL

  • Cyrus-Beck AlgorithmFind t such thatNL [P(t) - PL] = 0

    Substitute line equation for P(t)Solve for tt = NL [PL P0] / -NL [P1 - P0]P0P1

  • Cyrus-Beck AlgorithmCompute t for line intersection with all four edgesDiscard all (t < 0) and (t > 1)Classify each remaining intersection asPotentially Entering (PE)Potentially Leaving (PL)NL [P1 - P0] > 0 implies PLNL [P1 - P0] < 0 implies PENote that we computed this term when computing t

  • Compute PE with largest tCompute PL with smallest tClip to these two pointsCyrus-Beck Algorithm

  • Cyrus-Beck AlgorithmBecause of horizontal and vertical clip lines:Many computations reduce

    Normals: (-1, 0), (1, 0), (0, -1), (0, 1)Pick constant points on edgessolution for t:-(x0 - xleft) / (x1 - x0)(x0 - xright) / -(x1 - x0)-(y0 - ybottom) / (y1 - y0)(y0 - ytop) / -(y1 - y0)

  • ComparisonCohen-SutherlandRepeated clipping is expensiveBest used when trivial acceptance and rejection is possible for most linesCyrus-BeckComputation of t-intersections is cheapComputation of (x,y) clip points is only done onceAlgorithm doesnt consider trivial accepts/rejectsBest when many lines must be clippedLiang-Barsky: Optimized Cyrus-BeckNicholl et al.: Fastest, but doesnt do 3D

  • Clipping PolygonsClipping polygons is more complex than clipping the individual linesInput: polygonOutput: original polygon, new polygon, or nothingWhen can we trivially accept/reject a polygon as opposed to the line segments that make up the polygon?

  • What happens to a triangle during clipping?Possible outcomes:triangle triangleWhy Is Clipping Hard?triangle quadtriangle 5-gonHow many sides can a clipped triangle have?

  • How many sides?Seven

  • A really tough case: Why Is Clipping Hard?

  • A really tough case: Why Is Clipping Hard?concave polygon multiple polygons

  • Sutherland-Hodgeman ClippingBasic idea:Consider each edge of the viewport individuallyClip the polygon against the viewport edges equation

  • Sutherland-Hodgeman ClippingBasic idea:Consider each edge of the viewport individuallyClip the polygon against the edge equationAfter doing all edges, the polygon is fully clipped

  • Sutherland-Hodgeman ClippingBasic idea:Consider each edge of the viewport individuallyClip the polygon against the edge equationAfter doing all edges, the polygon is fully clipped

  • Sutherland-Hodgeman ClippingBasic idea:Consider each edge of the viewport individuallyClip the polygon against the edge equationAfter doing all edges, the polygon is fully clipped

  • Sutherland-Hodgeman ClippingBasic idea:Consider each edge of the viewport individuallyClip the polygon against the edge equationAfter doing all edges, the polygon is fully clipped

  • Sutherland-Hodgeman ClippingBasic idea:Consider each edge of the viewport individuallyClip the polygon against the edge equationAfter doing all edges, the polygon is fully clipped

  • Sutherland-Hodgeman ClippingBasic idea:Consider each edge of the viewport individuallyClip the polygon against the edge equationAfter doing all edges, the polygon is fully clipped

  • Sutherland-Hodgeman ClippingBasic idea:Consider each edge of the viewport individuallyClip the polygon against the edge equationAfter doing all edges, the polygon is fully clipped

  • Sutherland-Hodgeman ClippingBasic idea:Consider each edge of the viewport individuallyClip the polygon against the edge equationAfter doing all edges, the polygon is fully clipped

  • Sutherland-Hodgeman Clipping:The AlgorithmBasic idea:Consider each edge of the viewport individuallyClip the polygon against the edge equationAfter doing all edges, the polygon is fully clipped

  • Sutherland-Hodgeman ClippingInput/output for algorithm:Input: list of polygon vertices in order Output: list of clipped poygon vertices consisting of old vertices (maybe) and new vertices (maybe)Note: this is exactly what we expect from the clipping operation against each edge

  • Sutherland-Hodgeman ClippingSutherland-Hodgman basic routine:Go around polygon one vertex at a timeCurrent vertex has position p Previous vertex had position s, and it has been added to the output if appropriate

  • Sutherland-Hodgeman ClippingEdge from s to p takes one of four cases:(Orange line can be a line or a plane)

  • Sutherland-Hodgeman ClippingFour cases:s inside plane and p inside planeAdd p to outputNote: s has already been addeds inside plane and p outside planeFind intersection point iAdd i to outputs outside plane and p outside planeAdd nothings outside plane and p inside planeFind intersection point iAdd i to output, followed by p

  • Point-to-Plane testA very general test to determine if a point p is inside a plane P, defined by q and n:(p - q) n < 0: p inside P(p - q) n = 0: p on P(p - q) n > 0: p outside PRemember: p n = |p| |n| cos (q)q = angle between p and n

  • Finding Line-Plane IntersectionsEdge intersects plane P where E(t) is on Pq is a point on Pn is normal to P

    (L(t) - q) n = 0

    t = [(q - L0) n] / [(L1 - L0) n]

    The intersection point i = L(t) for this value of t

  • Line-Plane IntersectionsAgain, lots of opportunity for optimization