Sei sulla pagina 1di 52

CS 430: Computer Graphics 1

Assignment 1
Framework for World View Read XML Render Points in World Window Save to an image

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

Assignment 1: XML

<?xml version="1.0"?> <world> <window XLowerBound=700" YLowerBound=100" XUpperBound=800" YUpperBound=300" /> <shape type="point" color="000000"> <params x="25" y="25" /></shape> </world>

XML

Nodes define the XML doc <?xml

version="1.0"?> <world> Tags are nodes <window Even white space is a node! XLowerBound=700" YLowerBound=100" Nodes have children XUpperBound=800" YUpperBound=300" /> <shape type="dot"> <params x="50" y="50" /> </shape> </world>

Parsing XML in Java


Document doc = db.parse(file);

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

<?xml version="1.0"?> <company> <employee type="actor"> <firstname>Tom</firstname> <lastname>Cruise</lastname> </employee> <employee type="terminated"> <firstname>George</firstname>

<lastname>Shanks</lastname> </employee> </company>

Getsallofthenodeswithaspecificname!

Parsing XML in Java


NodeList 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") );

<?xml version="1.0"?> <company> <employee type="actor"> <firstname>Tom</firstname> <lastname>Cruise</lastname> </employee> <employee type="terminated"> <firstname>George</firstname>

<lastname>Shanks</lastname> </employee> </company>

Nodescanbeanything,anelementisanodecorrespondingtoaTAG Elementsmayhaveattributes(flagsinsidethetag)

Parsing XML in Java


Element el = (Element) n; NodeList children = el.getElementsByTagName("firstname") ; Element child = (Element) children.item(1); System.out.println("Child Node: "+child.getNodeName());

<?xml version="1.0"?> <company> <employee type="actor"> <firstname>Tom</firstname> <lastname>Cruise</lastname> </employee> <employee type="terminated"> <firstname>George</firstname>

<lastname>Shanks</lastname> </employee> </company>

Nodesalsocanhavechildnodes,hereweaccessthefirstnamenode.

Writing Images in Java

BufferedImage code example

Assignment Objectives
Read an xml file containing the world-view corrdinates, a set of colored points, and a list of 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

Assignment 1
Skeleton Java Code on Assignment Page use it! Start EARLY to avoid problems! Dont get caught up in XML, contact me if you have issues!

Line Drawing

Scan-Conversion Algorithms
Scan-Conversion:

Computing pixel coordinates for ideal line on 2D raster grid Pixels best visualized as circles/dots
Why?

Monitor hardware
12

1994 Foley/VanDam/Finer/Huges/Phillips ICG

In-Class Group Assignment

Drawing a Line
y = mx + B m = y / x Start at leftmost x and increment by 1

x = 1

yi = Round(mxi + B) This is expensive and inefficient Since x = 1, yi+ = yi + m 1


No

more multiplication!

This leads to an incremental algorithm


14

Digital Differential Analyzer (DDA)

If |slope| is less then 1


x = 1 else y = 1

Check for vertical line


m=

y Compute corresponding yk y (x) = m (1/m) xk+ = xk + x 1 yk+ = yk + y 1 Round (x,y) for pixel location

yk+1

xk xk+1
15

1994 Foley/VanDam/Finer/Huges/Phillips ICG

Generalizing DDA

If |slope| is less than or equal to 1


Ending

point should be right of starting point point should be above starting point

If |slope| is greater than 1


Ending

Vertical line is a special case

x = 0

16

Bresenhams Algorithm
1965

@ IBM Basic Idea:


Only

integer arithmetic Incremental

Consider

the implicit equation for a line:

17

1994 Foley/VanDam/Finer/Huges/Phillips ICG

Bresenhams Algorithm

Given: implicit line equation: Let: where r and q are points on the line and dx ,dy are positive

Then: Observe that all of these are integers and: for points above the line for points below the line

18

