change is_closed_path to are_ends_equal

sphere bugfix
This commit is contained in:
Adrian Mariano 2023-01-19 20:39:05 -05:00
parent 0568e2ef46
commit 8bd08bd4c1
6 changed files with 38 additions and 28 deletions

View file

@ -194,6 +194,19 @@ function all_equal(vec,eps=0) =
// Function: are_ends_equal()
// Usage:
// are_ends_equal(list, [eps]);
// Description:
// Returns true if the first and last points in the given list are equal to within epsilon.
// Arguments:
// list = list to check
// eps = Tolerance for approximate equality. Default: `EPSILON` (1e-9)
function are_ends_equal(list, eps=EPSILON) =
assert(is_list(list) && len(list)>0, "Must give a nonempty list")
approx(list[0], list[len(list)-1], eps=eps);
// Function: is_increasing() // Function: is_increasing()
// Usage: // Usage:
// bool = is_increasing(list, [strict]); // bool = is_increasing(list, [strict]);

View file

@ -87,21 +87,13 @@ function force_path(path, name="path") =
: path; : path;
// Function: is_closed_path()
// Usage:
// is_closed_path(path, [eps]);
// Description:
// Returns true if the first and last points in the given path are coincident.
function is_closed_path(path, eps=EPSILON) = approx(path[0], path[len(path)-1], eps=eps);
// Function: close_path() // Function: close_path()
// Usage: // Usage:
// close_path(path); // close_path(path);
// Description: // Description:
// If a path's last point does not coincide with its first point, closes the path so it does. // If a path's last point does not coincide with its first point, closes the path so it does.
function close_path(path, eps=EPSILON) = function close_path(path, eps=EPSILON) =
is_closed_path(path,eps=eps)? path : concat(path,[path[0]]); are_ends_equal(path,eps=eps)? path : concat(path,[path[0]]);
// Function: cleanup_path() // Function: cleanup_path()
@ -110,7 +102,7 @@ function close_path(path, eps=EPSILON) =
// Description: // Description:
// If a path's last point coincides with its first point, deletes the last point in the path. // If a path's last point coincides with its first point, deletes the last point in the path.
function cleanup_path(path, eps=EPSILON) = function cleanup_path(path, eps=EPSILON) =
is_closed_path(path,eps=eps)? [for (i=[0:1:len(path)-2]) path[i]] : path; are_ends_equal(path,eps=eps)? [for (i=[0:1:len(path)-2]) path[i]] : path;
/// Internal Function: _path_select() /// Internal Function: _path_select()
@ -1096,7 +1088,7 @@ function _assemble_a_path_from_fragments(fragments, rightmost=true, startfrag=0,
let( let(
path = fragments[startfrag], path = fragments[startfrag],
newfrags = [for (i=idx(fragments)) if (i!=startfrag) fragments[i]] newfrags = [for (i=idx(fragments)) if (i!=startfrag) fragments[i]]
) is_closed_path(path, eps=eps)? ( ) are_ends_equal(path, eps=eps)? (
// starting fragment is already closed // starting fragment is already closed
[path, newfrags] [path, newfrags]
) : let( ) : let(
@ -1109,7 +1101,7 @@ function _assemble_a_path_from_fragments(fragments, rightmost=true, startfrag=0,
// No remaining fragments connect! INCOMPLETE PATH! // No remaining fragments connect! INCOMPLETE PATH!
// Treat it as complete. // Treat it as complete.
[path, remainder] [path, remainder]
) : is_closed_path(foundfrag, eps=eps)? ( ) : are_ends_equal(foundfrag, eps=eps)? (
// Found fragment is already closed // Found fragment is already closed
[foundfrag, concat([path], remainder)] [foundfrag, concat([path], remainder)]
) : let( ) : let(

View file

@ -2020,7 +2020,7 @@ module sphere(r, d, anchor=CENTER, spin=0, orient=UP) {
} }
function sphere(r, d, anchor=CENTER, spin=0, orient=UP) = function sphere(r, d, anchor=CENTER, spin=0, orient=UP) =
spheroid(r=r, d=d, anchor=anchor, spin=spin, orient=orient); spheroid(r=r, d=d, style="orig", anchor=anchor, spin=spin, orient=orient);
// Function&Module: spheroid() // Function&Module: spheroid()

View file

@ -121,6 +121,18 @@ module test_is_decreasing() {
test_is_decreasing(); test_is_decreasing();
module test_are_ends_equal() {
assert(!are_ends_equal([[1,2,3],[4,5,6],[1,8,9]]));
assert(are_ends_equal([[1,2,3],[4,5,6],[1,8,9],[1,2,3]]));
assert(are_ends_equal([1,2,3,1.00004],eps=1e-2));
assert(are_ends_equal([3]));
}
test_are_ends_equal();
module test_find_approx() { module test_find_approx() {
assert(find_approx(1, [2,3,1.05,4,1,2,.99], eps=.1)==2); assert(find_approx(1, [2,3,1.05,4,1,2,.99], eps=.1)==2);
assert(find_approx(1, [2,3,1.05,4,1,2,.99], all=true, eps=.1)==[2,4,6]); assert(find_approx(1, [2,3,1.05,4,1,2,.99], all=true, eps=.1)==[2,4,6]);

View file

@ -33,13 +33,6 @@ test_is_1region();
module test_is_closed_path() {
assert(!is_closed_path([[1,2,3],[4,5,6],[1,8,9]]));
assert(is_closed_path([[1,2,3],[4,5,6],[1,8,9],[1,2,3]]));
}
test_is_closed_path();
module test_close_path() { module test_close_path() {
assert(close_path([[1,2,3],[4,5,6],[1,8,9]]) == [[1,2,3],[4,5,6],[1,8,9],[1,2,3]]); assert(close_path([[1,2,3],[4,5,6],[1,8,9]]) == [[1,2,3],[4,5,6],[1,8,9],[1,2,3]]);
assert(close_path([[1,2,3],[4,5,6],[1,8,9],[1,2,3]]) == [[1,2,3],[4,5,6],[1,8,9],[1,2,3]]); assert(close_path([[1,2,3],[4,5,6],[1,8,9],[1,2,3]]) == [[1,2,3],[4,5,6],[1,8,9],[1,2,3]]);

View file

@ -94,28 +94,28 @@ module test_cyl() {
$fn=12; $fn=12;
shape_compare() { shape_compare() {
cyl(r=50,l=10,circum=true,anchor=BOTTOM); cyl(r=50,l=10,circum=true,anchor=BOTTOM);
cylinder(r=50/cos(180/12),l=10); cylinder(r=50/cos(180/12),h=10);
} }
shape_compare() { shape_compare() {
cyl(r=50,l=10,circum=false,anchor=BOTTOM); cyl(r=50,l=10,circum=false,anchor=BOTTOM);
cylinder(r=50,l=10); cylinder(r=50,h=10);
} }
shape_compare() { shape_compare() {
cyl(r=50,l=10,chamfer=1,circum=true,anchor=BOTTOM); cyl(r=50,l=10,chamfer=1,circum=true,anchor=BOTTOM);
union() { union() {
r=50/cos(180/12); r=50/cos(180/12);
cylinder(r1=r-1,r2=r,l=1); cylinder(r1=r-1,r2=r,h=1);
up(1) cylinder(r=r,l=8); up(1) cylinder(r=r,h=8);
up(9) cylinder(r1=r,r2=r-1,l=1); up(9) cylinder(r1=r,r2=r-1,h=1);
} }
} }
shape_compare() { shape_compare() {
cyl(r=50,l=10,chamfer=1,circum=false,anchor=BOTTOM); cyl(r=50,l=10,chamfer=1,circum=false,anchor=BOTTOM);
union() { union() {
r=50; r=50;
cylinder(r1=r-1,r2=r,l=1); cylinder(r1=r-1,r2=r,h=1);
up(1) cylinder(r=r,l=8); up(1) cylinder(r=r,h=8);
up(9) cylinder(r1=r,r2=r-1,l=1); up(9) cylinder(r1=r,r2=r-1,h=1);
} }
} }
} }