Ray intersection: implicit

• Parametric ray

• Implicit surface Ray-Surface Intersection

• Point belonging to intersection

CS 417 Lecture 33

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 1 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 2

Ray- intersection: algebraic Ray-sphere intersection: algebraic

• Begin with general form: • Solution for t by quadratic formula:

• Parametric sphere:

– assume unit sphere; other in a moment…

– simpler form holds when d is a unit vector

but we won’t assume this in practice (reason later) • Substitute: – I’ll use the unit-vector form to make the geometric

interpretation – this is a quadratic equation in t

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 3 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 4

Ray-sphere intersection: geometric Ray-sphere: and tex. coords

• Normal: gradient of f

– this is obvious from the geometric standpoint

• Texture: use latitude-longitude parameterization

– again for unit sphere:

– where x = (x, y, z) is the intersection point

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 5 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 6 Ray-sphere intersection: examples Ray-triangle intersection

• Unit sphere, ray ((2,0,0), (-1,0,0)) • Intersect first

• Unit sphere, ray ((2,1,0), (-1,0,0)) – implicit plane:

• Unit sphere, ray ((0,0,0), (-1,0,0))

– substitute parametric ray and solve for t:

• Next check if point is inside triangle

– first project both to plane

– choose plane based on triangle normal

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 7 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 8

Ray-triangle intersection Ray-triangle intersection

• In plane, triangle is the intersection of 3 half spaces • In plane, triangle is the intersection of 3 half spaces

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 9 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 9

Ray-triangle intersection Ray-triangle intersection

• In plane, triangle is the intersection of 3 half spaces • In plane, triangle is the intersection of 3 half spaces

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 9 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 9 Intersecting transformed primitives Intersecting triangle meshes

• Transform ray inversely • Basically, intersect each triangle

• But want to live off a compact data structure

– this is one reason for using meshes in the first place

– don’t want to expand out to separate triangles!

– to summarize space issue:

• mesh: 3 ints + (vertex data) / 2 per triangle ] 0 0

0 • separate: 3 * (vertex data) per triangle 2

y e l r i • position, normal, tex coords: (vertex data) = 8 floats h S [ – thus mesh costs 12 + 32/2 = 28 bytes/triangle

– separate costs 32*3 = 96 bytes/triangle (3.4 x)

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 10 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 11

Intersecting triangle meshes Barycentric coordinates

• Second reason for mesh: continuity • A coordinate system for triangles

– shared positions reduce potential for cracks – algebraic viewpoint:

– want shared normals for smooth shading

– want shared texture coordinates for continuity

• This means we need to interpolate things across the – geometric viewpoint (areas):

triangle for every intersection calculation. • Triangle interior test: ] 0 0

– generally use linear interpolation 0 2

y e l r – implement this using barycentric coordinates i h S [

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 12 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 13

Barycentric coordinates Barycentric coordinates

• A coordinate system for triangles • Linear viewpoint: basis for the plane

– geometric viewpoint: distances ] 0 0 0 2

y e l r i h S [ – linear viewpoint: basis of edges

– in this view, the triangle interior test is just

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 14 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 15 Barycentric ray-tri. intersection Interpolating across triangles

• Directly compute barycentric coords of ray-plane • Intersection point is written in triangle basis

intersection point – makes inside-triangle test simple

– also makes linear interpolation simple

• Linear interpolation

– idea, again: values of some quantity at the three vertices

p define a unique affine function on the plane

– solve this 3x3 matrix system ] 0 0

0 – in barycentric basis it is easy to compute this function 2

– result gives ray parameter y e

l v v v r • want value at a, at b, at c i a b c

and barycentric coords in one h S [ • affine equation is a + βb + γc step x v v v v v – geometric interpretation • therefore coefficients are a, b – a, c – a

• resemblance to point equation is no accident

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 16 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 17

Barycentric intersection summary Intersecting other simple shapes

• Given: triangle (a, b, c) and ray p + td • Cube

• First form a 3x3 matrix system and solve it

– this gives you the barycentric coords ant the ray parameter ] 0 0 0 2

• Next use the barycentric coords to interpolate y e l r i

– position h S [

– normals

– texture coordinates • Cylinder – anything else you want

– intersect circle in 2D, plus two end caps – (all referring to the shared vertex data in the mesh)

• Cone • And return the whole lot (in an intersection record)

– left as an exercise

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 18 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 19

Intersection against many shapes Accelerating ray-scene intersection

• The basic idea is: • Intersecting a ray against all primitives spends much

tMin = +infinity; hitShape = null; time intersecting against far-away primitives

for shape in shapeList { • A spatial data structure helps by finding only the

t = intersect(ray, shape); primitives that are near the ray

if t < tMin { – generally they work by partitioning 3D space into a number

tMin = t; of regions and keeping track of which objects intersect which hitShape = shape; regions. } – ray traversal then amounts to enumerating the regions } intersected by the ray.

– but this is linear in the number of shapes

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 20 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 21 Acceleration by spatial subdivision More complex implicit surfaces

• Use a collection of axis-aligned boxes as the regions • E.g. defined by analytic functions or volume data

• Variants: regular (grid) and hierarchical (kdtree/octree) • Implicit intersection equation solved directly using root

finding (secant algorithm is popular) ] 2 0 ] 0 9 2

8 . l 9 a 1

t r e e

t n h s s g i a r l n G [ E

• Alternative: bounding volume hierarchy

Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 22 Cornell CS417 Spring 2003 • Lecture 33 © 2003 Steve Marschner • 23