From 70a6b7fac8b61d08df45e5de8c35bbb5d629c692 Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Wed, 9 Dec 2020 23:43:08 -0800 Subject: [PATCH 1/2] Fixed some undef warnings in distributors. --- distributors.scad | 18 ++++++++++-- math.scad | 57 +++++++++++++++++++++++++++++++++++++ tests/test_math.scad | 68 ++++++++++++++++++++++++++++++++++++++++++++ version.scad | 2 +- 4 files changed, 141 insertions(+), 4 deletions(-) diff --git a/distributors.scad b/distributors.scad index ff3482e..026dc72 100644 --- a/distributors.scad +++ b/distributors.scad @@ -171,7 +171,11 @@ module line_of(spacing, n, l, p1, p2) module xcopies(spacing, n, l, sp) { sp = is_finite(sp)? [sp,0,0] : sp; - line_of(l=l*RIGHT, spacing=spacing*RIGHT, n=n, p1=sp) children(); + line_of( + l=u_mul(l,RIGHT), + spacing=u_mul(spacing,RIGHT), + n=n, p1=sp + ) children(); } @@ -207,7 +211,11 @@ module xcopies(spacing, n, l, sp) module ycopies(spacing, n, l, sp) { sp = is_finite(sp)? [0,sp,0] : sp; - line_of(l=l*BACK, spacing=spacing*BACK, n=n, p1=sp) children(); + line_of( + l=u_mul(l,BACK), + spacing=u_mul(spacing,BACK), + n=n, p1=sp + ) children(); } @@ -243,7 +251,11 @@ module ycopies(spacing, n, l, sp) module zcopies(spacing, n, l, sp) { sp = is_finite(sp)? [0,0,sp] : sp; - line_of(l=l*UP, spacing=spacing*UP, n=n, p1=sp) children(); + line_of( + l=u_mul(l,UP), + spacing=u_mul(spacing,UP), + n=n, p1=sp + ) children(); } diff --git a/math.scad b/math.scad index 6cb1362..a2b811d 100644 --- a/math.scad +++ b/math.scad @@ -169,6 +169,63 @@ function lerp(a,b,u) = +// Section: Undef Safe Math + +// Function: u_add() +// Usage: +// x = u_add(a, b); +// Description: +// Adds `a` to `b`, returning the result, or undef if either value is `undef`. +// This emulates the way undefs used to be handled in versions of OpenSCAD before 2020. +// Arguments: +// a = First value. +// b = Second value. +function u_add(a,b) = is_undef(a) || is_undef(b)? undef : a + b; + + +// Function: u_sub() +// Usage: +// x = u_sub(a, b); +// Description: +// Subtracts `b` from `a`, returning the result, or undef if either value is `undef`. +// This emulates the way undefs used to be handled in versions of OpenSCAD before 2020. +// Arguments: +// a = First value. +// b = Second value. +function u_sub(a,b) = is_undef(a) || is_undef(b)? undef : a - b; + + +// Function: u_mul() +// Usage: +// x = u_mul(a, b); +// Description: +// Multiplies `a` by `b`, returning the result, or undef if either value is `undef`. +// This emulates the way undefs used to be handled in versions of OpenSCAD before 2020. +// Arguments: +// a = First value. +// b = Second value. +function u_mul(a,b) = + is_undef(a) || is_undef(b)? undef : + is_vector(a) && is_vector(b)? vmul(a,b) : + a * b; + + +// Function: u_div() +// Usage: +// x = u_div(a, b); +// Description: +// Divides `a` by `b`, returning the result, or undef if either value is `undef`. +// This emulates the way undefs used to be handled in versions of OpenSCAD before 2020. +// Arguments: +// a = First value. +// b = Second value. +function u_div(a,b) = + is_undef(a) || is_undef(b)? undef : + is_vector(a) && is_vector(b)? vdiv(a,b) : + a / b; + + + // Section: Hyperbolic Trigonometry // Function: sinh() diff --git a/tests/test_math.scad b/tests/test_math.scad index 56f3ae2..2103eb8 100644 --- a/tests/test_math.scad +++ b/tests/test_math.scad @@ -382,6 +382,74 @@ module test_lerp() { test_lerp(); +module test_u_add() { + assert_equal(u_add(1,2),3); + assert_equal(u_add(1,-2),-1); + assert_equal(u_add(-1,2),1); + assert_equal(u_add(-1,-2),-3); + assert_equal(u_add(243,-27),216); + assert_equal(u_add([2,3,4],[8,7,9]),[10,10,13]); + assert_equal(u_add(undef,27),undef); + assert_equal(u_add(undef,-27),undef); + assert_equal(u_add(243,undef),undef); + assert_equal(u_add(-43,undef),undef); + assert_equal(u_add(undef,[8,7,9]),undef); + assert_equal(u_add([2,3,4],undef),undef); +} +test_u_add(); + + +module test_u_sub() { + assert_equal(u_sub(1,2),-1); + assert_equal(u_sub(1,-2),3); + assert_equal(u_sub(-1,2),-3); + assert_equal(u_sub(-1,-2),1); + assert_equal(u_sub(243,-27),270); + assert_equal(u_sub([2,3,4],[8,7,9]),[-6,-4,-5]); + assert_equal(u_sub(undef,27),undef); + assert_equal(u_sub(undef,-27),undef); + assert_equal(u_sub(243,undef),undef); + assert_equal(u_sub(-43,undef),undef); + assert_equal(u_sub(undef,[8,7,9]),undef); + assert_equal(u_sub([2,3,4],undef),undef); +} +test_u_sub(); + + +module test_u_mul() { + assert_equal(u_mul(3,2),6); + assert_equal(u_mul(3,-2),-6); + assert_equal(u_mul(-3,2),-6); + assert_equal(u_mul(-3,-2),6); + assert_equal(u_mul(243,-27),-6561); + assert_equal(u_mul([2,3,4],[8,7,9]),[16,21,36]); + assert_equal(u_mul(undef,27),undef); + assert_equal(u_mul(undef,-27),undef); + assert_equal(u_mul(243,undef),undef); + assert_equal(u_mul(-43,undef),undef); + assert_equal(u_mul(undef,[8,7,9]),undef); + assert_equal(u_mul([2,3,4],undef),undef); +} +test_u_mul(); + + +module test_u_div() { + assert_equal(u_div(1,2),1/2); + assert_equal(u_div(1,-2),-1/2); + assert_equal(u_div(-1,2),-1/2); + assert_equal(u_div(-1,-2),1/2); + assert_equal(u_div(243,-27),-9); + assert_equal(u_div([8,7,9],[2,3,4]),[4,7/3,9/4]); + assert_equal(u_div(undef,27),undef); + assert_equal(u_div(undef,-27),undef); + assert_equal(u_div(243,undef),undef); + assert_equal(u_div(-43,undef),undef); + assert_equal(u_div(undef,[8,7,9]),undef); + assert_equal(u_div([2,3,4],undef),undef); +} +test_u_div(); + + module test_hypot() { assert_approx(hypot(20,30), norm([20,30])); } diff --git a/version.scad b/version.scad index dfc5f4f..37c60db 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,474]; +BOSL_VERSION = [2,0,475]; // Section: BOSL Library Version Functions From 4a25761b9a538434a278b9d3072e6bec893566f8 Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Wed, 9 Dec 2020 23:44:22 -0800 Subject: [PATCH 2/2] Added BOSL2 logo example. --- examples/BOSL2logo.scad | 53 +++++++++++++++++++++++++++++++++++++++++ version.scad | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 examples/BOSL2logo.scad diff --git a/examples/BOSL2logo.scad b/examples/BOSL2logo.scad new file mode 100644 index 0000000..8828b52 --- /dev/null +++ b/examples/BOSL2logo.scad @@ -0,0 +1,53 @@ +include +include +include +include +include + +$fa=1; +$fs=1; + +xdistribute(50) { + recolor("#f77") + diff("hole") + cuboid([45,45,10], chamfer=10, edges=[RIGHT+BACK,RIGHT+FRONT], anchor=FRONT) { + cuboid([30,30,11], chamfer=5, edges=[RIGHT+BACK,RIGHT+FRONT], $tags="hole"); + attach(FRONT,BACK, overlap=5) { + diff("hole") + cuboid([45,45,10], rounding=15, edges=[RIGHT+BACK,RIGHT+FRONT]) { + cuboid([30,30,11], rounding=10, edges=[RIGHT+BACK,RIGHT+FRONT], $tags="hole"); + } + } + } + + recolor("#7f7") + bevel_gear(pitch=8, teeth=20, face_width=12, shaft_diam=25, pitch_angle=45, slices=12, spiral_angle=30); + + x = 18; + y = 20; + s1 = 25; + s2 = 20; + sbez = [ + [-x,-y], [-x,-y-s1], + [ x,-y-s1], [ x,-y], [ x,-y+s2], + [-x, y-s2], [-x, y], [-x, y+s1], + [ x, y+s1], [ x, y] + ]; + recolor("#99f") + path_sweep(regular_ngon(n=3,d=10,spin=90), bezier_path(sbez)); + + recolor("#0bf") + translate([-15,-35,0]) + cubetruss_corner(size=10, strut=1, h=1, bracing=false, extents=[3,8,0,0,0], clipthick=0); + + recolor("#777") + xdistribute(24) { + screw("M12,70", head="hex", anchor="origin", orient=BACK) + attach(BOT,CENTER) + nut("M12", thickness=10, diameter=20, details=true); + screw("M12,70", head="hex", anchor="origin", orient=BACK) + attach(BOT,CENTER) + nut("M12", thickness=10, diameter=20, details=true); + } +} + diff --git a/version.scad b/version.scad index 37c60db..f295346 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,475]; +BOSL_VERSION = [2,0,476]; // Section: BOSL Library Version Functions