One use of AABBs in computer graphics is to approximate the shape of an object. The AABB is used to perfom fast approximated tests such as testing intersections and finding if an object is inside the view frustum. As objects are transformed (scaled, rotated and translated), we are interested in calculating the AABB of a transformed AABB.
Two computer representations of AABBs are usually used:
- Minimum and maximum coordinates.
- Center and half extents.
We are going to derive formulas to calculate the AABB of a transformed AABB for each representation, along with implementation details.
AABB as minimum and maximum coordinates
Given an AABB with minimum and maximum coordinates:
and an affine transformation matrix:
we want to transform the AABB by and calculate a new AABB.
An obvious algorithm to calculate the new AABB is to transform each corner of the original AABB and then calculate, for each coordinate, the minimum and maximum values. The eight corners of the original AABB are:
The transformed corners are:
The new minimum and maximum coordinates are:
- Note that the same arguments are used for a minimum operation and for a
maximum operation, so:
- The arguments need to be calculated only once.
- The minimum and maximum operations can be calculated at once with only one comparison.
- Can be implemented with 18 multiplications, 18 additions and 9 comparisons.
AABB as center and half extents
Given an AABB with center and half extents , and an affine transformation matrix , we want to transform the AABB by and calculate a new AABB.
The new center is calculated by applying the translation of the matrix :
An obvious algorithm to calculate the new half extents is to transform each corner of the AABB and then calculate, for each coordinate, the maximum of the absolute values. The original eight corners of the AABB are:
Or expressed more concisely:
We will transform the corners and see what we can do:
The minimum and maximum values for each coordinate are:
So we can take either the maximum coordinate or the absolute of the minimum coordinate to calculate the new half extents:
- Can be implemented with 9 multiplications, 9 additions and 9 absolute value operations.
In general, objects are not symmetric, so AABBs represented as minimum and maximum coordinates provide better shape approximation. This is the computer representation usually used because the better approximation pays off even if the implemenation needs more operations.