1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-23 23:03:23 +02:00
This commit is contained in:
Justin Lin
2020-03-30 08:36:14 +08:00
parent d5fce20b72
commit eebfb5d8d7
18 changed files with 81 additions and 196 deletions

View File

@@ -1,27 +1,45 @@
use <experimental/_impl/_nz_worley_comm.scad>; use <experimental/_impl/_nz_worley_comm.scad>;
use <util/sort.scad>;
function _neighbors(fcord, seed, dim, m, n) = [ function _neighbors(fcord, seed, cell_w) = [
for(y = [-1:1]) for(y = [-1:1])
for(x = [-1:1]) for(x = [-1:1])
let( let(
nx = fcord[0] + x, nx = fcord[0] + x,
ny = fcord[1] + y, ny = fcord[1] + y,
sd_base = nx + ny * dim, sd_base = nx + ny * cell_w,
sd1 = _lookup_noise_table(seed + sd_base), sd1 = _lookup_noise_table(seed + sd_base),
sd2 = _lookup_noise_table(sd1 * 255 + sd_base), sd2 = _lookup_noise_table(sd1 * 255 + sd_base),
nbr = [(nx + sd1) * m, (ny + sd2) * n] nbr = [(nx + sd1) * cell_w, (ny + sd2) * cell_w]
) )
nbr nbr
]; ];
function _nz_worley2(p, seed, dim, m, n, dist) = function _nz_worley2_classic(p, nbrs, dist) =
min([
for(nbr = nbrs)
if(!is_undef(nbr[1])) // Here's a workaround for a weired undef problem. bug of 2019.05?
_distance(nbr, p, dist)
]);
function _nz_worley2_border(p, nbrs, dist) =
let( let(
fcord = [floor(p[0] / m), floor(p[1] / n)],
nbrs = _neighbors(fcord, seed, dim, m, n),
dists = [ dists = [
for(nbr = nbrs) for(nbr = nbrs)
if(!is_undef(nbr[1])) // Here's a workaround for a weired undef problem. bug of 2019.05? if(!is_undef(nbr[1])) // Here's a workaround for a weired undef problem. bug of 2019.05?
_distance(nbr, p, dist) [nbr[0], nbr[1], norm(nbr - p)]
] ],
sorted = sort(dists, by = "z"),
a = [sorted[0][0], sorted[0][1]],
b = [sorted[1][0], sorted[1][1]],
m = (a + b) / 2
) )
min(dists); (p - m) * (a - m);
function _nz_worley2(p, seed, cell_w, dist) =
let(
fcord = [floor(p[0] / cell_w), floor(p[1] / cell_w)],
nbrs = _neighbors(fcord, seed, cell_w)
)
dist == "border" ? _nz_worley2_border(p, nbrs, dist) :
_nz_worley2_classic(p, nbrs, dist);

View File

