commit db7bfdf1058db9ac8b05308281a218fdd679ac20
parent 58e54e362bc4ad48db117bebce1d928d0ed442e3
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Wed, 9 Jul 2025 11:12:04 +0200
Change API for get_closest_point
Add an optional argument to return the underlying geometry of the
closest point.
The tests are not updated yet.
Diffstat:
2 files changed, 25 insertions(+), 5 deletions(-)
diff --git a/src/scad.h b/src/scad.h
@@ -394,14 +394,18 @@ scad_geometry_get_centerofmass
(struct scad_geometry* geometry,
double* center);
-/* Get the `closest- point on geometry `geometry' from point `from' and its
- * `distance'. */
+/* Get the `closest' point on geometry `geometry' from point `from'.
+ * Return the `closest' point and its `distance'.
+ * The underlying 2D geometry on wich the closest point is located is returned
+ * as a new geometry in `closest_geom' if it is not NULL.
+ * If `geometry' is 3D, this underlying geometry is (a part of) its boundary. */
SCAD_API res_T
scad_geometry_get_closest_point
(struct scad_geometry* geometry,
const double from[3],
double closest[3],
- double* distance);
+ double* distance,
+ struct scad_geometry** closest_geom); /* Can be NULL */
/* Get the normal of the geometry `geometry' at position `p'.
* The normal is set in `N' and the underlying 2D entity to which `p' belongs is
diff --git a/src/scad_geometry.c b/src/scad_geometry.c
@@ -726,7 +726,8 @@ scad_geometry_get_closest_point
(struct scad_geometry* geom,
const double from[3],
double closest[3],
- double* closest_distance)
+ double* closest_distance,
+ struct scad_geometry** out_geometry)
{
res_T res = RES_OK;
size_t i = 0;
@@ -737,7 +738,7 @@ scad_geometry_get_closest_point
struct darray_int tags;
const int* data = NULL;
size_t sz = 0;
- int initialized = 0;
+ int initialized = 0, min_tag = -1;
if(!geom || !from || !closest) {
res = RES_BAD_ARG;
@@ -769,6 +770,7 @@ scad_geometry_get_closest_point
ASSERT(coord_n == 3);
d = d3_len(d3_sub(tmp, from, coord));
if(d < min_d) {
+ min_tag = tag;
min_d = d;
d3_set(min, coord);
}
@@ -776,8 +778,22 @@ scad_geometry_get_closest_point
gmshFree(pcoord);
coord = pcoord = NULL;
}
+ ASSERT(min_tag != -1);
d3_set(closest, min);
*closest_distance = min_d;
+ if(out_geometry) {
+ ERR(geometry_create(out_geometry));
+ (*out_geometry)->gmsh_dimTags =
+ MEM_ALLOC(dev->allocator, 2 * sizeof(*(*out_geometry)->gmsh_dimTags));
+ if(!(*out_geometry)->gmsh_dimTags) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ (*out_geometry)->gmsh_dimTags_n = 2;
+ (*out_geometry)->gmsh_dimTags[0] = 2;
+ (*out_geometry)->gmsh_dimTags[1] = min_tag;
+ ERR(device_register_tags(*out_geometry));
+ }
exit:
if(initialized) darray_int_release(&tags);