Skip to content

Commit

Permalink
SimplePolygon/Polygons accessors for C bindings (elalish#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffder authored Mar 15, 2023
1 parent 19022c9 commit 5a7c3b1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bindings/c/include/manifoldc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ ManifoldSimplePolygon *manifold_simple_polygon(void *mem, ManifoldVec2 *ps,
size_t length);
ManifoldPolygons *manifold_polygons(void *mem, ManifoldSimplePolygon **ps,
size_t length);
size_t manifold_simple_polygon_length(ManifoldSimplePolygon *p);
size_t manifold_polygons_length(ManifoldPolygons *ps);
size_t manifold_polygons_simple_length(ManifoldPolygons *ps, int idx);
ManifoldVec2 manifold_simple_polygon_get_point(ManifoldSimplePolygon *p,
int idx);
ManifoldSimplePolygon *manifold_polygons_get_simple(void *mem,
ManifoldPolygons *ps,
int idx);
ManifoldVec2 manifold_polygons_get_point(ManifoldPolygons *ps, int simple_idx,
int pt_idx);

// Mesh Construction
ManifoldMeshGL *manifold_meshgl(void *mem, float *vert_props, size_t n_verts,
Expand Down
31 changes: 31 additions & 0 deletions bindings/c/manifoldc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,47 @@ ManifoldPolygons *manifold_polygons(void *mem, ManifoldSimplePolygon **ps,
return to_c(vec);
}

size_t manifold_simple_polygon_length(ManifoldSimplePolygon *p) {
return from_c(p)->size();
}

size_t manifold_polygons_length(ManifoldPolygons *ps) {
return from_c(ps)->size();
}

size_t manifold_polygons_simple_length(ManifoldPolygons *ps, int idx) {
return (*from_c(ps))[idx].size();
}

ManifoldVec2 manifold_simple_polygon_get_point(ManifoldSimplePolygon *p,
int idx) {
return to_c((*from_c(p))[idx]);
}

ManifoldSimplePolygon *manifold_polygons_get_simple(void *mem,
ManifoldPolygons *ps,
int idx) {
auto sp = (*from_c(ps))[idx];
return to_c(new (mem) SimplePolygon(sp));
}

ManifoldVec2 manifold_polygons_get_point(ManifoldPolygons *ps, int simple_idx,
int pt_idx) {
return to_c((*from_c(ps))[simple_idx][pt_idx]);
}

ManifoldManifold *manifold_union(void *mem, ManifoldManifold *a,
ManifoldManifold *b) {
auto m = (*from_c(a)) + (*from_c(b));
return to_c(new (mem) Manifold(m));
}

ManifoldManifold *manifold_difference(void *mem, ManifoldManifold *a,
ManifoldManifold *b) {
auto m = (*from_c(a)) - (*from_c(b));
return to_c(new (mem) Manifold(m));
}

ManifoldManifold *manifold_intersection(void *mem, ManifoldManifold *a,
ManifoldManifold *b) {
auto m = (*from_c(a)) ^ (*from_c(b));
Expand Down
19 changes: 19 additions & 0 deletions test/manifoldc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,22 @@ TEST(CBIND, compose_decompose) {
manifold_delete_manifold(decomposed[i]);
}
}

TEST(CBIND, polygons) {
ManifoldVec2 vs[] = {{0, 0}, {1, 1}, {2, 2}};
ManifoldSimplePolygon *sp =
manifold_simple_polygon(malloc(manifold_simple_polygon_size()), vs, 3);
ManifoldSimplePolygon *sps[] = {sp};
ManifoldPolygons *ps =
manifold_polygons(malloc(manifold_polygons_size()), sps, 1);

EXPECT_EQ(vs[0].x, manifold_simple_polygon_get_point(sp, 0).x);
EXPECT_EQ(vs[1].x, manifold_simple_polygon_get_point(sp, 1).x);
EXPECT_EQ(vs[2].x, manifold_simple_polygon_get_point(sp, 2).x);
EXPECT_EQ(vs[0].x, manifold_polygons_get_point(ps, 0, 0).x);
EXPECT_EQ(vs[1].x, manifold_polygons_get_point(ps, 0, 1).x);
EXPECT_EQ(vs[2].x, manifold_polygons_get_point(ps, 0, 2).x);

manifold_delete_simple_polygon(sp);
manifold_delete_polygons(ps);
}

0 comments on commit 5a7c3b1

Please sign in to comment.