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,135 @@
|
|
|
1
|
+
const {CellLabel} = require('../voronoi/hover-legend');
|
|
2
|
+
const THREE = AFRAME.THREE;
|
|
3
|
+
const metrics = ['height', 'area', 'width', 'depth', 'color'];
|
|
4
|
+
const DEFAULT_TEXT = '{name}\n{fheight} (height): {height}\n{farea} (area): {area}\n{fwidth} (width): {width}\n{fdepth} (depth): {depth}\n{fcolor} (color): {color}';
|
|
5
|
+
|
|
6
|
+
function legendLines(figure, options) {
|
|
7
|
+
if (figure.children) return [String(figure.name)];
|
|
8
|
+
const raw = figure.rawData;
|
|
9
|
+
const values = {name: figure.name};
|
|
10
|
+
for (const metric of metrics) {
|
|
11
|
+
const field = options[metric], value = raw[field];
|
|
12
|
+
values['f' + metric] = field || '';
|
|
13
|
+
values[metric] = metric !== 'color' && typeof value === 'number'
|
|
14
|
+
? Math.round(value * 100) / 100 : value ?? '';
|
|
15
|
+
}
|
|
16
|
+
const lines = options.legend_text.split('\n');
|
|
17
|
+
return lines.filter((line, i) => {
|
|
18
|
+
if (!i || options.legend_text !== DEFAULT_TEXT) return true;
|
|
19
|
+
const key = metrics[i - 1];
|
|
20
|
+
return options[key] && !(options.area && (key === 'width' || key === 'depth'));
|
|
21
|
+
}).map(line => line.replace(/\{(\w+)\}/g, (token, key) =>
|
|
22
|
+
Object.prototype.hasOwnProperty.call(values, key) ? values[key] : raw[key] ?? token)).join('\n').split('\n');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// The panel lives in the scene. The lot supplies an anchor, never a parent transform.
|
|
26
|
+
class BoatLabel {
|
|
27
|
+
constructor() {
|
|
28
|
+
this.host = document.createElement('a-entity');
|
|
29
|
+
this.host.classList.add('babialegend');
|
|
30
|
+
this.label = new CellLabel(THREE, this.host.object3D);
|
|
31
|
+
this.label.sprite.material.sizeAttenuation = true; // World units, with normal perspective in each eye.
|
|
32
|
+
this.point = new THREE.Vector3();
|
|
33
|
+
this.lotScale = new THREE.Vector3();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
configure(boats) {
|
|
37
|
+
const d = boats.data, figure = this.entity.babiaFigure;
|
|
38
|
+
const scene = boats.el.sceneEl;
|
|
39
|
+
if (this.host.parentElement !== scene) scene.appendChild(this.host);
|
|
40
|
+
this.host.object3D.scale.setScalar(d.legend_scale);
|
|
41
|
+
if (this.figure !== figure || this.template !== d.legend_text) {
|
|
42
|
+
this.figure = figure;
|
|
43
|
+
this.template = d.legend_text;
|
|
44
|
+
const lines = legendLines(figure, d);
|
|
45
|
+
this.label.paint(lines);
|
|
46
|
+
// Keep boats' original height (0.3 units per line plus padding)
|
|
47
|
+
// and the canvas aspect ratio, independently of legend_scale.
|
|
48
|
+
const height = 0.3 * (lines.length + 1), scale = this.label.sprite.scale;
|
|
49
|
+
scale.set(height * scale.x / scale.y, height, 1);
|
|
50
|
+
}
|
|
51
|
+
// A sprite always faces the camera; an explicitly different look-at
|
|
52
|
+
// target needs an oriented surface with the same texture instead.
|
|
53
|
+
const custom = d.legend_lookat && d.legend_lookat !== '[camera]';
|
|
54
|
+
if (custom) {
|
|
55
|
+
if (!this.plane) {
|
|
56
|
+
this.plane = new THREE.Mesh(new THREE.PlaneGeometry(1, 1).translate(0, 0.57, 0),
|
|
57
|
+
new THREE.MeshBasicMaterial({map: this.label.texture, side: THREE.DoubleSide,
|
|
58
|
+
transparent: true, depthTest: false, depthWrite: false, toneMapped: false}));
|
|
59
|
+
this.plane.raycast = () => {};
|
|
60
|
+
this.host.object3D.add(this.plane);
|
|
61
|
+
}
|
|
62
|
+
this.host.setAttribute('babia-lookat', d.legend_lookat);
|
|
63
|
+
this.plane.scale.copy(this.label.sprite.scale);
|
|
64
|
+
this.plane.name = this.label.sprite.name;
|
|
65
|
+
} else if (this.host.hasAttribute('babia-lookat')) {
|
|
66
|
+
this.host.removeAttribute('babia-lookat');
|
|
67
|
+
this.host.object3D.rotation.set(0, 0, 0);
|
|
68
|
+
}
|
|
69
|
+
this.label.sprite.visible = !custom;
|
|
70
|
+
this.label.sprite.renderOrder = figure.children ? 999 : 1000;
|
|
71
|
+
if (this.plane) {
|
|
72
|
+
this.plane.visible = !!custom;
|
|
73
|
+
this.plane.renderOrder = this.label.sprite.renderOrder;
|
|
74
|
+
}
|
|
75
|
+
const specified = boats.el.getDOMAttribute('babia-boats') || {};
|
|
76
|
+
this.fixedTitle = d.height_quarter_legend_title !== 12 || Object.prototype.hasOwnProperty.call(specified, 'height_quarter_legend_title');
|
|
77
|
+
this.fixedBuilding = d.height_building_legend !== 0 || Object.prototype.hasOwnProperty.call(specified, 'height_building_legend');
|
|
78
|
+
if (figure.children && !d.hideQuarterBoxLegend && !this.box) {
|
|
79
|
+
this.box = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1),
|
|
80
|
+
new THREE.MeshBasicMaterial({color: 'white', transparent: true, opacity: 0.4, depthWrite: false}));
|
|
81
|
+
this.box.raycast = () => {}; // Even unrestricted controller rays pass through the highlight.
|
|
82
|
+
this.entity.object3D.add(this.box);
|
|
83
|
+
this.entity.classList.add('babiaquarterboxactivated');
|
|
84
|
+
} else if (d.hideQuarterBoxLegend) this.removeBox();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
place(options, hitPoint) {
|
|
88
|
+
const host = this.host.object3D, target = this.entity.object3D;
|
|
89
|
+
const geometry = this.entity.getAttribute('geometry');
|
|
90
|
+
if (!geometry || !host.parent) return;
|
|
91
|
+
if (this.box) {
|
|
92
|
+
target.getWorldScale(this.lotScale);
|
|
93
|
+
this.box.scale.set(geometry.width, geometry.height + options.height_quarter_legend_box / (this.lotScale.y || 1), geometry.depth);
|
|
94
|
+
}
|
|
95
|
+
const quarter = !!this.figure.children;
|
|
96
|
+
if (options.legendsAsChildren) {
|
|
97
|
+
target.localToWorld(this.point.set(0, options.legendsAsChildrenHeight, 0));
|
|
98
|
+
} else if (hitPoint && !quarter && !this.fixedBuilding) {
|
|
99
|
+
this.point.copy(hitPoint);
|
|
100
|
+
} else {
|
|
101
|
+
target.localToWorld(this.point.set(0, geometry.height / 2, 0));
|
|
102
|
+
if (quarter && this.fixedTitle) this.point.y = options.height_quarter_legend_title;
|
|
103
|
+
if (!quarter && this.fixedBuilding) this.point.y += 1 + options.height_building_legend;
|
|
104
|
+
}
|
|
105
|
+
host.position.copy(host.parent.worldToLocal(this.point));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
removeBox() {
|
|
109
|
+
if (!this.box) return;
|
|
110
|
+
this.box.parent.remove(this.box);
|
|
111
|
+
this.box.material.dispose();
|
|
112
|
+
this.box.geometry.dispose();
|
|
113
|
+
this.entity.classList.remove('babiaquarterboxactivated');
|
|
114
|
+
this.box = null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
detach() {
|
|
118
|
+
this.removeBox();
|
|
119
|
+
this.label.hide();
|
|
120
|
+
if (this.host.parentElement) this.host.remove(); // A pooled hover may already be detached.
|
|
121
|
+
this.entity = this.figure = null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
dispose() {
|
|
125
|
+
this.detach();
|
|
126
|
+
if (this.plane) {
|
|
127
|
+
this.host.object3D.remove(this.plane);
|
|
128
|
+
this.plane.geometry.dispose();
|
|
129
|
+
this.plane.material.dispose();
|
|
130
|
+
}
|
|
131
|
+
this.label.dispose();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
module.exports = {BoatLabel, DEFAULT_TEXT};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const {snapshot} = require('../voronoi/layout');
|
|
2
|
+
const {Packing} = require('./layout');
|
|
3
|
+
const {enclose} = require('./rectangles');
|
|
4
|
+
|
|
5
|
+
class EvoCellsPlan {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.options = {...options};
|
|
8
|
+
this.frames = [];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
advance(roots) {
|
|
12
|
+
if (!this.current) {
|
|
13
|
+
if (!roots.length) return;
|
|
14
|
+
const d = this.options, size = d.size > 0 ? d.size : 8;
|
|
15
|
+
// Express nominal street widths in the metric units of the seed.
|
|
16
|
+
// The final camera fit then scales the entire city uniformly, once.
|
|
17
|
+
const unit = Math.sqrt(roots.reduce((s, n) => s + n.mass, 0)) / size;
|
|
18
|
+
const spacing = {halfGap: Math.max(0, d.separation) * unit / 2, border: Math.max(0, d.border) * unit};
|
|
19
|
+
this.current = new Packing(false, spacing);
|
|
20
|
+
this.reserved = new Packing(true, spacing);
|
|
21
|
+
}
|
|
22
|
+
if (this.current.advance(roots)) this.latest = this.current.capture();
|
|
23
|
+
if (this.reserved.advance(roots)) this.peak = this.reserved.capture();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// planFor calls this in chronological order for an already-loaded selector.
|
|
27
|
+
// Store rectangles, not whole hierarchies. A seek never evolves from the
|
|
28
|
+
// user's last visited date or advances a second playback clock.
|
|
29
|
+
observe(records, index) {
|
|
30
|
+
this.advance(snapshot(records, this.options).roots);
|
|
31
|
+
this.frames[index] = this.latest;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
frame(records, revision = this.clock?.selectedIndex) {
|
|
35
|
+
const d = this.options, {roots, range} = snapshot(records, d);
|
|
36
|
+
const archived = Number.isInteger(revision) && revision in this.frames;
|
|
37
|
+
if (!archived) this.advance(roots);
|
|
38
|
+
const reserved = d.time_evolution_reserve_space !== false;
|
|
39
|
+
const frame = reserved ? this.peak : archived ? this.frames[revision] : this.latest;
|
|
40
|
+
const cells = [], keys = new Set();
|
|
41
|
+
if (!frame || !roots.length) return {cells, keys, range};
|
|
42
|
+
if (!this.fit) {
|
|
43
|
+
const r = enclose(this.peak.root, this.latest.root);
|
|
44
|
+
this.fit = {x: r.x + r.w / 2, y: r.y + r.h / 2, k: (d.size > 0 ? d.size : 8) / Math.max(r.w, r.h)};
|
|
45
|
+
}
|
|
46
|
+
const {x, y, k} = this.fit, elevation = Math.max(0, d.zone_elevation);
|
|
47
|
+
const low = d.diffBuildingHeight ? range.min : 0, min = Math.max(0, d.minBuildingHeight),
|
|
48
|
+
span = Math.max(0, d.maxBuildingHeight - min);
|
|
49
|
+
const visit = (nodes, base) => {
|
|
50
|
+
for (const {id, key, data, children, mass, metric} of nodes) {
|
|
51
|
+
const lot = frame.lots.get(key), r = lot.rect, quarter = !!children;
|
|
52
|
+
const side = reserved && !quarter ? Math.sqrt(mass / lot.mass) : 1;
|
|
53
|
+
const dx = r.w * (1 - side) / 2, dy = r.h * (1 - side) / 2;
|
|
54
|
+
const left = (r.x + dx - x) * k, right = (r.x + r.w - dx - x) * k, bottom = (r.y + dy - y) * k,
|
|
55
|
+
top = (r.y + r.h - dy - y) * k;
|
|
56
|
+
const polygon = [{x: left, y: bottom}, {x: right, y: bottom}, {x: right, y: top}, {x: left, y: top}];
|
|
57
|
+
const height = quarter ?
|
|
58
|
+
elevation :
|
|
59
|
+
min + span * (metric > low && range.max > low ? (metric - low) / (range.max - low) : 0);
|
|
60
|
+
keys.add(key);
|
|
61
|
+
cells.push({id, key, data, quarter, polygon, base, height});
|
|
62
|
+
if (quarter) visit(children, base + elevation);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
visit(roots, 0);
|
|
66
|
+
return {cells, keys, range};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
module.exports = {EvoCellsPlan};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// EvoCells, Scheibel, Weyand & Döllner (IVAPP 2018), §§3–4.3.
|
|
2
|
+
// A treemap seeds the map. Subsequent revisions edit the packing; a parent
|
|
3
|
+
// stretches around its children rather than redistributing its area to them.
|
|
4
|
+
const {box, square, pad, squarify, resize, shift, settle, insertion} = require('./rectangles');
|
|
5
|
+
const token = (id, quarter) => JSON.stringify([id, quarter]);
|
|
6
|
+
function count(map, key, value) { map.set(key, map.has(key) ? null : value); }
|
|
7
|
+
|
|
8
|
+
function slotFor(entry, rect, spacing) {
|
|
9
|
+
const n = entry.node;
|
|
10
|
+
return entry.slot = {
|
|
11
|
+
key: n.key,
|
|
12
|
+
id: n.id,
|
|
13
|
+
mass: n.mass,
|
|
14
|
+
box: rect.w ? pad(rect, spacing.halfGap) : rect,
|
|
15
|
+
margin: spacing.border + spacing.halfGap,
|
|
16
|
+
children: n.children ? [] : null,
|
|
17
|
+
parent: null
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function attach(parent, slot) {
|
|
21
|
+
parent.children.push(slot);
|
|
22
|
+
slot.parent = parent;
|
|
23
|
+
}
|
|
24
|
+
function insert(parent, slot) {
|
|
25
|
+
const r = insertion(parent, slot.box);
|
|
26
|
+
shift(slot, r.x - slot.box.x, r.y - slot.box.y);
|
|
27
|
+
attach(parent, slot);
|
|
28
|
+
settle(slot);
|
|
29
|
+
}
|
|
30
|
+
function seedInto(parent, entries, rect, spacing) {
|
|
31
|
+
squarify(entries, rect, (entry, r) => {
|
|
32
|
+
const child = slotFor(entry, r, spacing);
|
|
33
|
+
if (child.children) seedInto(child, entry.children, r, spacing);
|
|
34
|
+
attach(parent, child);
|
|
35
|
+
if (spacing.halfGap || spacing.border) settle(child);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function seed(entry, spacing) {
|
|
39
|
+
const rect = square(entry.node.mass), slot = slotFor(entry, rect, spacing);
|
|
40
|
+
if (slot.children) seedInto(slot, entry.children, rect, spacing);
|
|
41
|
+
return slot;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class Packing {
|
|
45
|
+
constructor(reserved = false, spacing = {halfGap: 0, border: 0}) {
|
|
46
|
+
this.reserved = reserved;
|
|
47
|
+
this.spacing = spacing;
|
|
48
|
+
this.root = {box: box(0, 0, 0, 0), margin: 0, children: [], parent: null}; // Undrawn parent of a forest.
|
|
49
|
+
this.slots = new Map();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
advance(roots) {
|
|
53
|
+
const flat = [], oldUnique = new Map(), newUnique = new Map();
|
|
54
|
+
const wrap = node => {
|
|
55
|
+
const e = {node, seedMass: node.mass, type: token(node.id, !!node.children)};
|
|
56
|
+
flat.push(e);
|
|
57
|
+
count(newUnique, e.type, e);
|
|
58
|
+
if (node.children) e.children = node.children.map(wrap);
|
|
59
|
+
return e;
|
|
60
|
+
};
|
|
61
|
+
const forest = roots.map(wrap);
|
|
62
|
+
if (!this.root.box.w) {
|
|
63
|
+
if (!forest.length) return false;
|
|
64
|
+
this.root.box = square(roots.reduce((sum, n) => sum + n.mass, 0));
|
|
65
|
+
seedInto(this.root, forest, this.root.box, this.spacing);
|
|
66
|
+
for (const e of flat) this.slots.set(e.node.key, e.slot);
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Scoped paths are authoritative. A move can also be recognized by a
|
|
71
|
+
// unique stable ID, or by a child's identity inside a moved directory.
|
|
72
|
+
for (const s of this.slots.values()) {
|
|
73
|
+
s.before = s.box;
|
|
74
|
+
s.oldParent = s.parent;
|
|
75
|
+
if (!this.reserved) count(oldUnique, token(s.id, !!s.children), s);
|
|
76
|
+
}
|
|
77
|
+
const taken = new Set();
|
|
78
|
+
for (const e of flat) {
|
|
79
|
+
e.slot = this.slots.get(e.node.key);
|
|
80
|
+
if (e.slot) taken.add(e.slot);
|
|
81
|
+
}
|
|
82
|
+
const match = (entries, parent) => {
|
|
83
|
+
for (const e of entries) {
|
|
84
|
+
e.parent = parent;
|
|
85
|
+
if (!e.slot && !this.reserved) {
|
|
86
|
+
const sameParent = parent.slot?.children?.find(
|
|
87
|
+
s => !taken.has(s) && s.id === e.node.id && !!s.children === !!e.children);
|
|
88
|
+
const s = sameParent || (newUnique.get(e.type) && oldUnique.get(e.type));
|
|
89
|
+
if (s && !taken.has(s)) {
|
|
90
|
+
e.slot = s;
|
|
91
|
+
taken.add(s);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (e.children) match(e.children, e);
|
|
95
|
+
e.survives = !!e.slot || !!e.children?.some(c => c.survives);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const root = {slot: this.root, children: forest};
|
|
99
|
+
match(forest, root);
|
|
100
|
+
let changed = false;
|
|
101
|
+
if (!this.reserved) {
|
|
102
|
+
// 1. Remove departed addresses; detach moved subtrees as units.
|
|
103
|
+
const destinations = new Map(flat.filter(e => e.slot).map(e => [e.slot, e.parent.slot]));
|
|
104
|
+
for (const p of [this.root, ...this.slots.values()])
|
|
105
|
+
if (p.children) {
|
|
106
|
+
p.children = p.children.filter(s => {
|
|
107
|
+
if (destinations.get(s) === p) return true;
|
|
108
|
+
if (destinations.has(s) && !s.children) s.box = pad(square(s.mass), this.spacing.halfGap);
|
|
109
|
+
s.parent = null;
|
|
110
|
+
changed = true;
|
|
111
|
+
return false;
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// 2. Place moves. A move into a newly created parent is attached
|
|
115
|
+
// when that parent is built, preserving its internal coordinates.
|
|
116
|
+
for (const {slot, parent} of flat)
|
|
117
|
+
if (slot && !slot.parent && parent.slot) insert(parent.slot, slot);
|
|
118
|
+
}
|
|
119
|
+
// 3. Resize leaves about their centres. Shrinkage leaves free space;
|
|
120
|
+
// growth propagates intersections and ancestor stretching immediately.
|
|
121
|
+
for (const e of flat)
|
|
122
|
+
if (e.slot && !e.children) {
|
|
123
|
+
const {slot, node} = e, mass = this.reserved ? Math.max(slot.mass, node.mass) : node.mass;
|
|
124
|
+
if (mass === slot.mass) continue;
|
|
125
|
+
const gap = this.spacing.halfGap;
|
|
126
|
+
slot.box = pad(resize(pad(slot.box, -gap), mass), gap);
|
|
127
|
+
slot.mass = mass;
|
|
128
|
+
settle(slot);
|
|
129
|
+
changed = true;
|
|
130
|
+
}
|
|
131
|
+
// 4. Add squares or entire freshly seeded subtrees. Do not split or
|
|
132
|
+
// rescale existing neighbours just because another file has arrived.
|
|
133
|
+
const add = parent => {
|
|
134
|
+
for (const e of parent.children) {
|
|
135
|
+
if (!e.slot) {
|
|
136
|
+
if (!e.survives)
|
|
137
|
+
seed(e, this.spacing);
|
|
138
|
+
else {
|
|
139
|
+
slotFor(e, box(0, 0, 0, 0), this.spacing);
|
|
140
|
+
add(e);
|
|
141
|
+
}
|
|
142
|
+
changed = true;
|
|
143
|
+
} else if (e.children)
|
|
144
|
+
add(e);
|
|
145
|
+
if (e.slot.parent !== parent.slot) insert(parent.slot, e.slot);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
add(root);
|
|
149
|
+
if (!this.reserved) this.slots.clear();
|
|
150
|
+
for (const {slot, node} of flat) {
|
|
151
|
+
slot.key = node.key;
|
|
152
|
+
this.slots.set(node.key, slot);
|
|
153
|
+
}
|
|
154
|
+
for (const s of this.slots.values()) {
|
|
155
|
+
s.before = null;
|
|
156
|
+
s.oldParent = null;
|
|
157
|
+
}
|
|
158
|
+
return changed;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
capture() {
|
|
162
|
+
// Each changed box is replaced, so immutable endpoints can share boxes
|
|
163
|
+
// between dates without retaining meshes, input records or mutable slots.
|
|
164
|
+
const lots = new Map();
|
|
165
|
+
for (const [key, s] of this.slots) lots.set(key, {rect: pad(s.box, -this.spacing.halfGap), mass: s.mass});
|
|
166
|
+
return {root: this.root.box, lots};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
module.exports = {Packing};
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// World-space packing in metric units: one square unit per unit of leaf weight
|
|
2
|
+
// throughout the evolution. Padding and camera fit are presentation only.
|
|
3
|
+
const box = (x, y, w, h) => ({x, y, w, h});
|
|
4
|
+
const square = mass => box(0, 0, Math.sqrt(mass), Math.sqrt(mass));
|
|
5
|
+
const pad = (r, d) => d ? box(r.x - d, r.y - d, r.w + 2 * d, r.h + 2 * d) : r;
|
|
6
|
+
const right = r => r.x + r.w, top = r => r.y + r.h;
|
|
7
|
+
function enclose(a, b) {
|
|
8
|
+
if (!a.w || !a.h) return b;
|
|
9
|
+
if (!b.w || !b.h) return a;
|
|
10
|
+
const x = Math.min(a.x, b.x), y = Math.min(a.y, b.y);
|
|
11
|
+
return box(x, y, Math.max(right(a), right(b)) - x, Math.max(top(a), top(b)) - y);
|
|
12
|
+
}
|
|
13
|
+
// A fixed epsilon in metric units would suppress displacement in small-valued
|
|
14
|
+
// datasets. Compare at the precision of the actual coordinates instead.
|
|
15
|
+
const tol = (a, b) => 32 * Number.EPSILON * Math.max(Math.abs(a), Math.abs(b));
|
|
16
|
+
const exceeds = (a, b) => Number.isFinite(b) ? a - b > tol(a, b) : a > b;
|
|
17
|
+
const overlap = (lo, hi, min, max) => exceeds(Math.min(hi, max), Math.max(lo, min));
|
|
18
|
+
const intersects = (a, b) => overlap(a.x, right(a), b.x, right(b)) && overlap(a.y, top(a), b.y, top(b));
|
|
19
|
+
|
|
20
|
+
// Squarified seed (also used for wholly new subtrees). This splitting operation
|
|
21
|
+
// is never used to lay out the surviving siblings of an edited rectangle.
|
|
22
|
+
function squarify(items, region, place) {
|
|
23
|
+
const sorted =
|
|
24
|
+
items.filter(n => n.seedMass > 0).sort((a, b) => b.seedMass - a.seedMass || (a.node.key < b.node.key ? -1 : 1));
|
|
25
|
+
let {x, y, w, h} = region;
|
|
26
|
+
for (let i = 0; i < sorted.length;) {
|
|
27
|
+
const vertical = w >= h, edge = vertical ? h : w;
|
|
28
|
+
const worst = (sum, small, large) =>
|
|
29
|
+
Math.max(edge * edge * large / (sum * sum), sum * sum / (edge * edge * small));
|
|
30
|
+
const largest = sorted[i].seedMass;
|
|
31
|
+
let j = i + 1, sum = largest, score = worst(sum, largest, largest);
|
|
32
|
+
while (j < sorted.length) {
|
|
33
|
+
const mass = sorted[j].seedMass, next = worst(sum + mass, mass, largest);
|
|
34
|
+
if (next > score) break;
|
|
35
|
+
sum += mass;
|
|
36
|
+
score = next;
|
|
37
|
+
j++;
|
|
38
|
+
}
|
|
39
|
+
const strip = sum / edge;
|
|
40
|
+
let pos = vertical ? y : x;
|
|
41
|
+
for (; i < j; i++) {
|
|
42
|
+
const span = sorted[i].seedMass / strip;
|
|
43
|
+
place(sorted[i], vertical ? box(x, pos, strip, span) : box(pos, y, span, strip));
|
|
44
|
+
pos += span;
|
|
45
|
+
}
|
|
46
|
+
if (vertical) {
|
|
47
|
+
x += strip;
|
|
48
|
+
w -= strip;
|
|
49
|
+
} else {
|
|
50
|
+
y += strip;
|
|
51
|
+
h -= strip;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Grow the shorter edge (or shrink the longer) until square; beyond that
|
|
57
|
+
// resize both. Neither operation changes the centre of the edited rectangle.
|
|
58
|
+
function resize(r, mass) {
|
|
59
|
+
const side = Math.sqrt(mass), big = Math.max(r.w, r.h), small = Math.min(r.w, r.h);
|
|
60
|
+
const fixed = mass > r.w * r.h ? Math.max(side, big) : Math.min(side, small);
|
|
61
|
+
const other = mass / fixed, long = Math.max(fixed, other), short = Math.min(fixed, other);
|
|
62
|
+
const w = r.w >= r.h ? long : short, h = r.w >= r.h ? short : long;
|
|
63
|
+
return box(r.x + (r.w - w) / 2, r.y + (r.h - h) / 2, w, h);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Translate a directory rigidly, including its internal layout. Boxes are
|
|
67
|
+
// replaced, not mutated: saved historical coordinates may refer to them.
|
|
68
|
+
function shift(slot, dx, dy) {
|
|
69
|
+
const r = slot.box;
|
|
70
|
+
slot.box = box(r.x + dx, r.y + dy, r.w, r.h);
|
|
71
|
+
if (slot.children)
|
|
72
|
+
for (const child of slot.children) shift(child, dx, dy);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Resolve one intersection using the smallest axis-aligned translation. Sweep
|
|
76
|
+
// forward in the OLD order along that axis so the pushed sibling in turn pushes
|
|
77
|
+
// its neighbours. Only intervals that intersect the moving strip participate.
|
|
78
|
+
function push(siblings, source, target) {
|
|
79
|
+
const a = source.box, b = target.box;
|
|
80
|
+
const directions =
|
|
81
|
+
[['x', 1, right(a) - b.x], ['x', -1, right(b) - a.x], ['y', 1, top(a) - b.y], ['y', -1, top(b) - a.y]];
|
|
82
|
+
// If both rectangles survived from the same sibling group, prefer a
|
|
83
|
+
// separating side they already had. Pushing a survivor through the
|
|
84
|
+
// edited cell is not a stable transition, even if that exit is short.
|
|
85
|
+
const oldA = source.before, oldB = target.before;
|
|
86
|
+
const prior = oldA && oldB && source.oldParent === target.oldParent ?
|
|
87
|
+
[
|
|
88
|
+
!exceeds(right(oldA), oldB.x), !exceeds(right(oldB), oldA.x), !exceeds(top(oldA), oldB.y),
|
|
89
|
+
!exceeds(top(oldB), oldA.y)
|
|
90
|
+
] :
|
|
91
|
+
[];
|
|
92
|
+
const choices = prior.some(Boolean) ? directions.filter((_, i) => prior[i]) : directions;
|
|
93
|
+
const [axis, sign, distance] = choices.reduce((best, d) => d[2] < best[2] ? d : best);
|
|
94
|
+
const horizontal = axis === 'x', size = horizontal ? 'w' : 'h';
|
|
95
|
+
const start = r => sign > 0 ? r[axis] : -r[axis] - r[size];
|
|
96
|
+
const translate = (s, dist) => shift(s, horizontal ? dist * sign : 0, horizontal ? 0 : dist * sign);
|
|
97
|
+
const cross = (r, q) => horizontal ? overlap(r.y, top(r), q.y, top(q)) : overlap(r.x, right(r), q.x, right(q));
|
|
98
|
+
const ahead = siblings.filter(s => s !== source && s !== target && start(s.box) >= start(b))
|
|
99
|
+
.sort((s, t) => start(s.box) - start(t.box));
|
|
100
|
+
translate(target, distance);
|
|
101
|
+
const moved = [target];
|
|
102
|
+
for (const s of ahead) {
|
|
103
|
+
const r = s.box, from = start(r);
|
|
104
|
+
let stop = from;
|
|
105
|
+
for (const p of moved)
|
|
106
|
+
if (cross(r, p.box)) stop = Math.max(stop, start(p.box) + p.box[size]);
|
|
107
|
+
if (exceeds(stop, from)) {
|
|
108
|
+
translate(s, stop - from);
|
|
109
|
+
moved.push(s);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Stretch (never compact) enclosing rectangles. A stretched directory can
|
|
115
|
+
// now displace a whole neighbouring directory one level up, up to the root.
|
|
116
|
+
function settle(slot) {
|
|
117
|
+
for (let p = slot.parent; p; slot = p, p = p.parent) {
|
|
118
|
+
for (const other of p.children)
|
|
119
|
+
if (other !== slot && intersects(slot.box, other.box)) push(p.children, slot, other);
|
|
120
|
+
const before = p.box;
|
|
121
|
+
let after = before;
|
|
122
|
+
for (const child of p.children) after = enclose(after, pad(child.box, p.margin));
|
|
123
|
+
if (!exceeds(before.x, after.x) && !exceeds(before.y, after.y) && !exceeds(right(after), right(before)) &&
|
|
124
|
+
!exceeds(top(after), top(before)))
|
|
125
|
+
break;
|
|
126
|
+
p.box = after;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Insert from each of the four sides. Along a side, the skyline only changes
|
|
131
|
+
// at sibling edges. Prefer the least added enclosing area (least whitespace),
|
|
132
|
+
// then perimeter and proximity to the parent centre; ties use the side order.
|
|
133
|
+
function insertion(parent, rect) {
|
|
134
|
+
const {w, h} = rect;
|
|
135
|
+
if (!parent.box.w || !parent.box.h) return box(parent.box.x, parent.box.y, w, h);
|
|
136
|
+
const p = pad(parent.box, -parent.margin);
|
|
137
|
+
let best, bestArea = Infinity, bestPerimeter = Infinity, bestDistance = Infinity;
|
|
138
|
+
for (const horizontal of [true, false]) {
|
|
139
|
+
const pos = horizontal ? 'x' : 'y', dim = horizontal ? 'w' : 'h', tangent = horizontal ? 'y' : 'x',
|
|
140
|
+
span = horizontal ? 'h' : 'w';
|
|
141
|
+
const lo = p[tangent], hi = lo + p[span] - rect[span], a = p[pos], b = a + p[dim];
|
|
142
|
+
const candidates = new Set([lo, hi, (lo + hi) / 2]);
|
|
143
|
+
for (const {box: r} of parent.children) {
|
|
144
|
+
for (const t of [r[tangent] - rect[span], r[tangent] + r[span]])
|
|
145
|
+
if (t >= Math.min(lo, hi) && t <= Math.max(lo, hi)) candidates.add(t);
|
|
146
|
+
}
|
|
147
|
+
for (const t of candidates) {
|
|
148
|
+
let low = a, high = b - rect[dim];
|
|
149
|
+
for (const {box: r} of parent.children)
|
|
150
|
+
if (overlap(t, t + rect[span], r[tangent], r[tangent] + r[span])) {
|
|
151
|
+
low = Math.min(low, r[pos] - rect[dim]);
|
|
152
|
+
high = Math.max(high, r[pos] + r[dim]);
|
|
153
|
+
}
|
|
154
|
+
for (const u of [low, high]) {
|
|
155
|
+
const r = box(horizontal ? u : t, horizontal ? t : u, w, h), outer = enclose(p, r);
|
|
156
|
+
const area = outer.w * outer.h, perimeter = outer.w + outer.h;
|
|
157
|
+
const dist = (2 * r.x + w - 2 * p.x - p.w) ** 2 + (2 * r.y + h - 2 * p.y - p.h) ** 2;
|
|
158
|
+
if (!best || exceeds(bestArea, area) ||
|
|
159
|
+
(!exceeds(area, bestArea) &&
|
|
160
|
+
(exceeds(bestPerimeter, perimeter) ||
|
|
161
|
+
(!exceeds(perimeter, bestPerimeter) && dist < bestDistance)))) {
|
|
162
|
+
best = r;
|
|
163
|
+
bestArea = area;
|
|
164
|
+
bestPerimeter = perimeter;
|
|
165
|
+
bestDistance = dist;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return best;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
module.exports = {
|
|
174
|
+
box,
|
|
175
|
+
square,
|
|
176
|
+
pad,
|
|
177
|
+
enclose,
|
|
178
|
+
squarify,
|
|
179
|
+
resize,
|
|
180
|
+
shift,
|
|
181
|
+
settle,
|
|
182
|
+
insertion
|
|
183
|
+
};
|