Contents

  1. Ray-Sphere Intersection
  2. Ray-Plane Intersection
  3. Ray-Triangle Intersection
  4. Ray-Box Intersection

Lesson 3:

Ray-Geometry Intersection Algorithms

Basic Math

Ray Parametrization

\vec{p}(t)=\vec{o}+t\vec{d}

Origin point: 

\vec{o}\in\mathbb{R}^3

Direction unit vector: 

\vec{d}\in\mathbb{R}^3

Distance from origin: 

t\in\mathbb{R}

Sphere Parametrization

Point on the ray: 

\vec{p}\in\mathbb{R}^3
(\vec{p}-\vec{c})\cdot(\vec{p}-\vec{c})=r^2

Origin point: 

\vec{c}\in\mathbb{R}^3

Sphere radius: 

r\in\mathbb{R}

Point on the sphere: 

\vec{p}\in\mathbb{R}^3
\vec{c}
\vec{o}
t=1
t=2
t=3
\vec{d}
\vec{p}_1
\vec{p}_2
\vec{p}_1-\vec{c}
\vec{p}_2-\vec{c}

Basic Math

Ray Parametrization

Sphere Parametrization

\vec{o}
t=1
t=2
t=3
\vec{p}_1-\vec{c}
\vec{p}_2
\vec{p}_2-\vec{c}
struct Ray {
    Vec3f org;	// Origin
    Vec3f dir;	// Direction
    double t;	// Current/maximum hit distance
} ray;
class rt::CPrimSphere 
{
    Vec3f m_origin;  // Position of the sphere's center
    float m_radius;	 // Radius of the sphere
};
\vec{c}
\vec{p}_1
\vec{d}
\vec{p}(t)=\vec{o}+t\vec{d}
(\vec{p}-\vec{c})\cdot(\vec{p}-\vec{c})=r^2

Ray-Sphere Intersection

Ray Parametrization

Sphere Parametrization

\vec{o}
\vec{d}
\vec{c}
\vec{L}=\vec{c}-\vec{o}

Geometrical Approach

\vec{x}
\vec{b}
\vec{b}=\vec{o}+t_b\vec{d}
\vec{x}=\vec{o}+t_x\vec{d}
t_b = \vec{d}\cdot\vec{L}
\Delta
t_x=t_b-\Delta
r
h
h^2=|\vec{L}|^2-t^{2}_{b}
h^2=r^2-\Delta^2
|\vec{L}|^2-t^{2}_{b}=r^2-\Delta^2
\Delta = \sqrt{r^2-|\vec{L}|^2+t^{2}_{b}}

Find closest intersection point

Introduce auxiliary point

Find

\vec{x}
\vec{b}
\Delta
\vec{p}(t)=\vec{o}+t\vec{d}
(\vec{p}-\vec{c})\cdot(\vec{p}-\vec{c})=r^2

Ray-Sphere Intersection

Ray Parametrization

Sphere Parametrization

\vec{o}
\vec{d}
\vec{L}=\vec{c}-\vec{o}
\vec{c}

Please also mind special cases:

    1. Only one intersection point

h=r
\vec{x}=\vec{o}+t_x\vec{d}
t_x=t_b

Find closest intersection point

\vec{b}
h=r

Geometrical Approach

\vec{p}(t)=\vec{o}+t\vec{d}
(\vec{p}-\vec{c})\cdot(\vec{p}-\vec{c})=r^2
\Delta = 0

Ray-Sphere Intersection

Ray Parametrization

Sphere Parametrization

\vec{o}
\vec{d}
\vec{L}=\vec{c}-\vec{o}
\vec{c}
h>r
r^2-|\vec{L}|^2+t^{2}_{b}<0

Please also mind special cases:

    2. No intersection points

\vec{x}=\vec{o}+t_x\vec{d}
t_x=t_b-\Delta
\Delta =undefined

Find closest intersection point

