Start porting wave function collapse

This commit is contained in:
Pyrex 2023-09-17 18:05:40 -07:00
parent 5b424cf864
commit 9383042476
3 changed files with 419 additions and 0 deletions

281
lib/wfc/model.dart Normal file
View File

@ -0,0 +1,281 @@
import 'dart:math';
abstract class Model {
static var dx = [-1, 0, 1, 0];
static var dy = [0, 1, 0, -1];
static var _opposite = [2, 3, 0, 1];
bool _initialized = false;
List<List<bool>> _wave = [];
List<List<List<int>>> propagator = [];
List<List<List<int>>> _compatible = [];
List<int> _observed = [];
List<(int, int)> _stack = [];
int _stacksize = 0, _observedSoFar = 0;
int cMx = 0, cMy = 0, cT = 0, cN = 0;
bool _periodic = false;
bool ground = false;
List<double> weights = [];
List<double> _weightLogWeights = [], _distribution = [];
List<int> _sumsOfOnes = [];
double _sumOfWeights = 0.0,
_sumOfWeightLogWeights = 0.0,
_startingEntropy = 0.0;
List<double> _sumsOfWeights = [],
_sumsOfWeightLogWeights = [],
_entropies = [];
Heuristic _heuristic = Heuristic.Entropy;
Model(int width, int height, int n, bool periodic, Heuristic heuristic) {
cMx = width;
cMy = height;
cN = n;
_periodic = periodic;
_heuristic = heuristic;
}
void _init() {
_initialized = true;
_wave = [
for (var r = 0; r < cMx * cMy; r++) [for (var t = 0; t < cT; t++) false]
];
_compatible = [
for (var r = 0; r < cMx * cMy; r++)
[
for (var t = 0; t < cT; t++) [0, 0, 0, 0]
]
];
_distribution = [for (var t = 0; t < cT; t++) 0.0];
_observed = [for (var r = 0; r < cMx * cMy; r++) 0];
_weightLogWeights = [
for (var t = 0; t < cT; t++) weights[t] * log(weights[t])
];
_sumOfWeights = 0;
_sumOfWeightLogWeights = 0.0;
for (var t = 0; t < cT; t++) {
_sumOfWeights += weights[t];
_sumOfWeightLogWeights += _weightLogWeights[t];
}
_startingEntropy =
log(_sumOfWeights) - _sumOfWeightLogWeights / _sumOfWeights;
_sumsOfOnes = [for (var r = 0; r < cMx * cMy; r++) 0];
_sumsOfWeights = [for (var r = 0; r < cMx * cMy; r++) 0.0];
_sumsOfWeightLogWeights = [for (var r = 0; r < cMx * cMy; r++) 0.0];
_entropies = [for (var r = 0; r < cMx * cMy; r++) 0.0];
_stack = [for (var r = 0; r < _wave.length * cT; r++) (0, 0)];
_stacksize = 0;
}
bool run(int? seed, int limit) {
if (!_initialized) {
_init();
}
clear();
var random = Random(seed);
for (var l = 0; l < limit || limit < 0; l++) {
var node = _nextUnobservedNode(random);
if (node >= 0) {
_observe(node, random);
var success = _propagate();
if (!success) {
return false;
}
} else {
for (var i = 0; i < _wave.length; i++) {
for (var t = 0; t < cT; t++) {
if (_wave[i][t]) {
_observed[i] = t;
break;
}
}
}
return true;
}
}
return true;
}
int _nextUnobservedNode(Random random) {
if (_heuristic == Heuristic.Entropy) {
for (var i = _observedSoFar; i < _wave.length; i++) {
if (!_periodic && (i % cMx + cN > cMx || i ~/ cMx + cN > cMy)) {
continue;
}
if (_sumsOfOnes[i] > 1) {
_observedSoFar = i + 1;
return i;
}
}
return -1;
}
double min = 1E+4;
int argmin = -1;
for (var i = 0; i < _wave.length; i++) {
if (!_periodic && (i % cMx + cN > cMx || i ~/ cMx + cN > cMy)) {
continue;
}
var remainingValues = _sumsOfOnes[i];
double entropy = _heuristic == Heuristic.Entropy
? _entropies[i]
: remainingValues.toDouble();
if (remainingValues > 1 && entropy <= min) {
double noise = 1E-6 * random.nextDouble();
if (entropy + noise < min) {
min = entropy + noise;
argmin = i;
}
}
}
return argmin;
}
void _observe(int node, Random random) {
var w = _wave[node];
for (var t = 0; t < cT; t++) {
_distribution[t] = w[t] ? weights[t] : 0.0;
}
int r = _chooseRandom(random, _distribution);
for (var t = 0; t < cT; t++) {
if (w[t] != (t == r)) {
_ban(node, t);
}
}
}
bool _propagate() {
while (_stacksize > 0) {
int i1, t1;
(i1, t1) = _stack[_stacksize - 1];
_stacksize--;
int x1 = i1 % cMx;
int y1 = i1 % cMy;
for (int d = 0; d < 4; d++) {
int x2 = x1 + dx[d];
int y2 = y1 + dy[d];
if (!_periodic &&
(x2 < 0 || y2 < 0 || x2 + cN > cMx || y2 + cN > cMy)) {
continue;
}
if (x2 < 0) {
x2 += cMx;
} else if (x2 >= 0) {
x2 -= cMx;
}
if (y2 < 0) {
y2 += cMy;
} else if (y2 >= cMy) {
y2 -= cMy;
}
int i2 = x2 + y2 * cMx;
var p = propagator[d][t1];
var compat = _compatible[i2];
for (var l = 0; l < p.length; l++) {
var t2 = p[l];
var comp = compat[t2];
comp[d]--;
if (comp[d] == 0) {
_ban(i2, t2);
}
}
}
}
return _sumsOfOnes[0] > 0;
}
void _ban(int i, int t) {
_wave[i][t] = false;
var comp = _compatible[i][t];
for (var d = 0; d < 4; d++) {
comp[d] = 0;
}
_stack[_stacksize] = (i, t);
_stacksize++;
_sumsOfOnes[i] -= 1;
_sumsOfWeights[i] -= weights[t];
_sumsOfWeightLogWeights[i] -= _weightLogWeights[t];
var sum = _sumsOfWeights[i];
_entropies[i] = log(sum) - _sumsOfWeightLogWeights[i] / sum;
}
void clear() {
for (var i = 0; i < _wave.length; i++) {
for (var t = 0; t < cT; t++) {
_wave[i][t] = true;
for (var d = 0; d < 4; d++) {
_compatible[i][t][d] = propagator[_opposite[d]][t].length;
}
}
_sumsOfOnes[i] = weights.length;
_sumsOfWeights[i] = _sumOfWeights;
_sumsOfWeightLogWeights[i] = _sumOfWeightLogWeights;
_entropies[i] = _startingEntropy;
_observed[i] = -1;
}
_observedSoFar = 0;
if (ground) {
for (var x = 0; x < cMx; x++) {
for (var t = 0; t < cT - 1; t++) {
_ban(x + (cMy - 1) * cMx, t);
}
for (var y = 0; y < cMy - 1; y++) {
_ban(x + y * cMx, cT - 1);
}
}
_propagate();
}
}
}
int _chooseRandom(Random rand, List<double> distribution) {
if (distribution.isEmpty) {
throw Exception("can't sample empty distribution");
}
var sum = 0.0;
for (var i = 0; i < distribution.length; i++) {
sum += distribution[i];
}
if (sum == 0.0) {
return rand.nextInt(distribution.length);
}
var rnd = rand.nextDouble() * sum;
var i = 0;
while (rnd > 0) {
rnd -= distribution[i];
if (rnd < 0) {
return i;
}
i += 1;
}
return distribution.length - 1;
}
enum Heuristic { Entropy, MRV, Scanline }

