Skip to content

Commit

Permalink
+ DLA2DLazy
Browse files Browse the repository at this point in the history
  • Loading branch information
Lampese committed Aug 24, 2023
1 parent 6ed85f6 commit 4b1e1a0
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 73 deletions.
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import * as IFS from './src/ifs.js';
import * as Lineamp from './src/lineamp.js';
import * as Symmetry from './src/hyper.js';
import { Turtle2D, Turtle3D } from './src/turtle.js';
import { DLA2D } from './src/DLA2D.js';
import { DLA2D, DLA2DLazy } from './src/DLA2D.js';
import { DLA3D } from './src/DLA3D.js';

const DLA = { DLA2D, DLA3D };
const DLA = { DLA2D, DLA3D, DLA2DLazy };

export {
Vec3,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pureeval/voxel-geometry",
"version": "1.2.10",
"version": "1.2.20",
"type": "module",
"description": "voxel-geometry",
"devDependencies": {
Expand Down
142 changes: 109 additions & 33 deletions src/DLA2D.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { Vec3, Space, vec3 } from './vector.js';
import { Space, vec3 } from './vector.js';
import { rand } from './utils.js';

class Point {
x: number;
y: number;
Stucked = false;
stucked = false;

constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
private Vary(steplength: number) {

private vary(steplength: number) {
return [this.x + rand() * steplength, this.y + rand() * steplength];
}
Walk(width: number, steplength: number) {
let [tox, toy] = this.Vary(steplength);

walk(width: number, steplength: number) {
let [tox, toy] = this.vary(steplength);
while (Math.abs(tox) > width / 2 || Math.abs(toy) > width / 2) {
[tox, toy] = this.Vary(steplength);
[tox, toy] = this.vary(steplength);
}
[this.x, this.y] = [tox, toy];
}
Expand All @@ -26,57 +29,117 @@ class DLASystem {
maxWalk: number;
iterations: number;
step: number;
Temperature: number;
Walkering: Point[];
Stucked: Point[];
temperature: number;
walkering: Point[];
stucked: Point[];
summoner: (width: number) => number[];

constructor(
width: number,
maxWalk: number,
iterations: number,
step: number,
Temperature: number,
temperature: number,
stuck: Space = [],
summoner: (width: number) => number[] = randPoint
) {
this.width = width;
this.maxWalk = maxWalk;
this.iterations = iterations;
this.Temperature = Temperature;
this.Walkering = [];
this.Stucked = [];
this.temperature = temperature;
this.walkering = [];
this.stucked = [];
this.step = step;
this.summoner = summoner;
if (stuck.length === 0) this.Stucked.push(new Point(0, 0));
else this.Stucked = stuck.map((v) => new Point(v.x, v.z));
while (this.Walkering.length < maxWalk) {
this.Walkering.push(toPoint(randPoint(this.width)));
if (stuck.length === 0) this.stucked.push(new Point(0, 0));
else this.stucked = stuck.map((v) => new Point(v.x, v.z));
while (this.walkering.length < maxWalk) {
this.walkering.push(toPoint(randPoint(this.width)));
}
}

run(): Space {
while (this.Walkering.length) {
while (this.walkering.length) {
for (let i = 1; i <= this.iterations; ++i) {
for (let j = 0; j < this.walkering.length; ++j) {
if (this.walkering[j].stucked === true) continue;
this.walkering[j].walk(this.width, 1);
for (let k = 0; k < this.stucked.length; ++k) {
if (checkStuck(this.walkering[j], this.stucked[k], this.step)) {
this.walkering[j].stucked = true;
this.stucked.push(this.walkering[j]);
break;
}
}
}
this.walkering = this.walkering.filter((v) => v.stucked === false);
}
while (this.walkering.length < this.maxWalk && this.temperature > 1) {
this.walkering.push(toPoint(randPoint(this.width)));
this.temperature *= 0.995;
}
}
return this.stucked.map((v) => vec3(v.x, 0, v.y));
}
}

class DLASystemLazy {
width: number;
maxWalk: number;
iterations: number;
step: number;
temperature: number;
walkering: Point[];
stucked: Point[];
summoner: (width: number) => number[];

constructor(
width: number,
maxWalk: number,
iterations: number,
step: number,
temperature: number,
stuck: Space = [],
summoner: (width: number) => number[] = randPoint
) {
this.width = width;
this.maxWalk = maxWalk;
this.iterations = iterations;
this.temperature = temperature;
this.walkering = [];
this.stucked = [];
this.step = step;
this.summoner = summoner;
if (stuck.length === 0) this.stucked.push(new Point(0, 0));
else this.stucked = stuck.map((v) => new Point(v.x, v.z));
while (this.walkering.length < maxWalk) {
this.walkering.push(toPoint(randPoint(this.width)));
}
}

*run() {
while (this.walkering.length) {
for (let i = 1; i <= this.iterations; ++i) {
for (let j = 0; j < this.Walkering.length; ++j) {
if (this.Walkering[j].Stucked === true) continue;
this.Walkering[j].Walk(this.width, 1);
for (let k = 0; k < this.Stucked.length; ++k) {
if (checkStuck(this.Walkering[j], this.Stucked[k], this.step)) {
this.Walkering[j].Stucked = true;
this.Stucked.push(this.Walkering[j]);
for (let j = 0; j < this.walkering.length; ++j) {
yield [this.walkering.length, this.stucked.length];
if (this.walkering[j].stucked === true) continue;
this.walkering[j].walk(this.width, 1);
for (let k = 0; k < this.stucked.length; ++k) {
if (checkStuck(this.walkering[j], this.stucked[k], this.step)) {
this.walkering[j].stucked = true;
this.stucked.push(this.walkering[j]);
break;
}
}
}
this.Walkering = this.Walkering.filter((v) => v.Stucked === false);
this.walkering = this.walkering.filter((v) => v.stucked === false);
}
while (this.Walkering.length < this.maxWalk && this.Temperature > 1) {
this.Walkering.push(toPoint(randPoint(this.width)));
this.Temperature *= 0.995;
while (this.walkering.length < this.maxWalk && this.temperature > 1) {
this.walkering.push(toPoint(randPoint(this.width)));
this.temperature *= 0.995;
}
}
return this.Stucked.map((v) => vec3(v.x, 0, v.y));
yield this.stucked.map((v) => vec3(v.x, 0, v.y));
}
}

Expand All @@ -92,8 +155,8 @@ function checkStuck(a: Point, b: Point, step: number): boolean {
return distance(a, b) < 1.8 * step;
}

function toPoint(arr: number[]): Point {
return new Point(arr[0], arr[1]);
function toPoint([x, y]: number[]): Point {
return new Point(x, y);
}

function DLA2D(
Expand All @@ -109,4 +172,17 @@ function DLA2D(
return sys.run();
}

export { DLA2D };
function DLA2DLazy(
width: number,
maxWalk: number,
iterations: number,
step: number,
Temperature: number,
stuck: Space = [],
summoner: (width: number) => number[] = randPoint
) {
const sys = new DLASystemLazy(width, maxWalk, iterations, step, Temperature, stuck, summoner);
return sys.run();
}

export { DLA2D, DLA2DLazy };
79 changes: 42 additions & 37 deletions src/DLA3D.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import { Vec3, Space, vec3 } from './vector.js';
import { Space, vec3 } from './vector.js';
import { rand } from './utils.js';

class Point {
x: number;
y: number;
z: number;
Stucked = false;
stucked = false;

constructor(x: number, y: number, z: number) {
this.x = x;
this.y = y;
this.z = z;
}
private Vary(steplength: number) {
return [
this.x + rand() * steplength,
this.y + rand() * steplength,
this.z + rand() * steplength
];

view(): [x: number, y: number, z: number] {
return [this.x, this.y, this.z];
}

private vary(steplength: number) {
return this.view().map((v) => v + rand() * steplength);
}
Walk(width: number, steplength: number) {
let [tox, toy, toz] = this.Vary(steplength);

walk(width: number, steplength: number) {
let [tox, toy, toz] = this.vary(steplength);
while (
Math.abs(tox) > width / 2 ||
Math.abs(toy) > width / 2 ||
Math.abs(toz) > width / 2
) {
[tox, toy, toz] = this.Vary(steplength);
[tox, toy, toz] = this.vary(steplength);
}
[this.x, this.y, this.z] = [tox, toy, toz];
}
Expand All @@ -36,57 +39,59 @@ class DLASystem {
maxWalk: number;
iterations: number;
step: number;
Temperature: number;
Walkering: Point[];
Stucked: Point[];
temperature: number;
walkering: Point[];
stucked: Point[];
summoner: (width: number) => number[];

constructor(
width: number,
maxWalk: number,
iterations: number,
step: number,
Temperature: number,
temperature: number,
stuck: Space = [],
summoner: (width: number) => number[] = randPoint
) {
this.width = width;
this.maxWalk = maxWalk;
this.iterations = iterations;
this.Temperature = Temperature;
this.Walkering = [];
this.Stucked = [];
this.temperature = temperature;
this.walkering = [];
this.stucked = [];
this.step = step;
this.summoner = summoner;
if (stuck.length === 0) this.Stucked.push(new Point(0, 0, 0));
else this.Stucked = stuck.map((v) => new Point(v.x, v.y, v.z));
while (this.Walkering.length < maxWalk) {
this.Walkering.push(toPoint(this.summoner(this.width)));

if (stuck.length === 0) this.stucked.push(new Point(0, 0, 0));
else this.stucked = stuck.map((v) => new Point(v.x, v.y, v.z));

while (this.walkering.length < maxWalk) {
this.walkering.push(toPoint(this.summoner(this.width)));
}
}

run(): Space {
while (this.Walkering.length) {
while (this.walkering.length) {
for (let i = 1; i <= this.iterations; ++i) {
for (let j = 0; j < this.Walkering.length; ++j) {
if (this.Walkering[j].Stucked === true) continue;
this.Walkering[j].Walk(this.width, 1);
for (let k = 0; k < this.Stucked.length; ++k) {
if (checkStuck(this.Walkering[j], this.Stucked[k], this.step)) {
this.Walkering[j].Stucked = true;
this.Stucked.push(this.Walkering[j]);
for (let j = 0; j < this.walkering.length; ++j) {
if (this.walkering[j].stucked === true) continue;
this.walkering[j].walk(this.width, 1);
for (let k = 0; k < this.stucked.length; ++k) {
if (checkStuck(this.walkering[j], this.stucked[k], this.step)) {
this.walkering[j].stucked = true;
this.stucked.push(this.walkering[j]);
break;
}
}
}
this.Walkering = this.Walkering.filter((v) => v.Stucked === false);
this.walkering = this.walkering.filter((v) => v.stucked === false);
}
while (this.Walkering.length < this.maxWalk && this.Temperature > 1) {
this.Walkering.push(toPoint(this.summoner(this.width)));
this.Temperature *= 0.995;
while (this.walkering.length < this.maxWalk && this.temperature > 1) {
this.walkering.push(toPoint(this.summoner(this.width)));
this.temperature *= 0.995;
}
}
return this.Stucked.map((v) => vec3(v.x, v.y, v.z));
return this.stucked.map((v) => vec3(v.x, v.y, v.z));
}
}

Expand All @@ -102,8 +107,8 @@ function checkStuck(a: Point, b: Point, step: number): boolean {
return distance(a, b) < 1.8 * step;
}

function toPoint(arr: number[]): Point {
return new Point(arr[0], arr[1], arr[2]);
function toPoint([x, y, z]: number[]): Point {
return new Point(x, y, z);
}

function DLA3D(
Expand Down

0 comments on commit 4b1e1a0

Please sign in to comment.