star-cpr

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

commit c893f4e10a3f56359224d7c276edbfbcd12446f2
parent ec191db746ff998fbf84ff111eb840db02f8b997
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Thu, 29 Sep 2022 16:33:28 +0200

Add polygon offset API function

Diffstat:
Mcmake/CMakeLists.txt | 1+
Msrc/scpr.h | 6++++++
Asrc/test_scpr_offset.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/test_scpr_polygon.c | 5+++++
4 files changed, 81 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -89,6 +89,7 @@ if(NOT NO_TEST) add_test(${_name} ${_name}) endfunction() new_test(test_scpr_clip) + new_test(test_scpr_offset) new_test(test_scpr_mesh) new_test(test_scpr_polygon) rcmake_copy_runtime_libraries(test_scpr_clip) diff --git a/src/scpr.h b/src/scpr.h @@ -79,6 +79,12 @@ scpr_polygon_setup_indexed_vertices void* data); /* Client data set as the last param of the callbacks */ SCPR_API res_T +scpr_offset_polygon + (struct scpr_polygon* polygon, + const double offset, /* Can be either positive or negative */ + const enum scpr_join_type join_type); + +SCPR_API res_T scpr_polygon_get_vertices_count (const struct scpr_polygon* polygon, size_t* nverts); diff --git a/src/test_scpr_offset.c b/src/test_scpr_offset.c @@ -0,0 +1,69 @@ +/* Copyright (C) 2016-2018, 2021 |Meso|Star> (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "scpr.h" +#include "test_scpr_utils.h" + +int +main(int argc, char** argv) +{ + const double coords[] = { + 0.0, 0.0, + 0.0, 1.0, + 1.0, 1.0, + 1.0, 0.0 + }; + const size_t nverts = sizeof(coords)/(2*sizeof(double)); + double pos[2]; + size_t i; + struct mem_allocator allocator; + struct polygon_context ctx; + struct scpr_polygon* polygon; + (void)argc, (void)argv; + + mem_init_proxy_allocator(&allocator, &mem_default_allocator); + + CHK(scpr_polygon_create(&allocator, &polygon) == RES_OK); + + ctx.coords = coords; + ctx.nverts = nverts; + + + CHK(scpr_polygon_setup_indexed_vertices(polygon, nverts, pget_pos, &ctx) + == RES_OK); + + CHK(scpr_offset_polygon(polygon, 0, SCPR_JOIN_MITER) == RES_OK); + for(i = 0; i < nverts; i++) { + CHK(scpr_polygon_get_position(polygon, i, pos) == RES_OK); + CHK(coords[2*i+0] == pos[0] && coords[2*i+1] == pos[1]); + } + + CHK(scpr_offset_polygon(polygon, 1, SCPR_JOIN_MITER) == RES_OK); + for(i = 0; i < nverts; i++) { + double expected[2]; + CHK(scpr_polygon_get_position(polygon, i, pos) == RES_OK); + expected[0] = coords[2*i+0] != 0 ? 2 : -1; + expected[1] = coords[2*i+1] != 0 ? 2 : -1; + CHK(expected[0] == pos[0] && expected[1] == pos[1]); + } + + CHK(scpr_polygon_ref_put(polygon) == RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +} + diff --git a/src/test_scpr_polygon.c b/src/test_scpr_polygon.c @@ -96,6 +96,11 @@ main(int argc, char** argv) CHK(pos[1] == coords[i*2+1]); } + CHK(scpr_offset_polygon(NULL, 0, SCPR_JOIN_TYPES_COUNT__) == RES_BAD_ARG); + CHK(scpr_offset_polygon(polygon, 0, SCPR_JOIN_TYPES_COUNT__) == RES_BAD_ARG); + CHK(scpr_offset_polygon(NULL, 0, SCPR_JOIN_MITER) == RES_BAD_ARG); + CHK(scpr_offset_polygon(polygon, 0, SCPR_JOIN_MITER) == RES_OK); + CHK(scpr_polygon_ref_put(polygon) == RES_OK); check_memory_allocator(&allocator);