diff --git a/src/voxel/_impl/_vx_contour_impl.scad b/src/voxel/_impl/_vx_contour_impl.scad index 75aa7cad..b36ec0c4 100644 --- a/src/voxel/_impl/_vx_contour_impl.scad +++ b/src/voxel/_impl/_vx_contour_impl.scad @@ -1,11 +1,15 @@ -use ; +use <../../util/set/hashset.scad>; +use <../../util/set/hashset_has.scad>; +use <../../__comm__/_pt2_hash.scad>; + +hash = function(p) _pt2_hash(p); function _vx_contour_corner_value(pts, x, y) = let( - c1 = has(pts, [x, y - 1], sorted = true) || has(pts, [x - 1, y - 1], sorted = true) ? 1 : 0, - c2 = has(pts, [x - 1, y], sorted = true) || has(pts, [x - 1, y + 1], sorted = true) ? 2 : 0, - c3 = has(pts, [x, y + 1], sorted = true) || has(pts, [x + 1, y + 1], sorted = true) ? 4 : 0, - c4 = has(pts, [x + 1, y], sorted = true) || has(pts, [x + 1, y - 1], sorted = true) ? 8 : 0 + c1 = hashset_has(pts, [x, y - 1], hash = hash) || hashset_has(pts, [x - 1, y - 1], hash = hash) ? 1 : 0, + c2 = hashset_has(pts, [x - 1, y], hash = hash) || hashset_has(pts, [x - 1, y + 1], hash = hash) ? 2 : 0, + c3 = hashset_has(pts, [x, y + 1], hash = hash) || hashset_has(pts, [x + 1, y + 1], hash = hash) ? 4 : 0, + c4 = hashset_has(pts, [x + 1, y], hash = hash) || hashset_has(pts, [x + 1, y - 1], hash = hash) ? 8 : 0 ) c1 + c2 + c3 + c4; @@ -18,6 +22,13 @@ _vx_contour_dir_table = [ function _vx_contour_dir(cr_value) = lookup(cr_value, _vx_contour_dir_table); +function _vx_contour(points) = + let( + // always start from the left-bottom pt + fst = min(points) + [-1, -1] + ) + _vx_contour_travel(hashset(points, hash = function(p) _pt2_hash(p)), fst, fst); + _vx_contour_nxt_offset = [ [1, 0], // RIGHT [0, -1], // DOWN diff --git a/src/voxel/vx_contour.scad b/src/voxel/vx_contour.scad index c92acc5e..f12767bf 100644 --- a/src/voxel/vx_contour.scad +++ b/src/voxel/vx_contour.scad @@ -9,12 +9,5 @@ **/ use <_impl/_vx_contour_impl.scad>; -use <../util/sort.scad>; -function vx_contour(points, sorted = false) = - let( - // always start from the left-bottom pt - sortedXY = sorted ? points : sort(points, by = "vt"), - fst = sortedXY[0] + [-1, -1] - ) - _vx_contour_travel(sortedXY, fst, fst); \ No newline at end of file +function vx_contour(points) = _vx_contour(points); \ No newline at end of file diff --git a/test/voxel/test_vx_contour.scad b/test/voxel/test_vx_contour.scad index ab5d10a8..cb2232e1 100644 --- a/test/voxel/test_vx_contour.scad +++ b/test/voxel/test_vx_contour.scad @@ -4,8 +4,9 @@ use ; module test_vx_contour() { echo("==== test_vx_contour ===="); - expected = [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [7, 1], [7, 2], [7, 3], [7, 4], [7, 5], [7, 6], [7, 7], [6, 7], [5, 7], [4, 7], [4, 6], [4, 5], [3, 5], [2, 5], [1, 5], [1, 4], [0, 4], [0, 3], [0, 2], [0, 1], [1, 1]]; + expected = [[0, 1], [1, 1], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [7, 1], [7, 2], [7, 3], [7, 4], [7, 5], [7, 6], [7, 7], [6, 7], [5, 7], [4, 7], [4, 6], [4, 5], [3, 5], [2, 5], [1, 5], [1, 4], [0, 4], [0, 3], [0, 2]]; actual = vx_contour(vx_ascii("d")); + assert(expected == actual); }