aframe-babia-components 1.4.0 → 1.5.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.
- package/common/city-history.js +53 -0
- package/components/visualizers/babia-boats.js +229 -2060
- package/components/visualizers/babia-city-evocells.js +189 -0
- package/components/visualizers/babia-city-evostreets.js +163 -0
- package/components/visualizers/babia-city-gospermap.js +190 -0
- package/components/visualizers/babia-city-hexgrid.js +6 -0
- package/components/visualizers/babia-city-hilbert.js +5 -0
- package/components/visualizers/babia-city-history-resistant.js +5 -0
- package/components/visualizers/babia-city-morton.js +5 -0
- package/components/visualizers/babia-city-voronoi.js +3 -211
- package/components/visualizers/boats/interactions.js +194 -0
- package/components/visualizers/boats/layout.js +148 -0
- package/components/visualizers/boats/legend.js +135 -0
- package/components/visualizers/evocells/history.js +69 -0
- package/components/visualizers/evocells/layout.js +170 -0
- package/components/visualizers/evocells/rectangles.js +183 -0
- package/components/visualizers/evostreets/layout.js +171 -0
- package/components/visualizers/gospermap/curve.js +91 -0
- package/components/visualizers/gospermap/layout.js +221 -0
- package/components/visualizers/gospermap/prisms.js +152 -0
- package/components/visualizers/gospermap/regions.js +99 -0
- package/components/visualizers/gospermap/selection.js +65 -0
- package/components/visualizers/hexgrid/grid.js +70 -0
- package/components/visualizers/hexgrid/layout.js +90 -0
- package/components/visualizers/hexgrid/prisms.js +71 -0
- package/components/visualizers/hilbert/layout.js +131 -0
- package/components/visualizers/history-resistant/layout.js +128 -0
- package/components/visualizers/morton/layout.js +79 -0
- package/components/visualizers/peak-lot-plan.js +72 -0
- package/components/visualizers/prism-city.js +226 -0
- package/components/visualizers/voronoi/history.js +35 -0
- package/components/visualizers/voronoi/hover-legend.js +6 -1
- package/components/visualizers/voronoi/layout.js +58 -23
- package/dist/aframe-babia-components.js +5872 -3118
- package/dist/aframe-babia-components.min.js +1 -1
- package/dist/aframe-babia-components.min.js.map +1 -1
- package/index.js +7 -0
- package/package.json +1 -1
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Orthogonal, linear EvoStreets: bottom-up double-sided strip packing.
|
|
2
|
+
// See Steinbrückner, Consistent Software Cities, §4.4.1, figs. 4.8–4.11.
|
|
3
|
+
const {number, footprint} = require('../voronoi/layout');
|
|
4
|
+
const defaults = {field: 'uid', width: 'width', depth: 'depth', area: '', height: 'height', color: '',
|
|
5
|
+
size: 8, separation: 0.08, border: 0.12, zone_elevation: 0.08,
|
|
6
|
+
minBuildingHeight: 0.03, maxBuildingHeight: 2, diffBuildingHeight: false, time_evolution_reserve_space: true};
|
|
7
|
+
function positive(value) {
|
|
8
|
+
const n = number(value);
|
|
9
|
+
return n > 0 ? n : 0.5;
|
|
10
|
+
}
|
|
11
|
+
const halfRoad = 0.34; // Packing units; one unit is the median building width at first fit.
|
|
12
|
+
|
|
13
|
+
function frontage(row, d) {
|
|
14
|
+
return Math.sqrt(footprint(row, d));
|
|
15
|
+
}
|
|
16
|
+
function rect(x, z, width, depth) {
|
|
17
|
+
const w = width / 2, d = depth / 2;
|
|
18
|
+
return [{x: x - w, y: z - d}, {x: x + w, y: z - d}, {x: x + w, y: z + d}, {x: x - w, y: z + d}];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class StreetPlan {
|
|
22
|
+
constructor(options = {}) {
|
|
23
|
+
this.options = {...defaults, ...options};
|
|
24
|
+
this.root = {key: '', children: new Map()};
|
|
25
|
+
this.next = 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Record addresses, not copies of snapshots. Equal-age siblings get a stable
|
|
29
|
+
// ID tie-break. Later arrivals append; a deletion never recycles an address.
|
|
30
|
+
observe(records, revision) {
|
|
31
|
+
const live = new Map(), d = this.options;
|
|
32
|
+
const visit = (rows, parent) => {
|
|
33
|
+
const occurrences = new Map();
|
|
34
|
+
const siblings = rows.map((row, i) => {
|
|
35
|
+
const id = String(row[d.field] ?? row.uid ?? row.id ?? row.name ?? i);
|
|
36
|
+
const n = occurrences.get(id) || 0;
|
|
37
|
+
occurrences.set(id, n + 1);
|
|
38
|
+
const street = Array.isArray(row.children);
|
|
39
|
+
return {row, id, street, token: JSON.stringify([id, n, street])};
|
|
40
|
+
}).sort((a, b) => a.token < b.token ? -1 : a.token > b.token ? 1 : 0);
|
|
41
|
+
for (const {row, id, street, token} of siblings) {
|
|
42
|
+
let slot = parent.children.get(token);
|
|
43
|
+
if (!slot) {
|
|
44
|
+
// Prefer turning back towards the ancestor's forward direction;
|
|
45
|
+
// otherwise single-child directories wind around their own subtree.
|
|
46
|
+
const firstBank = parent.bank === 0 ? 1 : 0;
|
|
47
|
+
slot = {id, key: parent.key + '/' + token, birth: revision, bank: (firstBank + parent.children.size) % 2,
|
|
48
|
+
children: street ? new Map() : null, peak: 0};
|
|
49
|
+
parent.children.set(token, slot);
|
|
50
|
+
this.reservedPacked = false;
|
|
51
|
+
}
|
|
52
|
+
live.set(slot, row);
|
|
53
|
+
if (street) visit(row.children, slot);
|
|
54
|
+
else {
|
|
55
|
+
const side = frontage(row, d);
|
|
56
|
+
if (side > slot.peak) {
|
|
57
|
+
slot.peak = side;
|
|
58
|
+
this.reservedPacked = false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
visit(Array.isArray(records) ? records : (records ? [records] : []), this.root);
|
|
64
|
+
return live;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// In each street's local frame length points away from its junction. left
|
|
68
|
+
// and right are its subtree's extents perpendicular to that direction.
|
|
69
|
+
pack(live) {
|
|
70
|
+
// Without a live set, pack the union of addresses at their peak widths.
|
|
71
|
+
this.reservedPacked = !live;
|
|
72
|
+
if (!this.unit) {
|
|
73
|
+
const widths = [];
|
|
74
|
+
const gather = slot => slot.children ? slot.children.forEach(gather) : widths.push(slot.peak);
|
|
75
|
+
gather(this.root);
|
|
76
|
+
widths.sort((a, b) => a-b);
|
|
77
|
+
this.unit = widths[widths.length >> 1] || 1;
|
|
78
|
+
}
|
|
79
|
+
const gap = Math.max(0, this.options.separation), setback = Math.max(0, this.options.border);
|
|
80
|
+
const measure = slot => {
|
|
81
|
+
if (!slot.children) {
|
|
82
|
+
const side = live ? frontage(live.get(slot), this.options) : slot.peak;
|
|
83
|
+
slot.length = side / this.unit;
|
|
84
|
+
slot.left = slot.right = slot.length / 2;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const ends = [halfRoad, halfRoad], wings = [halfRoad, halfRoad];
|
|
88
|
+
slot.start = 0;
|
|
89
|
+
slot.road = 2 * halfRoad;
|
|
90
|
+
let cohort;
|
|
91
|
+
for (const child of slot.children.values()) {
|
|
92
|
+
if (live && !live.has(child)) continue;
|
|
93
|
+
measure(child);
|
|
94
|
+
// Each new cohort starts at the far end of the older cohort,
|
|
95
|
+
// even if one bank previously ended before the other.
|
|
96
|
+
if (child.birth !== cohort) ends.fill(Math.max(...ends));
|
|
97
|
+
cohort = child.birth;
|
|
98
|
+
const bank = child.bank;
|
|
99
|
+
const back = bank ? child.right : child.left, ahead = bank ? child.left : child.right;
|
|
100
|
+
// Only the root has no junction behind it. Its first entrances
|
|
101
|
+
// stay anchored when their subtrees grow back past that origin.
|
|
102
|
+
child.station = ends[bank] + (slot === this.root && ends[bank] === halfRoad ? 0 : back);
|
|
103
|
+
ends[bank] = child.station + ahead + gap;
|
|
104
|
+
slot.start = Math.min(slot.start, child.station - back);
|
|
105
|
+
// A road stops just beyond its last entrance, not at the far
|
|
106
|
+
// edge of a whole perpendicular subcity's bounding rectangle.
|
|
107
|
+
slot.road = Math.max(slot.road, child.station + (child.children ? halfRoad : child.length / 2) + gap);
|
|
108
|
+
child.offset = halfRoad + (child.children ? 0 : setback);
|
|
109
|
+
wings[bank] = Math.max(wings[bank], child.offset + child.length);
|
|
110
|
+
}
|
|
111
|
+
slot.length = Math.max(slot.road, ...ends);
|
|
112
|
+
[slot.left, slot.right] = wings;
|
|
113
|
+
};
|
|
114
|
+
measure(this.root);
|
|
115
|
+
// Never rescale or recenter on a revision change. With known history the
|
|
116
|
+
// first fit covers the whole archive; an open-ended stream can grow outwards.
|
|
117
|
+
if (!this.fit && !live) {
|
|
118
|
+
const {start, length, left, right} = this.root;
|
|
119
|
+
const width = length - start, depth = left + right;
|
|
120
|
+
this.fit = {scale: positive(this.options.size) / Math.max(width, depth), x: -(start + length) / 2, z: (right - left) / 2,
|
|
121
|
+
// Turn long, narrow cities along the gallery's horizontal axis once, not on every frame.
|
|
122
|
+
turn: depth > 1.5 * width};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
frame(records, revision = this.next++) {
|
|
127
|
+
const live = this.observe(records, revision), cells = [], keys = new Set();
|
|
128
|
+
const range = {min: Infinity, max: 0, colorMin: Infinity, colorMax: -Infinity};
|
|
129
|
+
if (!live.size) return {cells, keys, range};
|
|
130
|
+
const reserve = this.options.time_evolution_reserve_space;
|
|
131
|
+
if (!this.fit || (reserve && !this.reservedPacked)) this.pack(null);
|
|
132
|
+
if (!reserve) this.pack(live);
|
|
133
|
+
const d = this.options, {scale, x, z} = this.fit;
|
|
134
|
+
for (const [slot, row] of live) if (!slot.children) {
|
|
135
|
+
const h = number(row[d.height]), c = number(row[d.color]);
|
|
136
|
+
if (h >= 0) { range.min = Math.min(range.min, h); range.max = Math.max(range.max, h); }
|
|
137
|
+
if (Number.isFinite(c)) { range.colorMin = Math.min(range.colorMin, c); range.colorMax = Math.max(range.colorMax, c); }
|
|
138
|
+
}
|
|
139
|
+
const low = d.diffBuildingHeight ? range.min : 0, hMin = Math.max(0, d.minBuildingHeight);
|
|
140
|
+
const heightSpan = Math.max(0, d.maxBuildingHeight - hMin);
|
|
141
|
+
const roots = Array.from(this.root.children.values()).filter(c => live.has(c));
|
|
142
|
+
const needsTrunk = roots.length > 1 || !roots[0].children;
|
|
143
|
+
const visit = (slot, ox, oz, dx, dz, parent) => {
|
|
144
|
+
const data = live.get(slot), quarter = !!slot.children, connector = slot === this.root;
|
|
145
|
+
const length = quarter ? slot.road : frontage(data, d) / this.unit, width = quarter ? 2 * halfRoad : length;
|
|
146
|
+
let height = Math.max(0, d.zone_elevation);
|
|
147
|
+
if (!quarter) {
|
|
148
|
+
const h = number(data[d.height]);
|
|
149
|
+
height = hMin + heightSpan * (h > low && range.max > low ? (h - low) / (range.max - low) : 0);
|
|
150
|
+
}
|
|
151
|
+
// Grow the roof about its lot centre, not away from the street edge.
|
|
152
|
+
const mid = (quarter ? length : slot.length) / 2;
|
|
153
|
+
const cx = (ox + dx * mid) * scale, cz = (oz + dz * mid) * scale;
|
|
154
|
+
if (!connector || needsTrunk) {
|
|
155
|
+
keys.add(slot.key);
|
|
156
|
+
cells.push({key: slot.key, id: connector ? 'Main street' : slot.id, quarter, connector, data: data || {},
|
|
157
|
+
parent, bank: slot.bank, birth: slot.birth, base: 0, height,
|
|
158
|
+
polygon: rect(cx, cz, (dx ? length : width) * scale, (dx ? width : length) * scale)});
|
|
159
|
+
}
|
|
160
|
+
if (quarter) for (const c of slot.children.values()) if (live.has(c)) {
|
|
161
|
+
const sign = c.bank ? -1 : 1, nx = -dz * sign, nz = dx * sign;
|
|
162
|
+
visit(c, ox + dx * c.station + nx * c.offset, oz + dz * c.station + nz * c.offset, nx, nz, slot.key);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
if (this.fit.turn) visit(this.root, z, -x, 0, -1, null);
|
|
166
|
+
else visit(this.root, x, z, 1, 0, null);
|
|
167
|
+
return {cells, keys, range};
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
module.exports = {StreetPlan, defaults};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// Auber et al., GosperMap §3: consecutive vertices of Gosper's flowsnake are
|
|
2
|
+
// neighbouring sites of a hexagonal Voronoi lattice. Both productions draw;
|
|
3
|
+
// +/- turn the turtle by 60 degrees. A begins with A at every order, so asking
|
|
4
|
+
// for a longer prefix never rotates, scales or otherwise moves an old site.
|
|
5
|
+
const rules = {
|
|
6
|
+
A: 'A-B--B+A++AA+B-',
|
|
7
|
+
B: '+A-BB--B-A++A+B'
|
|
8
|
+
};
|
|
9
|
+
const steps = [[1, 0], [0, 1], [-1, 1], [-1, 0], [0, -1], [1, -1]];
|
|
10
|
+
const corners = [[1, -1], [1, 1], [0, 2], [-1, 1], [-1, -1], [0, -2]];
|
|
11
|
+
const xUnit = Math.sqrt(3) / 2;
|
|
12
|
+
const key = (a, b) => a + ',' + b;
|
|
13
|
+
|
|
14
|
+
class GosperGrid {
|
|
15
|
+
constructor(length) {
|
|
16
|
+
this.sites = [];
|
|
17
|
+
this.index = new Map();
|
|
18
|
+
let q = 0, r = 0, heading = 0, order = 0;
|
|
19
|
+
while (7 ** order < length) order++;
|
|
20
|
+
const draw = (symbol, level) => {
|
|
21
|
+
if (!level) {
|
|
22
|
+
this.index.set(key(q, r), this.sites.length);
|
|
23
|
+
this.sites.push({q, r, x: (2 * q + r) * xUnit, y: 1.5 * r});
|
|
24
|
+
const [dq, dr] = steps[heading];
|
|
25
|
+
q += dq;
|
|
26
|
+
r += dr;
|
|
27
|
+
} else
|
|
28
|
+
for (const ch of rules[symbol]) {
|
|
29
|
+
if (this.sites.length >= length) break;
|
|
30
|
+
if (ch === '+')
|
|
31
|
+
heading = (heading + 1) % 6;
|
|
32
|
+
else if (ch === '-')
|
|
33
|
+
heading = (heading + 5) % 6;
|
|
34
|
+
else
|
|
35
|
+
draw(ch, level - 1);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
if (length) draw('A', order);
|
|
39
|
+
const vertices = new Map();
|
|
40
|
+
for (const s of this.sites) {
|
|
41
|
+
s.neighbours = steps.map(([dq, dr]) => this.index.get(key(s.q + dq, s.r + dr)) ?? -1);
|
|
42
|
+
s.corners = corners.map(([a, b]) => {
|
|
43
|
+
const u = 2 * s.q + s.r + a, v = 3 * s.r + b, id = key(u, v);
|
|
44
|
+
if (!vertices.has(id)) vertices.set(id, {x: u * xUnit, y: v / 2});
|
|
45
|
+
return vertices.get(id);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Discard shared edges; each remaining lattice vertex has exactly one
|
|
51
|
+
// outgoing perimeter edge (only three hexagons ever meet at a vertex).
|
|
52
|
+
// Integer corner addresses avoid cracks from floating-point comparisons.
|
|
53
|
+
outline(start, count) {
|
|
54
|
+
const end = start + count, edges = new Map();
|
|
55
|
+
for (let i = start; i < end; i++) {
|
|
56
|
+
const {corners, neighbours} = this.sites[i];
|
|
57
|
+
for (let side = 0; side < 6; side++) {
|
|
58
|
+
if (neighbours[side] >= start && neighbours[side] < end) continue;
|
|
59
|
+
edges.set(corners[side], corners[(side + 1) % 6]);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const rings = [];
|
|
63
|
+
while (edges.size) {
|
|
64
|
+
const first = edges.keys().next().value, ring = [];
|
|
65
|
+
let at = first;
|
|
66
|
+
do {
|
|
67
|
+
ring.push(at);
|
|
68
|
+
const next = edges.get(at);
|
|
69
|
+
edges.delete(at);
|
|
70
|
+
at = next;
|
|
71
|
+
} while (at !== first);
|
|
72
|
+
rings.push(ring);
|
|
73
|
+
}
|
|
74
|
+
// The interval is edge-connected. Its exterior is CCW, holes are CW.
|
|
75
|
+
return rings.sort((a, b) => signedArea(b) - signedArea(a));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function signedArea(ring) {
|
|
80
|
+
let sum = 0;
|
|
81
|
+
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++)
|
|
82
|
+
sum += ring[j].x * ring[i].y - ring[i].x * ring[j].y;
|
|
83
|
+
return sum / 2;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
module.exports = {
|
|
87
|
+
GosperGrid,
|
|
88
|
+
signedArea,
|
|
89
|
+
apothem: xUnit,
|
|
90
|
+
hexArea: 3 * xUnit
|
|
91
|
+
};
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// Auber et al., §3.2: DFS orders the leaves. Each weighted leaf owns a run
|
|
2
|
+
// of hexagons; a directory is the union of its children's consecutive runs.
|
|
3
|
+
const {number, footprint} = require('../voronoi/layout');
|
|
4
|
+
const {GosperGrid, apothem} = require('./curve');
|
|
5
|
+
|
|
6
|
+
// Bound the extra sites used for metric precision, not the number of files.
|
|
7
|
+
const SITE_BUDGET = 12288;
|
|
8
|
+
const emptyMass = 0.5;
|
|
9
|
+
const defaults = {
|
|
10
|
+
field: 'uid',
|
|
11
|
+
width: 'width',
|
|
12
|
+
depth: 'depth',
|
|
13
|
+
area: '',
|
|
14
|
+
height: 'height',
|
|
15
|
+
color: '',
|
|
16
|
+
size: 8,
|
|
17
|
+
border: 0.12,
|
|
18
|
+
separation: 0.08,
|
|
19
|
+
zone_elevation: 0.08,
|
|
20
|
+
minBuildingHeight: 0.03,
|
|
21
|
+
maxBuildingHeight: 2,
|
|
22
|
+
diffBuildingHeight: false,
|
|
23
|
+
time_evolution_reserve_space: true
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Shift each boundary line towards its occupied side. In contrast to scaling
|
|
27
|
+
// about a centroid, this does not pull concavities across another district.
|
|
28
|
+
// Hex turns are +/-60 degrees; the miter is bounded and the same formula
|
|
29
|
+
// enlarges a clockwise hole. Stay below the width of a one-cell neck.
|
|
30
|
+
function insetRing(ring, d, mapPoint) {
|
|
31
|
+
const normals = ring.map((p, i) => {
|
|
32
|
+
const b = ring[(i + 1) % ring.length], dx = b.x - p.x, dy = b.y - p.y, len = Math.hypot(dx, dy);
|
|
33
|
+
return {x: -dy / len, y: dx / len};
|
|
34
|
+
});
|
|
35
|
+
return ring.map((p, i) => {
|
|
36
|
+
const a = normals[(i + ring.length - 1) % ring.length], b = normals[i];
|
|
37
|
+
const m = d / (1 + a.x * b.x + a.y * b.y);
|
|
38
|
+
return mapPoint(p.x + m * (a.x + b.x), p.y + m * (a.y + b.y));
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class GosperPlan {
|
|
43
|
+
constructor(options = {}) {
|
|
44
|
+
this.options = {...defaults, ...options};
|
|
45
|
+
this.roots = new Map();
|
|
46
|
+
this.smallest = Infinity;
|
|
47
|
+
this.deepest = 0;
|
|
48
|
+
this.dirty = true;
|
|
49
|
+
this.outlines = new Map();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Keep first-seen sibling order (the supplied order for a static tree).
|
|
53
|
+
// New siblings append under their own parent rather than re-sorting or
|
|
54
|
+
// moving survivors. planFor primes these addresses in selector date order.
|
|
55
|
+
observe(records) {
|
|
56
|
+
const live = new Map(), d = this.options;
|
|
57
|
+
const visit = (rows, slots, path, depth) => {
|
|
58
|
+
const occurrences = new Map();
|
|
59
|
+
for (const [i, row] of rows.entries()) {
|
|
60
|
+
const id = String(row[d.field] ?? row.uid ?? row.id ?? row.name ?? i);
|
|
61
|
+
const count = occurrences.get(id) || 0, quarter = Array.isArray(row.children);
|
|
62
|
+
occurrences.set(id, count + 1);
|
|
63
|
+
const token = JSON.stringify([id, count, quarter]);
|
|
64
|
+
let slot = slots.get(token);
|
|
65
|
+
if (!slot) {
|
|
66
|
+
slot = {id, key: path + '/' + token, depth, children: quarter ? new Map() : null, peak: 0};
|
|
67
|
+
slots.set(token, slot);
|
|
68
|
+
this.dirty = true;
|
|
69
|
+
this.deepest = Math.max(this.deepest, depth);
|
|
70
|
+
}
|
|
71
|
+
const weight = quarter ? emptyMass : footprint(row, d);
|
|
72
|
+
const mass = Number.isFinite(weight) && weight > 0 ? weight : emptyMass;
|
|
73
|
+
live.set(slot, {row, mass});
|
|
74
|
+
if (quarter && row.children.length)
|
|
75
|
+
visit(row.children, slot.children, slot.key, depth + 1);
|
|
76
|
+
else {
|
|
77
|
+
this.smallest = Math.min(this.smallest, mass);
|
|
78
|
+
if (mass > slot.peak) {
|
|
79
|
+
slot.peak = mass;
|
|
80
|
+
this.dirty = true;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
visit(Array.isArray(records) ? records : records ? [records] : [], this.roots, '', 0);
|
|
86
|
+
return live;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
tileCount(mass) { return Math.max(1, Math.round(mass / this.quantum)); }
|
|
90
|
+
|
|
91
|
+
runs(live) {
|
|
92
|
+
const lots = new Map();
|
|
93
|
+
let cursor = 0;
|
|
94
|
+
const visit = slots => {
|
|
95
|
+
for (const slot of slots.values()) {
|
|
96
|
+
if (live && !live.has(slot)) continue;
|
|
97
|
+
const start = cursor;
|
|
98
|
+
if (slot.children) visit(slot.children);
|
|
99
|
+
// An explicitly empty directory is still a selectable district.
|
|
100
|
+
if (cursor === start)
|
|
101
|
+
cursor += slot.children ? 1 : this.tileCount(live ? live.get(slot).mass : slot.peak);
|
|
102
|
+
lots.set(slot, {start, count: cursor - start});
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
visit(this.roots);
|
|
106
|
+
return {lots, length: cursor};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
prepare() {
|
|
110
|
+
if (!this.dirty) return;
|
|
111
|
+
// Never form a sum of raw byte counts (which could overflow). Size the
|
|
112
|
+
// quantum in powers of two to retain small integer/half-integer ratios.
|
|
113
|
+
const masses = [];
|
|
114
|
+
const gather = slots => {
|
|
115
|
+
for (const s of slots.values()) {
|
|
116
|
+
if (s.children?.size)
|
|
117
|
+
gather(s.children);
|
|
118
|
+
else
|
|
119
|
+
masses.push(s.peak || emptyMass);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
gather(this.roots);
|
|
123
|
+
let quantum = this.quantum || Math.max(Number.MIN_VALUE, this.smallest / 4);
|
|
124
|
+
const max = masses.reduce((a, b) => Math.max(a, b), 0);
|
|
125
|
+
const minimum = max * Math.min(1, masses.reduce((sum, m) => sum + m / max, 0) / SITE_BUDGET);
|
|
126
|
+
while (quantum < minimum) quantum = Math.min(max, quantum * 2);
|
|
127
|
+
const resampled = this.quantum && quantum !== this.quantum;
|
|
128
|
+
this.quantum = quantum;
|
|
129
|
+
this.atlas = this.runs(null);
|
|
130
|
+
if (!this.grid || this.grid.sites.length < this.atlas.length || resampled)
|
|
131
|
+
this.grid = new GosperGrid(this.atlas.length);
|
|
132
|
+
if (!this.fit || resampled) {
|
|
133
|
+
let x = Infinity, y = Infinity, X = -Infinity, Y = -Infinity;
|
|
134
|
+
for (const s of this.grid.sites) {
|
|
135
|
+
x = Math.min(x, s.x - apothem);
|
|
136
|
+
X = Math.max(X, s.x + apothem);
|
|
137
|
+
y = Math.min(y, s.y - 1);
|
|
138
|
+
Y = Math.max(Y, s.y + 1);
|
|
139
|
+
}
|
|
140
|
+
this.fit = {x: (x + X) / 2, y: (y + Y) / 2, extent: Math.max(X - x, Y - y)};
|
|
141
|
+
}
|
|
142
|
+
this.outlines.clear();
|
|
143
|
+
this.dirty = false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
frame(records) {
|
|
147
|
+
const live = this.observe(records), cells = [], keys = new Set();
|
|
148
|
+
const range = {min: Infinity, max: 0, colorMin: Infinity, colorMax: -Infinity};
|
|
149
|
+
if (!live.size) return {cells, keys, range};
|
|
150
|
+
this.prepare();
|
|
151
|
+
const d = this.options, reserve = d.time_evolution_reserve_space !== false,
|
|
152
|
+
lots = reserve ? this.atlas : this.runs(live);
|
|
153
|
+
const fit = this.fit, k = (d.size > 0 ? d.size : 8) / fit.extent;
|
|
154
|
+
const mapPoint = (x, y) => ({x: (x - fit.x) * k, y: (y - fit.y) * k});
|
|
155
|
+
const gap = Math.max(0, d.separation) / 2, border = Math.max(0, d.border),
|
|
156
|
+
elevation = Math.max(0, d.zone_elevation);
|
|
157
|
+
// Equal spacing between coincident contours, including one-child
|
|
158
|
+
// packages. Cap all levels together so even the deepest cell survives.
|
|
159
|
+
const step = Math.min(1 / k, 0.36 * apothem / (gap + border * this.deepest || 1e-100));
|
|
160
|
+
for (const [slot, {row}] of live)
|
|
161
|
+
if (!slot.children) {
|
|
162
|
+
const h = number(row[d.height]), c = number(row[d.color]);
|
|
163
|
+
if (h >= 0) {
|
|
164
|
+
range.min = Math.min(range.min, h);
|
|
165
|
+
range.max = Math.max(range.max, h);
|
|
166
|
+
}
|
|
167
|
+
if (Number.isFinite(c)) {
|
|
168
|
+
range.colorMin = Math.min(range.colorMin, c);
|
|
169
|
+
range.colorMax = Math.max(range.colorMax, c);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const low = d.diffBuildingHeight ? range.min : 0, min = Math.max(0, d.minBuildingHeight),
|
|
173
|
+
span = Math.max(0, d.maxBuildingHeight - min);
|
|
174
|
+
// Cache only outlines used by this and the next redraw, not one mesh
|
|
175
|
+
// or one polygon per file per revision of a long-running stream.
|
|
176
|
+
const next = new Map();
|
|
177
|
+
for (const [slot, lot] of lots.lots) {
|
|
178
|
+
if (!live.has(slot)) continue;
|
|
179
|
+
const {row, mass} = live.get(slot), quarter = !!slot.children;
|
|
180
|
+
const middle = lot.start + Math.floor((lot.count - 1) / 2);
|
|
181
|
+
const count = quarter ? lot.count : this.tileCount(mass), start = middle - Math.floor((count - 1) / 2);
|
|
182
|
+
const shape = start + ':' + count, site = this.grid.sites[middle], anchor = mapPoint(site.x, site.y);
|
|
183
|
+
const raw = next.get(shape) || this.outlines.get(shape) || this.grid.outline(start, count);
|
|
184
|
+
next.set(shape, raw);
|
|
185
|
+
const inset = step * (gap + slot.depth * border), rings = raw.map(r => insetRing(r, inset, mapPoint));
|
|
186
|
+
// Shared lattice vertices let the animated roof triangulation meet
|
|
187
|
+
// the very same inset boundary as its final concave prism.
|
|
188
|
+
const boundary = new Map();
|
|
189
|
+
raw.forEach((ring, j) => ring.forEach((v, i) => boundary.set(v, rings[j][i])));
|
|
190
|
+
const h = number(row[d.height]),
|
|
191
|
+
height = quarter ? elevation :
|
|
192
|
+
min + span * (h > low && range.max > low ? (h - low) / (range.max - low) : 0);
|
|
193
|
+
cells.push({
|
|
194
|
+
id: slot.id,
|
|
195
|
+
key: slot.key,
|
|
196
|
+
data: row,
|
|
197
|
+
quarter,
|
|
198
|
+
base: slot.depth * elevation,
|
|
199
|
+
height,
|
|
200
|
+
start,
|
|
201
|
+
count,
|
|
202
|
+
lot,
|
|
203
|
+
anchor,
|
|
204
|
+
rings,
|
|
205
|
+
polygon: rings[0],
|
|
206
|
+
shape: [shape, inset, k, this.fit.x, this.fit.y].join(':'),
|
|
207
|
+
grid: this.grid,
|
|
208
|
+
mapPoint,
|
|
209
|
+
boundary
|
|
210
|
+
});
|
|
211
|
+
keys.add(slot.key);
|
|
212
|
+
}
|
|
213
|
+
this.outlines = next;
|
|
214
|
+
return {cells, keys, range};
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
module.exports = {
|
|
218
|
+
GosperPlan,
|
|
219
|
+
defaults,
|
|
220
|
+
insetRing
|
|
221
|
+
};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
const {prism, tilesOf, displayed, morph} = require('./regions');
|
|
2
|
+
const noHit = () => {};
|
|
3
|
+
|
|
4
|
+
function install(mesh, geometry) {
|
|
5
|
+
mesh.geometry.dispose();
|
|
6
|
+
mesh.geometry = geometry;
|
|
7
|
+
delete mesh.morphTargetDictionary;
|
|
8
|
+
delete mesh.morphTargetInfluences;
|
|
9
|
+
mesh.updateMorphTargets();
|
|
10
|
+
}
|
|
11
|
+
function visibility(mesh, live) {
|
|
12
|
+
mesh.visible = mesh.scale.y > 0;
|
|
13
|
+
if (live && mesh.visible)
|
|
14
|
+
delete mesh.raycast;
|
|
15
|
+
else
|
|
16
|
+
mesh.raycast = noHit;
|
|
17
|
+
}
|
|
18
|
+
const disjoint = (a, b) => a.start + a.count <= b.start || b.start + b.count <= a.start;
|
|
19
|
+
|
|
20
|
+
// One mesh per data record, not per hexagon. During a partial resize, tile
|
|
21
|
+
// buffers preserve the shared sites. A whole new, removed, or disjoint run can
|
|
22
|
+
// rise/fall as one cheap concave prism: never tessellate its entire interior
|
|
23
|
+
// just to animate its height (especially important on a multi-release seek).
|
|
24
|
+
class GosperPrisms {
|
|
25
|
+
constructor(THREE, el) {
|
|
26
|
+
this.THREE = THREE;
|
|
27
|
+
this.el = el;
|
|
28
|
+
this.group = new THREE.Group();
|
|
29
|
+
this.meshes = new Map();
|
|
30
|
+
this.tracks = new Map();
|
|
31
|
+
this.progress = 1;
|
|
32
|
+
}
|
|
33
|
+
set(cells, keys, duration, colorize) {
|
|
34
|
+
const T = this.THREE, next = new Map();
|
|
35
|
+
const target = (mesh, cell) => {
|
|
36
|
+
const old = mesh.userData.cell, key = (cell || old).key, track = this.tracks.get(key);
|
|
37
|
+
if (!cell && (!duration || !mesh.visible)) return this.drop(key, mesh);
|
|
38
|
+
// When interrupted before a disjoint run has swapped addresses,
|
|
39
|
+
// its displayed outline is still the previous one, not the target.
|
|
40
|
+
const before = track?.phase === 'replace' && !track.switched ? track.before : old;
|
|
41
|
+
const reshape = !before || !cell || before.shape !== cell.shape || !!track?.pairs;
|
|
42
|
+
const y0 = mesh.position.y, y1 = cell ? cell.base : y0, h0 = mesh.scale.y, h1 = cell ? cell.height : 0;
|
|
43
|
+
let pairs, phase;
|
|
44
|
+
if (!duration) {
|
|
45
|
+
if (reshape) install(mesh, prism(T, cell.rings));
|
|
46
|
+
} else if (reshape) {
|
|
47
|
+
if (!mesh.visible || !before) {
|
|
48
|
+
install(mesh, prism(T, cell.rings));
|
|
49
|
+
phase = 'arrive';
|
|
50
|
+
} else if (!track?.pairs && !cell)
|
|
51
|
+
phase = 'depart';
|
|
52
|
+
else if (!track?.pairs && disjoint(before, cell))
|
|
53
|
+
phase = 'replace';
|
|
54
|
+
else {
|
|
55
|
+
const from = track?.pairs ? displayed(track, this.progress) : tilesOf(before);
|
|
56
|
+
if (!cell && !from.size) return this.drop(key, mesh);
|
|
57
|
+
const to = cell ? tilesOf(cell) : new Map();
|
|
58
|
+
const grid = !cell || before.grid.sites.length > cell.grid.sites.length ? before.grid : cell.grid;
|
|
59
|
+
const result = morph(T, from, to, grid);
|
|
60
|
+
install(mesh, result.geometry);
|
|
61
|
+
pairs = result.pairs;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (duration && cell && !mesh.visible && h1 > 0) phase = 'arrive';
|
|
65
|
+
if (cell) {
|
|
66
|
+
mesh.userData.cell = cell;
|
|
67
|
+
colorize(mesh.material.color, cell);
|
|
68
|
+
} else
|
|
69
|
+
mesh.userData.cell = before;
|
|
70
|
+
if (duration && (phase || pairs || y0 !== y1 || h0 !== h1)) {
|
|
71
|
+
next.set(key, {
|
|
72
|
+
mesh,
|
|
73
|
+
y0,
|
|
74
|
+
y1,
|
|
75
|
+
h0,
|
|
76
|
+
h1,
|
|
77
|
+
pairs,
|
|
78
|
+
phase,
|
|
79
|
+
before: phase === 'replace' ? before : null,
|
|
80
|
+
switched: false,
|
|
81
|
+
exiting: !cell
|
|
82
|
+
});
|
|
83
|
+
} else {
|
|
84
|
+
mesh.position.y = y1;
|
|
85
|
+
mesh.scale.y = h1;
|
|
86
|
+
}
|
|
87
|
+
visibility(mesh, !!cell);
|
|
88
|
+
};
|
|
89
|
+
for (const c of cells) {
|
|
90
|
+
let m = this.meshes.get(c.key);
|
|
91
|
+
if (!m) {
|
|
92
|
+
m = new T.Mesh(undefined, new T.MeshStandardMaterial());
|
|
93
|
+
m.name = c.id;
|
|
94
|
+
m.el = this.el;
|
|
95
|
+
m.position.y = c.base;
|
|
96
|
+
m.scale.y = 0;
|
|
97
|
+
this.group.add(m);
|
|
98
|
+
this.meshes.set(c.key, m);
|
|
99
|
+
}
|
|
100
|
+
target(m, c);
|
|
101
|
+
}
|
|
102
|
+
for (const [key, m] of this.meshes)
|
|
103
|
+
if (!keys.has(key)) target(m, null);
|
|
104
|
+
this.tracks = next;
|
|
105
|
+
this.duration = duration;
|
|
106
|
+
this.elapsed = 0;
|
|
107
|
+
this.progress = next.size ? 0 : 1;
|
|
108
|
+
}
|
|
109
|
+
step(delta = 0) {
|
|
110
|
+
if (!this.tracks.size) return;
|
|
111
|
+
this.elapsed += Math.max(0, delta);
|
|
112
|
+
const t = Math.min(1, this.elapsed / this.duration), p = this.progress = t * t * (3 - 2 * t);
|
|
113
|
+
const out = Math.max(0, 1 - 2 * p), enter = Math.max(0, 2 * p - 1);
|
|
114
|
+
for (const [key, track] of this.tracks) {
|
|
115
|
+
const {mesh, y0, y1, h0, h1, pairs, phase, exiting} = track;
|
|
116
|
+
if (t === 1 && exiting) {
|
|
117
|
+
this.drop(key, mesh);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (phase === 'replace' && p >= 0.5 && !track.switched) {
|
|
121
|
+
install(mesh, prism(this.THREE, mesh.userData.cell.rings));
|
|
122
|
+
track.switched = true;
|
|
123
|
+
}
|
|
124
|
+
mesh.position.y = t === 1 ? y1 : y0 + (y1 - y0) * p;
|
|
125
|
+
mesh.scale.y = phase ? h0 * out + h1 * enter : t === 1 ? h1 : h0 + (h1 - h0) * p;
|
|
126
|
+
visibility(mesh, !exiting);
|
|
127
|
+
if (pairs) {
|
|
128
|
+
if (t === 1)
|
|
129
|
+
install(mesh, prism(this.THREE, mesh.userData.cell.rings));
|
|
130
|
+
else {
|
|
131
|
+
mesh.morphTargetInfluences[0] = 1 - Math.abs(2 * p - 1);
|
|
132
|
+
mesh.morphTargetInfluences[1] = enter;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (t === 1) this.tracks.clear();
|
|
137
|
+
}
|
|
138
|
+
finish() {
|
|
139
|
+
if (this.tracks.size) this.step(this.duration);
|
|
140
|
+
}
|
|
141
|
+
drop(key, m) {
|
|
142
|
+
this.group.remove(m);
|
|
143
|
+
m.geometry.dispose();
|
|
144
|
+
m.material.dispose();
|
|
145
|
+
this.meshes.delete(key);
|
|
146
|
+
}
|
|
147
|
+
clear() {
|
|
148
|
+
this.tracks.clear();
|
|
149
|
+
for (const [k, m] of this.meshes) this.drop(k, m);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
module.exports = {GosperPrisms};
|