Skip to content

Commit c873c51

Browse files
committed
Adding new mesh bindings from @rondlh
1 parent b58881f commit c873c51

1 file changed

Lines changed: 108 additions & 4 deletions

File tree

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

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ private static void loadNativeLibrary(String libName, File cacheDirectory) throw
4141
if (os.contains("win")) {
4242
platform = "win-" + arch;
4343
extension = ".dll";
44-
if(libName.startsWith("lib")) {
45-
libName=libName.substring(3, libName.length());
44+
if (libName.startsWith("lib")) {
45+
libName = libName.substring(3, libName.length());
4646
}
4747
} else if (os.contains("mac")) {
4848
platform = "mac-" + arch;
@@ -97,14 +97,14 @@ private static void loadNativeLibrary(String libName, File cacheDirectory) throw
9797
} else {
9898
System.out.println("Copy not performed, already in cache");
9999
}
100-
System.out.println("Loading library "+libFile.getAbsolutePath());
100+
System.out.println("Loading library " + libFile.getAbsolutePath());
101101
System.load(libFile.getAbsolutePath());
102102
loaded = true;
103103
} catch (Exception e) {
104104
throw new RuntimeException("Failed to load: " + libName, e);
105105
}
106106
}
107-
107+
108108
public static void loadNativeLibrarys(File cacheDirectory) throws Exception {
109109
loadNativeLibrary("libmanifold", cacheDirectory);
110110
loadNativeLibrary("libmanifoldc", cacheDirectory);
@@ -468,6 +468,110 @@ public ManifoldBindings(File cacheDirectory) throws Exception {
468468

469469
}
470470

471+
// Data structure
472+
public record MeshData64(double[] vertices, long[] triangles, int vertCount, int triCount) {
473+
}
474+
475+
public MemorySegment importMeshGL64(double[] vertices, long[] triangles, long nVerts, long nTris) throws Throwable {
476+
MethodHandle mh = functions.get("manifold_meshgl64");
477+
if (mh == null)
478+
throw new RuntimeException("manifold_meshgl64 not found");
479+
480+
try (Arena arena = Arena.ofConfined()) {
481+
MemorySegment vertPtr = arena.allocate(3 * nVerts * Double.BYTES);
482+
vertPtr.copyFrom(MemorySegment.ofArray(vertices));
483+
484+
MemorySegment triPtr = arena.allocate(3 * nTris * Long.BYTES);
485+
triPtr.copyFrom(MemorySegment.ofArray(triangles));
486+
487+
MemorySegment meshGLmem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
488+
MemorySegment meshGL = null;
489+
490+
try {
491+
meshGL = (MemorySegment) mh.invoke(meshGLmem, vertPtr, nVerts, 3L, triPtr, nTris);
492+
493+
MemorySegment mergedMem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
494+
MemorySegment merged = null;
495+
496+
try {
497+
merged = (MemorySegment) functions.get("manifold_meshgl64_merge").invoke(mergedMem, meshGL);
498+
499+
MemorySegment manMem = (MemorySegment) functions.get("manifold_alloc_manifold").invoke();
500+
MemorySegment result = (MemorySegment) functions.get("manifold_of_meshgl64").invoke(manMem, merged);
501+
502+
try {
503+
functions.get("manifold_delete_meshgl64").invoke(merged);
504+
} catch (Throwable ignored) {
505+
}
506+
try {
507+
functions.get("manifold_delete_meshgl64").invoke(meshGL);
508+
} catch (Throwable ignored) {
509+
}
510+
511+
return result;
512+
513+
} catch (Throwable e) {
514+
if (merged != null)
515+
try {
516+
functions.get("manifold_delete_meshgl64").invoke(merged);
517+
} catch (Throwable ignored) {
518+
}
519+
throw e;
520+
}
521+
} catch (Throwable e) {
522+
if (meshGL != null)
523+
try {
524+
functions.get("manifold_delete_meshgl64").invoke(meshGL);
525+
} catch (Throwable ignored) {
526+
}
527+
throw e;
528+
}
529+
}
530+
}
531+
532+
public MeshData64 exportMeshGL64(MemorySegment manifold) throws Throwable {
533+
MemorySegment mem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
534+
MemorySegment meshGL = (MemorySegment) functions.get("manifold_get_meshgl64").invoke(mem, manifold);
535+
536+
try {
537+
long numVert = (long) functions.get("manifold_meshgl64_num_vert").invoke(meshGL);
538+
long numTri = (long) functions.get("manifold_meshgl64_num_tri").invoke(meshGL);
539+
long numProp = (long) functions.get("manifold_meshgl64_num_prop").invoke(meshGL);
540+
541+
double[] vertices = new double[(int) (3 * numVert)];
542+
long[] triangles = new long[(int) (3 * numTri)];
543+
544+
try (Arena temp = Arena.ofConfined()) {
545+
if (numVert > 0) {
546+
long vertLen = (long) functions.get("manifold_meshgl64_vert_properties_length").invoke(meshGL);
547+
MemorySegment tempMem = temp.allocate(vertLen * Double.BYTES);
548+
functions.get("manifold_meshgl64_vert_properties").invoke(tempMem, meshGL);
549+
550+
for (int i = 0; i < numVert; i++)
551+
for (int j = 0; (j < 3) && (j < numProp); j++)
552+
vertices[i * 3 + j] = tempMem.getAtIndex(ValueLayout.JAVA_DOUBLE, i * numProp + j);
553+
}
554+
555+
if (numTri > 0) {
556+
long triLen = (long) functions.get("manifold_meshgl64_tri_length").invoke(meshGL);
557+
MemorySegment tempMem = temp.allocate(triLen * Long.BYTES);
558+
functions.get("manifold_meshgl64_tri_verts").invoke(tempMem, meshGL);
559+
560+
for (int i = 0; i < triLen; i++)
561+
triangles[i] = tempMem.getAtIndex(ValueLayout.JAVA_LONG, i);
562+
}
563+
}
564+
565+
return new MeshData64(vertices, triangles, (int) numVert, (int) numTri);
566+
567+
} finally {
568+
try {
569+
functions.get("manifold_delete_meshgl64").invoke(meshGL);
570+
} catch (Throwable ignored) {
571+
}
572+
}
573+
}
574+
471575
// load
472576
private void load(String name, MemoryLayout returnLayout, MemoryLayout... argLayouts) {
473577
try {

0 commit comments

Comments
 (0)