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 2d410f86bc39497a57e279ff71637b7a5ff5237a
parent 96cdfd210c0dc6efab995c122a7497d16dbd73c9
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Thu,  5 Dec 2019 11:22:33 +0100

Add new API and test code

Diffstat:
Msrc/sg3_report.c | 96++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Msrc/star-geometry.h | 8++++++++
Msrc/test_sg3_report.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++--
3 files changed, 150 insertions(+), 5 deletions(-)

diff --git a/src/sg3_report.c b/src/sg3_report.c @@ -72,7 +72,7 @@ report_record_triangle int done = 0; size_t j; keep_prop_def[i] = trg_d->property_defined[i]; - if (triangle->properties[i] == SG3_UNDEFINED_PROPERTY) + if(triangle->properties[i] == SG3_UNDEFINED_PROPERTY) trg_d->defs_include_undefined = 1; else trg_d->property_defined[i] = 1; definitions = trg_d->defs + i; @@ -114,7 +114,7 @@ report_record_triangle } } - if ((!keep_prop_def[SG3_FRONT] || !keep_prop_def[SG3_BACK]) + if((!keep_prop_def[SG3_FRONT] || !keep_prop_def[SG3_BACK]) && trg_d->property_defined[SG3_FRONT] && trg_d->property_defined[SG3_BACK]) { /* Both sides are now defined */ @@ -122,7 +122,7 @@ report_record_triangle report->undef_side_count--; } - if (!keep_prop_def[SG3_INTFACE] && trg_d->property_defined[SG3_INTFACE]) { + if(!keep_prop_def[SG3_INTFACE] && trg_d->property_defined[SG3_INTFACE]) { /* Interface is now defined */ ASSERT(report->undef_intface_count > 0); report->undef_intface_count--; @@ -448,6 +448,96 @@ error: } res_T +sg3_report_dump_as_C_code +(const struct sg3_report* report, + FILE* stream, + const char* name_prefix) +{ + res_T res = RES_OK; + const struct vertex* vertices; + const struct triangle* triangles; + size_t vsz, tsz, i; + if(!report || !stream || !name_prefix || !report->associated + || report->merge_conflict_count > 0 || report->properties_conflict_count) + { + if(report && !report->associated) + log_err(report->dev, + "%s: report is not associated to a geometry\n", + FUNC_NAME); + if(report->merge_conflict_count > 0 || report->properties_conflict_count) + log_err(report->dev, + "%s: cannot dump geometries with conflict as C code\n", + FUNC_NAME); + res = RES_BAD_ARG; + goto error; + } + /* Headers */ + fprintf(stream, "/* Dump of star-geometry report. */\n"); + vsz = darray_vertex_size_get(&report->v_n_t->unique_vertices); + ASSERT(vsz <= UINT_MAX); + tsz = darray_triangle_size_get(&report->v_n_t->unique_triangles); + ASSERT(tsz <= UINT_MAX); + + if(vsz == 0 || tsz == 0) { + log_err(report->dev, + "%s: no geometry to dump\n", + FUNC_NAME); + res = RES_BAD_ARG; + goto error; + } + + /* Dump vertices */ + vertices = darray_vertex_cdata_get(&report->v_n_t->unique_vertices); + fprintf(stream, + "const double %s_vertices[%u][3] =\n" + " {\n", + name_prefix, (unsigned)vsz); + FOR_EACH(i, 0, vsz-1) + fprintf(stream, + " { %f, %f, %f },\n", SPLIT3(vertices[i].coord)); + fprintf(stream, + " { %f, %f, %f }\n", SPLIT3(vertices[vsz - 1].coord)); + fprintf(stream, + " }" + "}\n"); + + /* Dump triangles */ + triangles = darray_triangle_cdata_get(&report->v_n_t->unique_triangles); + fprintf(stream, + "const unsigned %s_triangles[%u][3] =\n" + " {\n", + name_prefix, (unsigned)tsz); + FOR_EACH(i, 0, tsz-1) + fprintf(stream, + " { %u, %u, %u },\n", SPLIT3(triangles[i].vertex_ids)); + fprintf(stream, + " { %u, %u, %u }\n", SPLIT3(triangles[tsz - 1].vertex_ids)); + fprintf(stream, + " }" + "}\n"); + + /* Dump properties */ + triangles = darray_triangle_cdata_get(&report->v_n_t->unique_triangles); + fprintf(stream, + "const unsigned %s_properties[%u][3] =\n" + " {\n", + name_prefix, (unsigned)tsz); + FOR_EACH(i, 0, tsz - 1) + fprintf(stream, + " { %u, %u, %u },\n", SPLIT3(triangles[i].properties)); + fprintf(stream, + " { %u, %u, %u }\n", SPLIT3(triangles[tsz - 1].properties)); + fprintf(stream, + " }" + "}\n"); + +exit: + return res; +error: + goto exit; +} + +res_T sg3_report_ref_get(struct sg3_report* report) { if(!report) return RES_BAD_ARG; diff --git a/src/star-geometry.h b/src/star-geometry.h @@ -185,6 +185,14 @@ sg3_report_dump_as_obj FILE* stream, int flags); +/* Dump the report as C code. + * The associated geometry cannot be empty and must be conflict-free */ +SG3_API res_T +sg3_report_dump_as_C_code + (const struct sg3_report* report, + FILE* stream, + const char* name_prefix); + SG3_API res_T sg3_report_ref_get (struct sg3_report* report); diff --git a/src/test_sg3_report.c b/src/test_sg3_report.c @@ -16,6 +16,8 @@ #include "star-geometry.h" #include "test_sg3_utils.h" +#include <rsys/double3.h> + #include <stdio.h> static res_T @@ -30,6 +32,22 @@ validate return RES_OK; } +static res_T +merge_trg + (const unsigned global_id, + const unsigned itri, + const int reversed_triangle, + unsigned triangle_properties[3], + const unsigned merged_properties[3], + void* context, + int* merge_conflict) +{ + ASSERT(merge_conflict); + (void)global_id; (void)itri; (void)reversed_triangle; (void)context; + (void)triangle_properties; (void)merged_properties; (void)merge_conflict; + *merge_conflict = itri; + return RES_OK; +} int main(int argc, char** argv) { @@ -37,8 +55,9 @@ main(int argc, char** argv) struct sg3_device* dev; struct sg3_report* report; struct sg3_geometry* geometry; - unsigned count; - (void)argc, (void)argv; + unsigned count;; + struct context ctx; + (void)argc, (void)argv; OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator)); OK(sg3_device_create(NULL, &allocator, 1, &dev)); @@ -104,6 +123,7 @@ main(int argc, char** argv) BA(sg3_report_dump_as_obj(NULL, stdout, SG3_ALL_TRIANGLES)); /* Not associated to a geometry */ BA(sg3_report_dump_as_obj(report, stdout, SG3_ALL_TRIANGLES)); + BA(sg3_report_dump_as_C_code(report, stdout, "test")); OK(sg3_geometry_create(dev, report, &geometry)); OK(sg3_report_validate_properties(report, validate, NULL)); @@ -120,6 +140,33 @@ main(int argc, char** argv) OK(sg3_report_get_properties_conflict_count(report, &count)); CHK(count == 0); OK(sg3_report_dump_as_obj(report, stdout, SG3_ALL_TRIANGLES)); + /* Empty geometry */ + BA(sg3_report_dump_as_C_code(report, stdout, "test")); + + /* A 3D cube. + * 2 enclosures (inside, outside) sharing the same triangles, + * but opposite sides */ + ctx.positions = cube_vertices; + ctx.indices = cube_indices; + ctx.scale = 1; + ctx.reverse_vrtx = 0; + ctx.reverse_med = 0; + d3(ctx.offset, 0, 0, 0); + ctx.front_media = medium0; + ctx.back_media = medium1; + ctx.intface = intface0; + ctx.custom = NULL; + + OK(sg3_geometry_add(geometry, 2, get_indices, get_media, + get_intface, 6, get_position, NULL, NULL, &ctx)); + OK(sg3_report_dump_as_C_code(report, stdout, "test")); + /* Conflicts */ + ctx.front_media = medium1; + OK(sg3_geometry_add(geometry, 2, get_indices, get_media, + get_intface, 6, get_position, NULL, merge_trg, &ctx)); + + BA(sg3_report_dump_as_C_code(report, stdout, "test")); + OK(sg3_geometry_ref_put(geometry)); OK(sg3_report_ref_put(report));