From 4d9833a368ecbf882c23e4379eef24c25f1463d8 Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Mon, 11 Sep 2023 19:55:07 -0700 Subject: [PATCH] Added resample_path() tests. --- paths.scad | 8 ++-- tests/test_paths.scad | 92 ++++++++++++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 26 deletions(-) diff --git a/paths.scad b/paths.scad index 28b439f..2c42ae2 100644 --- a/paths.scad +++ b/paths.scad @@ -532,13 +532,13 @@ function resample_path(path, n, spacing, keep_corners, closed=true) = pcnt = len(path), plen = path_length(path, closed=closed), subpaths = [ for (p = pair(corners)) [for(i = [p.x:1:p.y]) path[i%pcnt]] ], - n = is_undef(n)? n : closed? n+1 : n + n = is_undef(n)? undef : closed? n+1 : n ) assert(n==undef || n >= len(corners), "There are nore than `n=` corners whose angle is greater than `keep_corners=`.") let( lens = [for (subpath = subpaths) path_length(subpath)], part_ns = is_undef(n) - ? [for (i=idx(subpaths)) ceil(lens[i]/spacing)-1] + ? [for (i=idx(subpaths)) max(1,round(lens[i]/spacing)-1)] : let( ccnt = len(corners), parts = [for (l=lens) (n-ccnt) * l/plen], @@ -549,8 +549,8 @@ function resample_path(path, n, spacing, keep_corners, closed=true) = let( subpath = subpaths[i], splen = lens[i], - n = part_ns[i] + 1, - distlist = lerpn(0, splen, n, false), + pn = part_ns[i] + 1, + distlist = lerpn(0, splen, pn, false), cuts = path_cut_points(subpath, distlist, closed=false) ) each column(cuts,0), diff --git a/tests/test_paths.scad b/tests/test_paths.scad index 11e2c1a..cf8d740 100644 --- a/tests/test_paths.scad +++ b/tests/test_paths.scad @@ -173,29 +173,77 @@ test_subdivide_long_segments(); module test_resample_path(){ path = xscale(2,circle($fn=250, r=10)); - sampled = resample_path(path, 16); - assert_approx(sampled, - [[20, 0], [17.1657142861, -5.13020769642], - [11.8890531315, -8.04075246881], [6.03095737128, - -9.53380030092], [1.72917236085e-14, -9.99921044204], - [-6.03095737128, -9.53380030092], [-11.8890531315, - -8.04075246881], [-17.1657142861, -5.13020769642], [-20, - -3.19176120946e-14], [-17.1657142861, 5.13020769642], - [-11.8890531315, 8.04075246881], [-6.03095737128, - 9.53380030092], [-4.20219414821e-14, 9.99921044204], - [6.03095737128, 9.53380030092], [11.8890531315, - 8.04075246881], [17.1657142861, 5.13020769642]]); + assert_approx( + resample_path(path, 16), [ + [20, 0], + [17.1657142861, -5.13020769642], + [11.8890531315, -8.04075246881], + [6.03095737128, -9.53380030092], + [1.72917236085e-14, -9.99921044204], + [-6.03095737128, -9.53380030092], + [-11.8890531315, -8.04075246881], + [-17.1657142861, -5.13020769642], + [-20, -3.19176120946e-14], + [-17.1657142861, 5.13020769642], + [-11.8890531315, 8.04075246881], + [-6.03095737128, 9.53380030092], + [-4.20219414821e-14, 9.99921044204], + [6.03095737128, 9.53380030092], + [11.8890531315, 8.04075246881], + [17.1657142861, 5.13020769642] + ] + ); path2 = square(20); - assert_approx(resample_path(path2, spacing=6), - [[20, 0], [13.8461538462, 0], [7.69230769231, 0], [1.53846153846, 0], - [0, 4.61538461538], [0, 10.7692307692], [0, 16.9230769231], [3.07692307692, 20], - [9.23076923077, 20], [15.3846153846, 20], [20, 18.4615384615], [20, 12.3076923077], [20, 6.15384615385]]); - assert_equal(resample_path(path2, spacing=6,closed=false),[[20, 0], [14, 0], [8, 0], [2, 0], [0, 4], [0, 10], [0, 16], [2, 20], [8, 20], [14, 20], [20, 20]]); - assert_approx(resample_path(path, spacing=17), - [[20, 0], [8.01443073309, -9.16170407964], - [-8.01443073309, -9.16170407964], [-20, - -1.59309060367e-14], [-8.01443073309, 9.16170407964], - [8.01443073309, 9.16170407964]]); + assert_approx( + resample_path(path2, spacing=6), [ + [20, 0], [13.8461538462, 0], [7.69230769231, 0], + [1.53846153846, 0], [0, 4.61538461538], + [0, 10.7692307692], [0, 16.9230769231], + [3.07692307692, 20], [9.23076923077, 20], + [15.3846153846, 20], [20, 18.4615384615], + [20, 12.3076923077], [20, 6.15384615385] + ] + ); + assert_equal( + resample_path(path2, spacing=6,closed=false), [ + [20, 0], [14, 0], [ 8, 0], [ 2, 0], + [ 0, 4], [ 0,10], [ 0,16], [ 2,20], + [ 8,20], [14,20], [20,20] + ] + ); + assert_equal( + resample_path(path2, n=7, keep_corners=90, closed=true), [ + [20,0],[10,0],[0,0],[0,10],[0,20],[20,20],[20,10] + ] + ); + assert_equal( + resample_path(path2, n=7, keep_corners=90, closed=false), [ + [20,0],[10,0],[0,0],[0,10],[0,20],[10,20],[20,20] + ] + ); + assert_approx( + resample_path(path2, spacing=6, keep_corners=90, closed=false), [ + [20, 0], [13.3333333333, 0], [6.66666666667,0], + [ 0, 0], [ 0,6.66666666667], [0,13.3333333333], + [ 0,20], [ 6.66666666667,20], [13.3333333333,20], + [20,20] + ] + ); + assert_approx( + resample_path(path2, spacing=6, keep_corners=90), [ + [20, 0], [13.3333333333, 0], [ 6.66666666667, 0], + [ 0, 0], [ 0, 6.66666666667], [ 0,13.3333333333], + [ 0,20], [ 6.66666666667,20], [13.3333333333,20], + [20,20], [20,13.3333333333], [20, 6.66666666667] + ] + ); + assert_approx( + resample_path(path, spacing=17), [ + [20, 0], [8.01443073309, -9.16170407964], + [-8.01443073309, -9.16170407964], [-20, -1.59309060367e-14], + [-8.01443073309, 9.16170407964], [8.01443073309, 9.16170407964] + ] + ); } test_resample_path();