Open Inventor Release 2024.2.0
 
Loading...
Searching...
No Matches
Calculating a Bounding Box

The bounding-box action computes a 3D bounding box that encloses the shapes in a subgraph under a node or defined by a path. This action also computes the center point of these shapes (see Setting the Center Field of a Transform Node ). SoGet-BoundingBoxAction is typically called on a path, which enables you to obtain a bounding box for a specific object in world coordinates. This action returns an SbBox3f, which specifies a 3D box aligned with the x-, y-, and z-axes in world coordinate space.

Create an Instance of the Action

An example of creating an instance of SoGetBoundingBoxAction is

C++ :

SbViewportRegion vpReg;
vpReg.setWindowSize( 300, 200 );
SoGetBoundingBoxAction bboxAction( vpReg );

C# :

SbViewportRegion vpReg = new SbViewportRegion();
vpReg.SetWindowSize( 300, 200 );
SoGetBoundingBoxAction bboxAction = new SoGetBoundingBoxAction( vpReg );

Java :

SbViewportRegion vpReg = new SbViewportRegion();
vpReg.setWindowSize( ( short )300, ( short )200 );
SoGetBoundingBoxAction bboxAction = new SoGetBoundingBoxAction( vpReg );

This constructor has one parameter, the viewport region. This information is needed for computing the bounding box of screen-aligned or screen-sized objects, such as SoText2.

Apply the Action

SoGetBoundingBoxAction can be applied to the root node of a subgraph, to a path, or to a path list.

Obtain Results

Three methods access the results of SoGetBoundingBoxAction :

getBoundingBox() returns an SbBox3f bounding box that encloses the shape or shapes
getCenter() returns the computed center point for the shapes
getXfBoundingBox() returns an SbXfBox3f bounding box

The center point returned by getCenter() is defined differently for different objects. For example, the center of an SoFaceSet is defined as the average of its vertices' coordinates. The center of a group is defined as the average of the centers of the objects in the group.

An SbXfBox3f stores the original bounding box for a shape and the matrix that transforms it to the correct world space. The advantage to using an SbXfBox3f instead of an SbBox3f is that the bounding box isn't enlarged unnecessarily. You may want to use this class if you need to perform additional transformations on the bounding box.

Setting the Center Field of a Transform Node shows using an SoGetBoundingBoxAction (bboxAction) to return the center of the graph rooted by a node so that rotations can be made around it.

Example : Setting the Center Field of a Transform Node

C++ :

SbViewportRegion myViewport;
SoTransform* myTransform;
SoGetBoundingBoxAction bboxAction( myViewport );
bboxAction.apply( root );
myTransform->center = bboxAction.getCenter();

C# :

SbViewportRegion myViewport = new SbViewportRegion();
SoTransform myTransform = new SoTransform();
SoGetBoundingBoxAction bboxAction = new SoGetBoundingBoxAction( myViewport );
bboxAction.Apply( root );
myTransform.center.Value = bboxAction.GetCenter();

Java :

SbViewportRegion myViewport = new SbViewportRegion();
SoTransform myTransform = new SoTransform();
SoGetBoundingBoxAction bboxAction = new SoGetBoundingBoxAction( myViewport );
bboxAction.apply( root );
myTransform.center.setValue( bboxAction.getCenter() );