Hi Ole,

I was under the impressed that there was a function in the API to call
in order to perform these transformations. In the meantime I did
something similar to your code.

My VRML-Worls contain several shapes which represent a sinlge Object. I
interate through all shapes and geometries in it and calculate the
minimum and maximum coordinates. Then I am able to resize and translate
that properly.

Best Regards,

Andreas


------------- class BranchGroupNormalizer.java

import javax.vecmath.Vector3f;
import javax.vecmath.Point3d;
import javax.media.j3d.*;

class BranchGroupNormalizer extends TransformGroup {
        private javax.media.j3d.BranchGroup bg;

public BranchGroupNormalizer(BranchGroup aBG) {
        super();
        bg = aBG;
        updateTransform();
}

private void updateTransform() {
        Point3d min = new Point3d();
        Point3d max = new Point3d();
        Point3d point = new Point3d();

        Shape3D aShape3D;
        for (int i = 0; i < bg.numChildren(); i++){
                Geometry geom;
                GeometryArray ga;
                try {
                        aShape3D = (Shape3D)bg.getChild(i);
                } catch (ClassCastException cce) {
                        // Node was not a Shape3D
                        continue;
                }
                for (int j = 0; j < aShape3D.numGeometries(); j++) {
                        geom = aShape3D.getGeometry(j);
                        if (geom instanceof GeometryArray) {
                                ga = (GeometryArray)geom;
                                int num = ga.getVertexCount();
                                for (int k = 0 ; k < num ; k++ )
                                {
                                        ga.getCoordinate(k, point);
                                        if (point.x < min.x) {
                                                min.x = point.x;
                                        } else if (point.x > max.x) {
                                                max.x = point.x;
                                        }
                                        if (point.y < min.y) {
                                                min.y = point.y;
                                        } else if (point.y > max.y) {
                                                max.y = point.y;
                                        }
                                        if (point.z < min.z) {
                                                min.z = point.z;
                                        } else if (point.z > max.z) {
                                                max.z = point.z;
                                        }
                                }
                        }
                }
        }
        Transform3D back2Origin = new Transform3D();
        back2Origin.setTranslation(
                new Vector3f(   -(float)(0.5*(max.x+min.x)),
                                -(float)(0.5*(max.y+min.y)),
                                -(float)(0.5*(max.z+min.z))));
        Transform3D scale2UnitCube = new Transform3D();
        scale2UnitCube.setScale(2/Math.max(
                max.x-min.x,
                Math.max(max.y-min.y, max.z-min.z)));
        scale2UnitCube.mul(back2Origin);
        setTransform(scale2UnitCube);
}
}


------------- How to use it (snippet from HelloUniverse.java

VrmlLoader vrmlLoader = new VrmlLoader();
try {
        Scene aScene = vrmlLoader.load("Arbeitsplatz.wrl");
        BranchGroupNormalizer bgn = new
BranchGroupNormalizer(aScene.getSceneGroup());
        bgn.addChild(aScene.getSceneGroup());
        objTrans.addChild(bgn);
} catch (Exception e) {
        System.out.println(e);
}

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to