lecture 03 - lines

Upload: vishrav-chaudhary

Post on 07-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 Lecture 03 - Lines

    1/52

    CS 430: Computer Graphics

    1

  • 8/4/2019 Lecture 03 - Lines

    2/52

    Assignment 1

    Framework for World View Read XML Render Points in World

    Window Save to an image

    Details:Due April 12th, 2pmMakefile required Submit via email Attach zip/tar.gz

  • 8/4/2019 Lecture 03 - Lines

    3/52

    Assignment 1: XML

  • 8/4/2019 Lecture 03 - Lines

    4/52

    XML

    Nodes define the XML docTags are nodes Even white space is a node!Nodes have children

  • 8/4/2019 Lecture 03 - Lines

    5/52

    Parsing XML in JavaDocument doc = db.parse(file);

    NodeList nodeLst =doc.getElementsByTagName("employee");

    Tom

    Cruise

    George

    Shanks

    Gets all of the nodes with a specific name!

  • 8/4/2019 Lecture 03 - Lines

    6/52

    Parsing XML in JavaNodeList nodeLst =

    doc.getElementsByTagName("employee");

    Node n = nodeLst.item(0);

    if (n.getNodeType() ==Node.ELEMENT_NODE) {

    Element el = (Element) n;

    System.out.println(

    el.getAttribute("type") );

    Tom

    Cruise

    George

    Shanks

    Nodes can be anything, an element is a node corresponding to a TAG

    Elements may have attributes (flags inside the tag)

  • 8/4/2019 Lecture 03 - Lines

    7/52

    Parsing XML in JavaElement el = (Element) n;

    NodeList children =el.getElementsByTagName("firstname");

    Element child = (Element)children.item(1);

    System.out.println("ChildNode: "+child.getNodeName());

    Tom

    Cruise

    George

    Shanks

    Nodes also can have child nodes, here we access the firstname node.

  • 8/4/2019 Lecture 03 - Lines

    8/52

    Writing Images in Java

    BufferedImage code example

  • 8/4/2019 Lecture 03 - Lines

    9/52

    Assignment Objectives

    Read an xml file containing the world-viewcorrdinates, a set of colored points, and a listof transformation matricies.

    Create a global transformation matrix from the

    list of transformation matricies. Apply this transformation matrix to the points. Clip the points against the world window. Draw the points to the world window buffer. Save the results to an image

  • 8/4/2019 Lecture 03 - Lines

    10/52

    Assignment 1

    Skeleton Java Code on Assignment Page useit!

    Start EARLY to avoid problems! Dont get caught up in XML, contact me if you

    have issues!

  • 8/4/2019 Lecture 03 - Lines

    11/52

    Line Drawing

  • 8/4/2019 Lecture 03 - Lines

    12/52

    12

    Scan-Conversion AlgorithmsScan-Conversion:

    Computing pixelcoordinates forideal line on2D raster grid

    Pixels bestvisualized as

    circles/dotsWhy? Monitor

    hardware

    1994 Foley/VanDam/Finer/Huges/Phillips ICG

  • 8/4/2019 Lecture 03 - Lines

    13/52

    In-Class Group Assignment

  • 8/4/2019 Lecture 03 - Lines

    14/52

    14

    Drawing a Line

    y= mx+ B m = y /x Start at leftmostxand increment by 1

    x =

    1yi = Round(mxi + B)

    This is expensive and inefficient Since x = 1,yi+1 =yi + m

    No more multiplication!This leads to an incremental algorithm

  • 8/4/2019 Lecture 03 - Lines

    15/52

    15

    Digital Differential Analyzer (DDA) If |slope| is less then 1

    x = 1 elsey = 1

    Check for vertical line m = Compute corresponding

    y(x) = m (1/m) xk+1 = xk +x

    yk+1 = yk +y

    Round (x,y) for pixellocation

    1994 Foley/VanDam/Finer/Huges/Phillips ICG

    xk+1

    x

    xk

    yk+1

    yky

  • 8/4/2019 Lecture 03 - Lines

    16/52

    16

    Generalizing DDA If |slope| is less than or equal to 1

    Ending point should be right of starting point

    If |slope| is greater than 1 Ending point should be above starting point

    Vertical line is a special case x= 0

  • 8/4/2019 Lecture 03 - Lines

    17/52

    17

    Bresenhams Algorithm1965 @ IBMBasic Idea:

    Only integerarithmetic Incremental

    Consider theimplicitequationfor a line:

    1994 Foley/VanDam/Finer/Huges/Phillips ICG

  • 8/4/2019 Lecture 03 - Lines

    18/52

    18

    Bresenhams AlgorithmGiven:implicitline equation:Let:

    where rand q are points on the line and dx,dyare positiveThen:Observe that all of these are integersand: for points above the line

    for points below the line

    Pics/Math courtesy of Dave Mount @ UMD-CP

  • 8/4/2019 Lecture 03 - Lines

    19/52

    19

    Bresenhams AlgorithmSuppose we just

    finished(assume 0 slope

    1) other casessymmetric

    Which pixel next?

    E or NE

    Pics/Math courtesy of Dave Mount @ UMD-CP

    MQ

  • 8/4/2019 Lecture 03 - Lines

    20/52

    20

    Assume: Q = exacty value atymidway between E and NE:Observe:If , then pick EElse pick NE

    If ,it doesnt matter

    Bresenhams Algorithm

    Pics/Math courtesy of Dave Mount @ UMD-CP

    M

    Q < M

    Q = M MQ

  • 8/4/2019 Lecture 03 - Lines

    21/52

    21

    Create modified implicit function (2x)

    Create a decision variable D to select, where D

    is the value offat the midpoint:

    Bresenhams Algorithm

    Pics/Math courtesy of Dave Mount @ UMD-CP

    M

  • 8/4/2019 Lecture 03 - Lines

    22/52

    22

    If then M is below the lineNE is the closest pixel

    If then M is above the line E is the closest pixel

    Bresenhams Algorithm

    M

    Note: because we multiplied by 2x, D is now aninteger which is very good news

    How do we make this incremental??

  • 8/4/2019 Lecture 03 - Lines

    23/52

    23

    What increment for computing a new D? Next midpoint is:

    Hence, increment by:

    Case I: When E is next

    Pics/Math courtesy of Dave Mount @ UMD-CP

    M

  • 8/4/2019 Lecture 03 - Lines

    24/52

    24

    Case II: When NE is next

    What increment for computing a new D? Next midpoint is:

    Hence, increment by:

    Pics/Math courtesy of Dave Mount @ UMD-CP

    M

  • 8/4/2019 Lecture 03 - Lines

    25/52

    25

    How to get an initial value for D? Suppose we start at: Initial midpoint is:Then:

    Pics/Math courtesy of Dave Mount @ UMD-CP

  • 8/4/2019 Lecture 03 - Lines

    26/52

    26

    The Algorithm

    Assumptions:

    0 slope 1

    Pre-computed:

    Pics/Math courtesy of Dave Mount @ UMD-CP

    void bresenham( point q, point r ){int dx,dy,D,x,y, incrE, incrNE;if( r.x

  • 8/4/2019 Lecture 03 - Lines

    27/52

    Problem!

    Only works for In class exercise: extend so it works for void bresenham( point q, point r ){

    int dx,dy,D,x,y, incrE, incrNE;if( r.x

  • 8/4/2019 Lecture 03 - Lines

    28/52

    Problem!

    In class exercise: change so it works for void bresenham( point q, point r ){

    int dx,dy,D,x,y, incrE, incrNE;if( r.x

  • 8/4/2019 Lecture 03 - Lines

    29/52

    29

    Generalize Algorithm

    Ifqx > rx, swap points

    If slope > 1, always increment y, conditionallyincrement x

    If -1 < slope < 0, always increment x,conditionally decrement y

    If slope > 1, always increment y, conditionallyincrement x (Rework D increments)

    If slope < -1, always decrement y,conditionally increment x (Rework Dincrements)

  • 8/4/2019 Lecture 03 - Lines

    30/52

    30

    Some issues with Bresenhams

    AlgorithmsPixel density varies

    based on slopestraight lines look

    darker, morepixels per unitlength

    Endpoint orderLine from P1 to P2

    should match P2to P1

    Always choose E

    when hitting M, 1994 Foley/VanDam/Finer/Huges/Phillips ICG

  • 8/4/2019 Lecture 03 - Lines

    31/52

    31

    Some issues with Bresenhams

    AlgorithmsHow to handle the line when it hits the clip

    window?

    Vertical intersectionsCould change line slopeNeed to change init cond.

    Horizontal intersections

    Again, changes in theboundary conditions

    Cant just intersectthe line w/ the box

    1994 Foley/VanDam/Finer/Huges/Phillips ICG

  • 8/4/2019 Lecture 03 - Lines

    32/52

    Line clipping

    Lecture Credits: Most pictures are fromFoley/VanDam; Additional and extensive thanks

    also goes to those credited on individual slides

  • 8/4/2019 Lecture 03 - Lines

    33/52

    33

    Scissoring Clipping

    Pics/Math courtesy of Dave Mount @ UMD CP

    Performed during scan conversion of

    the line (circle, polygon)

    Compute the next point (x,y)If xmin x x max and ymin y y max Then output the point.Else do nothing

    Issues with scissoring:

    Too slowDoes more work then necessary

    Better to clip lines to window, thandraw lines that are outside of

    window

  • 8/4/2019 Lecture 03 - Lines

    34/52

    34

    The Cohen-Sutherland Line Clipping

    AlgorithmHow to clip lines to fit

    in windows?easy to tell if whole

    line falls w/inwindow

    harder to tell whatpart falls inside

    Pics/Math courtesy of Dave Mount @ UMD CP

  • 8/4/2019 Lecture 03 - Lines

    35/52

    35

    Cohen-SutherlandBasic Idea:First, do easy test

    completelyinsideor outside thebox?

    If no, we need a

    more complextestNote: we will also

    need to figure out

    how line Pics/Math courtesy of Dave Mount @ UMD CP

  • 8/4/2019 Lecture 03 - Lines

    36/52

    36

    Cohen-Sutherland

    Perform trivial accept and reject Assign each end point a location code

    Perform bitwise logical operations on a lineslocation codes Accept or reject based on result Frequently provides no information

    Then perform more complex line intersection

  • 8/4/2019 Lecture 03 - Lines

    37/52

    37

    Cohen-SutherlandThe Easy Test:Compute 4-bit code

    based onendpointsP1 and

    P2

    Pics/Math courtesy of Dave Mount @ UMD CP

  • 8/4/2019 Lecture 03 - Lines

    38/52

    38

    Cohen-Sutherland

    Line is completelyvisible iff both codevalues of endpointsare 0, i.e.

    If line segments arecompletely outsidethe window, then

    Pics/Math courtesy of Dave Mount @ UMD CP

  • 8/4/2019 Lecture 03 - Lines

    39/52

    39

    Cohen-SutherlandOtherwise,

    we clip the lines:

    We know that thereis a bit flip, w.o.l.g.assume its (x0,x1)

    Which bit? Try `em all!

    suppose its bit 4Thenx0

  • 8/4/2019 Lecture 03 - Lines

    40/52

    40

    Cohen-SutherlandClearly:Using similartriangles

    Solving foryc gives

    Pics/Math courtesy of Dave Mount @ UMD CP

  • 8/4/2019 Lecture 03 - Lines

    41/52

    41

    Cohen-SutherlandReplace

    (x0,y0) with (xc,yc)Re-compute codesContinue until all

    bit flips (clip lines)are processed,i.e. all points areinside the clipwindow

    Pics/Math courtesy of Dave Mount @ UMD CP

  • 8/4/2019 Lecture 03 - Lines

    42/52

    42

    Cohen Sutherland

    0101

    1010

  • 8/4/2019 Lecture 03 - Lines

    43/52

    43

    Cohen Sutherland

    0101

    0010

  • 8/4/2019 Lecture 03 - Lines

    44/52

    44

    Cohen Sutherland

    0001

    0010

  • 8/4/2019 Lecture 03 - Lines

    45/52

    45

    Cohen Sutherland

    0001

    0000

  • 8/4/2019 Lecture 03 - Lines

    46/52

    46

    Cohen Sutherland

    0000

    0000

  • 8/4/2019 Lecture 03 - Lines

    47/52

    47

    Parametric Line Clipping

    Developed by Cyrus and Beck in 1978Used to clip 2D/3D lines against convex

    polygon/polyhedron

    Liang and Barsky (1984) algorithmefficient in clipping upright 2D/3Dclipping regions

    Cyrus-Beck may be reduced to more

    efficient Liang-Barsky caseBased on parametric form of a line

    Line: P(t) = P0 +t(P1-P0)

  • 8/4/2019 Lecture 03 - Lines

    48/52

    48

    Parametric Line Equation

    Line: P(t) = P0 +t(P1-P0)

    t value defines a point on the line going throughP0 and P1

    0

  • 8/4/2019 Lecture 03 - Lines

    49/52

    49

    The Cyrus-Beck Technique

    Cohen-Sutherland algorithm computes (x,y)intersections of the line and clipping edge

    Cyrus-Beck finds a value of parameter t for

    intersections of the line and clipping edges Simple comparisons used to find actual

    intersection points Liang-Barsky optimizes it by examining tvalues

    as they are generated to reject some linesegments immediately

  • 8/4/2019 Lecture 03 - Lines

    50/52

    50

    Finding the Intersection PointsLine P(t) = P

    0

    +t(P1

    -P0

    )Point on the edge PeiNi Normal to edge i

    Make sure1.D 0, or P 1 P02.Ni D 0, lines are not

    parallel

    1994 Foley/VanDam/Finer/Huges/Phillips ICG

    DN

    ]P[PNt

    )-P(PLet D

    ]-Pt[PN]-P[PN])-P-Pt(P[PN

    ][P(t)-PN

    i

    Eii

    iEii

    Eii

    Eii

    =

    =

    =+

    =+

    =

    0

    01

    010

    010

    0

    0

    0

  • 8/4/2019 Lecture 03 - Lines

    51/52

    51

    Calculating Ni

    Ni for window edges WT: (0,1) WB: (0, -1) WL: (-1,0) WR: (1,0)

    Ni

    for arbitrary edges Calculate edge direction

    E = (V1 - V0) / |V1 - V0|

    Be sure to process edges in CCW order

    Rotate direction vector -90 Nx = Ey Ny = -Ex

  • 8/4/2019 Lecture 03 - Lines

    52/52

    52

    Finding the Line Segment

    Calculate intersection points between line and every windowline

    Classify points as potentially entering (PE) or leaving (PL)

    PE if crosses edge into inside half plane => angle P0P1 and Ni

    greater 90 => Ni

    D < 0

    PL otherwise.

    Find Te = max(te)

    Find Tl = min(tl)

    Discard ifTe

    > Tl

    IfTe < 0 Te = 0

    IfTl > 1 Tl = 1

    Use Te, Tlto compute intersection coordinates (xe,ye), (xl,yl)