\vec{b}

Geometrical Approach

\vec{p}(t)=\vec{o}+t\vec{d}
(\vec{p}-\vec{c})\cdot(\vec{p}-\vec{c})=r^2
h>r

Ray-Sphere Intersection

Ray Parametrization

Sphere Parametrization

Please also mind special cases:

    3. Ray origin lies in the sphere

\vec{x}=\vec{o}+t_x\vec{d}
t_x=t_b+\Delta
\Delta = \sqrt{r^2-|\vec{L}|^2+t^{2}_{b}}

Find closest intersection point

Geometrical Approach

\vec{p}(t)=\vec{o}+t\vec{d}
(\vec{p}-\vec{c})\cdot(\vec{p}-\vec{c})=r^2
\vec{o}
\vec{d}
\vec{L}
\vec{c}
h
\vec{b}
\Delta
|\vec{L}| < r

Ray-Sphere Intersection

Ray Parametrization

Sphere Parametrization

Substitute ray equation into the sphere equation and solve it for 

(\vec{o}+t\vec{d}-\vec{c})\cdot(\vec{o}+t\vec{d}-\vec{c})-r^2=0
t
(t\vec{d} + (\vec{o}-\vec{c}))^2-r^2=0
t^2|\vec{d}|^2 - 2t\vec{d}\cdot\vec{L} + |\vec{L}|^2-r^2=0

Solve it as a quadratic equation

t_{1,2}=\frac{-b\pm\sqrt{b^2-4ac}}{2a}
1t^2 + (-2\vec{d}\cdot\vec{L})t + (|\vec{L}|^2-r^2)=0
(t\vec{d} - \vec{L})^2-r^2=0
b
c
t_{1,2}=\vec{d}\cdot\vec{L}\pm\sqrt{r^2-|\vec{L}|^2+(\vec{d}\cdot\vec{L})^2}
a

Analytical Approach

\vec{p}(t)=\vec{o}+t\vec{d}
(\vec{p}-\vec{c})\cdot(\vec{p}-\vec{c})=r^2

Ray-Plane Intersection

Ray Parametrization

Plane Parametrization

\vec{p}(t)=\vec{o}+t\vec{d}
(\vec{p}-\vec{a})\cdot\vec{n}=0

Point on the plane: 

\vec{a}\in\mathbb{R}^3

Normal to the plane: 

\vec{n}\in\mathbb{R}^3

Point on the plane: 

\vec{p}\in\mathbb{R}^3

Substitute ray equation into the plane equation and solve it for 

(\vec{o}+t\vec{d}-\vec{a})\cdot\vec{n}=0
t
t\vec{d}\cdot\vec{n} + (\vec{o}-\vec{a})\cdot\vec{n}=0
t\vec{d}\cdot\vec{n} = (\vec{a}-\vec{o})\cdot\vec{n}

Analytical Approach

t = \frac{(\vec{a}-\vec{o})\cdot\vec{n}}{\vec{d}\cdot\vec{n}}

There might be 0 or 1 or infinitely many solutions

\vec{n}
\vec{a}
\vec{p}

Ray-Triangle Intersection

Barycentric Coordinates

In order to represent point p lying on the segment defined by known points b and c we can use the same logic which lies behind the ray definition:

\vec{c}
\vec{q}
\vec{q} = \vec{b} + t(\vec{c} - \vec{b})
\vec{q} = \vec{b} + t\vec{c} - t\vec{b}
\vec{q} = (1-t)\vec{b} + t\vec{c}
\vec{b}

Ray-Triangle Intersection

Barycentric Coordinates

By analogy to represent point q lying on the segment defined by known points a and q we can write:

