How to get the proper bounding box of any shape

Hello Guys,

I've come across the following code to get the bounding box of a shape:
Bnd_Box box;
BRepBndLib::Add(shape, box);
box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);

The problem is, this does not work for every shape. As mentioned in the source code, "This is a quick algorithm but only works if the shape S is composed of polygonal planar faces". For example if you test this with a sphere, I get a 2-dimensional bounding box back and these two dimensions that exist aren't even the same size.

I've read that I have to tesselate/triangulate the shape and I tried it, but I must be doing something wrong. Here is what I'm doing:
BRepMesh::Mesh(shape,0.1); // shape is the TopoDS_Shape I want the bouding box
PVERTEX V1, V2;
TopExp_Explorer ex;
for ( ex.Init( m_shape, TopAbs_FACE ) ; ex.More(); ex.Next() )
{
TopoDS_Face F = TopoDS::Face(ex.Current());
// Get triangulation
TopLoc_Location L;
Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L);
const Poly_Array1OfTriangle & triangles = facing->Triangles();
const TColgp_Array1OfPnt & nodes = facing->Nodes();
if (!facing.IsNull())
{
for ( int i=facing->NbTriangles(); i >= 1; --i ) // (indeksy 1...N)
{
Poly_Triangle triangle = triangles(i);

Standard_Integer node1,node2,node3;
triangle.Get(node1, node2, node3);

gp_Pnt v1 = nodes(node1);
gp_Pnt v2 = nodes(node2);
gp_Pnt v3 = nodes(node3);
}
}
}

Bnd_Box box;
BRepBndLib::Add(shape, box);
box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);

-------
I also tried
Standard_Real aDeflection = 0.001, deflection;
Bnd_Box box;
BRepBndLib::Add(shape, box);
box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
deflection= MAX3_( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin)*aDeflection;
BRepMesh_IncrementalMesh Inc(shape, deflection);
box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
--------

Both to no sucess. Any idea what I'm doing wrong? Thanks a lot in advance!

Cauchy Ding's picture

Hi Bernardo,

Have you tried to test whether the location is identity?
TopLoc_Location L;
...
if(L.IsIdentity())

If the L is not identity, when you try to calculate the bounding box by yourself, you have to multiple this location to each point.

Ding

bcarneiro's picture

I'm sorry, am I trying to calculate the bounding box by myself? From what I understood from the code I posted, I thought that did the triangulation of my shape, and then when I extract the bounding box through the normal way, I would get the correct bounding box, or am I wrong?

Thanks a lot in advance!!

Cauchy Ding's picture

Standard_Real aDeflection = 0.001, deflection;
Bnd_Box box;
BRepBndLib::Add(shape, box);
box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
deflection= MAX3_( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin)*aDeflection;
BRepMesh_IncrementalMesh Inc(shape, deflection);
/** try to call it again*/
box.SetVoid();
BRepBndLib::Add(shape, box);
box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);

bcarneiro's picture

Thanks a lot for the quick reply Cauchy Ding, but it still didn't work. Sadly it made no difference. Any other idea guys? Thanks a lot in advance!