Skip to content

Commit ee014e4

Browse files
committed
move the import/export to the bottom
1 parent 792cbf2 commit ee014e4

1 file changed

Lines changed: 99 additions & 116 deletions

File tree

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

Lines changed: 99 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@
1313
import java.lang.foreign.MemorySegment;
1414
import java.nio.*;
1515
import java.nio.charset.StandardCharsets;
16-
import java.util.ArrayList;
17-
import java.util.List;
18-
import java.util.zip.*;
19-
import java.io.*;
20-
import java.lang.foreign.MemorySegment;
21-
import java.nio.*;
22-
import java.nio.charset.StandardCharsets;
23-
import java.util.ArrayList;
2416
import java.util.List;
2517
import java.util.zip.*;
2618

@@ -546,114 +538,6 @@ public ArrayList<double[][]> slice(MemorySegment m, double height) throws Throwa
546538
}
547539
}
548540

549-
private void deleteMeshGL64(MemorySegment seg) {
550-
if (seg == null)
551-
return;
552-
try {
553-
functions.get("manifold_delete_meshgl64").invoke(seg);
554-
} catch (Throwable ignored) {
555-
}
556-
}
557-
558-
// Data structure
559-
public record MeshData64(double[] vertices, long[] triangles, int vertCount, int triCount) {
560-
}
561-
562-
public MemorySegment importMeshGL64(double[] vertices, long[] triangles, long nVerts, long nTris) throws Throwable {
563-
MethodHandle mh = functions.get("manifold_meshgl64");
564-
if (mh == null)
565-
throw new RuntimeException("manifold_meshgl64 not found");
566-
567-
try (Arena arena = Arena.ofConfined()) {
568-
MemorySegment vertPtr = arena.allocate(3 * nVerts * Double.BYTES);
569-
vertPtr.copyFrom(MemorySegment.ofArray(vertices));
570-
571-
MemorySegment triPtr = arena.allocate(3 * nTris * Long.BYTES);
572-
triPtr.copyFrom(MemorySegment.ofArray(triangles));
573-
574-
MemorySegment meshGLmem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
575-
MemorySegment meshGL = null;
576-
577-
try {
578-
meshGL = (MemorySegment) mh.invoke(meshGLmem, vertPtr, nVerts, 3L, triPtr, nTris);
579-
580-
MemorySegment mergedMem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
581-
MemorySegment merged = null;
582-
583-
try {
584-
merged = (MemorySegment) functions.get("manifold_meshgl64_merge").invoke(mergedMem, meshGL);
585-
586-
MemorySegment manMem = (MemorySegment) functions.get("manifold_alloc_manifold").invoke();
587-
MemorySegment result = (MemorySegment) functions.get("manifold_of_meshgl64").invoke(manMem, merged);
588-
589-
try {
590-
functions.get("manifold_delete_meshgl64").invoke(meshGL);
591-
} catch (Throwable ignored) {
592-
}
593-
594-
return result;
595-
596-
} catch (Throwable e) {
597-
if (merged != null)
598-
try {
599-
functions.get("manifold_delete_meshgl64").invoke(merged);
600-
} catch (Throwable ignored) {
601-
}
602-
throw e;
603-
}
604-
} catch (Throwable e) {
605-
if (meshGL != null)
606-
try {
607-
functions.get("manifold_delete_meshgl64").invoke(meshGL);
608-
} catch (Throwable ignored) {
609-
}
610-
throw e;
611-
}
612-
}
613-
}
614-
615-
public MeshData64 exportMeshGL64(MemorySegment manifold) throws Throwable {
616-
MemorySegment mem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
617-
MemorySegment meshGL = (MemorySegment) functions.get("manifold_get_meshgl64").invoke(mem, manifold);
618-
619-
try {
620-
long numVert = (long) functions.get("manifold_meshgl64_num_vert").invoke(meshGL);
621-
long numTri = (long) functions.get("manifold_meshgl64_num_tri").invoke(meshGL);
622-
long numProp = (long) functions.get("manifold_meshgl64_num_prop").invoke(meshGL);
623-
624-
double[] vertices = new double[(int) (3 * numVert)];
625-
long[] triangles = new long[(int) (3 * numTri)];
626-
627-
try (Arena temp = Arena.ofConfined()) {
628-
if (numVert > 0) {
629-
long vertLen = (long) functions.get("manifold_meshgl64_vert_properties_length").invoke(meshGL);
630-
MemorySegment tempMem = temp.allocate(vertLen * Double.BYTES);
631-
functions.get("manifold_meshgl64_vert_properties").invoke(tempMem, meshGL);
632-
633-
for (int i = 0; i < numVert; i++)
634-
for (int j = 0; (j < 3) && (j < numProp); j++)
635-
vertices[i * 3 + j] = tempMem.getAtIndex(ValueLayout.JAVA_DOUBLE, i * numProp + j);
636-
}
637-
638-
if (numTri > 0) {
639-
long triLen = (long) functions.get("manifold_meshgl64_tri_length").invoke(meshGL);
640-
MemorySegment tempMem = temp.allocate(triLen * Long.BYTES);
641-
functions.get("manifold_meshgl64_tri_verts").invoke(tempMem, meshGL);
642-
643-
for (int i = 0; i < triLen; i++)
644-
triangles[i] = tempMem.getAtIndex(ValueLayout.JAVA_LONG, i);
645-
}
646-
}
647-
648-
return new MeshData64(vertices, triangles, (int) numVert, (int) numTri);
649-
650-
} finally {
651-
try {
652-
functions.get("manifold_delete_meshgl64").invoke(meshGL);
653-
} catch (Throwable ignored) {
654-
}
655-
}
656-
}
657541

