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

optimization

This commit is contained in:
Justin Lin
2022-05-01 20:32:19 +08:00
parent 2c7db9d85c
commit 19b2526483
3 changed files with 28 additions and 69 deletions

View File

@@ -1,7 +1,5 @@
use <_nz_worley_comm.scad>;
_less = function(a, b) a[2] < b[2];
function _neighbors(fcord, seed, grid_w) =
let(range = [-1:1], fx = fcord.x, fy = fcord.y)
[
@@ -17,30 +15,10 @@ function _neighbors(fcord, seed, grid_w) =
nbr
];
function _nz_worley2_classic(p, nbrs, dist) =
let(
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")
)
_euclidean_half_sorted(cells, _less)[0];
function _nz_worley2_border(p, nbrs) =
let(
cells = [for(nbr = nbrs) [each nbr, norm(nbr - p)]],
sorted_cells = _border_half_sorted(cells, _less),
fst = sorted_cells[0],
snd = orted_cells[1],
a = [fst.x, fst.y],
m = (a + [snd.x, snd.y]) / 2
)
[fst.x, fst.y, (p - m) * (a - m)];
function _nz_worley2(p, seed, grid_w, dist) =
let(
fcord = [floor(p.x / grid_w), floor(p.y / grid_w)],
nbrs = _neighbors(fcord, seed, grid_w)
)
dist == "border" ? _nz_worley2_border(p, nbrs) :
_nz_worley2_classic(p, nbrs, dist);
dist == "border" ? _nz_worley_border(p, nbrs) :
_nz_worley_classic(p, nbrs, dist);

View File

@@ -1,7 +1,5 @@
use <_nz_worley_comm.scad>;
_less = function(a, b) a[3] < b[3];
function _neighbors(fcord, seed, grid_w) =
let(range = [-1:1], fx = fcord.x, fy = fcord.y, fz = fcord.z)
[
@@ -19,30 +17,10 @@ function _neighbors(fcord, seed, grid_w) =
nbr
];
function _nz_worley3_classic(p, nbrs, dist) =
let(
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")
)
_euclidean_half_sorted(cells, _less)[0];
function _nz_worley3_border(p, nbrs) =
let(
cells = [for(nbr = nbrs) [each nbr, norm(nbr - p)]],
sorted_cells = _border_half_sorted(cells, _less),
fst = sorted_cells[0],
snd = sorted_cells[1],
a = [fst.x, fst.y, fst.z],
m = (a + [snd.x, snd.y, snd.z]) / 2
)
[fst.x, fst.y, fst.z, (p - m) * (a - m)];
function _nz_worley3(p, seed, grid_w, dist) =
let(
fcord = [floor(p.x / grid_w), floor(p.y / grid_w), floor(p.z / grid_w)],
nbrs = _neighbors(fcord, seed, grid_w)
)
dist == "border" ? _nz_worley3_border(p, nbrs) :
_nz_worley3_classic(p, nbrs, dist);
dist == "border" ? _nz_worley_border(p, nbrs) :
_nz_worley_classic(p, nbrs, dist);

View File

@@ -9,25 +9,28 @@ function _manhattan(v) = sum([for(d = v) abs(d)]);
function _chebyshev(p1, p2) =
max([for(i = [0:len(p1) - 1]) abs(p1[i] - p2[i])]);
function _euclidean_half_sorted(cells, less) =
let(leng = len(cells))
leng <= 1 ? cells :
leng == 2 ? less(cells[0], cells[1]) ? cells : [cells[1], cells[0]] :
let(
pivot = cells[0],
before = [for(j = 1; j < leng; j = j + 1) if(less(cells[j], pivot)) cells[j]]
)
[each _euclidean_half_sorted(before, less), pivot];
function _nz_worley_classic(p, nbrs, dist) =
let(
dists = dist == "euclidean" ? [for(nbr = nbrs) norm(nbr - p)] :
dist == "manhattan" ? [for(nbr = nbrs) _manhattan(nbr - p)] :
dist == "chebyshev" ? [for(nbr = nbrs) _chebyshev(nbr, p)] :
assert("Unknown distance option"),
m = min(dists),
i = search(m, dists)[0]
)
[each nbrs[i], m];
function _border_half_sorted(cells, less) =
let(leng = len(cells))
leng <= 1 ? cells :
leng == 2 ? less(cells[0], cells[1]) ? cells : [cells[1], cells[0]] :
let(
pivot = cells[0],
before = [for(j = 1; j < leng; j = j + 1) if(less(cells[j], pivot)) cells[j]]
)
before == [] ?
[pivot, each _border_half_sorted([for(j = 1; j < leng; j = j + 1) if(!less(cells[j], pivot)) cells[j]], less)] :
[each _border_half_sorted(before, less), pivot];
function _nz_worley_border(p, nbrs) =
let(
dists = [for(nbr = nbrs) norm(nbr - p)],
m1 = min(dists),
i1 = search(m1, dists)[0],
dists2 = [for(i = [0:len(dists) - 1]) if(i != i1) dists[i]],
nbrs2 = [for(i = [0:len(nbrs) - 1]) if(i != i1) nbrs[i]],
m2 = min(dists2),
i2 = search(m2, dists2)[0],
fst = nbrs[i1],
snd = nbrs2[i2],
m = (fst + snd) / 2
)
[each fst, (p - m) * (fst - m)];