Ray Polygon Intersection. Lecture #17 Thursday, October 28th, 2014 Review - Ray Casting! Goal: throw rays through pixels, intersect them with surfaces! Compute surface reflectance at points of intersection PRP (fp) 10/28/14 Bruce Draper & J. Ross Beveridge 2014 2
Review - Cameras & Ray Casting Given PRP, then where is pixel P x,y?! Remember the camera s rotation matrix:! N is the vector in the direction of the VPN! U is the camera s x axis in world coordinates! V is the camera s y axis in world coordinates! N, U, V are all unit length! Focal length d from PRP to image plane P α,β = PRP dvpn +αu + βv 10/28/14 Bruce Draper & J. Ross Beveridge 2014 3 Review - Cameras (II)! A ray is a parametric line for t > 0.! Move away from PRP, here called Q! in direction of vector with base at Q! and tip at P R α,β R α,β P α,β = Q +αu + βv dvpn ( t) = Q + t(p α,β Q) ( t) = Q + t αu + βv dvpn ( ) 10/28/14 Bruce Draper & J. Ross Beveridge 2014 4
Ray/Surface Intersection! Implicit surfaces are defined by f (P) = 0! Given a ray R t ( ) = Q + tw! The intersection is solved by ( ) = 0 f Q + tw 10/28/14 Bruce Draper & J. Ross Beveridge 2014 5 Intersect a Polygon Face! Find intersection point P on infinite plane.! Test if point P is inside polygon (later).! General equation for a plane in 3D: ax + by + cz ρ = 0! Recall an (x,y,z) point P is on a plane iff: N P = ρ where N = a b c 10/28/14 Bruce Draper & J. Ross Beveridge 2014 6
Here is a worked Example 10/28/14 Bruce Draper & J. Ross Beveridge 2014 7 Planar Intersection Find t-star Commonly described as t-star, the value of t along the ray where the ray punches through the infinite plane. N ( Q + t * W ) = ρ t * N W = ρ N Q t * = ρ N Q N W The 3D point of intersection is: ( ) = Q + t * W R t * Be careful. What if this is zero? Cost: 6 mults, 1 div, 5 adds, 1 negation 10/28/14 Bruce Draper & J. Ross Beveridge 2014 8
Now About Inside/Outside! Finding point of intersection arguably easy.! Is the point inside the bounded polygon?! Multiple ways to approach this question! Odd/even parity for general polygons! Divide convex polygons into triangles, perform triangle inside/outside test 10/28/14 Bruce Draper & J. Ross Beveridge 2014 9 Arbitrary polygons Step 1: project from 3D to 2D! Polygon edges are vectors in 3D! Point of ray/plane intersection is in 3D! but they all reside on the same plane! test inside/outside in 2D, not 3D! Not just faster; fewer stability issues! How do we project onto a 2D plane? 10/28/14 Bruce Draper & J. Ross Beveridge 2014 10
But, there is a faster way.! Orthographic projection onto either:! XY, YZ or XZ plane.! How? Just drop one dimension (set it to zero).! But, be careful! If your polygon is in the XY plane (z=const)! and you drop X or Y,! your polygon collapses to a line.! Close to parallel is essentially just as bad.! If your polygon is almost in the XY plane (Δz 0), then round-off can create problems 10/28/14 Bruce Draper & J. Ross Beveridge 2014 11 Illustration favored choice! Six sided polygon nearly parallel with?! See the trap. XY Plane View (red,green) XZ Plane View (red,blue) YZ Plane View (green,blue) 10/28/14 Bruce Draper & J. Ross Beveridge 2014 12
Projections for Intersection (II)! To be safe, drop the dimension with the largest value in N (the plane normal)! This is the coordinate most orthogonal to the plane, and therefore the safest to drop! Alternative: rotate the coordinate system to make N the Z axis! Rotation matrix is easy to compute! More multiplies, but no round-off issues! Fewer cases in your code 10/28/14 Bruce Draper & J. Ross Beveridge 2014 13 Rotate align N with Z! Equation of the plane. ˆN P = ρ where ˆN = a b c and ˆN ˆN =1! Rotation R =?????? a b c There are a variety of ways to selection the other to orthogonal basis vectors. 10/28/14 Bruce Draper & J. Ross Beveridge 2014 14
2D Polygon Membership! Either way,! We now have a 2D problem:! Polygon specified with 2D vertices! Point P (of planar intersection) is a 2D point! Task:! is P inside or outside of the polygon? 10/28/14 Bruce Draper & J. Ross Beveridge 2014 15 Odd/Even Parity Rule! Tests whether a 2D point P is inside or outside of a polygon! Step 1: draw a ray from P in any direction in the plane. (we overwork word ray here)! Step 2: count boundary crossings! Odd # of intersections inside! Even # of intersections outside 10/28/14 Bruce Draper & J. Ross Beveridge 2014 16
Odd/Even Illustrated Direction doesn t matter! 10/28/14 Bruce Draper & J. Ross Beveridge 2014 17 Odd/Even Intersections! Represent boundaries as rays! B i = V i+1 + t b (V i+1 V i )! Start ray at intersection P in any direction! R = P + t p (1,0) works nicely! To intersect ray R with polygon P! Intersect R with every boundary! An intersection is valid iff! t p 0 and 0 t b < 1.0! Odd # intersections => inside, even outside. 10/28/14 Bruce Draper & J. Ross Beveridge 2014 18
Is this efficient?! No!!!! But it s easy! and anyone in graphics ought to know it.! because it is general, works for! non-convex polygons,! self-intersection polygons.! However, efficiency matters! There are better methods for special cases! We will teach one in particular for triangles. 10/28/14 Bruce Draper & J. Ross Beveridge 2014 19 Ray/Triangle Intersections! Ray/Triangle intersections are efficient and can be computed directly in 3D! They rely on the following implicit definition of a triangle: P = A + β ( B A) +γ ( C A) β > 0,γ > 0, β +γ <1 10/28/14 Bruce Draper & J. Ross Beveridge 2012 20
Visualize the Math P = A + 0.50( B A) + 0.48( C A) P β = 0.5 γ = 0.48 A C-A 10/28/14 Bruce Draper & J. Ross Beveridge 2012 21 Think About a Trapezoid P = A + β ( B A) +γ ( C A) C-A β >1 γ < 0 β > 0, γ > 0 β > 0, γ > 0 1< β +γ < 2 γ >1 0 < β +γ <1 A C-A β < 0 10/28/14 Bruce Draper & J. Ross Beveridge 2012 22
Solve for implicit intersections! To find intersection, f(l+tu) = 0 (slide #4) Q + tw = A + β ( B A) +γ(c A)! This is a set of 3 linear equations with 3 unknowns: unkowns t, β, γ Q x + tw x = A x + β ( B x A x ) +γ ( C x A x ) Q y + tw y = A y + β B y A y ( ) +γ ( C y A y ) Q z + tw z = A z + β ( B z A z ) +γ ( C z A z ) 10/28/14 Bruce Draper & J. Ross Beveridge 2012 23 Push it through! Constants on one side. Q x A x = β ( B x A x ) +γ ( C x A x ) tw x Q y A y = β B y A y ( ) +γ ( C y A y ) tw y Q z A z = β ( B z A z ) +γ ( C z A z ) tw z! Now in Matrix Form ( B x A x ) ( C x A x ) W x ( B y A y ) C y A y ( B z A z ) C z A z ( ) W y ( ) W z β γ t = Q x A x Q y A y Q z A z 10/28/14 Bruce Draper & J. Ross Beveridge 2012 24
And, how to solve! Use a numerical solver, or! thanks to Wikipedia 10/28/14 Bruce Draper & J. Ross Beveridge 2014 25