Tweaks for planar bounding_bax()

This commit is contained in:
Garth Minette 2021-01-07 20:16:28 -08:00
parent 694335b4e3
commit 7eb66b2eab
2 changed files with 32 additions and 16 deletions

View file

@ -14,28 +14,38 @@
// Usage: // Usage:
// bounding_box() ... // bounding_box() ...
// Description: // Description:
// Returns an axis-aligned cube shape that exactly contains all the 3D children given. // Returns the smallest axis-aligned square (or cube) shape that contains all the 2D (or 3D)
// children given. The module children() is supposed to be a 3d shape when planar=false and
// a 2d shape when planar=true otherwise the system will issue a warning of mixing dimension
// or scaling by 0.
// Arguments: // Arguments:
// excess = The amount that the bounding box should be larger than needed to bound the children, in each axis. // excess = The amount that the bounding box should be larger than needed to bound the children, in each axis.
// planar = If true, creates a 2D bounding rectangle. Is false, creates a 3D bounding cube. Default: false // planar = If true, creates a 2D bounding rectangle. Is false, creates a 3D bounding cube. Default: false
// Example: // Example(3D):
// #bounding_box() { // module shapes() {
// translate([10,8,4]) cube(5); // translate([10,8,4]) cube(5);
// translate([3,0,12]) cube(2); // translate([3,0,12]) cube(2);
// } // }
// translate([10,8,4]) cube(5); // #bounding_box() shapes();
// translate([3,0,12]) cube(2); // shapes();
// Example(2D):
// module shapes() {
// translate([10,8]) square(5);
// translate([3,0]) square(2);
// }
// #bounding_box(planar=true) shapes();
// shapes();
module bounding_box(excess=0, planar=false) { module bounding_box(excess=0, planar=false) {
xs = excess>.1? excess : 1; // a 3d (or 2d when planar=true) approx. of the children projection on X axis
// a 3D approx. of the children projection on X axis module _xProjection() {
module _xProjection()
if (planar) { if (planar) {
projection() projection()
rotate([90,0,0]) rotate([90,0,0])
linear_extrude(xs, center=true) linear_extrude(1, center=true)
hull() hull()
children(); children();
} else { } else {
xs = excess<.1? 1: excess;
linear_extrude(xs, center=true) linear_extrude(xs, center=true)
projection() projection()
rotate([90,0,0]) rotate([90,0,0])
@ -44,6 +54,7 @@ module bounding_box(excess=0, planar=false) {
hull() hull()
children(); children();
} }
}
// a bounding box with an offset of 1 in all axis // a bounding box with an offset of 1 in all axis
module _oversize_bbox() { module _oversize_bbox() {
@ -61,18 +72,23 @@ module bounding_box(excess=0, planar=false) {
} }
} }
// offsets a cube by `excess`
module _shrink_cube() { module _shrink_cube() {
intersection() { intersection() {
translate((1-excess)*[ 1, 1, planar?0: 1]) children(); translate((1-excess)*[ 1, 1, 1]) children();
translate((1-excess)*[-1,-1, planar?0:-1]) children(); translate((1-excess)*[-1,-1,-1]) children();
} }
} }
render(convexity=2) if(planar) {
if (excess>.1) { offset(excess-1/2) _oversize_bbox() children();
_oversize_bbox() children();
} else { } else {
_shrink_cube() _oversize_bbox() children(); render(convexity=2)
if (excess>.1) {
_oversize_bbox() children();
} else {
_shrink_cube() _oversize_bbox() children();
}
} }
} }

View file

@ -6,7 +6,7 @@
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
BOSL_VERSION = [2,0,520]; BOSL_VERSION = [2,0,521];
// Section: BOSL Library Version Functions // Section: BOSL Library Version Functions