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,99 @@
|
|
|
1
|
+
// Settled lots have just one triangulated roof and perimeter walls. Do not
|
|
2
|
+
// use the Voronoi convex-ring morph: an interval of the flowsnake is concave.
|
|
3
|
+
function prism(THREE, rings) {
|
|
4
|
+
const path = (ring, shape) => {
|
|
5
|
+
shape.moveTo(ring[0].x, -ring[0].y);
|
|
6
|
+
for (let i = 1; i < ring.length; i++) shape.lineTo(ring[i].x, -ring[i].y);
|
|
7
|
+
shape.closePath();
|
|
8
|
+
return shape;
|
|
9
|
+
};
|
|
10
|
+
const shape = path(rings[0], new THREE.Shape());
|
|
11
|
+
shape.holes = rings.slice(1).map(r => path(r, new THREE.Path()));
|
|
12
|
+
return new THREE.ExtrudeGeometry(shape, {depth: 1, bevelEnabled: false}).rotateX(-Math.PI / 2);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function tilesOf(cell) {
|
|
16
|
+
const tiles = new Map();
|
|
17
|
+
for (let i = cell.start; i < cell.start + cell.count; i++) {
|
|
18
|
+
tiles.set(i, cell.grid.sites[i].corners.map(v => cell.boundary.get(v) || cell.mapPoint(v.x, v.y)));
|
|
19
|
+
}
|
|
20
|
+
return tiles;
|
|
21
|
+
}
|
|
22
|
+
const center = points => (
|
|
23
|
+
{x: points.reduce((v, p) => v + p.x, 0) / points.length, y: points.reduce((v, p) => v + p.y, 0) / points.length});
|
|
24
|
+
const collapsed = points => Array(6).fill(center(points));
|
|
25
|
+
const equal = (a, b) => a.x === b.x && a.y === b.y;
|
|
26
|
+
const welded = (a, b, i) => b && equal(a[i], b[(i + 4) % 6]) && equal(a[(i + 1) % 6], b[(i + 3) % 6]);
|
|
27
|
+
|
|
28
|
+
// First vacate a transferred address, then fill it. Lost and gained roofs
|
|
29
|
+
// must not occupy the same hexagon halfway through a current-footprint seek.
|
|
30
|
+
function fraction(pair, t) { return pair.entering ? Math.max(0, 2 * t - 1) : pair.leaving ? Math.min(1, 2 * t) : t; }
|
|
31
|
+
|
|
32
|
+
function displayed(track, progress) {
|
|
33
|
+
const tiles = new Map();
|
|
34
|
+
for (const [idx, pair] of track.pairs) {
|
|
35
|
+
const {from, to} = pair, t = fraction(pair, progress);
|
|
36
|
+
const ring = t === 0 ? from :
|
|
37
|
+
t === 1 ? to :
|
|
38
|
+
from.map((p, i) => ({x: p.x + t * (to[i].x - p.x), y: p.y + t * (to[i].y - p.y)}));
|
|
39
|
+
if (!ring.every(p => equal(p, ring[0]))) tiles.set(idx, ring);
|
|
40
|
+
}
|
|
41
|
+
return tiles;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Match tiles by curve address, not by polygon vertex number. Shared tiles
|
|
45
|
+
// stay where they are; only gained/lost cells grow/collapse, inside their own
|
|
46
|
+
// hexagons. No triangles span bays when an outline changes topology on seek.
|
|
47
|
+
function morph(THREE, before, after, grid) {
|
|
48
|
+
const pairs = new Map(), pos = [], half = [], dest = [], normals = [], indices = [];
|
|
49
|
+
for (const idx of new Set([...before.keys(), ...after.keys()])) {
|
|
50
|
+
const a = before.get(idx), b = after.get(idx);
|
|
51
|
+
pairs.set(idx, {from: a || collapsed(b), to: b || collapsed(a), entering: !a, leaving: !b});
|
|
52
|
+
}
|
|
53
|
+
for (const [idx, pair] of pairs) {
|
|
54
|
+
const {from, to} = pair, mid = fraction(pair, 0.5);
|
|
55
|
+
const vertex = (p, q, h, nx, ny, nz) => {
|
|
56
|
+
pos.push(p.x, h, p.y);
|
|
57
|
+
half.push(p.x + mid * (q.x - p.x), h, p.y + mid * (q.y - p.y));
|
|
58
|
+
dest.push(q.x, h, q.y);
|
|
59
|
+
normals.push(nx, ny, nz);
|
|
60
|
+
};
|
|
61
|
+
const ac = center(from), bc = center(to), base = pos.length / 3;
|
|
62
|
+
for (const h of [0, 1]) {
|
|
63
|
+
const first = base + 7 * h;
|
|
64
|
+
vertex(ac, bc, h, 0, h ? 1 : -1, 0);
|
|
65
|
+
for (let j = 0; j < 6; j++) {
|
|
66
|
+
vertex(from[j], to[j], h, 0, h ? 1 : -1, 0);
|
|
67
|
+
const a = first + 1 + j, b = first + 1 + (j + 1) % 6;
|
|
68
|
+
indices.push(first, h ? b : a, h ? a : b);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
for (let j = 0; j < 6; j++) {
|
|
72
|
+
const neighbour = pairs.get(grid.sites[idx]?.neighbours[j]);
|
|
73
|
+
if (neighbour && welded(from, neighbour.from, j) && welded(to, neighbour.to, j)) continue;
|
|
74
|
+
const end = (j + 1) % 6, v = pos.length / 3;
|
|
75
|
+
const dx = to[end].x - to[j].x + from[end].x - from[j].x,
|
|
76
|
+
dy = to[end].y - to[j].y + from[end].y - from[j].y;
|
|
77
|
+
const length = Math.hypot(dx, dy) || 1, nx = dy / length, nz = -dx / length;
|
|
78
|
+
vertex(from[j], to[j], 0, nx, 0, nz);
|
|
79
|
+
vertex(from[j], to[j], 1, nx, 0, nz);
|
|
80
|
+
vertex(from[end], to[end], 1, nx, 0, nz);
|
|
81
|
+
vertex(from[end], to[end], 0, nx, 0, nz);
|
|
82
|
+
indices.push(v, v + 1, v + 2, v, v + 2, v + 3);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const geometry = new THREE.BufferGeometry();
|
|
86
|
+
geometry.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3));
|
|
87
|
+
geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));
|
|
88
|
+
geometry.morphAttributes.position = [half, dest].map(a => new THREE.Float32BufferAttribute(a, 3));
|
|
89
|
+
geometry.setIndex(indices);
|
|
90
|
+
return {geometry, pairs};
|
|
91
|
+
}
|
|
92
|
+
module.exports = {
|
|
93
|
+
prism,
|
|
94
|
+
tilesOf,
|
|
95
|
+
displayed,
|
|
96
|
+
morph,
|
|
97
|
+
center,
|
|
98
|
+
fraction
|
|
99
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const {CellLabel} = require('../voronoi/hover-legend');
|
|
2
|
+
const {center, fraction} = require('./regions');
|
|
3
|
+
|
|
4
|
+
// Prefer a site present throughout a transition. Even the area centroid of
|
|
5
|
+
// a concave district can land in another package. When two runs are disjoint
|
|
6
|
+
// the old label vanishes with its last tile before the new one appears.
|
|
7
|
+
function labelSites(track, middle) {
|
|
8
|
+
if (!track?.pairs) return null;
|
|
9
|
+
let shared, leaving, entering;
|
|
10
|
+
for (const [i, pair] of track.pairs) {
|
|
11
|
+
const dist = Math.abs(i - middle);
|
|
12
|
+
if (!pair.entering && !pair.leaving && (!shared || dist < shared.dist)) shared = {dist, pair};
|
|
13
|
+
if (pair.leaving && (!leaving || dist < leaving.dist)) leaving = {dist, pair};
|
|
14
|
+
if (pair.entering && (!entering || dist < entering.dist)) entering = {dist, pair};
|
|
15
|
+
}
|
|
16
|
+
const at = site => site && {pair: site.pair, from: center(site.pair.from), to: center(site.pair.to)};
|
|
17
|
+
return {before: at(shared || leaving), after: at(shared || entering)};
|
|
18
|
+
}
|
|
19
|
+
class DistrictSelection {
|
|
20
|
+
constructor(THREE, scene) {
|
|
21
|
+
this.THREE = THREE;
|
|
22
|
+
this.scene = scene;
|
|
23
|
+
this.mesh = null;
|
|
24
|
+
this.point = new THREE.Vector3();
|
|
25
|
+
}
|
|
26
|
+
set(mesh, prisms, options) {
|
|
27
|
+
if (this.mesh && this.mesh !== mesh) this.mesh.material.color.set(options.base_color);
|
|
28
|
+
this.mesh = mesh;
|
|
29
|
+
this.sites = null;
|
|
30
|
+
if (!mesh) {
|
|
31
|
+
this.label?.hide();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const c = mesh.userData.cell;
|
|
35
|
+
this.sites = labelSites(prisms.tracks.get(c.key), c.start + Math.floor((c.count - 1) / 2));
|
|
36
|
+
mesh.material.color.set(options.quarterHighlightColor);
|
|
37
|
+
if (!this.label) {
|
|
38
|
+
this.label = new CellLabel(this.THREE, this.scene);
|
|
39
|
+
this.label.sprite.renderOrder = 999;
|
|
40
|
+
}
|
|
41
|
+
this.update(prisms);
|
|
42
|
+
}
|
|
43
|
+
update(prisms) {
|
|
44
|
+
if (!this.mesh) return;
|
|
45
|
+
if (!this.mesh.visible) return this.label.hide();
|
|
46
|
+
const cell = this.mesh.userData.cell, track = prisms.tracks.get(cell.key);
|
|
47
|
+
const {x, y} = (track?.phase === 'replace' && !track.switched ? track.before : cell).anchor;
|
|
48
|
+
this.point.set(x, 1, y);
|
|
49
|
+
if (this.sites && track) {
|
|
50
|
+
const site = prisms.progress < 0.5 ? this.sites.before : this.sites.after;
|
|
51
|
+
if (!site) return this.label.hide();
|
|
52
|
+
const {pair, from, to} = site, t = fraction(pair, prisms.progress);
|
|
53
|
+
if ((pair.entering && !t) || (pair.leaving && t === 1)) return this.label.hide();
|
|
54
|
+
this.point.set(from.x + t * (to.x - from.x), 1, from.y + t * (to.y - from.y));
|
|
55
|
+
} else
|
|
56
|
+
this.sites = null;
|
|
57
|
+
this.label.show(cell, this.mesh.localToWorld(this.point));
|
|
58
|
+
}
|
|
59
|
+
dispose() {
|
|
60
|
+
this.label?.dispose();
|
|
61
|
+
this.mesh = null;
|
|
62
|
+
this.sites = null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
module.exports = {DistrictSelection};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Pointy-top axial coordinates (Red Blob Games). The six directions are CCW;
|
|
2
|
+
// direction i crosses the edge between corners i and i+1 of a tile.
|
|
3
|
+
const directions = [[1, 0], [0, 1], [-1, 1], [-1, 0], [0, -1], [1, -1]];
|
|
4
|
+
const apothem = Math.sqrt(3) / 2;
|
|
5
|
+
const corners = [[apothem, -.5], [apothem, .5], [0, 1], [-apothem, .5], [-apothem, -.5], [0, -1]];
|
|
6
|
+
const address = (q, r) => q + ',' + r;
|
|
7
|
+
const center = ({q, r}) => ({x: apothem * (2*q + r), y: 1.5*r});
|
|
8
|
+
function project(site, fit, radius) {
|
|
9
|
+
const p = center(site);
|
|
10
|
+
return {x: (p.x - fit.x) * radius, y: (p.y - fit.y) * radius};
|
|
11
|
+
}
|
|
12
|
+
const distance = (q, r) => Math.max(Math.abs(q), Math.abs(r), Math.abs(q+r));
|
|
13
|
+
|
|
14
|
+
// Grow along a district's free boundary, nearest its home tile first. Axial
|
|
15
|
+
// tie-breaks make placement independent of Map iteration order. Even an
|
|
16
|
+
// entirely landlocked district can grow: spill to the nearest vacant ring.
|
|
17
|
+
function vacancy(home, frontier, occupied) {
|
|
18
|
+
if (!occupied.size) return {q: 0, r: 0};
|
|
19
|
+
let best, nearest = Infinity;
|
|
20
|
+
for (const [at, tile] of frontier) {
|
|
21
|
+
if (occupied.has(at)) { frontier.delete(at); continue; }
|
|
22
|
+
const {q, r} = tile, d = distance(q - home.q, r - home.r);
|
|
23
|
+
if (d < nearest || (d === nearest && (q < best.q || (q === best.q && r < best.r)))) {
|
|
24
|
+
best = tile; nearest = d;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (best) return best;
|
|
28
|
+
for (let ring = 1; ; ring++) {
|
|
29
|
+
let q = home.q, r = home.r - ring;
|
|
30
|
+
for (const [dq, dr] of directions) for (let i = 0; i < ring; i++) {
|
|
31
|
+
if (!occupied.has(address(q, r))) return {q, r};
|
|
32
|
+
q += dq; r += dr;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Keep survivors before placing *any* births. A directory gets a selectable
|
|
38
|
+
// home tile even when empty. Its raised floor is the union of all descendant
|
|
39
|
+
// tiles, not a convex hull that could cover an unrelated package.
|
|
40
|
+
function allocate(nodes, previous, reserve) {
|
|
41
|
+
if (previous && (reserve || previous.sites.size === nodes.size) &&
|
|
42
|
+
Array.from(nodes.keys()).every(key => previous.sites.has(key))) return previous;
|
|
43
|
+
const sites = new Map(), districts = new Map([['', new Map()]]), frontiers = new Map([['', new Map()]]);
|
|
44
|
+
const occupied = districts.get('');
|
|
45
|
+
function add(key, site) {
|
|
46
|
+
const at = address(site.q, site.r);
|
|
47
|
+
sites.set(key, site);
|
|
48
|
+
if (site.quarter) { districts.set(key, new Map()); frontiers.set(key, new Map()); }
|
|
49
|
+
for (let parent = site.quarter ? key : site.parent; parent !== undefined; parent = sites.get(parent)?.parent) {
|
|
50
|
+
districts.get(parent).set(at, site);
|
|
51
|
+
const frontier = frontiers.get(parent);
|
|
52
|
+
frontier.delete(at);
|
|
53
|
+
for (const [dq, dr] of directions) {
|
|
54
|
+
const q = site.q + dq, r = site.r + dr, neighbour = address(q, r);
|
|
55
|
+
if (!occupied.has(neighbour)) frontier.set(neighbour, {q, r});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Maps are in parent-before-child order. Their grids are immutable once
|
|
60
|
+
// published, so a direct/backward seek can use an earlier date verbatim.
|
|
61
|
+
if (previous) for (const [key, site] of previous.sites) if (reserve || nodes.has(key)) add(key, site);
|
|
62
|
+
for (const [key, {parent, quarter}] of nodes) if (!sites.has(key)) {
|
|
63
|
+
const home = sites.get(parent) || {q: 0, r: 0};
|
|
64
|
+
const position = vacancy(home, frontiers.get(parent), occupied);
|
|
65
|
+
add(key, {...position, parent, quarter});
|
|
66
|
+
}
|
|
67
|
+
return {sites, districts};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = {allocate, center, project, apothem, address, directions, corners};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const {snapshot, occupy} = require('../voronoi/layout');
|
|
2
|
+
const {allocate, center, project, apothem, corners} = require('./grid');
|
|
3
|
+
|
|
4
|
+
const defaults = {field: 'uid', area: '', width: 'width', depth: 'depth', height: 'height', color: '',
|
|
5
|
+
size: 8, separation: 0.08, border: 0.12, zone_elevation: 0.08, minBuildingHeight: 0.03,
|
|
6
|
+
maxBuildingHeight: 2, diffBuildingHeight: false, time_evolution_reserve_space: true};
|
|
7
|
+
|
|
8
|
+
class HexGridPlan {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.options = {...defaults, ...options};
|
|
11
|
+
this.frames = [];
|
|
12
|
+
this.peak = 0;
|
|
13
|
+
this.bounds = {left: Infinity, bottom: Infinity, right: -Infinity, top: -Infinity};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
advance(roots) {
|
|
17
|
+
const nodes = new Map();
|
|
18
|
+
const visit = (siblings, parent) => {
|
|
19
|
+
for (const {key, children, mass} of siblings) {
|
|
20
|
+
nodes.set(key, {parent, quarter: !!children});
|
|
21
|
+
if (children) visit(children, key);
|
|
22
|
+
else this.peak = Math.max(this.peak, mass);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
visit(roots, '');
|
|
26
|
+
this.active = allocate(nodes, this.active, false);
|
|
27
|
+
this.reserved = this.reserved ? allocate(nodes, this.reserved, true) : this.active;
|
|
28
|
+
// Fit the loaded selector history, or the first non-empty stream frame.
|
|
29
|
+
// Never recenter a running stream just because another ring was added.
|
|
30
|
+
if (!this.fit) for (const grid of [this.active, this.reserved]) {
|
|
31
|
+
for (const site of grid.sites.values()) {
|
|
32
|
+
const {x, y} = center(site), b = this.bounds;
|
|
33
|
+
b.left = Math.min(b.left, x - apothem); b.right = Math.max(b.right, x + apothem);
|
|
34
|
+
b.bottom = Math.min(b.bottom, y - 1); b.top = Math.max(b.top, y + 1);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// planFor primes these addresses in chronological order. Cache addresses,
|
|
40
|
+
// not meshes or the selector's records; playback is owned by the selector.
|
|
41
|
+
observe(records, index) {
|
|
42
|
+
this.advance(snapshot(records, this.options).roots);
|
|
43
|
+
if (Number.isInteger(index)) this.frames[index] = this.active;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
frame(records, revision = this.clock?.selectedIndex) {
|
|
47
|
+
const d = this.options, live = snapshot(records, d), archived = this.frames[revision];
|
|
48
|
+
if (!archived) this.advance(live.roots);
|
|
49
|
+
const grid = d.time_evolution_reserve_space ? this.reserved : archived || this.active;
|
|
50
|
+
const lots = new Map(), anchors = new Map();
|
|
51
|
+
if (!live.roots.length) return {cells: [], anchors, range: live.range};
|
|
52
|
+
if (!this.fit) {
|
|
53
|
+
const {left, bottom, right, top} = this.bounds;
|
|
54
|
+
this.fit = {x: (right + left)/2, y: (top + bottom)/2, extent: Math.max(right-left, top-bottom)};
|
|
55
|
+
}
|
|
56
|
+
const radius = (d.size > 0 ? d.size : defaults.size) / this.fit.extent;
|
|
57
|
+
// Street widths are in scene units; cap insets so a tiny lot still has
|
|
58
|
+
// a roof. With both set to zero, equal-weight roofs can tile exactly.
|
|
59
|
+
const street = Math.min(0.18*radius, Math.max(0, d.separation)/(2*apothem));
|
|
60
|
+
const rim = Math.min(0.22*radius, Math.max(0, d.border)/apothem);
|
|
61
|
+
const roof = radius - street - rim;
|
|
62
|
+
// Each address owns one full tile, independent of its occupant's mass.
|
|
63
|
+
// Scale live roof areas together, never their spacing or height.
|
|
64
|
+
let peak = 0;
|
|
65
|
+
if (d.time_evolution_reserve_space) peak = this.peak;
|
|
66
|
+
else {
|
|
67
|
+
const weigh = nodes => nodes.forEach(n => { if (n.children) weigh(n.children); else peak = Math.max(peak, n.mass); });
|
|
68
|
+
weigh(live.roots);
|
|
69
|
+
}
|
|
70
|
+
const visit = nodes => {
|
|
71
|
+
for (const {key, children} of nodes) {
|
|
72
|
+
const p = project(grid.sites.get(key), this.fit, radius), size = children ? radius - street : roof;
|
|
73
|
+
anchors.set(key, p);
|
|
74
|
+
lots.set(key, {mass: peak, polygon: corners.map(([x, y]) => ({x: p.x + size*x, y: p.y + size*y}))});
|
|
75
|
+
if (children) visit(children);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
visit(live.roots);
|
|
79
|
+
const frame = occupy(live, {lots, anchors}, d);
|
|
80
|
+
for (const cell of frame.cells) if (cell.quarter) {
|
|
81
|
+
cell.tiles = grid.districts.get(cell.key);
|
|
82
|
+
cell.radius = radius;
|
|
83
|
+
cell.street = street;
|
|
84
|
+
cell.fit = this.fit;
|
|
85
|
+
}
|
|
86
|
+
return frame;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
module.exports = {HexGridPlan};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const {CityPrisms} = require('../voronoi/prisms');
|
|
2
|
+
const {address, directions, corners, center, project} = require('./grid');
|
|
3
|
+
const steps = directions.map(([q, r]) => center({q, r}));
|
|
4
|
+
|
|
5
|
+
// A district can be concave, have a hole, or span more than one island. Tile
|
|
6
|
+
// its caps and emit only exterior walls; a convex prism would fill those gaps.
|
|
7
|
+
function districtGeometry(THREE, {tiles, fit, radius, street}) {
|
|
8
|
+
const vertices = [], indices = [];
|
|
9
|
+
for (const site of tiles.values()) {
|
|
10
|
+
const p = project(site, fit, radius), start = vertices.length / 3;
|
|
11
|
+
const neighbours = directions.map(([q, r]) => tiles.has(address(site.q + q, site.r + r)));
|
|
12
|
+
const ring = corners.map(([x, y], i) => {
|
|
13
|
+
const j = (i + 5) % 6;
|
|
14
|
+
// Three tiles meet at a vertex. Inset toward the sole occupied
|
|
15
|
+
// centre, away from the sole missing centre, or not at all. Both
|
|
16
|
+
// sides of a shared edge thus compute the same endpoints.
|
|
17
|
+
let dx = 0, dy = 0;
|
|
18
|
+
if (!neighbours[i] && !neighbours[j]) { dx = -x; dy = -y; }
|
|
19
|
+
else if (neighbours[i] !== neighbours[j]) {
|
|
20
|
+
const missing = steps[neighbours[i] ? j : i];
|
|
21
|
+
dx = x - missing.x; dy = y - missing.y;
|
|
22
|
+
}
|
|
23
|
+
return {x: p.x + x*radius + street*dx, z: p.y + y*radius + street*dy};
|
|
24
|
+
});
|
|
25
|
+
for (const h of [0, 1]) for (const v of ring) vertices.push(v.x, h, v.z);
|
|
26
|
+
for (let i = 1; i < 5; i++) {
|
|
27
|
+
indices.push(start, start+i, start+i+1, start+6, start+7+i, start+6+i);
|
|
28
|
+
}
|
|
29
|
+
for (let i = 0; i < 6; i++) if (!neighbours[i]) {
|
|
30
|
+
const a = ring[i], b = ring[(i+1)%6], j = vertices.length/3;
|
|
31
|
+
vertices.push(a.x, 0, a.z, a.x, 1, a.z, b.x, 1, b.z, b.x, 0, b.z);
|
|
32
|
+
indices.push(j, j+1, j+2, j, j+2, j+3);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const geo = new THREE.BufferGeometry();
|
|
36
|
+
geo.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
|
|
37
|
+
geo.setIndex(indices);
|
|
38
|
+
geo.computeVertexNormals();
|
|
39
|
+
return geo;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Keep one pickable mesh per directory, including disjoint parts of its
|
|
43
|
+
// floor. Buildings use the same keyed prism animations as the Voronoi city.
|
|
44
|
+
class HexPrisms extends CityPrisms {
|
|
45
|
+
set(cells, keys, duration, colorize) {
|
|
46
|
+
for (const [key, mesh] of this.meshes) if (mesh.userData.cell.quarter && !keys.has(key)) this.drop(key, mesh);
|
|
47
|
+
const buildings = [];
|
|
48
|
+
for (const cell of cells) {
|
|
49
|
+
if (!cell.quarter) { buildings.push(cell); continue; }
|
|
50
|
+
let mesh = this.meshes.get(cell.key);
|
|
51
|
+
const last = mesh?.userData.cell;
|
|
52
|
+
if (!mesh) {
|
|
53
|
+
mesh = new this.THREE.Mesh(districtGeometry(this.THREE, cell), new this.THREE.MeshStandardMaterial());
|
|
54
|
+
mesh.name = cell.id;
|
|
55
|
+
mesh.el = this.el;
|
|
56
|
+
this.group.add(mesh);
|
|
57
|
+
this.meshes.set(cell.key, mesh);
|
|
58
|
+
} else if (last.tiles !== cell.tiles || last.radius !== cell.radius || last.street !== cell.street) {
|
|
59
|
+
mesh.geometry.dispose();
|
|
60
|
+
mesh.geometry = districtGeometry(this.THREE, cell);
|
|
61
|
+
}
|
|
62
|
+
mesh.userData.cell = cell;
|
|
63
|
+
mesh.visible = cell.height > 0;
|
|
64
|
+
mesh.position.y = cell.base;
|
|
65
|
+
mesh.scale.y = cell.height;
|
|
66
|
+
colorize(mesh.material.color, cell);
|
|
67
|
+
}
|
|
68
|
+
super.set(buildings, keys, duration, colorize);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
module.exports = {HexPrisms};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Tak & Cockburn, Enhanced Spatial Stability with Hilbert and Moore Treemaps,
|
|
2
|
+
// §5, Figs. 6–9 (doi:10.1109/TVCG.2012.108). Coordinates are on the X/Z plane.
|
|
3
|
+
const {PeakLotPlan} = require('../peak-lot-plan');
|
|
4
|
+
|
|
5
|
+
// An oriented rectangle has an entry corner (x,y), a vector u along its
|
|
6
|
+
// entry-to-exit side, and a perpendicular vector v pointing into the curve.
|
|
7
|
+
// Cutting in these local coordinates also works for rotated/reflected curves.
|
|
8
|
+
function patch(r, u, v, width, height) {
|
|
9
|
+
return {x: r.x + u*r.ux + v*r.vx, y: r.y + u*r.uy + v*r.vy,
|
|
10
|
+
ux: width*r.ux, uy: width*r.uy, vx: height*r.vx, vy: height*r.vy};
|
|
11
|
+
}
|
|
12
|
+
const widthOf = r => Math.abs(r.ux || r.uy);
|
|
13
|
+
const heightOf = r => Math.abs(r.vx || r.vy);
|
|
14
|
+
|
|
15
|
+
// Hilbert's U: swap the axes in A, carry them through B and C, and
|
|
16
|
+
// reverse-swap them in D. The exit of each subcurve faces the next entry.
|
|
17
|
+
function turn(r, i) {
|
|
18
|
+
if (i === 0) return {...r, ux: r.vx, uy: r.vy, vx: r.ux, vy: r.uy};
|
|
19
|
+
if (i === 3) return {x: r.x + r.ux + r.vx, y: r.y + r.uy + r.vy,
|
|
20
|
+
ux: -r.vx, uy: -r.vy, vx: -r.ux, vy: -r.uy};
|
|
21
|
+
return r;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Ordered weighted cuts. across() reads left to right, up() bottom to top,
|
|
25
|
+
// in the local frame. Numbers are the *input* ordinals, not a sort by size.
|
|
26
|
+
const across = (...parts) => ({horizontal: true, parts});
|
|
27
|
+
const up = (...parts) => ({horizontal: false, parts});
|
|
28
|
+
const horizontalSplit = up(across(0, 3), across(1, 2));
|
|
29
|
+
const verticalSplit = across(up(0, 1), up(3, 2));
|
|
30
|
+
const templates = [[], [0], [across(0, 1)], [
|
|
31
|
+
across(0, 1, 2), across(0, up(2, 1)), up(across(0, 2), 1), across(up(0, 1), 2)
|
|
32
|
+
], [
|
|
33
|
+
across(0, 1, 2, 3), across(0, up(3, 2, 1)), up(across(0, up(3, 2)), 1),
|
|
34
|
+
up(across(up(0, 1), 3), 2), across(up(0, 1, 2), 3), horizontalSplit, verticalSplit
|
|
35
|
+
]];
|
|
36
|
+
|
|
37
|
+
function tile(pattern, weights, rect) {
|
|
38
|
+
const tiles = [];
|
|
39
|
+
const weight = p => typeof p === 'number' ? weights[p] : p.parts.reduce((s, part) => s + weight(part), 0);
|
|
40
|
+
function cut(p, r) {
|
|
41
|
+
if (typeof p === 'number') { tiles[p] = r; return; }
|
|
42
|
+
const total = weight(p);
|
|
43
|
+
let offset = 0;
|
|
44
|
+
for (const part of p.parts) {
|
|
45
|
+
const span = weight(part) / total;
|
|
46
|
+
cut(part, p.horizontal ? patch(r, offset, 0, span, 1) : patch(r, 0, offset, 1, span));
|
|
47
|
+
offset += span;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
cut(pattern, rect);
|
|
51
|
+
return tiles;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function terminal(weights, rect) {
|
|
55
|
+
let best, bestScore = Infinity;
|
|
56
|
+
for (const pattern of templates[weights.length]) {
|
|
57
|
+
const tiles = tile(pattern, weights, rect);
|
|
58
|
+
// Minimizing the sum is equivalent to minimizing the mean for this n.
|
|
59
|
+
const score = tiles.reduce((sum, r) => {
|
|
60
|
+
const aspect = widthOf(r) / heightOf(r);
|
|
61
|
+
return sum + Math.max(aspect, 1 / aspect);
|
|
62
|
+
}, 0);
|
|
63
|
+
if (!best || score < bestScore * (1 - 1e-12)) {
|
|
64
|
+
bestScore = score;
|
|
65
|
+
best = pattern === horizontalSplit || pattern === verticalSplit ? tiles.map(turn) : tiles;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return best;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const massOf = nodes => nodes.reduce((sum, n) => sum + n.mass, 0);
|
|
72
|
+
|
|
73
|
+
function quadrants(nodes) {
|
|
74
|
+
// Cut the ordered prefix sums nearest 1/4, 1/2 and 3/4 of total mass.
|
|
75
|
+
// Never split an item, and leave at least one item for each remaining arm.
|
|
76
|
+
const total = massOf(nodes), groups = [];
|
|
77
|
+
let start = 0, sum = 0;
|
|
78
|
+
for (let i = 1; i <= 3; i++) {
|
|
79
|
+
const target = total * (i / 4);
|
|
80
|
+
let end = start + 1;
|
|
81
|
+
sum += nodes[start].mass;
|
|
82
|
+
while (end < nodes.length - (4 - i) && Math.abs(sum + nodes[end].mass - target) < Math.abs(sum - target)) {
|
|
83
|
+
sum += nodes[end++].mass;
|
|
84
|
+
}
|
|
85
|
+
groups.push(nodes.slice(start, end));
|
|
86
|
+
start = end;
|
|
87
|
+
}
|
|
88
|
+
groups.push(nodes.slice(start));
|
|
89
|
+
return groups;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function distribute(nodes, rect, place) {
|
|
93
|
+
if (!nodes.length) return;
|
|
94
|
+
if (nodes.length <= 4) {
|
|
95
|
+
const tiles = terminal(nodes.map(n => n.mass), rect);
|
|
96
|
+
nodes.forEach((node, i) => place(node, tiles[i]));
|
|
97
|
+
} else {
|
|
98
|
+
const groups = quadrants(nodes);
|
|
99
|
+
const tiles = tile(horizontalSplit, groups.map(massOf), rect);
|
|
100
|
+
groups.forEach((group, i) => distribute(group, turn(tiles[i], i), place));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function inset(r, distance) {
|
|
105
|
+
if (!(distance > 0)) return r;
|
|
106
|
+
const w = widthOf(r), h = heightOf(r), d = Math.min(distance, w / 6, h / 6);
|
|
107
|
+
return patch(r, d/w, d/h, 1 - 2*d/w, 1 - 2*d/h);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function partitionLots(roots, options) {
|
|
111
|
+
const lots = new Map(), anchors = new Map();
|
|
112
|
+
const size = options.size > 0 ? options.size : 8;
|
|
113
|
+
function place(node, rect) {
|
|
114
|
+
const r = inset(rect, options.separation / 2);
|
|
115
|
+
const x = r.x + r.ux + r.vx, y = r.y + r.uy + r.vy;
|
|
116
|
+
const left = Math.min(x, r.x), right = Math.max(x, r.x), bottom = Math.min(y, r.y), top = Math.max(y, r.y);
|
|
117
|
+
// Consistent CCW winding in world coordinates for morphing and picking.
|
|
118
|
+
const polygon = [{x: left, y: bottom}, {x: right, y: bottom}, {x: right, y: top}, {x: left, y: top}];
|
|
119
|
+
lots.set(node.key, {polygon, mass: node.mass});
|
|
120
|
+
anchors.set(node.key, {x: (left + right) / 2, y: (bottom + top) / 2});
|
|
121
|
+
if (node.children) distribute(node.children, inset(r, options.border), place);
|
|
122
|
+
}
|
|
123
|
+
distribute(roots, {x: -size/2, y: size/2, ux: size, uy: 0, vx: 0, vy: -size}, place);
|
|
124
|
+
return {lots, anchors};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
class HilbertPlan extends PeakLotPlan {
|
|
128
|
+
constructor(options) { super(options, partitionLots); }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
module.exports = {HilbertPlan};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// Pfahler et al., Visualizing Evolving Software Cities (2020), §III, Fig. 4.
|
|
2
|
+
// Pack the union of maximum footprints, bottom-up, before placing a revision.
|
|
3
|
+
const {byKey, positive, snapshot} = require('../voronoi/layout');
|
|
4
|
+
|
|
5
|
+
// Next-fit decreasing-height shelf packing. Pack the children of a package
|
|
6
|
+
// first; their enclosing rectangle is one item in its parent's bin. No item
|
|
7
|
+
// is rotated, so mapped widths and depths keep their axes at every level.
|
|
8
|
+
function pack(roots, options) {
|
|
9
|
+
const size = options.size > 0 ? options.size : 8;
|
|
10
|
+
const floorArea = nodes => nodes.sort(byKey).reduce((sum, n) =>
|
|
11
|
+
sum + (n.children ? floorArea(n.children) || 0.5 : n.width * n.depth), 0);
|
|
12
|
+
// Express nominal street widths in metric units, then fit the whole plan
|
|
13
|
+
// uniformly. Changing a dataset's unit of measure need not widen its streets.
|
|
14
|
+
const unit = Math.sqrt(floorArea(roots)) / size;
|
|
15
|
+
const gap = Math.max(0, options.separation) * unit, border = Math.max(0, options.border) * unit;
|
|
16
|
+
function shelves(nodes, rim) {
|
|
17
|
+
const boxes = nodes.map(({key, children, width, depth}) =>
|
|
18
|
+
children ? {key, ...shelves(children, border)} : {key, width, depth});
|
|
19
|
+
boxes.sort((a, b) => b.depth - a.depth || b.width - a.width || byKey(a, b));
|
|
20
|
+
let area = 0, widest = 0;
|
|
21
|
+
for (const b of boxes) {
|
|
22
|
+
area += (b.width + gap) * (b.depth + gap);
|
|
23
|
+
widest = Math.max(widest, b.width);
|
|
24
|
+
}
|
|
25
|
+
const target = Math.max(widest, Math.sqrt(area));
|
|
26
|
+
let x = 0, z = 0, rowDepth = 0, width = 0;
|
|
27
|
+
for (const b of boxes) {
|
|
28
|
+
if (x && x + b.width > target) {
|
|
29
|
+
z += rowDepth + gap;
|
|
30
|
+
x = rowDepth = 0;
|
|
31
|
+
}
|
|
32
|
+
b.left = rim + x;
|
|
33
|
+
b.top = rim + z;
|
|
34
|
+
width = Math.max(width, x + b.width);
|
|
35
|
+
rowDepth = Math.max(rowDepth, b.depth);
|
|
36
|
+
x += b.width + gap;
|
|
37
|
+
}
|
|
38
|
+
// An explicitly empty directory still has a selectable base.
|
|
39
|
+
return {boxes, width: (width || Math.SQRT1_2) + 2 * rim, depth: (z + rowDepth || Math.SQRT1_2) + 2 * rim};
|
|
40
|
+
}
|
|
41
|
+
const root = shelves(roots, 0), scale = size / Math.max(root.width, root.depth);
|
|
42
|
+
const lots = new Map();
|
|
43
|
+
function place(boxes, left, top) {
|
|
44
|
+
for (const b of boxes) {
|
|
45
|
+
const x = left + b.left, z = top + b.top;
|
|
46
|
+
lots.set(b.key, {center: {x: (x + b.width / 2) * scale, y: (z + b.depth / 2) * scale},
|
|
47
|
+
width: b.width, depth: b.depth});
|
|
48
|
+
if (b.boxes) place(b.boxes, x, z);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
place(root.boxes, -root.width / 2, -root.depth / 2);
|
|
52
|
+
return {lots, scale};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function rectangle({x, y}, w, d) {
|
|
56
|
+
return [{x: x - w / 2, y: y - d / 2}, {x: x + w / 2, y: y - d / 2},
|
|
57
|
+
{x: x + w / 2, y: y + d / 2}, {x: x - w / 2, y: y + d / 2}];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
class HistoryResistantPlan {
|
|
61
|
+
constructor(options) {
|
|
62
|
+
this.options = {time_evolution_reserve_space: true, ...options};
|
|
63
|
+
this.slots = new Map();
|
|
64
|
+
this.roots = [];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
invalidateGeometry() {
|
|
68
|
+
this.atlas = null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// planFor calls observe for every loaded date before the first frame. For
|
|
72
|
+
// an open stream the same pass learns only arrivals and new maxima. Keep
|
|
73
|
+
// addresses and capacities, never a retained copy of each version's tree.
|
|
74
|
+
observe(records) {
|
|
75
|
+
const d = this.options, live = snapshot(records, d);
|
|
76
|
+
const merge = (nodes, siblings) => {
|
|
77
|
+
for (const n of nodes) {
|
|
78
|
+
let slot = this.slots.get(n.key);
|
|
79
|
+
if (!slot) {
|
|
80
|
+
slot = {key: n.key, children: n.children ? [] : null, width: 0, depth: 0};
|
|
81
|
+
siblings.push(slot);
|
|
82
|
+
this.slots.set(n.key, slot);
|
|
83
|
+
this.invalidateGeometry();
|
|
84
|
+
}
|
|
85
|
+
if (n.children) merge(n.children, slot.children);
|
|
86
|
+
else {
|
|
87
|
+
n.width = d.area ? Math.sqrt(n.mass) : positive(n.data[d.width]);
|
|
88
|
+
n.depth = d.area ? n.width : positive(n.data[d.depth]);
|
|
89
|
+
// A wide, shallow revision and a later narrow, deep one
|
|
90
|
+
// must both fit at this centre. Peak area alone cannot do that.
|
|
91
|
+
if (n.width > slot.width || n.depth > slot.depth) {
|
|
92
|
+
slot.width = Math.max(slot.width, n.width);
|
|
93
|
+
slot.depth = Math.max(slot.depth, n.depth);
|
|
94
|
+
this.invalidateGeometry();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
merge(live.roots, this.roots);
|
|
100
|
+
return live;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
frame(records) {
|
|
104
|
+
const d = this.options, {roots, range} = this.observe(records);
|
|
105
|
+
const cells = [], anchors = new Map();
|
|
106
|
+
if (!roots.length) return {cells, anchors, range};
|
|
107
|
+
if (d.time_evolution_reserve_space && !this.atlas) this.atlas = pack(this.roots, d);
|
|
108
|
+
const plan = d.time_evolution_reserve_space ? this.atlas : pack(roots, d);
|
|
109
|
+
const elevation = Math.max(0, d.zone_elevation), low = d.diffBuildingHeight ? range.min : 0;
|
|
110
|
+
const min = Math.max(0, d.minBuildingHeight), span = Math.max(0, d.maxBuildingHeight - min);
|
|
111
|
+
const visit = (nodes, base) => {
|
|
112
|
+
for (const n of nodes) {
|
|
113
|
+
const {key, id, data, children, metric} = n, quarter = !!children;
|
|
114
|
+
const lot = plan.lots.get(key), {width, depth} = quarter ? lot : n;
|
|
115
|
+
const polygon = rectangle(lot.center, width * plan.scale, depth * plan.scale);
|
|
116
|
+
const fraction = metric > low && range.max > low ? (metric - low) / (range.max - low) : 0;
|
|
117
|
+
const height = quarter ? elevation : min + span * fraction;
|
|
118
|
+
anchors.set(key, lot.center);
|
|
119
|
+
cells.push({key, id, data, quarter, polygon, base, height});
|
|
120
|
+
if (quarter) visit(children, base + elevation);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
visit(roots, 0);
|
|
124
|
+
return {cells, anchors, range};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = {HistoryResistantPlan};
|