How-To: Get the Net Bounding of a Segment

Although a SegmentKey has a method to obtain the bounding (i.e. ShowBounding) this approach will not include any modeling matrices applied to the segment or those inherited. That is to say the net modelling matrix.

Instructions

To obtain the bounding which includes the net modelling matrix, you must use the ShowNetBounding method of a KeyPath. A KeyPath “is an ordered list of keys that forms an unambiguous chain from a scene graph leaf to its root.” This approach ensures the bounding information will account for all concatenated modelling transforms for a particular segment in a given location in the scene graph.

Consider the following sample code which can be added to the CHPSView class in the mfc_sandbox;

void CHPSView::OnUserCode1()
{
	Point points[8] = { Point(0, 0, 0), Point(2, 0, 0), Point(2, 2, 0), Point(0, 2, 0),
		Point(0, 0, 2), Point(2, 0, 2), Point(2, 2, 2), Point(0, 2, 2) };

	int faceList[30] = { 4, 0, 1, 2, 3,
		4, 1, 5, 6, 2,
		4, 5, 4, 7, 6,
		4, 4, 0, 3, 7,
		4, 3, 2, 6, 7,
		4, 0, 4, 5, 1 };

	SegmentKey parentSegmentKey = GetCanvas().GetFrontView().GetAttachedModel().GetSegmentKey().Subsegment("test");
	parentSegmentKey.GetModellingMatrixControl().Translate(0, 0, 10);
	
	SegmentKey mySegmentKey = parentSegmentKey.Subsegment("geom");
	mySegmentKey.InsertShell(8, points, 30, faceList);
	mySegmentKey.GetModellingMatrixControl().Translate(10, 10, 0);

    // First obtain the bounding from this segment for comparison
	BoundingKit bKit;
	mySegmentKey.ShowBounding(bKit);

	SimpleSphere sphere;
	SimpleCuboid cuboid;
	bKit.ShowVolume(sphere, cuboid);
	
	TRACE("bounding sphere : center = { %g, %g, %g }, r = %g\n",
		sphere.center.x, sphere.center.y, sphere.center.z, sphere.radius);
	TRACE("bounding cuboid : min = { %g, %g, %g }, max = { %g, %g, %g }\n",
		cuboid.min.x, cuboid.min.y, cuboid.min.z,
		cuboid.max.x, cuboid.max.y, cuboid.max.z);

    // Next obtain the net bounding
	BoundingKit netBKit;
	// Build the key path
	KeyPath kpath;
	kpath.PushBack(mySegmentKey);
	kpath.PushBack(parentSegmentKey); // optional
	kpath.PushBack(GetCanvas().GetFrontView().GetAttachedModel().GetSegmentKey());
	kpath.ShowNetBounding(netBKit);
	netBKit.ShowVolume(sphere, cuboid);

	TRACE("net bounding sphere : center = { %g, %g, %g }, r = %g\n",
		sphere.center.x, sphere.center.y, sphere.center.z, sphere.radius);
	TRACE("net bounding cuboid : min = { %g, %g, %g }, max = { %g, %g, %g }\n",
		cuboid.min.x, cuboid.min.y, cuboid.min.z,
		cuboid.max.x, cuboid.max.y, cuboid.max.z);
}
  • Build and launch the mfc_sandbox
  • Execute Use Code 1
  • Note the following in the debug output window;
bounding sphere : center = { 1, 1, 1 }, r = 1.73205
bounding cuboid : min = { 0, 0, 0 }, max = { 2, 2, 2 }
net bounding sphere : center = { 11, 11, 11 }, r = 1.73205
net bounding cuboid : min = { 10, 10, 10 }, max = { 12, 12, 12 }