I have a set of 3D points, or in fact small spheres, that I need to enclose with the smallest possible 3D box using Unity 3D.
In the case where the enclosing box can only be moved and scaled, the solution is quite trivial, you simply iterate over all the points and encapsulate each one. But I also need to find the best possible orientation for the box.
So, to illustrate the problem in ASCII, given a basic 2D scenario with only two points:
Y
| * (0,1)
|
|
|
| * (1,0)
-------------------- X
Using a regular growing bounding box, you will end up with a very large enclosing box containing mostly blank space, while in this case I need a box that is very thin and rotated about 45 degrees around the Z axis. Basically just a line connecting the two points.
I never know how many points that needs to be grouped together though. And as mentioned, it has to work in 3D.
So far I only tried the basic approach where I do not rotate the encapsulating box for the best fit. The result is really way off what I need.
I was considering a brute force method, based on genetic algorithms, where I generate huge amounts of random boxes and simply select the one with the lowest area while still containing all the points. However, that is too slow.
GameObject go = points[0];
Bounds b = new Bounds(go.transform.position,go.transform.localScale);
for (int i=1;i<points.Count;i++)
{
go = points[i];
b.Encapsulate(new Bounds(go.transform.position, go.transform.localScale));
}
GameObject containingBox = Instantiate(boxPrefab);
containingBox.transform.position = b.center;
containingBox.transform.localScale = b.size;
containingBox.transform.rotation= Quaternion.Identity; //How to calculate?






