@statelyai/flow 0.13.4 → 0.15.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 +5 -1
- package/dist/esm/index.d.ts +13 -2
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +39 -14
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/layout/dagre.d.ts +2 -1
- package/dist/esm/layout/dagre.d.ts.map +1 -0
- package/dist/esm/layout/dagre.js +2 -0
- package/dist/esm/layout/dagre.js.map +1 -0
- package/dist/esm/layout/elk.d.ts +2 -1
- package/dist/esm/layout/elk.d.ts.map +1 -0
- package/dist/esm/layout/elk.js +2 -0
- package/dist/esm/layout/elk.js.map +1 -0
- package/dist/esm/layout/stately.d.ts +2 -1
- package/dist/esm/layout/stately.d.ts.map +1 -0
- package/dist/esm/layout/stately.js +2 -0
- package/dist/esm/layout/stately.js.map +1 -0
- package/dist/esm/shared.d.ts +2 -1
- package/dist/esm/shared.d.ts.map +1 -0
- package/dist/esm/shared.js +2 -0
- package/dist/esm/shared.js.map +1 -0
- package/package.json +4 -3
- package/src/__fixtures__/nested.ts +106 -0
- package/src/appearances.test.ts +428 -0
- package/src/appearances.ts +144 -0
- package/src/authoritative-route.ts +14 -0
- package/src/changeSet.test.ts +132 -0
- package/src/changeSet.ts +337 -0
- package/src/config.ts +41 -0
- package/src/configure.test.ts +212 -0
- package/src/connection-lifetime.ts +1 -0
- package/src/connectionPreview.test.ts +102 -0
- package/src/connectionPreview.ts +82 -0
- package/src/content.ts +203 -0
- package/src/createFlow.fit-frame.test.ts +256 -0
- package/src/createFlow.modern.test.ts +743 -0
- package/src/createFlow.test.ts +632 -0
- package/src/createFlow.ts +3463 -0
- package/src/createFlow.view.test.ts +212 -0
- package/src/createFlow.viewport-controls.test.ts +221 -0
- package/src/edge-geometry.ts +318 -0
- package/src/engine.cache.test.ts +728 -0
- package/src/engine.hierarchy.test.ts +1023 -0
- package/src/engine.test.ts +1965 -0
- package/src/engine.ts +3385 -0
- package/src/entity-changes.test.ts +309 -0
- package/src/entity-changes.ts +395 -0
- package/src/examples/flow-based-math.test.ts +25 -0
- package/src/examples/flow-based-math.ts +210 -0
- package/src/external-edit.test.ts +337 -0
- package/src/external-edit.ts +358 -0
- package/src/factories.test.ts +104 -0
- package/src/factories.ts +70 -0
- package/src/geometry.ts +13 -0
- package/src/graph-index.test.ts +56 -0
- package/src/graph-index.ts +194 -0
- package/src/hierarchy.test.ts +175 -0
- package/src/hierarchy.ts +330 -0
- package/src/highlights.test.ts +130 -0
- package/src/index.ts +469 -0
- package/src/input/index.ts +615 -0
- package/src/input/input.test.ts +2577 -0
- package/src/input/keymap.ts +111 -0
- package/src/input/modes.ts +1715 -0
- package/src/input/reparent.test.ts +214 -0
- package/src/layout/dagre.ts +138 -0
- package/src/layout/elk.ts +1382 -0
- package/src/layout/index.ts +12 -0
- package/src/layout/layout.test.ts +1248 -0
- package/src/layout/shared.ts +260 -0
- package/src/layout/stately-globals.d.ts +9 -0
- package/src/layout/stately.test.ts +246 -0
- package/src/layout/stately.ts +528 -0
- package/src/layout-animation.test.ts +223 -0
- package/src/layout-contract.test.ts +414 -0
- package/src/marker.test.ts +23 -0
- package/src/marker.ts +23 -0
- package/src/measurement-anchor.test.ts +232 -0
- package/src/minimap.test.ts +96 -0
- package/src/minimap.ts +137 -0
- package/src/nested-fixture.test.ts +178 -0
- package/src/port-measurement.test.ts +27 -0
- package/src/port-measurements.test.ts +187 -0
- package/src/profile.ts +124 -0
- package/src/projections.test.ts +103 -0
- package/src/projections.ts +295 -0
- package/src/render-invalidation.test.ts +62 -0
- package/src/render-invalidation.ts +55 -0
- package/src/renderer-geometry.test.ts +356 -0
- package/src/renderer-transactions.test.ts +234 -0
- package/src/routing/avoid.ts +417 -0
- package/src/routing/index.test.ts +361 -0
- package/src/routing/index.ts +366 -0
- package/src/routing/readTracking.test.ts +103 -0
- package/src/routing/readTracking.ts +233 -0
- package/src/selection-commands.test.ts +355 -0
- package/src/serialize.test.ts +167 -0
- package/src/serialize.ts +254 -0
- package/src/shapes/index.test.ts +45 -0
- package/src/shapes/index.ts +142 -0
- package/src/snap.test.ts +123 -0
- package/src/snap.ts +395 -0
- package/src/spatial.test.ts +676 -0
- package/src/spatial.ts +457 -0
- package/src/store/index.ts +4121 -0
- package/src/store.test.ts +2903 -0
- package/src/toolbar.test.ts +31 -0
- package/src/toolbar.ts +62 -0
- package/src/types.test.ts +15 -0
- package/src/types.ts +487 -0
- package/src/validation.ts +558 -0
- package/src/view.ts +172 -0
- package/src/viewport/animate.ts +285 -0
- package/src/viewport/index.ts +296 -0
- package/src/viewport.test.ts +569 -0
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import type { EdgeRouter, EdgeRouterContext } from './index';
|
|
2
|
+
import type { PathData, Point, Rect } from '../types';
|
|
3
|
+
|
|
4
|
+
export type AvoidRouterOptions = {
|
|
5
|
+
margin?: number;
|
|
6
|
+
maxGridLines?: number;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const avoidRouter: EdgeRouter = configureAvoidRouter();
|
|
10
|
+
|
|
11
|
+
export function configureAvoidRouter(options: AvoidRouterOptions = {}): EdgeRouter {
|
|
12
|
+
return (edge, ctx) => {
|
|
13
|
+
const margin = options.margin ?? 8;
|
|
14
|
+
const source = ctx.sourcePoint ?? edgeMidpoint(ctx.sourceBounds, ctx.sourceDirection);
|
|
15
|
+
const target = ctx.targetPoint ?? edgeMidpoint(ctx.targetBounds, ctx.targetDirection);
|
|
16
|
+
const obstacles = ctx.obstacles.map((rect) => inflateRect(rect, margin));
|
|
17
|
+
|
|
18
|
+
// The Hanan grid needs two coordinates per obstacle plus the endpoints, so a
|
|
19
|
+
// fixed cap bails whole edges to the (non-avoiding) fallback on dense graphs.
|
|
20
|
+
// Scale the cap with the obstacle count so dense fields still route via A*.
|
|
21
|
+
const gridCap = Math.max(
|
|
22
|
+
options.maxGridLines ?? 96,
|
|
23
|
+
obstacles.length * 2 + 8,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const points = routeThroughPoints(
|
|
27
|
+
source,
|
|
28
|
+
[...(ctx.waypoints ?? []), target],
|
|
29
|
+
obstacles,
|
|
30
|
+
gridCap,
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
if (!points) return fallbackStepPath(source, target, ctx, obstacles);
|
|
34
|
+
return {
|
|
35
|
+
points,
|
|
36
|
+
sourceDirection: ctx.sourceDirection,
|
|
37
|
+
targetDirection: ctx.targetDirection,
|
|
38
|
+
} satisfies PathData;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function fallbackStepPath(
|
|
43
|
+
source: Point,
|
|
44
|
+
target: Point,
|
|
45
|
+
ctx: EdgeRouterContext,
|
|
46
|
+
obstacles: Rect[],
|
|
47
|
+
): PathData {
|
|
48
|
+
const targets = [...(ctx.waypoints ?? []), target];
|
|
49
|
+
const naive = orthogonalFallback(source, targets, ctx.sourceDirection);
|
|
50
|
+
return {
|
|
51
|
+
points: detourAroundObstacles(naive, obstacles),
|
|
52
|
+
sourceDirection: ctx.sourceDirection,
|
|
53
|
+
targetDirection: ctx.targetDirection,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Last-resort obstacle avoidance for the orthogonal fallback: when a horizontal
|
|
59
|
+
* or vertical segment cuts through an inflated obstacle interior, reroute it
|
|
60
|
+
* over or under (or left/right of) the obstacle's outer edge. This keeps the
|
|
61
|
+
* fallback orthogonal and off obstacle interiors even when A* bails.
|
|
62
|
+
*/
|
|
63
|
+
function detourAroundObstacles(points: Point[], obstacles: Rect[]): Point[] {
|
|
64
|
+
if (obstacles.length === 0) return points;
|
|
65
|
+
let result = points;
|
|
66
|
+
// A few passes let a detour introduced for one obstacle be re-checked
|
|
67
|
+
// against the others it may now cross.
|
|
68
|
+
for (let pass = 0; pass < 4; pass++) {
|
|
69
|
+
const next: Point[] = [result[0]];
|
|
70
|
+
let changed = false;
|
|
71
|
+
for (let i = 1; i < result.length; i++) {
|
|
72
|
+
const a = next.at(-1)!;
|
|
73
|
+
const b = result[i];
|
|
74
|
+
const blocker = firstBlockingObstacle(a, b, obstacles);
|
|
75
|
+
if (!blocker) {
|
|
76
|
+
pushDistinct(next, b);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
changed = true;
|
|
80
|
+
for (const via of detourCorners(a, b, blocker)) pushDistinct(next, via);
|
|
81
|
+
pushDistinct(next, b);
|
|
82
|
+
}
|
|
83
|
+
result = simplifyPolyline(next);
|
|
84
|
+
if (!changed) break;
|
|
85
|
+
}
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function firstBlockingObstacle(a: Point, b: Point, obstacles: Rect[]): Rect | null {
|
|
90
|
+
for (const rect of obstacles) {
|
|
91
|
+
if (segmentIntersectsInterior(a, b, rect)) return rect;
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Orthogonal corner points that route from `a` toward `b` around `rect`.
|
|
98
|
+
* Picks the shorter side (above/below for horizontal, left/right for vertical).
|
|
99
|
+
*/
|
|
100
|
+
function detourCorners(a: Point, b: Point, rect: Rect): Point[] {
|
|
101
|
+
const pad = 1;
|
|
102
|
+
if (a.y === b.y) {
|
|
103
|
+
const above = rect.y - pad;
|
|
104
|
+
const below = rect.y + rect.height + pad;
|
|
105
|
+
const lane = Math.abs(a.y - above) <= Math.abs(a.y - below) ? above : below;
|
|
106
|
+
const left = Math.min(a.x, b.x) === a.x ? rect.x - pad : rect.x + rect.width + pad;
|
|
107
|
+
const right = left === rect.x - pad ? rect.x + rect.width + pad : rect.x - pad;
|
|
108
|
+
return [
|
|
109
|
+
{ x: left, y: a.y },
|
|
110
|
+
{ x: left, y: lane },
|
|
111
|
+
{ x: right, y: lane },
|
|
112
|
+
{ x: right, y: b.y },
|
|
113
|
+
];
|
|
114
|
+
}
|
|
115
|
+
if (a.x === b.x) {
|
|
116
|
+
const leftLane = rect.x - pad;
|
|
117
|
+
const rightLane = rect.x + rect.width + pad;
|
|
118
|
+
const lane = Math.abs(a.x - leftLane) <= Math.abs(a.x - rightLane) ? leftLane : rightLane;
|
|
119
|
+
const top = Math.min(a.y, b.y) === a.y ? rect.y - pad : rect.y + rect.height + pad;
|
|
120
|
+
const bottom = top === rect.y - pad ? rect.y + rect.height + pad : rect.y - pad;
|
|
121
|
+
return [
|
|
122
|
+
{ x: a.x, y: top },
|
|
123
|
+
{ x: lane, y: top },
|
|
124
|
+
{ x: lane, y: bottom },
|
|
125
|
+
{ x: a.x, y: bottom },
|
|
126
|
+
];
|
|
127
|
+
}
|
|
128
|
+
return [];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function orthogonalFallback(
|
|
132
|
+
source: Point,
|
|
133
|
+
targets: Point[],
|
|
134
|
+
sourceDirection: EdgeRouterContext['sourceDirection'],
|
|
135
|
+
): Point[] {
|
|
136
|
+
const points: Point[] = [source];
|
|
137
|
+
let current = source;
|
|
138
|
+
let direction = sourceDirection;
|
|
139
|
+
for (const target of targets) {
|
|
140
|
+
pushDistinct(points, computeStepMidpoint(current, target, direction));
|
|
141
|
+
pushDistinct(points, target);
|
|
142
|
+
direction = Math.abs(target.x - current.x) > Math.abs(target.y - current.y)
|
|
143
|
+
? target.x >= current.x ? 'right' : 'left'
|
|
144
|
+
: target.y >= current.y ? 'bottom' : 'top';
|
|
145
|
+
current = target;
|
|
146
|
+
}
|
|
147
|
+
return points;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function computeStepMidpoint(
|
|
151
|
+
source: Point,
|
|
152
|
+
target: Point,
|
|
153
|
+
sourceDirection: EdgeRouterContext['sourceDirection'],
|
|
154
|
+
): Point {
|
|
155
|
+
if (sourceDirection === 'left' || sourceDirection === 'right') {
|
|
156
|
+
return { x: (source.x + target.x) / 2, y: source.y };
|
|
157
|
+
}
|
|
158
|
+
return { x: source.x, y: (source.y + target.y) / 2 };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function routeThroughPoints(
|
|
162
|
+
source: Point,
|
|
163
|
+
targets: Point[],
|
|
164
|
+
obstacles: Rect[],
|
|
165
|
+
maxGridLines: number,
|
|
166
|
+
): Point[] | null {
|
|
167
|
+
const routed: Point[] = [source];
|
|
168
|
+
let current = source;
|
|
169
|
+
|
|
170
|
+
for (const target of targets) {
|
|
171
|
+
const segment = routeSegment(current, target, obstacles, maxGridLines);
|
|
172
|
+
if (!segment) return null;
|
|
173
|
+
for (const point of segment.slice(1)) pushDistinct(routed, point);
|
|
174
|
+
current = target;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return simplifyPolyline(routed);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function routeSegment(
|
|
181
|
+
rawSource: Point,
|
|
182
|
+
rawTarget: Point,
|
|
183
|
+
obstacles: Rect[],
|
|
184
|
+
maxGridLines: number,
|
|
185
|
+
): Point[] | null {
|
|
186
|
+
// When an endpoint falls inside an inflated obstacle (e.g. a large margin
|
|
187
|
+
// swallows the point, or a neighbor's inflation overlaps it), snap it just
|
|
188
|
+
// outside rather than bailing to the non-avoiding fallback. The original
|
|
189
|
+
// endpoint is prepended/appended so the path still starts/ends exactly there.
|
|
190
|
+
const source = snapOutside(rawSource, obstacles);
|
|
191
|
+
const target = snapOutside(rawTarget, obstacles);
|
|
192
|
+
if (pointInsideAny(source, obstacles) || pointInsideAny(target, obstacles)) return null;
|
|
193
|
+
|
|
194
|
+
const xs = uniqueSorted([
|
|
195
|
+
source.x,
|
|
196
|
+
target.x,
|
|
197
|
+
...obstacles.flatMap((rect) => [rect.x, rect.x + rect.width]),
|
|
198
|
+
]);
|
|
199
|
+
const ys = uniqueSorted([
|
|
200
|
+
source.y,
|
|
201
|
+
target.y,
|
|
202
|
+
...obstacles.flatMap((rect) => [rect.y, rect.y + rect.height]),
|
|
203
|
+
]);
|
|
204
|
+
if (xs.length > maxGridLines || ys.length > maxGridLines) return null;
|
|
205
|
+
|
|
206
|
+
const sourceKey = pointKey(source);
|
|
207
|
+
const targetKey = pointKey(target);
|
|
208
|
+
const open = new Set([sourceKey]);
|
|
209
|
+
const cameFrom = new Map<string, string>();
|
|
210
|
+
const gScore = new Map([[sourceKey, 0]]);
|
|
211
|
+
const fScore = new Map([[sourceKey, manhattan(source, target)]]);
|
|
212
|
+
|
|
213
|
+
while (open.size > 0) {
|
|
214
|
+
const currentKey = lowestScore(open, fScore);
|
|
215
|
+
const current = pointFromKey(currentKey);
|
|
216
|
+
if (currentKey === targetKey) {
|
|
217
|
+
const path = reconstruct(cameFrom, currentKey);
|
|
218
|
+
// Reattach the true endpoints if we snapped them out of an obstacle.
|
|
219
|
+
// The snapped point lies just outside the obstacle edge, so the extra
|
|
220
|
+
// stub segment is short and stays orthogonal.
|
|
221
|
+
if (rawSource.x !== source.x || rawSource.y !== source.y) path.unshift(rawSource);
|
|
222
|
+
if (rawTarget.x !== target.x || rawTarget.y !== target.y) path.push(rawTarget);
|
|
223
|
+
return path;
|
|
224
|
+
}
|
|
225
|
+
open.delete(currentKey);
|
|
226
|
+
|
|
227
|
+
for (const neighbor of neighbors(current, xs, ys, obstacles)) {
|
|
228
|
+
const neighborKey = pointKey(neighbor);
|
|
229
|
+
const tentative = (gScore.get(currentKey) ?? Infinity) + manhattan(current, neighbor);
|
|
230
|
+
if (tentative >= (gScore.get(neighborKey) ?? Infinity)) continue;
|
|
231
|
+
cameFrom.set(neighborKey, currentKey);
|
|
232
|
+
gScore.set(neighborKey, tentative);
|
|
233
|
+
fScore.set(neighborKey, tentative + manhattan(neighbor, target));
|
|
234
|
+
open.add(neighborKey);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function neighbors(
|
|
242
|
+
point: Point,
|
|
243
|
+
xs: number[],
|
|
244
|
+
ys: number[],
|
|
245
|
+
obstacles: Rect[],
|
|
246
|
+
): Point[] {
|
|
247
|
+
const result: Point[] = [];
|
|
248
|
+
const xIndex = xs.indexOf(point.x);
|
|
249
|
+
const yIndex = ys.indexOf(point.y);
|
|
250
|
+
if (xIndex < 0 || yIndex < 0) return result;
|
|
251
|
+
|
|
252
|
+
for (const nextX of [xs[xIndex - 1], xs[xIndex + 1]]) {
|
|
253
|
+
if (nextX === undefined) continue;
|
|
254
|
+
const next = { x: nextX, y: point.y };
|
|
255
|
+
if (!pointInsideAny(next, obstacles) && !segmentIntersectsAny(point, next, obstacles)) {
|
|
256
|
+
result.push(next);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
for (const nextY of [ys[yIndex - 1], ys[yIndex + 1]]) {
|
|
260
|
+
if (nextY === undefined) continue;
|
|
261
|
+
const next = { x: point.x, y: nextY };
|
|
262
|
+
if (!pointInsideAny(next, obstacles) && !segmentIntersectsAny(point, next, obstacles)) {
|
|
263
|
+
result.push(next);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return result;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function segmentIntersectsAny(a: Point, b: Point, obstacles: Rect[]): boolean {
|
|
271
|
+
return obstacles.some((rect) => segmentIntersectsInterior(a, b, rect));
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function segmentIntersectsInterior(a: Point, b: Point, rect: Rect): boolean {
|
|
275
|
+
if (a.y === b.y) {
|
|
276
|
+
const y = a.y;
|
|
277
|
+
if (y <= rect.y || y >= rect.y + rect.height) return false;
|
|
278
|
+
return Math.min(a.x, b.x) < rect.x + rect.width && Math.max(a.x, b.x) > rect.x;
|
|
279
|
+
}
|
|
280
|
+
if (a.x === b.x) {
|
|
281
|
+
const x = a.x;
|
|
282
|
+
if (x <= rect.x || x >= rect.x + rect.width) return false;
|
|
283
|
+
return Math.min(a.y, b.y) < rect.y + rect.height && Math.max(a.y, b.y) > rect.y;
|
|
284
|
+
}
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* If `point` is inside an inflated obstacle, move it just outside along the
|
|
290
|
+
* nearest edge (keeping one coordinate fixed so the stub stays orthogonal).
|
|
291
|
+
* Repeats a few times in case the point sits in overlapping inflated rects.
|
|
292
|
+
*/
|
|
293
|
+
function snapOutside(point: Point, obstacles: Rect[]): Point {
|
|
294
|
+
let current = point;
|
|
295
|
+
for (let i = 0; i < obstacles.length + 1; i++) {
|
|
296
|
+
const rect = obstacles.find((r) => pointStrictlyInside(current, r));
|
|
297
|
+
if (!rect) return current;
|
|
298
|
+
const pad = 1;
|
|
299
|
+
const toLeft = current.x - (rect.x - pad);
|
|
300
|
+
const toRight = rect.x + rect.width + pad - current.x;
|
|
301
|
+
const toTop = current.y - (rect.y - pad);
|
|
302
|
+
const toBottom = rect.y + rect.height + pad - current.y;
|
|
303
|
+
const min = Math.min(toLeft, toRight, toTop, toBottom);
|
|
304
|
+
if (min === toLeft) current = { x: rect.x - pad, y: current.y };
|
|
305
|
+
else if (min === toRight) current = { x: rect.x + rect.width + pad, y: current.y };
|
|
306
|
+
else if (min === toTop) current = { x: current.x, y: rect.y - pad };
|
|
307
|
+
else current = { x: current.x, y: rect.y + rect.height + pad };
|
|
308
|
+
}
|
|
309
|
+
return current;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function pointStrictlyInside(point: Point, rect: Rect): boolean {
|
|
313
|
+
return (
|
|
314
|
+
point.x > rect.x &&
|
|
315
|
+
point.x < rect.x + rect.width &&
|
|
316
|
+
point.y > rect.y &&
|
|
317
|
+
point.y < rect.y + rect.height
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function pointInsideAny(point: Point, obstacles: Rect[]): boolean {
|
|
322
|
+
return obstacles.some(
|
|
323
|
+
(rect) =>
|
|
324
|
+
point.x > rect.x &&
|
|
325
|
+
point.x < rect.x + rect.width &&
|
|
326
|
+
point.y > rect.y &&
|
|
327
|
+
point.y < rect.y + rect.height,
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function reconstruct(cameFrom: Map<string, string>, currentKey: string): Point[] {
|
|
332
|
+
const path = [pointFromKey(currentKey)];
|
|
333
|
+
while (cameFrom.has(currentKey)) {
|
|
334
|
+
currentKey = cameFrom.get(currentKey)!;
|
|
335
|
+
path.unshift(pointFromKey(currentKey));
|
|
336
|
+
}
|
|
337
|
+
return path;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function simplifyPolyline(points: Point[]): Point[] {
|
|
341
|
+
const deduped: Point[] = [];
|
|
342
|
+
for (const point of points) pushDistinct(deduped, point);
|
|
343
|
+
|
|
344
|
+
const simplified: Point[] = [];
|
|
345
|
+
for (const point of deduped) {
|
|
346
|
+
simplified.push(point);
|
|
347
|
+
while (simplified.length >= 3) {
|
|
348
|
+
const a = simplified[simplified.length - 3];
|
|
349
|
+
const b = simplified[simplified.length - 2];
|
|
350
|
+
const c = simplified[simplified.length - 1];
|
|
351
|
+
if ((a.x === b.x && b.x === c.x) || (a.y === b.y && b.y === c.y)) {
|
|
352
|
+
simplified.splice(simplified.length - 2, 1);
|
|
353
|
+
} else {
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return simplified;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function pushDistinct(points: Point[], point: Point): void {
|
|
362
|
+
const last = points.at(-1);
|
|
363
|
+
if (last?.x === point.x && last.y === point.y) return;
|
|
364
|
+
points.push(point);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function edgeMidpoint(rect: Rect, direction: EdgeRouterContext['sourceDirection']): Point {
|
|
368
|
+
switch (direction) {
|
|
369
|
+
case 'top':
|
|
370
|
+
return { x: rect.x + rect.width / 2, y: rect.y };
|
|
371
|
+
case 'bottom':
|
|
372
|
+
return { x: rect.x + rect.width / 2, y: rect.y + rect.height };
|
|
373
|
+
case 'left':
|
|
374
|
+
return { x: rect.x, y: rect.y + rect.height / 2 };
|
|
375
|
+
case 'right':
|
|
376
|
+
return { x: rect.x + rect.width, y: rect.y + rect.height / 2 };
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function inflateRect(rect: Rect, margin: number): Rect {
|
|
381
|
+
return {
|
|
382
|
+
x: rect.x - margin,
|
|
383
|
+
y: rect.y - margin,
|
|
384
|
+
width: rect.width + margin * 2,
|
|
385
|
+
height: rect.height + margin * 2,
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function uniqueSorted(values: number[]): number[] {
|
|
390
|
+
return [...new Set(values)].sort((a, b) => a - b);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function lowestScore(open: Set<string>, scores: Map<string, number>): string {
|
|
394
|
+
let best: string | undefined;
|
|
395
|
+
let bestScore = Infinity;
|
|
396
|
+
for (const key of open) {
|
|
397
|
+
const score = scores.get(key) ?? Infinity;
|
|
398
|
+
if (score < bestScore) {
|
|
399
|
+
best = key;
|
|
400
|
+
bestScore = score;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return best!;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function manhattan(a: Point, b: Point): number {
|
|
407
|
+
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function pointKey(point: Point): string {
|
|
411
|
+
return `${point.x},${point.y}`;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function pointFromKey(key: string): Point {
|
|
415
|
+
const [x, y] = key.split(',').map(Number);
|
|
416
|
+
return { x, y };
|
|
417
|
+
}
|