Skip to content

Commit

Permalink
custom prng as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
gugray committed Jul 3, 2023
1 parent f9da36a commit e5dcbf5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {Vector} from "./vector.js";
import {Masker} from "./masker.js";
import {LookupGrid} from "./lookup-grid.js";
import {Integrator} from "./integrator.js";
import {shuffle} from "./shuffle.js";

const STATE_INIT = 0;
const STATE_STREAMLINE = 1;
const STATE_PROCESS_QUEUE = 2;
const STATE_DONE = 3;
const STATE_SEED_STREAMLINE = 4;

Expand All @@ -21,6 +21,7 @@ function createStreamlineGenerator({
width,
height,
seed,
rand,
minStartDist = 8,
maxStartDist = 36,
endRatio = 0.4,
Expand All @@ -34,12 +35,9 @@ function createStreamlineGenerator({
if (maxStartDist < 2 || maxStartDist > 254) throw "2 <= maxStartDist <= 254 expected";
if (endRatio < 0.1 || endRatio >= 1) throw "0.1 <= endRatio < 1 expected";

if (!seed) {
seed = new Vector(
Math.random() * width,
Math.random() * height
);
}
if (!rand) rand = Math.random;

if (!seed) seed = new Vector(rand() * width, rand() * height);

const startMask = new Masker({
width: width,
Expand Down Expand Up @@ -98,7 +96,6 @@ function createStreamlineGenerator({

if (state === STATE_INIT) initProcessing();
else if (state === STATE_STREAMLINE) continueStreamline();
else if (state === STATE_PROCESS_QUEUE) processQueue();
else if (state === STATE_SEED_STREAMLINE) seedStreamline();

if (isAsync && window.performance.now() - start > maxMsecPerIteration)
Expand All @@ -119,25 +116,32 @@ function createStreamlineGenerator({
return;
}
addStreamLineToQueue();
state = STATE_PROCESS_QUEUE;
state = STATE_STREAMLINE;
}

function seedStreamline() {
let currentStreamLine = finishedStreamlineIntegrators[0];
let nextSeed = currentStreamLine.getNextValidSeed();
if (nextSeed) {
integrator = new Integrator(nextSeed, startMask, stopMask, icfg);
state = STATE_STREAMLINE;
} else {
while (finishedStreamlineIntegrators.length > 0) {
let currentStreamLine = finishedStreamlineIntegrators[0];
let nextSeed = currentStreamLine.getNextValidSeed();
if (nextSeed) {
integrator = new Integrator(nextSeed, startMask, stopMask, icfg);
state = STATE_STREAMLINE;
return;
}
finishedStreamlineIntegrators.shift();
state = STATE_PROCESS_QUEUE;
continue;
}
}

function processQueue() {
if (finishedStreamlineIntegrators.length == 0)
// Find new random seed
const allFreeCoords = startMask.getFreeCoords();
// None left
if (allFreeCoords.length == 0) {
state = STATE_DONE;
else state = STATE_SEED_STREAMLINE;
return;
}
shuffle(allFreeCoords, rand);
const randomSeed = new Vector(allFreeCoords[0][0], allFreeCoords[0][1]);
integrator = new Integrator(randomSeed, startMask, stopMask, icfg);
state = STATE_STREAMLINE;
}

function continueStreamline() {
Expand Down
14 changes: 14 additions & 0 deletions src/shuffle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function shuffle(array, rndFun) {
if (!rndFun) rndFun = Math.random;
// Fisher-Yates shuffle
// https://stackoverflow.com/a/2450976
let ix = array.length, randIx;
while (ix != 0) {
randIx = Math.floor(rndFun() * ix);
ix--;
[array[ix], array[randIx]] = [array[randIx], array[ix]];
}
return array;
}

export {shuffle}

0 comments on commit e5dcbf5

Please sign in to comment.