I am currently trying to solve a problem to place boxes of various sizes (all cubes or rectangular prisms) within a defined volume.
In the application I am working on, a user defines a length, width, and height of a volume, as well as the length, width, and height of n boxes to place within the volume. The output will be a 3D visualization of all the boxes in space within the volume. The placement of these objects does not need to be optimal, nor do I care if boxes are touching or not, just as long as they exist in the space without overlapping.
My original approach to solve this problem was as follows:
for every box
{
place box at (0,0,0);
while True;
{
for every box that has already been placed
{
if the boxes overlap
{
create a vector pointing in the opposite direction of the other box;
move the box along the vector some dist. X, unless at the border of the available volume;
}
}
if the box didn't overlap with any other box
{
break out of while loop and continue to the next box;
}
}
}
This mostly worked. The main issue with this approach is time. When running this, the first 3 boxes take about 10 seconds, the following 3 get placed after 1:30, and the next 3 take over 10 minutes to finish. For larger assemblies, this is completely unusable. Additionally, this approach has a major flaw in that a box can get stuck between other boxes and the sides of the volume because the resulting vector will point outside of the space.
Are there any other more efficient approaches to solve similar problems that I could adapt to this situation? Again, the goal of this is not to find a perfect space optimization or to fill the entire space.
I am working with CAD, so the information available to me is anything I can access with the API, such as the global coordinates of the vertices of all of the boxes and the coordinates of the vertices of the available volume.