@expofp/wayfinding-geometry 3.52.2 → 3.54.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/README.md CHANGED
@@ -58,6 +58,23 @@ Limits:
58
58
  - Without `maxBridge`, two L-shaped fences facing each other close into one building, however far
59
59
  apart they stand: the bridges run across the two open corners.
60
60
 
61
+ ## `readWalls(polylines, eps?)` and `readWallRuns(flat, eps?)`
62
+
63
+ The walls as `buildingOutlines`, the compiler and the viewer all read them. `readWallRuns` reads one
64
+ entry, `readWalls` every entry of a layer in order. The export writes a segment per vertex pair,
65
+ joined by repeating the shared vertex; where a pair does not touch the next, a new wall starts, so
66
+ one entry may be several walls — read as one chain, the jump would be a wall nobody drew. Vertices
67
+ within `eps` (default 0, in the walls' units) are one; a wall of three vertices or more ending where it
68
+ began is `closed`, its first vertex not repeated. An odd trailing number is dropped, and walls of fewer
69
+ than two vertices are left out.
70
+
71
+ ## `BoxGrid`
72
+
73
+ Files boxes under the cells of a square grid, about one box per cell, and visits the boxes meeting a
74
+ query box without checking all of them: the walls a wall or a line of sight might cross. `alongRay`
75
+ visits the boxes cell by cell along a ray, nearest cells first, with the ray parameter where each cell
76
+ ends, so a search for the first wall a ray meets can stop as soon as its best hit is nearer.
77
+
61
78
  ## Development
62
79
 
63
80
  - Build: `pnpm nx build @expofp/wayfinding-geometry`
package/dist/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
+ export { BoxGrid } from './lib/box-grid.js';
1
2
  export { buildingOutlines } from './lib/building-outlines.js';
3
+ export { readWallRuns, readWalls, type Wall } from './lib/wall-graph.js';
2
4
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1 +1,3 @@
1
+ export { BoxGrid } from './lib/box-grid.js';
1
2
  export { buildingOutlines } from './lib/building-outlines.js';
3
+ export { readWallRuns, readWalls } from './lib/wall-graph.js';
@@ -0,0 +1,52 @@
1
+ import { type BoxLike, type Point2Like } from '@expofp/geometry';
2
+ /**
3
+ * Boxes filed under the cells of a square grid they touch: finds the boxes meeting a box, or lying in
4
+ * the way of a ray, fast.
5
+ */
6
+ export declare class BoxGrid {
7
+ private readonly size;
8
+ private readonly cells;
9
+ private readonly boxes;
10
+ /** Per box, the last search that met it: a box in several cells is visited once. */
11
+ private readonly seen;
12
+ private search;
13
+ /** The cells holding boxes lie within these columns and rows. */
14
+ private readonly filled;
15
+ /**
16
+ * @param extent - the area the boxes lie in
17
+ * @param count - how many boxes it is sized for: about one per cell
18
+ */
19
+ constructor(extent: BoxLike, count: number);
20
+ /**
21
+ * Files a box under the cells it touches.
22
+ * @param box - the box
23
+ * @returns its index: how many boxes were added before it
24
+ */
25
+ add(box: BoxLike): number;
26
+ /**
27
+ * Calls `visit` once with the index of each box meeting `box`, until a call returns true. `visit`
28
+ * must not search the same grid: a search inside it makes the outer one visit boxes again.
29
+ * @param box - the box to meet
30
+ * @param visit - called with a box's index; returning true stops
31
+ * @returns true when a call stopped it
32
+ */
33
+ some(box: BoxLike, visit: (index: number) => boolean): boolean;
34
+ /**
35
+ * Visits the boxes filed in the cells a ray passes, cell by cell from where it starts, each box once.
36
+ * It only narrows the candidates: whether the ray meets a box is the caller's test. As with `some`,
37
+ * `visit` must not search the same grid.
38
+ * @param origin - the ray's point at parameter 0
39
+ * @param direction - its direction: parameter `t` is at `origin + t · direction`
40
+ * @param from - the parameter to start at
41
+ * @param visit - gets the cell's boxes not visited yet and the parameter where the ray leaves the
42
+ * cell; returning true stops
43
+ */
44
+ alongRay(origin: Point2Like, direction: Point2Like, from: number, visit: (indices: readonly number[], leaves: number) => boolean): void;
45
+ /**
46
+ * The columns and rows of the cells a box touches.
47
+ * @param box - the box
48
+ * @returns `[minX, minY, maxX, maxY]`
49
+ */
50
+ private cellRange;
51
+ }
52
+ //# sourceMappingURL=box-grid.d.ts.map
@@ -0,0 +1,147 @@
1
+ import { boxIntersects } from '@expofp/geometry';
2
+ /**
3
+ * Boxes filed under the cells of a square grid they touch: finds the boxes meeting a box, or lying in
4
+ * the way of a ray, fast.
5
+ */
6
+ export class BoxGrid {
7
+ size;
8
+ cells = new Map();
9
+ boxes = [];
10
+ /** Per box, the last search that met it: a box in several cells is visited once. */
11
+ seen = [];
12
+ search = 0;
13
+ /** The cells holding boxes lie within these columns and rows. */
14
+ filled = { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity };
15
+ /**
16
+ * @param extent - the area the boxes lie in
17
+ * @param count - how many boxes it is sized for: about one per cell
18
+ */
19
+ constructor(extent, count) {
20
+ const width = extent.max.x - extent.min.x;
21
+ const height = extent.max.y - extent.min.y;
22
+ const cells = count || 1;
23
+ this.size = Math.sqrt((width * height) / cells) || Math.max(width, height) / cells || 1;
24
+ }
25
+ /**
26
+ * Files a box under the cells it touches.
27
+ * @param box - the box
28
+ * @returns its index: how many boxes were added before it
29
+ */
30
+ add(box) {
31
+ const index = this.boxes.length;
32
+ const { filled } = this;
33
+ const [minX, minY, maxX, maxY] = this.cellRange(box);
34
+ this.boxes.push(box);
35
+ filled.minX = Math.min(filled.minX, minX);
36
+ filled.minY = Math.min(filled.minY, minY);
37
+ filled.maxX = Math.max(filled.maxX, maxX);
38
+ filled.maxY = Math.max(filled.maxY, maxY);
39
+ for (let cx = minX; cx <= maxX; cx++) {
40
+ for (let cy = minY; cy <= maxY; cy++) {
41
+ const key = `${cx}:${cy}`;
42
+ const cell = this.cells.get(key);
43
+ if (cell)
44
+ cell.push(index);
45
+ else
46
+ this.cells.set(key, [index]);
47
+ }
48
+ }
49
+ return index;
50
+ }
51
+ /**
52
+ * Calls `visit` once with the index of each box meeting `box`, until a call returns true. `visit`
53
+ * must not search the same grid: a search inside it makes the outer one visit boxes again.
54
+ * @param box - the box to meet
55
+ * @param visit - called with a box's index; returning true stops
56
+ * @returns true when a call stopped it
57
+ */
58
+ some(box, visit) {
59
+ const search = ++this.search;
60
+ const { filled } = this;
61
+ const [minX, minY, maxX, maxY] = this.cellRange(box);
62
+ for (let cx = Math.max(minX, filled.minX); cx <= Math.min(maxX, filled.maxX); cx++) {
63
+ for (let cy = Math.max(minY, filled.minY); cy <= Math.min(maxY, filled.maxY); cy++) {
64
+ const cell = this.cells.get(`${cx}:${cy}`);
65
+ if (!cell)
66
+ continue;
67
+ for (const index of cell) {
68
+ if (this.seen[index] === search)
69
+ continue;
70
+ this.seen[index] = search;
71
+ if (boxIntersects(this.boxes[index], box) && visit(index))
72
+ return true;
73
+ }
74
+ }
75
+ }
76
+ return false;
77
+ }
78
+ /**
79
+ * Visits the boxes filed in the cells a ray passes, cell by cell from where it starts, each box once.
80
+ * It only narrows the candidates: whether the ray meets a box is the caller's test. As with `some`,
81
+ * `visit` must not search the same grid.
82
+ * @param origin - the ray's point at parameter 0
83
+ * @param direction - its direction: parameter `t` is at `origin + t · direction`
84
+ * @param from - the parameter to start at
85
+ * @param visit - gets the cell's boxes not visited yet and the parameter where the ray leaves the
86
+ * cell; returning true stops
87
+ */
88
+ alongRay(origin, direction, from, visit) {
89
+ const { size, filled } = this;
90
+ const startX = origin.x + direction.x * from;
91
+ const startY = origin.y + direction.y * from;
92
+ const stepX = Math.sign(direction.x);
93
+ const stepY = Math.sign(direction.y);
94
+ // The ray parameter at the next column or row border, and how much it grows per cell.
95
+ const border = (start, d, cell) => d > 0 ? ((cell + 1) * size - start) / d : d < 0 ? (cell * size - start) / d : Infinity;
96
+ const perX = direction.x ? size / Math.abs(direction.x) : Infinity;
97
+ const perY = direction.y ? size / Math.abs(direction.y) : Infinity;
98
+ let cx = Math.floor(startX / size);
99
+ let cy = Math.floor(startY / size);
100
+ let nextX = from + border(startX, direction.x, cx);
101
+ let nextY = from + border(startY, direction.y, cy);
102
+ const search = ++this.search;
103
+ for (;;) {
104
+ const fresh = [];
105
+ for (const index of this.cells.get(`${cx}:${cy}`) ?? []) {
106
+ if (this.seen[index] === search)
107
+ continue;
108
+ this.seen[index] = search;
109
+ fresh.push(index);
110
+ }
111
+ // A ray going nowhere meets only what its start cell holds.
112
+ if (visit(fresh, Math.min(nextX, nextY)) || (!stepX && !stepY))
113
+ return;
114
+ if (nextX < nextY) {
115
+ cx += stepX;
116
+ nextX += perX;
117
+ }
118
+ else {
119
+ cy += stepY;
120
+ nextY += perY;
121
+ }
122
+ // Past the last filed cell the ray meets nothing more.
123
+ if ((stepX > 0 && cx > filled.maxX) || (stepX < 0 && cx < filled.minX))
124
+ return;
125
+ if ((stepY > 0 && cy > filled.maxY) || (stepY < 0 && cy < filled.minY))
126
+ return;
127
+ if (!stepX && (cx < filled.minX || cx > filled.maxX))
128
+ return;
129
+ if (!stepY && (cy < filled.minY || cy > filled.maxY))
130
+ return;
131
+ }
132
+ }
133
+ /**
134
+ * The columns and rows of the cells a box touches.
135
+ * @param box - the box
136
+ * @returns `[minX, minY, maxX, maxY]`
137
+ */
138
+ cellRange(box) {
139
+ const { size } = this;
140
+ return [
141
+ Math.floor(box.min.x / size),
142
+ Math.floor(box.min.y / size),
143
+ Math.floor(box.max.x / size),
144
+ Math.floor(box.max.y / size),
145
+ ];
146
+ }
147
+ }
@@ -1,10 +1,9 @@
1
1
  import { type WallGraph } from './wall-graph.js';
2
2
  /**
3
- * Joins each loose wall end to the nearest node ahead of it on its wall's line where a wall runs on —
4
- * a door or a gap in a straight wall — within the widest bridge. The walls beside the gap need only
5
- * be a quarter as long as it: the corner rule asks for longer, and misses a door by a short jamb.
6
- * With no limit the gap must be well short of the longer wall beside it, or two buildings in line would
7
- * join.
3
+ * Joins each loose wall end to the nearest node ahead on its wall's line where a wall runs on — a door
4
+ * in a straight wall — within the widest bridge. The walls beside the gap need only be a quarter as
5
+ * long as it, or a door with a short jamb is missed. With no limit the gap must be well short of the
6
+ * longer wall beside it, or two buildings in line would join.
8
7
  * @param graph - the wall graph; the bridges are added to it
9
8
  * @param maxBridge - the widest gap
10
9
  */
@@ -1,7 +1,9 @@
1
1
  import { pointDistance } from '@expofp/geometry';
2
2
  import { addBridge, edgeKey, EPS, looseNodes, otherEnd, sees, } from './wall-graph.js';
3
- /** How far a wall's continuation may turn off its line, as the sine of the angle: drawing error. */
4
- const STRAIGHT_SINE = Math.sin((5 * Math.PI) / 180);
3
+ /** How far a wall's continuation may turn off its line: drawing error. */
4
+ const STRAIGHT_ANGLE = (5 * Math.PI) / 180;
5
+ const STRAIGHT_TANGENT = Math.tan(STRAIGHT_ANGLE);
6
+ const STRAIGHT_COSINE = Math.cos(STRAIGHT_ANGLE);
5
7
  /** The shortest wall a straight gap is read off, as a share of the gap: a shorter stroke has no line. */
6
8
  const MIN_SIDE_SHARE = 0.25;
7
9
  /** With no limit, the widest straight gap, as a share of the longer wall beside it: a door is narrower. */
@@ -17,24 +19,23 @@ function direction(from, to) {
17
19
  return length ? { x: (to.x - from.x) / length, y: (to.y - from.y) / length } : null;
18
20
  }
19
21
  /**
20
- * Joins each loose wall end to the nearest node ahead of it on its wall's line where a wall runs on —
21
- * a door or a gap in a straight wall — within the widest bridge. The walls beside the gap need only
22
- * be a quarter as long as it: the corner rule asks for longer, and misses a door by a short jamb.
23
- * With no limit the gap must be well short of the longer wall beside it, or two buildings in line would
24
- * join.
22
+ * Joins each loose wall end to the nearest node ahead on its wall's line where a wall runs on — a door
23
+ * in a straight wall — within the widest bridge. The walls beside the gap need only be a quarter as
24
+ * long as it, or a door with a short jamb is missed. With no limit the gap must be well short of the
25
+ * longer wall beside it, or two buildings in line would join.
25
26
  * @param graph - the wall graph; the bridges are added to it
26
27
  * @param maxBridge - the widest gap
27
28
  */
28
29
  export function closeStraightGaps(graph, maxBridge) {
29
30
  const { nodes, edges, at } = graph;
30
- const runsOnCosine = Math.sqrt(1 - STRAIGHT_SINE * STRAIGHT_SINE);
31
31
  const limited = Number.isFinite(maxBridge);
32
32
  for (const u of looseNodes(graph)) {
33
33
  if (at[u].length !== 1)
34
34
  continue;
35
- const ahead = direction(nodes[otherEnd(edges[at[u][0]], u)], nodes[u]);
35
+ const wall = edges[at[u][0]];
36
+ const ahead = direction(nodes[otherEnd(wall, u)], nodes[u]);
36
37
  // The straight piece the gap opens from: a wall drawn there and back measures twice its run.
37
- const ownWall = edges[at[u][0]].length;
38
+ const ownWall = wall.length;
38
39
  if (!ahead)
39
40
  continue;
40
41
  let nearest = -1;
@@ -45,14 +46,14 @@ export function closeStraightGaps(graph, maxBridge) {
45
46
  const along = x * ahead.x + y * ahead.y;
46
47
  if (v === u || along <= EPS || along > nearestGap || ownWall < MIN_SIDE_SHARE * along)
47
48
  continue;
48
- if (Math.abs(x * ahead.y - y * ahead.x) > STRAIGHT_SINE * along)
49
+ if (Math.abs(x * ahead.y - y * ahead.x) > STRAIGHT_TANGENT * along)
49
50
  continue;
50
51
  // The longest piece of wall going on from there along the same line, away from the gap.
51
52
  let runsOn = 0;
52
53
  for (const index of at[v]) {
53
54
  const edge = edges[index];
54
55
  const on = !edge.bridge && direction(nodes[v], nodes[otherEnd(edge, v)]);
55
- if (on && on.x * ahead.x + on.y * ahead.y >= runsOnCosine)
56
+ if (on && on.x * ahead.x + on.y * ahead.y >= STRAIGHT_COSINE)
56
57
  runsOn = Math.max(runsOn, edge.length);
57
58
  }
58
59
  if (runsOn < MIN_SIDE_SHARE * along)
@@ -1,8 +1,9 @@
1
- import { Box, type Point2Like } from '@expofp/geometry';
1
+ import { type Point2Like } from '@expofp/geometry';
2
+ import { BoxGrid } from './box-grid.js';
2
3
  /** Rounding error allowed in segment parameters, turns and sines. */
3
4
  export declare const EPS = 1e-9;
4
5
  /** One wall of a layer, read from its entry in the plan data. */
5
- interface Wall {
6
+ export interface Wall {
6
7
  /** Its vertices; a closed wall does not repeat the first one. */
7
8
  readonly points: readonly Point2Like[];
8
9
  /** Whether the wall ends where it began. */
@@ -31,21 +32,11 @@ export interface WallGraph {
31
32
  readonly at: number[][];
32
33
  /** Per node that ends an open wall, that wall's length. */
33
34
  readonly wallEnds: Map<number, number>;
34
- /** The edges by the cells of a square grid their bounds touch, for finding what a bridge crosses. */
35
- readonly grid: EdgeGrid;
35
+ /** The edges' bounds, filed in edge order: a box's index is its edge's. For what a bridge crosses. */
36
+ readonly grid: BoxGrid;
36
37
  /** Loose ends whose best closure needs a longer bridge: their group stays open. */
37
38
  readonly blocked: Set<number>;
38
39
  }
39
- /** Edges by the cells of a square grid their bounds touch. */
40
- interface EdgeGrid {
41
- readonly size: number;
42
- readonly cells: Map<string, number[]>;
43
- /** Per edge, its bounds. */
44
- readonly boxes: Box[];
45
- /** Per edge, the last search that looked at it: an edge in several cells is checked once. */
46
- readonly seen: number[];
47
- search: number;
48
- }
49
40
  /**
50
41
  * The cross product of two vectors.
51
42
  * @param a - one vector
@@ -75,12 +66,21 @@ export declare const edgeKey: (a: number, b: number) => string;
75
66
  */
76
67
  export declare function pushTo<K>(map: Map<K, number[]>, key: K, value: number): void;
77
68
  /**
78
- * Reads the wall entries as vertex chains: consecutive repeats collapse, an odd trailing number is
79
- * dropped, and an entry ending where it began is a closed wall.
69
+ * Splits one wall entry into the continuous walls it draws. The export stores a segment per vertex
70
+ * pair, joined by repeating the shared vertex; where a pair does not touch the next, a new wall
71
+ * starts — read as one chain, that jump would be a wall nobody drew.
72
+ * @param flat - the entry as a flat `x, y, …` list; an odd trailing number is dropped
73
+ * @param eps - how far apart two vertices may lie and still be one
74
+ * @returns its walls of two vertices or more, in drawing order
75
+ */
76
+ export declare function readWallRuns(flat: readonly number[], eps?: number): Wall[];
77
+ /**
78
+ * Reads a layer's wall entries as walls (`readWallRuns`, entry by entry).
80
79
  * @param polylines - the layer's walls as flat `x, y, …` lists
80
+ * @param eps - how far apart two vertices may lie and still be one, in their units
81
81
  * @returns the walls of two vertices or more, in entry order
82
82
  */
83
- export declare function readWalls(polylines: readonly (readonly number[])[]): Wall[];
83
+ export declare function readWalls(polylines: readonly (readonly number[])[], eps?: number): Wall[];
84
84
  /**
85
85
  * Cuts every wall where another one crosses it, ends on it or runs along it, and joins the pieces
86
86
  * at the points they share. A piece drawn twice is one edge.
@@ -130,5 +130,4 @@ export declare const joined: (graph: WallGraph, a: number, b: number) => boolean
130
130
  * @returns true when the bridge is clear
131
131
  */
132
132
  export declare function sees(graph: WallGraph, a: number, b: number): boolean;
133
- export {};
134
133
  //# sourceMappingURL=wall-graph.d.ts.map
@@ -1,4 +1,5 @@
1
1
  import { Box, boxIntersects, lineIntersection, pointDistance, } from '@expofp/geometry';
2
+ import { BoxGrid } from './box-grid.js';
2
3
  /** Rounding error allowed in segment parameters, turns and sines. */
3
4
  export const EPS = 1e-9;
4
5
  const boxOf = (a, b) => Box.fromPoints([a, b]);
@@ -37,31 +38,79 @@ export function pushTo(map, key, value) {
37
38
  map.set(key, [value]);
38
39
  }
39
40
  /**
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
41
+ * The step parity that carries an entry's repeated joint vertices, or `-1` for a plain polyline.
42
+ * @param vertices - the entry's vertices
43
+ * @param eps - how far apart two vertices may lie and still be one
44
+ * @returns `0`, `1` or `-1`
44
45
  */
45
- export function readWalls(polylines) {
46
+ function jointParity(vertices, eps) {
47
+ const glued = [0, 0];
48
+ const total = [0, 0];
49
+ for (let k = 0; k + 1 < vertices.length; k++) {
50
+ total[k % 2]++;
51
+ if (pointDistance(vertices[k], vertices[k + 1]) <= eps)
52
+ glued[k % 2]++;
53
+ }
54
+ // The parity glued more often, by share: a vertex written three times glues both parities once,
55
+ // and only the joints' parity is glued at every step.
56
+ const parity = glued[0] * total[1] >= glued[1] * total[0] ? 0 : 1;
57
+ // The joint parity is glued at least half the time; otherwise every step is a real segment.
58
+ return glued[parity] * 2 >= total[parity] && glued[parity] > 0 ? parity : -1;
59
+ }
60
+ /**
61
+ * One continuous run of vertices as a wall: coincident neighbors merge, and a run ending where it
62
+ * began is closed.
63
+ * @param vertices - the run's vertices
64
+ * @param eps - how far apart two vertices may lie and still be one
65
+ * @returns the wall
66
+ */
67
+ function wallOf(vertices, eps) {
68
+ const points = [];
69
+ for (const vertex of vertices) {
70
+ const last = points[points.length - 1];
71
+ if (last && pointDistance(vertex, last) <= eps)
72
+ continue;
73
+ points.push(vertex);
74
+ }
75
+ const closed = points.length > 2 && pointDistance(points[0], points[points.length - 1]) <= eps;
76
+ if (closed)
77
+ points.pop();
78
+ return { points, closed };
79
+ }
80
+ /**
81
+ * Splits one wall entry into the continuous walls it draws. The export stores a segment per vertex
82
+ * pair, joined by repeating the shared vertex; where a pair does not touch the next, a new wall
83
+ * starts — read as one chain, that jump would be a wall nobody drew.
84
+ * @param flat - the entry as a flat `x, y, …` list; an odd trailing number is dropped
85
+ * @param eps - how far apart two vertices may lie and still be one
86
+ * @returns its walls of two vertices or more, in drawing order
87
+ */
88
+ export function readWallRuns(flat, eps = 0) {
89
+ const vertices = [];
90
+ for (let i = 0; i + 1 < flat.length; i += 2)
91
+ vertices.push({ x: flat[i], y: flat[i + 1] });
92
+ if (vertices.length < 2)
93
+ return [];
94
+ const joint = jointParity(vertices, eps);
46
95
  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)
96
+ let start = 0;
97
+ for (let k = 0; k + 1 < vertices.length; k++) {
98
+ if (k % 2 !== joint || pointDistance(vertices[k], vertices[k + 1]) <= eps)
56
99
  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 });
100
+ walls.push(wallOf(vertices.slice(start, k + 1), eps));
101
+ start = k + 1;
63
102
  }
64
- return walls;
103
+ walls.push(wallOf(vertices.slice(start), eps));
104
+ return walls.filter(({ points }) => points.length >= 2);
105
+ }
106
+ /**
107
+ * Reads a layer's wall entries as walls (`readWallRuns`, entry by entry).
108
+ * @param polylines - the layer's walls as flat `x, y, …` lists
109
+ * @param eps - how far apart two vertices may lie and still be one, in their units
110
+ * @returns the walls of two vertices or more, in entry order
111
+ */
112
+ export function readWalls(polylines, eps = 0) {
113
+ return polylines.flatMap((flat) => readWallRuns(flat, eps));
65
114
  }
66
115
  /**
67
116
  * Cuts every wall where another one crosses it, ends on it or runs along it, and joins the pieces
@@ -147,46 +196,11 @@ export function buildGraph(walls) {
147
196
  }
148
197
  }
149
198
  // 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])));
199
+ const grid = new BoxGrid(Box.fromPoints(nodes), edges.length);
200
+ for (const edge of edges)
201
+ grid.add(boxOf(nodes[edge.a], nodes[edge.b]));
154
202
  return { nodes, edges, at, wallEnds, grid, blocked: new Set() };
155
203
  }
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
204
  /**
191
205
  * Where `point` lies against the line through `a` and `b`: along it (`0` at `a`, `1` at `b`) and how
192
206
  * far off it.
@@ -250,7 +264,7 @@ export function addBridge(graph, a, b) {
250
264
  const { nodes, edges, at } = graph;
251
265
  at[a].push(edges.length);
252
266
  at[b].push(edges.length);
253
- addToGrid(graph.grid, edges.length, boxOf(nodes[a], nodes[b]));
267
+ graph.grid.add(boxOf(nodes[a], nodes[b]));
254
268
  edges.push({ a, b, bridge: true, drawn: null, length: pointDistance(nodes[a], nodes[b]) });
255
269
  }
256
270
  /**
@@ -285,21 +299,14 @@ export const joined = (graph, a, b) => graph.at[a].some((edge) => otherEnd(graph
285
299
  export function sees(graph, a, b) {
286
300
  const { nodes, edges, grid } = graph;
287
301
  const chord = { p0: nodes[a], p1: nodes[b] };
288
- const box = boxOf(chord.p0, chord.p1);
289
- const search = ++grid.search;
290
302
  if (joined(graph, a, b))
291
303
  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;
304
+ const blocked = grid.some(boxOf(chord.p0, chord.p1), (index) => {
296
305
  const edge = edges[index];
297
306
  if (edge.a === a || edge.a === b || edge.b === a || edge.b === b)
298
307
  return false;
299
- if (!boxIntersects(grid.boxes[index], box))
300
- return false;
301
308
  const hit = lineIntersection(chord, { p0: nodes[edge.a], p1: nodes[edge.b] });
302
309
  return !!hit && hit.t > EPS && hit.t < 1 - EPS && hit.u >= -EPS && hit.u <= 1 + EPS;
303
- }));
310
+ });
304
311
  return !blocked;
305
312
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expofp/wayfinding-geometry",
3
- "version": "3.52.2",
3
+ "version": "3.54.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/expofp/efp-app.git",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "tslib": "^2.3.0",
30
- "@expofp/geometry": "3.52.2"
30
+ "@expofp/geometry": "3.54.0"
31
31
  },
32
32
  "files": [
33
33
  "dist",