commit 0150e13b08c8cc08169c8fde75ebed2b1ca55d41
parent 4774911797f3283aed4421556280ba1b7eea9996
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Fri, 22 Aug 2025 18:19:33 +0200
Add the scad_add_cone and scad_add_torus API calls
Diffstat:
| M | src/scad.h | | | 28 | ++++++++++++++++++++++++++++ |
| M | src/scad_geometry.c | | | 106 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 134 insertions(+), 0 deletions(-)
diff --git a/src/scad.h b/src/scad.h
@@ -279,6 +279,34 @@ scad_add_sphere
const double radius,
struct scad_geometry** sphere);
+/* Add a `cone' to the scene, defined by the center `center' of its first
+ * circular face, the vector `axis' defining its axis and its 2 radii `radius1'
+ * and `radius2'. Note that the 2 radii cannot be identical (one of them can be
+ * zero).
+ * The `angle' argument defines the angular opening (from 0 to 2*PI). */
+SCAD_API res_T
+scad_add_cone
+ (const double center[3],
+ const double axis[3],
+ const double radius1,
+ const double radius2,
+ const double angle,
+ struct scad_geometry** cone);
+
+/* Add a `torus' to the scene, defined by its `center', the vector `axis'
+ * defining its axis and its 2 positive radii `radius1' and `radius2'.
+ * The `angle' argument defines the angular opening (from 0 to 2*PI).
+ * If `z_axis' is provided, it defines the Z axis of the torus. Otherwise the
+ * absolute Z axis is used. */
+SCAD_API res_T
+scad_add_torus
+ (const double center[3],
+ const double radius1,
+ const double radius2,
+ const double angle,
+ const double z_axis[3], /* Can be NULL */
+ struct scad_geometry** torus);
+
/* Scale the geometry `geometry' by factors `scale' along the three coordinate axes;
* Use `center', as the center of the homothetic transformation. */
SCAD_API res_T
diff --git a/src/scad_geometry.c b/src/scad_geometry.c
@@ -1397,6 +1397,112 @@ error:
}
SCAD_API res_T
+scad_add_cone
+ (const double center[3],
+ const double axis[3],
+ const double radius1,
+ const double radius2,
+ const double angle,
+ struct scad_geometry** cone)
+{
+ res_T res = RES_OK;
+ int ierr, gmsh_ID;
+ struct scad_geometry* geom = NULL;
+ struct scad_device* dev = get_device();
+ struct mem_allocator* allocator = NULL;
+
+ if(!center || !axis || radius1 < 0 || radius2 < 0 || (radius1 == radius2)
+ || angle < 0 || angle > 2*PI || !cone)
+ {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ ERR(check_device(FUNC_NAME));
+ allocator = dev->allocator;
+
+ gmsh_ID =
+ gmshModelOccAddCone(SPLIT3(center), SPLIT3(axis), radius1, radius2,
+ -1, 2*PI, &ierr);
+ ERR(gmsh_err_to_res_T(ierr));
+
+ ERR(geometry_create(&geom));
+ geom->gmsh_dimTags_n = 2;
+ geom->gmsh_dimTags = MEM_ALLOC(allocator, 2 * sizeof(*geom->gmsh_dimTags));
+ if(!geom->gmsh_dimTags) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ geom->gmsh_dimTags[0] = 3;
+ geom->gmsh_dimTags[1] = gmsh_ID;
+
+ ERR(device_register_tags(geom));
+
+exit:
+ if(cone) *cone = geom;
+ return res;
+error:
+ if(geom) {
+ SCAD(geometry_ref_put(geom));
+ geom = NULL;
+ }
+ goto exit;
+}
+
+SCAD_API res_T
+scad_add_torus
+ (const double center[3],
+ const double radius1,
+ const double radius2,
+ const double angle,
+ const double z_axis[3], /* Can be NULL */
+ struct scad_geometry** torus)
+{
+ res_T res = RES_OK;
+ int ierr, gmsh_ID;
+ struct scad_geometry* geom = NULL;
+ struct scad_device* dev = get_device();
+ struct mem_allocator* allocator = NULL;
+
+ if(!center || radius1 <= 0 || radius2 <= 0 || angle < 0 || angle > 2*PI
+ || !torus)
+ {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ ERR(check_device(FUNC_NAME));
+ allocator = dev->allocator;
+
+ gmsh_ID =
+ gmshModelOccAddTorus(SPLIT3(center), radius1, radius2,
+ -1, 2*PI, z_axis, (z_axis == NULL ? 0 : 3), &ierr);
+ ERR(gmsh_err_to_res_T(ierr));
+
+ ERR(geometry_create(&geom));
+ geom->gmsh_dimTags_n = 2;
+ geom->gmsh_dimTags = MEM_ALLOC(allocator, 2 * sizeof(*geom->gmsh_dimTags));
+ if(!geom->gmsh_dimTags) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ geom->gmsh_dimTags[0] = 3;
+ geom->gmsh_dimTags[1] = gmsh_ID;
+
+ ERR(device_register_tags(geom));
+
+exit:
+ if(torus) *torus = geom;
+ return res;
+error:
+ if(geom) {
+ SCAD(geometry_ref_put(geom));
+ geom = NULL;
+ }
+ goto exit;
+}
+
+SCAD_API res_T
scad_geometry_equal
(const struct scad_geometry* geom1,
const struct scad_geometry* geom2,