Skip to main content

[JAVA3D-INTEREST] Strange results visualizing a BoundingBox

No replies
Anonymous

Dear People !

I have written some functions for my project to calculate my own
BoundingBoxes. The function traverses the SceneGraph until it
reaches a leaf and then makes the boxes bottom up.
This is the code:
----- cut ------
/**
* Collects all points of the given Shape3D shape and inserts them in the List points.
*
* @param shape the Shape3D to be explored
* @param points the List to write the points into
*/
private void collectGeom(Shape3D shape, List points) {
for(Enumeration e = shape.getAllGeometries(); e.hasMoreElements();) {
GeometryArray go = (GeometryArray) e.nextElement();
if(go == null) continue;
for(int j = 0; j maxx) maxx = p.x;
if(p.y > maxy) maxy = p.y;
if(p.z > maxz) maxz = p.z;
}
BoundingBox b = new BoundingBox(new Point3d(minx,miny,minz), new Point3d(maxx,maxy,maxz));
shape.setBoundsAutoCompute(false);
shape.setBounds(b);
boxes.add(b);
}
else {
System.out.println("Unknown object type: "+o.getClass());
}
} // for(i)
return boxes;
}
----- cut ------

I call buildBoundingBox() on the root branchgroup.
This code works well at least it generates BoundingBoxes. But if
i try to visualize the BoundingBoxes with the following code, the Boxes
are only at the correct position, if they are children of a Branchgroup or
an identity transformgroup.
in all other cases, the Boxes are somehow rotated in various positions
but not the correct ones.

----- cut ------
public void setSelected(boolean selected) {
super.setSelected(selected);

Group group = (Group) getJ3dObject();

if(super.selectState) {
BoundingBox b = (BoundingBox) group.getBounds();
Box highlightBox;
Transform3D t3d = new Transform3D();
Point3d lower = new Point3d();
Point3d upper = new Point3d();

b.getLower(lower);
b.getUpper(upper);

t3d.setTranslation(new Vector3d(lower.x+(upper.x-lower.x)/2, lower.y+(upper.y-lower.y)/2, lower.z+(upper.z-lower.z)/2));
highlightTrans.setTransform(t3d);

Appearance ap = new Appearance();
PolygonAttributes pa = new PolygonAttributes();
pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
ap.setPolygonAttributes(pa);
highlightBox = new Box((new Double(upper.x-lower.x)).floatValue()/2,
(new Double(upper.y-lower.y)).floatValue()/2,
(new Double(upper.z-lower.z)).floatValue()/2, ap);

highlightBox.setCapability(Box.ENABLE_GEOMETRY_PICKING);
highlightBox.getShape(Box.BACK).getGeometry(0).setCapability(Geometry.ALLOW_INTERSECT);
highlightBox.getShape(Box.FRONT).getGeometry(0).setCapability(Geometry.ALLOW_INTERSECT);
highlightBox.getShape(Box.BOTTOM).getGeometry(0).setCapability(Geometry.ALLOW_INTERSECT);
highlightBox.getShape(Box.TOP).getGeometry(0).setCapability(Geometry.ALLOW_INTERSECT);
highlightBox.getShape(Box.LEFT).getGeometry(0).setCapability(Geometry.ALLOW_INTERSECT);
highlightBox.getShape(Box.RIGHT).getGeometry(0).setCapability(Geometry.ALLOW_INTERSECT);

highlightTrans.addChild(highlightBox);

J3DContext.getInstance().setLive(false);
group.addChild(highlightTrans);
J3DContext.getInstance().setLive(true);

}
else {
J3DContext.getInstance().setLive(false);
group.removeChild(highlightTrans);
J3DContext.getInstance().setLive(true);
}
}
----- cut ------

So, please help me to find the error in this code.
I'm thinking about that for a week already but can not find
my mistake.

--
MfG, Christian Welzel
(Sektionsvorsitzender und Admin AG DSN Gerokstrasse)

GPG-Key: http://www.camlann.de/key.asc
Fingerprint: 4F50 19BF 3346 36A6 CFA9 DBDC C268 6D24 70A1 AD15
[att1.dat]