From 7aebad420c60c2684ede69c3565379284910d3e3 Mon Sep 17 00:00:00 2001 From: Arthur Moore Date: Thu, 2 May 2024 00:26:54 -0400 Subject: [PATCH] Re-worked generic-helpers.rounded_square * Now behaves more like rounded_rectangle, but creates 2d or 3d items. * Deprecated rounded_rectangle. * Added significant input checks to ensure values are valid. --- generic-helpers.scad | 51 +++++++++++++++++++++++++++++++------ gridfinity-spiral-vase.scad | 12 ++++++--- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/generic-helpers.scad b/generic-helpers.scad index 43a32b7..0a1f0ec 100644 --- a/generic-helpers.scad +++ b/generic-helpers.scad @@ -7,15 +7,50 @@ function clp(x,a,b) = min(max(x,a),b); function is_even(number) = (number%2)==0; -module rounded_rectangle(length, width, height, rad) { - linear_extrude(height) - offset(rad) - offset(-rad) - square([length,width], center = true); +/** + * @brief Create `square`, with rounded corners. + * @param size Same as `square`. See details for differences. + * @param radius Radius of the corners. 0 is the same as just calling `square` + * @param center Same as `square`. + * @details "size" accepts both the standard number or a 2d vector the same as `square`. + * However, if passed a 3d vector, this will apply a `linear_extrude` to the resulting shape. + */ +module rounded_square(size, radius, center = true) { + assert(is_num(size) || + (is_list(size) && ( + (len(size) == 2 && is_num(size.x) && is_num(size.y)) || + (len(size) == 3 && is_num(size.x) && is_num(size.y) && is_num(size.z)) + )) + ); + assert(is_num(radius) && radius >= 0 && is_bool(center)); + + // Make sure something is produced. + if (is_num(size)) { + assert((size/2) > radius); + } else { + assert((size.x/2) > radius && (size.y/2 > radius)); + if (len(size) == 3) { + assert(size.z > 0); + } + } + + if (is_list(size) && len(size) == 3) { + linear_extrude(size.z) + offset(radius) + offset(-radius) + square([size.x, size.y], center = center); + } else { + offset(radius) + offset(-radius) + square(size, center = center); + } } -module rounded_square(length, height, rad) { - rounded_rectangle(length, length, height, rad); +/** + * @deprecated Use rounded_square(...) + */ +module rounded_rectangle(length, width, height, rad) { + rounded_square([length, width, height], rad); } module copy_mirror(vec=[0,1,0]) { @@ -56,7 +91,7 @@ unity_matrix = [ * @param vector A 2d or 3d vectorm * @returns Magnitude of the vector. */ - function vector_magnitude(vector) = +function vector_magnitude(vector) = sqrt(vector.x^2 + vector.y^2 + (len(vector) == 3 ? vector.z^2 : 0)); /** diff --git a/gridfinity-spiral-vase.scad b/gridfinity-spiral-vase.scad index e03ebd7..f8b13ea 100644 --- a/gridfinity-spiral-vase.scad +++ b/gridfinity-spiral-vase.scad @@ -219,13 +219,17 @@ module block_magnet_blank(o = 0, half = true) { module block_base_blank(o = 0) { mirror([0,0,1]) { hull() { - rounded_square(l_grid-o-0.05-2*r_c2-2*r_c1, h_base, r_fo3); - rounded_square(l_grid-o-0.05-2*r_c2, h_base-r_c1, r_fo2); + linear_extrude(h_base) + rounded_square(l_grid-o-0.05-2*r_c2-2*r_c1, r_fo3); + linear_extrude(h_base-r_c1) + rounded_square(l_grid-o-0.05-2*r_c2, r_fo2); } hull() { - rounded_square(l_grid-o-0.05-2*r_c2, r_c2, r_fo2); + linear_extrude(r_c2) + rounded_square(l_grid-o-0.05-2*r_c2, r_fo2); mirror([0,0,1]) - rounded_square(l_grid-o-0.05, d_bottom, r_fo1); + linear_extrude(d_bottom) + rounded_square(l_grid-o-0.05, r_fo1); } } }