/**
* Sets the <code>model</code> displayed by this component.
* The model is shown at its default orientation and in a box of 1 unit wide.
*/
public void setModel(BranchGroup model) {
TransformGroup modelTransformGroup = (TransformGroup)this.sceneTree.getChild(0);
modelTransformGroup.removeAllChildren();
if (model != null) {
model = (BranchGroup)ModelManager.getInstance().cloneNode(model);
model.setCapability(BranchGroup.ALLOW_DETACH);
setNodeCapabilities(model);
if (model.numChildren() > 0) {
BoundingBox bounds = null;
try {
bounds = ModelManager.getInstance().getBounds(model);
} catch (IllegalArgumentException ex) {
// Model is empty
}
if (bounds != null) {
Point3d lower = new Point3d();
bounds.getLower(lower);
Point3d upper = new Point3d();
bounds.getUpper(upper);
// Translate model to center
Transform3D translation = new Transform3D ();
translation.setTranslation(
new Vector3d(-lower.x - (upper.x - lower.x) / 2,
-lower.y - (upper.y - lower.y) / 2,
-lower.z - (upper.z - lower.z) / 2));
// Scale model to make it fit in a 1.8 unit wide box
Transform3D modelTransform = new Transform3D();
modelTransform.setScale (1.8 / Math.max (Math.max (upper.x -lower.x, upper.y - lower.y),
upper.z - lower.z));
modelTransform.mul(translation);
modelTransformGroup.setTransform(modelTransform);
modelTransformGroup.addChild(model);
}
}
}
}