From 5c37382c26563f6da7c2ee3248caa84f74d5e06b Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Tue, 24 Mar 2020 07:27:29 +0800 Subject: [PATCH] flat isolines/isobands --- examples/image_slicer.scad | 6 +- .../_impl/_marching_squares_impl.scad | 56 +++++++++---------- .../demo/marching_squares_demo.scad | 13 ++--- .../demo/marching_squares_demo2.scad | 6 +- src/experimental/note.md | 1 + 5 files changed, 35 insertions(+), 47 deletions(-) diff --git a/examples/image_slicer.scad b/examples/image_slicer.scad index eab8c341..16f62aa3 100644 --- a/examples/image_slicer.scad +++ b/examples/image_slicer.scad @@ -118,10 +118,8 @@ module image_slicer(levels, level_step, contour_width) { module contours(points, z, contour_width) { union() { - for(row = marching_squares(points, [0, z])) { - for(iso_band = row) { - polygon([for(p = iso_band) [p[0], p[1]]]); - } + for(iso_band = marching_squares(points, [0, z])) { + polygon([for(p = iso_band) [p[0], p[1]]]); } } } diff --git a/src/experimental/_impl/_marching_squares_impl.scad b/src/experimental/_impl/_marching_squares_impl.scad index 703aec88..a54b6fad 100644 --- a/src/experimental/_impl/_marching_squares_impl.scad +++ b/src/experimental/_impl/_marching_squares_impl.scad @@ -110,20 +110,18 @@ function _isolines_of(cell_pts, sigma) = function _marching_squares_isolines(points, sigma) = let(labeled_pts = _isolines_pn_label(points, sigma)) [ - for(y = [0:len(labeled_pts) - 2]) - [ - for(x = [0:len(labeled_pts[0]) - 2]) - let( - p0 = labeled_pts[y][x], - p1 = labeled_pts[y + 1][x], - p2 = labeled_pts[y + 1][x + 1], - p3 = labeled_pts[y][x + 1], - cell_pts = [p0, p1, p2, p3], - isolines_lt = _isolines_of(cell_pts, sigma) - ) - if(isolines_lt != []) - each isolines_lt - ] + for(y = [0:len(labeled_pts) - 2]) + for(x = [0:len(labeled_pts[0]) - 2]) + let( + p0 = labeled_pts[y][x], + p1 = labeled_pts[y + 1][x], + p2 = labeled_pts[y + 1][x + 1], + p3 = labeled_pts[y][x + 1], + cell_pts = [p0, p1, p2, p3], + isolines_lt = _isolines_of(cell_pts, sigma) + ) + if(isolines_lt != []) + each isolines_lt ]; /* @@ -940,7 +938,7 @@ function _case2010_isobands(cell_pts, lower, upper) = function _isobands_of(cell_pts, lower, upper) = let(cv = _isobands_corner_value(cell_pts)) // no contour - cv == "0000" ? [] : + cv == "0000" || cv == "2222" ? [] : // single square cv == "1111" ? [[for(i = 3; i >= 0; i = i - 1) [cell_pts[i][0], cell_pts[i][1]]]] : // single triangle @@ -1026,27 +1024,23 @@ function _isobands_of(cell_pts, lower, upper) = cv == "1202" ? _case1202_isobands(cell_pts, lower, upper) : cv == "1020" ? _case1020_isobands(cell_pts, lower, upper) : cv == "0212" ? _case0212_isobands(cell_pts, lower, upper) : - cv == "2010" ? _case2010_isobands(cell_pts, lower, upper) : - // no contour - []; // 2222 + _case2010_isobands(cell_pts, lower, upper); function _marching_squares_isobands(points, lower, upper) = let(labeled_pts = _isobands_tri_label(points, lower, upper)) [ for(y = [0:len(labeled_pts) - 2]) - [ - for(x = [0:len(labeled_pts[0]) - 2]) - let( - p0 = labeled_pts[y][x], - p1 = labeled_pts[y + 1][x], - p2 = labeled_pts[y + 1][x + 1], - p3 = labeled_pts[y][x + 1], - cell_pts = [p0, p1, p2, p3], - isobands_lt = _isobands_of(cell_pts, lower, upper) - ) - if(isobands_lt != []) - each isobands_lt - ] + for(x = [0:len(labeled_pts[0]) - 2]) + let( + p0 = labeled_pts[y][x], + p1 = labeled_pts[y + 1][x], + p2 = labeled_pts[y + 1][x + 1], + p3 = labeled_pts[y][x + 1], + cell_pts = [p0, p1, p2, p3], + isobands_lt = _isobands_of(cell_pts, lower, upper) + ) + if(isobands_lt != []) + each isobands_lt ]; /* diff --git a/src/experimental/demo/marching_squares_demo.scad b/src/experimental/demo/marching_squares_demo.scad index cc133119..081d076f 100644 --- a/src/experimental/demo/marching_squares_demo.scad +++ b/src/experimental/demo/marching_squares_demo.scad @@ -11,14 +11,11 @@ points = [ ]; -for(row = marching_squares(points, 0.1)) { - for(isoline = row) { - hull_polyline2d(isoline, width = .1); - } +for(isoline = marching_squares(points, 0.1)) { + hull_polyline2d(isoline, width = .1); } -translate([12, 0]) for(row = marching_squares(points, [-.2, .2])) { - for(isoband = row) { - polygon([for(p = isoband) [p[0], p[1]]]); - } +translate([12, 0]) +for(isoband = marching_squares(points, [-.2, .2])) { + polygon([for(p = isoband) [p[0], p[1]]]); } \ No newline at end of file diff --git a/src/experimental/demo/marching_squares_demo2.scad b/src/experimental/demo/marching_squares_demo2.scad index e30ba18a..27cfab53 100644 --- a/src/experimental/demo/marching_squares_demo2.scad +++ b/src/experimental/demo/marching_squares_demo2.scad @@ -111,9 +111,7 @@ points = [ ]; for(z = [0:40:255]) { - for(row = marching_squares(points, z)) { - for(isoline = row) { - hull_polyline2d(isoline, width = .25); - } + for(isoline = marching_squares(points, z)) { + hull_polyline2d(isoline, width = .25); } } \ No newline at end of file diff --git a/src/experimental/note.md b/src/experimental/note.md index 54c309e2..1aadae9b 100644 --- a/src/experimental/note.md +++ b/src/experimental/note.md @@ -27,6 +27,7 @@ Preview - `util/dedup` - `util/bsearch` - `util/sort` supports `by = "vt"` +- `shape_circle` Bugfixes: - `helix_extrude`: wrong orientation when CLK \ No newline at end of file