star-cad

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

commit 27ac0550ac41d1da6893e8f5ed0e390ea44de88f
parent c604a8ab9dbb502fa893bcfb457cd64d8a5a8427
Author: Benjamin Piaud <benjamin.piaud@meso-star.com>
Date:   Mon, 15 Aug 2022 15:49:23 +0200

Add get size operator for scad_geomtry and replace key type in name
htable by struct str

Diffstat:
Msrc/scad.h | 6++++++
Msrc/scad_geometry.c | 49+++++++++++++++++++++++++++++++++++++++++--------
Msrc/scad_scene.h | 22+++++++++++++++++-----
3 files changed, 64 insertions(+), 13 deletions(-)

diff --git a/src/scad.h b/src/scad.h @@ -115,6 +115,12 @@ scad_scene_ref_put * If provided, names must be unique by scene. ******************************************************************************/ +/* `size' is the number of primitives of the geometry `geom' */ +SCAD_API res_T +scad_geometry_get_size + (const struct scad_geometry* geom, + size_t* size); + /* Add a rectangle to the scene, defined by a point `xyz' and * `dxdy' the extents along the x-, y-axes. */ SCAD_API res_T diff --git a/src/scad_geometry.c b/src/scad_geometry.c @@ -40,11 +40,19 @@ scad_geometry_create struct scad_geometry* geom = NULL; ASSERT(scene && out_geometry); - if(name && htable_names_find(&scene->geometry_names, &name)) { - /* if defined, names must be unique */ - res = RES_BAD_ARG; - goto error; + if (name) { + struct str str_name; + + str_init(scene->device->allocator, &str_name); + ERR(str_set(&str_name, name)); + if(name && htable_names_find(&scene->geometry_names, &str_name)) { + /* if defined, names must be unique */ + res = RES_BAD_ARG; + goto error; + } + str_release(&str_name); } + geom = (struct scad_geometry*)MEM_CALLOC(scene->device->allocator, 1, sizeof(*geom)); if(!geom) { @@ -56,8 +64,13 @@ scad_geometry_create geom->scene = scene; scene->device->need_synchro = 1; if(name) { - ERR(str_set(&geom->name, name)); - ERR(htable_names_set(&scene->geometry_names, &name, &geom)); + struct str str_name; + + str_init(scene->device->allocator, &str_name); + ERR(str_set(&str_name, name)); + ERR(str_copy(&geom->name, &str_name)); + ERR(htable_names_set(&scene->geometry_names, &str_name, &geom)); + str_release(&str_name); } end: @@ -130,6 +143,26 @@ error: /****************************************************************************** * Exported functions *****************************************************************************/ + +res_T +scad_geometry_get_size + (const struct scad_geometry* geom, + size_t* size) +{ + res_T res = RES_OK; + + if (!geom) goto error; + + *size = geom->gmsh_dimTags_n / 2; + +exit: + return res; +error: + res = RES_BAD_ARG; + goto exit; +} + + res_T scad_scene_add_rectangle (struct scad_scene* scene, @@ -828,7 +861,7 @@ error: goto exit; } -SCAD_API res_T +res_T scad_geometry_copy (const struct scad_geometry* geom, const char* name, /* Can be NULL */ @@ -989,7 +1022,7 @@ error: goto exit; } -SCAD_API res_T +res_T scad_scene_step_import (struct scad_scene* scene, const char* filename, diff --git a/src/scad_scene.h b/src/scad_scene.h @@ -23,18 +23,30 @@ struct scad_geometry; #include <rsys/ref_count.h> #include <rsys/hash_table.h> +#include <rsys/str.h> static INLINE char -name_eq(const char* const* a, const char* const* b) +eq_str(const struct str* a, const struct str* b) { - ASSERT(a && b); - return strcmp(*a, *b) == 0; + return !strcmp(str_cget(a), str_cget(b)); +} + +static INLINE size_t +hash_str(const struct str* a) +{ + return hash_fnv32(str_cget(a), str_len(a)); } #define HTABLE_NAME names #define HTABLE_DATA struct scad_geometry* -#define HTABLE_KEY const char* -#define HTABLE_KEY_FUNCTOR_EQ name_eq +#define HTABLE_KEY struct str +#define HTABLE_KEY_FUNCTOR_INIT str_init +#define HTABLE_KEY_FUNCTOR_RELEASE str_release +#define HTABLE_KEY_FUNCTOR_COPY str_copy +#define HTABLE_KEY_FUNCTOR_COPY_AND_RELEASE str_copy_and_release +#define HTABLE_KEY_FUNCTOR_EQ eq_str +#define HTABLE_KEY_FUNCTOR_HASH hash_str + #include <rsys/hash_table.h> struct scad_scene {