star-cpr

Clip 2D meshes with 2D polygons
git clone git://git.meso-star.fr/star-cpr.git
Log | Files | Refs | README | LICENSE

commit 805b57fd650b1c9811aa221a4e9bc68d757b1327
parent 75448a6615b0153519dd5ccdf65e3ed3f8f3f11b
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Tue,  4 Oct 2022 18:39:54 +0200

Add a test on polygons with hole offset

Diffstat:
Msrc/test_scpr_offset.c | 128+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 128 insertions(+), 0 deletions(-)

diff --git a/src/test_scpr_offset.c b/src/test_scpr_offset.c @@ -224,11 +224,139 @@ test_double(void) CHK(mem_allocated_size() == 0); } +static void +test_internal(void) +{ + const double coords0[] = { + -10, -10, + -10, 10, + 10, 10, + 10, -10 + }; + const double coords1[] = { + -5, -5, + 5, -5, + 5, 5, + -5, 5 + }; + const double coords2[] = { + -9, -9, + -9, 9, + 9, 9, + 9, -9 + }; + const double coords3[] = { + -6, -6, + 6, -6, + 6, 6, + -6, 6 + }; + const double coords4[] = { + -15, -15, + -15, 15, + 15, 15, + 15, -15 + }; + double** coords; + size_t nverts[] = { 4, 4 }; + size_t ncomps = 2; + struct mem_allocator allocator; + struct polygon_context ctx; + struct scpr_polygon* polygon; + struct scpr_polygon* expected; + int eq; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + + coords = MEM_CALLOC(&allocator, ncomps, sizeof(*coords)); + *coords = MEM_CALLOC(&allocator, nverts[0], 2*sizeof(**coords)); + *(coords+1) = MEM_CALLOC(&allocator, nverts[1], 2*sizeof(**coords)); + memcpy(*coords, coords0, 2*nverts[0]*sizeof(**coords)); + memcpy(*(coords+1), coords1, 2*nverts[1]*sizeof(**coords)); + + CHK(scpr_polygon_create(&allocator, &polygon) == RES_OK); + CHK(scpr_polygon_create(&allocator, &expected) == RES_OK); + + ctx.coords = coords; + ctx.nverts = nverts; + ctx.ncomps = ncomps; + + CHK(scpr_polygon_setup_indexed_vertices(polygon, ncomps, pget_nverts, pget_pos, &ctx) + == RES_OK); + CHK(scpr_polygon_setup_indexed_vertices(expected, ncomps, pget_nverts, pget_pos, &ctx) + == RES_OK); + + /* Offset 0 = unchanged */ + CHK(scpr_offset_polygon(polygon, 0, SCPR_JOIN_MITER) == RES_OK); + CHK(scpr_polygon_eq(polygon, expected, &eq) == RES_OK); + CHK(eq); + + /* Offset -1 */ + memcpy(*coords, coords3, 2*nverts[0]*sizeof(**coords)); + memcpy(*(coords+1), coords2, 2*nverts[1]*sizeof(**coords)); + CHK(scpr_polygon_setup_indexed_vertices(expected, ncomps, pget_nverts, pget_pos, &ctx) + == RES_OK); + + CHK(scpr_offset_polygon(polygon, -1, SCPR_JOIN_MITER) == RES_OK); + CHK(scpr_polygon_eq(polygon, expected, &eq) == RES_OK); + CHK(eq); + + /* Offset 1: back to original polygon */ + memcpy(*coords, coords0, 2*nverts[0]*sizeof(**coords)); + memcpy(*(coords+1), coords1, 2*nverts[1]*sizeof(**coords)); + CHK(scpr_polygon_setup_indexed_vertices(expected, ncomps, pget_nverts, pget_pos, &ctx) + == RES_OK); + + CHK(scpr_offset_polygon(polygon, 1, SCPR_JOIN_MITER) == RES_OK); + CHK(scpr_polygon_eq(polygon, expected, &eq) == RES_OK); + CHK(eq); + + /* Offset 5: internal path disappears */ + ncomps = 1; + ctx.ncomps = ncomps; + memcpy(*coords, coords4, 2*nverts[0]*sizeof(**coords)); + CHK(scpr_polygon_setup_indexed_vertices(expected, ncomps, pget_nverts, pget_pos, &ctx) + == RES_OK); + + CHK(scpr_offset_polygon(polygon, 5, SCPR_JOIN_MITER) == RES_OK); + CHK(scpr_polygon_eq(polygon, expected, &eq) == RES_OK); + CHK(eq); + + /* From the original polygon, offset -2.5: empty polygon */ + ncomps = 2; + ctx.ncomps = ncomps; + memcpy(*coords, coords0, 2*nverts[0]*sizeof(**coords)); + memcpy(*(coords+1), coords1, 2*nverts[1]*sizeof(**coords)); + CHK(scpr_polygon_setup_indexed_vertices(polygon, ncomps, pget_nverts, pget_pos, &ctx) + == RES_OK); + + ncomps = 0; + ctx.ncomps = ncomps; + CHK(scpr_polygon_setup_indexed_vertices(expected, ncomps, pget_nverts, pget_pos, &ctx) + == RES_OK); + + CHK(scpr_offset_polygon(polygon, -2.5, SCPR_JOIN_MITER) == RES_OK); + CHK(scpr_polygon_eq(polygon, expected, &eq) == RES_OK); + CHK(eq); + + CHK(scpr_polygon_ref_put(polygon) == RES_OK); + CHK(scpr_polygon_ref_put(expected) == RES_OK); + + MEM_RM(&allocator, *coords); + MEM_RM(&allocator, *(coords+1)); + MEM_RM(&allocator, coords); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); +} + int main(int argc, char** argv) { (void)argc; (void)argv; test_single(); test_double(); + test_internal(); return 0; }