@margoengine/wfc 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,147 @@
1
+ import { MargoError as e } from "@margoengine/math/errors";
2
+ //#region src/overlapping.ts
3
+ var t = [
4
+ -1,
5
+ 0,
6
+ 1,
7
+ 0
8
+ ], n = [
9
+ 0,
10
+ 1,
11
+ 0,
12
+ -1
13
+ ], r = class {
14
+ constructor(e, { N: r = 3, periodicInput: i = !0, symmetry: a = 8 } = {}) {
15
+ this.stats = {
16
+ backtracks: 0,
17
+ attempts: 0
18
+ };
19
+ let { data: o, width: s, height: c } = e;
20
+ if (!Number.isInteger(r) || r < 2 || r > 5 || !Number.isInteger(a) || a < 1 || a > 8 || !Number.isInteger(s) || !Number.isInteger(c) || s < r || c < r || s * c > 1 << 20 || o.length !== s * c) throw RangeError("Invalid WFC sample or pattern options; use integer width, height and N, and a data array of width times height values");
21
+ let l = /* @__PURE__ */ new Map(), u = Uint16Array.from(o, (e) => {
22
+ if (!Number.isFinite(e)) throw RangeError("Sample values must be finite; replace NaN and Infinity in the sample");
23
+ let t = l.get(e);
24
+ if (t === void 0) {
25
+ if (t = l.size, t > 65535) throw RangeError("Too many sample colors; use at most 65,536 distinct values");
26
+ l.set(e, t);
27
+ }
28
+ return t;
29
+ });
30
+ this.N = r, this.colors = [...l.keys()];
31
+ let d = (e) => Uint16Array.from({ length: r * r }, (t, n) => e(n % r, Math.floor(n / r))), f = /* @__PURE__ */ new Map(), p = [], m = [];
32
+ for (let e = 0; e < (i ? c : c - r + 1); e++) for (let t = 0; t < (i ? s : s - r + 1); t++) {
33
+ let n = [d((n, r) => u[(t + n) % s + (e + r) % c * s])];
34
+ for (let e = 1; e < a; e++) {
35
+ let t = n[e & 1 ? e - 1 : e - 2];
36
+ n.push(e & 1 ? d((e, n) => t[r - 1 - e + n * r]) : d((e, n) => t[r - 1 - n + e * r]));
37
+ }
38
+ for (let e of n) {
39
+ let t = e.join(), n = f.get(t);
40
+ if (n === void 0) {
41
+ if (p.length === 4096) throw RangeError("Sample has more than 4096 patterns; use a smaller N or sample");
42
+ f.set(t, p.length), p.push(e), m.push(1);
43
+ } else m[n]++;
44
+ }
45
+ }
46
+ this.pixels = p, this.patterns = p.length, this.weights = Float64Array.from(m), this.weightLogs = this.weights.map((e) => e * Math.log(e));
47
+ let h = (e, t, n, i) => {
48
+ for (let a = Math.max(0, i); a < Math.min(r, r + i); a++) for (let o = Math.max(0, n); o < Math.min(r, r + n); o++) if (e[o + r * a] !== t[o - n + r * (a - i)]) return !1;
49
+ return !0;
50
+ };
51
+ this.propagator = t.map((e, t) => p.map((r) => Int32Array.from(p.flatMap((i, a) => h(r, i, e, n[t]) ? [a] : []))));
52
+ }
53
+ solve(r, i, { seed: a = 1, periodic: o = !1, backtracks: s = 1e3, attempts: c = 4, pins: l = [], ground: u, signal: d } = {}) {
54
+ let f = this.N, p = this.patterns, m = o ? r : r - f + 1, h = o ? i : i - f + 1, g = m * h;
55
+ if (!Number.isInteger(r) || !Number.isInteger(i) || m < 1 || h < 1 || g * p > 1 << 22 || !Number.isSafeInteger(a) || !Number.isInteger(s) || s < 0 || s > 1e5 || !Number.isInteger(c) || c < 1 || c > 64) throw RangeError("Invalid WFC output size or solve options; use an integer output size and attempts from 1 to 64");
56
+ for (let e of l) if (e.position.length !== 2 || Array.from(e.position).some((e, t) => !Number.isInteger(e) || e < 0 || e >= [r, i][t]) || !Number.isFinite(e.value)) throw RangeError("Invalid overlapping pin; use an output pixel inside the image and a finite sample value");
57
+ if (u && (!Number.isFinite(u.value) || !Number.isInteger(u.rows ?? 1) || (u.rows ?? 1) < 1 || (u.rows ?? 1) > i)) throw RangeError("Invalid ground rows; use a finite sample value and an integer row count from 1 to output height");
58
+ d?.throwIfAborted();
59
+ let { weights: _, weightLogs: v, propagator: y } = this, b = new Uint8Array(g * p), x = new Int32Array(g * p * 4), S = new Uint8Array(g * p), C = new Int32Array(g), w = new Float64Array(g), T = new Float64Array(g), E = [], D = [], O = [], k = a >>> 0 || 1, A = !1, j = () => (k ^= k << 13, k ^= k >>> 17, k ^= k << 5, (k >>> 0) / 4294967296), M = (e, r) => {
60
+ let i = e % m + t[r], a = Math.floor(e / m) + n[r];
61
+ if (o) i = (i + m) % m, a = (a + h) % h;
62
+ else if (i < 0 || a < 0 || i >= m || a >= h) return -1;
63
+ return i + a * m;
64
+ }, N = (e, t) => {
65
+ let n = e * p + t;
66
+ b[n] = 0, E.push(n), D.push(n), w[e] -= _[t], T[e] -= v[t], --C[e] === 0 && (A = !0);
67
+ }, P = () => {
68
+ for (d?.throwIfAborted(); D.length && !A;) {
69
+ let e = D.pop(), t = Math.floor(e / p), n = e % p;
70
+ for (let e = 0; e < 4; e++) {
71
+ let r = M(t, e);
72
+ if (!(r < 0)) for (let t of y[e][n]) --x[(r * p + t) * 4 + e] === 0 && b[r * p + t] && N(r, t);
73
+ }
74
+ S[e] = 1;
75
+ }
76
+ return !A;
77
+ }, F = (e) => {
78
+ for (; E.length > e;) {
79
+ let e = E.pop(), t = Math.floor(e / p), n = e % p;
80
+ if (S[e]) {
81
+ for (let e = 0; e < 4; e++) {
82
+ let r = M(t, e);
83
+ if (r >= 0) for (let t of y[e][n]) x[(r * p + t) * 4 + e]++;
84
+ }
85
+ S[e] = 0;
86
+ }
87
+ b[e] = 1, C[t]++, w[t] += _[n], T[t] += v[n];
88
+ }
89
+ D.length = 0, A = !1;
90
+ };
91
+ this.stats.backtracks = 0;
92
+ for (let e = 1; e <= c; e++) {
93
+ this.stats.attempts = e, b.fill(1), S.fill(0), E.length = D.length = O.length = 0, A = !1;
94
+ let t = _.reduce((e, t) => e + t, 0), n = v.reduce((e, t) => e + t, 0);
95
+ C.fill(p), w.fill(t), T.fill(n);
96
+ for (let e = 0; e < g; e++) for (let t = 0; t < p; t++) for (let n = 0; n < 4; n++) x[(e * p + t) * 4 + n] = y[(n + 2) % 4][t].length;
97
+ let a = (e, t, n) => {
98
+ let r = this.colors.indexOf(n);
99
+ for (let n = 0; n < f; n++) for (let i = 0; i < f; i++) {
100
+ let a = o ? ((e - i) % m + m) % m : e - i, s = o ? ((t - n) % h + h) % h : t - n;
101
+ if (a < 0 || a >= m || s < 0 || s >= h) continue;
102
+ let c = a + s * m;
103
+ for (let e = 0; e < p; e++) b[c * p + e] && this.pixels[e][i + n * f] !== r && N(c, e);
104
+ }
105
+ };
106
+ for (let e of l) a(e.position[0], e.position[1], e.value);
107
+ if (u) for (let e = i - (u.rows ?? 1); e < i; e++) for (let t = 0; t < r; t++) a(t, e, u.value);
108
+ for (let e = 0; e < g; e++) for (let t = 0; t < p; t++) for (let n = 0; n < 4; n++) !y[(n + 2) % 4][t].length && M(e, (n + 2) % 4) >= 0 && b[e * p + t] && N(e, t);
109
+ let c = P(), d = 0;
110
+ for (;;) {
111
+ if (!c) {
112
+ let e = O.pop();
113
+ if (!e || d++ >= s) break;
114
+ this.stats.backtracks++, F(e.mark), N(e.cell, e.pattern), c = P();
115
+ continue;
116
+ }
117
+ let e = -1, t = Infinity;
118
+ for (let n = 0; n < g; n++) if (C[n] > 1) {
119
+ let r = Math.log(w[n]) - T[n] / w[n] + 1e-6 * j();
120
+ r < t && (t = r, e = n);
121
+ }
122
+ if (e < 0) return this.render(b, r, i, m, o);
123
+ let n = j() * w[e], a = -1;
124
+ for (let t = 0; t < p && !(b[e * p + t] && (a = t, n -= _[t], n <= 0)); t++);
125
+ O.push({
126
+ mark: E.length,
127
+ cell: e,
128
+ pattern: a
129
+ });
130
+ for (let t = 0; t < p; t++) t !== a && b[e * p + t] && N(e, t);
131
+ c = P();
132
+ }
133
+ }
134
+ throw new e("failed", "WFC contradicted within the backtrack and attempt budget; check pins/ground, raise backtracks or attempts, or try another seed. Help: https://margoengine.com/llms/packages/wfc.md");
135
+ }
136
+ render(e, t, n, r, i) {
137
+ let a = this.N, o = this.patterns, s = new Float64Array(t * n);
138
+ for (let c = 0; c < n; c++) for (let l = 0; l < t; l++) {
139
+ let u = i || l < r ? 0 : l - r + 1, d = i || c < n - a + 1 ? 0 : c - (n - a + 1) + 1, f = l - u + (c - d) * r, p = 0;
140
+ for (; !e[f * o + p];) p++;
141
+ s[l + c * t] = this.colors[this.pixels[p][u + d * a]];
142
+ }
143
+ return s;
144
+ }
145
+ };
146
+ //#endregion
147
+ export { r as OverlappingModel };
@@ -0,0 +1,43 @@
1
+ import { type GraphOptions } from './graph.js';
2
+ /** Voxel sample, X fastest then Y then Z; arbitrary finite numerical labels. */
3
+ export interface VoxelSample {
4
+ data: ArrayLike<number>;
5
+ width: number;
6
+ height: number;
7
+ depth: number;
8
+ }
9
+ /** Pattern extraction defaults: N 2, periodic input, identity orientation and no reflections. */
10
+ export interface Overlapping3DOptions {
11
+ N?: number;
12
+ periodicInput?: boolean;
13
+ symmetry?: 'none' | 'y' | 'cube';
14
+ reflections?: boolean;
15
+ }
16
+ /** Pin an original sample value at an output voxel coordinate. */
17
+ export interface VoxelPin {
18
+ position: readonly [number, number, number];
19
+ value: number;
20
+ }
21
+ /** Bounded search defaults: seed 1, four attempts, 10000 backtracks, nonperiodic output and no pins or signal. */
22
+ export interface VoxelSolveOptions extends GraphOptions {
23
+ periodic?: boolean;
24
+ pins?: readonly VoxelPin[];
25
+ }
26
+ /** Learn cubic sample windows and synthesize volumes whose every NxNxN window belongs to the learned set. */
27
+ export declare class Overlapping3DModel {
28
+ /** Number of distinct learned patterns. */
29
+ readonly patterns: number;
30
+ /** Counts from the latest bounded search. */
31
+ readonly stats: {
32
+ attempts: number;
33
+ backtracks: number;
34
+ decisions: number;
35
+ };
36
+ private readonly pixels;
37
+ private readonly solver;
38
+ private readonly N;
39
+ /** At most 1048576 sample voxels and 4096 distinct patterns; N is 2..5. No source data retained. */
40
+ constructor(sample: VoxelSample, { N, periodicInput, symmetry, reflections }?: Overlapping3DOptions);
41
+ /** Return original sample labels, X fastest. Nonperiodic outputs must be at least N on each axis. */
42
+ solve(width: number, height: number, depth: number, { periodic, pins, ...options }?: VoxelSolveOptions): Float64Array;
43
+ }
@@ -0,0 +1,186 @@
1
+ import { n as e, r as t, t as n } from "./graph-BI3KRQ6B.js";
2
+ //#region src/overlapping3d.ts
3
+ var r = [
4
+ [
5
+ 1,
6
+ 0,
7
+ 0
8
+ ],
9
+ [
10
+ -1,
11
+ 0,
12
+ 0
13
+ ],
14
+ [
15
+ 0,
16
+ 1,
17
+ 0
18
+ ],
19
+ [
20
+ 0,
21
+ -1,
22
+ 0
23
+ ],
24
+ [
25
+ 0,
26
+ 0,
27
+ 1
28
+ ],
29
+ [
30
+ 0,
31
+ 0,
32
+ -1
33
+ ]
34
+ ], i = class {
35
+ constructor(t, { N: i = 2, periodicInput: a = !0, symmetry: o = "none", reflections: s = !1 } = {}) {
36
+ this.pixels = [];
37
+ let c = e([
38
+ t.width,
39
+ t.height,
40
+ t.depth
41
+ ], 1048576);
42
+ if (!Number.isInteger(i) || i < 2 || i > 5 || c.some((e) => e < i) || t.data.length !== c[0] * c[1] * c[2] || ![
43
+ "none",
44
+ "y",
45
+ "cube"
46
+ ].includes(o) || typeof s != "boolean" || typeof a != "boolean") throw RangeError("Invalid voxel sample; provide N 2..5, dimensions at least N and matching finite data");
47
+ let l = Array.from(t.data);
48
+ if (l.some((e) => !Number.isFinite(e))) throw RangeError("Invalid voxel value; replace NaN and Infinity with finite labels");
49
+ this.N = i;
50
+ let u = [];
51
+ for (let e of [
52
+ [
53
+ 0,
54
+ 1,
55
+ 2
56
+ ],
57
+ [
58
+ 0,
59
+ 2,
60
+ 1
61
+ ],
62
+ [
63
+ 1,
64
+ 0,
65
+ 2
66
+ ],
67
+ [
68
+ 1,
69
+ 2,
70
+ 0
71
+ ],
72
+ [
73
+ 2,
74
+ 0,
75
+ 1
76
+ ],
77
+ [
78
+ 2,
79
+ 1,
80
+ 0
81
+ ]
82
+ ]) for (let t = 0; t < 8; t++) {
83
+ let n = Array(9).fill(0);
84
+ for (let r = 0; r < 3; r++) n[r * 3 + e[r]] = t & 1 << r ? -1 : 1;
85
+ n[0] * (n[4] * n[8] - n[5] * n[7]) - n[3] * (n[1] * n[8] - n[2] * n[7]) + n[6] * (n[1] * n[5] - n[2] * n[4]) < 0 && !s || o === "none" && e.some((e, t) => e !== t) || (o !== "none" || t === 0 || s && t === 1) && (o !== "y" || n[4] === 1 && n[1] === 0 && n[7] === 0) && u.push(n);
86
+ }
87
+ let d = /* @__PURE__ */ new Map(), f = [], p = c.map((e) => a ? e : e - i + 1), m = (i - 1) / 2;
88
+ for (let e = 0; e < p[2]; e++) for (let t = 0; t < p[1]; t++) for (let n = 0; n < p[0]; n++) for (let r of u) {
89
+ let a = [];
90
+ for (let o = 0; o < i; o++) for (let s = 0; s < i; s++) for (let u = 0; u < i; u++) {
91
+ let i = [
92
+ u - m,
93
+ s - m,
94
+ o - m
95
+ ], d = [
96
+ n,
97
+ t,
98
+ e
99
+ ].map((e, t) => (e + m + i[0] * r[t] + i[1] * r[3 + t] + i[2] * r[6 + t]) % c[t]);
100
+ a.push(l[d[0] + c[0] * (d[1] + c[1] * d[2])]);
101
+ }
102
+ let o = a.join(), s = d.get(o);
103
+ if (s === void 0) {
104
+ if (this.pixels.length === 4096) throw RangeError("Too many voxel patterns; reduce sample complexity, N or symmetry below 4097 patterns");
105
+ d.set(o, this.pixels.length), this.pixels.push(a), f.push(1);
106
+ } else f[s]++;
107
+ }
108
+ this.patterns = this.pixels.length;
109
+ let h = this.pixels.map((e) => r.map((t, n) => {
110
+ let r = Math.floor(n / 2), a = n % 2 == 0, o = [];
111
+ for (let t = 0; t < i; t++) for (let n = 0; n < i; n++) for (let s = 0; s < i; s++) {
112
+ let c = [
113
+ s,
114
+ n,
115
+ t
116
+ ][r];
117
+ (a ? c > 0 : c < i - 1) && o.push(e[s + i * (n + i * t)]);
118
+ }
119
+ return o.join();
120
+ })), g = r.map((e, t) => {
121
+ let n = /* @__PURE__ */ new Map();
122
+ return h.forEach((e, r) => {
123
+ let i = n.get(e[t]) ?? [];
124
+ i.push(r), n.set(e[t], i);
125
+ }), n;
126
+ });
127
+ this.solver = new n(this.pixels.map((e, t) => ({
128
+ weight: f[t],
129
+ neighbors: r.map((e, n) => g[n ^ 1].get(h[t][n]) ?? [])
130
+ })), 6), this.stats = this.solver.stats;
131
+ }
132
+ solve(n, i, a, { periodic: o = !1, pins: s = [], ...c } = {}) {
133
+ c.signal?.throwIfAborted();
134
+ let l = e([
135
+ n,
136
+ i,
137
+ a
138
+ ]), u = this.N;
139
+ if (typeof o != "boolean" || !o && l.some((e) => e < u)) throw RangeError("Invalid voxel output; nonperiodic dimensions must be at least N");
140
+ let d = l.map((e) => o ? e : e - u + 1), f = d[0] * d[1] * d[2], p = /* @__PURE__ */ new Map();
141
+ if (f * this.patterns > 16777216) throw RangeError("Voxel output is too large; reduce dimensions or sample complexity below 16777217 cell/pattern pairs");
142
+ let m = (e, t) => (e % t + t) % t;
143
+ for (let e of Array.from(s)) {
144
+ if (!e || !Number.isFinite(e.value)) throw RangeError("Invalid voxel pin; provide a finite sample value and output position");
145
+ t(e.position, l);
146
+ for (let t = 0; t < u; t++) for (let n = 0; n < u; n++) for (let r = 0; r < u; r++) {
147
+ let i = e.position.map((e, i) => e - [
148
+ r,
149
+ n,
150
+ t
151
+ ][i]);
152
+ if (!o && i.some((e, t) => e < 0 || e >= d[t])) continue;
153
+ let a = i.map((e, t) => m(e, d[t])), s = r + u * (n + u * t), c = [], l = a[0] + d[0] * (a[1] + d[1] * a[2]), f = p.get(l);
154
+ if (f) for (let t of f) this.pixels[t][s] === e.value && c.push(t);
155
+ else this.pixels.forEach((t, n) => {
156
+ t[s] === e.value && c.push(n);
157
+ });
158
+ p.set(l, c);
159
+ }
160
+ }
161
+ let h = this.solver.solve(f, (e, t) => {
162
+ let n = [
163
+ e % d[0],
164
+ Math.floor(e / d[0]) % d[1],
165
+ Math.floor(e / (d[0] * d[1]))
166
+ ].map((e, n) => e + r[t][n]);
167
+ if (!o && n.some((e, t) => e < 0 || e >= d[t])) return -1;
168
+ let i = n.map((e, t) => m(e, d[t]));
169
+ return i[0] + d[0] * (i[1] + d[1] * i[2]);
170
+ }, [...p].map(([e, t]) => ({
171
+ cell: e,
172
+ tiles: t
173
+ })), c), g = new Float64Array(n * i * a);
174
+ for (let e = 0; e < a; e++) for (let t = 0; t < i; t++) for (let r = 0; r < n; r++) {
175
+ let a = [
176
+ r,
177
+ t,
178
+ e
179
+ ], s = a.map((e, t) => o ? e : Math.min(e, d[t] - 1)), c = a.map((e, t) => e - s[t]);
180
+ g[r + n * (t + i * e)] = this.pixels[h[s[0] + d[0] * (s[1] + d[1] * s[2])]][c[0] + u * (c[1] + u * c[2])];
181
+ }
182
+ return g;
183
+ }
184
+ };
185
+ //#endregion
186
+ export { i as Overlapping3DModel };
@@ -0,0 +1,21 @@
1
+ import type { Tile } from './index.js';
2
+ /** One authored tile. Socket labels match by exact equality on opposite faces. */
3
+ export interface SocketTile {
4
+ /** +X, -X, +Y, -Y, +Z, -Z labels; each label describes an orientation-independent connector. */
5
+ sockets: readonly string[];
6
+ /** Total relative frequency across this source's variants, default 1. */
7
+ weight?: number;
8
+ /** Rotations around +Y (default), all 24 proper cube rotations, or identity only. */
9
+ rotations?: 'y' | 'cube' | 'none';
10
+ }
11
+ /** Expanded solver tile and the transform needed to render its authored geometry. */
12
+ export interface SocketVariant extends Tile {
13
+ /** Index into the authored source array. */
14
+ source: number;
15
+ /** Column-major 3x3 rotation, mapping source vectors to world vectors. No reflections. */
16
+ rotation: readonly number[];
17
+ /** Rotated labels, in solver direction order. */
18
+ sockets: readonly string[];
19
+ }
20
+ /** Build reciprocal adjacency and stable Uint16-compatible IDs for ConstrainedWFC; at most 4096 variants. */
21
+ export declare function buildSocketTiles(sources: readonly SocketTile[]): SocketVariant[];
@@ -0,0 +1,92 @@
1
+ //#region src/sockets.ts
2
+ var e = [
3
+ [
4
+ 1,
5
+ 0,
6
+ 0
7
+ ],
8
+ [
9
+ -1,
10
+ 0,
11
+ 0
12
+ ],
13
+ [
14
+ 0,
15
+ 1,
16
+ 0
17
+ ],
18
+ [
19
+ 0,
20
+ -1,
21
+ 0
22
+ ],
23
+ [
24
+ 0,
25
+ 0,
26
+ 1
27
+ ],
28
+ [
29
+ 0,
30
+ 0,
31
+ -1
32
+ ]
33
+ ], t = (e, t) => [
34
+ e[1] * t[2] - e[2] * t[1],
35
+ e[2] * t[0] - e[0] * t[2],
36
+ e[0] * t[1] - e[1] * t[0]
37
+ ];
38
+ function n(n) {
39
+ let r = [];
40
+ if (!n.length || n.length > 4096) throw RangeError("WFC sockets need 1..4096 source tiles");
41
+ for (let i = 0; i < n.length; i++) {
42
+ let a = n[i];
43
+ if (!a) throw RangeError("Invalid WFC sockets; provide a source tile at every index");
44
+ let o = a.rotations ?? "y", s = a.weight ?? 1;
45
+ if (a.sockets.length !== 6 || Array.from(a.sockets).some((e) => typeof e != "string") || ![
46
+ "y",
47
+ "cube",
48
+ "none"
49
+ ].includes(o) || !Number.isFinite(s) || s <= 0) throw RangeError("Invalid WFC sockets; provide six string labels, positive finite weight and y/cube/none rotations");
50
+ let c = [];
51
+ for (let n of e) for (let r of e) {
52
+ if (n.some((e, t) => e && r[t])) continue;
53
+ let e = t(n, r), i = [
54
+ ...n,
55
+ ...r,
56
+ ...e
57
+ ].map((e) => e || 0);
58
+ (o === "cube" || r[0] === 0 && r[1] === 1 && r[2] === 0) && (o !== "none" || n[0] === 1) && c.push(i);
59
+ }
60
+ if (r.length + c.length > 4096) throw RangeError("WFC sockets support at most 4096 expanded variants");
61
+ let l = s / c.length;
62
+ if (!(l > 0)) throw RangeError("WFC socket weight underflows after rotation splitting; increase the source weight or use fewer rotations");
63
+ for (let t of c) {
64
+ let n = [
65
+ ,
66
+ ,
67
+ ,
68
+ ,
69
+ ,
70
+ ,
71
+ ];
72
+ e.forEach((r, i) => {
73
+ let o = [
74
+ 0,
75
+ 1,
76
+ 2
77
+ ].map((e) => r[0] * t[e] + r[1] * t[3 + e] + r[2] * t[6 + e]), s = e.findIndex((e) => e.every((e, t) => e === o[t]));
78
+ n[s] = a.sockets[i];
79
+ }), r.push({
80
+ source: i,
81
+ rotation: t,
82
+ sockets: n,
83
+ weight: l,
84
+ neighbors: []
85
+ });
86
+ }
87
+ }
88
+ for (let e of r) e.neighbors = e.sockets.map((e, t) => r.flatMap((n, r) => n.sockets[t ^ 1] === e ? [r] : []));
89
+ return r;
90
+ }
91
+ //#endregion
92
+ export { n as buildSocketTiles };
@@ -0,0 +1,31 @@
1
+ import type { Tile } from './index.js';
2
+ /** The six Gumin symmetry classes; F retains all eight dihedral orientations. */
3
+ export type TileSymmetry = 'X' | 'I' | '\\' | 'L' | 'T' | 'F';
4
+ /** One named source tile. Weight defaults to 1 per distinct orientation, matching Gumin tilesets. */
5
+ export interface NamedTile {
6
+ name: string;
7
+ symmetry?: TileSymmetry;
8
+ weight?: number;
9
+ }
10
+ /** A horizontal left/right rule; names may end with an orientation action 0..7. */
11
+ export interface TileNeighbor {
12
+ left: string;
13
+ right: string;
14
+ }
15
+ /** Solver-ready variant with an assembly-compatible source and column-major 3x3 transform. */
16
+ export interface TilesetVariant extends Tile {
17
+ name: string;
18
+ source: number;
19
+ orientation: number;
20
+ rotation: readonly number[];
21
+ }
22
+ /** Authored tiles, rules and optional named tile subsets. */
23
+ export interface TilesetDefinition {
24
+ tiles: readonly NamedTile[];
25
+ neighbors: readonly TileNeighbor[];
26
+ subsets?: Readonly<Record<string, readonly string[]>>;
27
+ }
28
+ /** Expand classes and the full D4 orbit of each neighbor rule; vertical neighbors are unconstrained. Subset defaults to all tiles. */
29
+ export declare function buildTileset(definition: TilesetDefinition, subset?: string): TilesetVariant[];
30
+ /** Parse Gumin set/tiles/neighbors/subsets XML in Node or browsers, without DOM dependencies or external entity loading. */
31
+ export declare function parseTilesetXML(text: string): TilesetDefinition;
@@ -0,0 +1,169 @@
1
+ //#region src/tileset.ts
2
+ var e = () => /* @__PURE__ */ RangeError("Invalid WFC tileset; provide distinct tile names, X/I/\\/L/T/F symmetry and valid named neighbor orientations");
3
+ function t(e) {
4
+ let t = e === "F" ? 8 : e === "L" || e === "T" ? 4 : e === "X" ? 1 : 2, n = (e) => t === 8 ? e < 4 ? (e + 1) % 4 : 4 + (e + 3) % 4 : (e + 1) % t, r = (t) => e === "L" ? t % 2 ? t - 1 : t + 1 : e === "T" ? t % 2 ? 4 - t : t : e === "\\" ? 1 - t : e === "F" ? (t + 4) % 8 : t;
5
+ return {
6
+ n: t,
7
+ action: (e, t) => {
8
+ for (let r = 0; r < t % 4; r++) e = n(e);
9
+ return t < 4 ? e : r(e);
10
+ }
11
+ };
12
+ }
13
+ function n(e) {
14
+ let t = e % 4, n = [
15
+ 1,
16
+ 0,
17
+ -1,
18
+ 0
19
+ ][t], r = [
20
+ 0,
21
+ 1,
22
+ 0,
23
+ -1
24
+ ][t], i = e < 4 ? 1 : -1;
25
+ return [
26
+ i * n,
27
+ 0,
28
+ -r,
29
+ 0,
30
+ 1,
31
+ 0,
32
+ i * r,
33
+ 0,
34
+ n
35
+ ];
36
+ }
37
+ function r(r, i) {
38
+ if (!r.tiles.length || r.tiles.length > 4096) throw e();
39
+ let a = /* @__PURE__ */ new Map(), o = Array.from(r.tiles, (n) => {
40
+ if (!n || typeof n.name != "string" || !n.name.trim() || /\s[0-7]$/.test(n.name) || a.has(n.name) || ![
41
+ "X",
42
+ "I",
43
+ "\\",
44
+ "L",
45
+ "T",
46
+ "F"
47
+ ].includes(n.symmetry ?? "X") || !Number.isFinite(n.weight ?? 1) || (n.weight ?? 1) <= 0) throw e();
48
+ return a.set(n.name, a.size), t(n.symmetry ?? "X");
49
+ }), s = i === void 0 ? r.tiles.map((e) => e.name) : r.subsets?.[i];
50
+ if (!s?.length || Array.from(s).some((e) => !a.has(e)) || new Set(s).size !== s.length) throw e();
51
+ let c = new Set(s), l = [], u = [];
52
+ r.tiles.forEach((t, r) => {
53
+ if (l[r] = u.length, !c.has(t.name)) return;
54
+ let i = o[r];
55
+ if (u.length + i.n > 4096) throw e();
56
+ for (let e = 0; e < i.n; e++) u.push({
57
+ name: t.name,
58
+ source: r,
59
+ orientation: e,
60
+ rotation: n(e),
61
+ weight: t.weight ?? 1,
62
+ neighbors: Array.from({ length: 6 }, () => [])
63
+ });
64
+ });
65
+ let d = (t) => {
66
+ if (typeof t != "string") throw e();
67
+ let n = /^(.*) ([0-7])$/.exec(t), r = n ? n[1] : t, i = a.get(r);
68
+ if (i === void 0) throw e();
69
+ return {
70
+ source: i,
71
+ id: o[i].action(0, n ? Number(n[2]) : 0),
72
+ active: c.has(r)
73
+ };
74
+ }, f = u.map(() => Array.from({ length: 6 }, () => /* @__PURE__ */ new Set()));
75
+ for (let t of Array.from(r.neighbors)) {
76
+ if (!t) throw e();
77
+ let r = d(t.left), i = d(t.right);
78
+ if (r.active && i.active) for (let e = 0; e < 8; e++) {
79
+ let t = l[r.source] + o[r.source].action(r.id, e), a = l[i.source] + o[i.source].action(i.id, e), s = n(e), c = s[0] === 1 ? 0 : s[0] === -1 ? 1 : s[2] === 1 ? 4 : 5;
80
+ f[t][c].add(a), f[a][c ^ 1].add(t);
81
+ }
82
+ }
83
+ return u.forEach((e, t) => {
84
+ e.neighbors = f[t].map((e, t) => t === 2 || t === 3 ? u.map((e, t) => t) : [...e].sort((e, t) => e - t));
85
+ }), u;
86
+ }
87
+ function i(t) {
88
+ if (typeof t != "string" || t.length > 4194304) throw e();
89
+ let n = {
90
+ name: "",
91
+ attrs: {},
92
+ children: []
93
+ }, r = [n], i = 0, a = (t) => t.replace(/&(?:#x[0-9a-fA-F]+|#[0-9]+|amp|lt|gt|quot|apos);|&/g, (t) => {
94
+ let n = {
95
+ "&amp;": "&",
96
+ "&lt;": "<",
97
+ "&gt;": ">",
98
+ "&quot;": "\"",
99
+ "&apos;": "'"
100
+ };
101
+ if (n[t]) return n[t];
102
+ if (t === "&") throw e();
103
+ let r = parseInt(t.slice(t[2] === "x" ? 3 : 2, -1), t[2] === "x" ? 16 : 10);
104
+ if (!(r === 9 || r === 10 || r === 13 || r >= 32 && r <= 1114111 && !(r >= 55296 && r <= 57343) && r !== 65534 && r !== 65535)) throw e();
105
+ return String.fromCodePoint(r);
106
+ });
107
+ for (let n of t.matchAll(/<!--(?:[^-]|-(?!-))*-->|<\?[\s\S]*?\?>|<\/[A-Za-z][\w-]*\s*>|<[A-Za-z][\w-]*(?:\s+[A-Za-z][\w-]*\s*=\s*(?:"[^"<]*"|'[^'<]*'))*\s*\/?>/g)) {
108
+ if (t.slice(i, n.index).trim()) throw e();
109
+ i = n.index + n[0].length;
110
+ let o = n[0];
111
+ if (o.startsWith("<!--") || o.startsWith("<?")) continue;
112
+ let s = /^<\/?([\w-]+)/.exec(o)[1];
113
+ if (o.startsWith("</")) {
114
+ if (r.length === 1 || r.pop().name !== s) throw e();
115
+ continue;
116
+ }
117
+ let c = {
118
+ name: s,
119
+ attrs: Object.create(null),
120
+ children: []
121
+ };
122
+ for (let t of o.matchAll(/([A-Za-z][\w-]*)\s*=\s*(?:"([^"]*)"|'([^']*)')/g)) {
123
+ if (t[1] in c.attrs) throw e();
124
+ c.attrs[t[1]] = a((t[2] ?? t[3]).replace(/[\t\r\n]/g, " "));
125
+ }
126
+ if (r.at(-1).children.push(c), o.endsWith("/>") || r.push(c), r.length > 8) throw e();
127
+ }
128
+ if (t.slice(i).trim() || r.length !== 1 || n.children.length !== 1 || n.children[0].name !== "set") throw e();
129
+ return n.children[0];
130
+ }
131
+ function a(t) {
132
+ let n = i(t), a = new Map(n.children.map((e) => [e.name, e]));
133
+ if (a.size !== n.children.length || n.children.some((e) => ![
134
+ "tiles",
135
+ "neighbors",
136
+ "subsets"
137
+ ].includes(e.name))) throw e();
138
+ let o = a.get("tiles")?.children.map((t) => {
139
+ if (t.name !== "tile" || t.children.length) throw e();
140
+ return {
141
+ name: t.attrs.name,
142
+ symmetry: t.attrs.symmetry,
143
+ weight: t.attrs.weight === void 0 ? void 0 : Number(t.attrs.weight)
144
+ };
145
+ }), s = a.get("neighbors")?.children.map((t) => {
146
+ if (t.name !== "neighbor" || t.children.length) throw e();
147
+ return {
148
+ left: t.attrs.left,
149
+ right: t.attrs.right
150
+ };
151
+ });
152
+ if (!o || !s) throw e();
153
+ let c = Object.create(null);
154
+ for (let t of a.get("subsets")?.children ?? []) {
155
+ if (t.name !== "subset" || !t.attrs.name || t.attrs.name in c) throw e();
156
+ c[t.attrs.name] = t.children.map((t) => {
157
+ if (t.name !== "tile" || !t.attrs.name || t.children.length) throw e();
158
+ return t.attrs.name;
159
+ });
160
+ }
161
+ let l = {
162
+ tiles: o,
163
+ neighbors: s,
164
+ subsets: c
165
+ };
166
+ return r(l), l;
167
+ }
168
+ //#endregion
169
+ export { r as buildTileset, a as parseTilesetXML };
package/dist/wfc.wasm ADDED
Binary file