1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-12 09:44:16 +02:00

use len(not_collapsed_coords) == 0 to stop

This commit is contained in:
Justin Lin
2022-03-21 09:06:14 +08:00
parent cb0a4bed9f
commit 5b85e635d2
2 changed files with 15 additions and 25 deletions

View File

@@ -1,6 +1,5 @@
use <util/rand.scad>; use <util/rand.scad>;
use <util/some.scad>; use <util/some.scad>;
use <util/every.scad>;
use <util/sum.scad>; use <util/sum.scad>;
use <util/map/hashmap.scad>; use <util/map/hashmap.scad>;
use <util/map/hashmap_put.scad>; use <util/map/hashmap_put.scad>;
@@ -33,7 +32,6 @@ function _weights_of_tiles(weights, symbols, leng, i = 0) =
- wf_weights(wf) - wf_weights(wf)
- wf_eigenstates(wf) - wf_eigenstates(wf)
- wf_eigenstates_at(wf, x, y) - wf_eigenstates_at(wf, x, y)
- wf_is_all_collapsed(wf, notCollaspedCoords)
- wf_collapse(wf, x, y) - wf_collapse(wf, x, y)
- wf_entropy(wf, x, y) - wf_entropy(wf, x, y)
- wf_coord_min_entropy(wf, notCollaspedCoords) - wf_coord_min_entropy(wf, notCollaspedCoords)
@@ -55,14 +53,6 @@ function wf_weights(wf) = wf[2];
function wf_eigenstates(wf) = wf[3]; function wf_eigenstates(wf) = wf[3];
function wf_eigenstates_at(wf, x, y) = wf_eigenstates(wf)[y][x]; function wf_eigenstates_at(wf, x, y) = wf_eigenstates(wf)[y][x];
function wf_is_all_collapsed(wf, notCollaspedCoords) =
is_undef(notCollaspedCoords) ?
every(
wf_eigenstates(wf),
function(row) every(row, function(states) len(states) == 1)
) :
every(notCollaspedCoords, function(coord) len(wf_eigenstates_at(wf, coord.x, coord.y)) == 1);
function wf_collapse(wf, x, y) = function wf_collapse(wf, x, y) =
let( let(
weights = wf_weights(wf), weights = wf_weights(wf),
@@ -125,12 +115,11 @@ function wf_not_collapsed_coords(wf, notCollaspedCoords) =
function wf_coord_min_entropy(wf, notCollaspedCoords) = function wf_coord_min_entropy(wf, notCollaspedCoords) =
let( let(
coords = wf_not_collapsed_coords(wf, notCollaspedCoords), entropyCoord = notCollaspedCoords[0],
entropyCoord = coords[0],
entropy = wf_entropy(wf, entropyCoord.x, entropyCoord.y) - (rand() / 1000), entropy = wf_entropy(wf, entropyCoord.x, entropyCoord.y) - (rand() / 1000),
min_coord = _wf_coord_min_entropy(wf, coords, len(coords), entropy, entropyCoord) min_coord = _wf_coord_min_entropy(wf, notCollaspedCoords, len(notCollaspedCoords), entropy, entropyCoord)
) )
[min_coord, coords]; min_coord;
function _wf_coord_min_entropy(wf, coords, coords_leng, entropy, entropyCoord, i = 1) = function _wf_coord_min_entropy(wf, coords, coords_leng, entropy, entropyCoord, i = 1) =
i == coords_leng ? entropyCoord : i == coords_leng ? entropyCoord :
@@ -220,18 +209,19 @@ function _doDirs(tm, stack, cx, cy, current_tiles, dirs, leng, i = 0) =
function tilemap_generate(tm, notCollaspedCoords) = function tilemap_generate(tm, notCollaspedCoords) =
let(wf = tilemap_wf(tm)) let(wf = tilemap_wf(tm))
wf_is_all_collapsed(wf, notCollaspedCoords) ? collapsed_tiles(wf) : len(notCollaspedCoords) == 0 ? collapsed_tiles(wf) :
let( let(
coord_notCollaspedCoords = wf_coord_min_entropy(wf, notCollaspedCoords), min_coord = wf_coord_min_entropy(wf, notCollaspedCoords),
x = coord_notCollaspedCoords[0].x, x = min_coord.x,
y = coord_notCollaspedCoords[0].y y = min_coord.y,
) ntm = tilemap_propagate([
tilemap_generate(tilemap_propagate([
tilemap_width(tm), tilemap_width(tm),
tilemap_height(tm), tilemap_height(tm),
tilemap_compatibilities(tm), tilemap_compatibilities(tm),
wf_collapse(wf, x, y) wf_collapse(wf, x, y)
], x, y), coord_notCollaspedCoords[1]); ], x, y)
)
tilemap_generate(ntm, wf_not_collapsed_coords(tilemap_wf(ntm)));
function neighbor_dirs(x, y, width, height) = [ function neighbor_dirs(x, y, width, height) = [

View File

@@ -5,7 +5,6 @@ use <../util/rand.scad>;
function tile_wfc(size, sample) = function tile_wfc(size, sample) =
let( let(
tm = tilemap(size[0], size[1], sample), tm = tilemap(size[0], size[1], sample),
wf = tilemap_wf(tm),
// random start // random start
x = floor(rand(size[0] * 0.25, size[0] * 0.75)), x = floor(rand(size[0] * 0.25, size[0] * 0.75)),
y = floor(rand(size[1] * 0.25, size[1] * 0.75)), y = floor(rand(size[1] * 0.25, size[1] * 0.75)),
@@ -13,10 +12,11 @@ function tile_wfc(size, sample) =
tilemap_width(tm), tilemap_width(tm),
tilemap_height(tm), tilemap_height(tm),
tilemap_compatibilities(tm), tilemap_compatibilities(tm),
wf_collapse(wf, x, y) wf_collapse(tilemap_wf(tm), x, y)
], x, y) ], x, y),
notCollapsedCoords = wf_not_collapsed_coords(tilemap_wf(first_collasped_propagated))
) )
tilemap_generate(first_collasped_propagated); tilemap_generate(first_collasped_propagated, notCollapsedCoords);
/* /*