@@ -1,6 +1,7 @@
use <experimental/_impl/_nz_worley_comm.scad>; use <experimental/_impl/_nz_worley_comm.scad>;
use <util/sort.scad>;
function _neighbors(fcord, seed, dim, m, n, o) = [ function _neighbors(fcord, seed, cell_w) = [
for(z = [-1:1]) for(z = [-1:1])
for(y = [-1:1]) for(y = [-1:1])
for(x = [-1:1]) for(x = [-1:1])
@@ -8,23 +9,40 @@ function _neighbors(fcord, seed, dim, m, n, o) = [
nx = fcord[0] + x, nx = fcord[0] + x,
ny = fcord[1] + y, ny = fcord[1] + y,
nz = fcord[2] + z, nz = fcord[2] + z,
sd_base = nx + ny * dim + nz * dim * dim, sd_base = nx + ny * cell_w + nz * cell_w * cell_w,
sd1 = _lookup_noise_table(seed + sd_base), sd1 = _lookup_noise_table(seed + sd_base),
sd2 = _lookup_noise_table(sd1 * 255 + sd_base), sd2 = _lookup_noise_table(sd1 * 255 + sd_base),
sd3 = _lookup_noise_table(sd2 * 255 + sd_base), sd3 = _lookup_noise_table(sd2 * 255 + sd_base),
nbr = [(nx + sd1) * m, (ny + sd2) * n, (nz + sd3) * o] nbr = [(nx + sd1) * cell_w, (ny + sd2) * cell_w, (nz + sd3) * cell_w]
) )
nbr nbr
]; ];
function _nz_worley3(p, seed, dim, m, n, o, dist) = function _nz_worley3_classic(p, nbrs, dist) =
min([
for(nbr = nbrs)
if(!is_undef(nbr[1])) // Here's a workaround for a weired undef problem. bug of 2019.05?
_distance(nbr, p, dist)
]);
function _nz_worley3_border(p, nbrs, dist) =
let( let(
fcord = [floor(p[0] / m), floor(p[1] / n), floor(p[2] / o)],
nbrs = _neighbors(fcord, seed, dim, m, n, o),
dists = [ dists = [
for(nbr = nbrs) for(nbr = nbrs)
if(!is_undef(nbr[1])) // Here's a workaround for a weired undef problem. bug of 2019.05? if(!is_undef(nbr[1])) // Here's a workaround for a weired undef problem. bug of 2019.05?
_distance(nbr, p, dist) [nbr[0], nbr[1], nbr[2], norm(nbr - p)]
] ],
sorted = sort(dists, by = "idx", idx = 3),
a = [sorted[0][0], sorted[0][1], sorted[0][2]],
b = [sorted[1][0], sorted[1][1], sorted[1][2]],
m = (a + b) / 2
) )
min(dists); (p - m) * (a - m);
function _nz_worley3(p, seed, cell_w, dist) =
let(
fcord = [floor(p[0] / cell_w), floor(p[1] / cell_w), floor(p[2] / cell_w)],
nbrs = _neighbors(fcord, seed, cell_w)
)
dist == "border" ? _nz_worley3_border(p, nbrs, dist) :
_nz_worley3_classic(p, nbrs, dist);

View File

@@ -1,19 +0,0 @@
use <experimental/nz_voronoi2.scad>;
size = [100, 50];
dim = 5;
seed = 5;
points = [
for(y = [0:size[1] - 1])
for(x = [0:size[0] - 1])
[x, y, nz_voronoi2(size, x, y, seed, dim)]
];
max_dist = max([for(p = points) p[2]]);
for(p = points) {
c = p[2] / max_dist;
color([c, c, c])
translate([p[0], p[1]])
square(1);
}

View File

@@ -1,21 +0,0 @@
use <experimental/nz_voronoi2s.scad>;
size = [100, 50];
dim = 5;
seed = 5;
points = [
for(y = [0:size[1] - 1])
for(x = [0:size[0] - 1])
[x, y]
];
noises = nz_voronoi2s(size, points, seed, dim);
max_dist = max(noises);
for(i = [0:len(noises) - 1]) {
c = noises[i] / max_dist;
color([c, c, c])
translate(points[i])
square(1);
}

View File

@@ -1,21 +0,0 @@
use <experimental/nz_voronoi3.scad>;
size = [20, 20, 20];
dim = 2;
seed = 1;
points = [
for(z = [0:size[2] - 1])
for(y = [0:size[1] - 1])
for(x = [0:size[0] - 1])
[x, y, z, nz_voronoi3(size, x, y, z, seed, dim)]
];
max_dist = max([for(p = points) p[3]]);
for(p = points) {
c = p[3] / max_dist * 2;
color([c > 1 ? 1 : c , 0, 0])
translate([p[0], p[1], p[2]])
cube(1);
}

View File

@@ -1,22 +0,0 @@
use <experimental/nz_voronoi3s.scad>;
size = [20, 20, 20];
dim = 2;
seed = 5;
points = [
for(z = [0:size[2] - 1])
for(y = [0:size[1] - 1])
for(x = [0:size[0] - 1])
[x, y, z]
];
noises = nz_voronoi3s(size, points, seed, dim);
max_dist = max(noises);
for(i = [0:len(noises) - 1]) {
c = noises[i] / max_dist * 2;
color([c > 1 ? 1 : c , 0, 0])
translate(points[i])
square(1);
}

