# How do you compute the smallest bounding box

Given the attached mesh, how does one compute the smallest bounding box? The mesh has no rotation.
The white area represents the current bounding box.

Thatâs a simple question regarding a non-trivial problem. Which is, btw, subject of ongoing academic research. Search âmultivariate optimisationâ for more.

I could have wrote a complex question but I prefer to keep it light. I can conclude there is no simple way atm. Thanks for your reply.

There was no offence intended re. your question. It is perfectly adequate to bring across what you are looking for. Itâs just, that you happened to tap into a very deep pit âŚ

Thanks for this. Yes it isnât a trivial problem. But as Iâm new to threejs I was wondering if it had something for it out of the box. Although as an alternative I was wondering if trying something such as aligning the âlongest partâ of a mesh to an axis before measuring the boundingbox wouldnât do better (which I have no idea how to do it yet).

three.js can compute axis-aligned bounding boxes and object-oriented bounding boxes. But if (as it sounds) this mesh doesnât have any rotation â just the vertex data happens to have the shape of a rotated box â I donât think thereâs anything built in that would fit the box more tightly for you. If you could settle for a ConvexHull instead of a box, thatâs an option.

1 Like

Letâs assume, youâre interested in the smallest (by volume) bounding box. Which depends on the rotational state of your mesh, that is: r.x, r.y, r.z, assuming itâs the mesh youâre rotating. Because Three.js can give you axis-aligned bounding boxes only.

To give you a little idea how this problem could be approached: a genetic algorithm, which is imo nothing more than a fancy word for a computerised trial&error method. Itâs an iterative process, which involves the repeated introduction of âsmallâ changes in one or more of the result-influencing parameters, in your case: r.x, r.y, r.z.

It goes like this:

âPopulation initialisationâ: your first mesh orientation which you showed us.
âFitness calculationâ: compute the bounding box volume for this set of parameters r.x, r.y, r.z .
âStop criteriaâ: is it good enough? If so, youâre done.
If not good enough,
âSelectâ the one parameter set which you currently have.
âCrossover and Mutationâ: change one parameter of this set by a small amount, i.e. `r.x += đ`

Recompute bounding box volume.

If it has improved (become smaller than the previous one) , continue along the same direction of the last change, that is: keep increasing r.x , until you arrive at the 1st increase in bounding box volume.

If your 1st change resulted in a deterioration already, reverse the direction of your change: `r.x -= đ`
and proceed as before.

Note, that you may get trapped in a local minimum, which may be worse than the global minimum: