Ray Casting and Surface Intersections in Computer Graphics

Rendering Foundations and Ray Concepts

  • Rendering Process Overview:

    • Solves the visibility / hidden surface problem by determining what scene elements are visible from a given perspective.
    • Projects 3D scene objects onto a 2D sensor plane.
    • Evaluates light transport and shading models, such as the Phong illumination model and the global Rendering Equation, to calculate color and luminance.
    • Employs ray casting to explicitly compute ray-object intersections.
  • Ray Definition:

    • A ray is mathematically modeled as a half-line defined by an origin point o\mathbf{o} and a normalized direction vector d\mathbf{d}.
    • Expressed in parametric vector form as:     r(t)=o+t d\mathbf{r}(t) = \mathbf{o} + t\,\mathbf{d}     where t≥0t \ge 0 is the ray parameter representing distance along the direction vector.
    • In orthographic projection models, rays are cast parallel to one another from the camera sensor plane.
  • Ray-Scene Intersection Goal:

    • Computes the intersection point p\mathbf{p} on a surface corresponding to a sensor position q\mathbf{q}.
    • Requires evaluating intersections across all scene objects to isolate the nearest surface intersection, defined by the minimal positive distance parameter t≥0t \ge 0.

Ray Casting vs. Ray Tracing

  • Ray Casting:

    • Primary Objective: Solves the fundamental visibility problem ("What object is visible at each sensor pixel?").
    • Primary Rays: Rays originating at or ending at the camera sensor.
    • Determines the exact surface intersection position p\mathbf{p} corresponding to pixel q\mathbf{q}.
  • Ray Tracing:

    • Primary Objective: Computes radiative light transport along primary rays toward the camera ("What exact color and intensity does the pixel receive?").
    • Secondary Rays: Rays generated at surface interaction points that do not directly originate or terminate at the sensor plane.
    • Shadow Rays: Specific secondary rays cast directly toward light sources to evaluate occlusion and direct illumination.
    • Illumination Pathways:
    • Ray 1 & Ray 2: Model direct illumination, traveling from light sources to surface interaction points.
    • Ray 3: Models indirect illumination, capturing light emitted or reflected from one surface point qq toward another surface point pp.
    • Ray 4: Models primary incoming light transported from surface point pp directly into the camera sensor.
    • Computational Challenge: Calculating outgoing light from point qq toward point pp depends recursively on incoming light at qq from point pp and all other surrounding surfaces. The final outgoing radiance along a primary ray is the integral sum of incoming radiance from all incident directions (secondary rays), weighted by local surface material properties (BRDF).

Intersection with Implicit Surfaces

  • Implicit Surface Formulation:

    • Defined implicitly as the zero-level set of a scalar function f(p)f(\mathbf{p}):     f(p)=0f(\mathbf{p}) = 0
    • An intersection with a ray r(t)=o+t d\mathbf{r}(t) = \mathbf{o} + t\,\mathbf{d} occurs at parameter values tt that satisfy:     f(o+t d)=0f(\mathbf{o} + t\,\mathbf{d}) = 0
  • Plane Intersection Example:

    • A plane is defined implicitly by a reference point r\mathbf{r}, a surface point p\mathbf{p}, and a unit surface normal vector n\mathbf{n}:     f(p)=n⋅(p−r)=0f(\mathbf{p}) = \mathbf{n} \cdot (\mathbf{p} - \mathbf{r}) = 0
    • Substituting the ray equation p=o+t d\mathbf{p} = \mathbf{o} + t\,\mathbf{d} yields:     n⋅(o+t d−r)=0\mathbf{n} \cdot (\mathbf{o} + t\,\mathbf{d} - \mathbf{r}) = 0
    • Solving explicitly for the parametric intersection distance tt:     t=n⋅(r−o)n⋅dt = \frac{\mathbf{n} \cdot (\mathbf{r} - \mathbf{o})}{\mathbf{n} \cdot \mathbf{d}}
    • Valid solution condition: A unique intersection exists provided the ray direction is not orthogonal to the surface normal (n⋅d≠0\mathbf{n} \cdot \mathbf{d} \neq 0).
  • Surface Normal Derivation:

    • The normalized vector perpendicular to an implicit surface at point p\mathbf{p} is given by the spatial gradient of the implicit function:     n=∇f(p)=(∂f∂x∂f∂y∂f∂z)\mathbf{n} = \nabla f(\mathbf{p}) = \begin{pmatrix} \frac{\partial f}{\partial x} \\ \frac{\partial f}{\partial y} \\ \frac{\partial f}{\partial z} \end{pmatrix}
    • For a planar implicit function f(p)=n⋅(p−r)f(\mathbf{p}) = \mathbf{n} \cdot (\mathbf{p} - \mathbf{r}), the gradient directly simplifies to the plane normal vector n\mathbf{n}.

