Java3d 读取 3d 对象的每个多边形 [英] Java3d read each polygon of an 3d-object

查看:21
本文介绍了Java3d 读取 3d 对象的每个多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java3d(版本 1.6)并尝试从任何对象读取所有多边形.

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

我使用以下代码加载了一个对象:

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 或 Scene Object 读取计算出的多边形,并将每个多边形放在一个主要由 Point3d 数组组成的类中.在这门课上,我构建了一些算法来搜索特定的点和东西.那么我将如何获得这些多边形?

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 或球体,因为它们不够精确.我也希望有不同的解决方案!

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).

其次可以看ObjectFile 类的 Java 文档.我建议您使用标志TRIANGULATE"以确保获得仅包含凸多边形的多边形数组以简化计算.

Secondly, you can 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.

Scene 对象的分支组包含一个 Shape3D 对象.这个 Shape3D 对象包含一个 Geometry 对象,它存储您的多边形.ObjectFile 的源代码是 此处.看看 这一行.

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.可以看到该组被添加到场景中 此处.调用 组.getAllChildren(),循环所有子节点,使用instanceof判断子节点是否是Shape3D的实例.对于每个 Shape3D,调用 getGeometry() 或 getAllGeometries().几何图形应该是一个 GeometryArray,也许是一个 TriangleArray.getCoordRefBuffer() 在 Java 3D 1.6 中的工作方式可能不完全相同,因为我们删除了 J3DBuffer,使用了 getCoordRefDouble()、getCoordRefFloat() 或 getCoordinate() 或 getCoordinates() 的任何变体.请确保您使用 Java 3D 1.6,以便我们讨论相同的代码和相同的版本.旧版本已过时且无需维护.

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 nz 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

这篇关于Java3d 读取 3d 对象的每个多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