Ray Casting
This page describes the mathematical theory behind the ray casting algorithms implemented in Crater.rs. Ray casting has been fundamental to computer graphics since the early work of Appel 6.
Mathematical Foundation
Ray casting is the computational problem of finding a tensor such that , where is the function representing ray extensions:
Here, represents ray origins and represents ray directions.
This library implements multiple algorithms for determining ray intersections with isosurfaces and regions. The algorithms are configured via the RayCastAlgorithm
enum.
Analytical Method
The analytical method for computing ray intersections is a special, but highly performant and precise algorithm. This algorithm computes the intersection point analytically, by making the above 1-D implicit function explicit:
is reformulated as:
Where is a RayField
. RayField
s are similar to ScalarField
s, but take an extra input parameter, a tensor of unit directions . The field returns the rank-1 tensor of distances from to the point at which along the direction .
A valid may not be analytically available. Therefore, the analytical method is not available generally, for all . For common ScalarField
s in this library, the mapping is implemented already.
Concrete Implementations
(Hyper)planes
For a hyperplane with normal vector , we define the following two quantities:
Then, the ray field is:
There are a few degenerate cases:
- If , then the ray is parallel to the hyperplane, and the ray field is undefined.
- If , then the ray origin is on the hyperplane.
- If , then the ray intersection is behind the ray origin.
(Hyper)spheres
For a hypersphere with radius , the primal scalar field is defined as:
For ray intersections, the 1-D implicit function is:
The final equation is a quadratic form over . Using the quadratic equation:
The discriminant is:
The possible solutions for take the form:
There are a few cases:
- If , then the ray does not intersect the sphere.
- If , then the ray intersects the sphere at a single point (tangent).
- If , then the ray intersects the sphere at two points, one or both of which may be behind the ray.
In all cases, we take the smallest non-negative root.
(Hyper)cones
The primal scalar field for a cone with unit-axis and opening angle is defined as:
For ray intersections, the 1-D implicit function is:
The final equation is a quadratic form over . Using the quadratic equation:
The discriminant is:
The possible solutions for take the form:
There are a few cases:
- If , then the ray does not intersect the cone.
- If , then the ray intersects the cone at a single point (tangent).
- If , then the ray intersects the cone at two points, one or both of which may be behind the ray.
In all cases, we take the smallest non-negative root.
(Hyper)cylinders
The primal scalar field for a hypercylinder with radius extending along the last dimension is defined as:
where the summation is over the first dimensions. For ray intersections, we project the problem to the first dimensions:
Let for and for .
The 1-D implicit function becomes:
This reduces to a quadratic form with coefficients:
The solutions follow the same quadratic formula as hyperspheres, taking the smallest non-negative root.
(Hyper)tori
The primal scalar field for a hypertorus with major radius and minor radius is defined as:
For ray intersections, the analytical solution involves solving a quartic equation, which is significantly more complex than quadratic equations. Due to this complexity and numerical stability concerns, analytical solutions are not implemented for hypertori. Alternative approaches like sphere tracing 8 may be more suitable for such complex implicit surfaces.
Bracket and Bisect Algorithm
The ray casting process consists of two main steps:
- Ray Bracketing: Finding intervals along rays that contain potential intersections
- Ray Bisection: Refining these intervals to precisely locate intersection points
Ray Bracketing Algorithm
The ray bracketing algorithm efficiently finds intervals along rays that contain isosurface intersections. This implementation leverages tensor operations for parallel processing of multiple rays.
Algorithm Steps:
- Initialize parameters:
- Tensor field , ray origins , unit directions
- Step size , maximum iterations
- Initialize ray parameters:
- (left bracket)
- (right bracket)
- Iterate until convergence or max iterations:
- Set
- Evaluate field at right bracket:
- Create mask for rays that don't yet have a bracket:
- Update brackets using the mask
- Return brackets such that
Ray Bisection Algorithm
After bracketing finds intervals containing isosurface intersections, the bisection algorithm refines these brackets to precisely locate the intersection points.
Algorithm Steps:
- Initialize with bracketing tensors containing the isosurface
- Iterate until convergence or max iterations:
- Compute midpoint:
- Create masks for which half contains the root:
- Update brackets based on which half contains the root
- Return refined intersection points