Thu, 11/06/2025 - 22:52
Forums:
Hello everyone,
I am trying to compute the bounding box of a TopoDS_Shape, but I need the box to be expressed in a specific local coordinate system, not in the global one.
For example, I have a transformation (represented by a gp_Ax2 or a gp_Trsf) that defines the local coordinate system in which I want the bounding box to be calculated.
My question is:
What is the recommended way in OpenCascade to compute the bounding box of a shape with respect to a custom coordinate system?
I would like to get the bounding box dimensions and position relative to that local system, not the global one.
Any advice or example code would be greatly appreciated.
Thank you!
Fri, 11/07/2025 - 22:10
If you want computing AABB (axes-aligned bounding box) of shape within a local coordinate system, the only thing to do is to calculate a proper transformation to this local system and apply it to shape or AABB.
Lets start from defining an auxiliary tool for printing AABB (
Bnd_Box):and construct a trivially-oriented Cone:
then define a Cone having the same dimensions, but with a more complex orientation:
The bounding boxes do not match, which is expected. Now, lets define transformation
gp_Trsffrom one coordinate system to another, apply it to our complex-oriented Cone as locationTopLoc_Locationand compute AABB for it:We see, that dimensions of this AABB now matches to AABB of trivially-oriented Cone, but min/max is shifted. This is because within
TopoDS_Shape::Located()we have overridden original local transformation of the shape. To fix this, we need multiplying transformations:Now we got expected AABB of a Cone within the local coordinate system (in this case, this local coordinate system is where Cone was actually defined - just to be able to compare values).
Instead of computing a new AABB on transformed (located)
TopoDS_Shape, we might also apply transformation to AABBBnd_Boxitself:Notice that transformed AABB is valid - it contains the entire Cone, - but it is less optimal, as it will grow up while transforming AABB as a simple box. A new AABB computed for transformed
TopoDS_Shapewill be more tight, as it will consider transformation on-the-fly.Notice, that in either way
BRepBndLib::Add()computes AABB that might be a little bit larger than actual geometry by design, to may calculations reasonably fast. FlaguseTriangulationtoBRepBndLib::Add()would ask computing AABB on triangulation instead of geometry, when it is available. There are also methodsBRepBndLib::AddOptimal()calculating a more precise AABB, andBRepBndLib::AddOBB()calculating an oriented-bounding box instead of axes-aligned one (the latter should more friendly to be transformed).Mon, 11/10/2025 - 14:06
Dear Mr. gkv311 n,
I don’t know what you ate, drank, or how you slept before replying to this message, but for me, your answer is one of the clearest and most complete I have ever received on this forum. I truly cannot thank you enough for the information you shared—it helped me solve my problem!
The maximum precision for the calculated bounding-box dimensions was achieved using:
BRepBndLib::AddOptimal(myTopoDS_Shape, myBoundaryBox, false, false);
Final dimensions of myBoundaryBox:
X
165.87302434444427 – expected value
165.8730212948662 – OCCT result
Y
91.942996978759766 – expected value
91.94299849844691 – OCCT result
Z
10 – expected value
10.0000002 – OCCT result
Thank you again, I really appreciate your help!