\vec{c}
\vec{q}
\vec{q} = \vec{b} + t(\vec{c} - \vec{b})
\vec{q} = \vec{b} + t\vec{c} - t\vec{b}
\vec{q} = (1-t)\vec{b} + t\vec{c}
\vec{b}
\vec{a}
\vec{p}
\vec{p} = \vec{q} + s(\vec{a} - \vec{q})
\vec{p} = \vec{q} + s\vec{a} - s\vec{q}
\vec{p} = (1-s)\vec{q} + s\vec{a}
\vec{p} = (1-s)\left((1-t)\vec{b} + t\vec{c}\right) + s\vec{a}
\vec{p} = s\vec{a} + (1-s)(1-t)\vec{b} + (1-s)t\vec{c}
u
v
w
1 = s + (1-s)(1-t) + (1-s)t

Ray-Triangle Intersection

Barycentric Coordinates

Thus any point p within a triangle abc may be expressed as a linear combination of its vertices:

\vec{c}
\vec{q}
\vec{b}
\vec{a}
\vec{p}
\vec{p} = w\vec{a} + u\vec{b} + v\vec{c}
w+u+v=1

where parameters

or

\vec{p} = (1-u-v)\vec{a} + u\vec{b} + v\vec{c}

are called barycentric coordinates

Ray-Triangle Intersection

Barycentric Coordinates

Geometrical meaning 

\vec{c}
\vec{b}
\vec{a}
\vec{p}
\vec{p} = (1-u-v)\vec{a} + u\vec{b} + v\vec{c}
u
v
1-u-v
u=\frac{S_{apc}}{S_{abc}}
v=\frac{S_{abp}}{S_{abc}}
1-u-v=\frac{S_{pbc}}{S_{abc}}

Where S designates the signed area of a triangle

Ray-Triangle Intersection

Möller-Trumbore Algorithm

Ray Parametrization

\vec{p}(t)=\vec{o}+t\vec{d}

Triangle Parametrization

\vec{p}(u,v)=(1-u-v)\vec{a}+u\vec{b}+v\vec{c}

Triangle vertices: 

\vec{a}, \vec{b}, \vec{c}\in\mathbb{R}^3
u,v\in\mathbb{R}

Barycentric coordinates

\vec{a}
\vec{b}
\vec{c}

Substitute ray equation into the triangle equation and solve it for 

\vec{o}+t\vec{d}=(1-u-v)\vec{a}+u\vec{b}+v\vec{c}=0

Analytical Approach

t
\begin{bmatrix} -\vec{d} & \vec{b}-\vec{a} & \vec{c}-\vec{a} \end{bmatrix} \begin{pmatrix} t \\ u \\ v \end{pmatrix} = \vec{o}-\vec{a}
\vec{e_1}
\vec{e_2}
\begin{bmatrix} -\vec{d} & \vec{e_1} & \vec{e_2} \end{bmatrix} \begin{pmatrix} t \\ u \\ v \end{pmatrix} = \vec{T}

Point on the triangle: 

\vec{p}\in\mathbb{R}^3
\vec{p}(u,v)
\vec{o}
\vec{T}
t

Ray-Triangle Intersection

Möller-Trumbore Algorithm

\begin{bmatrix} -\vec{d} & \vec{e_1} & \vec{e_2} \end{bmatrix} \begin{pmatrix} t \\ u \\ v \end{pmatrix} = \vec{T}

Using the Cramer’s Rules for Systems of Linear Equations with Three Variables:

