a good pdf on clipping

Upload: nishantrun2

Post on 02-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 A good pdf on Clipping

    1/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    ClippingConcepts, Algorithms for

    line clipping

    Clipping - 10/16/12

  • 8/11/2019 A good pdf on Clipping

    2/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    Clipping endpoints

    If

  • 8/11/2019 A good pdf on Clipping

    3/16

  • 8/11/2019 A good pdf on Clipping

    4/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    Clip Rect Divide plane into 9 regions

    Compute the sign bit of 4 comparisons between a vertex and anedge

    ; ; ;

    point lies inside only if all four sign bits are 0, otherwise exceeds edge

    4 bit outcode records results of four bounds tests: First bit: above top edge

    Second bit: below bottom edge

    Third bit: to the right of right edge

    Fourth bit: to the left of left edge Compute outcodes for both vertices of each line (denoted OC0

    and OC1)

    Lines with OC0 = 0 (i.e., 0000) andOC1 = 0 can be triviallyaccepted.

    Lines lying entirely in a half plane outside an edge can be triviallyrejectedif (OC0ANDOC1) 0 (i.e., they share an outside bit)

    Cohen-Sutherland Line Clipping in 2D

    Clipping - 10/16/12

  • 8/11/2019 A good pdf on Clipping

    5/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    Very similar to 2D

    Divide volume into 27 regions (Picture aRubiks cube)

    6-bit outcode records results of 6 boundstests First bit: behind back plane

    Second bit: in front of front plane

    Third bit: above top plane

    Fourth bit: below bottom plane

    Fifth bit: to the right of right plane

    Sixth bit: to the left of left plane

    Again, lines with OC0= 0 and OC1= 0 canbe trivially accepted

    Lines lying entirely in a volume outside of

    a plane can be trivially rejected: OC0AND

    OC10 (i.e., they share an outside bit)

    Cohen-Sutherland Line Clipping in 3D

    Clipping - 10/16/12

    Back plane

    000000 (in front)

    100000 (behind)

    0

    0

    0

    0

    00

    Top plane

    001000 (above)

    000000 (below)

    Right plane

    000000 (to left of)

    000010 (to right of)

  • 8/11/2019 A good pdf on Clipping

    6/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    If we can neither trivially accept/reject (T/A, T/R),divide and conquer

    Subdivide line into two segments; then T/A or T/R oneor both segments:

    use a clip edge to cut line

    use outcodes to choose the edges that are crossed

    for a given clip edge, if a lines two outcodes differ in thecorresponding bit, the line has one vertex on each side of theedge, thus crosses

    pick an order for checking edges: top bottom right left

    compute the intersection point

    the clip edge fixes eitherxory

    can substitute into the line equation

    iterate for the newly shortened line, extra clips mayhappen (e.g., E-I at H)

    Cohen-Sutherland Algorithm (1/3)

    D

    C B

    A

    E

    Clipping - 10/16/12

  • 8/11/2019 A good pdf on Clipping

    7/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    =0 + ( 0)and =0 + 1

    ( 0)

    Algorithm:

    Cohen-Sutherland Algorithm (2/3)

    ComputeOutCode(x0, y0, outcode0);

    ComputeOutCode(x1, y1, outcode1);

    repeat

    check for trivial reject or trivial accept

    pick the point that is outside the clip rectangle

    ifTOP then

    x = x0 + (x1 x0) * (ymax y0) / (y1 y0);

    y = ymax;

    else ifBOTTOMthen

    x = x0 + (x1 x0) * (ymin y0) / (y1 y0);

    y = ymin;

    else ifRIGHTthen

    y = y0 + (y1 y0) * (xmax x0)

    x = xmax;

    else ifLEFTthen

    y = y0 + (y1 y0) * (xmin x0)

    x = xmin;

    if (x0, y0 is the outer point) then

    x0 = x; y0 = y; ComputeOutCod

    else

    x1 = x; y1 = y; ComputeOutCod

    until done

    Clipping - 10/16/12

  • 8/11/2019 A good pdf on Clipping

    8/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    Similar algorithm for using 3D outcodes to clip against canonical parallel vi

    Cohen-Sutherland Algorithm (3/3)

    xmin = ymin = -1; xmax = ymax = 1;

    zmin = -1; zmax = 0;

    ComputeOutCode(x0, y0, z0, outcode0);

    ComputeOutCode(x1, y1, z1, outcode1);

    repeat

    check for trivial reject or trivial accept

    pick the point that is outside the clip rectangle

    ifTOP then

    x = x0 + (x1 x0) * (ymax y0) / (y1 y0);

    z = z0 + (z1 z0) * (ymax y0) / (y1 y0);

    y = ymax;else ifBOTTOMthen

    x = x0 + (x1 x0) * (ymin y0) / (y1 y0);

    z = z0 + (z1 z0) * (ymin y0) / (y1 y0);

    y = ymin;

    else ifRIGHTthen

    y = y0 + (y1 y0) * (xmax x0) / (x1 x0);

    z = z0 + (z1 z0) * (xmax x0) / (x1 x0);

    x = xmax;

    else ifLEFTthen

    y = y0 + (y1 y0) * (xmin x0) / (x1

    z = z0 + (z1 z0) * (xmin x0) / (x1 x = xmin;

    else if NEAR then

    x = x0 + (x1 x0) * (zmax z0) / (z1

    y = y0 + (y1 y0) * (zmax z0) / (z1

    z = zmax;

    else if FAR then

    x = x0 + (x1 x0) * (zmin z0) / (z1

    y = y0 + (y1 y0) * (zmin z0) / (z1

    z = zmin;

    if (x0, y0, z0 is the outer point) then

    x0 = x; y0 = y; z0 = z;

    ComputeOutCode(x0, y0, z0, outcode

    else

    x1 = x; y1 = y; z1 = z;

    ComputeOutCode(x1, y1, z1, outcode

    until done

    Clipping - 10/16/12

  • 8/11/2019 A good pdf on Clipping

    9/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    Dont round and then scan

    convert, because the line will

    have the wrong slope: calculate

    decision variable based on pixel

    chosen on left edge (remember:

    = + )

    Horizontal edge problem:

    Clipping/rounding produces pixelA; to get pixel B, round up x of the

    intersection of line with

    = and pick pixel

    above:

    Scan Conversion after Clipping

    B A

    x = xmin

    y = ymin

    y = ymin 1

    y = ymin 1/2

    (, + )

    (, ( + ))

    =

    Clipping - 10/16/12

  • 8/11/2019 A good pdf on Clipping

    10/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    Sutherland-Hodgman Polygon Clipping

    The 2D Sutherland-Hodgman algorithm generalizes to higher dime

    We can use it to clip polygons to the 3D view volume one plane at a

    Section 36.5 in textbook

  • 8/11/2019 A good pdf on Clipping

    11/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    Use parametric line formulation:

    Determine where line intersects the

    infinite line formed by each clip

    rectangle edge

    solve for t multiple times dependingon the number of clip edges crossed

    decide which of these intersections

    actually occur on the rectangle

    For : use any point on edge Ei

    Cyrus-Beck/Liang-Barsky Parametric Line Clippi

    =0 + 1 0

    Clipping - 10/16/12

  • 8/11/2019 A good pdf on Clipping

    12/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    Now solve for the value of tat the intersection of P0P1with the edge Ei:

    First, substitute for P(t):

    Next, group terms and distribute dot product:

    Let Dbe the vector from P0toP1=(P1P0), andsolve for t:

    note that this gives a valid value of tonly if thedenominator of the expression is nonzero.

    For this to be true, it must be the case that: Ni0 (that is, the normal should not be 0; this could

    occur only as a mistake)

    D0 (that is, P1P0)

    Ni D0 (edge Eiand line Dare not parallel; if theyare, no intersection).

    The algorithm checks these conditions.

    Cyrus-Beck/Liang-Barsky Parametric Line Clippi

    [0 + 1

    0 +

    = [

    Clipping - 10/16/12

  • 8/11/2019 A good pdf on Clipping

    13/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    Eliminate s outside [0,1] on the line

    Which remaining s produce interior

    intersections? Cant just take the innermost values!

    Move from P0to P1; for a given edge,just before crossing:

    if Ni D < 0 Potentially Entering (PE);

    if Ni D > 0

    Potentially Leaving (PL) Pick inner PE/PL pair: for with

    max , for with min , and > 0, < 1

    If

  • 8/11/2019 A good pdf on Clipping

    14/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    Cyrus-Beck/Liang-Barsky Line Clipping AlgorithmPre-calculate Niand select PEi

    for each edge;

    foreach line segment to be clipped

    ifP1= P0 then line is degenerate so clip as a point;

    elsebegin

    tE= 0; tL= 1;

    foreach candidate intersection with a clip edge

    ifNi D 0 then{Ignore edges parallel to line}

    begin

    calculate t; {of line and clip edge intersection}

    use sign of Ni

    D to categorize as PE or PL;if PE thentE= max(tE,t);

    ifPL thentL= min(tL,t);

    end

    iftE> tLthen return nil

    else return P(tE) and P(tL) as true clip intersections

    end

    Clipping - 10/16/12

  • 8/11/2019 A good pdf on Clipping

    15/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    D = P1 P0= (x1 x0, y1 y0)

    Leave PEi

    as an arbitrary point on clip edge: its a free variable and d

    Parametric Line Clipping for Upright Clip Rectan

    Calculations for Parametric Line Clipping Algorithm

    (x0-x,y0- ymax)(x, ymax)(0,1)top: y = ymax

    (x0-x,y0- ymin)(x, ymin)(0,-1)bottom: y = ymin

    (x0- xmax,y0-y)(xmax,y)(1,0)right: x = xmax

    (x0- xmin,y0-y)(xmin, y)(-1,0)left: x = xmin

    P0-PEiPEi

    Normal NiClip EdgeiD

    iN

    PPi

    N

    t

    =0

    (

    )01

    (

    min0(

    xx

    xx

    )01(

    ma0(

    xx

    xx

    )01

    (

    mi0(

    yy

    yy

    )01

    (

    ma0(

    yy

    yy

    Clipping - 10/16/12

  • 8/11/2019 A good pdf on Clipping

    16/16

    CS123 | INTRODUCTION TO COMPUTER GRAPHICS

    Andries van Dam

    Examine t:

    Numerator is just the directed distance to an edge; sign co

    to OC

    Denominator is just the horizontal or vertical projection o

    dxor dy; sign determines PE or PL for a given edge

    Ratio is constant of proportionality: how far over from P

    intersection is relative to dxor dy

    Parametric Line Clipping for Upright Clip Rectan

    Clipping - 10/16/12