star-geometry-3d

Clean and decorate 3D geometries
git clone git://git.meso-star.fr/star-geometry-3d.git
Log | Files | Refs | README | LICENSE

commit 1053ecc28446bded3bd03453585654791233e8b1
parent 692675671f2caec8c4a968f07e1997a6a6eb99f2
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Mon, 17 Feb 2020 17:21:50 +0100

Add stuff to ease a possible change in API types

Diffstat:
Mcmake/CMakeLists.txt | 5++++-
Msrc/sg3d.h | 29++++++++++++++++-------------
Msrc/sg3d_device.c | 16++++++++--------
Msrc/sg3d_geometry.c | 269+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Msrc/sg3d_geometry.h | 86++++++++++++++++++++++++++++++++-----------------------------------------------
Msrc/sg3d_misc.h | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/sgX3d_undefs.h | 6+++---
Msrc/test_sg3d_many_triangles.c | 2+-
Msrc/test_sg3d_some_triangles.c | 2+-
Msrc/test_sg3d_utils.h | 8++++----
10 files changed, 271 insertions(+), 206 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -29,7 +29,8 @@ cmake_dependent_option(SMALL_ADDITIONAL_TESTS "NOT NO_TEST" OFF) cmake_dependent_option(HUGE_ADDITIONAL_TESTS - "Build additional tests originally written for star-enclosures that involve millions of triangles" OFF + "Build additional tests originally written for star-enclosures that involve \ +millions of triangles" OFF "NOT NO_TEST" OFF) ################################################################################ @@ -106,6 +107,8 @@ add_library(sg3d SHARED target_link_libraries(sg3d RSys ${MATH_LIB}) set_target_properties(sg3d PROPERTIES +# C99 needed in case of printf "PRTF_API" used +# C_STANDARD 99 DEFINE_SYMBOL SG3D_SHARED_BUILD VERSION ${VERSION} SOVERSION ${VERSION_MAJOR}) diff --git a/src/sg3d.h b/src/sg3d.h @@ -30,8 +30,8 @@ #define SG3D_API extern IMPORT_SYM #endif -/* Helper macro that asserts if the invocation of the star-geometry function - * `Func' returns an error. One should use this macro on star-geometry +/* Helper macro that asserts if the invocation of the star-geometry-3d function + * `Func' returns an error. One should use this macro on star-geometry-3d * function calls for which no explicit error checking is performed. */ #ifndef NDEBUG #define SG3D(Func) ASSERT(sg3d_ ## Func == RES_OK) @@ -44,7 +44,7 @@ struct logger; struct mem_allocator; struct senc_scene; -/* Forward declaration of the star-geometry opaque data types. These data +/* Forward declaration of the star-geometry-3d opaque data types. These data * types are ref counted. Once created the caller implicitly owns the created * data, i.e. its reference counter is set to 1. The sg3d_<TYPE>_ref_<get|put> * functions get or release a reference on the data, i.e. they increment or @@ -97,13 +97,15 @@ enum sg3d_c_dump_qualifiers { *****************************************************************************/ #define SG3D_UNSPECIFIED_PROPERTY UINT_MAX - /***************************************************************************** - * A type to hold callbacks for sg3d_geometry_add. - ****************************************************************************/ +/***************************************************************************** + * A type to hold callbacks for sg3d_geometry_add. + ****************************************************************************/ struct sg3d_geometry_add_callbacks { /* User function that provides vertices ids for added triangles */ void(*get_indices) - (const unsigned itri, unsigned ids[SG3D_GEOMETRY_DIMENSION], void* context); + (const unsigned itri, + unsigned ids[SG3D_GEOMETRY_DIMENSION], + void* context); /* User function that provides properties for added triangles */ void(*get_properties) /* Can be NULL <=> SG3D_UNSPECIFIED_PROPERTY used */ (const unsigned itri, @@ -158,13 +160,14 @@ sg3d_compatible_property (const unsigned p1, const unsigned p2) { - if(p1 == SG3D_UNSPECIFIED_PROPERTY || p2 == SG3D_UNSPECIFIED_PROPERTY) return 1; + if(p1 == SG3D_UNSPECIFIED_PROPERTY || p2 == SG3D_UNSPECIFIED_PROPERTY) + return 1; return (p1 == p2); } /****************************************************************************** - * star-geometry device. It is an handle toward the star-geometry library. - * It manages the star-geometry resources. + * star-geometry-3d device. It is an handle toward the star-geometry-3d + * library. It manages the star-geometry-3d resources. *****************************************************************************/ SG3D_API res_T sg3d_device_create @@ -182,7 +185,7 @@ sg3d_device_ref_put (struct sg3d_device* dev); /****************************************************************************** - * star-geometry geometry. + * star-geometry-3d geometry. * It stores decorated geometry accumulated through calls to sg3d_geometry_add, * information related to this geometry and its creation process, including * merge conflicts. @@ -228,8 +231,8 @@ sg3d_geometry_reserve * non-SG3D_UNSPECIFIED_PROPERTY property is never overridden. * When deduplicating triangles, the first occurence remains (with its * original index in user world). After consistency being computed, a final - * step consists in rewriting SG3D_UNSPECIFIED_PROPERTY properties if the merged - * property is defined. */ + * step consists in rewriting SG3D_UNSPECIFIED_PROPERTY properties if the + * merged property is defined. */ SG3D_API res_T sg3d_geometry_add (struct sg3d_geometry* geometry, diff --git a/src/sg3d_device.c b/src/sg3d_device.c @@ -21,9 +21,9 @@ #include <stdarg.h> - /******************************************************************************* - * Helper functions - ******************************************************************************/ +/****************************************************************************** + * Helper functions + *****************************************************************************/ static void log_msg (struct sg3d_device* dev, @@ -48,9 +48,9 @@ device_release(ref_T* ref) MEM_RM(dev->allocator, dev); } -/******************************************************************************* +/****************************************************************************** * Exported functions - ******************************************************************************/ + *****************************************************************************/ res_T sg3d_device_create (struct logger* logger, @@ -75,7 +75,7 @@ sg3d_device_create if(verbose) { /* Do not use helper log functions since dev is not initialised */ CHK(logger_print(log, LOG_ERROR, - "%s: could not allocate the star-geometry device.\n", FUNC_NAME) + "%s: could not allocate the star-geometry-3d device.\n", FUNC_NAME) == RES_OK); } res = RES_MEM_ERR; @@ -113,9 +113,9 @@ sg3d_device_ref_put(struct sg3d_device* dev) return RES_OK; } -/******************************************************************************* +/****************************************************************************** * Local functions - ******************************************************************************/ + *****************************************************************************/ void log_err(struct sg3d_device* dev, const char* msg, ...) { diff --git a/src/sg3d_geometry.c b/src/sg3d_geometry.c @@ -21,9 +21,9 @@ #include <limits.h> - /******************************************************************************* - * Helper functions - ******************************************************************************/ +/****************************************************************************** + * Helper functions + *****************************************************************************/ static void geometry_release(ref_T* ref) { @@ -42,7 +42,7 @@ geometry_release(ref_T* ref) } static FINLINE int /* Return 1 if reversed */ -trg_make_key(struct unsigned3* k, const unsigned t[3]) +trg_make_key(struct vrtx_id3* k, const vrtx_id_t t[3]) { ASSERT(t); ASSERT(t[0] != t[1] && t[0] != t[2] && t[1] != t[2]); @@ -109,7 +109,7 @@ dump_trg_property descriptions = darray_trg_descriptions_cdata_get(&geom->trg_descriptions); FOR_EACH(i, 0, darray_triangle_size_get(&geom->unique_triangles)) { - unsigned property = SG3D_UNSPECIFIED_PROPERTY; + prop_id_t property = SG3D_UNSPECIFIED_PROPERTY; size_t tdefs_count = darray_definition_size_get(&descriptions[i].defs[type]); if(tdefs_count && descriptions[i].property_defined[type]) { @@ -124,19 +124,20 @@ dump_trg_property tdefs++; /* Next value */ } } - /* In VTK dumps INT_MAX is used for both unspecified and conflict */ - fprintf(stream, "%u\n", MMIN(property, INT_MAX)); + /* In VTK dumps INT_MAX is used for both unspecified and conflict + * (VTK ascii format doesn't allow a greater value) */ + fprintf(stream, "%d\n", (int)MMIN(property, INT_MAX)); } } -/******************************************************************************* +/****************************************************************************** * Local functions - ******************************************************************************/ + *****************************************************************************/ res_T geometry_register_triangle (struct sg3d_geometry* geom, const struct triangle* triangle, - const unsigned triangle_unique_id, + const trg_id_t triangle_unique_id, const unsigned set_id, const int merge_conflict) { @@ -222,16 +223,17 @@ error: res_T geometry_enlarge_trg_descriptions (struct sg3d_geometry* geom, - const size_t sz) + const trg_id_t sz) { res_T res = RES_OK; size_t old_sz = darray_trg_descriptions_size_get(&geom->trg_descriptions); if(sz <= old_sz) return RES_OK; - ASSERT(sz - old_sz < UINT_MAX); ERR(darray_trg_descriptions_resize(&geom->trg_descriptions, sz)); - geom->trg_with_unspecified_sides_count += (unsigned)(sz - old_sz); - geom->trg_with_unspecified_intface_count += (unsigned)(sz - old_sz); + ASSERT(geom->trg_with_unspecified_sides_count + sz - old_sz <= TRG_MAX__); + ASSERT(geom->trg_with_unspecified_intface_count + sz - old_sz <= TRG_MAX__); + geom->trg_with_unspecified_sides_count += (trg_id_t)(sz - old_sz); + geom->trg_with_unspecified_intface_count += (trg_id_t)(sz - old_sz); exit: return res; @@ -270,7 +272,7 @@ dump_partition dump = trg_descriptions[i].properties_conflict; } if(!dump) continue; - fprintf(stream, "f %u %u %u\n", + fprintf(stream, "f "PRTF_VRTX" "PRTF_VRTX" "PRTF_VRTX"\n", /* OBJ indexing starts at 1 */ 1 + triangles[i].vertex_ids[0], 1 + triangles[i].vertex_ids[1], @@ -278,9 +280,9 @@ dump_partition } } -/******************************************************************************* +/****************************************************************************** * Exported functions - ******************************************************************************/ + *****************************************************************************/ res_T sg3d_geometry_create (struct sg3d_device* dev, @@ -333,9 +335,9 @@ error: res_T sg3d_geometry_reserve (struct sg3d_geometry* geom, - const unsigned vertices_count, - const unsigned triangles_count, - const unsigned properties_count) + const vrtx_id_t vertices_count, + const trg_id_t triangles_count, + const prop_id_t properties_count) { res_T res = RES_OK; if(!geom) return RES_BAD_ARG; @@ -356,19 +358,26 @@ error: res_T sg3d_geometry_add (struct sg3d_geometry* geom, - const unsigned nverts, - const unsigned ntris, + const vrtx_id_t nverts, + const trg_id_t ntris, const struct sg3d_geometry_add_callbacks* callbacks, void* ctx) /* Can be NULL */ { res_T res = RES_OK; struct mem_allocator* alloc; size_t nutris, nuverts; - unsigned i, n_new_uverts = 0, n_new_utris = 0; + vrtx_id_t nv, n_new_uverts = 0; + trg_id_t nt, n_new_utris = 0; struct triangle* trg; /* Tmp table of IDs to record unique IDs of the currently added vertices */ - struct darray_uint unique_vertice_ids; + struct darray_vertice_ids unique_vertice_ids; int unique_vertice_ids_initialized = 0; + get_indices_t get_ind; + get_properties_t get_prop; + get_position_t get_pos; + add_triangle_t add_trg; + merge_triangle_t mrg_trg; + degenerated_triangle_t dege_trg; if(!geom || !callbacks || !callbacks->get_indices || !callbacks->get_position) { @@ -376,14 +385,20 @@ sg3d_geometry_add goto error; } + get_ind = callbacks->get_indices; + get_prop = callbacks->get_properties; + get_pos = callbacks->get_position; + add_trg = callbacks->add_triangle; + mrg_trg = callbacks->merge_triangle; + dege_trg = callbacks->degenerated_triangle; alloc = geom->dev->allocator; nuverts = darray_vertex_size_get(&geom->unique_vertices); nutris = darray_triangle_size_get(&geom->unique_triangles); /* Make room for new geometry; suppose no more duplicates */ - darray_uint_init(alloc, &unique_vertice_ids); + darray_vertice_ids_init(alloc, &unique_vertice_ids); unique_vertice_ids_initialized = 1; - ERR(darray_uint_reserve(&unique_vertice_ids, nverts)); + ERR(darray_vertice_ids_reserve(&unique_vertice_ids, nverts)); ERR(darray_vertex_reserve(&geom->unique_vertices, nuverts + nverts)); ERR(darray_triangle_reserve(&geom->unique_triangles, nutris + ntris)); ERR(htable_vrtx_reserve(&geom->unique_vertices_ids, nuverts + nverts)); @@ -391,39 +406,39 @@ sg3d_geometry_add ASSERT(nutris == darray_trg_descriptions_size_get(&geom->trg_descriptions)); ERR(darray_trg_descriptions_reserve(&geom->trg_descriptions, nutris + ntris)); /* Get vertices and deduplicate */ - FOR_EACH(i, 0, nverts) { - unsigned* p_vrtx; + FOR_EACH(nv, 0, nverts) { + vrtx_id_t* p_vrtx; struct vertex tmp; - unsigned v_idx; - callbacks->get_position(i, tmp.coord, ctx); + vrtx_id_t v_idx; + get_pos(nv, tmp.coord, ctx); p_vrtx = htable_vrtx_find(&geom->unique_vertices_ids, &tmp); if(p_vrtx) { /* Duplicate vertex */ v_idx = *p_vrtx; } else { /* New vertex */ - ASSERT(nuverts + n_new_uverts < UINT_MAX); - v_idx = (unsigned)(nuverts + n_new_uverts); + ASSERT(nuverts + n_new_uverts <= VRTX_MAX__); + v_idx = (vrtx_id_t)(nuverts + n_new_uverts); ASSERT(v_idx == htable_vrtx_size_get(&geom->unique_vertices_ids)); ERR(darray_vertex_push_back(&geom->unique_vertices, &tmp)); ERR(htable_vrtx_set(&geom->unique_vertices_ids, &tmp, &v_idx)); ++n_new_uverts; } - /* Keep the unique ID for vertex i */ - ERR(darray_uint_push_back(&unique_vertice_ids, &v_idx)); + /* Keep the unique ID for vertex nv */ + ERR(darray_vertice_ids_push_back(&unique_vertice_ids, &v_idx)); } /* Get triangles and deduplicate */ trg = darray_triangle_data_get(&geom->unique_triangles); - FOR_EACH(i, 0, ntris) { + FOR_EACH(nt, 0, ntris) { int j, reversed; - struct unsigned3 trg_key; + struct vrtx_id3 trg_key; struct triangle tmp = TRG_UNDEF__; - unsigned* p_trg; + trg_id_t* p_trg; struct trg_descriptions* trg_descriptions = NULL; - unsigned unique_id; + trg_id_t unique_id; - callbacks->get_indices(i, tmp.vertex_ids, ctx); + get_ind(nt, tmp.vertex_ids, ctx); FOR_EACH(j, 0, 3) { if(tmp.vertex_ids[j] >= nverts) { res = RES_BAD_ARG; @@ -431,19 +446,19 @@ sg3d_geometry_add } /* Replace the vertex ID by its the unique ID */ tmp.vertex_ids[j] - = darray_uint_cdata_get(&unique_vertice_ids)[tmp.vertex_ids[j]]; + = darray_vertice_ids_cdata_get(&unique_vertice_ids)[tmp.vertex_ids[j]]; } if(tmp.vertex_ids[0] == tmp.vertex_ids[1] || tmp.vertex_ids[0] == tmp.vertex_ids[2] || tmp.vertex_ids[1] == tmp.vertex_ids[2]) { int abort = 0; - if(callbacks->degenerated_triangle) { + if(dege_trg) { /* Let the client app rule. */ - ERR(callbacks->degenerated_triangle(i, ctx, &abort)); + ERR(dege_trg(nt, ctx, &abort)); } else { - log_warn(geom->dev, "%s: triangle %u is degenerated.\n", - FUNC_NAME, i); + log_warn(geom->dev, "%s: triangle "PRTF_TRG" is degenerated.\n", + FUNC_NAME, nt); } if(abort) { res = RES_BAD_ARG; @@ -452,14 +467,13 @@ sg3d_geometry_add else continue; } /* Get properties */ - if(callbacks->get_properties) - callbacks->get_properties(i, tmp.properties, ctx); + if(get_prop) get_prop(nt, tmp.properties, ctx); /* Find duplicate triangles */ reversed = trg_make_key(&trg_key, tmp.vertex_ids); p_trg = htable_trg_find(&geom->unique_triangles_ids, &trg_key); if(p_trg) { /* Duplicate triangle. Need to check duplicate validity */ - struct unsigned3 utrg_key; + struct vrtx_id3 utrg_key; int ureversed = trg_make_key(&utrg_key, trg[*p_trg].vertex_ids); int same = (reversed == ureversed); int already_conflict; @@ -469,23 +483,23 @@ sg3d_geometry_add trg_descriptions = darray_trg_descriptions_data_get(&geom->trg_descriptions); if(!same) - SWAP(unsigned, tmp.properties[SG3D_FRONT], tmp.properties[SG3D_BACK]); - already_conflict = trg_descriptions[i].merge_conflict; - if(callbacks->merge_triangle) { + SWAP(prop_id_t, tmp.properties[SG3D_FRONT], tmp.properties[SG3D_BACK]); + already_conflict = trg_descriptions[nt].merge_conflict; + if(mrg_trg) { /* Let the client app rule. */ - ERR(callbacks->merge_triangle(*p_trg, i, !same, trg[*p_trg].properties, - tmp.properties, ctx, &trg_descriptions[i].merge_conflict)); + ERR(mrg_trg(unique_id, nt, !same, trg[*p_trg].properties, + tmp.properties, ctx, &trg_descriptions[nt].merge_conflict)); } else { FOR_EACH(j, 0, SG3D_PROP_TYPES_COUNT__) { if(!sg3d_compatible_property(trg[*p_trg].properties[j], tmp.properties[j])) { - trg_descriptions[i].merge_conflict = 1; + trg_descriptions[nt].merge_conflict = 1; break; } } } - if(trg_descriptions[i].merge_conflict && !already_conflict) + if(trg_descriptions[nt].merge_conflict && !already_conflict) geom->merge_conflict_count++; /* Replace SG3D_UNSPECIFIED_PROPERTY properties */ FOR_EACH(j, 0, SG3D_PROP_TYPES_COUNT__) { @@ -498,11 +512,10 @@ sg3d_geometry_add } } else { /* New triangle */ - ASSERT(nutris + n_new_utris < UINT_MAX); - unique_id = (unsigned)(nutris + n_new_utris); - tmp.user_id = geom->triangle_count_including_duplicates + i; - if(callbacks->add_triangle) - ERR(callbacks->add_triangle(unique_id, i, ctx)); + ASSERT(nutris + n_new_utris <= TRG_MAX__); + unique_id = (trg_id_t)(nutris + n_new_utris); + tmp.user_id = geom->triangle_count_including_duplicates + nt; + if(add_trg) ERR(add_trg(unique_id, nt, ctx)); ERR(geometry_enlarge_trg_descriptions(geom, 1 + unique_id)); trg_descriptions = darray_trg_descriptions_data_get(&geom->trg_descriptions); @@ -517,8 +530,8 @@ sg3d_geometry_add n_new_utris++; } ERR(geometry_register_triangle(geom, &tmp, unique_id, geom->set_id, - trg_descriptions[i].properties_conflict)); - if(trg_descriptions[i].properties_conflict) + trg_descriptions[nt].properties_conflict)); + if(trg_descriptions[nt].properties_conflict) geom->merge_conflict_count++; } @@ -532,7 +545,7 @@ exit: geom->triangle_count_including_duplicates += ntris; } if(unique_vertice_ids_initialized) - darray_uint_release(&unique_vertice_ids); + darray_vertice_ids_release(&unique_vertice_ids); return res; error: goto exit; @@ -541,11 +554,11 @@ error: res_T sg3d_geometry_validate_properties (struct sg3d_geometry* geom, - res_T(*validate)(const unsigned, const unsigned*, void*, int*), + res_T(*validate)(const trg_id_t, const prop_id_t*, void*, int*), void* ctx) { size_t sz__; - unsigned i, sz; + trg_id_t i, sz; struct trg_descriptions* trg_descriptions; res_T res = RES_OK; @@ -555,14 +568,14 @@ sg3d_geometry_validate_properties } sz__ = darray_trg_descriptions_size_get(&geom->trg_descriptions); - ASSERT(sz__ <= UINT_MAX); + ASSERT(sz__ <= PROP_MAX__); sz = (unsigned)sz__; trg_descriptions = darray_trg_descriptions_data_get(&geom->trg_descriptions); geom->properties_conflict_count = 0; /* Reset count */ FOR_EACH(i, 0, sz) { - unsigned p, j; - unsigned props[SG3D_PROP_TYPES_COUNT__]; + int p; + prop_id_t props[SG3D_PROP_TYPES_COUNT__]; struct trg_descriptions* trgd = trg_descriptions + i; /* Validate only triangle not flagged with merge_conflict */ if(trgd->merge_conflict) { @@ -571,6 +584,7 @@ sg3d_geometry_validate_properties } /* Get properties for non-conflict triangles */ FOR_EACH(p, 0, SG3D_PROP_TYPES_COUNT__) { + size_t j; const struct definition* defs = darray_definition_cdata_get(trgd->defs + p); props[p] = SG3D_UNSPECIFIED_PROPERTY; FOR_EACH(j, 0, darray_definition_size_get(trgd->defs + p)) { @@ -595,7 +609,7 @@ error: res_T sg3d_geometry_get_unique_vertices_count (const struct sg3d_geometry* geom, - unsigned* count) + vrtx_id_t* count) { res_T res = RES_OK; size_t sz; @@ -604,8 +618,8 @@ sg3d_geometry_get_unique_vertices_count goto error; } sz = darray_vertex_size_get(&geom->unique_vertices); - ASSERT(sz <= UINT_MAX); - *count = (unsigned)sz; + ASSERT(sz <= VRTX_MAX__); + *count = (vrtx_id_t)sz; exit: return res; error: @@ -615,8 +629,8 @@ error: res_T sg3d_geometry_get_unique_vertex (const struct sg3d_geometry* geom, - const unsigned ivtx, - double coord[3]) + const vrtx_id_t ivtx, + double coord[SG3D_GEOMETRY_DIMENSION]) { res_T res = RES_OK; const struct vertex* vertices; @@ -637,7 +651,7 @@ error: res_T sg3d_geometry_get_added_triangles_count (const struct sg3d_geometry* geom, - unsigned* count) + trg_id_t* count) { res_T res = RES_OK; if (!geom || !count) { @@ -654,7 +668,7 @@ error: res_T sg3d_geometry_get_unique_triangles_count (const struct sg3d_geometry* geom, - unsigned* count) + trg_id_t* count) { res_T res = RES_OK; size_t sz; @@ -663,7 +677,7 @@ sg3d_geometry_get_unique_triangles_count goto error; } sz = darray_triangle_size_get(&geom->unique_triangles); - ASSERT(sz <= UINT_MAX); + ASSERT(sz <= TRG_MAX__); *count = (unsigned)sz; exit: return res; @@ -674,8 +688,8 @@ error: res_T sg3d_geometry_get_unique_triangle_vertices (const struct sg3d_geometry* geom, - const unsigned itri, - unsigned indices[3]) + const trg_id_t itri, + vrtx_id_t indices[SG3D_GEOMETRY_DIMENSION]) { res_T res = RES_OK; const struct triangle* triangles; @@ -687,7 +701,8 @@ sg3d_geometry_get_unique_triangle_vertices goto error; } triangles = darray_triangle_cdata_get(&geom->unique_triangles); - FOR_EACH(i, 0, 3) indices[i] = triangles[itri].vertex_ids[i]; + FOR_EACH(i, 0, SG3D_GEOMETRY_DIMENSION) + indices[i] = triangles[itri].vertex_ids[i]; exit: return res; error: @@ -697,8 +712,8 @@ error: res_T sg3d_geometry_get_unique_triangle_properties (const struct sg3d_geometry* geom, - const unsigned itri, - unsigned properties[SG3D_PROP_TYPES_COUNT__]) + const trg_id_t itri, + prop_id_t properties[SG3D_PROP_TYPES_COUNT__]) { res_T res = RES_OK; const struct triangle* triangles; @@ -721,8 +736,8 @@ error: res_T sg3d_geometry_get_unique_triangle_user_id (const struct sg3d_geometry* geom, - const unsigned itri, - unsigned* user_id) + const trg_id_t itri, + trg_id_t* user_id) { res_T res = RES_OK; const struct triangle* triangles; @@ -743,7 +758,7 @@ error: res_T sg3d_geometry_get_unique_triangles_with_unspecified_side_count (const struct sg3d_geometry* geom, - unsigned* count) + trg_id_t* count) { res_T res = RES_OK; if(!geom || !count) { @@ -760,7 +775,7 @@ error: res_T sg3d_geometry_get_unique_triangles_with_unspecified_interface_count (const struct sg3d_geometry* geom, - unsigned* count) + trg_id_t* count) { res_T res = RES_OK; if(!geom || !count) { @@ -777,7 +792,7 @@ error: res_T sg3d_geometry_get_unique_triangles_with_merge_conflict_count (const struct sg3d_geometry* geom, - unsigned* count) + trg_id_t* count) { res_T res = RES_OK; if(!geom || !count) { @@ -794,7 +809,7 @@ error: res_T sg3d_geometry_get_unique_triangles_with_properties_conflict_count (const struct sg3d_geometry* geom, - unsigned* count) + trg_id_t* count) { res_T res = RES_OK; if(!geom || !count) { @@ -828,19 +843,19 @@ sg3d_geometry_dump_as_obj goto error; } /* Headers */ - fprintf(stream, "# Dump of star-geometry\n"); + fprintf(stream, "# Dump of star-geometry-3d\n"); fprintf(stream, "# Geometry counts:\n"); vsz = darray_vertex_size_get(&geom->unique_vertices); - ASSERT(vsz <= UINT_MAX); - fprintf(stream, "# . %u vertices\n", (unsigned)vsz); + ASSERT(vsz <= VRTX_MAX__); + fprintf(stream, "# . "PRTF_VRTX" vertices\n", (vrtx_id_t)vsz); tsz = darray_triangle_size_get(&geom->unique_triangles); - ASSERT(tsz <= UINT_MAX); - fprintf(stream, "# . %u triangles\n", (unsigned)tsz); + ASSERT(tsz <= TRG_MAX__); + fprintf(stream, "# . "PRTF_TRG" triangles\n", (trg_id_t)tsz); fprintf(stream, - "# . %u triangles flagged with a merge conflict\n", + "# . "PRTF_TRG" triangles flagged with a merge conflict\n", geom->merge_conflict_count); fprintf(stream, - "# . %u triangles flagged with a property conflict\n", + "# . "PRTF_TRG" triangles flagged with a property conflict\n", geom->merge_conflict_count); /* Dump vertices */ @@ -849,9 +864,12 @@ sg3d_geometry_dump_as_obj fprintf(stream, "v %g %g %g\n", SPLIT3(vertices[i].coord)); /* Dump triangles by groups */ - dump_partition(geom, stream, "Valid_triangles", SG3D_OBJ_DUMP_VALID_PRIMITIVE); - dump_partition(geom, stream, "Merge_conflicts", SG3D_OBJ_DUMP_MERGE_CONFLICTS); - dump_partition(geom, stream, "Property_conflicts", SG3D_OBJ_DUMP_PROPERTY_CONFLICTS); + dump_partition(geom, stream, + "Valid_triangles", SG3D_OBJ_DUMP_VALID_PRIMITIVE); + dump_partition(geom, stream, + "Merge_conflicts", SG3D_OBJ_DUMP_MERGE_CONFLICTS); + dump_partition(geom, stream, + "Property_conflicts", SG3D_OBJ_DUMP_PROPERTY_CONFLICTS); exit: return res; @@ -879,28 +897,30 @@ sg3d_geometry_dump_as_vtk } /* Headers */ fprintf(stream, "# vtk DataFile Version 3.0\n"); - fprintf(stream, "Dump of star-geometry geometry\n"); + fprintf(stream, "Dump of star-geometry-3d geometry\n"); fprintf(stream, "ASCII\n"); fprintf(stream, "DATASET POLYDATA\n"); /* Dump vertices */ vsz = darray_vertex_size_get(&geom->unique_vertices); - ASSERT(vsz <= UINT_MAX); - fprintf(stream, "POINTS %u double\n", (unsigned)vsz); + ASSERT(vsz <= VRTX_MAX__); + fprintf(stream, "POINTS "PRTF_VRTX" double\n", (vrtx_id_t)vsz); vertices = darray_vertex_cdata_get(&geom->unique_vertices); FOR_EACH(i, 0, vsz) fprintf(stream, "%g %g %g\n", SPLIT3(vertices[i].coord)); /* Dump triangles */ tsz = darray_triangle_size_get(&geom->unique_triangles); - ASSERT(4 * tsz <= UINT_MAX); - fprintf(stream, "POLYGONS %u %u\n", (unsigned)tsz, (unsigned)(4 * tsz)); + ASSERT(4 * tsz <= TRG_MAX__); + fprintf(stream, "POLYGONS "PRTF_TRG" "PRTF_TRG"\n", + (trg_id_t)tsz, (trg_id_t)(4 * tsz)); triangles = darray_triangle_cdata_get(&geom->unique_triangles); FOR_EACH(i, 0, tsz) - fprintf(stream, "3 %u %u %u\n", SPLIT3(triangles[i].vertex_ids)); + fprintf(stream, "3 "PRTF_VRTX" "PRTF_VRTX" "PRTF_VRTX"\n", + SPLIT3(triangles[i].vertex_ids)); /* Start triangles properties */ - fprintf(stream, "CELL_DATA %u\n", (unsigned)tsz); + fprintf(stream, "CELL_DATA "PRTF_TRG"\n", (trg_id_t)tsz); descriptions = darray_trg_descriptions_cdata_get(&geom->trg_descriptions); /* Dump front medium */ @@ -921,20 +941,19 @@ sg3d_geometry_dump_as_vtk /* Dump user_id */ fprintf(stream, "SCALARS User_ID int\n"); fprintf(stream, "LOOKUP_TABLE default\n"); - FOR_EACH(i, 0, tsz) - fprintf(stream, "%u\n", triangles[i].user_id); + FOR_EACH(i, 0, tsz) fprintf(stream, PRTF_TRG"\n", triangles[i].user_id); /* Dump merge conflict status */ fprintf(stream, "SCALARS Merge_conflict int\n"); fprintf(stream, "LOOKUP_TABLE default\n"); FOR_EACH(i, 0, tsz) - fprintf(stream, "%d\n", descriptions[i].merge_conflict); + fprintf(stream, PRTF_TRG"\n", descriptions[i].merge_conflict); /* Dump property conflict status */ fprintf(stream, "SCALARS Property_conflict int\n"); fprintf(stream, "LOOKUP_TABLE default\n"); FOR_EACH(i, 0, tsz) - fprintf(stream, "%d\n", descriptions[i].properties_conflict); + fprintf(stream, PRTF_TRG"\n", descriptions[i].properties_conflict); /* Dump rank of the sg3d_geometry_add that created the triangle */ fprintf(stream, "SCALARS Created_at_sg3d_geometry_add int\n"); @@ -988,13 +1007,13 @@ sg3d_geometry_dump_as_c_code if(!name_prefix) name_prefix = ""; /* Headers */ if(name_prefix && name_prefix[0] != '\0') - fprintf(stream, "/* Dump of star-geometry '%s'. */\n", name_prefix); + fprintf(stream, "/* Dump of star-geometry-3d '%s'. */\n", name_prefix); else - fprintf(stream, "/* Dump of star-geometry. */\n"); + fprintf(stream, "/* Dump of star-geometry-3d. */\n"); vsz = darray_vertex_size_get(&geom->unique_vertices); - ASSERT(3 * vsz <= UINT_MAX); + ASSERT(3 * vsz <= VRTX_MAX__); tsz = darray_triangle_size_get(&geom->unique_triangles); - ASSERT(3 * tsz <= UINT_MAX); + ASSERT(3 * tsz <= TRG_MAX__); if(vsz == 0 || tsz == 0) { log_err(geom->dev, @@ -1013,14 +1032,14 @@ sg3d_geometry_dump_as_c_code else qualifiers = ""; /* Dump vertices */ - fprintf(stream, "%sunsigned %s_vertices_count = %u;\n", - qualifiers, name_prefix, (unsigned)vsz); + fprintf(stream, "%s"VRTX_TYPE_NAME" %s_vertices_count = "PRTF_VRTX";\n", + qualifiers, name_prefix, (vrtx_id_t)vsz); vertices = darray_vertex_cdata_get(&geom->unique_vertices); fprintf(stream, - "%sdouble %s_vertices[%u] =\n" + "%sdouble %s_vertices["PRTF_VRTX"] =\n" "{\n", - qualifiers, name_prefix, (unsigned)(3 * vsz)); + qualifiers, name_prefix, (vrtx_id_t)(3 * vsz)); FOR_EACH(i, 0, vsz - 1) fprintf(stream, " %g, %g, %g,\n", SPLIT3(vertices[i].coord)); @@ -1030,34 +1049,36 @@ sg3d_geometry_dump_as_c_code "};\n"); /* Dump triangles */ - fprintf(stream, "%sunsigned %s_triangles_count = %u;\n", - qualifiers, name_prefix, (unsigned)tsz); + fprintf(stream, "%s"TRG_TYPE_NAME" %s_triangles_count = "PRTF_TRG";\n", + qualifiers, name_prefix, (trg_id_t)tsz); triangles = darray_triangle_cdata_get(&geom->unique_triangles); fprintf(stream, - "%sunsigned %s_triangles[%u] =\n" + "%s"TRG_TYPE_NAME" %s_triangles["PRTF_TRG"] =\n" "{\n", - qualifiers, name_prefix, (unsigned)(3 * tsz)); + qualifiers, name_prefix, (trg_id_t)(3 * tsz)); FOR_EACH(i, 0, tsz - 1) fprintf(stream, - " %u, %u, %u,\n", SPLIT3(triangles[i].vertex_ids)); + " "PRTF_VRTX", "PRTF_VRTX", "PRTF_VRTX",\n", + SPLIT3(triangles[i].vertex_ids)); fprintf(stream, - " %u, %u, %u\n", SPLIT3(triangles[tsz - 1].vertex_ids)); + " "PRTF_VRTX", "PRTF_VRTX", "PRTF_VRTX"\n", + SPLIT3(triangles[tsz - 1].vertex_ids)); fprintf(stream, "};\n"); /* Dump properties */ fprintf(stream, - "%sunsigned %s_properties[%u] =\n" + "%s"PROP_TYPE_NAME" %s_properties["PRTF_PROP"] =\n" "{\n", - qualifiers, name_prefix, (unsigned)(SG3D_PROP_TYPES_COUNT__ * tsz)); + qualifiers, name_prefix, (prop_id_t)(SG3D_PROP_TYPES_COUNT__ * tsz)); FOR_EACH(i, 0, tsz) { int p; fprintf(stream, " "); FOR_EACH(p, 0, SG3D_PROP_TYPES_COUNT__) { if(triangles[i].properties[p] == SG3D_UNSPECIFIED_PROPERTY) fprintf(stream, " SG3D_UNSPECIFIED_PROPERTY"); - else fprintf(stream," %u", triangles[i].properties[p]); + else fprintf(stream," "PRTF_PROP"", triangles[i].properties[p]); if(i < tsz-1 || p < 2) fprintf(stream, ","); if(p == 2) fprintf(stream, "\n"); } diff --git a/src/sg3d_geometry.h b/src/sg3d_geometry.h @@ -21,19 +21,20 @@ #include <rsys/ref_count.h> #include <rsys/dynamic_array.h> +#include <rsys/dynamic_array_uint.h> #include <rsys/hash_table.h> /* Forward declaration of external opaque data types */ -/******************************************************************************* +/****************************************************************************** * A type to store triangles - ******************************************************************************/ + *****************************************************************************/ struct triangle { - unsigned vertex_ids[3]; + vrtx_id_t vertex_ids[3]; /* FRONT/BACK/INTERFACE property */ - unsigned properties[SG3D_PROP_TYPES_COUNT__]; + prop_id_t properties[SG3D_PROP_TYPES_COUNT__]; /* ID of the triangle in user world, i.e. without deduplication */ - unsigned user_id; + trg_id_t user_id; }; #define TRG_UNDEF__ {\ { SG3D_UNSPECIFIED_PROPERTY, SG3D_UNSPECIFIED_PROPERTY, SG3D_UNSPECIFIED_PROPERTY },\ @@ -44,9 +45,9 @@ struct triangle { #define DARRAY_DATA struct triangle #include <rsys/dynamic_array.h> -/******************************************************************************* +/****************************************************************************** * A type to store vertices - ******************************************************************************/ + *****************************************************************************/ struct vertex { double coord[3]; }; @@ -54,13 +55,13 @@ struct vertex { #define DARRAY_DATA struct vertex #include <rsys/dynamic_array.h> -/******************************************************************************* +/****************************************************************************** * A type to map triangle vertices to IDs in unique_triangles - ******************************************************************************/ -struct unsigned3 { unsigned x[3]; }; + *****************************************************************************/ +struct vrtx_id3 { vrtx_id_t x[3]; }; static FINLINE int -trg_key_eq(const struct unsigned3* k1, const struct unsigned3* k2) +trg_key_eq(const struct vrtx_id3* k1, const struct vrtx_id3* k2) { ASSERT(k1 && k2); ASSERT(k1->x[0] < k1->x[1] && k1->x[1] < k1->x[2]); @@ -71,14 +72,14 @@ trg_key_eq(const struct unsigned3* k1, const struct unsigned3* k2) } #define HTABLE_NAME trg -#define HTABLE_KEY struct unsigned3 -#define HTABLE_DATA unsigned +#define HTABLE_KEY struct vrtx_id3 +#define HTABLE_DATA trg_id_t #define HTABLE_KEY_FUNCTOR_EQ trg_key_eq #include <rsys/hash_table.h> - /******************************************************************************* - * A type to map vertex coordinates to IDs in unique_vertices - ******************************************************************************/ +/****************************************************************************** + * A type to map vertex coordinates to IDs in unique_vertices + *****************************************************************************/ static FINLINE int vrtx_eq(const struct vertex* v1, const struct vertex* v2) { @@ -90,19 +91,19 @@ vrtx_eq(const struct vertex* v1, const struct vertex* v2) #define HTABLE_NAME vrtx #define HTABLE_KEY struct vertex -#define HTABLE_DATA unsigned +#define HTABLE_DATA vrtx_id_t #define HTABLE_KEY_FUNCTOR_EQ vrtx_eq #include <rsys/hash_table.h> -/******************************************************************************* +/****************************************************************************** * Types to record sources and values of triangle descriptions. - ******************************************************************************/ + *****************************************************************************/ - /* A type to store a value and the files defining this value - * (usualy a single file) */ +/* A type to store a value and the files defining this value + * (usualy a single file) */ struct definition { /* The value */ - unsigned property_value; + prop_id_t property_value; /* The IDs of the geometry sets that defined the value */ struct darray_uint set_ids; }; @@ -211,26 +212,9 @@ release_trg_descriptions #define DARRAY_FUNCTOR_RELEASE release_trg_descriptions #include <rsys/dynamic_array.h> -/******************************************************************************* - * A type to store interface IDs, as star-enclosures doesn't manage them. - ******************************************************************************/ -static FINLINE void -init_trg_intfaceid - (struct mem_allocator* alloc, - unsigned* data) -{ - ASSERT(data); (void)alloc; - *data = SG3D_UNSPECIFIED_PROPERTY; -} - -#define DARRAY_NAME intface_id -#define DARRAY_DATA unsigned -#define DARRAY_FUNCTOR_INIT init_trg_intfaceid -#include <rsys/dynamic_array.h> - -/******************************************************************************* +/****************************************************************************** * Types to store geometry amid sg3d_geometry_add calls. - ******************************************************************************/ + *****************************************************************************/ struct sg3d_geometry { /* Record unique (i.e. deduplicated) triangles */ struct darray_triangle unique_triangles; @@ -247,26 +231,26 @@ struct sg3d_geometry { /* Counts */ unsigned set_id; - unsigned triangle_count_including_duplicates; - unsigned sides_with_defined_medium_count; - unsigned trg_with_unspecified_sides_count; - unsigned trg_with_unspecified_intface_count; - unsigned merge_conflict_count; - unsigned properties_conflict_count; + trg_id_t triangle_count_including_duplicates; + side_id_t sides_with_defined_medium_count; + trg_id_t trg_with_unspecified_sides_count; + trg_id_t trg_with_unspecified_intface_count; + trg_id_t merge_conflict_count; + trg_id_t properties_conflict_count; struct sg3d_device* dev; ref_T ref; }; -/******************************************************************************* +/****************************************************************************** * Local functions - ******************************************************************************/ + *****************************************************************************/ extern LOCAL_SYM res_T geometry_register_triangle (struct sg3d_geometry* geometry, const struct triangle* triangle, - const unsigned triangle_unique_id, + const trg_id_t triangle_unique_id, const unsigned set_id, const int merge_conflict); @@ -274,6 +258,6 @@ geometry_register_triangle extern LOCAL_SYM res_T geometry_enlarge_trg_descriptions (struct sg3d_geometry* geom, - const size_t sz); + const trg_id_t sz); #endif /* SG3D_GEOMETRY_H__ */ diff --git a/src/sg3d_misc.h b/src/sg3d_misc.h @@ -16,6 +16,60 @@ #ifndef SG3D_MISC_H__ #define SG3D_MISC_H__ +#include <rsys/dynamic_array.h> + #define ERR(Expr) if((res = (Expr)) != RES_OK) goto error; +/* The following types must be defined accordingly with the types + * used in sg3d.h */ + +/* Trg IDs use the same type than Side IDs */ +typedef unsigned trg_id_t; +/* TRG_MAX__ is limited to half the max of the base type to allow to count +* sides */ +#define TRG_MAX__ (UINT_MAX/2) +#define TRG_NULL__ UINT_MAX +#define PRTF_TRG "%u" +#define TRG_TYPE_NAME "unsigned" + +/* Side IDs type use the same base type than Trg IDs */ +typedef trg_id_t side_id_t; +#define SIDE_MAX__ (2*TRG_MAX__) +#define SIDE_NULL__ TRG_NULL__ + +/* Vertex IDs type */ +typedef unsigned vrtx_id_t; +#define VRTX_MAX__ (UINT_MAX-1) +#define VRTX_NULL__ UINT_MAX +#define PRTF_VRTX "%u" +#define VRTX_TYPE_NAME "unsigned" + +#define DARRAY_NAME vertice_ids +#define DARRAY_DATA vrtx_id_t +#include <rsys/dynamic_array.h> + +/* Property IDs type. + * Cannot be larger than unsigned, as the API uses it. */ +typedef unsigned prop_id_t; +#define PROP_MAX__ (UINT_MAX-1) /* MAX is for unspecified medium */ +#define PROP_NULL__ UINT_MAX +#define PRTF_PROP "%u" +#define PROP_TYPE_NAME "unsigned" + +#if (PROP_MAX__+1 != SG3D_UNSPECIFIED_PROPERTY) +#error "Inconsistant values" +#endif + +/* Types of the callbacks. + * Provided callbacks are cast into these types to avoid arguments cast + * and to check types coherency */ +typedef void(*get_indices_t) (const trg_id_t, prop_id_t*, void*); +typedef void(*get_properties_t) (const trg_id_t, prop_id_t*, void*); +typedef void(*get_position_t) (const vrtx_id_t, double*, void*); +typedef res_T(*add_triangle_t) (const trg_id_t, const trg_id_t, void*); +typedef res_T(*merge_triangle_t) + (const trg_id_t, const trg_id_t, const int, prop_id_t*, const prop_id_t*, + void*, int*); +typedef res_T(*degenerated_triangle_t) (const trg_id_t, void*, int*); + #endif /* SG3D_MISC_H__ */ diff --git a/src/sgX3d_undefs.h b/src/sgX3d_undefs.h @@ -18,9 +18,9 @@ #endif /* Star-geometry-XD macros generic to the SGXD_DIM */ -#undef SENCXD -#undef sencXd -#undef SENCXD_ +#undef SGXD +#undef sgXd +#undef SGXD_ /* Function names that require additional dedicated macros */ #undef sgXd_geometry_get_added_primitives_count diff --git a/src/test_sg3d_many_triangles.c b/src/test_sg3d_many_triangles.c @@ -64,7 +64,7 @@ main(int argc, char** argv) OK(sg3d_geometry_reserve(geom, NB_CYL * cyl_vrtx_count, NB_CYL * cyl_trg_count, 0)); FOR_EACH(i, 0, NB_CYL) { m1 = i; - d3(ctx.ctx.offset, 0, 0, i * 10); + d3(ctx.ctx.offset, 0, 0, (double)i * 10); OK(sg3d_geometry_add(geom, cyl_vrtx_count, cyl_trg_count, &callbacks, &ctx)); } S3DUT(mesh_ref_put(cyl)); diff --git a/src/test_sg3d_some_triangles.c b/src/test_sg3d_some_triangles.c @@ -64,7 +64,7 @@ main(int argc, char** argv) OK(sg3d_geometry_reserve(geom, NB_CYL * cyl_vrtx_count, NB_CYL * cyl_trg_count, 0)); FOR_EACH(i, 0, NB_CYL) { m1 = i; - d3(ctx.ctx.offset, 0, 0, i * 10); + d3(ctx.ctx.offset, 0, 0, (double)i * 10); OK(sg3d_geometry_add(geom, cyl_vrtx_count, cyl_trg_count, &callbacks, &ctx)); } S3DUT(mesh_ref_put(cyl)); diff --git a/src/test_sg3d_utils.h b/src/test_sg3d_utils.h @@ -24,9 +24,9 @@ #define ME(Cond) CHK((Cond) == RES_MEM_ERR) -/******************************************************************************* +/****************************************************************************** * Memory allocator - ******************************************************************************/ + *****************************************************************************/ static INLINE void check_memory_allocator(struct mem_allocator* allocator) { @@ -38,9 +38,9 @@ check_memory_allocator(struct mem_allocator* allocator) } } -/******************************************************************************* +/****************************************************************************** * Geometry - ******************************************************************************/ + *****************************************************************************/ /* 3D cube */ static const double cube_vertices[8/*#vertices*/ * 3/*#coords per vertex*/] = { 0.0, 0.0, 0.0,