1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-12 09:44:16 +02:00

refactor: use sorted

This commit is contained in:
Justin Lin
2022-04-15 09:21:36 +08:00
parent 89b5602026
commit ed4d8efb55
8 changed files with 43 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
use <../util/slice.scad>;
use <../util/sort.scad>;
use <../util/sorted.scad>;
// oa->ob ct_clk : greater than 0
function _convex_hull_impl_dir(o, a, b) = cross(a - o, b - o);
@@ -34,10 +34,10 @@ function _convex_hull_upper_chain(points, chain, m, t, i) =
function _convex_hull2(points) =
let(
sorted = sort(points, by = function(p1, p2) p1 < p2 ? -1 : 1),
leng = len(sorted),
lwr_ch = _convex_hull_lower_chain(sorted, leng, [], 0, 0),
sorted_pts = sorted(points),
leng = len(sorted_pts),
lwr_ch = _convex_hull_lower_chain(sorted_pts, leng, [], 0, 0),
leng_lwr_ch = len(lwr_ch),
chain = _convex_hull_upper_chain(sorted, lwr_ch, leng_lwr_ch, leng_lwr_ch + 1, leng - 2)
chain = _convex_hull_upper_chain(sorted_pts, lwr_ch, leng_lwr_ch, leng_lwr_ch + 1, leng - 2)
)
[for(i = [0:len(chain) - 2]) chain[i]];

View File

@@ -1,4 +1,4 @@
use <../util/sort.scad>;
use <../util/sorted.scad>;
function normal(pts, f) = cross(pts[f[1]] - pts[f[0]], pts[f[2]] - pts[f[0]]);
@@ -69,15 +69,15 @@ function _all_faces(v0, v1, v2, v3, pts, pts_leng, vis, cur_faces, i = 0) =
function _convex_hull3(pts) =
let(
sorted = sort(pts, by = function(p1, p2) p1 < p2 ? -1 : 1),
leng = len(sorted),
sorted_pts = sorted(pts),
leng = len(sorted_pts),
v0 = 0,
v1 = _fst_v1(sorted, leng, v0 + 1),
v1 = _fst_v1(sorted_pts, leng, v0 + 1),
v2 = assert(v1 < leng, "common points")
_fst_v2(sorted, leng, v1, v1 + 1),
_fst_v2(sorted_pts, leng, v1, v1 + 1),
n = assert(v2 < leng, "collinear points")
cross(sorted[v1] - sorted[v0], sorted[v2] - sorted[v0]),
v3_d = _fst_v3(sorted, leng, n, 0, v2 + 1),
cross(sorted_pts[v1] - sorted_pts[v0], sorted_pts[v2] - sorted_pts[v0]),
v3_d = _fst_v3(sorted_pts, leng, n, 0, v2 + 1),
v3 = v3_d[0],
d = assert(v3 < leng, "coplanar points")
v3_d[1],
@@ -96,13 +96,13 @@ function _convex_hull3(pts) =
],
zeros = [for(j = [0:leng - 1]) 0],
init_vis = [for(i = [0:leng - 1]) zeros],
faces = _all_faces(v0, v1, v2, v3, sorted, leng, init_vis, fst_tetrahedron), // counter-clockwise
faces = _all_faces(v0, v1, v2, v3, sorted_pts, leng, init_vis, fst_tetrahedron), // counter-clockwise
reversed = [
for(face = faces) // OpenSCAD requires clockwise.
[face[2], face[1], face[0]]
]
)
[
sorted,
sorted_pts,
reversed
];

View File

@@ -11,7 +11,7 @@
use <../__comm__/_pt2_hash.scad>;
use <_impl/_mz_wang_tiles_impl.scad>;
use <mz_square_get.scad>;
use <../util/sort.scad>;
use <../util/sorted.scad>;
use <../util/set/hashset.scad>;
use <../util/set/hashset_elems.scad>;
@@ -45,7 +45,10 @@ function mz_wang_tiles(cells, left_border = true, bottom_border = true) =
[type == "TOP_WALL" || type == "NO_WALL" ? 1 : 0, y * 2 + 1]
]
),
dot_pts = sort(hashset_elems(hashset(all, hash = function(p) _pt2_hash(p))), by = "vt")
dot_pts = sorted(
hashset_elems(hashset(all, hash = function(p) _pt2_hash(p))),
function(a, b) let(d = a - b) d.y == 0 ? d.x : d.y
)
)
[
for(y = [0:rows - 1], x = [0:columns - 1])

View File

@@ -1,4 +1,4 @@
use <../../util/sort.scad>;
use <../../util/sorted.scad>;
use <../../util/sum.scad>;
function _manhattan(v) = sum([for(d = v) abs(d)]);
@@ -23,9 +23,9 @@ function _nz_cell_border(cells, p) =
[each cell, norm(cell - p)]
],
idx = len(cells[0]),
sorted = sort(dists, by = "idx", idx = idx),
sorted0 = sorted[0],
sorted1 = sorted[1],
sorted_dists = sorted(dists, cmp = function(a, b) a[idx] - b[idx]),
sorted0 = sorted_dists[0],
sorted1 = sorted_dists[1],
a = [for(i = [0:idx - 1]) sorted0[i]],
m = (a + [for(i = [0:idx - 1]) sorted1[i]]) / 2
)

