From c3d5484fde396a7ec8f34b4a1285b09a724e7876 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 9 Apr 2020 14:53:05 +0800 Subject: [PATCH] symmetric --- examples/voronoi/voronoi_torus.scad | 15 ++++++++++++++ .../_impl/_voronoi_square_cells_impl.scad | 20 +++++++++++-------- src/experimental/voronoi_square_cells.scad | 10 +++++++--- 3 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 examples/voronoi/voronoi_torus.scad diff --git a/examples/voronoi/voronoi_torus.scad b/examples/voronoi/voronoi_torus.scad new file mode 100644 index 00000000..7da18956 --- /dev/null +++ b/examples/voronoi/voronoi_torus.scad @@ -0,0 +1,15 @@ +use ; +use ; +use ; + +size = [40, 80]; +grid_w = 5; +cells = voronoi_square_cells(size, grid_w); + +$fn = 4; + +for(cell = cells) { + cell_poly = [for(p = cell[1]) ptf_torus(size, p, [10, 5], [360, 360])]; + + hull_polyline3d(cell_poly, thickness = 1); +} \ No newline at end of file diff --git a/src/experimental/_impl/_voronoi_square_cells_impl.scad b/src/experimental/_impl/_voronoi_square_cells_impl.scad index 65861ec7..f9ef0317 100644 --- a/src/experimental/_impl/_voronoi_square_cells_impl.scad +++ b/src/experimental/_impl/_voronoi_square_cells_impl.scad @@ -4,28 +4,32 @@ _noise_table = [0.592157, 0.627451, 0.537255, 0.356863, 0.352941, 0.0588235, 0.5 function _lookup_noise_table(i) = _noise_table[i % 256]; -function cell_pt(fcord, grid_w, seed, x, y) = +function cell_pt(fcord, grid_w, seed, x, y, gw, gh) = let( nx = fcord[0] + x, ny = fcord[1] + y, - sd_base = abs(nx + ny * grid_w), + sd_x = nx < 0 ? nx + gw : + nx >= gw ? nx % gw : nx, + sd_y = ny < 0 ? ny + gh : + ny >= gh ? ny % gh : ny, + sd_base = abs(sd_x + sd_y * grid_w), sd1 = _lookup_noise_table(seed + sd_base), sd2 = _lookup_noise_table(sd1 * 255 + sd_base) ) [(nx + sd1) * grid_w, (ny + sd2) * grid_w]; // 21-nearest-neighbor -function _neighbors(fcord, seed, grid_w) = +function _neighbors(fcord, seed, grid_w, gw, gh) = concat( [ for(y = [-1:1]) for(x = [-1:1]) - cell_pt(fcord, grid_w, seed, x, y) + cell_pt(fcord, grid_w, seed, x, y, gw, gh) ], - [for(x = [-1:1]) cell_pt(fcord, grid_w, seed, x, -2)], - [for(x = [-1:1]) cell_pt(fcord, grid_w, seed, x, 2)], - [for(y = [-1:1]) cell_pt(fcord, grid_w, seed, -2, y)], - [for(y = [-1:1]) cell_pt(fcord, grid_w, seed, 2, y)] + [for(x = [-1:1]) cell_pt(fcord, grid_w, seed, x, -2, gw, gh)], + [for(x = [-1:1]) cell_pt(fcord, grid_w, seed, x, 2, gw, gh)], + [for(y = [-1:1]) cell_pt(fcord, grid_w, seed, -2, y, gw, gh)], + [for(y = [-1:1]) cell_pt(fcord, grid_w, seed, 2, y, gw, gh)] ); function _cells_lt_before_intersection(shape, size, points, pt, half_region_size) = diff --git a/src/experimental/voronoi_square_cells.scad b/src/experimental/voronoi_square_cells.scad index c57a18c3..6e1cd5a5 100644 --- a/src/experimental/voronoi_square_cells.scad +++ b/src/experimental/voronoi_square_cells.scad @@ -8,13 +8,17 @@ function voronoi_square_cells(size, grid_w, seed) = region_size = grid_w * 3, half_region_size = region_size * 0.5, shape = shape_square(grid_w * 3), - cell_nbrs_lt = [for(cy = [0:grid_w:size[1]]) - for(cx = [0:grid_w:size[0]]) + gw = size[0] / grid_w, + gh = size[1] / grid_w, + cell_nbrs_lt = [for(cy = [-grid_w:grid_w:size[1]]) + for(cx = [-grid_w:grid_w:size[0]]) let( nbrs = _neighbors( [floor(cx / grid_w), floor(cy / grid_w)], sd, - grid_w + grid_w, + gw, + gh ), p = nbrs[4], points = concat(