Search in sources :

Example 1 with ObservableFaceArray

use of javafx.scene.shape.ObservableFaceArray in project FXyzLib by Birdasaur.

the class MeshUtils method mesh2CSG.

public static CSG mesh2CSG(Mesh mesh) throws IOException {
    List<Polygon> polygons = new ArrayList<>();
    List<Vector3d> vertices = new ArrayList<>();
    if (mesh instanceof TriangleMesh) {
        // Get faces
        ObservableFaceArray faces = ((TriangleMesh) mesh).getFaces();
        int[] f = new int[faces.size()];
        faces.toArray(f);
        // Get vertices
        ObservableFloatArray points = ((TriangleMesh) mesh).getPoints();
        float[] p = new float[points.size()];
        points.toArray(p);
        // convert faces to polygons
        for (int i = 0; i < faces.size() / 6; i++) {
            int i0 = f[6 * i], i1 = f[6 * i + 2], i2 = f[6 * i + 4];
            vertices.add(new Vector3d(p[3 * i0], p[3 * i0 + 1], p[3 * i0 + 2]));
            vertices.add(new Vector3d(p[3 * i1], p[3 * i1 + 1], p[3 * i1 + 2]));
            vertices.add(new Vector3d(p[3 * i2], p[3 * i2 + 1], p[3 * i2 + 2]));
            polygons.add(Polygon.fromPoints(vertices));
            vertices = new ArrayList<>();
        }
    }
    return CSG.fromPolygons(new PropertyStorage(), polygons);
}
Also used : PropertyStorage(eu.mihosoft.vrl.v3d.PropertyStorage) TriangleMesh(javafx.scene.shape.TriangleMesh) ArrayList(java.util.ArrayList) ObservableFaceArray(javafx.scene.shape.ObservableFaceArray) Vector3d(eu.mihosoft.vrl.v3d.Vector3d) Polygon(eu.mihosoft.vrl.v3d.Polygon) ObservableFloatArray(javafx.collections.ObservableFloatArray)

Example 2 with ObservableFaceArray

use of javafx.scene.shape.ObservableFaceArray in project FXyzLib by Birdasaur.

the class MeshUtils method mesh2STL.

public static void mesh2STL(String fileName, Mesh mesh) throws IOException {
    if (!(mesh instanceof TriangleMesh)) {
        return;
    }
    // Get faces
    ObservableFaceArray faces = ((TriangleMesh) mesh).getFaces();
    int[] f = new int[faces.size()];
    faces.toArray(f);
    // Get vertices
    ObservableFloatArray points = ((TriangleMesh) mesh).getPoints();
    float[] p = new float[points.size()];
    points.toArray(p);
    StringBuilder sb = new StringBuilder();
    sb.append("solid meshFX\n");
    // convert faces to polygons
    for (int i = 0; i < faces.size() / 6; i++) {
        int i0 = f[6 * i], i1 = f[6 * i + 2], i2 = f[6 * i + 4];
        Point3D pA = new Point3D(p[3 * i0], p[3 * i0 + 1], p[3 * i0 + 2]);
        Point3D pB = new Point3D(p[3 * i1], p[3 * i1 + 1], p[3 * i1 + 2]);
        Point3D pC = new Point3D(p[3 * i2], p[3 * i2 + 1], p[3 * i2 + 2]);
        Point3D pN = pB.subtract(pA).crossProduct(pC.subtract(pA)).normalize();
        sb.append("  facet normal ").append(pN.getX()).append(" ").append(pN.getY()).append(" ").append(pN.getZ()).append("\n");
        sb.append("    outer loop\n");
        sb.append("      vertex ").append(pA.getX()).append(" ").append(pA.getY()).append(" ").append(pA.getZ()).append("\n");
        sb.append("      vertex ").append(pB.getX()).append(" ").append(pB.getY()).append(" ").append(pB.getZ()).append("\n");
        sb.append("      vertex ").append(pC.getX()).append(" ").append(pC.getY()).append(" ").append(pC.getZ()).append("\n");
        sb.append("    endloop\n");
        sb.append("  endfacet\n");
    }
    sb.append("endsolid meshFX\n");
    // write file
    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), Charset.forName("UTF-8"), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
        writer.write(sb.toString());
    }
}
Also used : ObservableFaceArray(javafx.scene.shape.ObservableFaceArray) TriangleMesh(javafx.scene.shape.TriangleMesh) Point3D(javafx.geometry.Point3D) ObservableFloatArray(javafx.collections.ObservableFloatArray) BufferedWriter(java.io.BufferedWriter)

Aggregations

ObservableFloatArray (javafx.collections.ObservableFloatArray)2 ObservableFaceArray (javafx.scene.shape.ObservableFaceArray)2 TriangleMesh (javafx.scene.shape.TriangleMesh)2 Polygon (eu.mihosoft.vrl.v3d.Polygon)1 PropertyStorage (eu.mihosoft.vrl.v3d.PropertyStorage)1 Vector3d (eu.mihosoft.vrl.v3d.Vector3d)1 BufferedWriter (java.io.BufferedWriter)1 ArrayList (java.util.ArrayList)1 Point3D (javafx.geometry.Point3D)1