Cosmetics

This commit is contained in:
RonaldoCMP 2021-04-11 12:34:59 +01:00
parent f9e7baa515
commit 376609d52f
2 changed files with 114 additions and 113 deletions

View file

@ -205,7 +205,8 @@ function is_func(x) = version_num()>20210000 && is_function(x);
// Description: // Description:
// Tests whether input is a list of entries which all have the same list structure // Tests whether input is a list of entries which all have the same list structure
// and are filled with finite numerical data. You can optionally specify a required // and are filled with finite numerical data. You can optionally specify a required
// list structure with the pattern argument. It returns `true` for the empty list. // list structure with the pattern argument.
// It returns `true` for the empty list regardless the value of the `pattern`.
// Arguments: // Arguments:
// list = list to check // list = list to check
// pattern = optional pattern required to match // pattern = optional pattern required to match
@ -293,7 +294,7 @@ function default(v,dflt=undef) = is_undef(v)? dflt : v;
// v = The list whose items are being checked. // v = The list whose items are being checked.
// recursive = If true, sublists are checked recursively for defined values. The first sublist that has a defined item is returned. // recursive = If true, sublists are checked recursively for defined values. The first sublist that has a defined item is returned.
// Examples: // Examples:
// val = first_defined([undef,7,undef,true]); // Returns: 1 // val = first_defined([undef,7,undef,true]); // Returns: 7
function first_defined(v,recursive=false,_i=0) = function first_defined(v,recursive=false,_i=0) =
_i<len(v) && ( _i<len(v) && (
is_undef(v[_i]) || ( is_undef(v[_i]) || (
@ -605,15 +606,15 @@ function segs(r) =
// Module: no_children() // Module: no_children()
// Topics: Error Checking
// Usage: // Usage:
// no_children($children); // no_children($children);
// Topics: Error Checking
// See Also: no_function(), no_module()
// Description: // Description:
// Assert that the calling module does not support children. Prints an error message to this effect and fails if children are present, // Assert that the calling module does not support children. Prints an error message to this effect and fails if children are present,
// as indicated by its argument. // as indicated by its argument.
// Arguments: // Arguments:
// $children = number of children the module has. // $children = number of children the module has.
// See Also: no_function(), no_module()
// Example: // Example:
// module foo() { // module foo() {
// no_children($children); // no_children($children);
@ -676,7 +677,7 @@ function _valstr(x) =
// expected = The value that was expected. // expected = The value that was expected.
// info = Extra info to print out to make the error clearer. // info = Extra info to print out to make the error clearer.
// Example: // Example:
// assert_approx(1/3, 0.333333333333333, str("numer=",1,", demon=",3)); // assert_approx(1/3, 0.333333333333333, str("number=",1,", demon=",3));
module assert_approx(got, expected, info) { module assert_approx(got, expected, info) {
no_children($children); no_children($children);
if (!approx(got, expected)) { if (!approx(got, expected)) {

View file

@ -454,120 +454,120 @@ function path_torsion(path, closed=false) =
// path2 = path_chamfer_and_rounding(path, closed=true, chamfer=chamfs, rounding=rs); // path2 = path_chamfer_and_rounding(path, closed=true, chamfer=chamfs, rounding=rs);
// stroke(path2, closed=true); // stroke(path2, closed=true);
function path_chamfer_and_rounding(path, closed, chamfer, rounding) = function path_chamfer_and_rounding(path, closed, chamfer, rounding) =
let ( let (
path = deduplicate(path,closed=true), path = deduplicate(path,closed=true),
lp = len(path), lp = len(path),
chamfer = is_undef(chamfer)? repeat(0,lp) : chamfer = is_undef(chamfer)? repeat(0,lp) :
is_vector(chamfer)? list_pad(chamfer,lp,0) : is_vector(chamfer)? list_pad(chamfer,lp,0) :
is_num(chamfer)? repeat(chamfer,lp) : is_num(chamfer)? repeat(chamfer,lp) :
assert(false, "Bad chamfer value."), assert(false, "Bad chamfer value."),
rounding = is_undef(rounding)? repeat(0,lp) : rounding = is_undef(rounding)? repeat(0,lp) :
is_vector(rounding)? list_pad(rounding,lp,0) : is_vector(rounding)? list_pad(rounding,lp,0) :
is_num(rounding)? repeat(rounding,lp) : is_num(rounding)? repeat(rounding,lp) :
assert(false, "Bad rounding value."), assert(false, "Bad rounding value."),
corner_paths = [ corner_paths = [
for (i=(closed? [0:1:lp-1] : [1:1:lp-2])) let( for (i=(closed? [0:1:lp-1] : [1:1:lp-2])) let(
p1 = select(path,i-1), p1 = select(path,i-1),
p2 = select(path,i), p2 = select(path,i),
p3 = select(path,i+1) p3 = select(path,i+1)
) )
chamfer[i] > 0? _corner_chamfer_path(p1, p2, p3, side=chamfer[i]) : chamfer[i] > 0? _corner_chamfer_path(p1, p2, p3, side=chamfer[i]) :
rounding[i] > 0? _corner_roundover_path(p1, p2, p3, r=rounding[i]) : rounding[i] > 0? _corner_roundover_path(p1, p2, p3, r=rounding[i]) :
[p2] [p2]
], ],
out = [ out = [
if (!closed) path[0], if (!closed) path[0],
for (i=(closed? [0:1:lp-1] : [1:1:lp-2])) let( for (i=(closed? [0:1:lp-1] : [1:1:lp-2])) let(
p1 = select(path,i-1), p1 = select(path,i-1),
p2 = select(path,i), p2 = select(path,i),
crn1 = select(corner_paths,i-1), crn1 = select(corner_paths,i-1),
crn2 = corner_paths[i], crn2 = corner_paths[i],
l1 = norm(last(crn1)-p1), l1 = norm(last(crn1)-p1),
l2 = norm(crn2[0]-p2), l2 = norm(crn2[0]-p2),
needed = l1 + l2, needed = l1 + l2,
seglen = norm(p2-p1), seglen = norm(p2-p1),
check = assert(seglen >= needed, str("Path segment ",i," is too short to fulfill rounding/chamfering for the adjacent corners.")) check = assert(seglen >= needed, str("Path segment ",i," is too short to fulfill rounding/chamfering for the adjacent corners."))
) each crn2, ) each crn2,
if (!closed) last(path) if (!closed) last(path)
] ]
) deduplicate(out); ) deduplicate(out);
function _corner_chamfer_path(p1, p2, p3, dist1, dist2, side, angle) = function _corner_chamfer_path(p1, p2, p3, dist1, dist2, side, angle) =
let( let(
v1 = unit(p1 - p2), v1 = unit(p1 - p2),
v2 = unit(p3 - p2), v2 = unit(p3 - p2),
n = vector_axis(v1,v2), n = vector_axis(v1,v2),
ang = vector_angle(v1,v2), ang = vector_angle(v1,v2),
path = (is_num(dist1) && is_undef(dist2) && is_undef(side))? ( path = (is_num(dist1) && is_undef(dist2) && is_undef(side))? (
// dist1 & optional angle // dist1 & optional angle
assert(dist1 > 0) assert(dist1 > 0)
let(angle = default(angle,(180-ang)/2)) let(angle = default(angle,(180-ang)/2))
assert(is_num(angle)) assert(is_num(angle))
assert(angle > 0 && angle < 180) assert(angle > 0 && angle < 180)
let( let(
pta = p2 + dist1*v1, pta = p2 + dist1*v1,
a3 = 180 - angle - ang a3 = 180 - angle - ang
) assert(a3>0, "Angle too extreme.") ) assert(a3>0, "Angle too extreme.")
let( let(
side = sin(angle) * dist1/sin(a3), side = sin(angle) * dist1/sin(a3),
ptb = p2 + side*v2 ptb = p2 + side*v2
) [pta, ptb] ) [pta, ptb]
) : (is_undef(dist1) && is_num(dist2) && is_undef(side))? ( ) : (is_undef(dist1) && is_num(dist2) && is_undef(side))? (
// dist2 & optional angle // dist2 & optional angle
assert(dist2 > 0) assert(dist2 > 0)
let(angle = default(angle,(180-ang)/2)) let(angle = default(angle,(180-ang)/2))
assert(is_num(angle)) assert(is_num(angle))
assert(angle > 0 && angle < 180) assert(angle > 0 && angle < 180)
let( let(
ptb = p2 + dist2*v2, ptb = p2 + dist2*v2,
a3 = 180 - angle - ang a3 = 180 - angle - ang
) assert(a3>0, "Angle too extreme.") ) assert(a3>0, "Angle too extreme.")
let( let(
side = sin(angle) * dist2/sin(a3), side = sin(angle) * dist2/sin(a3),
pta = p2 + side*v1 pta = p2 + side*v1
) [pta, ptb] ) [pta, ptb]
) : (is_undef(dist1) && is_undef(dist2) && is_num(side))? ( ) : (is_undef(dist1) && is_undef(dist2) && is_num(side))? (
// side & optional angle // side & optional angle
assert(side > 0) assert(side > 0)
let(angle = default(angle,(180-ang)/2)) let(angle = default(angle,(180-ang)/2))
assert(is_num(angle)) assert(is_num(angle))
assert(angle > 0 && angle < 180) assert(angle > 0 && angle < 180)
let( let(
a3 = 180 - angle - ang a3 = 180 - angle - ang
) assert(a3>0, "Angle too extreme.") ) assert(a3>0, "Angle too extreme.")
let( let(
dist1 = sin(a3) * side/sin(ang), dist1 = sin(a3) * side/sin(ang),
dist2 = sin(angle) * side/sin(ang), dist2 = sin(angle) * side/sin(ang),
pta = p2 + dist1*v1, pta = p2 + dist1*v1,
ptb = p2 + dist2*v2 ptb = p2 + dist2*v2
) [pta, ptb] ) [pta, ptb]
) : (is_num(dist1) && is_num(dist2) && is_undef(side) && is_undef(side))? ( ) : (is_num(dist1) && is_num(dist2) && is_undef(side) && is_undef(side))? (
// dist1 & dist2 // dist1 & dist2
assert(dist1 > 0) assert(dist1 > 0)
assert(dist2 > 0) assert(dist2 > 0)
let( let(
pta = p2 + dist1*v1, pta = p2 + dist1*v1,
ptb = p2 + dist2*v2 ptb = p2 + dist2*v2
) [pta, ptb] ) [pta, ptb]
) : ( ) : (
assert(false,"Bad arguments.") assert(false,"Bad arguments.")
) )
) path; ) path;
function _corner_roundover_path(p1, p2, p3, r, d) = function _corner_roundover_path(p1, p2, p3, r, d) =
let( let(
r = get_radius(r=r,d=d,dflt=undef), r = get_radius(r=r,d=d,dflt=undef),
res = circle_2tangents(p1, p2, p3, r=r, tangents=true), res = circle_2tangents(p1, p2, p3, r=r, tangents=true),
cp = res[0], cp = res[0],
n = res[1], n = res[1],
tp1 = res[2], tp1 = res[2],
ang = res[4]+res[5], ang = res[4]+res[5],
steps = floor(segs(r)*ang/360+0.5), steps = floor(segs(r)*ang/360+0.5),
step = ang / steps, step = ang / steps,
path = [for (i=[0:1:steps]) move(cp, p=rot(a=-i*step, v=n, p=tp1-cp))] path = [for (i=[0:1:steps]) move(cp, p=rot(a=-i*step, v=n, p=tp1-cp))]
) path; ) path;