star-cad

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

commit 379de06d892ede315f5979614dade1b01f4ae811
parent cc41bf6e24aa42b0322b7936443fbb55e6b04900
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Wed, 10 Jul 2024 11:26:40 +0200

BugFix: missing output triangles when forcing normals

The code used to force normal's orientation had 2 problems:
1) Some triangles where output more than once.
2) There was a typo in the count when writting triangles.
The conjonction of the 2 problems resulted in some triangles being
output more than once, and other triangles missing in output.

Diffstat:
Msrc/scad.c | 17+++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/scad.c b/src/scad.c @@ -25,6 +25,7 @@ #include <rsys/str.h> #include <rsys/dynamic_array_int.h> #include <rsys/dynamic_array_double.h> +#include <rsys/dynamic_array_char.h> #include <rsys/double3.h> #include <rsys/float3.h> @@ -489,6 +490,7 @@ scad_stl_sort_orientation int convention, initialized = 0; struct sg3d_geometry_add_callbacks callbacks = SG3D_ADD_CALLBACKS_NULL__; struct darray_double new_triangles; + char* tin = NULL; char *process = NULL, *contact_0 = NULL; unsigned ocount = 0, process_count = 0; @@ -634,11 +636,21 @@ scad_stl_sort_orientation } /* Just a guess */ ERR(darray_double_reserve(&new_triangles, 9 * tcount_in)); + tin = MEM_CALLOC(allocator, 1, 9 * tcount_in); + if(!tin) { + res= RES_MEM_ERR; + goto error; + } for(e = 1; e < ecount; e++) { ERR(senc3d_scene_get_enclosure(senc3d_scn, e, &enclosure)); ERR(senc3d_enclosure_get_header(enclosure, &header)); for(i = 0; i < header.unique_primitives_count; i++) { - unsigned n, k, vrt[3]; + unsigned n, k, idx, vrt[3]; + enum senc3d_side side; + /* Ensure that input triangles are included only once */ + ERR(senc3d_enclosure_get_triangle_id(enclosure, i, &idx, &side)); + if(tin[idx]) continue; /* Allready in output */ + tin[idx] = 1; /* Get the vertices as they are ordered in the enclosure's mesh */ ERR(senc3d_enclosure_get_triangle(enclosure, i, vrt)); /* Rewrite vertices according to enclosure's mesh */ @@ -660,6 +672,7 @@ scad_stl_sort_orientation exit: MEM_RM(allocator, process); MEM_RM(allocator, contact_0); + MEM_RM(allocator, tin); if(initialized) darray_double_release(&new_triangles); if(sg3d) SG3D(device_ref_put(sg3d)); if(geom) SG3D(geometry_ref_put(geom)); @@ -704,7 +717,7 @@ scad_stl_data_write /* If sort_orientation fails, try to write the file anyway to allow debugging */ tmp_res = scad_stl_sort_orientation(&sorted, filename, orientation); coord = darray_double_data_get(&sorted); - coord_n = darray_double_size_get(triangles); + coord_n = darray_double_size_get(&sorted); if(binary) ERR(write_binary_stl(filename, coord, coord_n/9)); else ERR(write_ascii_stl(filename, coord, coord_n/9)); ERR(tmp_res);