To celebrate the fact that the term is over and that I now have roughly a month to work uninterrupted on my own project, I've decided to work on a problem that is somewhat related - finding an algorithm to detect whether or not two 3D oriented boxes are intersecting each other
Given Information
The algorithm is given as input two oriented boxes defined as:
A:
position; a
edge-vectors; q, r, s
B:
position, b
edge-vectors: t, u, v
Where:
- q, r, s are perpendicular to each other and t, u, v are perpendicular to each other.
- a + q + r + s is the corner of A opposite to a and b + t + u + v is the corner of B opposite to b.
The Unkown
What is an efficient way to tell if two such boxes, A and B intersect?
The Plan
By definition, it is known that two sets intersect if they both contain at least one common element (or point in this case).
Linear separability, which is the ability to separate two sets of points with a linear object like a line or plane, will prove useful here.
With these types of geometrical problems, its always best to try and see if there is a 1 or 2 dimensional equivalent. If an algorithm can be found for those, then it may possible to generalize the solution and come up with the required 3-dimensional solution.
The Attempt
One Dimension
This problem degenerates to that of determining whether or not two line segments intersect. As a consequence of the definition of intersection, its obvious that two line segments intersect if an endpoint of one is within the bounds of the other. Visually, if they do not intersect, then there will be a gap between them.
Therefore, boxes A and B intersect <=> (b <= a <= b + t OR b <= a + q <= b + t OR a <= b <= a + q OR a <= b + t <= a + q).
Two Dimensions
In two dimensions, this becomes more complicated. With an axis aligned box, its obvious that two boxes intersect <=> there is a 1-dimensional intersection on both the x and y axes. The boolean formula would be:
(bx <= ax <= bx + t OR bx <= ax + q <= bx + t OR ax <= bx <= ax + q OR ax <= bx + t <= ax + q) AND
(by <= ay <= by + u OR by <= ay + r <= by + u OR ay <= by <= ay + r OR ay <= by + u <= ay + r)
The above expression is illustrated in the following diagram; while A and B intersect on the x-axis, they do not intersect on the y-axis.
Extending this to the case where the sides of individual boxes are still perpendicular, but where the two boxes are not necessarily aligned to the same axes makes this a bit more tricky. After experimenting with many different pairs of As and Bs, with the intuition that perhaps a version of the axis-aligned approach, modified to use the rotated axes, can be used here; I came up with the following hypothesis:
Two oriented boxes A and B do not intersect iff the projections of A and B on some axis do not intersect.
I know how to prove that this is true in one direction - that two oriented boxes A and B do not intersect if the projections of A and B on some axis do not intersect. Unfortunately, I'm not sure how to formally prove this in the other direction, though I speculate I would need to use the fact that A and B are convex shapes and a theorem relating convex shapes to linear separability.
Proof
Assume A and B are oriented boxes and that the projections of A and B do not intersect on some axis.
Then A and B are linearly separable (i.e., there is some line or plane that separates A and B, perpendicular to that axis).
Then since A and B are linearly separable, A and B do not intersect.
In all tests where A and B did not intersect, it was noted that there was no intersection on at least one of the axes of q, r, t or u. Thus:
Two oriented boxes A and B do not intersect iff the projections of A and B do not intersect on q, r, t or u.
While I haven't been able to produce a proof of this hypothesis, I'll attempt to justify it by saying that the axis-aligned expression is just a special case of this in which the axes of q and t are the same and where the axes of r and u are the same
Thus, I've come up with an algorithm for determining if two boxes in 2D intersect: see if the projections of the boxes onto the axes of q, r, t and u all intersect using the 1D intersection test. If on one of those axes, the boxes are found to be disjoint, then the boxes must be disjoint.
In the following case, A and B do not intersect because their projections are disjoint on an axis of B.
Extending to 3D
I found that extending my 2D algorithm to 3 dimensions, by adding in 1-dimensional intersect tests for the axes defined by s and v, worked on all test cases I tried. Moreover, I devised an alternative way of formulating the 3D algorithm based on the 2D algorithm. Consider the following hypothesis, which is another logical way of extending the 2D algorithm to 3 dimensions:
Two oriented boxes A and B do not intersect iff the projections of A and B onto some plane (particularly, some plane coplanar to a face of A or B) do not intersect.
Observe how the faces of A and B are defined; for example, one face on A has the edge vectors q and r. Thus, in order to test the intersection on that face, the 1D intersection test is performed for q and r. Therefore, with all the faces of A and B, this involves performing the same algorithm as above.
More formally, the algorithm is:
1. For each vector, w, in {q, r, s, t, u, v}:
1a) Project the corners of A and B onto w.
1b) Since the projections are all 1-dimensional values, determine the largest value for the projection of A and the smallest value for the projection of A. Do likewise for B.
1c) Apply the 1-dimensional intersection test using the values from 1b.
1d) If the test fails (no intersection) then A and B do not intersect.
2. If the 1-dimensional tests never failed, then A and B intersect.
Extending the result
I'm pretty sure that my hypothesis leads to the following general result:
If two shapes intersect, then their projections on every axis intersect.
The converse, however, isn't true because of the following case involving a concave shape:
An Interesting Personal Note
When I was looking into machine learning last summer; I came across the above mentioned concept called linear separability in an article about Support Vector Machines. Now that I think about it, I suppose I've shown that two oriented boxes that don't intersect are linearly separable and that the line separating them is parallel to an edge of one of the boxes. I suppose the above hypothesis involving an equivalence fails only when the two shapes in question are not linearly separable since it should still be possible to find some curve or surface that separates any non-intersecting shapes.