@expofp/wayfinding-geometry 0.0.0-bootstrap.0 → 0.0.0-stage

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.
@@ -1,305 +0,0 @@
1
- import { Box, boxIntersects, lineIntersection, pointDistance, } from '@expofp/geometry';
2
- /** Rounding error allowed in segment parameters, turns and sines. */
3
- export const EPS = 1e-9;
4
- const boxOf = (a, b) => Box.fromPoints([a, b]);
5
- /**
6
- * The cross product of two vectors.
7
- * @param a - one vector
8
- * @param b - the other
9
- * @returns `a × b`
10
- */
11
- export const cross = (a, b) => a.x * b.y - b.x * a.y;
12
- /**
13
- * The end of an edge that is not `node`.
14
- * @param edge - the edge
15
- * @param node - one of its ends
16
- * @returns the other end
17
- */
18
- export const otherEnd = (edge, node) => (edge.a === node ? edge.b : edge.a);
19
- /**
20
- * A key for the edge between two nodes, either way round.
21
- * @param a - one node
22
- * @param b - the other
23
- * @returns the key
24
- */
25
- export const edgeKey = (a, b) => (a < b ? `${a}:${b}` : `${b}:${a}`);
26
- /**
27
- * Appends a value to the list under a key, starting the list if there is none.
28
- * @param map - the lists
29
- * @param key - the key
30
- * @param value - the value
31
- */
32
- export function pushTo(map, key, value) {
33
- const list = map.get(key);
34
- if (list)
35
- list.push(value);
36
- else
37
- map.set(key, [value]);
38
- }
39
- /**
40
- * Reads the wall entries as vertex chains: consecutive repeats collapse, an odd trailing number is
41
- * dropped, and an entry ending where it began is a closed wall.
42
- * @param polylines - the layer's walls as flat `x, y, …` lists
43
- * @returns the walls of two vertices or more, in entry order
44
- */
45
- export function readWalls(polylines) {
46
- const walls = [];
47
- for (const flat of polylines) {
48
- const points = [];
49
- for (let i = 0; i + 1 < flat.length; i += 2) {
50
- const last = points[points.length - 1];
51
- if (last?.x === flat[i] && last.y === flat[i + 1])
52
- continue;
53
- points.push({ x: flat[i], y: flat[i + 1] });
54
- }
55
- if (points.length < 2)
56
- continue;
57
- const first = points[0];
58
- const last = points[points.length - 1];
59
- const closed = points.length > 3 && first.x === last.x && first.y === last.y;
60
- if (closed)
61
- points.pop();
62
- walls.push({ points, closed });
63
- }
64
- return walls;
65
- }
66
- /**
67
- * Cuts every wall where another one crosses it, ends on it or runs along it, and joins the pieces
68
- * at the points they share. A piece drawn twice is one edge.
69
- * @param walls - the layer's walls
70
- * @returns the wall graph
71
- */
72
- export function buildGraph(walls) {
73
- const segments = [];
74
- walls.forEach(({ points, closed }, wall) => {
75
- const count = closed ? points.length : points.length - 1;
76
- for (let index = 0; index < count; index++) {
77
- segments.push({
78
- p0: points[index],
79
- p1: points[(index + 1) % points.length],
80
- wall,
81
- index,
82
- cuts: [],
83
- });
84
- }
85
- });
86
- const boxes = segments.map(({ p0, p1 }) => boxOf(p0, p1));
87
- // Swept along x: a segment meets only those whose x-range overlaps its own.
88
- const byLeft = segments.map((_, i) => i).sort((i, j) => boxes[i].min.x - boxes[j].min.x || i - j);
89
- for (let i = 0; i < byLeft.length; i++) {
90
- const first = byLeft[i];
91
- for (let j = i + 1; j < byLeft.length; j++) {
92
- const second = byLeft[j];
93
- if (boxes[second].min.x > boxes[first].max.x)
94
- break;
95
- if (!boxIntersects(boxes[first], boxes[second]))
96
- continue;
97
- cutAtMeeting(segments[Math.min(first, second)], segments[Math.max(first, second)]);
98
- }
99
- }
100
- const nodes = [];
101
- const ids = new Map();
102
- const nodeOf = (point) => {
103
- const key = `${point.x},${point.y}`;
104
- let id = ids.get(key);
105
- if (id === undefined) {
106
- id = nodes.length;
107
- ids.set(key, id);
108
- nodes.push(point);
109
- }
110
- return id;
111
- };
112
- const edges = [];
113
- const seen = new Set();
114
- for (const { p0, p1, wall, index, cuts } of segments) {
115
- const stops = [{ t: 0, point: p0 }, ...cuts.sort((a, b) => a.t - b.t), { t: 1, point: p1 }];
116
- for (let k = 0; k + 1 < stops.length; k++) {
117
- const a = nodeOf(stops[k].point);
118
- const b = nodeOf(stops[k + 1].point);
119
- if (a === b || seen.has(edgeKey(a, b)))
120
- continue;
121
- seen.add(edgeKey(a, b));
122
- edges.push({
123
- a,
124
- b,
125
- bridge: false,
126
- drawn: [wall, index + stops[k].t],
127
- length: pointDistance(nodes[a], nodes[b]),
128
- });
129
- }
130
- }
131
- const at = nodes.map(() => []);
132
- edges.forEach(({ a, b }, index) => {
133
- at[a].push(index);
134
- at[b].push(index);
135
- });
136
- const wallEnds = new Map();
137
- for (const { points, closed } of walls) {
138
- if (closed)
139
- continue;
140
- let length = 0;
141
- for (let k = 0; k + 1 < points.length; k++)
142
- length += pointDistance(points[k], points[k + 1]);
143
- for (const end of [points[0], points[points.length - 1]]) {
144
- const id = ids.get(`${end.x},${end.y}`);
145
- if (id !== undefined)
146
- wallEnds.set(id, Math.max(wallEnds.get(id) ?? 0, length));
147
- }
148
- }
149
- // About one edge per cell: a bridge is checked against the edges near it, not all of them.
150
- const extent = Box.fromPoints(nodes);
151
- const size = Math.max(extent.size.x, extent.size.y) / Math.ceil(Math.sqrt(edges.length || 1)) || 1;
152
- const grid = { size, cells: new Map(), boxes: [], seen: [], search: 0 };
153
- edges.forEach((edge, index) => addToGrid(grid, index, boxOf(nodes[edge.a], nodes[edge.b])));
154
- return { nodes, edges, at, wallEnds, grid, blocked: new Set() };
155
- }
156
- /**
157
- * Calls back with each grid cell a box touches.
158
- * @param grid - the grid
159
- * @param box - the box
160
- * @param visit - called with each cell's key; returning true stops
161
- * @returns true when a call stopped it
162
- */
163
- function eachCell(grid, box, visit) {
164
- const { size } = grid;
165
- for (let cx = Math.floor(box.min.x / size); cx <= Math.floor(box.max.x / size); cx++) {
166
- for (let cy = Math.floor(box.min.y / size); cy <= Math.floor(box.max.y / size); cy++) {
167
- if (visit(`${cx}:${cy}`))
168
- return true;
169
- }
170
- }
171
- return false;
172
- }
173
- /**
174
- * Files an edge under the grid cells its bounds touch.
175
- * @param grid - the grid
176
- * @param index - the edge
177
- * @param box - its bounds
178
- */
179
- function addToGrid(grid, index, box) {
180
- grid.boxes[index] = box;
181
- eachCell(grid, box, (key) => {
182
- const cell = grid.cells.get(key);
183
- if (cell)
184
- cell.push(index);
185
- else
186
- grid.cells.set(key, [index]);
187
- return false;
188
- });
189
- }
190
- /**
191
- * Where `point` lies against the line through `a` and `b`: along it (`0` at `a`, `1` at `b`) and how
192
- * far off it.
193
- * @param a - the line's start
194
- * @param b - its end
195
- * @param point - the point
196
- * @returns the place along the line, and the distance from it
197
- */
198
- export function placeOn(a, b, point) {
199
- const span = { x: b.x - a.x, y: b.y - a.y };
200
- const offset = { x: point.x - a.x, y: point.y - a.y };
201
- const square = span.x * span.x + span.y * span.y;
202
- return {
203
- along: (offset.x * span.x + offset.y * span.y) / square,
204
- off: Math.abs(cross(span, offset)) / Math.sqrt(square),
205
- };
206
- }
207
- /**
208
- * Records where two wall segments meet, on each: a crossing, an end on the other, or an overlap.
209
- * @param s - one segment
210
- * @param o - the other
211
- */
212
- function cutAtMeeting(s, o) {
213
- const hit = lineIntersection(s, o);
214
- if (hit) {
215
- const { t, u } = hit;
216
- if (t < -EPS || t > 1 + EPS || u < -EPS || u > 1 + EPS)
217
- return;
218
- const sEnd = t <= EPS ? s.p0 : t >= 1 - EPS ? s.p1 : null;
219
- const oEnd = u <= EPS ? o.p0 : u >= 1 - EPS ? o.p1 : null;
220
- const point = sEnd ?? oEnd ?? { x: hit.point.x, y: hit.point.y };
221
- if (!sEnd)
222
- s.cuts.push({ t, point });
223
- if (!oEnd)
224
- o.cuts.push({ t: u, point });
225
- return;
226
- }
227
- // Parallel. On one line, each is cut where the other ends.
228
- const span = pointDistance(s.p0, s.p1);
229
- if (placeOn(s.p0, s.p1, o.p0).off > EPS * span || placeOn(s.p0, s.p1, o.p1).off > EPS * span) {
230
- return;
231
- }
232
- for (const [cut, other] of [
233
- [s, o],
234
- [o, s],
235
- ]) {
236
- for (const end of [other.p0, other.p1]) {
237
- const t = placeOn(cut.p0, cut.p1, end).along;
238
- if (t > EPS && t < 1 - EPS)
239
- cut.cuts.push({ t, point: end });
240
- }
241
- }
242
- }
243
- /**
244
- * Adds a bridge between two nodes.
245
- * @param graph - the wall graph
246
- * @param a - one end
247
- * @param b - the other
248
- */
249
- export function addBridge(graph, a, b) {
250
- const { nodes, edges, at } = graph;
251
- at[a].push(edges.length);
252
- at[b].push(edges.length);
253
- addToGrid(graph.grid, edges.length, boxOf(nodes[a], nodes[b]));
254
- edges.push({ a, b, bridge: true, drawn: null, length: pointDistance(nodes[a], nodes[b]) });
255
- }
256
- /**
257
- * The nodes with a single edge: loose wall ends.
258
- * @param graph - the wall graph
259
- * @returns the nodes
260
- */
261
- export function looseNodes(graph) {
262
- const loose = [];
263
- graph.at.forEach((edges, node) => {
264
- if (edges.length === 1)
265
- loose.push(node);
266
- });
267
- return loose;
268
- }
269
- /**
270
- * Whether two nodes are already joined by an edge.
271
- * @param graph - the wall graph
272
- * @param a - one node
273
- * @param b - the other
274
- * @returns true when an edge joins them
275
- */
276
- export const joined = (graph, a, b) => graph.at[a].some((edge) => otherEnd(graph.edges[edge], a) === b);
277
- /**
278
- * Whether a straight bridge between two nodes would cross no wall and no bridge, touching the graph
279
- * at its own ends only.
280
- * @param graph - the wall graph
281
- * @param a - one end
282
- * @param b - the other
283
- * @returns true when the bridge is clear
284
- */
285
- export function sees(graph, a, b) {
286
- const { nodes, edges, grid } = graph;
287
- const chord = { p0: nodes[a], p1: nodes[b] };
288
- const box = boxOf(chord.p0, chord.p1);
289
- const search = ++grid.search;
290
- if (joined(graph, a, b))
291
- return false;
292
- const blocked = eachCell(grid, box, (key) => (grid.cells.get(key) ?? []).some((index) => {
293
- if (grid.seen[index] === search)
294
- return false;
295
- grid.seen[index] = search;
296
- const edge = edges[index];
297
- if (edge.a === a || edge.a === b || edge.b === a || edge.b === b)
298
- return false;
299
- if (!boxIntersects(grid.boxes[index], box))
300
- return false;
301
- const hit = lineIntersection(chord, { p0: nodes[edge.a], p1: nodes[edge.b] });
302
- return !!hit && hit.t > EPS && hit.t < 1 - EPS && hit.u >= -EPS && hit.u <= 1 + EPS;
303
- }));
304
- return !blocked;
305
- }
@@ -1,33 +0,0 @@
1
- import { type WallGraph } from './wall-graph.js';
2
- /** A connected group of the graph and the walk round its outside. */
3
- export interface Group {
4
- /** Its index among the groups. */
5
- readonly id: number;
6
- /** Its edges. */
7
- readonly edges: number[];
8
- /** Nodes round the outside; the edge at position `k` runs to position `k + 1`. */
9
- readonly outer: number[];
10
- /** Twice the signed area of the outer walk: zero for an open group, negative otherwise. */
11
- readonly outerSum: number;
12
- /** Whether it encloses an area. */
13
- readonly building: boolean;
14
- /** Whether it lies inside another group's building: a room, a wall in a hall. */
15
- nested: boolean;
16
- }
17
- /**
18
- * The connected groups with the walk round each one's outside: its area is zero for an open group,
19
- * negative for a closed one (a face walk is positive).
20
- * @param graph - the wall graph
21
- * @returns the groups, and each node's group
22
- */
23
- export declare function groupsOf(graph: WallGraph): {
24
- groups: Group[];
25
- groupOf: number[];
26
- };
27
- /**
28
- * Marks the groups lying inside another group's building as nested in it.
29
- * @param graph - the wall graph
30
- * @param groups - its groups
31
- */
32
- export declare function markNested(graph: WallGraph, groups: readonly Group[]): void;
33
- //# sourceMappingURL=wall-groups.d.ts.map
@@ -1,97 +0,0 @@
1
- import { Box, lineAngle, ringContainsPoint } from '@expofp/geometry';
2
- import { cross, otherEnd } from './wall-graph.js';
3
- /**
4
- * The connected groups with the walk round each one's outside: its area is zero for an open group,
5
- * negative for a closed one (a face walk is positive).
6
- * @param graph - the wall graph
7
- * @returns the groups, and each node's group
8
- */
9
- export function groupsOf(graph) {
10
- const { nodes, edges, at } = graph;
11
- // Each node's edges by direction, as the neighbor and the edge.
12
- const order = at.map((list, node) => list
13
- .map((edge) => {
14
- const next = otherEnd(edges[edge], node);
15
- return { next, edge, angle: lineAngle(nodes[node], nodes[next]) };
16
- })
17
- .sort((p, q) => p.angle - q.angle));
18
- const groupOf = nodes.map(() => -1);
19
- let count = 0;
20
- nodes.forEach((_, start) => {
21
- if (groupOf[start] !== -1 || !at[start].length)
22
- return;
23
- const stack = [start];
24
- groupOf[start] = count;
25
- while (stack.length) {
26
- for (const { next } of order[stack.pop()]) {
27
- if (groupOf[next] !== -1)
28
- continue;
29
- groupOf[next] = count;
30
- stack.push(next);
31
- }
32
- }
33
- count++;
34
- });
35
- const groupEdges = Array.from({ length: count }, () => []);
36
- edges.forEach((edge, index) => groupEdges[groupOf[edge.a]].push(index));
37
- // Per edge, whether it was walked from `a` (bit 1) and from `b` (bit 2).
38
- const walked = new Uint8Array(edges.length);
39
- const side = (edge, from) => (edges[edge].a === from ? 1 : 2);
40
- const outer = Array.from({ length: count }, () => ({ walk: [], sum: Infinity }));
41
- const walks = Array.from({ length: count }, () => 0);
42
- edges.forEach((edge, index) => {
43
- for (const from of [edge.a, edge.b]) {
44
- if (walked[index] & side(index, from))
45
- continue;
46
- const walk = [];
47
- let sum = 0;
48
- let u = from;
49
- let through = index;
50
- while (!(walked[through] & side(through, u))) {
51
- const v = otherEnd(edges[through], u);
52
- const list = order[v];
53
- const k = list.findIndex((entry) => entry.edge === through);
54
- walked[through] |= side(through, u);
55
- walk.push(u);
56
- sum += cross(nodes[u], nodes[v]);
57
- through = list[(k - 1 + list.length) % list.length].edge;
58
- u = v;
59
- }
60
- const group = groupOf[from];
61
- walks[group]++;
62
- if (sum < outer[group].sum)
63
- outer[group] = { walk, sum };
64
- }
65
- });
66
- return {
67
- groups: outer.map(({ walk, sum }, id) => ({
68
- id,
69
- edges: groupEdges[id],
70
- outer: walk,
71
- outerSum: sum,
72
- building: walks[id] > 1,
73
- nested: false,
74
- })),
75
- groupOf,
76
- };
77
- }
78
- /**
79
- * Marks the groups lying inside another group's building as nested in it.
80
- * @param graph - the wall graph
81
- * @param groups - its groups
82
- */
83
- export function markNested(graph, groups) {
84
- const rings = groups.map((group) => {
85
- if (!group.building)
86
- return null;
87
- const ring = group.outer.map((node) => graph.nodes[node]);
88
- return { ring, box: Box.fromPoints(ring) };
89
- });
90
- for (const group of groups) {
91
- const point = graph.nodes[group.outer[0]];
92
- group.nested = rings.some((found, index) => found !== null &&
93
- index !== group.id &&
94
- found.box.containsPoint(point) &&
95
- ringContainsPoint(found.ring, point));
96
- }
97
- }