From 183b3b74b61975b8e32c6598a97091b93544bc58 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 30 Jun 2022 10:54:22 +0800 Subject: [PATCH] add seed param --- src/pp/_impl/_pp_poisson.scad | 21 ++++++++++----------- src/pp/pp_poisson.scad | 6 +++--- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/pp/_impl/_pp_poisson.scad b/src/pp/_impl/_pp_poisson.scad index 2b3d58d5..078b4e59 100644 --- a/src/pp/_impl/_pp_poisson.scad +++ b/src/pp/_impl/_pp_poisson.scad @@ -1,6 +1,5 @@ use <../../matrix/m_replace.scad> use <../../util/every.scad> -use <../pp_disk.scad> function sampling(size, r, start, k) = let( @@ -43,19 +42,19 @@ function sampling_noAdjacentNeighbor(s, sample, x, y) = norm(sample - p) >= sampling_r(s)) ); -function sampling_randomSample(s, pos) = +function sampling_randomSample(s, pos, seed) = let( r = sampling_r(s), - a = rands(0, 360, 1)[0] + a = rands(0, 360, 1, seed)[0] ) - rands(r, 2 * r, 1)[0] * [cos(a), sin(a)] + pos; + rands(r, 2 * r, 1, seed + 1)[0] * [cos(a), sin(a)] + pos; function sampling_hasActive(s) = len(sampling_active(s)) > 0; -function sampling_kSamples(s, pos) = [ +function sampling_kSamples(s, pos, seed) = [ for(n = [0:sampling_k(s) - 1]) let( - sample = sampling_randomSample(s, pos), + sample = sampling_randomSample(s, pos, seed + n), w = sampling_w(s), x = floor(sample.x / w), y = floor(sample.y / w) @@ -77,12 +76,12 @@ function _sampling_minDistSample(s, pos, samples, min_s, min_dist, i = 1) = ) _sampling_minDistSample(s, pos, samples, params[0], params[1], i + 1); -function sampling_trySampleFromOneActive(s) = +function sampling_trySampleFromOneActive(s, seed) = let( active = sampling_active(s), - i = floor(rands(0, len(active) - 1, 1)[0]), + i = floor(rands(0, len(active) - 1, 1, seed)[0]), pos = active[i], - samples = sampling_kSamples(s, pos) + samples = sampling_kSamples(s, pos, seed) ) len(samples) == 0 ? [ @@ -115,6 +114,6 @@ function sampling_trySampleFromOneActive(s) = [each sampling_history(s), [pos, sample]] ]; -function _pp_poisson(s) = - !sampling_hasActive(s) ? s : _pp_poisson(sampling_trySampleFromOneActive(s)); +function _pp_poisson(s, seed, count = 0) = + !sampling_hasActive(s) ? s : _pp_poisson(sampling_trySampleFromOneActive(s, seed + count), seed, count + 1); \ No newline at end of file diff --git a/src/pp/pp_poisson.scad b/src/pp/pp_poisson.scad index 5812c8f6..3a350b2c 100644 --- a/src/pp/pp_poisson.scad +++ b/src/pp/pp_poisson.scad @@ -1,8 +1,8 @@ use <_impl/_pp_poisson.scad> -function pp_poisson(size, r, start, k = 30, history = false) = +function pp_poisson(size, r, start, k = 30, seed = undef, history = false) = let( - s = _pp_poisson(sampling(size, r, start, k)), + s = _pp_poisson(sampling(size, r, start, k), is_undef(seed) ? floor(rands(0, 1000, 1)[0]) : seed), samples = [ for(row = sampling_grid(s), sample = row) if(!is_undef(sample)) sample @@ -15,7 +15,7 @@ use use -pts_history = pp_poisson([100, 100], 5, history = true); +pts_history = pp_poisson([100, 100], 5, seed = 1, history = true); for(p = pts_history[0]) { translate(p) circle(1);