\begin{pmatrix} t \\ u \\ v \end{pmatrix} = \frac{1}{ \begin{vmatrix} -\vec{d} & \vec{e_1} & \vec{e_2} \end{vmatrix} } \begin{pmatrix} \begin{vmatrix} \vec{T} & \vec{e_1} & \vec{e_2} \end{vmatrix} \\ \begin{vmatrix} -\vec{d} & \vec{T} & \vec{e_2} \end{vmatrix} \\ \begin{vmatrix} -\vec{d} & \vec{e_1} & \vec{T} \end{vmatrix} \end{pmatrix}
\begin{vmatrix} -\vec{d} & \vec{e_1} & \vec{e_2} \end{vmatrix} = \begin{vmatrix} -d_x & e_{1x} & e_{2x} \\ -d_y & e_{1y} & e_{2y} \\ -d_z & e_{1z} & e_{2z} \end{vmatrix} = \begin{vmatrix} -d_x & -d_y & -d_z \\ e_{1x} & e_{1y} & e_{1z} \\ e_{2x} & e_{2y} & e_{2z} \end{vmatrix} =-\vec{d}\cdot\left(\vec{e_1}\times\vec{e_2}\right) =\vec{d}\cdot\left(\vec{e_2}\times\vec{e_1}\right) =
=\left(\vec{d}\times\vec{e_2}\right)\cdot\vec{e_1}

determinant:

Ray-Triangle Intersection

Möller-Trumbore Algorithm

\begin{bmatrix} -\vec{d} & \vec{e_1} & \vec{e_2} \end{bmatrix} \begin{pmatrix} t \\ u \\ v \end{pmatrix} = \vec{T}

Using the Cramer’s Rules for Systems of Linear Equations with Three Variables:

\begin{pmatrix} t \\ u \\ v \end{pmatrix} = \frac{1}{ \begin{vmatrix} -\vec{d} & \vec{e_1} & \vec{e_2} \end{vmatrix} } \begin{pmatrix} \begin{vmatrix} \vec{T} & \vec{e_1} & \vec{e_2} \end{vmatrix} \\ \begin{vmatrix} -\vec{d} & \vec{T} & \vec{e_2} \end{vmatrix} \\ \begin{vmatrix} -\vec{d} & \vec{e_1} & \vec{T} \end{vmatrix} \end{pmatrix}
\begin{pmatrix} t \\ u \\ v \end{pmatrix} = \frac{1}{ \left(\vec{d}\times\vec{e_2}\right)\cdot\vec{e_1} } \begin{pmatrix} (\vec{T}\times\vec{e_1})\cdot\vec{e_2} \\ (\vec{d}\times\vec{e_2})\cdot\vec{T} \\ (\vec{T}\times\vec{e_1})\cdot\vec{d} \end{pmatrix}
= \frac{1}{ \vec{P}\cdot\vec{e_1} } \begin{pmatrix} \vec{Q}\cdot\vec{e_2} \\ \vec{P}\cdot\vec{T} \\ \vec{Q}\cdot\vec{d} \end{pmatrix}

Ray-Triangle Intersection

Möller-Trumbore Algorithm

bool rt::CPrimTriangle::intersect(Ray& ray) const
{
    const Vec3f pvec = ray.dir.cross(m_edge2);
    const float det = m_edge1.dot(pvec);
    if (fabs(det) < Epsilon)
        return false;

    const float inv_det = 1.0f / det;
    const Vec3f tvec = ray.org - m_a;
    float u = tvec.dot(pvec);
    u *= inv_det;
    if (u < 0.0f || u > 1.0f)
        return false;

    const Vec3f qvec = tvec.cross(m_edge1);
    float v = ray.dir.dot(qvec);
    v *= inv_det;
    if (v < 0.0f || v + u > 1.0f)
        return false;

    float t = m_edge2.dot(qvec);
    t *= inv_det;
    if (ray.t <= t || t < Epsilon)
        return false;

    ray.t = t;
    return true;
}
\begin{pmatrix} t \\ u \\ v \end{pmatrix} = \frac{1}{ \vec{P}\cdot\vec{e_1} } \begin{pmatrix} \vec{Q}\cdot\vec{e_2} \\ \vec{P}\cdot\vec{T} \\ \vec{Q}\cdot\vec{d} \end{pmatrix}
\vec{P} = \vec{d}\times\vec{e2}
\vec{T} = \vec{o}-\vec{a}
\vec{Q} = \vec{T}\times\vec{e1}

where

Ray-Triangle Intersection

