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.
Files changed (38) hide show
  1. package/common/city-history.js +53 -0
  2. package/components/visualizers/babia-boats.js +229 -2060
  3. package/components/visualizers/babia-city-evocells.js +189 -0
  4. package/components/visualizers/babia-city-evostreets.js +163 -0
  5. package/components/visualizers/babia-city-gospermap.js +190 -0
  6. package/components/visualizers/babia-city-hexgrid.js +6 -0
  7. package/components/visualizers/babia-city-hilbert.js +5 -0
  8. package/components/visualizers/babia-city-history-resistant.js +5 -0
  9. package/components/visualizers/babia-city-morton.js +5 -0
  10. package/components/visualizers/babia-city-voronoi.js +3 -211
  11. package/components/visualizers/boats/interactions.js +194 -0
  12. package/components/visualizers/boats/layout.js +148 -0
  13. package/components/visualizers/boats/legend.js +135 -0
  14. package/components/visualizers/evocells/history.js +69 -0
  15. package/components/visualizers/evocells/layout.js +170 -0
  16. package/components/visualizers/evocells/rectangles.js +183 -0
  17. package/components/visualizers/evostreets/layout.js +171 -0
  18. package/components/visualizers/gospermap/curve.js +91 -0
  19. package/components/visualizers/gospermap/layout.js +221 -0
  20. package/components/visualizers/gospermap/prisms.js +152 -0
  21. package/components/visualizers/gospermap/regions.js +99 -0
  22. package/components/visualizers/gospermap/selection.js +65 -0
  23. package/components/visualizers/hexgrid/grid.js +70 -0
  24. package/components/visualizers/hexgrid/layout.js +90 -0
  25. package/components/visualizers/hexgrid/prisms.js +71 -0
  26. package/components/visualizers/hilbert/layout.js +131 -0
  27. package/components/visualizers/history-resistant/layout.js +128 -0
  28. package/components/visualizers/morton/layout.js +79 -0
  29. package/components/visualizers/peak-lot-plan.js +72 -0
  30. package/components/visualizers/prism-city.js +226 -0
  31. package/components/visualizers/voronoi/history.js +35 -0
  32. package/components/visualizers/voronoi/hover-legend.js +6 -1
  33. package/components/visualizers/voronoi/layout.js +58 -23
  34. package/dist/aframe-babia-components.js +5872 -3118
  35. package/dist/aframe-babia-components.min.js +1 -1
  36. package/dist/aframe-babia-components.min.js.map +1 -1
  37. package/index.js +7 -0
  38. package/package.json +1 -1
@@ -1,213 +1,5 @@
1
1
  /* global AFRAME */
2
- const {colors, findProdComponent, parseJson} = require('../others/common');
3
- const {NotiBuffer} = require('../../common/noti-buffer');
4
- const {layout, number} = require('./voronoi/layout');
5
- const {CityPrisms} = require('./voronoi/prisms');
6
- const {CellLabel} = require('./voronoi/hover-legend');
7
- const {QuarterSelection} = require('./voronoi/quarter-selection');
8
- const THREE = AFRAME.THREE;
9
- const name = 'babia-city-voronoi';
10
- const controls = new Set(['legend', 'animation', 'animationDuration', 'from', 'data']);
2
+ const prismCity = require('./prism-city');
3
+ const {VoronoiPlan} = require('./voronoi/history');
11
4
 