658542
// load
659543
private void load(String name, MemoryLayout returnLayout, MemoryLayout... argLayouts) {
@@ -1871,5 +1755,104 @@ private static String attrString(String tag, String attr) {
18711755
/** Flat, unindexed or indexed mesh arrays used only during file parsing. */
18721756
private record RawMesh(float[] vertices, int[] triangles) {
18731757
}
1758+
// Data structure
1759+
public record MeshData64(double[] vertices, long[] triangles, int vertCount, int triCount) {
1760+
}
1761+
1762+
public MemorySegment importMeshGL64(double[] vertices, long[] triangles, long nVerts, long nTris) throws Throwable {
1763+
MethodHandle mh = functions.get("manifold_meshgl64");
1764+
if (mh == null)
1765+
throw new RuntimeException("manifold_meshgl64 not found");
1766+
1767+
try (Arena arena = Arena.ofConfined()) {
1768+
MemorySegment vertPtr = arena.allocate(3 * nVerts * Double.BYTES);
1769+
vertPtr.copyFrom(MemorySegment.ofArray(vertices));
1770+
1771+
MemorySegment triPtr = arena.allocate(3 * nTris * Long.BYTES);
1772+
triPtr.copyFrom(MemorySegment.ofArray(triangles));
1773+
1774+
MemorySegment meshGLmem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
1775+
MemorySegment meshGL = null;
1776+
1777+
try {
1778+
meshGL = (MemorySegment) mh.invoke(meshGLmem, vertPtr, nVerts, 3L, triPtr, nTris);
1779+
1780+
MemorySegment mergedMem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
1781+
MemorySegment merged = null;
1782+
1783+
try {
1784+
merged = (MemorySegment) functions.get("manifold_meshgl64_merge").invoke(mergedMem, meshGL);
1785+
1786+
MemorySegment manMem = (MemorySegment) functions.get("manifold_alloc_manifold").invoke();
1787+
MemorySegment result = (MemorySegment) functions.get("manifold_of_meshgl64").invoke(manMem, merged);
1788+
1789+
try {
1790+
functions.get("manifold_delete_meshgl64").invoke(meshGL);
1791+
} catch (Throwable ignored) {
1792+
}
1793+
1794+
return result;
1795+
1796+
} catch (Throwable e) {
1797+
if (merged != null)
1798+
try {
1799+
functions.get("manifold_delete_meshgl64").invoke(merged);
1800+
} catch (Throwable ignored) {
1801+
}
1802+
throw e;
1803+
}
1804+
} catch (Throwable e) {
1805+
if (meshGL != null)
1806+
try {
1807+
functions.get("manifold_delete_meshgl64").invoke(meshGL);
1808+
} catch (Throwable ignored) {
1809+
}
1810+
throw e;
1811+
}
1812+
}
1813+
}
1814+
1815+
public MeshData64 exportMeshGL64(MemorySegment manifold) throws Throwable {
1816+
MemorySegment mem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
1817+
MemorySegment meshGL = (MemorySegment) functions.get("manifold_get_meshgl64").invoke(mem, manifold);
1818+
1819+
try {
1820+
long numVert = (long) functions.get("manifold_meshgl64_num_vert").invoke(meshGL);
1821+
long numTri = (long) functions.get("manifold_meshgl64_num_tri").invoke(meshGL);
1822+
long numProp = (long) functions.get("manifold_meshgl64_num_prop").invoke(meshGL);
1823+
1824+
double[] vertices = new double[(int) (3 * numVert)];
1825+
long[] triangles = new long[(int) (3 * numTri)];
1826+
1827+
try (Arena temp = Arena.ofConfined()) {
1828+
if (numVert > 0) {
1829+
long vertLen = (long) functions.get("manifold_meshgl64_vert_properties_length").invoke(meshGL);
1830+
MemorySegment tempMem = temp.allocate(vertLen * Double.BYTES);
1831+
functions.get("manifold_meshgl64_vert_properties").invoke(tempMem, meshGL);
1832+
1833+
for (int i = 0; i < numVert; i++)
1834+
for (int j = 0; (j < 3) && (j < numProp); j++)
1835+
vertices[i * 3 + j] = tempMem.getAtIndex(ValueLayout.JAVA_DOUBLE, i * numProp + j);
1836+
}
1837+
1838+
if (numTri > 0) {
1839+
long triLen = (long) functions.get("manifold_meshgl64_tri_length").invoke(meshGL);
1840+
MemorySegment tempMem = temp.allocate(triLen * Long.BYTES);
1841+
functions.get("manifold_meshgl64_tri_verts").invoke(tempMem, meshGL);
1842+
1843+
for (int i = 0; i < triLen; i++)
1844+
triangles[i] = tempMem.getAtIndex(ValueLayout.JAVA_LONG, i);
1845+
}
1846+
}
1847+
1848+
return new MeshData64(vertices, triangles, (int) numVert, (int) numTri);
1849+
1850+
} finally {
1851+
try {
1852+
functions.get("manifold_delete_meshgl64").invoke(meshGL);
1853+
} catch (Throwable ignored) {
1854+
}
1855+
}
1856+
}
18741857

18751858
}

0 commit comments

Comments
 (0)