Möller-Trumbore Algorithm

Geometrical Interpretation

\vec{a}
\vec{b}
\vec{c}
\vec{o}
\vec{d}
\begin{bmatrix} -\vec{d} & \vec{b}-\vec{a} & \vec{c}-\vec{a} \end{bmatrix} \begin{pmatrix} t \\ u \\ v \end{pmatrix} = \vec{o}-\vec{a}

Ray-Triangle Intersection

Möller-Trumbore Algorithm

Geometrical Interpretation

\vec{b}-\vec{a}
\vec{c}-\vec{a}
\vec{o}-\vec{a}
\vec{d}
\begin{bmatrix} -\vec{d} & \vec{b}-\vec{a} & \vec{c}-\vec{a} \end{bmatrix} \begin{pmatrix} t \\ u \\ v \end{pmatrix} = \vec{o}-\vec{a}

Translation

\vec{T}=\vec{o}-\vec{a}

Ray-Triangle Intersection

Möller-Trumbore Algorithm

Geometrical Interpretation

1
1
M^{-1}(\vec{o}-\vec{a})
\vec{d}
\begin{bmatrix} -\vec{d} & \vec{b}-\vec{a} & \vec{c}-\vec{a} \end{bmatrix} \begin{pmatrix} t \\ u \\ v \end{pmatrix} = \vec{o}-\vec{a}

Translation

\vec{T}=\vec{o}-\vec{a}

Change of base of the ray origin 

M= \begin{bmatrix} -\vec{d} & \vec{b}-\vec{a} & \vec{c}-\vec{a} \end{bmatrix}
u
v

Ray-Box Intersection

Ray Parametrization

Axis-Aligned Bounding Box (aabb)

\vec{p}(t)=\vec{o}+t\vec{d}
\vec{p}_{min},\vec{p}_{max}\in\mathbb{R}^3

Points bounding the box volume: 

\vec{p}_{min}
\vec{p}_{max}

Bounded Volume

Ray-Box Intersection

A box is the intersection of 3 pairs of slabs in 3 dimensions

  • Each slab contains two parallel planes and the space between them

We can detect the intersection between the ray and the box by detecting the intersections between the ray and the three pairs of slabs

  • For each pair of slabs there are two intersection points tnear and tfar, and there are 6 intersection points in total for 3 pairs
  • If the maximal tnear is ahead of the minimal tfar on the ray, the ray misses the box. Otherwise it hits the box.
\vec{p}_{min}

Bounded Volume

\vec{p}_{min}

Bounded Volume

\vec{o}
t^{near}_{y}
t^{far}_{y}
t^{near}_{x}
t^{far}_{x}
t^{near}_{y}
t^{far}_{y}
t^{near}_{x}
t^{far}_{x}
\vec{o}
\max_{i\in\{x,y,z\}}t^{near}_{i} < \min_{i\in\{x,y,z\}}t^{far}_{i}

Ray-Box Intersection

\vec{p}_{min}

Bounded Volume

\vec{p}_{min}

Bounded Volume

\vec{o}
t^{near}_{y}
t^{far}_{y}
t^{near}_{x}
t^{far}_{x}
t^{near}_{y}
t^{far}_{y}
t^{near}_{x}
t^{far}_{x}
\vec{o}
\max_{i\in\{x,y,z\}}t^{near}_{i} < \min_{i\in\{x,y,z\}}t^{far}_{i}
bool intersect(Ray& ray) {
    calculate tnear.x , tfar.x , tnear.y , tfar.y , tnear.z , tfar.z on 3 axes;
    tnear = max(tnear.x , tnear.y , tnear.z);
    tfar  = min(tfar.x , tfar.y , tfar.z);
    if (tnear < tfar && tnear < ray.t) {
        ray.t = tnear;    // report intersection at tnear
        return true;
    }
    return false;         // no intersection
}
Made with Slides.com