@expofp/wayfinding-geometry 0.0.0-stage → 3.52.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 +63 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/lib/building-outlines.d.ts +13 -0
- package/dist/lib/building-outlines.js +126 -0
- package/dist/lib/closures.d.ts +32 -0
- package/dist/lib/closures.js +254 -0
- package/dist/lib/corners.d.ts +19 -0
- package/dist/lib/corners.js +129 -0
- package/dist/lib/walks.d.ts +86 -0
- package/dist/lib/walks.js +179 -0
- package/dist/lib/wall-graph.d.ts +134 -0
- package/dist/lib/wall-graph.js +305 -0
- package/dist/lib/wall-groups.d.ts +33 -0
- package/dist/lib/wall-groups.js +97 -0
- package/package.json +34 -3
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { type Point2Like } from '@expofp/geometry';
|
|
2
|
+
import { type Edge, type WallGraph } from './wall-graph.js';
|
|
3
|
+
import type { Group } from './wall-groups.js';
|
|
4
|
+
/** A group's outer walk, measured for closing it: running area, nodes by position, edges walked twice. */
|
|
5
|
+
export interface Walk {
|
|
6
|
+
/** The walk's nodes. */
|
|
7
|
+
readonly walk: readonly number[];
|
|
8
|
+
/** Twice the signed area under the walk from its start to each position. */
|
|
9
|
+
readonly prefix: readonly number[];
|
|
10
|
+
/** Each node's positions on the walk. */
|
|
11
|
+
readonly positions: ReadonlyMap<number, readonly number[]>;
|
|
12
|
+
/** Edges with the outside on both sides: positions of the two passes. */
|
|
13
|
+
readonly twice: readonly {
|
|
14
|
+
readonly edge: Edge;
|
|
15
|
+
readonly at: readonly [number, number];
|
|
16
|
+
}[];
|
|
17
|
+
/** Walls the group has in all. */
|
|
18
|
+
readonly walls: number;
|
|
19
|
+
/** Bridge length the group has in all. */
|
|
20
|
+
readonly bridged: number;
|
|
21
|
+
/** Walls the group holds now, inside or on its building; none when open. */
|
|
22
|
+
readonly heldWalls: number;
|
|
23
|
+
/** Bridge length the group holds now. */
|
|
24
|
+
readonly heldBridged: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* How much a path of walls turns, the stretches it walks there and back left out: they turn
|
|
28
|
+
* nowhere.
|
|
29
|
+
* @param points - the path's vertices
|
|
30
|
+
* @returns the signed turn in radians
|
|
31
|
+
*/
|
|
32
|
+
export declare function turnOf(points: readonly Point2Like[]): number;
|
|
33
|
+
/**
|
|
34
|
+
* The walls and bridge length a group has in all.
|
|
35
|
+
* @param graph - the wall graph
|
|
36
|
+
* @param group - the group
|
|
37
|
+
* @returns the walls, and the bridge length
|
|
38
|
+
*/
|
|
39
|
+
export declare function contentsOf(graph: WallGraph, group: Group): {
|
|
40
|
+
walls: number;
|
|
41
|
+
bridged: number;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Measures a group's outer walk for closing it.
|
|
45
|
+
* @param graph - the wall graph
|
|
46
|
+
* @param group - the group
|
|
47
|
+
* @returns the measured walk
|
|
48
|
+
*/
|
|
49
|
+
export declare function walkOf(graph: WallGraph, group: Group): Walk;
|
|
50
|
+
/**
|
|
51
|
+
* Twice the signed area under a stretch of the walk.
|
|
52
|
+
* @param w - the walk
|
|
53
|
+
* @param start - the stretch's first position
|
|
54
|
+
* @param end - its last
|
|
55
|
+
* @returns the shoelace sum of the stretch
|
|
56
|
+
*/
|
|
57
|
+
export declare const stretchSum: (w: Walk, start: number, end: number) => number;
|
|
58
|
+
/**
|
|
59
|
+
* A stretch of the walk.
|
|
60
|
+
* @param walk - the walk's nodes
|
|
61
|
+
* @param start - the stretch's first position
|
|
62
|
+
* @param end - its last, included
|
|
63
|
+
* @returns the stretch's nodes
|
|
64
|
+
*/
|
|
65
|
+
export declare function stretch(walk: readonly number[], start: number, end: number): number[];
|
|
66
|
+
/**
|
|
67
|
+
* What closing a stretch of the walk adds to the group's building: walls and bridge length.
|
|
68
|
+
* @param w - the walk
|
|
69
|
+
* @param start - the closed stretch's first position
|
|
70
|
+
* @param end - its last
|
|
71
|
+
* @returns the walls, and the bridge length
|
|
72
|
+
*/
|
|
73
|
+
export declare function gainOf(w: Walk, start: number, end: number): {
|
|
74
|
+
walls: number;
|
|
75
|
+
bridged: number;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Whether a bridge from a node, at one of its places on the walk, leaves into the outside there.
|
|
79
|
+
* @param graph - the wall graph
|
|
80
|
+
* @param w - the walk
|
|
81
|
+
* @param k - the node's position on the walk
|
|
82
|
+
* @param toward - the node the bridge goes to
|
|
83
|
+
* @returns true when the bridge leaves into the side the walk runs round
|
|
84
|
+
*/
|
|
85
|
+
export declare function leavesOutward(graph: WallGraph, w: Walk, k: number, toward: number): boolean;
|
|
86
|
+
//# sourceMappingURL=walks.d.ts.map
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { lineAngle } from '@expofp/geometry';
|
|
2
|
+
import { cross, edgeKey, pushTo } from './wall-graph.js';
|
|
3
|
+
/**
|
|
4
|
+
* How much a path of walls turns, the stretches it walks there and back left out: they turn
|
|
5
|
+
* nowhere.
|
|
6
|
+
* @param points - the path's vertices
|
|
7
|
+
* @returns the signed turn in radians
|
|
8
|
+
*/
|
|
9
|
+
export function turnOf(points) {
|
|
10
|
+
const course = [];
|
|
11
|
+
let turn = 0;
|
|
12
|
+
for (const point of points) {
|
|
13
|
+
const before = course[course.length - 2];
|
|
14
|
+
if (before === point)
|
|
15
|
+
course.pop();
|
|
16
|
+
else
|
|
17
|
+
course.push(point);
|
|
18
|
+
}
|
|
19
|
+
for (let i = 1; i + 1 < course.length; i++) {
|
|
20
|
+
const a = course[i - 1];
|
|
21
|
+
const b = course[i];
|
|
22
|
+
const c = course[i + 1];
|
|
23
|
+
turn += Math.atan2((b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x), (b.x - a.x) * (c.x - b.x) + (b.y - a.y) * (c.y - b.y));
|
|
24
|
+
}
|
|
25
|
+
return turn;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Whether a walk position lies in a stretch, going round the walk.
|
|
29
|
+
* @param k - the position
|
|
30
|
+
* @param start - where the stretch starts
|
|
31
|
+
* @param end - where it ends, not included
|
|
32
|
+
* @returns true when `k` lies in it
|
|
33
|
+
*/
|
|
34
|
+
const within = (k, start, end) => start <= end ? k >= start && k < end : k >= start || k < end;
|
|
35
|
+
/**
|
|
36
|
+
* The walls and bridge length a group has in all.
|
|
37
|
+
* @param graph - the wall graph
|
|
38
|
+
* @param group - the group
|
|
39
|
+
* @returns the walls, and the bridge length
|
|
40
|
+
*/
|
|
41
|
+
export function contentsOf(graph, group) {
|
|
42
|
+
let walls = 0;
|
|
43
|
+
let bridged = 0;
|
|
44
|
+
for (const index of group.edges) {
|
|
45
|
+
const edge = graph.edges[index];
|
|
46
|
+
if (edge.bridge)
|
|
47
|
+
bridged += edge.length;
|
|
48
|
+
else
|
|
49
|
+
walls++;
|
|
50
|
+
}
|
|
51
|
+
return { walls, bridged };
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Measures a group's outer walk for closing it.
|
|
55
|
+
* @param graph - the wall graph
|
|
56
|
+
* @param group - the group
|
|
57
|
+
* @returns the measured walk
|
|
58
|
+
*/
|
|
59
|
+
export function walkOf(graph, group) {
|
|
60
|
+
const { nodes, edges } = graph;
|
|
61
|
+
const walk = group.outer;
|
|
62
|
+
const prefix = [0];
|
|
63
|
+
const passes = new Map();
|
|
64
|
+
const positions = new Map();
|
|
65
|
+
walk.forEach((node, k) => {
|
|
66
|
+
const next = walk[(k + 1) % walk.length];
|
|
67
|
+
const key = edgeKey(node, next);
|
|
68
|
+
prefix.push(prefix[k] + cross(nodes[node], nodes[next]));
|
|
69
|
+
pushTo(passes, key, k);
|
|
70
|
+
pushTo(positions, node, k);
|
|
71
|
+
});
|
|
72
|
+
const byKey = new Map(group.edges.map((index) => [edgeKey(edges[index].a, edges[index].b), edges[index]]));
|
|
73
|
+
const twice = [];
|
|
74
|
+
for (const [key, at] of passes) {
|
|
75
|
+
if (at.length === 2)
|
|
76
|
+
twice.push({ edge: byKey.get(key), at: [at[0], at[1]] });
|
|
77
|
+
}
|
|
78
|
+
const { walls, bridged } = contentsOf(graph, group);
|
|
79
|
+
let outWalls = 0;
|
|
80
|
+
let outBridged = 0;
|
|
81
|
+
for (const { edge } of twice) {
|
|
82
|
+
if (edge.bridge)
|
|
83
|
+
outBridged += edge.length;
|
|
84
|
+
else
|
|
85
|
+
outWalls++;
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
walk,
|
|
89
|
+
prefix,
|
|
90
|
+
positions,
|
|
91
|
+
twice,
|
|
92
|
+
walls,
|
|
93
|
+
bridged,
|
|
94
|
+
heldWalls: group.building ? walls - outWalls : 0,
|
|
95
|
+
heldBridged: group.building ? bridged - outBridged : 0,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Twice the signed area under a stretch of the walk.
|
|
100
|
+
* @param w - the walk
|
|
101
|
+
* @param start - the stretch's first position
|
|
102
|
+
* @param end - its last
|
|
103
|
+
* @returns the shoelace sum of the stretch
|
|
104
|
+
*/
|
|
105
|
+
export const stretchSum = (w, start, end) => start <= end
|
|
106
|
+
? w.prefix[end] - w.prefix[start]
|
|
107
|
+
: w.prefix[w.walk.length] - w.prefix[start] + w.prefix[end];
|
|
108
|
+
/**
|
|
109
|
+
* A stretch of the walk.
|
|
110
|
+
* @param walk - the walk's nodes
|
|
111
|
+
* @param start - the stretch's first position
|
|
112
|
+
* @param end - its last, included
|
|
113
|
+
* @returns the stretch's nodes
|
|
114
|
+
*/
|
|
115
|
+
export function stretch(walk, start, end) {
|
|
116
|
+
const nodes = [];
|
|
117
|
+
for (let k = start;; k = (k + 1) % walk.length) {
|
|
118
|
+
nodes.push(walk[k]);
|
|
119
|
+
if (k === end)
|
|
120
|
+
return nodes;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The walls and bridge length still outside when a stretch of the walk is closed off: the edges
|
|
125
|
+
* walked on both sides by the rest of the walk only.
|
|
126
|
+
* @param w - the walk
|
|
127
|
+
* @param start - the closed stretch's first position
|
|
128
|
+
* @param end - its last
|
|
129
|
+
* @returns the walls, and the bridge length
|
|
130
|
+
*/
|
|
131
|
+
function leftOutside(w, start, end) {
|
|
132
|
+
let walls = 0;
|
|
133
|
+
let bridged = 0;
|
|
134
|
+
for (const { edge, at } of w.twice) {
|
|
135
|
+
if (!within(at[0], end, start) || !within(at[1], end, start))
|
|
136
|
+
continue;
|
|
137
|
+
if (edge.bridge)
|
|
138
|
+
bridged += edge.length;
|
|
139
|
+
else
|
|
140
|
+
walls++;
|
|
141
|
+
}
|
|
142
|
+
return { walls, bridged };
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* What closing a stretch of the walk adds to the group's building: walls and bridge length.
|
|
146
|
+
* @param w - the walk
|
|
147
|
+
* @param start - the closed stretch's first position
|
|
148
|
+
* @param end - its last
|
|
149
|
+
* @returns the walls, and the bridge length
|
|
150
|
+
*/
|
|
151
|
+
export function gainOf(w, start, end) {
|
|
152
|
+
const outside = leftOutside(w, start, end);
|
|
153
|
+
return {
|
|
154
|
+
walls: w.walls - outside.walls - w.heldWalls,
|
|
155
|
+
bridged: w.bridged - outside.bridged - w.heldBridged,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Whether a bridge from a node, at one of its places on the walk, leaves into the outside there.
|
|
160
|
+
* @param graph - the wall graph
|
|
161
|
+
* @param w - the walk
|
|
162
|
+
* @param k - the node's position on the walk
|
|
163
|
+
* @param toward - the node the bridge goes to
|
|
164
|
+
* @returns true when the bridge leaves into the side the walk runs round
|
|
165
|
+
*/
|
|
166
|
+
export function leavesOutward(graph, w, k, toward) {
|
|
167
|
+
const { nodes } = graph;
|
|
168
|
+
const node = w.walk[k];
|
|
169
|
+
const before = w.walk[(k - 1 + w.walk.length) % w.walk.length];
|
|
170
|
+
const after = w.walk[(k + 1) % w.walk.length];
|
|
171
|
+
if (before === after)
|
|
172
|
+
return true;
|
|
173
|
+
const full = 2 * Math.PI;
|
|
174
|
+
const clockwise = (from, to) => {
|
|
175
|
+
const turn = (lineAngle(nodes[node], nodes[from]) - lineAngle(nodes[node], nodes[to])) % full;
|
|
176
|
+
return turn <= 0 ? turn + full : turn;
|
|
177
|
+
};
|
|
178
|
+
return clockwise(before, toward) < clockwise(before, after);
|
|
179
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { Box, type Point2Like } from '@expofp/geometry';
|
|
2
|
+
/** Rounding error allowed in segment parameters, turns and sines. */
|
|
3
|
+
export declare const EPS = 1e-9;
|
|
4
|
+
/** One wall of a layer, read from its entry in the plan data. */
|
|
5
|
+
interface Wall {
|
|
6
|
+
/** Its vertices; a closed wall does not repeat the first one. */
|
|
7
|
+
readonly points: readonly Point2Like[];
|
|
8
|
+
/** Whether the wall ends where it began. */
|
|
9
|
+
readonly closed: boolean;
|
|
10
|
+
}
|
|
11
|
+
/** A piece of wall, or a bridge, between two nodes of the wall graph. */
|
|
12
|
+
export interface Edge {
|
|
13
|
+
/** One end; for a piece of wall, the one the wall is drawn from. */
|
|
14
|
+
readonly a: number;
|
|
15
|
+
/** The other end. */
|
|
16
|
+
readonly b: number;
|
|
17
|
+
/** A bridge closes a gap for the outline only; it is no wall. */
|
|
18
|
+
readonly bridge: boolean;
|
|
19
|
+
/** For a piece of wall, its wall by entry order and where along it the piece starts. */
|
|
20
|
+
readonly drawn: readonly [wall: number, at: number] | null;
|
|
21
|
+
/** Its length. */
|
|
22
|
+
readonly length: number;
|
|
23
|
+
}
|
|
24
|
+
/** The walls cut where they meet, as nodes and edges. */
|
|
25
|
+
export interface WallGraph {
|
|
26
|
+
/** Where walls meet or end. */
|
|
27
|
+
readonly nodes: Point2Like[];
|
|
28
|
+
/** Pieces of wall and bridges. */
|
|
29
|
+
readonly edges: Edge[];
|
|
30
|
+
/** Per node, its edges. */
|
|
31
|
+
readonly at: number[][];
|
|
32
|
+
/** Per node that ends an open wall, that wall's length. */
|
|
33
|
+
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;
|
|
36
|
+
/** Loose ends whose best closure needs a longer bridge: their group stays open. */
|
|
37
|
+
readonly blocked: Set<number>;
|
|
38
|
+
}
|
|
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
|
+
/**
|
|
50
|
+
* The cross product of two vectors.
|
|
51
|
+
* @param a - one vector
|
|
52
|
+
* @param b - the other
|
|
53
|
+
* @returns `a × b`
|
|
54
|
+
*/
|
|
55
|
+
export declare const cross: (a: Point2Like, b: Point2Like) => number;
|
|
56
|
+
/**
|
|
57
|
+
* The end of an edge that is not `node`.
|
|
58
|
+
* @param edge - the edge
|
|
59
|
+
* @param node - one of its ends
|
|
60
|
+
* @returns the other end
|
|
61
|
+
*/
|
|
62
|
+
export declare const otherEnd: (edge: Edge, node: number) => number;
|
|
63
|
+
/**
|
|
64
|
+
* A key for the edge between two nodes, either way round.
|
|
65
|
+
* @param a - one node
|
|
66
|
+
* @param b - the other
|
|
67
|
+
* @returns the key
|
|
68
|
+
*/
|
|
69
|
+
export declare const edgeKey: (a: number, b: number) => string;
|
|
70
|
+
/**
|
|
71
|
+
* Appends a value to the list under a key, starting the list if there is none.
|
|
72
|
+
* @param map - the lists
|
|
73
|
+
* @param key - the key
|
|
74
|
+
* @param value - the value
|
|
75
|
+
*/
|
|
76
|
+
export declare function pushTo<K>(map: Map<K, number[]>, key: K, value: number): void;
|
|
77
|
+
/**
|
|
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.
|
|
80
|
+
* @param polylines - the layer's walls as flat `x, y, …` lists
|
|
81
|
+
* @returns the walls of two vertices or more, in entry order
|
|
82
|
+
*/
|
|
83
|
+
export declare function readWalls(polylines: readonly (readonly number[])[]): Wall[];
|
|
84
|
+
/**
|
|
85
|
+
* Cuts every wall where another one crosses it, ends on it or runs along it, and joins the pieces
|
|
86
|
+
* at the points they share. A piece drawn twice is one edge.
|
|
87
|
+
* @param walls - the layer's walls
|
|
88
|
+
* @returns the wall graph
|
|
89
|
+
*/
|
|
90
|
+
export declare function buildGraph(walls: readonly Wall[]): WallGraph;
|
|
91
|
+
/**
|
|
92
|
+
* Where `point` lies against the line through `a` and `b`: along it (`0` at `a`, `1` at `b`) and how
|
|
93
|
+
* far off it.
|
|
94
|
+
* @param a - the line's start
|
|
95
|
+
* @param b - its end
|
|
96
|
+
* @param point - the point
|
|
97
|
+
* @returns the place along the line, and the distance from it
|
|
98
|
+
*/
|
|
99
|
+
export declare function placeOn(a: Point2Like, b: Point2Like, point: Point2Like): {
|
|
100
|
+
along: number;
|
|
101
|
+
off: number;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Adds a bridge between two nodes.
|
|
105
|
+
* @param graph - the wall graph
|
|
106
|
+
* @param a - one end
|
|
107
|
+
* @param b - the other
|
|
108
|
+
*/
|
|
109
|
+
export declare function addBridge(graph: WallGraph, a: number, b: number): void;
|
|
110
|
+
/**
|
|
111
|
+
* The nodes with a single edge: loose wall ends.
|
|
112
|
+
* @param graph - the wall graph
|
|
113
|
+
* @returns the nodes
|
|
114
|
+
*/
|
|
115
|
+
export declare function looseNodes(graph: WallGraph): number[];
|
|
116
|
+
/**
|
|
117
|
+
* Whether two nodes are already joined by an edge.
|
|
118
|
+
* @param graph - the wall graph
|
|
119
|
+
* @param a - one node
|
|
120
|
+
* @param b - the other
|
|
121
|
+
* @returns true when an edge joins them
|
|
122
|
+
*/
|
|
123
|
+
export declare const joined: (graph: WallGraph, a: number, b: number) => boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Whether a straight bridge between two nodes would cross no wall and no bridge, touching the graph
|
|
126
|
+
* at its own ends only.
|
|
127
|
+
* @param graph - the wall graph
|
|
128
|
+
* @param a - one end
|
|
129
|
+
* @param b - the other
|
|
130
|
+
* @returns true when the bridge is clear
|
|
131
|
+
*/
|
|
132
|
+
export declare function sees(graph: WallGraph, a: number, b: number): boolean;
|
|
133
|
+
export {};
|
|
134
|
+
//# sourceMappingURL=wall-graph.d.ts.map
|
|
@@ -0,0 +1,305 @@
|
|
|
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
|
+
}
|