View File

@@ -1,14 +1,14 @@
use <experimental/nz_worley2.scad>; use <experimental/nz_worley2.scad>;
size = [100, 50]; size = [100, 50];
dim = 5; cell_w = 10;
dist = "euclidean"; // [euclidean, manhattan, chebyshev] dist = "euclidean"; // [euclidean, manhattan, chebyshev, border]
seed = 5; seed = 51;
points = [ points = [
for(y = [0:size[1] - 1]) for(y = [0:size[1] - 1])
for(x = [0:size[0] - 1]) for(x = [0:size[0] - 1])
[x, y, nz_worley2(size, x, y, seed, dim, dist)] [x, y, nz_worley2(x, y, seed, cell_w, dist)]
]; ];
max_dist = max([for(p = points) p[2]]); max_dist = max([for(p = points) p[2]]);

View File

@@ -1,8 +1,8 @@
use <experimental/nz_worley2s.scad>; use <experimental/nz_worley2s.scad>;
size = [100, 50]; size = [100, 50];
dim = 5; cell_w = 10;
dist = "euclidean"; // [euclidean, manhattan, chebyshev] dist = "euclidean"; // [euclidean, manhattan, chebyshev, border]
seed = 5; seed = 5;
points = [ points = [
@@ -11,7 +11,7 @@ points = [
[x, y] [x, y]
]; ];
noises = nz_worley2s(size, points, seed, dim, dist); noises = nz_worley2s(points, seed, cell_w, dist);
max_dist = max(noises); max_dist = max(noises);
for(i = [0:len(noises) - 1]) { for(i = [0:len(noises) - 1]) {

View File

@@ -1,15 +1,15 @@
use <experimental/nz_worley3.scad>; use <experimental/nz_worley3.scad>;
size = [20, 20, 20]; size = [20, 20, 20];
dim = 5; cell_w = 5;
dist = "euclidean"; // [euclidean, manhattan, chebyshev] dist = "euclidean"; // [euclidean, manhattan, chebyshev, border]
seed = 5; seed = 5;
points = [ points = [
for(z = [0:size[2] - 1]) for(z = [0:size[2] - 1])
for(y = [0:size[1] - 1]) for(y = [0:size[1] - 1])
for(x = [0:size[0] - 1]) for(x = [0:size[0] - 1])
[x, y, z, nz_worley3(size, x, y, z, seed, dim, dist)] [x, y, z, nz_worley3(x, y, z, seed, cell_w, dist)]
]; ];
max_dist = max([for(p = points) p[3]]); max_dist = max([for(p = points) p[3]]);

View File

@@ -1,8 +1,8 @@
use <experimental/nz_worley3s.scad>; use <experimental/nz_worley3s.scad>;
size = [20, 20, 20]; size = [20, 20, 20];
dim = 5; cell_w = 5;
dist = "euclidean"; // [euclidean, manhattan, chebyshev] dist = "euclidean"; // [euclidean, manhattan, chebyshev, border]
seed = 5; seed = 5;
points = [ points = [
@@ -12,7 +12,7 @@ points = [
[x, y, z] [x, y, z]
]; ];
noises = nz_worley3s(size, points, seed, dim, dist); noises = nz_worley3s(points, seed, cell_w, dist);
max_dist = max(noises); max_dist = max(noises);

View File

@@ -1,11 +0,0 @@
use <util/rand.scad>;
use <experimental/_impl/_nz_voronoi2_impl.scad>;
function nz_voronoi2(size, x, y, seed, dim = 2) =
let(
sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256),
// m*n pixels per grid
m = size[0] / dim,
n = size[1] / dim
)
_nz_voronoi2([x, y], sd, dim, m, n);

View File

@@ -1,11 +0,0 @@
use <util/rand.scad>;
use <experimental/_impl/_nz_voronoi2_impl.scad>;
function nz_voronoi2s(size, points, seed, dim = 2) =
let(
sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256),
// m*n pixels per grid
m = size[0] / dim,
n = size[1] / dim
)
[for(p = points) _nz_voronoi2(p, sd, dim, m, n)];

View File

@@ -1,12 +0,0 @@
use <util/rand.scad>;
use <experimental/_impl/_nz_voronoi3_impl.scad>;
function nz_voronoi3(size, x, y, z, seed, dim = 2) =
let(
sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256),
// m*n pixels per grid
m = size[0] / dim,
n = size[1] / dim,
o = size[2] / dim
)
_nz_voronoi3([x, y, z], sd, dim, m, n, o);

View File

@@ -1,12 +0,0 @@
use <util/rand.scad>;
use <experimental/_impl/_nz_voronoi3_impl.scad>;
function nz_voronoi3s(size, points, seed, dim = 2) =
let(
sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256),
// m*n pixels per grid
m = size[0] / dim,
n = size[1] / dim,
o = size[2] / dim
)
[for(p = points) _nz_voronoi3(p, sd, dim, m, n, o)];