Pics/Math courtesy of Dave Mount @ UMD-CP

Bresenhams Algorithm
Suppose

we just finished
(assume

0 slope 1) other cases symmetric

Which
E

pixel next?

or NE

19
Pics/Math courtesy of Dave Mount @ UMD-CP

Bresenhams Algorithm
Assume: Q = exact y value at y midway between E and NE: Observe: If Q < M , then pick E Else pick NE If Q=M , it doesnt matter

20
Pics/Math courtesy of Dave Mount @ UMD-CP

Bresenhams Algorithm

Create modified implicit function (2x) Create a decision variable D to select, where D is the value of f at the midpoint:

M
21
Pics/Math courtesy of Dave Mount @ UMD-CP

Bresenhams Algorithm

If
NE

then M is below the line


is the closest pixel

If
E

then M is above the line


is the closest pixel

No te :be c ause we multiplie dby2x,Disno wan

inte ge rwhic hisve rygo o dne ws M Ho wdo we make thisinc re me ntal??

22

Case I: When E is next


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

Hence, increment by:

23
Pics/Math courtesy of Dave Mount @ UMD-CP

Case II: When NE is next


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

Hence, increment by:

24
Pics/Math courtesy of Dave Mount @ UMD-CP

How to get an initial value for D?


Suppose we start at: Initial midpoint is: Then:

25
Pics/Math courtesy of Dave Mount @ UMD-CP

The Algorithm
void bresenham( point q, point r ){ int dx,dy,D,x,y, incrE, incrNE; if( r.x<q.x ) swap(q,r); y=q.y;dx=r.x-q.x; dy=r.y-q.y; D=2*dy-dx; incrE=dy*2; incrNE=(dy-dx)*2; for( x=q.x;x<r.x;x++) { writePixel(x,y); if( D<=0 ) D+=incrE; else { D+=incrNE; y++; } }

Assumptions:

0 slope 1
26

Pre-computed:
Pics/Math courtesy of Dave Mount @ UMD-CP

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<q.x ) swap(q,r); y=q.y;dx=r.x-q.x; dy=r.y-q.y; D=2*dy-dx; incrE=dy*2; incrNE=(dy-dx)*2; for( x=q.x;x<r.x;x++) { writePixel(x,y); if( D<=0 ) D+=incrE; else { D+=incrNE; y++; } }

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<q.x ) swap(q,r); y=q.y;dx=r.x-q.x; dy=r.y-q.y; D=2*dy-dx; incrE=dy*2; incrNE=(dy-dx)*2; for( x=q.x;x<r.x;x++) { writePixel(x,y); if( D<=0 ) D+=incrE; else { D+=incrNE; y++; } } }

Generalize Algorithm

If qx > rx, swap points

If slope > 1, always increment y, conditionally increment x If -1 < slope < 0, always increment x, conditionally decrement y If slope > 1, always increment y, conditionally increment x (Rework D increments) If slope < -1, always decrement y, conditionally increment x (Rework D increments)
29

Some issues with Bresenhams Algorithms


Pixel

density varies based on slope


straight

lines look darker, more pixels per unit length Endpoint order Line from P1 to P2 should match P2 to P1 Always choose E when hitting M,

30

1994 Foley/VanDam/Finer/Huges/Phillips ICG

Some issues with Bresenhams Algorithms


How

to handle the line when it hits the clip window? Vertical intersections
Could

change line slope Need to change init cond.


Horizontal
Again,

intersections

changes in the boundary conditions Cant just intersect the line w/ the box

31

1994 Foley/VanDam/Finer/Huges/Phillips ICG

Line clipping

LectureCredits:Mostpicturesarefrom Foley/VanDam;Additionalandextensivethanks alsogoestothosecreditedonindividualslides

Scissoring Clipping Performed during scan conversion of the line (circle, polygon)

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

Issues

with scissoring:

Too

slow Does more work then necessary


Better

to clip lines to window, than draw lines that are outside of window

