commit c43cbc31957b721093754f24817c9a8f80717f4a
parent 5ac32698d7f0956f4af2def29024d5cb099eb7d1
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Fri, 28 Mar 2025 11:36:10 +0100
Add some debug code
If a write operation on triangles that requests normal orientation fails
due to some triangles having both sides in, output an STL file with the
double sided triangles to allow locate them.
Note that in this case (and others), the whole set of triangles is
output to a STL file without normal orientation, also to help debugging.
Diffstat:
| M | src/scad.c | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 50 insertions(+), 0 deletions(-)
diff --git a/src/scad.c b/src/scad.c
@@ -493,6 +493,9 @@ scad_stl_sort_orientation
char* tin = NULL;
char *contact_0 = NULL;
unsigned ocount = 0;
+ struct darray_double dblsided;
+ struct str dbl_name;
+ int dblsided_initialized = 0;
if(!triangles || !set_name) {
res = RES_BAD_ARG;
@@ -600,10 +603,53 @@ scad_stl_sort_orientation
enclosure = NULL;
}
if(two > 0) {
+ size_t idx;
+ res_T r;
log_error(get_device(),
"Triangle set '%s' define an invalid closed volume "
"(%u / %u triangles with both sides in).\n",
set_name, (unsigned)two, utcount_in);
+ /* Output the two-sides-in triangles */
+ darray_double_init(allocator, &dblsided);
+ str_init(allocator, &dbl_name);
+ dblsided_initialized = 1;
+ ERR(darray_double_reserve(&dblsided, 9 * two));
+ for(e = 0; e < ecount; e++) {
+ ERR(senc3d_scene_get_enclosure(senc3d_scn, e, &enclosure));
+ ERR(senc3d_enclosure_get_header(enclosure, &header));
+ if(header.primitives_count == header.unique_primitives_count) continue;
+ for(i = header.unique_primitives_count; i < header.primitives_count; i++) {
+ enum senc3d_side side;
+ unsigned gid, j, k;
+ unsigned trg[3];
+ double v[3];
+ ERR(senc3d_enclosure_get_triangle_id(enclosure, i, &gid, &side));
+ ERR(senc3d_enclosure_get_triangle(enclosure, gid, trg));
+ ASSERT(side == (SENC3D_FRONT | SENC3D_BACK));
+ for(j = 0; j < 3; j++) {
+ ERR(senc3d_enclosure_get_vertex(enclosure, trg[j], v));
+ for(k = 0; k < 3; k++) {
+ ERR(darray_double_push_back(&dblsided, v+k));
+ }
+ }
+ }
+ ERR(senc3d_enclosure_ref_put(enclosure));
+ enclosure = NULL;
+ }
+ idx = strlen(set_name);
+ ASSERT(idx > 3 && 0 == strcmp(set_name + idx - 4, ".stl"));
+ str_set(&dbl_name, set_name);
+ str_insert(&dbl_name, idx - 4, "_double_sided_triangles");
+ r = scad_stl_data_write(&dblsided, str_cget(&dbl_name),
+ Scad_keep_normals_unchanged, 0);
+ if(r == RES_OK) {
+ log_error(get_device(),
+ "Saved double sided triangles to file '%s'.\n",
+ str_cget(&dbl_name));
+ } else {
+ log_error(get_device(),
+ "Could not save double sided triangles to a file.\n");
+ }
res = RES_BAD_ARG;
goto error;
}
@@ -668,6 +714,10 @@ scad_stl_sort_orientation
exit:
MEM_RM(allocator, contact_0);
MEM_RM(allocator, tin);
+ if(dblsided_initialized) {
+ darray_double_release(&dblsided);
+ str_release(&dbl_name);
+ }
if(initialized) darray_double_release(&new_triangles);
if(sg3d) SG3D(device_ref_put(sg3d));
if(geom) SG3D(geometry_ref_put(geom));