8.5. 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 Example 8.1, “ 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( C++ | Java ), which specifies a 3D box aligned with the x-, y-, and z-axes in world coordinate space.

An example of creating an instance of SoGetBoundingBoxAction( C++ | Java | .NET ) is


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

.NET
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( C++ | Java | .NET ).

SoGetBoundingBoxAction( C++ | Java | .NET ) can be applied to the root node of a subgraph, to a path, or to a path list.

Three methods access the results of SoGetBoundingBoxAction( C++ | Java | .NET ):

The center point returned by getCenter() is defined differently for different objects. For example, the center of an SoFaceSet( C++ | Java | .NET ) 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( C++ | Java ) 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( C++ | Java ) instead of an SbBox3f( C++ | Java ) 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.

Example 8.1, “ Setting the Center Field of a Transform Node shows using an SoGetBoundingBoxAction( C++ | Java | .NET ) (bboxAction) to return the center of the graph rooted by a node so that rotations can be made around it.