miho / JCSG

Java implementation of BSP based CSG (Constructive Solid Geometry)
Other
177 stars 52 forks source link

CSG class : issue in OBJ file generation : faces are not saved #50

Closed karimall72 closed 6 years ago

karimall72 commented 6 years ago

Hello,

Currently, using the method toObjString(StringBuilder sb) {...} leads to a file whose content looks like:

Group

g v3d.csg

Vertices

v 0.0 10.0 0.0 v 2.0 10.0 0.0 v 2.0 5.0 0.0 v 10.0 5.0 0.0

Faces

Indices of faces are not written in the file. The issue can be easily solved by adding one line in a loop of the method (update of the indices list, see last line below):

    for (Polygon p : polygons) {
        List<Integer> polyIndices = new ArrayList<>();

        p.vertices.stream().forEach((v) -> {
            if (!vertices.contains(v)) {
                vertices.add(v);
                v.toObjString(sb);
                polyIndices.add(vertices.size());
            } else {
                polyIndices.add(vertices.indexOf(v) + 1);
            }
        });

        // The line below solves the issue, but was it the idea of the dev ??
        indices.add(new PolygonStruct(null, polyIndices, null));

    }

Regards

miho commented 6 years ago

Please indicate which flavor of JCSG you are referring to. It looks like you are using a fork. While these forks do often contain interesting new features, they are usually not as stable as the mainline version.

Have a look at the original code: https://github.com/miho/JCSG/blob/f2b4c4b6ee15a4f2958052e3e573119da97d56c9/src/main/java/eu/mihosoft/jcsg/CSG.java#L795

I'm closing this now. But if you have trouble with the mainline version (https://github.com/miho/JCSG/) let us know and we can reopen the issue.

karimall72 commented 6 years ago

Hello,

I am using version v0.5.7 (at least, I hope so, but I am not very familiar with Git, I have to learn).

I think the situation is the following : OBJ file generation works fine if you use:

    ObjFile objF = new TestPolyhedron().toCSG().toObj();
    objF.toFiles(Paths.get("testPolyhedron.obj")); 

But you get into trouble if you use:

FileUtil.write(Paths.get("testPolyhedron.obj"), new TestPolyhedron().toCSG().toObjString());

It leads to a call to the method: public StringBuilder toObjString(StringBuilder sb) where the update of the indices list is not done.

The easiest fix would probably be to remove from the CSG API the methods toObjString() and toObjString(StringBuilder sb). One only way of doing one thing is enough ;)

Regards