Skip to content

Cannot push to repository, bugs in CSGtoJavafx.java, additions for the bindings #77

@rondlh

Description

@rondlh

BTW: Big network issue here. I cannot push anything to JCSG (Authentication failed, it wants me to sign in on github Enterprise). I cannot even create a PR for it because it defaults to "https://github.com/NeuronRobotics/JCSG"
Spotless is a big additional headache we don't need!

CSGtoJavafx.java this one solves big inefficiencies, and bugs in finding the min/max bounds. Do we actually need/use the min/max?
CSGtoJavafx.java

The bindings need some additional code, 1 data structure and 2 functions:

// Data structure
public record MeshData64(double[] vertices, long[] triangles, int vertCount, int triCount) {}

public MemorySegment importMeshGL64(double[] vertices, long[] triangles, long nVerts, long nTris) throws Throwable {
	MethodHandle mh = functions.get("manifold_meshgl64");
	if (mh == null) throw new RuntimeException("manifold_meshgl64 not found");

	try (Arena arena = Arena.ofConfined()) {
		MemorySegment vertPtr = arena.allocate(3 * nVerts * Double.BYTES);
		vertPtr.copyFrom(MemorySegment.ofArray(vertices));
		
		MemorySegment triPtr = arena.allocate(3 * nTris * Long.BYTES);
		triPtr.copyFrom(MemorySegment.ofArray(triangles));

		MemorySegment meshGLmem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
		MemorySegment meshGL = null;
		
		try {
			meshGL = (MemorySegment) mh.invoke(meshGLmem, vertPtr, nVerts, 3L, triPtr, nTris);
			
			MemorySegment mergedMem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
			MemorySegment merged = null;
			
			try {
				merged = (MemorySegment) functions.get("manifold_meshgl64_merge").invoke(mergedMem, meshGL);
				
				MemorySegment manMem = (MemorySegment) functions.get("manifold_alloc_manifold").invoke();
				MemorySegment result = (MemorySegment) functions.get("manifold_of_meshgl64").invoke(manMem, merged);
				
				try { functions.get("manifold_delete_meshgl64").invoke(merged); } catch (Throwable ignored) {}
				try { functions.get("manifold_delete_meshgl64").invoke(meshGL); } catch (Throwable ignored) {}
				
				return result;
				
			} catch (Throwable e) {
				if (merged != null) try { functions.get("manifold_delete_meshgl64").invoke(merged); } catch (Throwable ignored) {}
				throw e;
			}
		} catch (Throwable e) {
			if (meshGL != null) try { functions.get("manifold_delete_meshgl64").invoke(meshGL); } catch (Throwable ignored) {}
			throw e;
		}
	}
}

public MeshData64 exportMeshGL64(MemorySegment manifold) throws Throwable {
	MemorySegment mem = (MemorySegment) functions.get("manifold_alloc_meshgl64").invoke();
	MemorySegment meshGL = (MemorySegment) functions.get("manifold_get_meshgl64").invoke(mem, manifold);

	try {
		long numVert = (long) functions.get("manifold_meshgl64_num_vert").invoke(meshGL);
		long numTri = (long) functions.get("manifold_meshgl64_num_tri").invoke(meshGL);
		long numProp = (long) functions.get("manifold_meshgl64_num_prop").invoke(meshGL);

		double[] vertices = new double[(int)(3 * numVert)];
		long[] triangles = new long[(int)(3 * numTri)];

		try (Arena temp = Arena.ofConfined()) {
			if (numVert > 0) {
				long vertLen = (long) functions.get("manifold_meshgl64_vert_properties_length").invoke(meshGL);
				MemorySegment tempMem = temp.allocate(vertLen * Double.BYTES);
				functions.get("manifold_meshgl64_vert_properties").invoke(tempMem, meshGL);
				
				for (int i = 0; i < numVert; i++)
					for (int j = 0; (j < 3) && (j < numProp); j++)
						vertices[i * 3 + j] = tempMem.getAtIndex(ValueLayout.JAVA_DOUBLE, i * numProp + j);
			}

			if (numTri > 0) {
				long triLen = (long) functions.get("manifold_meshgl64_tri_length").invoke(meshGL);
				MemorySegment tempMem = temp.allocate(triLen * Long.BYTES);
				functions.get("manifold_meshgl64_tri_verts").invoke(tempMem, meshGL);
				
				for (int i = 0; i < triLen; i++)
					triangles[i] = tempMem.getAtIndex(ValueLayout.JAVA_LONG, i);
			}
		}

		return new MeshData64(vertices, triangles, (int)numVert, (int)numTri);
		
	} finally {
		try { functions.get("manifold_delete_meshgl64").invoke(meshGL); } catch (Throwable ignored) {}
	}
}

CSG to Manifold and Manifold to CSG converter. Both single and double precision.
NOTE: Some sections are commented out so it compiles, the need the additional bindings code.
First add the code to the bindings, then uncomment the sections.
CSGManifoldConverter.java

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions