From dba17df1551f91dee94994aea12fe6fd86c2ae9d Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 4 Mar 2021 17:14:59 +0800 Subject: [PATCH] use hashset to improve performance --- src/voxel/vx_bezier.scad | 7 +++---- src/voxel/vx_circle.scad | 7 ++++--- src/voxel/vx_polygon.scad | 4 ++-- src/voxel/vx_union.scad | 8 +++----- test/voxel/test_vx_circle.scad | 2 +- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/voxel/vx_bezier.scad b/src/voxel/vx_bezier.scad index 72b315f1..14e89ccc 100644 --- a/src/voxel/vx_bezier.scad +++ b/src/voxel/vx_bezier.scad @@ -11,14 +11,13 @@ use <__comm__/__to2d.scad>; use <__comm__/__to3d.scad>; use <_impl/_vx_bezier_impl.scad>; -use <../util/sort.scad>; -use <../util/dedup.scad>; +use ; function vx_bezier(p1, p2, p3, p4) = let( is2d = len(p1) == 2, pts = is2d ? _vx_bezier2(__to3d(p1), __to3d(p2), __to3d(p3), __to3d(p4), []) : _vx_bezier3(p1, p2, p3, p4, []), - sorted = dedup(sort(pts, by = "vt"), sorted = true) + deduped = hashset_list(hashset(pts)) ) - is2d ? [for(p = sorted) __to2d(p)] : sorted; \ No newline at end of file + is2d ? [for(p = deduped) __to2d(p)] : deduped; \ No newline at end of file diff --git a/src/voxel/vx_circle.scad b/src/voxel/vx_circle.scad index 42b4608c..41595644 100644 --- a/src/voxel/vx_circle.scad +++ b/src/voxel/vx_circle.scad @@ -9,9 +9,10 @@ **/ use <_impl/_vx_circle_impl.scad>; -use <../util/sort.scad>; -use <../util/dedup.scad>; +use ; function vx_circle(radius, filled = false) = let(all = _vx_circle_impl(radius, filled)) - dedup(sort(all, by = "vt"), sorted = true); \ No newline at end of file + hashset_list( + hashset(all) + ); \ No newline at end of file diff --git a/src/voxel/vx_polygon.scad b/src/voxel/vx_polygon.scad index 8ce50534..fe477726 100644 --- a/src/voxel/vx_polygon.scad +++ b/src/voxel/vx_polygon.scad @@ -1,7 +1,7 @@ use <../in_shape.scad>; use <../util/sort.scad>; -use <../util/dedup.scad>; use ; +use ; function vx_polygon(points, filled = false) = let(contour = vx_polyline(concat(points, [points[0]]))) @@ -28,4 +28,4 @@ function vx_polygon(points, filled = false) = ] ) ) - dedup(sort(all, by = "vt"), sorted = true); \ No newline at end of file + hashset_list(hashset(all)); \ No newline at end of file diff --git a/src/voxel/vx_union.scad b/src/voxel/vx_union.scad index 34d8a29f..344a0c61 100644 --- a/src/voxel/vx_union.scad +++ b/src/voxel/vx_union.scad @@ -8,11 +8,9 @@ * **/ -use <../util/sort.scad>; -use <../util/dedup.scad>; +use ; function vx_union(points1, points2) = - dedup( - sort(concat(points1, points2), by = "vt"), - sorted = true + hashset_list( + hashset(concat(points1, points2)) ); \ No newline at end of file diff --git a/test/voxel/test_vx_circle.scad b/test/voxel/test_vx_circle.scad index 34f25d2b..63109379 100644 --- a/test/voxel/test_vx_circle.scad +++ b/test/voxel/test_vx_circle.scad @@ -4,7 +4,7 @@ use ; module test_vx_circle() { echo("==== test_vx_circle ===="); - expected = [[-3, -10], [-2, -10], [-1, -10], [0, -10], [1, -10], [2, -10], [3, -10], [-5, -9], [-4, -9], [-3, -9], [-2, -9], [-1, -9], [0, -9], [1, -9], [2, -9], [3, -9], [4, -9], [5, -9], [-6, -8], [-5, -8], [-4, -8], [-3, -8], [-2, -8], [-1, -8], [0, -8], [1, -8], [2, -8], [3, -8], [4, -8], [5, -8], [6, -8], [-7, -7], [-6, -7], [-5, -7], [-4, -7], [-3, -7], [-2, -7], [-1, -7], [0, -7], [1, -7], [2, -7], [3, -7], [4, -7], [5, -7], [6, -7], [7, -7], [-8, -6], [-7, -6], [-6, -6], [-5, -6], [-4, -6], [-3, -6], [-2, -6], [-1, -6], [0, -6], [1, -6], [2, -6], [3, -6], [4, -6], [5, -6], [6, -6], [7, -6], [8, -6], [-9, -5], [-8, -5], [-7, -5], [-6, -5], [-5, -5], [-4, -5], [-3, -5], [-2, -5], [-1, -5], [0, -5], [1, -5], [2, -5], [3, -5], [4, -5], [5, -5], [6, -5], [7, -5], [8, -5], [9, -5], [-9, -4], [-8, -4], [-7, -4], [-6, -4], [-5, -4], [-4, -4], [-3, -4], [-2, -4], [-1, -4], [0, -4], [1, -4], [2, -4], [3, -4], [4, -4], [5, -4], [6, -4], [7, -4], [8, -4], [9, -4], [-10, -3], [-9, -3], [-8, -3], [-7, -3], [-6, -3], [-5, -3], [-4, -3], [-3, -3], [-2, -3], [-1, -3], [0, -3], [1, -3], [2, -3], [3, -3], [4, -3], [5, -3], [6, -3], [7, -3], [8, -3], [9, -3], [10, -3], [-10, -2], [-9, -2], [-8, -2], [-7, -2], [-6, -2], [-5, -2], [-4, -2], [-3, -2], [-2, -2], [-1, -2], [0, -2], [1, -2], [2, -2], [3, -2], [4, -2], [5, -2], [6, -2], [7, -2], [8, -2], [9, -2], [10, -2], [-10, -1], [-9, -1], [-8, -1], [-7, -1], [-6, -1], [-5, -1], [-4, -1], [-3, -1], [-2, -1], [-1, -1], [0, -1], [1, -1], [2, -1], [3, -1], [4, -1], [5, -1], [6, -1], [7, -1], [8, -1], [9, -1], [10, -1], [-10, 0], [-9, 0], [-8, 0], [-7, 0], [-6, 0], [-5, 0], [-4, 0], [-3, 0], [-2, 0], [-1, 0], [0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0], [10, 0], [-10, 1], [-9, 1], [-8, 1], [-7, 1], [-6, 1], [-5, 1], [-4, 1], [-3, 1], [-2, 1], [-1, 1], [0, 1], [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [7, 1], [8, 1], [9, 1], [10, 1], [-10, 2], [-9, 2], [-8, 2], [-7, 2], [-6, 2], [-5, 2], [-4, 2], [-3, 2], [-2, 2], [-1, 2], [0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [5, 2], [6, 2], [7, 2], [8, 2], [9, 2], [10, 2], [-10, 3], [-9, 3], [-8, 3], [-7, 3], [-6, 3], [-5, 3], [-4, 3], [-3, 3], [-2, 3], [-1, 3], [0, 3], [1, 3], [2, 3], [3, 3], [4, 3], [5, 3], [6, 3], [7, 3], [8, 3], [9, 3], [10, 3], [-9, 4], [-8, 4], [-7, 4], [-6, 4], [-5, 4], [-4, 4], [-3, 4], [-2, 4], [-1, 4], [0, 4], [1, 4], [2, 4], [3, 4], [4, 4], [5, 4], [6, 4], [7, 4], [8, 4], [9, 4], [-9, 5], [-8, 5], [-7, 5], [-6, 5], [-5, 5], [-4, 5], [-3, 5], [-2, 5], [-1, 5], [0, 5], [1, 5], [2, 5], [3, 5], [4, 5], [5, 5], [6, 5], [7, 5], [8, 5], [9, 5], [-8, 6], [-7, 6], [-6, 6], [-5, 6], [-4, 6], [-3, 6], [-2, 6], [-1, 6], [0, 6], [1, 6], [2, 6], [3, 6], [4, 6], [5, 6], [6, 6], [7, 6], [8, 6], [-7, 7], [-6, 7], [-5, 7], [-4, 7], [-3, 7], [-2, 7], [-1, 7], [0, 7], [1, 7], [2, 7], [3, 7], [4, 7], [5, 7], [6, 7], [7, 7], [-6, 8], [-5, 8], [-4, 8], [-3, 8], [-2, 8], [-1, 8], [0, 8], [1, 8], [2, 8], [3, 8], [4, 8], [5, 8], [6, 8], [-5, 9], [-4, 9], [-3, 9], [-2, 9], [-1, 9], [0, 9], [1, 9], [2, 9], [3, 9], [4, 9], [5, 9], [-3, 10], [-2, 10], [-1, 10], [0, 10], [1, 10], [2, 10], [3, 10]]; + expected = [[-1, 0], [-7, -1], [0, -1], [-2, 1], [-6, -2], [-10, 2], [-3, 2], [-5, -3], [-4, 3], [-4, -4], [-5, 4], [3, 9], [-3, -5], [-6, 5], [-2, -6], [-7, 6], [0, 6], [2, 8], [-1, -7], [1, 7], [-2, 0], [-6, -1], [-10, 1], [-3, 1], [-5, -2], [-4, 2], [-4, -3], [-5, 3], [-3, -4], [-6, 4], [4, 9], [-2, -5], [-7, 5], [0, 5], [-1, -6], [-8, 6], [1, 6], [3, 8], [2, 7], [-10, 0], [-3, 0], [-5, -1], [-4, 1], [-4, -2], [-5, 2], [-10, -3], [-3, -3], [-6, 3], [3, 10], [-2, -4], [-7, 4], [0, 4], [-1, -5], [-8, 5], [1, 5], [5, 9], [2, 6], [4, 8], [3, 7], [-4, 0], [-4, -1], [-5, 1], [-10, -2], [-3, -2], [-6, 2], [2, 10], [-2, -3], [-7, 3], [0, 3], [-1, -4], [-8, 4], [1, 4], [5, -9], [9, -5], [-9, 5], [2, 5], [6, -8], [8, -6], [3, 6], [5, 8], [7, -7], [4, 7], [-5, 0], [-1, -10], [-10, -1], [-3, -1], [-6, 1], [1, 10], [-2, -2], [-7, 2], [0, 2], [-1, -3], [-8, 3], [1, 3], [4, -9], [9, -4], [-9, 4], [2, 4], [8, -5], [3, 5], [5, -8], [7, -6], [4, 6], [6, 8], [6, -7], [5, 7], [0, 10], [-6, 0], [-2, -1], [-7, 1], [0, 1], [-2, -10], [-1, -2], [-8, 2], [1, 2], [9, -3], [-9, 3], [2, 3], [3, -9], [8, -4], [3, 4], [7, -5], [4, 5], [4, -8], [6, -6], [5, 6], [5, -7], [6, 7], [-7, 0], [0, 0], [-1, -1], [-8, 1], [1, 1], [9, -2], [-9, 2], [2, 2], [-3, -10], [8, -3], [3, 3], [2, -9], [7, -4], [4, 4], [6, -5], [5, 5], [3, -8], [5, -6], [6, 6], [4, -7], [7, 7], [-8, 0], [1, 0], [9, -1], [-9, 1], [2, 1], [8, -2], [3, 2], [7, -3], [4, 3], [1, -9], [6, -4], [5, 4], [-1, 9], [5, -5], [6, 5], [2, -8], [4, -6], [7, 6], [3, -7], [-9, 0], [2, 0], [8, -1], [3, 1], [7, -2], [4, 2], [6, -3], [5, 3], [10, 3], [0, -9], [5, -4], [6, 4], [-2, 9], [4, -5], [7, 5], [1, -8], [3, -6], [8, 6], [-1, 8], [2, -7], [3, 0], [7, -1], [4, 1], [6, -2], [5, 2], [10, 2], [5, -3], [10, -3], [6, 3], [-3, 10], [4, -4], [7, 4], [-3, 9], [3, -5], [8, 5], [0, -8], [2, -6], [-2, 8], [1, -7], [-1, 7], [0, -10], [4, 0], [6, -1], [5, 1], [10, 1], [5, -2], [10, -2], [6, 2], [-2, 10], [4, -3], [7, 3], [3, -4], [8, 4], [-4, 9], [-5, -9], [-9, -5], [2, -5], [9, 5], [-6, -8], [-8, -6], [1, -6], [-1, 6], [-3, 8], [-7, -7], [0, -7], [-2, 7], [5, 0], [10, 0], [1, -10], [5, -1], [10, -1], [6, 1], [-1, 10], [4, -2], [7, 2], [3, -3], [8, 3], [-4, -9], [-9, -4], [2, -4], [9, 4], [-8, -5], [1, -5], [-1, 5], [-5, 9], [-5, -8], [-7, -6], [0, -6], [-2, 6], [-4, 8], [-6, -7], [-3, 7], [6, 0], [4, -1], [7, 1], [2, -10], [3, -2], [8, 2], [-9, -3], [2, -3], [9, 3], [-3, -9], [-8, -4], [1, -4], [-1, 4], [-7, -5], [0, -5], [-2, 5], [-4, -8], [-6, -6], [-3, 6], [-5, 8], [-5, -7], [-4, 7], [7, 0], [3, -1], [8, 1], [-9, -2], [2, -2], [9, 2], [3, -10], [-8, -3], [1, -3], [-1, 3], [-2, -9], [-7, -4], [0, -4], [-2, 4], [0, 9], [-6, -5], [-3, 5], [-3, -8], [-5, -6], [-4, 6], [-6, 8], [-4, -7], [-5, 7], [8, 0], [-9, -1], [2, -1], [9, 1], [-8, -2], [1, -2], [-1, 2], [-7, -3], [0, -3], [-2, 3], [-1, -9], [-6, -4], [-3, 4], [1, 9], [-5, -5], [-4, 5], [-2, -8], [-4, -6], [-5, 6], [0, 8], [-3, -7], [-6, 7], [9, 0], [-8, -1], [1, -1], [-1, 1], [-7, -2], [0, -2], [-2, 2], [-6, -3], [-10, 3], [-3, 3], [-5, -4], [-4, 4], [2, 9], [-4, -5], [-5, 5], [-1, -8], [-3, -6], [-6, 6], [1, 8], [-2, -7], [-7, 7], [0, 7]]; actual = vx_circle(10, true); assertEqualPoints(expected, actual);