Intersection with Quadric Surfaces

  • Quadric Surface Properties:

    • Surfaces represented by general second-degree polynomial quadratic equations.
    • Include geometric primitives such as spheres, ellipsoids, paraboloids, hyperboloids, cones, and cylinders.
    • Intersecting a straight ray with a quadric yields a quadratic equation in parameter tt, resulting in zero, one, or two real intersection solutions.
  • Unit Sphere at Origin:

    • Implicit equation for a unit sphere centered at origin (0,0,0)(0, 0, 0) with radius R=1R = 1:     f(p)=p⋅p−1=x2+y2+z2−1=0f(\mathbf{p}) = \mathbf{p} \cdot \mathbf{p} - 1 = x^2 + y^2 + z^2 - 1 = 0
    • Substituting ray equation r(t)=o+t d\mathbf{r}(t) = \mathbf{o} + t\,\mathbf{d}:     (o+t d)⋅(o+t d)−1=0(\mathbf{o} + t\,\mathbf{d}) \cdot (\mathbf{o} + t\,\mathbf{d}) - 1 = 0(d⋅d) t2+2 (o⋅d) t+(o⋅o−1)=0(\mathbf{d} \cdot \mathbf{d})\,t^2 + 2\,(\mathbf{o} \cdot \mathbf{d})\,t + (\mathbf{o} \cdot \mathbf{o} - 1) = 0
    • Standard quadratic form A t2+B t+C=0A\,t^2 + B\,t + C = 0 coefficients:     A=d⋅dA = \mathbf{d} \cdot \mathbf{d}B=2 (o⋅d)B = 2\,(\mathbf{o} \cdot \mathbf{d})C=o⋅o−1C = \mathbf{o} \cdot \mathbf{o} - 1
    • Surface normal at intersection point p\mathbf{p}:     n=∇f(p)=2 p∝p\mathbf{n} = \nabla f(\mathbf{p}) = 2\,\mathbf{p} \propto \mathbf{p}
  • Discriminant Analysis (B2−4ACB^2 - 4AC):

    • Zero Intersections (B2−4AC<0B^2 - 4AC < 0): Ray misses the sphere entirely; no real solutions exist.
    • One Intersection (B2−4AC=0B^2 - 4AC = 0): Ray is perfectly tangent to the sphere surface at point p3=o+t3 d3\mathbf{p}_3 = \mathbf{o} + t_3\,\mathbf{d}_3 with a single root:     t3=−B2At_3 = \frac{-B}{2A}
    • Two Intersections (B2−4AC>0B^2 - 4AC > 0): Ray pierces the sphere, entering at p1=o+t1 d2\mathbf{p}_1 = \mathbf{o} + t_1\,\mathbf{d}_2 and exiting at p2=o+t2 d2\mathbf{p}_2 = \mathbf{o} + t_2\,\mathbf{d}_2:     t1,2=−B±B2−4AC2At_{1,2} = \frac{-B \pm \sqrt{B^2 - 4AC}}{2A}