View File

@@ -1,11 +1,6 @@
use <util/rand.scad>; use <util/rand.scad>;
use <experimental/_impl/_nz_worley2_impl.scad>; use <experimental/_impl/_nz_worley2_impl.scad>;
function nz_worley2(size, x, y, seed, dim = 2, dist = "euclidean") = function nz_worley2(x, y, seed, cell_w = 10, dist = "euclidean") =
let( let(sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256))
sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256), _nz_worley2([x, y], sd, cell_w, dist);
// m*n pixels per grid
m = size[0] / dim,
n = size[1] / dim
)
_nz_worley2([x, y], sd, dim, m, n, dist);

View File

@@ -1,11 +1,6 @@
use <util/rand.scad>; use <util/rand.scad>;
use <experimental/_impl/_nz_worley2_impl.scad>; use <experimental/_impl/_nz_worley2_impl.scad>;
function nz_worley2s(size, points, seed, dim = 2, dist = "euclidean") = function nz_worley2s(points, seed, cell_w = 10, dist = "euclidean") =
let( let(sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256))
sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256), [for(p = points) _nz_worley2(p, sd, cell_w, dist)];
// m*n pixels per grid
m = size[0] / dim,
n = size[1] / dim
)
[for(p = points) _nz_worley2(p, sd, dim, m, n, dist)];

View File

@@ -1,12 +1,6 @@
use <util/rand.scad>; use <util/rand.scad>;
use <experimental/_impl/_nz_worley3_impl.scad>; use <experimental/_impl/_nz_worley3_impl.scad>;
function nz_worley3(size, x, y, z, seed, dim = 2, dist = "euclidean") = function nz_worley3(x, y, z, seed, cell_w = 10, dist = "euclidean") =
let( let(sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256))
sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256), _nz_worley3([x, y, z], sd, cell_w, dist);
// m*n pixels per grid
m = size[0] / dim,
n = size[1] / dim,
o = size[2] / dim
)
_nz_worley3([x, y, z], sd, dim, m, n, o, dist);

View File

@@ -1,12 +1,6 @@
use <util/rand.scad>; use <util/rand.scad>;
use <experimental/_impl/_nz_worley3_impl.scad>; use <experimental/_impl/_nz_worley3_impl.scad>;
function nz_worley3s(size, points, seed, dim = 2, dist = "euclidean") = function nz_worley3s(points, seed, cell_w = 10, dist = "euclidean") =
let( let(sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256))
sd = 6 + (is_undef(seed) ? floor(rand(0, 256)) : seed % 256), [for(p = points) _nz_worley3(p, sd, cell_w, dist)];
// m*n pixels per grid
m = size[0] / dim,
n = size[1] / dim,
o = size[2] / dim
)
[for(p = points) _nz_worley3(p, sd, dim, m, n, o, dist)];