33

Pics/MathcourtesyofDaveMount@UMDCP

The Cohen-Sutherland Line Clipping Algorithm


How

to clip lines to fit in windows?


easy

to tell if whole line falls w/in window harder to tell what part falls inside

34

Pics/MathcourtesyofDaveMount@UMDCP

Cohen-Sutherland
Basic Idea: First, do easy test

completely

inside or outside the box?

If

no, we need a more complex test Note: we will also need to figure out how line

35

Pics/MathcourtesyofDaveMount@UMDCP

Cohen-Sutherland

Perform trivial accept and reject

Assign each end point a location code Perform bitwise logical operations on a lines location codes Accept or reject based on result Frequently provides no information
Then

perform more complex line intersection

36

Cohen-Sutherland
The

Easy Test: Compute 4-bit code based on endpoints P1 and P2


37

Pics/MathcourtesyofDaveMount@UMDCP

Cohen-Sutherland

Line is completely visible iff both code values of endpoints are 0, i.e.

If line segments are completely outside the window, then

38

Pics/MathcourtesyofDaveMount@UMDCP

Cohen-Sutherland
Otherwise, we clip the lines: We know that there is a bit flip, w.o.l.g. assume its (x0 ,x1 )

Which

bit? Try `em all!

suppose

its bit 4 Then x0 < WL and we know that x1 WL We need to find the point: (xc ,yc )

39

Pics/MathcourtesyofDaveMount@UMDCP

Cohen-Sutherland
Clearly: Using Solving

similar triangles

for yc gives

40

Pics/MathcourtesyofDaveMount@UMDCP

Cohen-Sutherland
Replace

(x0 ,y0 ) with (xc ,yc ) Re-compute codes Continue until all bit flips (clip lines) are processed, i.e. all points are inside the clip window

41

Pics/MathcourtesyofDaveMount@UMDCP

Cohen Sutherland
0101 1010

42

Cohen Sutherland
0101 0010

43

Cohen Sutherland
0001 0010

44

Cohen Sutherland
0001 0000

45

Cohen Sutherland
0000 0000

46

Parametric Line Clipping


Developed

by Cyrus and Beck in 1978 Used to clip 2D/3D lines against convex polygon/polyhedron Liang and Barsky (1984) algorithm efficient in clipping upright 2D/3D clipping regions Cyrus-Beck may be reduced to more efficient Liang-Barsky case Based on parametric form of a line
Line:

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

47

Parametric Line Equation


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

P1

t value defines a point on the line going through P0 and P1 0 <= t <= 1 defines line segment between P0 and P1 P(0) = P0 P(1) = P1

48

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 t values as they are generated to reject some line segments immediately

49

Finding the Intersection Points


Line P(t) = P0 +t(P1-P0) Point on the edge P ei N Normal to edge i i

N i [P(t)-PEi ] = 0 [P0 + t(P -P0 )-PEi ] = 0 1 [P0 -PEi ] + N i t[P -P0 ] = 0 1 D = (P -P0 ) 1

N i N i

Let

N i [P0 PEi ] t= Ni D

Make sure 1. D 0, or P1 P0 2. Ni D 0, lines are not parallel

50

1994Foley/VanDam/Finer/Huges/PhillipsICG

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 Nx = E y Ny = -Ex
51

Rotate direction vector -90


Finding the Line Segment

Calculate intersection points between line and every window line Classify points as potentially entering (PE) or leaving (PL) PE if crosses edge into inside half plane => angle P0 P1 and Ni greater 90 => Ni D < 0 PL otherwise. Find Te = max(te) Find Tl = min(tl) Discard if Te > Tl If Te < 0 Te = 0 If Tl > 1 Tl = 1 Use Te, Tl to compute intersection coordinates (xe,ye), (xl,yl)
52

1994Foley/VanDam/Finer/Huges/PhillipsICG

Potrebbero piacerti anche