star-cad

Geometric operators for computer-aided design
git clone git://git.meso-star.fr/star-cad.git
Log | Files | Refs | README | LICENSE

commit f4945f8de0e8073185b5a478babf7a3668b0776f
parent 70ad5be84ce9145cdd9ae09599f04fa53c9303ac
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Wed, 26 Oct 2022 19:00:19 +0200

Add a new API function to define a polygon through a functor

Diffstat:
Msrc/scad.h | 15+++++++++++++--
Msrc/scad_geometry.c | 54+++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 62 insertions(+), 7 deletions(-)

diff --git a/src/scad.h b/src/scad.h @@ -146,7 +146,7 @@ scad_add_disk const double radius, struct scad_geometry** disk); /* Can be NULL: no handler returned. */ -/* Add a polygonal surface in (xy) plane to the scene at elevation z, defined by +/* Add a polygonal surface in (xy) plane to the scene at elevation z, defined by * the list of points of coordinates x, y.*/ SCAD_API res_T scad_add_polygon @@ -157,6 +157,17 @@ scad_add_polygon const size_t count, /* size of x and y arrays */ struct scad_geometry** polygon); /* Can be NULL: no handler returned. */ +/* Add a polygonal surface in (xy) plane to the scene at elevation z, defined by + * a functor.*/ +SCAD_API res_T +scad_add_polygon_fun + (const char* name, /* Can be NULL */ + void (*get_position)(const size_t ivert, double pos[2], void* data), + void* data, /* Custom data */ + const double z, + const size_t count, /* size of x and y arrays */ + struct scad_geometry** polygon); /* Can be NULL: no handler returned. */ + /* Add a parallelepipedic box to the scene, defined by a point `xyz' and * `dxdydz' the extents along the x-, y- and z-axes. */ SCAD_API res_T @@ -226,7 +237,7 @@ scad_intersect_geometries /* compute boundary intersection (the common part) of the geometries in * `geometries' and `tools'. */ SCAD_API res_T -scad_geometries_common_boundaries +scad_geometries_common_boundaries (const char* name, /* Can be NULL */ struct scad_geometry** geometries, const size_t geometries_count, diff --git a/src/scad_geometry.c b/src/scad_geometry.c @@ -29,6 +29,20 @@ /******************************************************************************* * Utility functions ******************************************************************************/ +struct coord_pair { + const double* x; + const double* y; +}; + +static void get_position_pair + (const size_t ivert, double pos[2], void* data) +{ + struct coord_pair* coords = data; + ASSERT(pos && coords); + pos[0] = coords->x[ivert]; + pos[1] = coords->y[ivert]; +} + static res_T scad_geometry_create (const char* name, @@ -408,10 +422,10 @@ error: } res_T -scad_add_polygon +scad_add_polygon_fun (const char* name, - const double* x, - const double* y, + void (*get_position)(const size_t ivert, double pos[2], void* data), + void* data, const double z, const size_t count, struct scad_geometry** out_geometry) @@ -424,7 +438,7 @@ scad_add_polygon int loop; res_T res = RES_OK; - if(!x || !y || count < 3 || !out_geometry) { + if(!get_position || count < 3 || !out_geometry) { res = RES_BAD_ARG; goto error; } @@ -437,7 +451,9 @@ scad_add_polygon goto error; } for (i=0; i<count; ++i) { - points[i] = gmshModelOccAddPoint(x[i], y[i], z, -1, -1, &ierr); + double pos[2]; + get_position(i, pos, data); + points[i] = gmshModelOccAddPoint(pos[0], pos[1], z, -1, -1, &ierr); ERR(gmsh_err_to_res_T(ierr)); } @@ -487,6 +503,34 @@ error: } res_T +scad_add_polygon + (const char* name, + const double* x, + const double* y, + const double z, + const size_t count, + struct scad_geometry** out_geometry) +{ + struct coord_pair pair; + res_T res = RES_OK; + + if(!x || !y || count < 3 || !out_geometry) { + res = RES_BAD_ARG; + goto error; + } + + pair.x = x; + pair.y = y; + return scad_add_polygon_fun(name, get_position_pair, &pair, z, count, + out_geometry); + +exit: + return res; +error: + goto exit; +} + +res_T scad_add_box (const char* name, const double xyz[3],