How to calculate the Bounding Sphere for selected objects

Most often, for quick simplified calculations with the object’s geometry, their Bounding Boxes are used – the minimum parallelepiped into which this object is inscribed. But sometimes the Bounding Sphere – the minimum sphere into which an object can be inscribed – can provide greater accuracy and simplify the calculations. While the location and size of the object’s Bounding Box is available in Blender at once, the Bounding Sphere we need to calculate manually

Let’s write a function that, based on the object list, returns the coordinates of the center of their Bounding Sphere and its radius.

Let’s define a function with two parameters:

objects – an object or an objects list, around which the Bounding Sphere needs to be calculated

mode – how the Bounding Sphere will be calculated: “GEOMETRY” – the exact method, based on the coordinates of each object vertex, or “BBOX” – simplified calculation, which used only 8 points from the already known for each object Bounding Box. This will give less accuracy (the sphere will be larger in diameter), but it will not lose performance when calculating for high poly objects.

Assume that through the “objects” parameter we can pass both a separate object and a list of objects.

Define a list of points coordinates which we will use in calculations (in the global coordinate system):

For a precise calculation, we will append the coordinates of all vertices of the object to it.

To calculate using the Bounding Box, it is enough to append the only eight coordinates of the Bounding Box parallelepiped to this list.

Having a list of coordinates of all the necessary points, we can find the central point for all of them – this will be the center of the required Bounding Sphere.

Let’s split the coordinates into three lists – separately coordinates projection to the X-axis, to the Y-axis, and to the Z-axis. It is always easier and faster to calculate coordinates in their projections than in 3D space.

Let’s define a function that will return the center point for two points from the list – with the maximum and minimum coordinates.

Now we can get the coordinates of the center point in all three projections. These will be the coordinates of the center of our Bounding Sphere.

Having the coordinates of the center and a list of all points, we can find the coordinates of the point that is most distant from the center. The distance between the center and the found point will be the radius for the Bounding Sphere.

Now we can return the parameters of the calculated Bounding Sphere.

The final code:

Nikita

Author: Nikita

Blender add-ons developer and articles writer.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comment
Inline Feedbacks
View all comments