antequ / fastlinkingnumbers

This is a curve topology verification tool based on Fast Linking Numbers for Topology Verification of Loopy Structures.
MIT License
26 stars 5 forks source link

Python wrappers #2

Open kburns opened 3 years ago

kburns commented 3 years ago

Thank you very much for making this fantastic code public! I think it has some great potential applications in analyzing numerical solutions to partial differential equations (e.g. fluid flow simulations). In this area, many people do data analysis in a language like Python, so I'm curious if you happen to have or might be interested in helping to produce Python wrappers for some of the functionality of the code? For example, wrappers that can directly take arrays describing a set of curves, and returning the linking numbers, rather than passing them through serialization to BCC files. I don't have much experience wrapping C++ code in Python but am happy to give it a shot, I just wanted to check if anyone on your team had already given this some thought. Thanks again!

antequ commented 3 years ago

Hi,

Thanks for asking about this! I don't have much experience with (or the bandwidth to add) python wrappers, so unfortunately I won't be able to do this. It is a very good suggestion that would be useful to a lot of people, and it would be great if anyone can help with this!

Here are two other solutions that you can try purely with python (they are not ideal, in that they might be more work than just writing wrappers):

1) You can try to write BCC files from your data in Python, then call verifycurves from the command line. This is likely the easiest solution. It's a simple binary file format; after the header, for each curve it's just the curve size (as a 32-bit integer) followed by the curve coordinates (as an array of single-precision floats, in this order: [v[0].x, v[0].y, v[0].z, v[1].x, v[1].y, v[1].z, v[2].x, ...]). See the bottom of this page for the file specification (make sure to use PL instead of C0 for bytes 4-5 if you are using polylines), and this page for a tutorial in C++ which you need to translate to python. Note that the second code section in the tutorial has a bug in it --- it should actually be something like this:

std::vector<cy::Vec3f> controlPoints( header.totalControlPointCount );
std::vector<int> firstControlPoint( header.curveCount + 1 );
std::vector<char> isCurveLoop( header.curveCount );
cy::Vec3f *cp = controlPoints.data();
int prevCP = 0;
for ( uint64_t i=0; i<header.curveCount; i++ ) {
    int curveControlPointCount;
    fread(&curveControlPointCount, sizeof(int), 1, pFile);
    isCurveLoop[i] = curveControlPointCount < 0;
    if ( curveControlPointCount < 0 ) curveControlPointCount = -curveControlPointCount;
    fread(cp, sizeof(cy::Vec3f), curveControlPointCount, pFile);
    cp += curveControlPointCount;
    firstControlPoint[i] = prevCP;
    prevCP += curveControlPointCount;
}
firstControlPoint[header.curveCount] = prevCP;

Alternately, you can just translate our code in Model::ExportBccFile here into Python.

2) You could output your vectors as line elements in the wavefront OBJ format (See here. Unfortunately I can't find any python OBJ libraries that output line elements from a quick web search), and then use our obj2bcc converter followed by verifycurves. You can write your own OBJ file writer in python; this might be just as hard as a BCC writer.

Good luck, and let me know if you are still stuck!

kburns commented 3 years ago

Ok, we've already written functions for writing BCC files from Python arrays, so we can definitely go that route for now, but of course it would be great to have wrappers that don't require serialization, eventually. We'll keep you updated!

saraedum commented 3 years ago

I don't know what this project does and did not have a look at the code base so feel free to ignore this comment.

I had great success recently when wrapping C++ libraries with cppyy. It usually only requires a tiny amount of glue code to work around a few things in C++ that cppyy cannot handle automatically. Maybe you want to try it out.