简体   繁体   English

Java3d读取3d对象的每个多边形

[英]Java3d read each polygon of an 3d-object

I'm using Java3d ( VERSION 1.6 ) and am trying to read all polygons from any object. 我正在使用Java3d( VERSION 1.6 ),并试图从任何对象读取所有多边形。

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();

        }

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. 现在,我想从该BranchGroup或Scene Object中读取计算出的多边形,并将其放入主要包含Point3d数组的类中。 With that class I build some algorithms to search for specific points and stuff. 通过该类,我构建了一些算法来搜索特定的点和东西。 So how would I get these polygons? 那么我将如何获得这些多边形?

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. 我不能使用BoundingBoxes或spheres,因为这不够精确。 I would appreciate a different solution as well! 我也将不胜感激!

EDIT: With the help of gouessej I got so far: 编辑:在gouessej的帮助下,到目前为止:

    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();

    }

But on the line ta.getCoordRefDouble() , it throws me an Exception: 但是在ta.getCoordRefDouble() ,它抛出了一个异常:

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? 这是什么意思,以及如何解决?

At first, Java 3D is NOT dead as you can see here (please edit your question). 最初,Java 3D并没有如您在此处看到的那样死(请编辑您的问题)。

Secondly, you can look at the Java documentation of the class ObjectFile . 其次,您可以查看ObjectFile类的Java文档 I advise you to use the flag "TRIANGULATE" to be sure to get a polygon array containing only convex polygons to ease your computations. 我建议您使用标志“ TRIANGULATE”,以确保获得仅包含凸多边形的多边形数组,以简化计算。

The branch group of your Scene object contains one Shape3D object. Scene对象的分支组包含一个Shape3D对象。 This Shape3D object contains a Geometry object, it stores your polygons. 此Shape3D对象包含一个Geometry对象,它存储您的多边形。 The source code of ObjectFile is here . ObjectFile的源代码在这里 Look at this line . 这条线

Edit.: You can get the BranchGroup of your scene by calling Scene.getSceneGroup(). 编辑:您可以通过调用Scene.getSceneGroup()获得场景的BranchGroup。 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. 调用Group.getAllChildren() ,在所有子项上循环,使用instanceof检查子项是否是Shape3D的实例。 For each Shape3D, call getGeometry() or getAllGeometries(). 对于每个Shape3D,调用getGeometry()或getAllGeometries()。 The geometry should be a GeometryArray, maybe a TriangleArray. 几何形状应该是GeometryArray,也许是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(). getCoordRefBuffer()在Java 3D 1.6中可能无法完全以相同的方式工作,因为我们删除了J3DBuffer,使用getCoordRefDouble(),getCoordRefFloat()或getCoordinate()或getCoordinates()的任何变体。 Please ensure that you use Java 3D 1.6 so that we are talking about the same code and the same version. 请确保您使用Java 3D 1.6,以便我们正在谈论相同的代码和相同的版本。 Older versions are obsolete and unmaintained. 旧版本已过时且未维护。

Edit.2: Rather call getInterleavedVertices() as its name implies if the vertices are interleaved. Edit.2:更确切地说,调用getInterleavedVertices()可以隐含地说顶点是交错的。 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 请记住,它可能也包含法线(在第一位置),不仅包含顶点坐标(在第二位置):nx ny nz vx vy vz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM