Skip to content

Commit d40f11b

Browse files
committed
Add error check
1 parent 9c25ee0 commit d40f11b

1 file changed

Lines changed: 33 additions & 23 deletions

File tree

bindings/java/src/main/java/com/cadoodlecad/manifold/ManifoldBindings.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525
import java.util.zip.*;
2626

2727
public class ManifoldBindings {
28+
public enum ManifoldError {
29+
NO_ERROR, NON_FINITE_VERTEX, NOT_MANIFOLD, VERTEX_INDEX_OUT_OF_BOUNDS, PROPERTIES_WRONG_LENGTH,
30+
MISSING_POSITION_PROPERTIES, MERGE_VECTORS_DIFFERENT_LENGTHS, MERGE_INDEX_OUT_OF_BOUNDS, TRANSFORM_WRONG_LENGTH,
31+
RUN_INDEX_WRONG_LENGTH, FACE_ID_WRONG_LENGTH, INVALID_CONSTRUCTION, RESULT_TOO_LARGE;
32+
33+
public static ManifoldError fromInt(int code) {
34+
ManifoldError[] values = values();
35+
if (code < 0 || code >= values.length)
36+
throw new IllegalArgumentException("Unknown ManifoldError code: " + code);
37+
return values[code];
38+
}
39+
}
2840

2941
private static boolean loaded = false;
3042

@@ -296,9 +308,8 @@ public ManifoldBindings(File cacheDirectory) throws Exception {
296308
load("manifold_polygons_simple_length", ValueLayout.JAVA_LONG, ValueLayout.ADDRESS, ValueLayout.JAVA_LONG);
297309
// ManifoldVec2 manifold_polygons_get_point(ManifoldPolygons* ps, size_t simple_idx, size_t pt_idx);
298310
// ManifoldVec2 = {double x, double y} = 16 bytes, returned by value
299-
load("manifold_polygons_get_point",
300-
MemoryLayout.structLayout(ValueLayout.JAVA_DOUBLE, ValueLayout.JAVA_DOUBLE),
301-
ValueLayout.ADDRESS, ValueLayout.JAVA_LONG, ValueLayout.JAVA_LONG);
311+
load("manifold_polygons_get_point", MemoryLayout.structLayout(ValueLayout.JAVA_DOUBLE, ValueLayout.JAVA_DOUBLE),
312+
ValueLayout.ADDRESS, ValueLayout.JAVA_LONG, ValueLayout.JAVA_LONG);
302313
// ManifoldPolygons* manifold_alloc_polygons();
303314
load("manifold_alloc_polygons", ValueLayout.ADDRESS);
304315

@@ -493,7 +504,11 @@ public ManifoldBindings(File cacheDirectory) throws Exception {
493504

494505
}
495506

496-
507+
// ManifoldError manifold_status(ManifoldManifold* m);
508+
public ManifoldError status(MemorySegment m) throws Throwable {
509+
int code = (int) functions.get("manifold_status").invoke(m);
510+
return ManifoldError.fromInt(code);
511+
}
497512

498513
public ArrayList<double[][]> slice(MemorySegment m, double height) throws Throwable {
499514
MemorySegment polygons = null;
@@ -531,7 +546,7 @@ public ArrayList<double[][]> slice(MemorySegment m, double height) throws Throwa
531546
}
532547
}
533548

534-
public void deleteMeshGL64(MemorySegment seg) {
549+
private void deleteMeshGL64(MemorySegment seg) {
535550
if (seg == null)
536551
return;
537552
try {
@@ -919,6 +934,7 @@ public MemorySegment hull(MemorySegment m) throws Throwable {
919934
MemorySegment mem = (MemorySegment) functions.get("manifold_alloc_manifold").invoke();
920935
return (MemorySegment) functions.get("manifold_hull").invoke(mem, m);
921936
}
937+
922938
/**
923939
* Computes the convex hull of a set of points.
924940
*
@@ -965,6 +981,7 @@ public MemorySegment hull(ArrayList<double[]> points) throws Throwable {
965981
return (MemorySegment) functions.get("manifold_hull_pts").invoke(mem, ptsBuffer, count);
966982
}
967983
}
984+
968985
// ManifoldManifold* manifold_batch_hull(void* mem, ManifoldManifoldVec* ms);
969986
public MemorySegment batchHull(MemorySegment[] shapes) throws Throwable {
970987
MemorySegment vec = (MemorySegment) functions.get("manifold_alloc_manifold_vec").invoke();
@@ -1031,13 +1048,6 @@ public MemorySegment[] split(MemorySegment a, MemorySegment b) throws Throwable
10311048
}
10321049
}
10331050

1034-
// Placeholder
1035-
// public MemorySegment[] decompose(MemorySegment m) throws Throwable {
1036-
// MemorySegment mem = (MemorySegment) functions.get("manifold_alloc_manifold_vec").invoke();
1037-
// MemorySegment vec = (MemorySegment) functions.get("manifold_decompose").invoke(mem, m);
1038-
// TODO: Needs vector access functions to implement
1039-
// return new MemorySegment[]{m};
1040-
// }
10411051

10421052
// Creates an independent copy (safe for boolean operations)
10431053
// ManifoldManifold* manifold_copy(void* mem, ManifoldManifold* m);
@@ -1091,12 +1101,12 @@ public MemorySegment importMeshGL(float[] vertices, int[] triangles, long nVerts
10911101

10921102
manMem = (MemorySegment) functions.get("manifold_alloc_manifold").invoke();
10931103
manifold = (MemorySegment) functions.get("manifold_of_meshgl").invoke(manMem, merged);
1094-
// System.out.println("meshGLMem addr: " + meshGLMem.address());
1095-
// System.out.println("meshGL addr: " + meshGL.address());
1096-
// System.out.println("mergedMem addr: " + mergedMem.address());
1097-
// System.out.println("merged addr: " + merged.address());
1098-
// System.out.println("manMem addr: " + manMem.address());
1099-
// System.out.println("manifold addr: " + manifold.address());
1104+
// System.out.println("meshGLMem addr: " + meshGLMem.address());
1105+
// System.out.println("meshGL addr: " + meshGL.address());
1106+
// System.out.println("mergedMem addr: " + mergedMem.address());
1107+
// System.out.println("merged addr: " + merged.address());
1108+
// System.out.println("manMem addr: " + manMem.address());
1109+
// System.out.println("manifold addr: " + manifold.address());
11001110
return manifold;
11011111

11021112
} finally {
@@ -1295,11 +1305,11 @@ public long estimateMemoryBytes(MemorySegment m) throws Throwable {
12951305
//private HashMap<MemorySegment,Exception> deleted = new HashMap<>();
12961306

12971307
public void delete(MemorySegment manifold) throws Throwable {
1298-
// if (deleted.containsKey(manifold)) {
1299-
// System.err.println("Memory was deleted at ");
1300-
// deleted.get(manifold).printStackTrace();
1301-
// throw new DoubleFreeException();
1302-
// }
1308+
// if (deleted.containsKey(manifold)) {
1309+
// System.err.println("Memory was deleted at ");
1310+
// deleted.get(manifold).printStackTrace();
1311+
// throw new DoubleFreeException();
1312+
// }
13031313
functions.get("manifold_delete_manifold").invoke(manifold);
13041314
//deleted.put(manifold, new Exception());
13051315
}

0 commit comments

Comments
 (0)