View File

@@ -1,5 +1,7 @@
use <_nz_worley_comm.scad>;
use <../../util/sort.scad>;
use <../../util/sorted.scad>;
_cmp = function(a, b) a[2] - b[2];
function _neighbors(fcord, seed, grid_w) =
let(range = [-1:1])
@@ -21,10 +23,9 @@ function _nz_worley2_classic(p, nbrs, dist) =
cells = dist == "euclidean" ? [for(nbr = nbrs) [each nbr, norm(nbr - p)]] :
dist == "manhattan" ? [for(nbr = nbrs) [each nbr, _manhattan(nbr - p)]] :
dist == "chebyshev" ? [for(nbr = nbrs) [each nbr, _chebyshev(nbr, p)]] :
assert("Unknown distance option"),
sorted = sort(cells, by = "idx", idx = 2)
assert("Unknown distance option")
)
sorted[0];
sorted(cells, _cmp)[0];
function _nz_worley2_border(p, nbrs) =
let(
@@ -32,9 +33,9 @@ function _nz_worley2_border(p, nbrs) =
for(nbr = nbrs)
[each nbr, norm(nbr - p)]
],
sorted = sort(cells, by = "z"),
a = [sorted[0].x, sorted[0].y],
m = (a + [sorted[1].x, sorted[1].y]) / 2
sorted_cells = sorted(cells, _cmp),
a = [sorted_cells[0].x, sorted_cells[0].y],
m = (a + [sorted_cells[1].x, sorted_cells[1].y]) / 2
)
[a[0], a[1], (p - m) * (a - m)];

View File

@@ -1,5 +1,7 @@
use <_nz_worley_comm.scad>;
use <../../util/sort.scad>;
use <../../util/sorted.scad>;
_cmp = function(a, b) a[3] - b[3];
function _neighbors(fcord, seed, grid_w) =
let(range = [-1:1])
@@ -23,10 +25,9 @@ function _nz_worley3_classic(p, nbrs, dist) =
cells = dist == "euclidean" ? [for(nbr = nbrs) [each nbr, norm(nbr - p)]] :
dist == "manhattan" ? [for(nbr = nbrs) [each nbr, _manhattan(nbr - p)]] :
dist == "chebyshev" ? [for(nbr = nbrs) [each nbr, _chebyshev(nbr, p)]] :
assert("Unknown distance option"),
sorted = sort(cells, by = "idx", idx = 3)
assert("Unknown distance option")
)
sorted[0];
sorted(cells, _cmp)[0];
function _nz_worley3_border(p, nbrs) =
let(
@@ -34,9 +35,9 @@ function _nz_worley3_border(p, nbrs) =
for(nbr = nbrs)
[each nbr, norm(nbr - p)]
],
sorted = sort(cells, by = "idx", idx = 3),
a = [sorted[0].x, sorted[0].y, sorted[0].z],
m = (a + [sorted[1].x, sorted[1].y, sorted[1].z]) / 2
sorted_cells = sorted(cells, _cmp),
a = [sorted_cells[0].x, sorted_cells[0].y, sorted_cells[0].z],
m = (a + [sorted_cells[1].x, sorted_cells[1].y, sorted_cells[1].z]) / 2
)
[a[0], a[1], a[2], (p - m) * (a - m)];

View File

@@ -9,7 +9,7 @@
**/
use <../__comm__/_face_normal.scad>;
use <../util/sort.scad>;
use <../util/sorted.scad>;
use <../util/sum.scad>;
use <../surface/sf_solidifyT.scad>;
use <../triangle/tri_delaunay.scad>;
@@ -43,7 +43,7 @@ module sf_thickenT(points, thickness, triangles = undef, direction = "BOTH", con
if(is_list(direction)) {
dir_v = direction / norm(direction);
mid = sort(points)[leng_pts / 2];
mid = sorted(points)[leng_pts / 2];
tri = cnn_tris[search([mid], points)[0]][0];
nv = _face_normal([points[tri[0]], points[tri[1]], points[tri[2]]]);
off = dir_v * thickness;

View File

@@ -1,6 +1,6 @@
use <../../util/sort.scad>;
use <../../util/sorted.scad>;
use <_convex_centroid.scad>;
function _convex_ct_clk_order(points) =
let(cpt = _convex_centroid(points))
sort(points, by = function(p1, p2) cross(p2 - cpt, p1 - cpt));
sorted(points, cmp = function(p1, p2) cross(p2 - cpt, p1 - cpt));