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:
// 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
// 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:
// list = list to check
// 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.
// recursive = If true, sublists are checked recursively for defined values. The first sublist that has a defined item is returned.
// 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) =
_i<len(v) && (
is_undef(v[_i]) || (
@ -605,15 +606,15 @@ function segs(r) =
// Module: no_children()
// Topics: Error Checking
// Usage:
// no_children($children);
// Topics: Error Checking
// See Also: no_function(), no_module()
// Description:
// 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.
// Arguments:
// $children = number of children the module has.
// See Also: no_function(), no_module()
// Example:
// module foo() {
// no_children($children);
@ -676,7 +677,7 @@ function _valstr(x) =
// expected = The value that was expected.
// info = Extra info to print out to make the error clearer.
// 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) {
no_children($children);
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);
// stroke(path2, closed=true);
function path_chamfer_and_rounding(path, closed, chamfer, rounding) =
let (
path = deduplicate(path,closed=true),
lp = len(path),
chamfer = is_undef(chamfer)? repeat(0,lp) :
is_vector(chamfer)? list_pad(chamfer,lp,0) :
is_num(chamfer)? repeat(chamfer,lp) :
assert(false, "Bad chamfer value."),
rounding = is_undef(rounding)? repeat(0,lp) :
is_vector(rounding)? list_pad(rounding,lp,0) :
is_num(rounding)? repeat(rounding,lp) :
assert(false, "Bad rounding value."),
corner_paths = [
for (i=(closed? [0:1:lp-1] : [1:1:lp-2])) let(
p1 = select(path,i-1),
p2 = select(path,i),
p3 = select(path,i+1)
)
chamfer[i] > 0? _corner_chamfer_path(p1, p2, p3, side=chamfer[i]) :
rounding[i] > 0? _corner_roundover_path(p1, p2, p3, r=rounding[i]) :
[p2]
],
out = [
if (!closed) path[0],
for (i=(closed? [0:1:lp-1] : [1:1:lp-2])) let(
p1 = select(path,i-1),
p2 = select(path,i),
crn1 = select(corner_paths,i-1),
crn2 = corner_paths[i],
l1 = norm(last(crn1)-p1),
l2 = norm(crn2[0]-p2),
needed = l1 + l2,
seglen = norm(p2-p1),
check = assert(seglen >= needed, str("Path segment ",i," is too short to fulfill rounding/chamfering for the adjacent corners."))
) each crn2,
if (!closed) last(path)
]
) deduplicate(out);
let (
path = deduplicate(path,closed=true),
lp = len(path),
chamfer = is_undef(chamfer)? repeat(0,lp) :
is_vector(chamfer)? list_pad(chamfer,lp,0) :
is_num(chamfer)? repeat(chamfer,lp) :
assert(false, "Bad chamfer value."),
rounding = is_undef(rounding)? repeat(0,lp) :
is_vector(rounding)? list_pad(rounding,lp,0) :
is_num(rounding)? repeat(rounding,lp) :
assert(false, "Bad rounding value."),
corner_paths = [
for (i=(closed? [0:1:lp-1] : [1:1:lp-2])) let(
p1 = select(path,i-1),
p2 = select(path,i),
p3 = select(path,i+1)
)
chamfer[i] > 0? _corner_chamfer_path(p1, p2, p3, side=chamfer[i]) :
rounding[i] > 0? _corner_roundover_path(p1, p2, p3, r=rounding[i]) :
[p2]
],
out = [
if (!closed) path[0],
for (i=(closed? [0:1:lp-1] : [1:1:lp-2])) let(
p1 = select(path,i-1),
p2 = select(path,i),
crn1 = select(corner_paths,i-1),
crn2 = corner_paths[i],
l1 = norm(last(crn1)-p1),
l2 = norm(crn2[0]-p2),
needed = l1 + l2,
seglen = norm(p2-p1),
check = assert(seglen >= needed, str("Path segment ",i," is too short to fulfill rounding/chamfering for the adjacent corners."))
) each crn2,
if (!closed) last(path)
]
) deduplicate(out);
function _corner_chamfer_path(p1, p2, p3, dist1, dist2, side, angle) =
let(
v1 = unit(p1 - p2),
v2 = unit(p3 - p2),
n = vector_axis(v1,v2),
ang = vector_angle(v1,v2),
path = (is_num(dist1) && is_undef(dist2) && is_undef(side))? (
// dist1 & optional angle
assert(dist1 > 0)
let(angle = default(angle,(180-ang)/2))
assert(is_num(angle))
assert(angle > 0 && angle < 180)
let(
pta = p2 + dist1*v1,
a3 = 180 - angle - ang
) assert(a3>0, "Angle too extreme.")
let(
side = sin(angle) * dist1/sin(a3),
ptb = p2 + side*v2
) [pta, ptb]
) : (is_undef(dist1) && is_num(dist2) && is_undef(side))? (
// dist2 & optional angle
assert(dist2 > 0)
let(angle = default(angle,(180-ang)/2))
assert(is_num(angle))
assert(angle > 0 && angle < 180)
let(
ptb = p2 + dist2*v2,
a3 = 180 - angle - ang
) assert(a3>0, "Angle too extreme.")
let(
side = sin(angle) * dist2/sin(a3),
pta = p2 + side*v1
) [pta, ptb]
) : (is_undef(dist1) && is_undef(dist2) && is_num(side))? (
// side & optional angle
assert(side > 0)
let(angle = default(angle,(180-ang)/2))
assert(is_num(angle))
assert(angle > 0 && angle < 180)
let(
a3 = 180 - angle - ang
) assert(a3>0, "Angle too extreme.")
let(
dist1 = sin(a3) * side/sin(ang),
dist2 = sin(angle) * side/sin(ang),
pta = p2 + dist1*v1,
ptb = p2 + dist2*v2
) [pta, ptb]
) : (is_num(dist1) && is_num(dist2) && is_undef(side) && is_undef(side))? (
// dist1 & dist2
assert(dist1 > 0)
assert(dist2 > 0)
let(
pta = p2 + dist1*v1,
ptb = p2 + dist2*v2
) [pta, ptb]
) : (
assert(false,"Bad arguments.")
)
) path;
let(
v1 = unit(p1 - p2),
v2 = unit(p3 - p2),
n = vector_axis(v1,v2),
ang = vector_angle(v1,v2),
path = (is_num(dist1) && is_undef(dist2) && is_undef(side))? (
// dist1 & optional angle
assert(dist1 > 0)
let(angle = default(angle,(180-ang)/2))
assert(is_num(angle))
assert(angle > 0 && angle < 180)
let(
pta = p2 + dist1*v1,
a3 = 180 - angle - ang
) assert(a3>0, "Angle too extreme.")
let(
side = sin(angle) * dist1/sin(a3),
ptb = p2 + side*v2
) [pta, ptb]
) : (is_undef(dist1) && is_num(dist2) && is_undef(side))? (
// dist2 & optional angle
assert(dist2 > 0)
let(angle = default(angle,(180-ang)/2))
assert(is_num(angle))
assert(angle > 0 && angle < 180)
let(
ptb = p2 + dist2*v2,
a3 = 180 - angle - ang
) assert(a3>0, "Angle too extreme.")
let(
side = sin(angle) * dist2/sin(a3),
pta = p2 + side*v1
) [pta, ptb]
) : (is_undef(dist1) && is_undef(dist2) && is_num(side))? (
// side & optional angle
assert(side > 0)
let(angle = default(angle,(180-ang)/2))
assert(is_num(angle))
assert(angle > 0 && angle < 180)
let(
a3 = 180 - angle - ang
) assert(a3>0, "Angle too extreme.")
let(
dist1 = sin(a3) * side/sin(ang),
dist2 = sin(angle) * side/sin(ang),
pta = p2 + dist1*v1,
ptb = p2 + dist2*v2
) [pta, ptb]
) : (is_num(dist1) && is_num(dist2) && is_undef(side) && is_undef(side))? (
// dist1 & dist2
assert(dist1 > 0)
assert(dist2 > 0)
let(
pta = p2 + dist1*v1,
ptb = p2 + dist2*v2
) [pta, ptb]
) : (
assert(false,"Bad arguments.")
)
) path;
function _corner_roundover_path(p1, p2, p3, r, d) =
let(
r = get_radius(r=r,d=d,dflt=undef),
res = circle_2tangents(p1, p2, p3, r=r, tangents=true),
cp = res[0],
n = res[1],
tp1 = res[2],
ang = res[4]+res[5],
steps = floor(segs(r)*ang/360+0.5),
step = ang / steps,
path = [for (i=[0:1:steps]) move(cp, p=rot(a=-i*step, v=n, p=tp1-cp))]
) path;
let(
r = get_radius(r=r,d=d,dflt=undef),
res = circle_2tangents(p1, p2, p3, r=r, tangents=true),
cp = res[0],
n = res[1],
tp1 = res[2],
ang = res[4]+res[5],
steps = floor(segs(r)*ang/360+0.5),
step = ang / steps,
path = [for (i=[0:1:steps]) move(cp, p=rot(a=-i*step, v=n, p=tp1-cp))]
) path;