Intersection with Parametric Surfaces

  • Parametric Surface Formulation:

    • Defined using continuous 2D parameter pairs (u,v)(u, v) over domain boundaries:     f(u,v)=(x(u,v)y(u,v)z(u,v))\mathbf{f}(u, v) = \begin{pmatrix} x(u, v) \\ y(u, v) \\ z(u, v) \end{pmatrix}
    • Ray intersection requires solving a non-linear system of 3 simultaneous equations with 3 unknown variables (t,u,v)(t, u, v):     o+t d=f(u,v)\mathbf{o} + t\,\mathbf{d} = \mathbf{f}(u, v)
  • Tangent and Normal Vectors:

    • Surface tangent vectors along parameter directions:     tu=∂f∂u\mathbf{t}_u = \frac{\partial \mathbf{f}}{\partial u}tv=∂f∂v\mathbf{t}_v = \frac{\partial \mathbf{f}}{\partial v}
    • Surface normal vector defined by the cross product of parameter tangents:     n=tu×tv=∂f∂u×∂f∂v\mathbf{n} = \mathbf{t}_u \times \mathbf{t}_v = \frac{\partial \mathbf{f}}{\partial u} \times \frac{\partial \mathbf{f}}{\partial v}
  • Specific Parametric Primitives:

    • Cylinder (aligned with z-axis, radius rr, height hh, parameters u∈[0,2π]u \in [0, 2\pi], v∈[0,h]v \in [0, h]):     f(u,v)=(r cos⁡(u)r sin⁡(u)v)\mathbf{f}(u, v) = \begin{pmatrix} r\,\cos(u) \\ r\,\sin(u) \\ v \end{pmatrix}
    • Sphere (centered at origin, radius rr, parameters u∈[0,2π]u \in [0, 2\pi], v∈[−π2,π2]v \in [-\frac{\pi}{2}, \frac{\pi}{2}]):     f(u,v)=(r cos⁡(v) cos⁡(u)r cos⁡(v) sin⁡(u)r sin⁡(v))\mathbf{f}(u, v) = \begin{pmatrix} r\,\cos(v)\,\cos(u) \\ r\,\cos(v)\,\sin(u) \\ r\,\sin(v) \end{pmatrix}
    • Disk (at height hh along z-axis, outer radius rr, inner radius rir_i, parameters u∈[ri,r]u \in [r_i, r], v∈[0,2π]v \in [0, 2\pi]):     f(u,v)=(u cos⁡(v)u sin⁡(v)h)\mathbf{f}(u, v) = \begin{pmatrix} u\,\cos(v) \\ u\,\sin(v) \\ h \end{pmatrix}
    • Cone (base radius rr, height hh, parameters u∈[0,h]u \in [0, h], v∈[0,2π]v \in [0, 2\pi]):     f(u,v)=(h−uh r cos⁡(v)h−uh r sin⁡(v)u)\mathbf{f}(u, v) = \begin{pmatrix} \frac{h - u}{h}\,r\,\cos(v) \\ \frac{h - u}{h}\,r\,\sin(v) \\ u \end{pmatrix}
    • Bounds on parameter domains enable precise rendering of partial, bounded geometric sub-sections.

Compound Objects and Constructive Solid Geometry (CSG)

  • Constructive Solid Geometry Concepts:

    • Combines simple solid primitives (cubes, spheres, cylinders) to build complex volumetric geometries via Boolean operators.
    • Requires strictly closed manifold surfaces and well-defined volumetric interiors.
  • Boolean CSG Operations:

    • Union (A∪BA \cup B): Combines volumes of both AA and BB.
    • Intersection (A∩BA \cap B): Retains only the overlapping volume common to both AA and BB.
    • Difference (B∖AB \setminus A or B−AB - A): Subtracts volume AA from volume BB. For example, subtracting a sphere from a cube creates a hollow spherical boundary inside the cube where sphere intersections are valid only inside the cube bounds, while cube surfaces inside the sphere are discarded.
  • Implementation via Ray-Interval Analysis:

    • Computes all entry and exit intersection parameter intervals along a ray for each primitive object.
    • Union: Selects the overall closest entering intersection point across primitives.
    • Intersection: Selects the closest intersection of AA located inside volume BB, or the closest intersection of BB inside volume AA.
    • Difference (B−AB - A): Evaluates ray intervals to keep intersections on BB outside AA, or surface intersections on AA inside BB with reversed normal directions.