12
- function setColor(color, cell, range, options) {
13
- const value = cell.data[options.color];
14
- if (cell.quarter) return color.set(options.base_color);
15
- if (!options.color || value == null || value === '') return color.set(options.building_color);
16
- const n = number(value);
17
- if (Number.isFinite(n)) {
18
- const t = range.colorMax > range.colorMin ? (n - range.colorMin) / (range.colorMax - range.colorMin) : 0.5;
19
- return color.setHSL((1 - t) * 0.62, 0.72, 0.48);
20
- }
21
- // Category colours must not depend on the order or presence of other keys.
22
- let hash = 0;
23
- for (const c of String(value)) hash = (31 * hash + c.charCodeAt(0)) >>> 0;
24
- return color.set(colors.get(hash, 'categoric'));
25
- }
26
-
27
- AFRAME.registerComponent(name, {
28
- schema: {
29
- data: {type: 'asset'},
30
- from: {type: 'string'},
31
- field: {type: 'string', default: 'uid'},
32
- width: {type: 'string', default: 'width'},
33
- depth: {type: 'string', default: 'depth'},
34
- area: {type: 'string'},
35
- height: {type: 'string', default: 'height'},
36
- color: {type: 'string'},
37
- minBuildingHeight: {type: 'number', default: 0.03},
38
- maxBuildingHeight: {type: 'number', default: 2},
39
- diffBuildingHeight: {type: 'boolean', default: false},
40
- size: {type: 'number', default: 8},
41
- separation: {type: 'number', default: 0.08},
42
- border: {type: 'number', default: 0.12},
43
- zone_elevation: {type: 'number', default: 0.08},
44
- base_color: {type: 'color', default: '#98e690'},
45
- quarterHighlightColor: {type: 'color', default: '#2867CC'},
46
- building_color: {type: 'color', default: '#E6B9A1'},
47
- legend: {type: 'boolean', default: true},
48
- animation: {type: 'boolean', default: true},
49
- animationDuration: {type: 'number', default: 500}
50
- },
51
-
52
- init: function () {
53
- this.notiBuffer = new NotiBuffer();
54
- this.anchors = new Map();
55
- this.prisms = new CityPrisms(THREE, this.el);
56
- this.group = this.prisms.group;
57
- this.meshes = this.prisms.meshes;
58
- this.el.setObject3D(name, this.group);
59
- this.selection = new QuarterSelection(THREE, this.el.sceneEl.object3D);
60
- this.cursors = new Set();
61
- this.onDisconnect = event => this.forgetCursor(event.currentTarget);
62
- this.onCanvasMove = () => { this.mouseOverCanvas = true; };
63
- this.onCanvasLeave = () => {
64
- this.mouseOverCanvas = false;
65
- this.updateHover();
66
- };
67
- },
68
-
69
- update: function (oldData) {
70
- const {data, from} = this.data;
71
- if (data) {
72
- this.unsubscribe();
73
- if (data !== oldData.data) return this.processData(parseJson(data));
74
- } else if (from !== oldData.from || oldData.data) {
75
- this.unsubscribe();
76
- // Same producers and ID convention as boats; a file URL belongs on queryjson.
77
- const prod = (!from || document.getElementById(from)) && findProdComponent(this.data, this.el);
78
- if (prod && prod.notiBuffer) {
79
- this.prodComponent = prod;
80
- this.subscription = prod.notiBuffer.register(snapshot => this.processData(snapshot));
81
- } else {
82
- console.warn(name + ': no producer found', from || this.el);
83
- this.processData([]);
84
- }
85
- return;
86
- }
87
- if (!this.data.animation || !(this.data.animationDuration > 0)) this.prisms.finish();
88
- if (!this.data.legend) this.hideLegend();
89
- // A tooltip or playback toggle should not solve hundreds of cells again.
90
- const redraw = Object.keys(this.data).some(k => !controls.has(k) && this.data[k] !== oldData[k]);
91
- if (this.newData && redraw) this.draw(false);
92
- },
93
-
94
- processData: function (snapshot) {
95
- this.newData = snapshot;
96
- this.draw(this.data.animation && this.meshes.size > 0);
97
- this.notiBuffer.set(snapshot);
98
- },
99
-
100
- draw: function (animate) {
101
- const records = Array.isArray(this.newData) ? this.newData : (this.newData ? [this.newData] : []);
102
- const {cells, range, anchors} = layout(records, this.data, this.anchors);
103
- this.hideLegend();
104
- const ms = animate && cells.length ? this.data.animationDuration : 0;
105
- this.prisms.set(cells, anchors, Number.isFinite(ms) ? Math.max(0, ms) : 0,
106
- (color, cell) => setColor(color, cell, range, this.data));
107
- this.anchors = anchors;
108
- // Surviving keys reuse their meshes. Clear the tint immediately if the
109
- // quarter is leaving (or became a leaf), not after its departure tween.
110
- const mesh = this.selection.mesh;
111
- if (mesh) {
112
- const cell = mesh.userData.cell;
113
- this.selection.set(cell.quarter && anchors.has(cell.key) ? mesh : null, this.prisms, this.data);
114
- }
115
- },
116
-
117
- play: function () {
118
- const canvas = this.el.sceneEl.canvas;
119
- if (canvas) {
120
- canvas.addEventListener('mousemove', this.onCanvasMove);
121
- canvas.addEventListener('mouseleave', this.onCanvasLeave);
122
- }
123
- },
124
-
125
- pause: function () {
126
- const canvas = this.el.sceneEl.canvas;
127
- if (canvas) {
128
- canvas.removeEventListener('mousemove', this.onCanvasMove);
129
- canvas.removeEventListener('mouseleave', this.onCanvasLeave);
130
- }
131
- for (const cursor of this.cursors) cursor.removeEventListener('controllerdisconnected', this.onDisconnect);
132
- this.cursors.clear();
133
- this.mouseOverCanvas = false;
134
- this.hideLegend();
135
- },
136
-
137
- tick: function (time, delta) {
138
- this.prisms.step(delta);
139
- this.selection.update(this.prisms);
140
- if (this.cursors.size && this.data.legend) this.updateHover();
141
- },
142
-
143
- pickedCell: function (hit) {
144
- const mesh = hit?.object;
145
- const cell = mesh?.parent === this.group && mesh.visible && mesh.userData.cell;
146
- return cell && this.anchors.has(cell.key) ? cell : null;
147
- },
148
-
149
- updateHover: function () {
150
- // All lots share an entity. Reuse the cursors' current intersections
151
- // (no extra raycasts), with the last entering hand taking precedence.
152
- let best;
153
- for (const cursor of this.cursors) {
154
- const mouse = cursor.components.cursor?.data.rayOrigin === 'mouse';
155
- if (mouse && (best || !this.mouseOverCanvas || this.el.sceneEl.is('vr-mode'))) continue;
156
- const ray = cursor.components.raycaster;
157
- const hit = ray?.data.enabled && ray.getIntersection(this.el);
158
- const cell = this.pickedCell(hit);
159
- if (cell && !cell.quarter && hit.point) best = hit;
160
- }
161
- if (!best || !this.data.legend) return this.hideLegend();
162
- if (!this.tooltip) this.tooltip = new CellLabel(THREE, this.el.sceneEl.object3D);
163
- this.tooltip.show(best.object.userData.cell, best.point, this.data);
164
- },
165
-
166
- forgetCursor: function (cursor) {
167
- if (!this.cursors.delete(cursor)) return;
168
- cursor.removeEventListener('controllerdisconnected', this.onDisconnect);
169
- this.updateHover();
170
- },
171
-
172
- hideLegend: function () {
173
- if (this.tooltip) this.tooltip.hide();
174
- },
175
-
176
- events: {
177
- mouseenter: function (event) {
178
- const cursor = event.detail?.cursorEl;
179
- if (!cursor?.components.raycaster) return;
180
- this.cursors.add(cursor);
181
- cursor.addEventListener('controllerdisconnected', this.onDisconnect);
182
- this.updateHover();
183
- },
184
- mouseleave: function (event) {
185
- this.forgetCursor(event.detail?.cursorEl);
186
- },
187
- click: function (event) {
188
- const hit = event.detail?.intersection;
189
- const cell = this.pickedCell(hit);
190
- if (!cell) return;
191
- const {id, data, quarter} = cell, detail = {id, data, quarter};
192
- if (quarter) {
193
- detail.selected = this.selection.mesh !== hit.object;
194
- this.selection.set(detail.selected ? hit.object : null, this.prisms, this.data);
195
- }
196
- this.el.emit('babia-voronoi-select', detail);
197
- }
198
- },
199
-
200
- unsubscribe: function () {
201
- if (this.prodComponent) this.prodComponent.notiBuffer.unregister(this.subscription);
202
- this.prodComponent = null;
203
- },
204
-
205
- remove: function () {
206
- this.unsubscribe();
207
- this.selection.dispose();
208
- this.prisms.clear();
209
- if (this.tooltip) this.tooltip.dispose();
210
- this.anchors.clear();
211
- this.el.removeObject3D(name);
212
- }
213
- });
5
+ AFRAME.registerComponent('babia-city-voronoi', prismCity(VoronoiPlan, 'babia-voronoi-select'));
@@ -0,0 +1,194 @@
1
+ const {BoatLabel} = require('./legend');
2
+
3
+ // State belongs to one chart. Cursor events target individual boxes; capture
4
+ // also handles non-bubbling events without installing listeners on every lot.
5
+ class BoatInteractions {
6
+ constructor(boats) {
7
+ this.boats = boats;
8
+ this.labels = new Map();
9
+ this.hovered = null;
10
+ this.cursors = new Map();
11
+ this.onEnter = e => {
12
+ if (!this.isLot(e.target) || e.target.babiaFigure.children) return;
13
+ const cursor = e.detail?.cursorEl;
14
+ this.cursors.set(cursor, e.target);
15
+ cursor?.addEventListener('controllerdisconnected', this.onDisconnect);
16
+ this.update();
17
+ };
18
+ this.onLeave = e => {
19
+ const cursor = e.detail?.cursorEl;
20
+ if (this.cursors.get(cursor) === e.target) this.forget(cursor);
21
+ };
22
+ this.onDisconnect = e => this.forget(e.currentTarget);
23
+ this.onMouseMove = () => { this.mouseOnCanvas = true; };
24
+ this.onCanvasLeave = () => { this.mouseOnCanvas = false; this.update(); };
25
+ this.onClick = e => {
26
+ if (!this.isLot(e.target) || e.target.babiaFigure.dontAddEvents) return;
27
+ this.togglePin(e.target);
28
+ };
29
+ }
30
+
31
+ isLot(entity) { return this.boats.boxes.get(entity.id) === entity; }
32
+
33
+ play() {
34
+ const el = this.boats.el;
35
+ el.addEventListener('mouseenter', this.onEnter, true);
36
+ el.addEventListener('mouseleave', this.onLeave, true);
37
+ el.addEventListener('click', this.onClick, true);
38
+ this.canvas = el.sceneEl.canvas;
39
+ this.canvas?.addEventListener('mousemove', this.onMouseMove);
40
+ this.canvas?.addEventListener('mouseleave', this.onCanvasLeave);
41
+ }
42
+
43
+ pause() {
44
+ const el = this.boats.el;
45
+ el.removeEventListener('mouseenter', this.onEnter, true);
46
+ el.removeEventListener('mouseleave', this.onLeave, true);
47
+ el.removeEventListener('click', this.onClick, true);
48
+ this.canvas?.removeEventListener('mousemove', this.onMouseMove);
49
+ this.canvas?.removeEventListener('mouseleave', this.onCanvasLeave);
50
+ for (const cursor of this.cursors.keys()) cursor?.removeEventListener('controllerdisconnected', this.onDisconnect);
51
+ this.cursors.clear();
52
+ this.mouseOnCanvas = false;
53
+ this.setHover(null);
54
+ }
55
+
56
+ dispose() {
57
+ this.pause();
58
+ for (const entity of this.labels.keys()) this.drop(entity);
59
+ this.spare?.dispose();
60
+ this.spare = null;
61
+ }
62
+
63
+ forget(cursor) {
64
+ this.cursors.delete(cursor);
65
+ cursor?.removeEventListener('controllerdisconnected', this.onDisconnect);
66
+ this.update();
67
+ }
68
+
69
+ update() {
70
+ if (!this.cursors.size && !this.labels.size) return;
71
+ let best = null, point;
72
+ const scene = this.boats.el.sceneEl;
73
+ for (const [cursor, entity] of this.cursors) {
74
+ const ray = cursor?.components.raycaster;
75
+ const mouse = cursor?.components.cursor?.data.rayOrigin === 'mouse';
76
+ if (mouse && (best || !this.mouseOnCanvas || scene.is('vr-mode') || scene.is('ar-mode'))) continue;
77
+ const hit = ray?.data.enabled && ray.getIntersection(entity);
78
+ if (cursor && !hit) continue;
79
+ best = entity;
80
+ point = hit?.point;
81
+ }
82
+ this.setHover(best);
83
+ for (const [entity, label] of this.labels) {
84
+ if (label.figure !== entity.babiaFigure) label.configure(this.boats);
85
+ label.place(this.boats.data, entity === best && !entity.alreadyActive ? point : null);
86
+ }
87
+ }
88
+
89
+ setHover(next) {
90
+ if (this.hovered === next) return;
91
+ const previous = this.hovered;
92
+ this.hovered = next;
93
+ if (previous && !previous.alreadyActive) this.close(previous, true);
94
+ if (next) this.open(next);
95
+ }
96
+
97
+ open(entity) {
98
+ if (this.labels.has(entity)) return this.labels.get(entity);
99
+ // Sweeping a ray across roofs reuses the canvas/texture and host.
100
+ const label = this.spare || new BoatLabel();
101
+ this.spare = null;
102
+ label.entity = entity;
103
+ this.labels.set(entity, label);
104
+ entity.legend = label.host;
105
+ label.configure(this.boats);
106
+ this.highlightGroup(entity);
107
+ return label;
108
+ }
109
+
110
+ close(entity, reuse = false) {
111
+ const label = this.labels.get(entity);
112
+ if (!label) return;
113
+ this.labels.delete(entity);
114
+ entity.legend = undefined;
115
+ if (reuse && !this.spare) {
116
+ label.detach();
117
+ this.spare = label;
118
+ } else label.dispose();
119
+ this.highlightGroup(entity);
120
+ }
121
+
122
+ togglePin(entity) {
123
+ if (entity.alreadyActive) {
124
+ this.drop(entity);
125
+ } else {
126
+ if (entity.highlightbuildingbyfieldactive) return;
127
+ entity.alreadyActive = true;
128
+ const label = this.open(entity);
129
+ // Existing experiment scenes use this list and entity.click() to close pins.
130
+ this.boats.entitiesWithLegend.push(label);
131
+ this.boats.legendsActive.push(label.host);
132
+ this.highlightQuarter(entity);
133
+ }
134
+ this.update();
135
+ }
136
+
137
+ drop(entity) {
138
+ for (const [cursor, target] of this.cursors) if (target === entity) {
139
+ cursor?.removeEventListener('controllerdisconnected', this.onDisconnect);
140
+ this.cursors.delete(cursor);
141
+ }
142
+ if (this.hovered === entity) this.hovered = null;
143
+ if (entity.alreadyActive) {
144
+ entity.alreadyActive = false;
145
+ this.boats.entitiesWithLegend = this.boats.entitiesWithLegend.filter(item => item.entity !== entity);
146
+ this.boats.legendsActive = this.boats.legendsActive.filter(host => host !== entity.legend);
147
+ this.highlightQuarter(entity);
148
+ }
149
+ this.close(entity);
150
+ }
151
+
152
+ highlightQuarter(entity) {
153
+ const quarter = entity.babiaFigure.children ? entity : entity.parentElement;
154
+ if (!quarter?.babiaFigure?.children) return;
155
+ const d = this.boats.data;
156
+ const lifted = d.highlightQuarter && this.boats.entitiesWithLegend.some(({entity}) =>
157
+ !entity.babiaFigure.children && entity.parentElement === quarter);
158
+ const lift = lifted ? 0.2 : 0;
159
+ quarter.object3D.position.y += lift - (quarter.boatLift || 0);
160
+ quarter.boatLift = lift;
161
+ quarter.setAttribute('color', lifted || (quarter.alreadyActive && d.highlightQuarterByClick)
162
+ ? '#bfbfbf' : quarter.getAttribute('babiaxrFirstColor'));
163
+ }
164
+
165
+ highlightGroup(entity) {
166
+ if (entity.babiaFigure.children) return;
167
+ const field = this.boats.data.highlightBuildingByField;
168
+ if (!field) return this.highlightBuilding(entity, false);
169
+ const value = entity.babiaRawData[field];
170
+ const active = Array.from(this.labels.keys()).some(other => !other.babiaFigure.children && other.babiaRawData[field] === value);
171
+ for (const other of this.boats.boxes.values()) {
172
+ if (!other.babiaFigure.children && other.babiaRawData[field] === value) this.highlightBuilding(other, active);
173
+ }
174
+ }
175
+
176
+ highlightBuilding(entity, related) {
177
+ const selected = this.labels.has(entity);
178
+ entity.highlightbuildingbyfieldactive = related && !selected;
179
+ const color = selected ? 'white' : related ? this.boats.data.highlightBuildingByFieldColor : entity.getAttribute('babiaxrFirstColor');
180
+ entity.setAttribute('color', color);
181
+ this.sizeHighlight(entity, selected);
182
+ }
183
+
184
+ sizeHighlight(entity, selected) {
185
+ const mesh = entity.getObject3D('mesh');
186
+ if (!mesh) return;
187
+ if (!selected) return mesh.scale.set(1, 1, 1);
188
+ // Grow only the visible mesh, never mutate the data geometry on hover.
189
+ const {width, height, depth} = entity.getAttribute('geometry');
190
+ mesh.scale.set(1 + (width > 0 ? 0.1 / width : 0), 1 + (height > 0 ? 0.1 / height : 0), 1 + (depth > 0 ? 0.1 / depth : 0));
191
+ }
192
+ }
193
+
194
+ module.exports = {BoatInteractions};
@@ -0,0 +1,148 @@
1
+ // Pack siblings around the four sides of the existing lots, keeping the
2
+ // original boats order and per-level padding. Coordinates here are X/Z seen
3
+ // in plan view; drawElements converts them to A-Frame's axes.
4
+ function pack(figures, padding, separation) {
5
+ let up = 0, down = 0, left = 0, right = 0;
6
+ let maxUp = 0, maxDown = 0, maxLeft = 0, maxRight = 0;
7
+ let x = 0, z = 0, along = 0, side = 'right';
8
+ for (const [i, f] of figures.entries()) {
9
+ const w = f.width, d = f.depth;
10
+ if (i === 0) {
11
+ up = d / 2; down = -up; right = w / 2; left = -right;
12
+ along = up;
13
+ } else if (f.height > 0) {
14
+ switch (side) {
15
+ case 'right':
16
+ x = right + w / 2; z = along - d / 2; along -= d;
17
+ maxRight = Math.max(maxRight, right + w);
18
+ if (along < down) { maxDown = along; up = Math.max(up, maxUp); along = right; side = 'down'; }
19
+ break;
20
+ case 'down':
21
+ x = along - w / 2; z = down - d / 2; along -= w;
22
+ maxDown = Math.min(maxDown, down - d);
23
+ if (along < left) { maxLeft = along; right = Math.max(right, maxRight); along = down; side = 'left'; }
24
+ break;
25
+ case 'left':
26
+ x = left - w / 2; z = along + d / 2; along += d;
27
+ maxLeft = Math.min(maxLeft, left - w);
28
+ if (along > up) { maxUp = along; down = Math.min(down, maxDown); along = left; side = 'up'; }
29
+ break;
30
+ case 'up':
31
+ x = along + w / 2; z = up + d / 2; along += w;
32
+ maxUp = Math.max(maxUp, up + d);
33
+ if (along > right) { maxRight = along; left = Math.min(left, maxLeft); along = up; side = 'right'; }
34
+ break;
35
+ }
36
+ }
37
+ f.posX = x; f.posY = z;
38
+ f.width -= separation; f.depth -= separation;
39
+ }
40
+ down = Math.min(down, maxDown); left = Math.min(left, maxLeft);
41
+ const width = Math.max(right, maxRight) - left + 2 * padding;
42
+ const depth = Math.max(up, maxUp) - down + 2 * padding;
43
+ return {width, depth, translate_matrix: {x: left + width / 2 - padding, y: 0, z: down + depth / 2 - padding}, children: figures};
44
+ }
45
+
46
+ function collectMetadata(items, options, id) {
47
+ const meta = {id, maxLevels: 0};
48
+ const metrics = ['height', ...(options.area ? ['area'] : ['width', 'depth'])];
49
+ if (options.color) metrics.push('color');
50
+ const ranges = metrics.map(() => ({sum: 0, count: 0}));
51
+ function visit(rows, level) {
52
+ meta.maxLevels = Math.max(meta.maxLevels, level);
53
+ for (const row of rows) {
54
+ if (row.children) { visit(row.children, level + 1); continue; }
55
+ metrics.forEach((metric, i) => {
56
+ const value = row[options[metric]], r = ranges[i];
57
+ if (value == null || value === 'null') return;
58
+ if (!r.count || value < r.min) r.min = value;
59
+ if (!r.count || value > r.max) r.max = value;
60
+ r.sum += Number(value); r.count++;
61
+ });
62
+ }
63
+ }
64
+ visit(items, 0);
65
+ metrics.forEach((metric, i) => {
66
+ const r = ranges[i];
67
+ meta[metric === 'color' ? 'color_max' : metric + 'Max'] = r.max;
68
+ meta[metric === 'color' ? 'color_min' : metric + 'Min'] = r.min;
69
+ meta[metric + 'Avg'] = Math.round(r.sum / r.count);
70
+ });
71
+ return meta;
72
+ }
73
+
74
+ function normalize(min, max, low, high, value) {
75
+ return max > min && Number.isFinite(Number(value)) ? low + (value - min) / (max - min) * (high - low) : low;
76
+ }
77
+
78
+ function buildingHeight(value, d, meta) {
79
+ return normalize(d.diffBuildingHeight ? meta.heightMin : 0, meta.heightMax,
80
+ d.minBuildingHeight, d.maxBuildingHeight, value);
81
+ }
82
+
83
+ function positive(value) { return Number.isFinite(value) && value > 0 ? value : 0; }
84
+
85
+ function makeFigures(items, d, meta, scaleY, categories, palette) {
86
+ const step = d.border * d.extra;
87
+ const seen80 = new Set(), seen20 = new Set(), seenWire = new Set();
88
+ let colorIndex = 0;
89
+ const nextOccurrence = new Map();
90
+ function lotId(name) {
91
+ // Legacy boats scenes sometimes only have `id`, or repeat a name.
92
+ // Never merge two boxes (or their pins) just because their names match.
93
+ const base = 'boat-' + name;
94
+ let id = base, n = nextOccurrence.get(base) || 1;
95
+ while (nextOccurrence.has(id)) id = base + '--' + ++n;
96
+ nextOccurrence.set(base, n);
97
+ nextOccurrence.set(id, 1);
98
+ return id;
99
+ }
100
+ function repeated(field, row, seen) {
101
+ if (!field) return false;
102
+ const previous = seen.has(row[field]);
103
+ seen.add(row[field]);
104
+ return !row.children && previous;
105
+ }
106
+ function buildingColor(value) {
107
+ if (typeof value === 'number') {
108
+ const t = normalize(meta.color_min, meta.color_max, 0, 1, value);
109
+ return `rgb(${Math.round(19 + 236 * t)},${Math.round(82 + 12 * t)},${Math.round(138 - 55 * t)})`;
110
+ }
111
+ if (typeof value !== 'string') return d.building_color;
112
+ if (!(value in categories)) categories[value] = palette[colorIndex++ % palette.length];
113
+ return categories[value];
114
+ }
115
+ function group(rows, padding) {
116
+ const figures = rows.map(row => {
117
+ const id = row[d.field] ?? row.id ?? row.name ?? 'unnamed';
118
+ const f = {id: lotId(id), rawData: row};
119
+ if (row.children) {
120
+ Object.assign(f, group(row.children, padding - step));
121
+ f.name = String(id ?? '');
122
+ f.height = d.zone_elevation / scaleY;
123
+ f.translate_matrix.y = f.height;
124
+ f.hierarchyLevel = f.name.split('/').length - 1;
125
+ f.renderOrder = f.hierarchyLevel;
126
+ f.alpha = d.baseAlpha;
127
+ f.color = d.gradientBaseColor ? `hsl(140, 100%,${Math.round(normalize(1, meta.maxLevels, 15, 100, f.hierarchyLevel))}%)` : d.base_color;
128
+ if (d.treeLayout) for (const child of row.children) if (!child.children) f.treeMetaphorHeight = child[d.height];
129
+ } else {
130
+ f.name = row.name || id;
131
+ f.height = buildingHeight(row[d.height], d, meta);
132
+ f.width = d.area ? Math.sqrt(row[d.area] || 0.5) : Number(row[d.width] || 0.5);
133
+ f.depth = d.area ? f.width : Number(row[d.depth] || 0.5);
134
+ f.color = buildingColor(row[d.color]); f.alpha = d.buildingAlpha;
135
+ if (d.windows) f.windows = Math.min(2 ** 24, Math.floor(positive(row[d.windows])));
136
+ if (d.glow) f.glow = positive(row[d.glow]);
137
+ }
138
+ if (repeated(d.transparent80ByRepeatedField, row, seen80)) { f.alpha = 0.8; f.renderOrder = 2000; }
139
+ if (repeated(d.transparent20ByRepeatedField, row, seen20)) { f.alpha = 0.45; f.renderOrder = 2000; }
140
+ f.wireframe = repeated(d.wireframeByRepeatedField, row, seenWire);
141
+ return f;
142
+ });
143
+ return pack(figures, padding, d.separation);
144
+ }
145
+ return group(items, step * (d.levels || meta.maxLevels + 1)).children;
146
+ }
147
+
148
+ module.exports = {collectMetadata, makeFigures, buildingHeight};