# Fast (!) and correct Frustum - AABB Intersection

## Recommended Posts

Hello, I needed an intersection test of the view frustum vs an axis aligned bounding box (AABB). Of course I could check all 8 box vertices against all 6 planes of the frustum, but I found a faster algorithm on this website, which uses a p-vertex and n-vertex: Link (section Geometric Approach - Testing Boxes II) To implement this algorithm I just used this code:
// Returns: INTERSECT : 0
//          INSIDE : 1
//          OUTSIDE : 2
int FrustumAABBIntersect(Plane *planes, Vector &mins, Vector &maxs) {
int    ret = INSIDE;
Vector vmin, vmax;

for(int i = 0; i < 6; ++i) {
// X axis
if(planes[i].normal.x > 0) {
vmin.x = mins.x;
vmax.x = maxs.x;
} else {
vmin.x = maxs.x;
vmax.x = mins.x;
}
// Y axis
if(planes[i].normal.y > 0) {
vmin.y = mins.y;
vmax.y = maxs.y;
} else {
vmin.y = maxs.y;
vmax.y = mins.y;
}
// Z axis
if(planes[i].normal.z > 0) {
vmin.z = mins.z;
vmax.z = maxs.z;
} else {
vmin.z = maxs.z;
vmax.z = mins.z;
}
if(Vector::DotProduct(planes[i].normal, vmin) + planes[i].d > 0)
return OUTSIDE;
if(Vector::DotProduct(planes[i].normal, vmax) + planes[i].d >= 0)
ret = INTERSECT;
}
return ret;
}


I assume that this algorithm is correct. But this is the problem: The frustum is described by 6 planes, where each plane has an infinite extentsion in all 3 dimensions. The real frustum actually is not composed of planes, rather it is composed of 6 rectangles (each with a finite extension). So the problem that I have is that the algorithm doesnt check if the AABB intersects the finite frustum; it checks for intersections with the 6 infinite planes. (Because I don't know if I explained the problem quite well, I will give an example: Imagine you have a far plane of 1000 units and a box which goes from 900 to 1100 units on the z-axis. The x coordinates of the box are huge, so the box is placed at a huge distance on the right side of the frustum and does NOT intersect the real frustum. But the box DOES intersect the far plane, so the algorithm will deliver an intersection). My question: Is there a (relatively) simple way to modify this algorithm (or use an other algorithm) to get only the REAL intersections with the frustum (and not with the infinite planes)?

##### Share on other sites
Quote:
 Original post by schupfBut this is the problem: The frustum is described by 6 planes, where each plane has an infinite extentsion in all 3 dimensions. The real frustum actually is not composed of planes, rather it is composed of 6 rectangles (each with a finite extension). So the problem that I have is that the algorithm doesnt check if the AABB intersects the finite frustum; it checks for intersections with the 6 infinite planes.

It is a normal property of 3D planes that they extend to infinity. A plane can be seen to divide the 3D space into a positive and a negative half-space, where the positive half space is the side of the space towards which the normal of the plane points to.

Now, define a frustum to be the intersection of the negative half-spaces of 6 planes left,right,up,down, near and far. (this is how we usually define arbitrary-shaped convex polyhedrons in 3D)

The code you pasted works by checking one plane at a time, whether the AABB is in the positive half-space of the plane (if so, no intersection, AABB lies outside), or whether the AABB intersects the plane (if so, return intersection). If the AABB doesn't lie in the positive half-space of ANY of the 6 planes, then it is completely in the negative half-spaces of the planes and hence inside the frustum.

The optimization that is being done in that code is that instead of checking all the 8 corner points of the AABB, we can skip some computations by the observation that it is easy to determine which of the corner vertices lie in the "most negative and positive directions" of the plane just by looking at the plane normal.

The code looks fine to me. Perhaps the problem lies in how you extract the planes of the frustum? The code uses the assumption that the normals point outwards from the frustum volume.

##### Share on other sites
@clb: You did not understand my problem;)
Just forget my question, I have just found the solution (shame on me that I did not see it from the beginning). I just have to skip the early out and always loop through all 6 planes. If one plane test resulted in OUTSIDe then it is outside, else its an intersection.

Anyway, I have an another question regarding the linked paper: click
In the section "Geometric Approach - Extracting the Planes" they show how to extract the 6 frustum planes in a geometrical way, by calculating the normal vector and a point on the plane. For the right plane their formula is this:
a = (nc + right * Wnear / 2) - p
(this is visualized by this picture:

They want to calculate the vector of the right point (a point on the right plane). The term in the parentheses is clear to me: nc + right * Wnear / 2, nc is the center point of the near plane, right is a unit vector to the right, Wnear/2 the half width of the near plane. But why do they substract p?? (p = eyepoint of camera). I mean they calculate a (vector from p to point on the right plane) and nc + right * Wnear / 2 already gives what they want. Anyone knows why they subtract p?

##### Share on other sites
Quote:
 Original post by schupfHello,I needed an intersection test of the view frustum vs an axis aligned bounding box (AABB). Of course I could check all 8 box vertices against all 6 planes of the frustum, but I found a faster algorithm on this website, which uses a p-vertex and n-vertex: Link (section Geometric Approach - Testing Boxes II)To implement this algorithm I just used this code:*** Source Snippet Removed ***I assume that this algorithm is correct.But this is the problem: The frustum is described by 6 planes, where each plane has an infinite extentsion in all 3 dimensions. The real frustum actually is not composed of planes, rather it is composed of 6 rectangles (each with a finite extension). So the problem that I have is that the algorithm doesnt check if the AABB intersects the finite frustum; it checks for intersections with the 6 infinite planes. (Because I don't know if I explained the problem quite well, I will give an example: Imagine you have a far plane of 1000 units and a box which goes from 900 to 1100 units on the z-axis. The x coordinates of the box are huge, so the box is placed at a huge distance on the right side of the frustum and does NOT intersect the real frustum. But the box DOES intersect the far plane, so the algorithm will deliver an intersection).My question: Is there a (relatively) simple way to modify this algorithm (or use an other algorithm) to get only the REAL intersections with the frustum (and not with the infinite planes)?
The AABB frustum culling algorithm you describe (which is fairly standard) is conservative; it favors efficiency at the expense of the occasional false positive. It's possible to perform an exact test, but it's unlikely to be worth the extra development effort or run-time cost (under normal circumstances, at least).

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628277
• Total Posts
2981776

• 10
• 11
• 17
• 11
• 9