Skip to content

Commit

Permalink
bindings update (#1018)
Browse files Browse the repository at this point in the history
* bindings: add MeshGL64 C bindings

* bindings: fix leak and address sanitizer issue

* bindings: add Mesh64 to python

* bindings: fix c binding 64 bit index type

* bindings: fix python 64 bit index type

* bindings: fix python again

* bindings: formatting
  • Loading branch information
pca006132 authored Nov 2, 2024
1 parent a8a90c7 commit f11ed8d
Show file tree
Hide file tree
Showing 8 changed files with 529 additions and 107 deletions.
8 changes: 8 additions & 0 deletions bindings/c/conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ ManifoldMeshGL *to_c(manifold::MeshGL *m) {
return reinterpret_cast<ManifoldMeshGL *>(m);
}

ManifoldMeshGL64 *to_c(manifold::MeshGL64 *m) {
return reinterpret_cast<ManifoldMeshGL64 *>(m);
}

ManifoldOpType to_c(manifold::OpType optype) {
ManifoldOpType op = MANIFOLD_ADD;
switch (optype) {
Expand Down Expand Up @@ -152,6 +156,10 @@ const manifold::MeshGL *from_c(ManifoldMeshGL *m) {
return reinterpret_cast<manifold::MeshGL const *>(m);
}

const manifold::MeshGL64 *from_c(ManifoldMeshGL64 *m) {
return reinterpret_cast<manifold::MeshGL64 const *>(m);
}

OpType from_c(ManifoldOpType optype) {
auto op = OpType::Add;
switch (optype) {
Expand Down
2 changes: 2 additions & 0 deletions bindings/c/conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ManifoldCrossSectionVec *to_c(CrossSectionVec *csv);
ManifoldSimplePolygon *to_c(manifold::SimplePolygon *p);
ManifoldPolygons *to_c(manifold::Polygons *ps);
ManifoldMeshGL *to_c(manifold::MeshGL *m);
ManifoldMeshGL64 *to_c(manifold::MeshGL64 *m);
ManifoldBox *to_c(manifold::Box *m);
ManifoldRect *to_c(manifold::Rect *m);
ManifoldError to_c(manifold::Manifold::Error error);
Expand All @@ -47,6 +48,7 @@ CrossSectionVec *from_c(ManifoldCrossSectionVec *csv);
const manifold::SimplePolygon *from_c(ManifoldSimplePolygon *m);
const manifold::Polygons *from_c(ManifoldPolygons *m);
const manifold::MeshGL *from_c(ManifoldMeshGL *m);
const manifold::MeshGL64 *from_c(ManifoldMeshGL64 *m);
OpType from_c(ManifoldOpType op);
CrossSection::FillRule from_c(ManifoldFillRule fillrule);
CrossSection::JoinType from_c(ManifoldJoinType jt);
Expand Down
53 changes: 46 additions & 7 deletions bindings/c/include/manifold/manifoldc.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,32 @@ ManifoldMeshGL *manifold_get_meshgl(void *mem, ManifoldManifold *m);
ManifoldMeshGL *manifold_meshgl_copy(void *mem, ManifoldMeshGL *m);
ManifoldMeshGL *manifold_meshgl_merge(void *mem, ManifoldMeshGL *m);

ManifoldMeshGL64 *manifold_meshgl64(void *mem, double *vert_props,
size_t n_verts, size_t n_props,
size_t *tri_verts, size_t n_tris);

ManifoldMeshGL64 *manifold_meshgl64_w_tangents(void *mem, double *vert_props,
size_t n_verts, size_t n_props,
size_t *tri_verts, size_t n_tris,
double *halfedge_tangent);
ManifoldMeshGL64 *manifold_get_meshgl64(void *mem, ManifoldManifold *m);
ManifoldMeshGL64 *manifold_meshgl64_copy(void *mem, ManifoldMeshGL64 *m);
ManifoldMeshGL64 *manifold_meshgl64_merge(void *mem, ManifoldMeshGL64 *m);

// SDF
// By default, the execution policy (sequential or parallel) of
// manifold_level_set will be chosen automatically depending on the size of the
// job and whether Manifold has been compiled with a PAR backend. If you are
// using these bindings from a language that has a runtime lock preventing the
// parallel execution of closures, then you should use manifold_level_set_seq to
// force sequential execution.
ManifoldManifold *manifold_level_set(void *mem,
double (*sdf)(double, double, double,
void *),
ManifoldManifold *manifold_level_set(void *mem, ManifoldSdf sdf,
ManifoldBox *bounds, double edge_length,
double level, double tolerance, void *ctx);
ManifoldManifold *manifold_level_set_seq(
void *mem, double (*sdf)(double, double, double, void *),
ManifoldBox *bounds, double edge_length, double level, double tolerance,
void *ctx);
ManifoldManifold *manifold_level_set_seq(void *mem, ManifoldSdf sdf,
ManifoldBox *bounds,
double edge_length, double level,
double tolerance, void *ctx);

// Manifold Vectors

Expand Down Expand Up @@ -158,9 +168,13 @@ ManifoldManifold *manifold_cylinder(void *mem, double height, double radius_low,
ManifoldManifold *manifold_sphere(void *mem, double radius,
int circular_segments);
ManifoldManifold *manifold_of_meshgl(void *mem, ManifoldMeshGL *mesh);
ManifoldManifold *manifold_of_meshgl64(void *mem, ManifoldMeshGL64 *mesh);
ManifoldManifold *manifold_smooth(void *mem, ManifoldMeshGL *mesh,
size_t *half_edges, double *smoothness,
size_t n_idxs);
ManifoldManifold *manifold_smooth64(void *mem, ManifoldMeshGL64 *mesh,
size_t *half_edges, double *smoothness,
size_t n_idxs);
ManifoldManifold *manifold_extrude(void *mem, ManifoldPolygons *cs,
double height, int slices,
double twist_degrees, double scale_x,
Expand Down Expand Up @@ -378,6 +392,27 @@ float *manifold_meshgl_run_transform(void *mem, ManifoldMeshGL *m);
uint32_t *manifold_meshgl_face_id(void *mem, ManifoldMeshGL *m);
float *manifold_meshgl_halfedge_tangent(void *mem, ManifoldMeshGL *m);

size_t manifold_meshgl64_num_prop(ManifoldMeshGL64 *m);
size_t manifold_meshgl64_num_vert(ManifoldMeshGL64 *m);
size_t manifold_meshgl64_num_tri(ManifoldMeshGL64 *m);
size_t manifold_meshgl64_vert_properties_length(ManifoldMeshGL64 *m);
size_t manifold_meshgl64_tri_length(ManifoldMeshGL64 *m);
size_t manifold_meshgl64_merge_length(ManifoldMeshGL64 *m);
size_t manifold_meshgl64_run_index_length(ManifoldMeshGL64 *m);
size_t manifold_meshgl64_run_original_id_length(ManifoldMeshGL64 *m);
size_t manifold_meshgl64_run_transform_length(ManifoldMeshGL64 *m);
size_t manifold_meshgl64_face_id_length(ManifoldMeshGL64 *m);
size_t manifold_meshgl64_tangent_length(ManifoldMeshGL64 *m);
double *manifold_meshgl64_vert_properties(void *mem, ManifoldMeshGL64 *m);
size_t *manifold_meshgl64_tri_verts(void *mem, ManifoldMeshGL64 *m);
size_t *manifold_meshgl64_merge_from_vert(void *mem, ManifoldMeshGL64 *m);
size_t *manifold_meshgl64_merge_to_vert(void *mem, ManifoldMeshGL64 *m);
size_t *manifold_meshgl64_run_index(void *mem, ManifoldMeshGL64 *m);
uint32_t *manifold_meshgl64_run_original_id(void *mem, ManifoldMeshGL64 *m);
double *manifold_meshgl64_run_transform(void *mem, ManifoldMeshGL64 *m);
size_t *manifold_meshgl64_face_id(void *mem, ManifoldMeshGL64 *m);
double *manifold_meshgl64_halfedge_tangent(void *mem, ManifoldMeshGL64 *m);

// memory size

size_t manifold_manifold_size();
Expand All @@ -388,6 +423,7 @@ size_t manifold_simple_polygon_size();
size_t manifold_polygons_size();
size_t manifold_manifold_pair_size();
size_t manifold_meshgl_size();
size_t manifold_meshgl64_size();
size_t manifold_box_size();
size_t manifold_rect_size();
size_t manifold_curvature_size();
Expand All @@ -401,6 +437,7 @@ ManifoldCrossSectionVec *manifold_alloc_cross_section_vec();
ManifoldSimplePolygon *manifold_alloc_simple_polygon();
ManifoldPolygons *manifold_alloc_polygons();
ManifoldMeshGL *manifold_alloc_meshgl();
ManifoldMeshGL64 *manifold_alloc_meshgl64();
ManifoldBox *manifold_alloc_box();
ManifoldRect *manifold_alloc_rect();

Expand All @@ -413,6 +450,7 @@ void manifold_destruct_cross_section_vec(ManifoldCrossSectionVec *csv);
void manifold_destruct_simple_polygon(ManifoldSimplePolygon *p);
void manifold_destruct_polygons(ManifoldPolygons *p);
void manifold_destruct_meshgl(ManifoldMeshGL *m);
void manifold_destruct_meshgl64(ManifoldMeshGL64 *m);
void manifold_destruct_box(ManifoldBox *b);
void manifold_destruct_rect(ManifoldRect *b);

Expand All @@ -425,6 +463,7 @@ void manifold_delete_cross_section_vec(ManifoldCrossSectionVec *csv);
void manifold_delete_simple_polygon(ManifoldSimplePolygon *p);
void manifold_delete_polygons(ManifoldPolygons *p);
void manifold_delete_meshgl(ManifoldMeshGL *m);
void manifold_delete_meshgl64(ManifoldMeshGL64 *m);
void manifold_delete_box(ManifoldBox *b);
void manifold_delete_rect(ManifoldRect *b);

Expand Down
4 changes: 4 additions & 0 deletions bindings/c/include/manifold/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct ManifoldCrossSectionVec ManifoldCrossSectionVec;
typedef struct ManifoldSimplePolygon ManifoldSimplePolygon;
typedef struct ManifoldPolygons ManifoldPolygons;
typedef struct ManifoldMeshGL ManifoldMeshGL;
typedef struct ManifoldMeshGL64 ManifoldMeshGL64;
typedef struct ManifoldBox ManifoldBox;
typedef struct ManifoldRect ManifoldRect;

Expand Down Expand Up @@ -103,3 +104,6 @@ typedef enum ManifoldJoinType {
MANIFOLD_JOIN_TYPE_ROUND,
MANIFOLD_JOIN_TYPE_MITER,
} ManifoldJoinType;

// function pointer
typedef double (*ManifoldSdf)(double, double, double, void*);
150 changes: 130 additions & 20 deletions bindings/c/manifoldc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,28 @@ ManifoldMeshGL *manifold_meshgl_w_tangents(void *mem, float *vert_props,
return to_c(mesh);
}

ManifoldMeshGL64 *manifold_meshgl64(void *mem, double *vert_props,
size_t n_verts, size_t n_props,
size_t *tri_verts, size_t n_tris) {
auto mesh = new (mem) MeshGL64();
mesh->numProp = n_props;
mesh->vertProperties = vector_of_array(vert_props, n_verts * n_props);
mesh->triVerts = vector_of_array(tri_verts, n_tris * 3);
return to_c(mesh);
}

ManifoldMeshGL64 *manifold_meshgl64_w_tangents(void *mem, double *vert_props,
size_t n_verts, size_t n_props,
size_t *tri_verts, size_t n_tris,
double *halfedge_tangent) {
auto mesh = new (mem) MeshGL64();
mesh->numProp = n_props;
mesh->vertProperties = vector_of_array(vert_props, n_verts * n_props);
mesh->triVerts = vector_of_array(tri_verts, n_tris * 3);
mesh->halfedgeTangent = vector_of_array(halfedge_tangent, n_tris * 3 * 4);
return to_c(mesh);
}

ManifoldManifold *manifold_smooth(void *mem, ManifoldMeshGL *mesh,
size_t *half_edges, double *smoothness,
size_t n_edges) {
Expand All @@ -374,11 +396,27 @@ ManifoldManifold *manifold_smooth(void *mem, ManifoldMeshGL *mesh,
return to_c(new (mem) Manifold(m));
}

ManifoldManifold *manifold_smooth64(void *mem, ManifoldMeshGL64 *mesh,
size_t *half_edges, double *smoothness,
size_t n_edges) {
auto smooth = std::vector<Smoothness>();
for (size_t i = 0; i < n_edges; ++i) {
smooth.push_back({half_edges[i], smoothness[i]});
}
auto m = Manifold::Smooth(*from_c(mesh), smooth);
return to_c(new (mem) Manifold(m));
}

ManifoldManifold *manifold_of_meshgl(void *mem, ManifoldMeshGL *mesh) {
auto m = Manifold(*from_c(mesh));
return to_c(new (mem) Manifold(m));
}

ManifoldManifold *manifold_of_meshgl64(void *mem, ManifoldMeshGL64 *mesh) {
auto m = Manifold(*from_c(mesh));
return to_c(new (mem) Manifold(m));
}

ManifoldManifold *manifold_extrude(void *mem, ManifoldPolygons *cs,
double height, int slices,
double twist_degrees, double scale_x,
Expand Down Expand Up @@ -422,6 +460,24 @@ ManifoldMeshGL *manifold_meshgl_merge(void *mem, ManifoldMeshGL *m) {
return m;
}

ManifoldMeshGL64 *manifold_get_meshgl64(void *mem, ManifoldManifold *m) {
auto mesh = from_c(m)->GetMeshGL64();
return to_c(new (mem) MeshGL64(mesh));
}

ManifoldMeshGL64 *manifold_meshgl64_copy(void *mem, ManifoldMeshGL64 *m) {
return to_c(new (mem) MeshGL64(*from_c(m)));
}

ManifoldMeshGL64 *manifold_meshgl64_merge(void *mem, ManifoldMeshGL64 *m) {
auto duplicate = new (mem) MeshGL64(*from_c(m));
if (duplicate->Merge()) {
return to_c(duplicate);
}
delete duplicate;
return m;
}

int manifold_meshgl_num_prop(ManifoldMeshGL *m) { return from_c(m)->numProp; }
int manifold_meshgl_num_vert(ManifoldMeshGL *m) { return from_c(m)->NumVert(); }
int manifold_meshgl_num_tri(ManifoldMeshGL *m) { return from_c(m)->NumTri(); }
Expand Down Expand Up @@ -494,6 +550,67 @@ float *manifold_meshgl_halfedge_tangent(void *mem, ManifoldMeshGL *m) {
return copy_data(mem, from_c(m)->halfedgeTangent);
}

size_t manifold_meshgl64_num_prop(ManifoldMeshGL64 *m) {
return from_c(m)->numProp;
}
size_t manifold_meshgl64_num_vert(ManifoldMeshGL64 *m) {
return from_c(m)->NumVert();
}
size_t manifold_meshgl64_num_tri(ManifoldMeshGL64 *m) {
return from_c(m)->NumTri();
}
size_t manifold_meshgl64_vert_properties_length(ManifoldMeshGL64 *m) {
return from_c(m)->vertProperties.size();
}
size_t manifold_meshgl64_tri_length(ManifoldMeshGL64 *m) {
return from_c(m)->triVerts.size();
}
size_t manifold_meshgl64_merge_length(ManifoldMeshGL64 *m) {
return from_c(m)->mergeFromVert.size();
}
size_t manifold_meshgl64_run_index_length(ManifoldMeshGL64 *m) {
return from_c(m)->runIndex.size();
}
size_t manifold_meshgl64_run_original_id_length(ManifoldMeshGL64 *m) {
return from_c(m)->runOriginalID.size();
}
size_t manifold_meshgl64_run_transform_length(ManifoldMeshGL64 *m) {
return from_c(m)->runTransform.size();
}
size_t manifold_meshgl64_face_id_length(ManifoldMeshGL64 *m) {
return from_c(m)->faceID.size();
}
size_t manifold_meshgl64_tangent_length(ManifoldMeshGL64 *m) {
return from_c(m)->halfedgeTangent.size();
}
double *manifold_meshgl64_vert_properties(void *mem, ManifoldMeshGL64 *m) {
return copy_data(mem, from_c(m)->vertProperties);
}
size_t *manifold_meshgl64_tri_verts(void *mem, ManifoldMeshGL64 *m) {
return copy_data(mem, from_c(m)->triVerts);
}
size_t *manifold_meshgl64_merge_from_vert(void *mem, ManifoldMeshGL64 *m) {
return copy_data(mem, from_c(m)->mergeFromVert);
}
size_t *manifold_meshgl64_merge_to_vert(void *mem, ManifoldMeshGL64 *m) {
return copy_data(mem, from_c(m)->mergeToVert);
}
size_t *manifold_meshgl64_run_index(void *mem, ManifoldMeshGL64 *m) {
return copy_data(mem, from_c(m)->runIndex);
}
uint32_t *manifold_meshgl64_run_original_id(void *mem, ManifoldMeshGL64 *m) {
return copy_data(mem, from_c(m)->runOriginalID);
}
double *manifold_meshgl64_run_transform(void *mem, ManifoldMeshGL64 *m) {
return copy_data(mem, from_c(m)->runTransform);
}
size_t *manifold_meshgl64_face_id(void *mem, ManifoldMeshGL64 *m) {
return copy_data(mem, from_c(m)->faceID);
}
double *manifold_meshgl64_halfedge_tangent(void *mem, ManifoldMeshGL64 *m) {
return copy_data(mem, from_c(m)->halfedgeTangent);
}

ManifoldManifold *manifold_as_original(void *mem, ManifoldManifold *m) {
auto orig = from_c(m)->AsOriginal();
return to_c(new (mem) Manifold(orig));
Expand Down Expand Up @@ -594,42 +711,33 @@ size_t manifold_manifold_size() { return sizeof(Manifold); }
size_t manifold_manifold_vec_size() { return sizeof(std::vector<Manifold>); }
size_t manifold_manifold_pair_size() { return sizeof(ManifoldManifoldPair); }
size_t manifold_meshgl_size() { return sizeof(MeshGL); }
size_t manifold_meshgl64_size() { return sizeof(MeshGL64); }
size_t manifold_box_size() { return sizeof(Box); }
size_t manifold_rect_size() { return sizeof(Rect); }

// allocation
ManifoldManifold *manifold_alloc_manifold() {
return to_c(static_cast<Manifold *>(malloc(manifold_manifold_size())));
return to_c(new manifold::Manifold);
}
ManifoldManifoldVec *manifold_alloc_manifold_vec() {
return to_c(static_cast<std::vector<Manifold> *>(
malloc(manifold_manifold_vec_size())));
return to_c(new std::vector<manifold::Manifold>);
}
ManifoldCrossSection *manifold_alloc_cross_section() {
return to_c(
static_cast<CrossSection *>(malloc(manifold_cross_section_size())));
return to_c(new CrossSection);
}
ManifoldCrossSectionVec *manifold_alloc_cross_section_vec() {
return to_c(static_cast<std::vector<CrossSection> *>(
malloc(manifold_cross_section_vec_size())));
return to_c(new std::vector<CrossSection>);
}
ManifoldSimplePolygon *manifold_alloc_simple_polygon() {
return to_c(
static_cast<SimplePolygon *>(malloc(manifold_simple_polygon_size())));
return to_c(new SimplePolygon);
}
ManifoldPolygons *manifold_alloc_polygons() {
return to_c(static_cast<std::vector<SimplePolygon> *>(
malloc(manifold_polygons_size())));
}
ManifoldMeshGL *manifold_alloc_meshgl() {
return to_c(static_cast<MeshGL *>(malloc(manifold_meshgl_size())));
}
ManifoldBox *manifold_alloc_box() {
return to_c(static_cast<Box *>(malloc(manifold_box_size())));
}
ManifoldRect *manifold_alloc_rect() {
return to_c(static_cast<Rect *>(malloc(manifold_rect_size())));
return to_c(new std::vector<SimplePolygon>);
}
ManifoldMeshGL *manifold_alloc_meshgl() { return to_c(new MeshGL); }
ManifoldMeshGL64 *manifold_alloc_meshgl64() { return to_c(new MeshGL64); }
ManifoldBox *manifold_alloc_box() { return to_c(new Box); }
ManifoldRect *manifold_alloc_rect() { return to_c(new Rect); }

// pointer free + destruction
void manifold_delete_cross_section(ManifoldCrossSection *c) {
Expand All @@ -647,6 +755,7 @@ void manifold_delete_manifold_vec(ManifoldManifoldVec *ms) {
delete from_c(ms);
}
void manifold_delete_meshgl(ManifoldMeshGL *m) { delete from_c(m); }
void manifold_delete_meshgl64(ManifoldMeshGL64 *m) { delete from_c(m); }
void manifold_delete_box(ManifoldBox *b) { delete from_c(b); }
void manifold_delete_rect(ManifoldRect *r) { delete from_c(r); }

Expand All @@ -666,6 +775,7 @@ void manifold_destruct_manifold_vec(ManifoldManifoldVec *ms) {
from_c(ms)->~ManifoldVec();
}
void manifold_destruct_meshgl(ManifoldMeshGL *m) { from_c(m)->~MeshGL(); }
void manifold_destruct_meshgl64(ManifoldMeshGL64 *m) { from_c(m)->~MeshGL64(); }
void manifold_destruct_box(ManifoldBox *b) { from_c(b)->~Box(); }
void manifold_destruct_rect(ManifoldRect *r) { from_c(r)->~Rect(); }

Expand Down
2 changes: 2 additions & 0 deletions bindings/python/examples/all_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def all_manifold():
m = Manifold.cube()
m2 = Manifold.cube().translate([2, 0, 0])
d = m.min_gap(m2, 2)
mesh2 = m.to_mesh64()
ok = mesh.merge()


def run():
Expand Down
Loading

0 comments on commit f11ed8d

Please sign in to comment.