<p>You can assign a coordinate system to each nAABB in such a way that the nAABB becomes an AABB in its own coordinate system. We call this a <em>local coordinate system</em>.</p>

<p>I assume rays are expressed in a <em>world</em> or <em>global coordinate system</em>. In order to test an nAABB for intersection, one first needs to apply the <em>world-to-local transformation</em> on the ray (origin and direction). This way we obtain a transformed ray which we can intersect with an AABB. Once an intersection point is found, we need to transform this back to world space via the inverse transformation (i.e. <em>local-to-world transformation</em>).</p>

<p>In practice an nAABB is thus expressed as an AABB with a pair of world-to-local and local-to-world transformation matrices.</p>

<p>If one applies a scaling ($S$) or translation ($T$) on an AABB, one obtains another AABB. If one applies a rotation ($R$) on an AABB, one obtains a nAABB.
Typically the corresponding transformation matrices are multiplied as follows $T * R * S$. Thus translation is applied after rotation. Since the $R$ component must be included in the world-to-local transformation, you need to include the $T$ component as well. The $S$ component can be included or directly applied to a unit AABB centered at the origin of the world-coordinate system. Including a $T$ component requires your world-to-local and local-to-world transformation matrices to be of size $4$x$4$ (expressed in <em>homogeneous coordinates</em>).</p>

<p><a href="https://github.com/mmp/pbrt-v2/blob/master/src/core/transform.cpp">Here</a> you can find C++ code to construct all the $4$x$4$ world-to-local matrices and their inverses you probably need.</p>

<p>Note that this approach works for other geometrical objects as well (if the transformations involve scaling, rotation and translation).</p>

CC BY-SA 4.0
2018-08-31T22:55:58.493Z
Assuming no scaling, could this be done with translation plus quaternion, eliminating the matrix multiplication?
jjxtra
CC BY-SA 4.0
2018-09-01T06:57:31.863Z
@jjxtra You can use (unit) quaternions to represent rotations, and you can apply quaternion multiplications to apply rotations. You can perform the latter with different operations: with or without a matrix multiplication.
Matthias
CC BY-SA 4.0
2018-09-01T15:37:53.227Z
got it working with just quaternion! Convert the rotated cube back to non-rotated coordinates and get the bounds with origin 0,0,0. The in the shader, translate ray origin by difference from cube center and then rotate raycasts by the cube rotation quaternion. Works great!
jjxtra
CC BY-SA 3.0
2016-03-24T07:59:15.040Z
2016-03-24T08:24:24.030Z
2016-03-24T08:24:24.030Z
Matthias
<p>I am trying to write a ray tracer to render boxes that are arbitrarily rotated, i.e. not necessarily axis aligned. While I am reasonably comfortable ray tracing axis-aligned bounding boxes (AABB), I don't know how to handle non-axis aligned objects. The geometry becomes quite difficult. I've looked through a textbook (<em>Ray Tracing from the Ground Up</em>) but couldn't find an answer.</p>

<p>How should I do it? Detailed suggestions would be greatly appreciated.</p>

CC BY-SA 3.0
2016-03-23T21:30:58.277Z
This question is a cross-post from http://gamedev.stackexchange.com/questions/118744/how-to-convert-non-axis-aligned-bounding-boxes-to-aabb
Newb
<raytracing><geometry>
How to convert Non-Axis Aligned Bounding Boxes to AABB
Newb
# How to convert Non-Axis Aligned Bounding Boxes to AABB

(Asked by: username_0 on 2016-03-23)

I am trying to write a ray tracer to render boxes that are arbitrarily rotated, i.e. not necessarily axis aligned. While I am reasonably comfortable ray tracing axis-aligned bounding boxes (AABB), I don't know how to handle non-axis aligned objects. The geometry becomes quite difficult. I've looked through a textbook (*Ray Tracing from the Ground Up*) but couldn't find an answer.

How should I do it? Detailed suggestions would be greatly appreciated.

**Tags:** raytracing, geometry

> **Comments:**
> **username_0**: This question is a cross-post from http://gamedev.stackexchange.com/questions/118744/how-to-convert-non-axis-aligned-bounding-boxes-to-aabb  

---

## 1 answers

### Answer by username_1 on 2016-03-24. Score: 5 | Accepted answer

You can assign a coordinate system to each nAABB in such a way that the nAABB becomes an AABB in its own coordinate system. We call this a *local coordinate system*.

I assume rays are expressed in a *world* or *global coordinate system*. In order to test an nAABB for intersection, one first needs to apply the *world-to-local transformation* on the ray (origin and direction). This way we obtain a transformed ray which we can intersect with an AABB. Once an intersection point is found, we need to transform this back to world space via the inverse transformation (i.e. *local-to-world transformation*).

In practice an nAABB is thus expressed as an AABB with a pair of world-to-local and local-to-world transformation matrices.

If one applies a scaling ($S$) or translation ($T$) on an AABB, one obtains another AABB. If one applies a rotation ($R$) on an AABB, one obtains a nAABB.
Typically the corresponding transformation matrices are multiplied as follows $T \* R \* S$. Thus translation is applied after rotation. Since the $R$ component must be included in the world-to-local transformation, you need to include the $T$ component as well. The $S$ component can be included or directly applied to a unit AABB centered at the origin of the world-coordinate system. Including a $T$ component requires your world-to-local and local-to-world transformation matrices to be of size $4$x$4$ (expressed in *homogeneous coordinates*).

[Here](https://github.com/mmp/pbrt-v2/blob/master/src/core/transform.cpp) you can find C++ code to construct all the $4$x$4$ world-to-local matrices and their inverses you probably need.

Note that this approach works for other geometrical objects as well (if the transformations involve scaling, rotation and translation).

> **Comments:**
> **username_2**: Assuming no scaling, could this be done with translation plus quaternion, eliminating the matrix multiplication?  
> **username_1**: @username_2 You can use (unit) quaternions to represent rotations, and you can apply quaternion multiplications to apply rotations. You can perform the latter with different operations: with or without a matrix multiplication.  
> **username_2**: got it working with just quaternion! Convert the rotated cube back to non-rotated coordinates and get the bounds with origin 0,0,0. The in the shader, translate ray origin by difference from cube center and then rotate raycasts by the cube rotation quaternion. Works great!  

---

