Bounding box generation - Open CASCADE

Bounding box generation -  Open CASCADE

Bounding boxes are needed to identify the size of the shape. Bounding boxes gives the extreme coordinates of a given geometry. 

In Open CASCADE there is no method to display the bounding box directly. You have to create a box using the data of the bounding box. In the following images, the bounding boxes are generated using the extracted data.

Axis-aligned bounding box generation


Axis Aligned Bounding Box

You can simply create an Oriented bounding box using the following code snippet.

TopoDS_Shape shape = ...; // the shape
Bnd_Box bndBox;
BRepBndLib::Add(shape, bndBox);

// get corner points
gp_Pnt cornerMinPoint = bndBox.CornerMin();
gp_Pnt cornerMaxPoint = bndBox.CornerMax();

// get bounding box top and bottom vertices
double bottomPointX, bottomPointY, bottomPointZ, topPointX, topPointY, topPointZ;
bndBox.Get(bottomPointX, bottomPointY, bottomPointZ, topPointX, topPointY, topPointZ);

When we create a bounding box using "Add()" function, the size of the bounding box is somewhat larger than the given shape. If you want to get the exact size of the bounding box, you need to use "AddClose()" function.

Axis-aligned optimal bounding box

The optimal bounding box gives you the exact size bounding box.

TopoDS_Shape shape = ...; // the shape
Bnd_Box bndBox;
BRepBndLib::AddOptimal(shape, bndBox);

// get corner points
gp_Pnt cornerMinPoint = bndBox.CornerMin();
gp_Pnt cornerMaxPoint = bndBox.CornerMax();

// get bounding box top and bottom vertices
double bottomPointX, bottomPointY, bottomPointZ, topPointX, topPointY, topPointZ;
bndBox.Get(bottomPointX, bottomPointY, bottomPointZ, topPointX, topPointY, topPointZ);

These two functions will give you an axis-aligned bounding box. This means the bounding box is aligned with the global axis system. 

Oriented bounding Box

Oriented Bounding Box

All the previous bounding boxes are aligned with the global coordinate system. But if there is a shape which is not aligned with the global coordinate system, the bounding box is not perfectly fit to that. At that point, we can create a bounding box using "AddOBB()" function.

TopoDS_Shape shape = ...; // the shape
Bnd_OBB bndBox;
BRepBndLib::AddOOB(shape, bndBox);

// get the directions of the axis system
gp_XYZ xDir = bndBox.XDirection();
gp_XYZ yDir = bndBox.YDirection();
gp_XYZ zDir = bndBox.ZDirection();

Select which type of bounding box based on your requirement.

Post a Comment

Previous Post Next Post