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