Ray-Triangle Intersections and Barycentric Coordinates

  • Triangle Representation:

    • Triangle meshes form the primary approximate surface representation for complex models (e.g., Stanford bunny).
    • Defined by 3 vertices p0,p1,p2\mathbf{p}_0, \mathbf{p}_1, \mathbf{p}_2 forming a planar face.
  • Barycentric Coordinates:

    • Expresses any point p\mathbf{p} lying within the plane of triangle p0p1p2\mathbf{p}_0\mathbf{p}_1\mathbf{p}_2 as a weighted linear combination:     p(α,β,γ)=α p0+β p1+γ p2\mathbf{p}(\alpha, \beta, \gamma) = \alpha\,\mathbf{p}_0 + \beta\,\mathbf{p}_1 + \gamma\,\mathbf{p}_2
    • Constraint equation:     α+β+γ=1  ⟹  α=1−β−γ\alpha + \beta + \gamma = 1 \implies \alpha = 1 - \beta - \gamma
    • Simplified 2-parameter expression:     p(β,γ)=(1−β−γ) p0+β p1+γ p2\mathbf{p}(\beta, \gamma) = (1 - \beta - \gamma)\,\mathbf{p}_0 + \beta\,\mathbf{p}_1 + \gamma\,\mathbf{p}_2p(β,γ)=p0+β (p1−p0)+γ (p2−p0)\mathbf{p}(\beta, \gamma) = \mathbf{p}_0 + \beta\,(\mathbf{p}_1 - \mathbf{p}_0) + \gamma\,(\mathbf{p}_2 - \mathbf{p}_0)
  • Spatial Domain Properties of Barycentric Coordinates:

    • Vertices: p0  ⟺  (1,0,0)\mathbf{p}_0 \iff (1, 0, 0), p1  ⟺  (0,1,0)\mathbf{p}_1 \iff (0, 1, 0), p2  ⟺  (0,0,1)\mathbf{p}_2 \iff (0, 0, 1).
    • Triangle Edges: One coordinate equals 00, while the remaining two parameters lie within [0,1][0, 1].
    • Interior Point: β≥0\beta \ge 0, γ≥0\gamma \ge 0, and β+γ≤1\beta + \gamma \le 1 (ensuring α≥0\alpha \ge 0).
    • Exterior Point: β<0\beta < 0, γ<0\gamma < 0, or β+γ>1\beta + \gamma > 1.
  • System of Linear Equations for Ray Intersection:

    • Equating the parametric ray equation and triangle surface equation:     o+t d=p0+β (p1−p0)+γ (p2−p0)\mathbf{o} + t\,\mathbf{d} = \mathbf{p}_0 + \beta\,(\mathbf{p}_1 - \mathbf{p}_0) + \gamma\,(\mathbf{p}_2 - \mathbf{p}_0)
    • Rearranging terms into matrix-vector form:     (−dp1−p0p2−p0)(tβγ)=o−p0\begin{pmatrix} -\mathbf{d} & \mathbf{p}_1 - \mathbf{p}_0 & \mathbf{p}_2 - \mathbf{p}_0 \end{pmatrix} \begin{pmatrix} t \\ \beta \\ \gamma \end{pmatrix} = \mathbf{o} - \mathbf{p}_0
  • Direct Solution via Cramer's Rule:

    • Let edge vectors e1=p1−p0\mathbf{e}_1 = \mathbf{p}_1 - \mathbf{p}_0, e2=p2−p0\mathbf{e}_2 = \mathbf{p}_2 - \mathbf{p}_0, and origin vector \mathbf{s} = \mathbf{o} - \mathbf{p}_0$.\n - Solved numerically using scalar triple products (evaluating parallelepiped volumes):\n    \begin{pmatrix} t \ \beta \ \gamma \end{pmatrix} = \frac{1}{(-\mathbf{d}) \cdot (\mathbf{e}1 \times \mathbf{e}_2)} \begin{pmatrix} \mathbf{s} \cdot (\mathbf{e}_1 \times \mathbf{e}_2) \ (-\mathbf{d}) \cdot (\mathbf{s} \times \mathbf{e}_2) \ (-\mathbf{d}) \cdot (\mathbf{e}_1 \times \mathbf{s}) \end{pmatrix}\n\n- **Intersection Criteria**:\n 1. Non-degenerated triangle with ray not parallel to plane: Determinant D = (-\mathbf{d}) \cdot (\mathbf{e}_1 \times \mathbf{e}_2) \neq 0\n 2. Point inside triangle boundary: \beta \ge 0,,\gamma \ge 0,and, and\beta + \gamma \le 1\n 3. Intersection in front of sensor: t \ge 0\n\n\n# Axis-Aligned Bounding Boxes (AABB) and Acceleration Structures\n\n- **Acceleration Structure Motivation**:\n - Encloses complex, high-polygon geometry inside simpler proxy geometries with highly efficient intersection tests.\n - Rays that miss the simplified outer proxy bounding box are immediately pruned, avoiding expensive individual triangle intersection checks.\n\n- **Axis-Aligned Bounding Box Characteristics**:\n - Alignment: Bounding surfaces are strictly aligned with principal coordinate axes (x, y, z).\n - Representation: Represented compactly by one 1D range interval per spatial dimension ([x_{\min}, x_{\max}], [y_{\min}, y_{\max}], [z_{\min}, z_{\max}]).\n - Transformation behavior: Easily translated alongside objects; however, non-translational transformations (such as rotations) require recomputing bounding intervals.\n - Bounding proxy alternatives include Object-Oriented Boxes (OBB), k−DiscreteOrientedPolytopes(-Discrete Oriented Polytopes (k-DOPs), and Bounding Spheres.\n\n- **Slab Intersection Method**:\n - A 3D box is defined by three intersecting orthogonal pairs of parallel planes called "slabs".\n - Ray-plane parameter math per coordinate axis (e.g., x-axis slab):\n    t{x1} = \frac{x_{\min} - o_x}{d_x}\n    t_{x2} = \frac{x_{\max} - o_x}{d_x}\n - Spatial entry and exit parameters for x-slab:\n    t_{x,\min} = \min(t_{x1}, t_{x2})\n    t_{x,\max} = \max(t_{x1}, t_{x2})\n - Overlap condition: A valid ray-box intersection occurs if and only if the maximum entry parameter across all slabs is less than or equal to the minimum exit parameter across all slabs:\n    t_{\text{enter}} = \max(t_{x,\min}, t_{y,\min}, t_{z,\min}) \le t_{\text{exit}} = \min(t_{x,\max}, t_{y,\max}, t_{z,\max})\n - Non-overlapping interval ranges indicate the ray completely misses the bounding box.\n\n- **Bounding Volume Hierarchies (BVH)**:\n - Hierarchical tree structure combining nested AABBs.\n - Structure: Root AABB \rightarrowChildSub−AABBsChild Sub-AABBs\rightarrow Leaf node object primitives.\n - Traversal algorithm: Test ray against parent bounding box; if intersected, recursively traverse child nodes. If missed, prune entire sub-tree.\n - Performance impact: Reduces computational complexity from linear search O(n)tologarithmicsearchto logarithmic searchO(\log n)boxchecksforscenemodelscontainingbox checks for scene models containingn polygon faces.\n - Overhead: Introduces additional memory footprint and preprocessing build times.\n\n\n# Iso-Surfaces in Grids and Volumetric Data\n\n- **Volumetric Surface Motivation**:\n - Enables ray casting of fluid surfaces directly from particle simulations without constructing explicit polygon surface meshes.\n\n- **Density Mapping onto 3D Grid**:\n - Fluid simulation particles carry discrete scalar mass density quantities.\n - Particle density values are mapped and interpolated onto surrounding regular 3D grid cell nodes.\n\n- **Trilinear Grid Cell Interpolation**:\n - Scalar density \rhoinsidealocalgridcelliscalculateddynamicallyatpointinside a local grid cell is calculated dynamically at point\mathbf{p} = (x, y, z) using continuous trilinear interpolation across the 8 surrounding grid corner nodes.\n\n- **Ray-Isosurface Intersection**:\n - Defines a target threshold scalar iso-value \rho_{\text{iso}}.\n - Evaluates ray equation \mathbf{r}(t) = \mathbf{o} + t\,\mathbf{d}.\n - Solves for distance parameter t where interpolated grid cell density matches the iso-value threshold:\n    \rho(\mathbf{r}(t)) = \rho_{\text{iso}}\n    subject to t \ge 0andand\mathbf{r}(t) being contained within cell bounds.\n\n- **Isosurface Intersection Normal**:\n - Surface normal at an iso-surface point is defined by the normalized spatial gradient vector of the scalar density field:\n    \mathbf{n} = \nabla \rho(\mathbf{r}(t))\n - Numerically approximated during ray casting using finite difference schemes across neighboring grid cell values.\n\n\n# Summary and Structural Overview\n\n- **Versatility of Ray Casting**:\n - Acts as a core rendering mechanism to evaluate sensor visibility across diverse geometric representations, including implicit surfaces, quadric volumes, parametric patches, polygon meshes, CSG solids, and grid-based volume iso-surfaces.\n- **Computational Complexity Management**:\n - Unaccelerated intersection evaluation is computationally expensive for high-complexity models.\n - Spatial acceleration structures (such as Bounding Volume Hierarchies utilizing Axis-Aligned Bounding Boxes) reduce scene interaction complexity to O(\log n)$$.
  • Mathematical Spectrum:

    • Standard primitives utilize closed-form linear or quadratic algebraic formulas (planes, triangles, spheres).
    • Volumetric isosurfaces require dynamic spatial evaluations, such as grid interpolation combined with numerical gradient extraction.