Slab method

From HandWiki
Short description: Ray-box intersection method

In computer graphics, the slab method is an algorithm used to solve the ray-box intersection problem in case of an axis-aligned bounding box (AABB), i.e. to determine the intersection points between a ray and the box. Due to its efficient nature, that can allow for a branch-free implementation, it is widely used in computer graphics applications.[1][2]

Algorithm

The idea behind the algorithm is to clip the ray with the planes containing the six faces of the box. Each pair of parallel planes defines a slab, and the volume contained in the box is the intersection of the three slabs. Therefore, the portion of ray within the box (if any, given that the ray effectively intersects the box) will be given by the intersection of the portions of ray within each of the three slabs.[3]

A tridimensional AABB can be represented by two triples [math]\displaystyle{ \boldsymbol l = (l_0, l_1, l_2) }[/math] and [math]\displaystyle{ \boldsymbol h = (h_0, h_1, h_2) }[/math] denoting the low and high bounds of the box along each dimension. A point [math]\displaystyle{ \boldsymbol p(t) = (p_0(t), p_1(t), p_2(t)) }[/math] along a ray with origin [math]\displaystyle{ \boldsymbol o = (o_0, o_1, o_2) }[/math] and direction [math]\displaystyle{ \boldsymbol r = (r_0, r_1, r_2) }[/math] can be expressed in parametric form as

[math]\displaystyle{ \boldsymbol p(t) = \boldsymbol o + t \boldsymbol r }[/math].

Assuming that all intersections exist, i.e. [math]\displaystyle{ r_i \ne 0 \; \forall i }[/math], solving for [math]\displaystyle{ t }[/math] gives

[math]\displaystyle{ t = \frac{\boldsymbol p - \boldsymbol o}{\boldsymbol r} }[/math]

and therefore the two intersections of the ray with the two planes orthogonal to the [math]\displaystyle{ i }[/math]-th coordinate axis will be given by

[math]\displaystyle{ \begin{align} t_i^{\text{low}} &= \frac{l_i - o_i}{r_i} \\ t_i^{\text{high}} &= \frac{h_i - o_i}{r_i} . \end{align} }[/math]

The close and far extrema of the segment within the [math]\displaystyle{ i }[/math]-th slab will be given by

[math]\displaystyle{ \begin{align} t_i^{\text{close}} &= \min \left\{ t_i^{\text{low}}, t_i^{\text{high}} \right\} \\ t_i^{\text{far}} &= \max \left\{ t_i^{\text{low}}, t_i^{\text{high}} \right\} \end{align} }[/math]

and the intersection of all these segments is

[math]\displaystyle{ \begin{align} t^{\text{close}} &= \max_i \left\{ t_i^{\text{close}} \right\} \\ t^{\text{far}} &= \min_i \left\{ t_i^{\text{far}} \right\} . \end{align} }[/math]

Such resulting segment will be inside the box, and therefore an intersection exists, only if [math]\displaystyle{ t^{\text{close}} \le t^{\text{far}} }[/math].[4] The sign of [math]\displaystyle{ t^{\text{close}} }[/math] determines if the intersection happens ahead or behind the origin of the ray, which might be interesting in applications such as ray casting, where only intersections in front of the camera are of interest. The two intersection points will therefore be given by

[math]\displaystyle{ \begin{align} \boldsymbol p^{\text{close}} &= \boldsymbol o + t^{\text{close}} \boldsymbol r \\ \boldsymbol p^{\text{far}} &= \boldsymbol o + t^{\text{far}} \boldsymbol r . \end{align} }[/math]

While the equations above are well defined for real-valued variables only if [math]\displaystyle{ r_i \ne 0 \; \forall i }[/math], i.e. if the ray is not parallel to any of the coordinate axes, the algorithm can be applied to an extended real number arithmetic (such as the one implemented by IEEE 754) to handle rays parallel to an axis, as long as the origin of the ray itself does not lie on one of the faces of the bounding box. In such arithmetic, the intersections with the planes parallel to the ray will be given by [math]\displaystyle{ t = +\infty }[/math] or [math]\displaystyle{ t = -\infty }[/math], and the algorithm will still work as expected. If the origin lies on a face of the bounding box, then for some [math]\displaystyle{ i }[/math] it will happen that [math]\displaystyle{ t_i = \frac{0}{0} }[/math], which is undefined (in IEEE 754 it is represented by NaN). However, implementations of the IEEE 754-2008 minNum and maxNum functions[5] will treat NaN as a missing value, and when comparing a well-defined value with a NaN they will always return the well-defined value,[6] and they will therefore be able to handle even such corner case. An alternative approach to handle corner cases is to avoid divisions by zero altogether, which can be achieved by replacing the inverse of zero with a large arbitrary constant number.[4]

References

Sources

  • IEEE Standards Committee (2008). IEEE standard for floating-point arithmetic: 754-2008. Washington, DC: IEEE Computer Society. 
  • Kay, Timothy L.; Kajiya, James T. (1986). "Ray tracing complex scenes". ACM SIGGRAPH computer graphics. 20. Dallas. pp. 269–278. 
  • Majercik, Alexander; Crassin, Cyril; Shirley, Peter; McGuire, Morgan (2018). "A Ray-Box Intersection Algorithm and Efficient Dynamic Voxel Rendering". Journal of Computer Graphics Techniques (JCGT) 7 (3): 66–81. 
  • Shirley, Peter; Wald, Ingo; Marrs, Adam (2021). "Ray Axis-Aligned Bounding Box Intersection". Ray Tracing Gems II. Berkeley, CA: Apress. 

External links