@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,129 +0,0 @@
1
- import { pointDistance } from '@expofp/geometry';
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);
5
- /** The shortest wall a straight gap is read off, as a share of the gap: a shorter stroke has no line. */
6
- const MIN_SIDE_SHARE = 0.25;
7
- /** With no limit, the widest straight gap, as a share of the longer wall beside it: a door is narrower. */
8
- const MAX_DOOR_SHARE = 0.6;
9
- /**
10
- * The direction from one point to another, of length 1.
11
- * @param from - where it starts
12
- * @param to - where it points
13
- * @returns the direction; `null` when the points coincide
14
- */
15
- function direction(from, to) {
16
- const length = pointDistance(from, to);
17
- return length ? { x: (to.x - from.x) / length, y: (to.y - from.y) / length } : null;
18
- }
19
- /**
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.
25
- * @param graph - the wall graph; the bridges are added to it
26
- * @param maxBridge - the widest gap
27
- */
28
- export function closeStraightGaps(graph, maxBridge) {
29
- const { nodes, edges, at } = graph;
30
- const runsOnCosine = Math.sqrt(1 - STRAIGHT_SINE * STRAIGHT_SINE);
31
- const limited = Number.isFinite(maxBridge);
32
- for (const u of looseNodes(graph)) {
33
- if (at[u].length !== 1)
34
- continue;
35
- const ahead = direction(nodes[otherEnd(edges[at[u][0]], u)], nodes[u]);
36
- // 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
- if (!ahead)
39
- continue;
40
- let nearest = -1;
41
- let nearestGap = maxBridge;
42
- for (let v = 0; v < nodes.length; v++) {
43
- const x = nodes[v].x - nodes[u].x;
44
- const y = nodes[v].y - nodes[u].y;
45
- const along = x * ahead.x + y * ahead.y;
46
- if (v === u || along <= EPS || along > nearestGap || ownWall < MIN_SIDE_SHARE * along)
47
- continue;
48
- if (Math.abs(x * ahead.y - y * ahead.x) > STRAIGHT_SINE * along)
49
- continue;
50
- // The longest piece of wall going on from there along the same line, away from the gap.
51
- let runsOn = 0;
52
- for (const index of at[v]) {
53
- const edge = edges[index];
54
- const on = !edge.bridge && direction(nodes[v], nodes[otherEnd(edge, v)]);
55
- if (on && on.x * ahead.x + on.y * ahead.y >= runsOnCosine)
56
- runsOn = Math.max(runsOn, edge.length);
57
- }
58
- if (runsOn < MIN_SIDE_SHARE * along)
59
- continue;
60
- // With no limit, a gap as wide as the walls beside it may be the street between two buildings.
61
- if (!limited && along > MAX_DOOR_SHARE * Math.max(ownWall, runsOn))
62
- continue;
63
- if (!sees(graph, u, v))
64
- continue;
65
- nearest = v;
66
- nearestGap = along;
67
- }
68
- if (nearest >= 0)
69
- addBridge(graph, u, nearest);
70
- }
71
- }
72
- /**
73
- * Joins two loose wall ends, each the other's nearest, across a gap shorter than both walls where one
74
- * wall runs into the gap or the two run past each other.
75
- * @param graph - the wall graph; the bridges are added to it
76
- * @param maxBridge - the widest corner gap
77
- */
78
- export function closeCorners(graph, maxBridge) {
79
- const { nodes, edges, at, wallEnds } = graph;
80
- const out = (node) => {
81
- const from = nodes[otherEnd(edges[at[node][0]], node)];
82
- return { x: nodes[node].x - from.x, y: nodes[node].y - from.y };
83
- };
84
- const tried = new Set();
85
- // Joining only takes ends away, so each end's others are sorted by distance once.
86
- const initial = looseNodes(graph);
87
- const byDistance = new Map(initial.map((u) => {
88
- const distance = new Map(initial.map((v) => [v, pointDistance(nodes[u], nodes[v])]));
89
- const others = initial
90
- .filter((v) => v !== u)
91
- .sort((v, w) => distance.get(v) - distance.get(w) || v - w);
92
- return [u, others];
93
- }));
94
- for (;;) {
95
- const nearest = new Map();
96
- for (const u of looseNodes(graph)) {
97
- const v = byDistance.get(u).find((w) => at[w].length === 1 && sees(graph, u, w));
98
- if (v !== undefined)
99
- nearest.set(u, v);
100
- }
101
- let added = false;
102
- for (const [u, v] of nearest) {
103
- if (u > v || nearest.get(v) !== u || tried.has(edgeKey(u, v)))
104
- continue;
105
- tried.add(edgeKey(u, v));
106
- const gap = pointDistance(nodes[u], nodes[v]);
107
- if (gap >= (wallEnds.get(u) ?? 0) || gap >= (wallEnds.get(v) ?? 0))
108
- continue;
109
- const toward = { x: nodes[v].x - nodes[u].x, y: nodes[v].y - nodes[u].y };
110
- const fromU = out(u);
111
- const fromV = out(v);
112
- const intoGap = fromU.x * toward.x + fromU.y * toward.y > 0 || fromV.x * toward.x + fromV.y * toward.y < 0;
113
- const pastEachOther = fromU.x * fromV.x + fromU.y * fromV.y < 0;
114
- // Neither runs into the gap: a step in the outline when they run past each other, else two
115
- // walls side by side or a bent wall's two ends.
116
- if (!intoGap && !pastEachOther)
117
- continue;
118
- // Too wide for a corner: the walls stay open rather than close some other way.
119
- if (gap > maxBridge) {
120
- graph.blocked.add(u).add(v);
121
- continue;
122
- }
123
- addBridge(graph, u, v);
124
- added = true;
125
- }
126
- if (!added)
127
- return;
128
- }
129
- }
@@ -1,86 +0,0 @@
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
package/dist/lib/walks.js DELETED
@@ -1,179 +0,0 @@
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
- }
@@ -1,134 +0,0 @@
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