1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-07-30 19:40:24 +02:00

refactor: use hashset

This commit is contained in:
Justin Lin
2022-04-15 08:51:52 +08:00
parent 4c3dca538d
commit a6a2b9d3f5
3 changed files with 19 additions and 14 deletions

View File

@@ -1,11 +1,15 @@
use <util/has.scad>;
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

View File

@@ -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);
function vx_contour(points) = _vx_contour(points);

View File

@@ -4,8 +4,9 @@ use <voxel/vx_contour.scad>;
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);
}