Line Polygon Clipping

Embed Size (px)

Citation preview

  • 8/13/2019 Line Polygon Clipping

    1/28

  • 8/13/2019 Line Polygon Clipping

    2/28

    Topics

    Viewing Transformation Pipeline in 2D

    Line and polygon clipping

    Brute force analytic solution Cohen-Sutherland Line Clipping Algorithm

    Cyrus-Beck Line Clipping Algorithm Sutherland-Hodgman Polygon Clipping

    Sampling Theorem (Nyquist Frequency)

  • 8/13/2019 Line Polygon Clipping

    3/28

    Viewing Transformation in 2D

    x world

    y world

    xview

    yview

    World and Viewing

    Coordinates

    Normalized DeviceCoordinates

    1

    1

    x view

    y view

    Clipping

    Device Coordinates

  • 8/13/2019 Line Polygon Clipping

    4/28

    Viewing Transformation in 2D

    A scene in World-Coordinates

    World-Coordinates to ViewingCoordinates

    Viewing Coordinates toNormalized Device Coordinates

    Normalized Device Coordinates

    to Device Coordinates

    WC

    VC

    NDCDC

    Clipping

    Objects are given in world coordinates The world is viewed through a window

    The window is mapped onto a device viewport

  • 8/13/2019 Line Polygon Clipping

    5/28

    Line and Polygon Clipping

    The problem:

    Given a set of 2D lines or polygons and a

    window, clip the lines or polygons to theirregions that are inside the window

  • 8/13/2019 Line Polygon Clipping

    6/28

    Motivations

    Efficiency

    Display in portion of a screen

    Occlusions

    clip rectangle

  • 8/13/2019 Line Polygon Clipping

    7/28

    Line Clipping

    We will deal only with lines (segments)

    Our window can be described by twoextreme points:

    (xmin,ymin) and (xmax,ymax)

    A point (x,y) is in the window iff:

    xmin x xmax and ymin y ymax

  • 8/13/2019 Line Polygon Clipping

    8/28

    Brute Force Analytic Solution

    The intersection of convex regions is always convex

    Since both Wand S are convex, their intersection is

    convex, i.e a single connected segment of S

    Question: Can the boundary of two convex shapes

    intersect more than twice?

    0, 1, or 2 intersections between a line and a window

    W W W

    S S S

  • 8/13/2019 Line Polygon Clipping

    9/28

    Pseudo Code for Midpoint Line Drawing

    Assume x1>x0 and 0 < slope 1

    Line(x0,y0,x1,y1)begin

    int dx, dy, x, y, d, E, E ;x:= x0; y=y0;

    dx := x1-x0; dy := y1-y0;

    d := 2*dy-dx;E := 2*dy; := 2*(dy-dx);

    PlotPixel(x,y);

    while(x < x1) do

    if (d < 0) thend:=d+ E;x:=x+1;

    end;

    else

    d:=d+ E;x:=x+1;

    y:=y+1;

    end;

    PlotPixel(x,y);

    end;end;

  • 8/13/2019 Line Polygon Clipping

    10/28

    Line Clipping

    Q E

    NEM

    y = ymin

    x = xmin

    (xmin,Round(mxmin+B))

    (xmin, mxmin+B)

    Midpoint Algorithm: Intersection with a vertical edge

  • 8/13/2019 Line Polygon Clipping

    11/28

    Line Clipping

    Midpoint Algorithm: Intersection with a horizontal edge

    AB y = ymin

    x = xmin

    y = ymin-1/2

  • 8/13/2019 Line Polygon Clipping

    12/28

    Cohen-Sutherland for Line Clipping

    Clipping is performed by computing intersections with four

    boundary segments of the window:

    Li, i=1,2,3,4

    Purpose: Fast treatment of lines that are trivially

    inside/outside the window

    Let P=(x,y) be a point to be classified against window W

    Idea: Assign Pa binary code consisting of a bit for eachedge of W. The bit is 1 if the pixel is in the half-plane that

    does not contain W

  • 8/13/2019 Line Polygon Clipping

    13/28

    Cohen-Sutherland for Line Clipping

    bit 1 0

    123

    y < yminy > ymax

    x > xmax

    y yminy ymax

    x xmax4 x < xmin x xmin

    0101

    0001

    01100100

    1001

    0010

    1010

    0000

    1000ymin

    ymax

    xmin xmax

    1

    234

  • 8/13/2019 Line Polygon Clipping

    14/28

    Cohen-Sutherland for Line Clipping

    Given a line segment S fromp0=(x0,y0) top1=(x1,y1)to be clipped against a window W

    If code(p0) AND code(p1) is not zero, then S istrivially rejected

    If code(p0) OR code(p1) is zero, then S is triviallyaccepted

    0101

    0001

    01100100

    1001

    0010

    1010

    0000

    1000

  • 8/13/2019 Line Polygon Clipping

    15/28

    Cohen-Sutherland for Line Clipping

    Otherwise: let assume w.l.o.g. thatp0 is outside W

    Find the intersection of S with the edge

    corresponding to the MSB in code(p0) that is equal to1. Call the intersection pointp2.

    Run the procedure for the new segment (p1 ,p2).

    A

    BC

    D

    EF

    GH

    I

    1

    2

    34

  • 8/13/2019 Line Polygon Clipping

    16/28

    Cyrus-Beck Line Clipping

    V0

    V1

    inside

    N

    U

    Inside/Outside Test:

    Assume WLOG that V=(V1-V0) is the border vector where

    "inside" is to its right

    If V=(Vx

    ,Vy

    ), N is the normal to V, pointing outside,

    defined by N=(-Vy,Vx)

    Vector U points "outside" if NU > 0

    Otherwise U points "inside"

  • 8/13/2019 Line Polygon Clipping

    17/28

    Cyrus-Beck Line Clipping

    P0

    inside

    N QP1V(t)

    L

    The parametric line P(t)=P0+(P1-P0)t

    The parametric vector V(t)=P(t)-Q

    The segment P0P1 intersects the line L at t0 satisfying V(t0)N=0

    The intersection point is P(t0)

    =P1-P0 points inside if (P1-P0)N

  • 8/13/2019 Line Polygon Clipping

    18/28

    Cyrus-Beck Line Clipping

    p0p1

    Ni Qi

    Denote p(t)=p0+(p1-p0)t t[0..1]

    Let Qi

    be a point on the edge Li

    with outside

    pointing normal Ni

    V(t) = p(t)-Qi is a parameterized vector from Qi to

    the segment P(t)

    Ni V(t) = 0 iff V(t) Ni

    We are looking for tsatisfying Ni V(t) = 0

  • 8/13/2019 Line Polygon Clipping

    19/28

    Cyrus-Beck Line Clipping

    0 = Ni V(t)

    = Ni (p(t)-Qi)

    = Ni (p0+(p1-p0)t-Qi)= Ni (p0-Qi) + Ni (p1-p0)t

    Solving for t we get:

    where =(p1-p0)

    Comment: If Ni=0, t has no solution (V(t) Ni)

    Ni(p0-Qi)

    -Ni(p1-p0)t =

    Ni(p0-Qi)

    -Ni=

  • 8/13/2019 Line Polygon Clipping

    20/28

    Cyrus-Beck Line Clipping

    p1

    p0

    PL

    PE

    p0

    p1

    PE

    PE

    PL

    PL

    p1

    PL

    PEp0

  • 8/13/2019 Line Polygon Clipping

    21/28

    Cyrus-Beck Line Clipping

    The intersection of p(t) with all four edges Li is

    computed, resulting in up to four ti values

    If ti1 , ti can be discarded

    Based on the sign of Ni, each intersection

    point is classified as PE (potentially entering) orPL (potentially leaving)

    PE

    with the largest tandPL

    with the smallest tprovide the domain of p(t) inside W

    The domain, if inverted, signals that p(t) is

    totally outside

  • 8/13/2019 Line Polygon Clipping

    22/28

    Sutherland-Hodgman

    Polygon-Clipping Algorithm

  • 8/13/2019 Line Polygon Clipping

    23/28

    Sutherland-Hodgman

    Polygon-Clipping Algorithm

    right clip boundary bottom clip boundary

    left clip boundary top clip boundary

    Idea: Clip a polygon by successively clipping against each

    (infinite) clip edge

    After each clipping a new set of vertices is produced.

  • 8/13/2019 Line Polygon Clipping

    24/28

    Sutherland-Hodgman

    Polygon-Clipping Algorithm

    clipboundary

    inside outsides

    clipboundary

    inside outside

    s

    p

    clipboundary

    inside outsidep

    si

    sclipboundary

    inside outside

    pi

    p added tooutput list

    i added tooutput list

    no output i and p added to

    output list

    p

    For each clip edge - scan the polygon and consider the

    relation between successive vertices of the polygon

    Each iteration adds 0, 1 or 2 new vertices

    Assume vertex s has been dealt with, vertex p follows:

  • 8/13/2019 Line Polygon Clipping

    25/28

    Sutherland-Hodgman

    Polygon-Clipping Algorithm

    Window

    V3

    V'2

    V2

    V1

    V3

    V2

    V1

    LeftClipping

    RightClipping

    BottomClipping

    TopClipping

    V1V

    2V3

    V1V

    2V2V3

    V1V

    2V2V3

    V1V

    2V2V2

  • 8/13/2019 Line Polygon Clipping

    26/28

    Sampling Theorem

    Question: How dense should be the pixel grid in

    order to draw properly a drawn object?

    Given a sampling at intervals equal to d then one

    may recover frequencies of wavelength > 2d

    Aliasing: If the sampling interval is more than 1/2

    the wavelength, erroneous frequencies may be

    produced

  • 8/13/2019 Line Polygon Clipping

    27/28

  • 8/13/2019 Line Polygon Clipping

    28/28

    Sampling Theorem

    2D Example: Moire Effect