Java3D的读取三维对象的每个多边形多边形、对象、Java3D

2023-09-08 01:11:59 作者:╯幸福°持有者

我用的Java3D(版本1.6 ),并想从任何对象读取所有的多边形。

I'm using Java3d (VERSION 1.6) and am trying to read all polygons from any object.

我用下面code加载一个对象:

I loaded one object using following code:

private BranchGroup loadObj(String p) {
        BranchGroup objRoot = new BranchGroup(); 
        TransformGroup tg = new TransformGroup();
        Transform3D t3d = new Transform3D();
        t3d.setScale(0.3);
        Matrix4d matrix = new Matrix4d();
        t3d.get(matrix);
        try
        {   
            Scene s = null;
            ObjectFile f = new ObjectFile ();
            String basepath = new File(p).getAbsolutePath();
            System.out.println(basepath);
            f.setBasePath(basepath);

            f.setFlags (0);

            s = f.load (s1);

            s.getSceneGroup().setBoundsAutoCompute(true);
            tg.addChild (s.getSceneGroup ());


            objRoot.addChild(tg);
            bounds.add(objRoot.getBounds());
            objRoot.compile();

        }

现在我喜欢看从BranchGroup或场景对象计算出的多边形,并把每一个类三维点的主要数组。与该类我建立了一些算法来搜索特定点之类的东西。因此,我将如何得到这些多边形?

Now I like to read the computed polygons from that BranchGroup or Scene Object and put each in a class of mainly an array of Point3d's. With that class I build some algorithms to search for specific points and stuff. So how would I get these polygons?

我之所以需要它是因为我想走在不平坦的表面。我不能使用BoundingBoxes或球,因为这是不是precise不够。我想AP preciate不同的解决方案,以及!

The reason I need it is because I'm trying to "walk" over an uneven surface. I can't use BoundingBoxes or spheres, for that is not precise enough. I would appreciate a different solution as well!

编辑: 随着gouessej的帮助下,我得那么远:

With the help of gouessej I got so far:

    try
    {   
        Scene s = null;
        ObjectFile f = new ObjectFile ();
        String basepath = new File(p).getAbsolutePath();
        System.out.println(basepath);
        f.setBasePath(basepath);

        f.setFlags (ObjectFile.TRIANGULATE);

        String s1 = p;
        s = f.load (s1);

        BranchGroup branch = s.getSceneGroup();
        branch.setBoundsAutoCompute(true);
        Shape3D shape = (Shape3D)branch.getChild(0);
        Geometry g = shape.getGeometry();
        TriangleArray ta = (TriangleArray)shape.getGeometry();
        System.out.println(ta.getVertexCount()); // Prints around 95.000, sounds about right
        System.out.println(ta.getVertexFormat()); // prints 387

        double[] coords = ta.getCoordRefDouble(); // line: 526; Here it throws the exception


        System.out.println(Arrays.toString(coords));  


        tg.addChild (branch);


        objRoot.addChild(tg);

        bounds.add(objRoot.getBounds());
        System.out.println();
        objRoot.compile();

    }

不过就行了 ta.getCoordRefDouble(),它引发了我的异常:

But on the line ta.getCoordRefDouble(), it throws me an Exception:

Exception in thread "main" java.lang.IllegalStateException: GeometryArray: cannot access individual array references in INTERLEAVED mode
    at javax.media.j3d.GeometryArray.getCoordRefDouble(GeometryArray.java:5755)
    at com.object.simpleTest.Test1.loadObj(Test1.java:526)
    at com.object.simpleTest.Test1.<init>(Test1.java:428)
    at com.object.simpleTest.Test1.main(Test1.java:686)

这是什么意思,以及如何解决它?

What does it mean and how to fix it?

推荐答案

首先,Java的3D是不是死了,你可以看到这里(请编辑您的问题)。

At first, Java 3D is NOT dead as you can see here (please edit your question).

其次,因为我们还没有修改的公共API,你仍然可以看看类ObjectFile 的Java文档。我建议你​​使用的国旗三角,以确保获得一个只包含凸多边形,以方便您的计算多边形数组。

Secondly, as we haven't modified the public API, you can still look at the Java documentation of the class ObjectFile. I advise you to use the flag "TRIANGULATE" to be sure to get a polygon array containing only convex polygons to ease your computations.

场景对象的分支组包含一个一个Shape3D对象。这一个Shape3D对象包含几何对象,它存储你的多边形。 ObjectFile的来源$ C ​​$ c是此处。看此行。

The branch group of your Scene object contains one Shape3D object. This Shape3D object contains a Geometry object, it stores your polygons. The source code of ObjectFile is here. Look at this line.

编辑:您可以通过调用Scene.getSceneGroup得到场景的BranchGroup()。你可以看到,该组被添加到场景此处。呼叫 Group.getAllChildren(),环路上的所有儿童,使用instanceof来检查孩子是否是一个Shape3D的一个实例。对于每一个Shape3D,调用getGeometry()或getAllGeometries()。几何形状应该是一个GeometryArray,也许TriangleArray。 getCoordRefBuffer()可能不以相同的方式准确地工作在爪哇三维1.6,因为我们除去J3DBuffer,使用getCoordRefDouble(),getCoordRefFloat()或getCoordinate()或getCoordinates的任何变体()。请确保您使用的Java 3D 1.6,使我们在谈论同样的code和相同的版本。旧版本已经过时无人维护。

Edit.: You can get the BranchGroup of your scene by calling Scene.getSceneGroup(). You can see that the group is added into the scene here. Call Group.getAllChildren(), loop on all children, use instanceof to check whether a child is an instance of Shape3D. For each Shape3D, call getGeometry() or getAllGeometries(). The geometry should be a GeometryArray, maybe a TriangleArray. getCoordRefBuffer() might not work exactly in the same way in Java 3D 1.6 because we removed J3DBuffer, use getCoordRefDouble(), getCoordRefFloat() or any variant of getCoordinate() or getCoordinates(). Please ensure that you use Java 3D 1.6 so that we are talking about the same code and the same version. Older versions are obsolete and unmaintained.

Edit.2:与其叫 getInterleavedVertices()的顾名思义如果顶点被交错。请记住,它可能包含法线太(在第一位置),不仅顶点坐标(在第二位置): NX NY新西兰VX VY VZ

Edit.2: Rather call getInterleavedVertices() as its name implies if the vertices are interleaved. Keep in mind that it might contain the normals too (in first position), not only the vertex coordinates (in second position): nx ny nz vx vy vz

 
精彩推荐
图片推荐