Coder Social home page Coder Social logo

Comments (2)

andybak avatar andybak commented on May 31, 2024

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.

from libfive-unity.

zalo avatar zalo commented on May 31, 2024

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.

from libfive-unity.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.