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 586500a494e4bbec421b2ea733966a164d00df6a
parent b905addcb8c666b5384ae7a24182affbdacc557c
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Thu, 12 Dec 2019 17:20:52 +0100

Fix dump as C and flatten the output arrays

Diffstat:
Msrc/sg3_geometry.c | 40+++++++++++++++++++---------------------
Msrc/star-geometry.h | 22++++++++++++++--------
Msrc/test_sg3_geometry.c | 7++++---
3 files changed, 37 insertions(+), 32 deletions(-)

diff --git a/src/sg3_geometry.c b/src/sg3_geometry.c @@ -668,7 +668,7 @@ sg3_geometry_dump_as_C_code const struct vertex* vertices; const struct triangle* triangles; size_t vsz, tsz, i; - if(!geom || !stream || !name_prefix + if(!geom || !stream || geom->merge_conflict_count || geom->properties_conflict_count || !geom->triangle_count_including_duplicates) @@ -686,12 +686,13 @@ sg3_geometry_dump_as_C_code res = RES_BAD_ARG; goto error; } + if(!name_prefix) name_prefix = ""; /* Headers */ fprintf(stream, "/* Dump of star-geometry. */\n"); vsz = darray_vertex_size_get(&geom->unique_vertices); - ASSERT(vsz <= UINT_MAX); + ASSERT(3 * vsz <= UINT_MAX); tsz = darray_triangle_size_get(&geom->unique_triangles); - ASSERT(tsz <= UINT_MAX); + ASSERT(3 * tsz <= UINT_MAX); if(vsz == 0 || tsz == 0) { log_err(geom->dev, @@ -704,47 +705,44 @@ sg3_geometry_dump_as_C_code /* Dump vertices */ vertices = darray_vertex_cdata_get(&geom->unique_vertices); fprintf(stream, - "const double %s_vertices[%u][3] =\n" + "const double %s_vertices[%u] =\n" " {\n", - name_prefix, (unsigned)vsz); + name_prefix, (unsigned)(3 * vsz)); FOR_EACH(i, 0, vsz - 1) fprintf(stream, - " { %f, %f, %f },\n", SPLIT3(vertices[i].coord)); + " %f, %f, %f,\n", SPLIT3(vertices[i].coord)); fprintf(stream, - " { %f, %f, %f }\n", SPLIT3(vertices[vsz - 1].coord)); + " %f, %f, %f\n", SPLIT3(vertices[vsz - 1].coord)); fprintf(stream, - " }" - "}\n"); + "};\n"); /* Dump triangles */ triangles = darray_triangle_cdata_get(&geom->unique_triangles); fprintf(stream, - "const unsigned %s_triangles[%u][3] =\n" + "const unsigned %s_triangles[%u] =\n" " {\n", - name_prefix, (unsigned)tsz); + name_prefix, (unsigned)(3 * tsz)); FOR_EACH(i, 0, tsz - 1) fprintf(stream, - " { %u, %u, %u },\n", SPLIT3(triangles[i].vertex_ids)); + " %u, %u, %u,\n", SPLIT3(triangles[i].vertex_ids)); fprintf(stream, - " { %u, %u, %u }\n", SPLIT3(triangles[tsz - 1].vertex_ids)); + " %u, %u, %u\n", SPLIT3(triangles[tsz - 1].vertex_ids)); fprintf(stream, - " }" - "}\n"); + "};\n"); /* Dump properties */ triangles = darray_triangle_cdata_get(&geom->unique_triangles); fprintf(stream, - "const unsigned %s_properties[%u][3] =\n" + "const unsigned %s_properties[%u] =\n" " {\n", - name_prefix, (unsigned)tsz); + name_prefix, (unsigned)(3 * tsz)); FOR_EACH(i, 0, tsz - 1) fprintf(stream, - " { %u, %u, %u },\n", SPLIT3(triangles[i].properties)); + " %u, %u, %u\n", SPLIT3(triangles[i].properties)); fprintf(stream, - " { %u, %u, %u }\n", SPLIT3(triangles[tsz - 1].properties)); + " %u, %u, %u\n", SPLIT3(triangles[tsz - 1].properties)); fprintf(stream, - " }" - "}\n"); + "};\n"); exit: return res; diff --git a/src/star-geometry.h b/src/star-geometry.h @@ -249,12 +249,13 @@ sg3_geometry_get_properties_conflict_count /* Dump a geometry in the provided stream in the OBJ format. * The geometry can include conflicts, but cannot be empty. - * The OBJ dump is made of the vertices and triangles, without properties, that - * have been added to the geometry. - * The part of the geometry that is dumped is defined by the flags argument, - * that should be ORed enum sg3_dump_content values. - * If more than 1 flag is used, triangle partitions are dumped in different - * OBJ groups. */ + * The dump is made of the vertices and some triangles, without their + * properties. The dumped triangles are defined by the flags argument, that + * must be ORed enum sg3_dump_content values. + * The dumped triangles are partitioned in the following groups: + * - Valid triangles + * - Merge conflicts + * - Property conflict */ SG3_API res_T sg3_geometry_dump_as_obj (const struct sg3_geometry* geometry, @@ -262,12 +263,17 @@ sg3_geometry_dump_as_obj int flags); /* Dump the geometry as C code. - * The geometry cannot be empty and must be conflict-free */ + * The geometry cannot be empty and must be conflict-free. + * The C code defines the 3 variables: + * - const double <name_prefix>_vertices[<N1>] = { .... }; + * - const unsigned <name_prefix>_triangles[<N2>] = { .... }; + * - const unsigned <name_prefix>_properties[<N2>] = { .... }; +* Where <N1> is 3 * vertices_count and <N2> is 3 * triangles_count. */ SG3_API res_T sg3_geometry_dump_as_C_code (const struct sg3_geometry* geometry, FILE* stream, - const char* name_prefix); + const char* name_prefix); /* Can be NULL or "" */ SG3_API res_T sg3_geometry_ref_get diff --git a/src/test_sg3_geometry.c b/src/test_sg3_geometry.c @@ -117,7 +117,7 @@ main(int argc, char** argv) BA(sg3_geometry_dump_as_obj(geom, stdout, 0)); BA(sg3_geometry_dump_as_obj(geom, NULL, SG3_ALL_TRIANGLES)); BA(sg3_geometry_dump_as_obj(NULL, stdout, SG3_ALL_TRIANGLES)); - /* Empty geometry */ + /* BA because geometry is empty */ BA(sg3_geometry_dump_as_obj(geom, stdout, SG3_ALL_TRIANGLES)); BA(sg3_geometry_dump_as_C_code(NULL, NULL, NULL)); @@ -125,9 +125,9 @@ main(int argc, char** argv) BA(sg3_geometry_dump_as_C_code(NULL, stdout, NULL)); BA(sg3_geometry_dump_as_C_code(NULL, NULL, "test")); BA(sg3_geometry_dump_as_C_code(geom, NULL, "test")); - BA(sg3_geometry_dump_as_C_code(geom, stdout, NULL)); BA(sg3_geometry_dump_as_C_code(NULL, stdout, "test")); - /* Empty geometry */ + /* BA because geometry is empty */ + BA(sg3_geometry_dump_as_C_code(geom, stdout, NULL)); BA(sg3_geometry_dump_as_C_code(geom, stdout, "test")); /* A 3D cube. @@ -145,6 +145,7 @@ main(int argc, char** argv) OK(sg3_geometry_add(geom, ntriangles, get_indices, get_properties, nvertices, get_position, NULL, NULL, &ctx)); OK(sg3_geometry_dump_as_obj(geom, stdout, SG3_ALL_TRIANGLES)); + OK(sg3_geometry_dump_as_C_code(geom, stdout, NULL)); OK(sg3_geometry_dump_as_C_code(geom, stdout, "test")); /* Conflicts with merge_trg callback */