zalo / libfive-unity

A CSharp wrapper for libfive with Unity bindings
Mozilla Public License 2.0
103 stars 15 forks source link

Support for libfive_tree_render_slice #1

Closed andybak closed 3 years ago

andybak commented 5 years ago

Hi,

I've been trying to add support for libfive_tree_render_slice and libfive_tree_render_slice3 but I'm not experienced in interacting with native code. I understand the code for RenderMesh and tried basing it on this but I'm not clear how to get subsequent items in the libfive_contour or libfive_contours array. Would it be as simple as incrementing the pointer each time by the size of that item?

What would the size of a contour point be?

andybak commented 3 years ago

It's been a while now but I think my question was about the size of a contour point in the datastructure (in bytes). I just needed to increment the pointer the right amount. I could probably have figured it out with trial and error but I got a little discouraged by the lack of response as it probably meant the chances of getting a PR accepted were low.

zalo commented 3 years ago

Apologies; I forgot to turn notifications on for issues to this repo. 🙃

The existing datastructures and functions can be viewed in libfive.cs.

First thing's first make sure these are imported:

using System.Runtime.InteropServices;
using libfivesharp.libFiveInternal;

By the looks of what's already been exposed in libfive-unity, libfive_tree_render_slice returns a pointer which can be marshaled into a libfive_contours struct, like this:

IntPtr libFiveContoursPtr = libfive.libfive_tree_render_slice(tree, region, height, resolution);
libfive_contours libFiveContours = (libfive_contours)Marshal.PtrToStructure(libFiveContoursPtr, typeof(libfive_contours));

The "tricky" part, I suppose, was what you mentioned, marshalling the pointer-to-the-native-array-of-contours into a CSharp-array-of-contours:

libfive_contour[] contours = new libfive_contour[libFiveContours.count];
for(int i = 0; i < contours.Length; i++){
  contours[i] = (libfive_contour)Marshal.PtrToStructure(libFiveContours.cs + (i * Marshal.SizeOf(typeof(libfive_contour)), typeof(libfive_contour));
}

Since each contour contains an array of points (which are themselves just floats), the fastest way to get them into CSharp would be to just copy the floats over wholesale (x and y interleaved):

float[] contour0Floats = new float[contours[0].count * 2];
Marshal.Copy(contours[0].pts, contour0Floats, 0, contour0Floats.Length);

And then you can just deinterleave those in a for-loop:

Vector2[] contour0 = new Vector2[contour0Floats.Length/2];
for(int j = 0; j < contour0.Length; j++){
  contour0[j] = new Vector2(contour0Floats[(j*2)], contour0Floats[(j*2)+1]);
}

And lastly, we delete the native memory (since we've successfully copied it all into managed memory):

libfive.libfive_contours_delete(libFiveContoursPtr);

This prevents that native memory from sticking around forever (which is called a "memory leak").

I haven't tested any of this, but that's the general flow for rescuing data from pointers.