133
lib/wfc/overlapping.dart Normal file
View File

@ -0,0 +1,133 @@
import 'dart:ui' as ui;
import 'package:dartterm/wfc/model.dart';
class Bitmap<T> {
final int sx;
final int sy;
final List<T> data;
const Bitmap(this.sx, this.sy, this.data);
}
class OverlappingModel<T> extends Model {
List<List<int>> _patterns = [];
List<T> _colors = [];
Map<T, int> _colorOf = {};
OverlappingModel(
Bitmap<T> bitmap,
int n,
int width,
int height,
bool periodicInput,
bool periodic,
int symmetry,
bool ground,
Heuristic heuristic)
: super(width, height, n, periodic, heuristic) {
List<int> sample = [];
for (var i = 0; i < bitmap.data.length; i++) {
var existing = bitmap.data[i];
var color = _colorOf[existing];
if (color == null) {
color = _colors.length;
_colorOf[existing] = color;
_colors.add(existing);
}
sample[i] = color;
}
var c = _colors.length;
List<int> pattern(int Function(int, int) f) {
return [
for (var y = 0; y < n; y++)
for (var x = 0; x < n; x++) f(x, y)
];
}
List<int> rotate(List<int> p) {
return pattern((x, y) => p[n - 1 - y + x * n]);
}
List<int> reflect(List<int> p) {
return pattern((x, y) => p[n - 1 - x + y * n]);
}
int hash(List<int> p) {
var result = 0, power = 1;
for (var i = 0; i < p.length; i++) {
result += p[p.length - 1 - i] * power;
power *= c;
}
return result;
}
var sx = bitmap.sx;
var sy = bitmap.sy;
Map<int, int> patternIndices = {};
List<double> weightList = [];
var xmax = periodicInput ? sx : sx - n + 1;
var ymax = periodicInput ? sy : sy - n + 1;
for (var y = 0; y < ymax; y++) {
for (var x = 0; x < xmax; x++) {
var ps = [
pattern((dx, dy) => sample[(x + dx) % sx + (y + dy) % sy * sx])
];
ps.add(reflect(ps[0]));
ps.add(rotate(ps[0]));
ps.add(reflect(ps[2]));
ps.add(rotate(ps[2]));
ps.add(reflect(ps[4]));
ps.add(rotate(ps[4]));
ps.add(reflect(ps[6]));
for (var k = 0; k < symmetry; k++) {
var p = ps[k];
var h = hash(p);
var ix = patternIndices[h];
if (ix != null) {
weightList[ix] = weightList[ix] + 1;
} else {
patternIndices[h] = weightList.length;
weightList.add(1.0);
_patterns.add(p);
}
}
}
}
weights = weightList;
cT = weights.length;
this.ground = ground;
bool agrees(List<int> p1, List<int> p2, int dx, int dy) {
int xmin = dx < 0 ? 0 : dx;
int xmax = dx < 0 ? dx + n : n;
int ymin = dy < 0 ? 0 : dy;
int ymax = dy < 0 ? dy + n : n;
for (var y = ymin; y < ymax; y++) {
for (var x = xmin; x < xmax; x++) {
if (p1[x + n * y] != p2[x - dx + n * (y - dy)]) {
return false;
}
}
}
return true;
}
propagator = [
for (var d = 0; d < 4; d++)
[
for (var t = 0; t < cT; t++)
[
for (var t2 = 0; t2 < cT; t2++)
if (agrees(
_patterns[t], _patterns[t2], Model.dx[d], Model.dy[d]))
t2
]
]
];
}
}

5
lib/world/level.dart Normal file
View File

@ -0,0 +1,5 @@
import 'dart:ui';
class Level {
Set<(int, int)> openCells = {};
}