ray polygon intersection. lecture #17 · now – about inside/outside ! finding point of...

13
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 10/28/14 © Bruce Draper & J. Ross Beveridge 2014 2 PRP (fp)

Upload: others

Post on 16-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

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

10/28/14 © Bruce Draper & J. Ross Beveridge 2014 2

PRP (fp)

Page 2: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

Review - Cameras & Ray Casting

Given PRP, then where is pixel Px,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

10/28/14 © Bruce Draper & J. Ross Beveridge 2014 3

Pα,β = PRP − dVPN +αU +βV

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

10/28/14 © Bruce Draper & J. Ross Beveridge 2014 4

Pα,β =Q+αU +βV − dVPN

Rα,β t( ) =Q+ t(Pα,β −Q)Rα,β t( ) =Q+ t αU +βV − dVPN( )

Page 3: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

Ray/Surface Intersection

!  Implicit surfaces are defined by

!  Given a ray

!  The intersection is solved by

10/28/14 © Bruce Draper & J. Ross Beveridge 2014 5

f (P) = 0

R t( ) =Q+ tW

f Q+ tW( ) = 0

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: !  Recall an (x,y,z) point P is on a plane iff:

10/28/14 © Bruce Draper & J. Ross Beveridge 2014 6

ax + by+ cz− ρ = 0

N ⋅P = ρ where N = a b c

Page 4: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

Here is a worked Example

10/28/14 © Bruce Draper & J. Ross Beveridge 2014 7

Planar Intersection – Find t-star

10/28/14 © Bruce Draper & J. Ross Beveridge 2014 8

N ⋅ Q+ t*W( ) = ρt*N ⋅W = ρ − N ⋅Q

t* = ρ − N ⋅QN ⋅W

Be careful. What if this is zero?

Cost: 6 mults, 1 div, 5 adds, 1 negation

Commonly described as t-star, the value of t along the ray where the ray punches through the infinite plane.

The 3D point of intersection is:

R t*( ) =Q+ t*W

Page 5: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

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

Page 6: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

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.

10/28/14 © Bruce Draper & J. Ross Beveridge 2014 12

XY Plane View (red,green)

XZ Plane View (red,blue)

YZ Plane View (green,blue)

Page 7: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

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.

!  Rotation

10/28/14 © Bruce Draper & J. Ross Beveridge 2014 14

N̂ ⋅P = ρ where N̂ = a b c and N̂ ⋅ N̂ =1

R =? ? ?? ? ?a b c

There are a variety of ways to selection the other to orthogonal basis vectors.

Page 8: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

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 16 © Bruce Draper & J. Ross Beveridge 2014

Page 9: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

Odd/Even Illustrated

Direction doesn’t matter!

10/28/14 17 © Bruce Draper & J. Ross Beveridge 2014

Odd/Even Intersections

!  Represent boundaries as rays !  Bi = Vi+1 + tb(Vi+1 – Vi)

!  Start ray at intersection P in any direction ! R = P + tp(1,0) works nicely…

!  To intersect ray R with polygon P !  Intersect R with every boundary !  An intersection is valid iff

!  tp ≥ 0 and 0 ≤ tb < 1.0

! Odd # intersections => inside, even outside.

10/28/14 © Bruce Draper & J. Ross Beveridge 2014 18

Page 10: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

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:

10/28/14 © Bruce Draper & J. Ross Beveridge 2012 20

P = A+β B− A( )+γ C − A( )β > 0,γ > 0,β +γ <1

Page 11: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

Visualize the Math …

10/28/14 © Bruce Draper & J. Ross Beveridge 2012 21

A C-A

β = 0.5 γ = 0.48

P = A+ 0.50 B− A( )+ 0.48 C − A( )

P

Think About a Trapezoid

10/28/14 © Bruce Draper & J. Ross Beveridge 2012 22

A C-A

C-A

β > 0, γ > 01< β +γ < 2

β < 0

β > 0, γ > 00 < β +γ <1

γ < 0γ >1

β >1

P = A+β B− A( )+γ C − A( )

Page 12: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

Solve for implicit intersections

!  To find intersection, f(L+tU) = 0 (slide #4) !  This is a set of 3 linear equations with 3

unknowns:

10/28/14 © Bruce Draper & J. Ross Beveridge 2012 23

Q+ tW = A+β B− A( )+γ (C − A)

Qx + tWx = Ax +β Bx − Ax( )+γ Cx − Ax( )Qy + tWy = Ay +β By − Ay( )+γ Cy − Ay( )Qz + tWz = Az +β Bz − Az( )+γ Cz − Az( )

unkowns t, β, γ

Push it through …

!  Constants on one side. !  Now in Matrix Form

10/28/14 © Bruce Draper & J. Ross Beveridge 2012 24

Qx − Ax = β Bx − Ax( )+γ Cx − Ax( )− tWx

Qy − Ay = β By − Ay( )+γ Cy − Ay( )− tWy

Qz − Az = β Bz − Az( )+γ Cz − Az( )− tWz

Bx − Ax( ) Cx − Ax( ) −Wx

By − Ay( ) Cy − Ay( ) −Wy

Bz − Az( ) Cz − Az( ) −Wz

β

γ

t=

Qx − Ax

Qy − Ay

Qz − Az

Page 13: Ray Polygon Intersection. Lecture #17 · Now – About Inside/Outside ! Finding point of intersection arguably easy. ! Is the point inside the bounded polygon? ! Multiple ways to

And, how to solve …

!  Use a numerical solver, or !  thanks to Wikipedia …

10/28/14 © Bruce Draper & J. Ross Beveridge 2014 25