Having a little issue calculating the bounding sphere radius

我的未来我决定 提交于 2019-12-11 02:42:41

问题


I have managed to calculate the bounding sphere radius in two ways, but none is giving me exactly what I want. I don't need a "pixel" perfect bounding sphere but I would like something better than what I currently have.

I'm using Wavefront .obj models and to calculate the bounding sphere radius for those models I extract the current model dimensions (I'm using the GLM library from Nate Robbins) which will give me the dimension on each axis.

First approach: Divide each axis by 2 and that will give me the radius on each axis. The largest is the one I'll use for my bounding sphere. This will work for most objects specific to my project. It will not work for some, like cube-shaped ones. Basically, if I have a cube and calculate the radius with this approach, the sphere will leave the cube corners outside.

Second approach: Divide each axis by 2 and that will give me the radius on each axis. Then I do this to take out the radius for the bounding sphere:

r = SQRT(x*x + y*y + z*z)

But this gives me a pretty large radius. The object will be totally enclosed in the sphere but the sphere is pretty large, more than it should be.

I don't understand what I'm doing wrong in the formula above, as far as I know it, it should work. But I'm obviously wrong...


回答1:


Your second approach should give you the bounding sphere for your bounding box, but as you discovered, it will be larger than necessary for anything other than a box.

A better bounding sphere can be found by translating the model points so they're centered on the origin using the bounding box dimensions you already have, then for each individual vertex calculate the radius from the origin for that point using the sqrt(x*x + y*y + z*z) formula. Whichever of those is the largest is the radius of your bounding sphere.

Note that this won't be the optimal bounding sphere. For that you'd have to find the convex hull of your model and use something like rotating calipers to pick the optimal center point for the sphere.

To show it in 2D, the red outline is the bounding box of the shape, and the blue circle is the bounding circle of the box. The improved circle using the polygon vertices, and centered on the box is green. Note that none of the points of the black polygon touch the blue circle.




回答2:


One easy way would be to use Miniball to calculate the exact bounding sphere of the model. Integrating it into your project is hassle-free, as it only consists of a single header. It is licensed under the GPL however, which could be a problem. Example:

#include "Miniball.h"

// ...

Miniball<3> boundingSphere;

// Iterate over all vertices in the model
for (int vertexId = 0; vertexId < model.getNumVertices(); ++vertexId) {
  // Convert vertex position to Miniball point type
  Point<3> point;
  for (int dim = 0; dim < 3; ++dim) {
    point[dim] = model.getVertex(vertexId)[dim];
  }
  // Add point to bounding sphere
  boundingSphere.check_in(point);
}
// Actually calculate the sphere
boundingSphere.build();

// Get back the results
Point<3> center = boundingSphere.center();
double radiusSq = boundingSphere.squared_radius();



回答3:


If it's a sphere, then don't you only need to work it out based on one axis? I might be way off here - but by definition, won't a sphere have the same width, height and depth? So radius on one axis=radius on another=radius of another?



来源:https://stackoverflow.com/questions/6083286/having-a-little-issue-calculating-the-bounding-sphere-radius

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!