@metronforge/concept-map 0.3.2 → 0.4.1
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 -2
- package/dist/layout/hints.d.ts +2 -2
- package/dist/layout/hints.d.ts.map +1 -1
- package/dist/layout/hints.js.map +1 -1
- package/dist/layout/index.d.ts +1 -6
- package/dist/layout/index.d.ts.map +1 -1
- package/dist/layout/index.js +1 -89
- package/dist/layout/index.js.map +1 -1
- package/dist/layout/layout.d.ts +7 -0
- package/dist/layout/layout.d.ts.map +1 -0
- package/dist/layout/layout.js +83 -0
- package/dist/layout/layout.js.map +1 -0
- package/dist/layout/place-sketch.d.ts +74 -0
- package/dist/layout/place-sketch.d.ts.map +1 -0
- package/dist/layout/place-sketch.js +20 -0
- package/dist/layout/place-sketch.js.map +1 -0
- package/dist/layout/place-with-hints.d.ts +7 -0
- package/dist/layout/place-with-hints.d.ts.map +1 -0
- package/dist/layout/place-with-hints.js +17 -0
- package/dist/layout/place-with-hints.js.map +1 -0
- package/dist/layout/relations/align-layout.d.ts.map +1 -1
- package/dist/layout/relations/align-layout.js +6 -2
- package/dist/layout/relations/align-layout.js.map +1 -1
- package/dist/layout/relations/apply.js +1 -1
- package/dist/layout/relations/apply.js.map +1 -1
- package/dist/layout/relations/bounds.d.ts +24 -0
- package/dist/layout/relations/bounds.d.ts.map +1 -0
- package/dist/layout/relations/bounds.js +126 -0
- package/dist/layout/relations/bounds.js.map +1 -0
- package/dist/layout/relations/check-linear.d.ts +17 -0
- package/dist/layout/relations/check-linear.d.ts.map +1 -0
- package/dist/layout/relations/check-linear.js +88 -0
- package/dist/layout/relations/check-linear.js.map +1 -0
- package/dist/layout/relations/check-objective.d.ts +10 -0
- package/dist/layout/relations/check-objective.d.ts.map +1 -0
- package/dist/layout/relations/check-objective.js +41 -0
- package/dist/layout/relations/check-objective.js.map +1 -0
- package/dist/layout/relations/check-witness.d.ts +4 -0
- package/dist/layout/relations/check-witness.d.ts.map +1 -1
- package/dist/layout/relations/check-witness.js +86 -5
- package/dist/layout/relations/check-witness.js.map +1 -1
- package/dist/layout/relations/cross.d.ts +1 -1
- package/dist/layout/relations/cross.d.ts.map +1 -1
- package/dist/layout/relations/cross.js +2 -2
- package/dist/layout/relations/cross.js.map +1 -1
- package/dist/layout/relations/derive.d.ts.map +1 -1
- package/dist/layout/relations/derive.js +18 -9
- package/dist/layout/relations/derive.js.map +1 -1
- package/dist/layout/relations/derived-input.d.ts +3 -0
- package/dist/layout/relations/derived-input.d.ts.map +1 -1
- package/dist/layout/relations/derived-input.js +9 -0
- package/dist/layout/relations/derived-input.js.map +1 -1
- package/dist/layout/relations/explain.d.ts +5 -0
- package/dist/layout/relations/explain.d.ts.map +1 -1
- package/dist/layout/relations/explain.js +53 -28
- package/dist/layout/relations/explain.js.map +1 -1
- package/dist/layout/relations/free-witness.d.ts +8 -0
- package/dist/layout/relations/free-witness.d.ts.map +1 -0
- package/dist/layout/relations/free-witness.js +38 -0
- package/dist/layout/relations/free-witness.js.map +1 -0
- package/dist/layout/relations/implication.d.ts +4 -0
- package/dist/layout/relations/implication.d.ts.map +1 -0
- package/dist/layout/relations/implication.js +23 -0
- package/dist/layout/relations/implication.js.map +1 -0
- package/dist/layout/relations/linear-differences.d.ts +16 -0
- package/dist/layout/relations/linear-differences.d.ts.map +1 -0
- package/dist/layout/relations/linear-differences.js +57 -0
- package/dist/layout/relations/linear-differences.js.map +1 -0
- package/dist/layout/relations/linear.d.ts +25 -0
- package/dist/layout/relations/linear.d.ts.map +1 -0
- package/dist/layout/relations/linear.js +75 -0
- package/dist/layout/relations/linear.js.map +1 -0
- package/dist/layout/relations/min-cost-flow.d.ts +7 -0
- package/dist/layout/relations/min-cost-flow.d.ts.map +1 -0
- package/dist/layout/relations/min-cost-flow.js +79 -0
- package/dist/layout/relations/min-cost-flow.js.map +1 -0
- package/dist/layout/relations/objective.d.ts +14 -0
- package/dist/layout/relations/objective.d.ts.map +1 -0
- package/dist/layout/relations/objective.js +33 -0
- package/dist/layout/relations/objective.js.map +1 -0
- package/dist/layout/relations/optimise.d.ts +20 -0
- package/dist/layout/relations/optimise.d.ts.map +1 -0
- package/dist/layout/relations/optimise.js +54 -0
- package/dist/layout/relations/optimise.js.map +1 -0
- package/dist/layout/relations/paths.d.ts +6 -0
- package/dist/layout/relations/paths.d.ts.map +1 -1
- package/dist/layout/relations/paths.js +35 -0
- package/dist/layout/relations/paths.js.map +1 -1
- package/dist/layout/relations/rank.d.ts +3 -2
- package/dist/layout/relations/rank.d.ts.map +1 -1
- package/dist/layout/relations/rank.js +5 -5
- package/dist/layout/relations/rank.js.map +1 -1
- package/dist/layout/relations/rational.d.ts +13 -0
- package/dist/layout/relations/rational.d.ts.map +1 -0
- package/dist/layout/relations/rational.js +28 -0
- package/dist/layout/relations/rational.js.map +1 -0
- package/dist/layout/relations/reduce-objective.d.ts +12 -0
- package/dist/layout/relations/reduce-objective.d.ts.map +1 -0
- package/dist/layout/relations/reduce-objective.js +37 -0
- package/dist/layout/relations/reduce-objective.js.map +1 -0
- package/dist/layout/relations/sketch-order.d.ts +7 -0
- package/dist/layout/relations/sketch-order.d.ts.map +1 -0
- package/dist/layout/relations/sketch-order.js +67 -0
- package/dist/layout/relations/sketch-order.js.map +1 -0
- package/dist/layout/relations/sketch-placement.d.ts +14 -0
- package/dist/layout/relations/sketch-placement.d.ts.map +1 -0
- package/dist/layout/relations/sketch-placement.js +123 -0
- package/dist/layout/relations/sketch-placement.js.map +1 -0
- package/dist/layout/relations/sketch-ranks.d.ts +5 -0
- package/dist/layout/relations/sketch-ranks.d.ts.map +1 -0
- package/dist/layout/relations/sketch-ranks.js +26 -0
- package/dist/layout/relations/sketch-ranks.js.map +1 -0
- package/dist/layout/relations/solve.d.ts +4 -1
- package/dist/layout/relations/solve.d.ts.map +1 -1
- package/dist/layout/relations/solve.js +10 -6
- package/dist/layout/relations/solve.js.map +1 -1
- package/dist/layout/route/channel-layout.d.ts +1 -0
- package/dist/layout/route/channel-layout.d.ts.map +1 -1
- package/dist/layout/route/channel-layout.js +4 -0
- package/dist/layout/route/channel-layout.js.map +1 -1
- package/dist/layout/route/channel-moved.d.ts +1 -1
- package/dist/layout/route/channel-moved.d.ts.map +1 -1
- package/dist/layout/route/channel-moved.js +8 -5
- package/dist/layout/route/channel-moved.js.map +1 -1
- package/dist/layout/sketch-agreement.d.ts +7 -0
- package/dist/layout/sketch-agreement.d.ts.map +1 -0
- package/dist/layout/sketch-agreement.js +48 -0
- package/dist/layout/sketch-agreement.js.map +1 -0
- package/dist/layout/sketch-bends.d.ts +5 -0
- package/dist/layout/sketch-bends.d.ts.map +1 -0
- package/dist/layout/sketch-bends.js +21 -0
- package/dist/layout/sketch-bends.js.map +1 -0
- package/dist/layout/sketch-conflicts.d.ts +8 -0
- package/dist/layout/sketch-conflicts.d.ts.map +1 -0
- package/dist/layout/sketch-conflicts.js +56 -0
- package/dist/layout/sketch-conflicts.js.map +1 -0
- package/dist/layout/sketch-returns.d.ts +4 -0
- package/dist/layout/sketch-returns.d.ts.map +1 -0
- package/dist/layout/sketch-returns.js +67 -0
- package/dist/layout/sketch-returns.js.map +1 -0
- package/dist/layout/sketch-route.d.ts +12 -0
- package/dist/layout/sketch-route.d.ts.map +1 -0
- package/dist/layout/sketch-route.js +109 -0
- package/dist/layout/sketch-route.js.map +1 -0
- package/dist/layout/sketch-safety.d.ts +6 -0
- package/dist/layout/sketch-safety.d.ts.map +1 -0
- package/dist/layout/sketch-safety.js +29 -0
- package/dist/layout/sketch-safety.js.map +1 -0
- package/dist/layout/sketch-shortcuts.d.ts +4 -0
- package/dist/layout/sketch-shortcuts.d.ts.map +1 -0
- package/dist/layout/sketch-shortcuts.js +51 -0
- package/dist/layout/sketch-shortcuts.js.map +1 -0
- package/dist/layout/stages/hints.d.ts.map +1 -1
- package/dist/layout/stages/hints.js +33 -15
- package/dist/layout/stages/hints.js.map +1 -1
- package/dist/layout/width.d.ts +1 -0
- package/dist/layout/width.d.ts.map +1 -1
- package/dist/layout/width.js +3 -0
- package/dist/layout/width.js.map +1 -1
- package/package.json +2 -2
- package/src/layout/hints.ts +2 -2
- package/src/layout/index.ts +1 -155
- package/src/layout/layout.ts +147 -0
- package/src/layout/place-sketch.ts +39 -0
- package/src/layout/place-with-hints.ts +32 -0
- package/src/layout/relations/align-layout.ts +11 -6
- package/src/layout/relations/apply.ts +1 -1
- package/src/layout/relations/bounds.ts +163 -0
- package/src/layout/relations/check-linear.ts +153 -0
- package/src/layout/relations/check-objective.ts +60 -0
- package/src/layout/relations/check-witness.ts +118 -5
- package/src/layout/relations/cross.ts +2 -1
- package/src/layout/relations/derive.ts +24 -9
- package/src/layout/relations/derived-input.ts +13 -0
- package/src/layout/relations/explain.ts +69 -32
- package/src/layout/relations/free-witness.ts +41 -0
- package/src/layout/relations/implication.ts +30 -0
- package/src/layout/relations/linear-differences.ts +77 -0
- package/src/layout/relations/linear.ts +107 -0
- package/src/layout/relations/min-cost-flow.ts +96 -0
- package/src/layout/relations/objective.ts +53 -0
- package/src/layout/relations/optimise.ts +97 -0
- package/src/layout/relations/paths.ts +35 -0
- package/src/layout/relations/rank.ts +7 -4
- package/src/layout/relations/rational.ts +39 -0
- package/src/layout/relations/reduce-objective.ts +60 -0
- package/src/layout/relations/sketch-order.ts +90 -0
- package/src/layout/relations/sketch-placement.ts +174 -0
- package/src/layout/relations/sketch-ranks.ts +29 -0
- package/src/layout/relations/solve.ts +14 -6
- package/src/layout/route/channel-layout.ts +7 -0
- package/src/layout/route/channel-moved.ts +8 -6
- package/src/layout/sketch-agreement.ts +69 -0
- package/src/layout/sketch-bends.ts +29 -0
- package/src/layout/sketch-conflicts.ts +69 -0
- package/src/layout/sketch-returns.ts +81 -0
- package/src/layout/sketch-route.ts +199 -0
- package/src/layout/sketch-safety.ts +64 -0
- package/src/layout/sketch-shortcuts.ts +65 -0
- package/src/layout/stages/hints.ts +47 -19
- package/src/layout/width.ts +4 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { LayoutProblem } from "./problem.js";
|
|
2
|
+
import { shortenArrows } from "./sketch-shortcuts.js";
|
|
3
|
+
import { sketchOrder } from "./sketch-agreement.js";
|
|
4
|
+
import type { LayoutGraph, LayoutPoint, LayoutResult } from "./types.js";
|
|
5
|
+
import { routeBounds, routeLayout } from "./route/channel-layout.js";
|
|
6
|
+
|
|
7
|
+
/** Try one straight return at a time through the overlap of its two boxes. */
|
|
8
|
+
function* alignedReturns(graph: LayoutGraph, placed: LayoutResult) {
|
|
9
|
+
const bounds = routeBounds(placed.nodes);
|
|
10
|
+
const rank = graph.direction === "right" ? 0 : 1;
|
|
11
|
+
const cross = 1 - rank;
|
|
12
|
+
for (const edge of graph.edges) {
|
|
13
|
+
const from = placed.nodes[edge.from];
|
|
14
|
+
const to = placed.nodes[edge.to];
|
|
15
|
+
const end = to[rank] + to[rank + 2];
|
|
16
|
+
if (from[rank] <= end) continue;
|
|
17
|
+
const lo = Math.max(from[cross], to[cross]);
|
|
18
|
+
const hi = Math.min(
|
|
19
|
+
from[cross] + from[cross + 2],
|
|
20
|
+
to[cross] + to[cross + 2],
|
|
21
|
+
);
|
|
22
|
+
if (lo >= hi) continue;
|
|
23
|
+
const mid = (lo + hi) / 2;
|
|
24
|
+
const points: LayoutPoint[] =
|
|
25
|
+
rank === 1
|
|
26
|
+
? [
|
|
27
|
+
[mid, from[rank]],
|
|
28
|
+
[mid, end],
|
|
29
|
+
]
|
|
30
|
+
: [
|
|
31
|
+
[from[rank], mid],
|
|
32
|
+
[end, mid],
|
|
33
|
+
];
|
|
34
|
+
const edges = { ...placed.edges, [edge.id]: { points } };
|
|
35
|
+
const route = routeLayout(
|
|
36
|
+
{ ...graph, edges: [edge] },
|
|
37
|
+
{ ...placed, edges },
|
|
38
|
+
placed.nodes,
|
|
39
|
+
bounds,
|
|
40
|
+
);
|
|
41
|
+
if (route) yield route;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function shortenSketchRoutes(
|
|
46
|
+
problem: LayoutProblem,
|
|
47
|
+
graph: LayoutGraph,
|
|
48
|
+
original: LayoutResult,
|
|
49
|
+
placed: LayoutResult,
|
|
50
|
+
candidates: (LayoutResult | null)[],
|
|
51
|
+
bends: (result: LayoutResult) => number,
|
|
52
|
+
safe: (base: LayoutResult, route: LayoutResult) => boolean,
|
|
53
|
+
): LayoutResult {
|
|
54
|
+
if (problem.constraints.some((hint) => hint.kind === "side")) return placed;
|
|
55
|
+
let best = placed;
|
|
56
|
+
const repairable: LayoutResult[] = [];
|
|
57
|
+
const originalBends = bends(original);
|
|
58
|
+
const cap = () => Math.min(originalBends, bends(best) + 4);
|
|
59
|
+
const improvesOrder = (route: LayoutResult) =>
|
|
60
|
+
sketchOrder(problem, route.nodes) > sketchOrder(problem, best.nodes);
|
|
61
|
+
const safeFinal = (route: LayoutResult) =>
|
|
62
|
+
safe(original, route) && safe(best, route);
|
|
63
|
+
for (const candidate of candidates) {
|
|
64
|
+
if (!candidate || !improvesOrder(candidate)) continue;
|
|
65
|
+
const excess = bends(candidate) - cap();
|
|
66
|
+
if (excess <= 0 || !safe(original, candidate)) continue;
|
|
67
|
+
if (excess <= 4) repairable.push(candidate);
|
|
68
|
+
for (const route of alignedReturns(graph, candidate)) {
|
|
69
|
+
if (bends(route) <= cap() && safe(candidate, route) && safeFinal(route)) {
|
|
70
|
+
best = route;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const cheapest = repairable
|
|
76
|
+
.filter(improvesOrder)
|
|
77
|
+
.sort((a, b) => bends(a) - bends(b))[0];
|
|
78
|
+
if (!cheapest) return best;
|
|
79
|
+
const route = shortenArrows(graph, cheapest, cap(), bends, safe);
|
|
80
|
+
return route && safeFinal(route) ? route : best;
|
|
81
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import type { RecordSketchConflict } from "./relations/sketch-order.js";
|
|
2
|
+
import { needsWidthPass } from "./width.js";
|
|
3
|
+
import { sketchOrder, meritsSketchCheck } from "./sketch-agreement.js";
|
|
4
|
+
import type { ResolvedMap, SourceMap } from "../schema.js";
|
|
5
|
+
import type { LayoutProblem } from "./problem.js";
|
|
6
|
+
import type { LayoutGraph, LayoutResult } from "./types.js";
|
|
7
|
+
import type { Measurer } from "./ports.js";
|
|
8
|
+
import type { RouteRefinement } from "./inspect-routes.js";
|
|
9
|
+
import { sketchPlacements } from "./relations/sketch-placement.js";
|
|
10
|
+
import { shortenSketchRoutes } from "./sketch-returns.js";
|
|
11
|
+
import { routeMoved } from "./route/channel-moved.js";
|
|
12
|
+
import { routeLayout, routeBounds } from "./route/channel-layout.js";
|
|
13
|
+
import { resolveMeasured } from "./resolve-measured.js";
|
|
14
|
+
import { placeAnnotations } from "./stages/placement.js";
|
|
15
|
+
import { checkHard } from "./stages/hard-check.js";
|
|
16
|
+
import { resolvedBends, bendCount } from "./sketch-bends.js";
|
|
17
|
+
import { safeSketch } from "./sketch-safety.js";
|
|
18
|
+
export { safeSketch } from "./sketch-safety.js";
|
|
19
|
+
/** Keep sketch order gains with at most four extra bends and no new hard
|
|
20
|
+
* contacts or crossings, before and after width refinement. */
|
|
21
|
+
export function routeSketch(
|
|
22
|
+
source: SourceMap,
|
|
23
|
+
problem: LayoutProblem,
|
|
24
|
+
graph: LayoutGraph,
|
|
25
|
+
original: LayoutResult,
|
|
26
|
+
measurer: Measurer,
|
|
27
|
+
refinement?: RouteRefinement,
|
|
28
|
+
sizingWidthEm?: number,
|
|
29
|
+
record?: RecordSketchConflict,
|
|
30
|
+
): LayoutResult {
|
|
31
|
+
const checks = {
|
|
32
|
+
draw: (result: LayoutResult) =>
|
|
33
|
+
resolveMeasured(source, graph, result, measurer),
|
|
34
|
+
safe: (base: LayoutResult, route: LayoutResult) =>
|
|
35
|
+
safeSketch(source, graph, base, route, measurer, refinement),
|
|
36
|
+
};
|
|
37
|
+
const placed = choosePlacement(
|
|
38
|
+
problem,
|
|
39
|
+
graph,
|
|
40
|
+
original,
|
|
41
|
+
checks,
|
|
42
|
+
sizingWidthEm,
|
|
43
|
+
record,
|
|
44
|
+
);
|
|
45
|
+
return chooseDetour(source, problem, graph, placed, measurer, checks);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function chooseDetour(
|
|
49
|
+
source: SourceMap,
|
|
50
|
+
problem: LayoutProblem,
|
|
51
|
+
graph: LayoutGraph,
|
|
52
|
+
placed: LayoutResult,
|
|
53
|
+
measurer: Measurer,
|
|
54
|
+
checks: SketchChecks,
|
|
55
|
+
): LayoutResult {
|
|
56
|
+
const draw = (result: LayoutResult) => placeAnnotations(checks.draw(result));
|
|
57
|
+
const before = draw(placed);
|
|
58
|
+
const options = { measureLabel: measurer };
|
|
59
|
+
const initial = checkHard(source, before, options);
|
|
60
|
+
let best = placed;
|
|
61
|
+
let hard = initial.hard;
|
|
62
|
+
const routes = detours(problem, graph, placed, initial.lines.boxes);
|
|
63
|
+
for (const candidate of routes) {
|
|
64
|
+
const after = draw(candidate);
|
|
65
|
+
// A wider first pass enlarges boxes when layout remeasures at a given width.
|
|
66
|
+
if (after.viewBox[0] > before.viewBox[0]) continue;
|
|
67
|
+
const defects = checkHard(source, after, options).hard;
|
|
68
|
+
if (
|
|
69
|
+
defects < hard &&
|
|
70
|
+
resolvedBends(after) <= resolvedBends(before) &&
|
|
71
|
+
checks.safe(placed, candidate)
|
|
72
|
+
) {
|
|
73
|
+
best = candidate;
|
|
74
|
+
hard = defects;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return best;
|
|
78
|
+
}
|
|
79
|
+
interface SketchChecks {
|
|
80
|
+
draw(result: LayoutResult): ResolvedMap;
|
|
81
|
+
safe(base: LayoutResult, route: LayoutResult): boolean;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function choosePlacement(
|
|
85
|
+
problem: LayoutProblem,
|
|
86
|
+
graph: LayoutGraph,
|
|
87
|
+
original: LayoutResult,
|
|
88
|
+
{ draw, safe }: SketchChecks,
|
|
89
|
+
sizingWidthEm?: number,
|
|
90
|
+
record?: RecordSketchConflict,
|
|
91
|
+
): LayoutResult {
|
|
92
|
+
const candidates = placementRoutes(problem, graph, original, record);
|
|
93
|
+
const bends = (result: LayoutResult) => resolvedBends(draw(result));
|
|
94
|
+
const originalBends = bends(original);
|
|
95
|
+
let best = original;
|
|
96
|
+
for (const option of candidates) {
|
|
97
|
+
if (!option) continue;
|
|
98
|
+
const gain =
|
|
99
|
+
sketchOrder(problem, option.nodes) - sketchOrder(problem, best.nodes);
|
|
100
|
+
if (
|
|
101
|
+
meritsSketchCheck(problem, gain, best, option) &&
|
|
102
|
+
bends(option) <=
|
|
103
|
+
Math.min(originalBends, bends(best) + (gain > 0 ? 4 : 0)) &&
|
|
104
|
+
safe(original, option)
|
|
105
|
+
)
|
|
106
|
+
best = option;
|
|
107
|
+
}
|
|
108
|
+
// Align return ports on the final box sizes. A sizing pass only estimates text width.
|
|
109
|
+
if (sizingWidthEm !== undefined && needsWidthPass(draw(best), sizingWidthEm))
|
|
110
|
+
return best;
|
|
111
|
+
return shortenSketchRoutes(
|
|
112
|
+
problem,
|
|
113
|
+
graph,
|
|
114
|
+
original,
|
|
115
|
+
best,
|
|
116
|
+
candidates,
|
|
117
|
+
bends,
|
|
118
|
+
safe,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function placementRoutes(
|
|
123
|
+
problem: LayoutProblem,
|
|
124
|
+
graph: LayoutGraph,
|
|
125
|
+
original: LayoutResult,
|
|
126
|
+
record?: RecordSketchConflict,
|
|
127
|
+
) {
|
|
128
|
+
const rerouted = routeLayout(
|
|
129
|
+
graph,
|
|
130
|
+
original,
|
|
131
|
+
original.nodes,
|
|
132
|
+
routeBounds(original.nodes),
|
|
133
|
+
);
|
|
134
|
+
const candidates = sketchPlacements(problem, original, record).flatMap(
|
|
135
|
+
(nodes) => sketchRoutes(problem, graph, original, nodes),
|
|
136
|
+
);
|
|
137
|
+
return [rerouted, ...candidates];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** A side shared by both ends can take an arrow around an intervening box. */
|
|
141
|
+
function* detours(
|
|
142
|
+
problem: LayoutProblem,
|
|
143
|
+
graph: LayoutGraph,
|
|
144
|
+
placed: LayoutResult,
|
|
145
|
+
contacts: ReturnType<typeof checkHard>["lines"]["boxes"],
|
|
146
|
+
) {
|
|
147
|
+
if (!contacts.length) return;
|
|
148
|
+
for (const edge of graph.edges) {
|
|
149
|
+
if (
|
|
150
|
+
!contacts.some((c) => c.band === "defect" && c.b === `line:${edge.id}`) ||
|
|
151
|
+
problem.constraints.some((h) => h.kind === "side" && h.edge === edge.id)
|
|
152
|
+
)
|
|
153
|
+
continue;
|
|
154
|
+
for (const side of ["right", "left", "top", "bottom"] as const) {
|
|
155
|
+
const route = routeLayout(
|
|
156
|
+
{ ...graph, edges: [edge] },
|
|
157
|
+
placed,
|
|
158
|
+
placed.nodes,
|
|
159
|
+
routeBounds(placed.nodes),
|
|
160
|
+
{ [edge.id]: { from: side, to: side } },
|
|
161
|
+
);
|
|
162
|
+
if (route) yield route;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function sketchRoutes(
|
|
168
|
+
problem: LayoutProblem,
|
|
169
|
+
graph: LayoutGraph,
|
|
170
|
+
original: LayoutResult,
|
|
171
|
+
nodes: LayoutResult["nodes"] | null,
|
|
172
|
+
): (LayoutResult | null)[] {
|
|
173
|
+
if (!nodes) return [];
|
|
174
|
+
const bounds = routeBounds(nodes);
|
|
175
|
+
const routes = [
|
|
176
|
+
routeMoved(graph, original, nodes, bounds),
|
|
177
|
+
routeLayout(graph, original, nodes, bounds),
|
|
178
|
+
];
|
|
179
|
+
// Engine ports can face away from neighbours after a sketch reorders boxes.
|
|
180
|
+
if (!problem.constraints.some((hint) => hint.kind === "side")) {
|
|
181
|
+
const pins = new Set(
|
|
182
|
+
problem.constraints.flatMap((hint) =>
|
|
183
|
+
hint.kind === "pin" ? [hint.node] : [],
|
|
184
|
+
),
|
|
185
|
+
);
|
|
186
|
+
if (pins.size > 1) {
|
|
187
|
+
const facing = routeMoved(graph, original, nodes, bounds, pins);
|
|
188
|
+
routes.push(facing);
|
|
189
|
+
// Perpendicular ports can remove a bend between diagonally separated boxes.
|
|
190
|
+
if (
|
|
191
|
+
facing &&
|
|
192
|
+
bendCount(facing) > bendCount(original) &&
|
|
193
|
+
sketchOrder(problem, nodes) > sketchOrder(problem, original.nodes)
|
|
194
|
+
)
|
|
195
|
+
routes.push(routeMoved(graph, original, nodes, bounds, pins, true));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return routes;
|
|
199
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { SourceMap } from "../schema.js";
|
|
2
|
+
import type { LayoutGraph, LayoutResult } from "./types.js";
|
|
3
|
+
import type { Measurer } from "./ports.js";
|
|
4
|
+
import type { RouteRefinement } from "./inspect-routes.js";
|
|
5
|
+
import { createRouteInspector } from "./inspect-routes.js";
|
|
6
|
+
import { resolveMeasured } from "./resolve-measured.js";
|
|
7
|
+
import { placeAnnotations } from "./stages/placement.js";
|
|
8
|
+
import { check } from "./stages/report.js";
|
|
9
|
+
import { refine } from "./stages/refine.js";
|
|
10
|
+
import { atWidth } from "./width.js";
|
|
11
|
+
|
|
12
|
+
export function safeSketch(
|
|
13
|
+
source: SourceMap,
|
|
14
|
+
graph: LayoutGraph,
|
|
15
|
+
original: LayoutResult,
|
|
16
|
+
candidate: LayoutResult,
|
|
17
|
+
measurer: Measurer,
|
|
18
|
+
refinement?: RouteRefinement,
|
|
19
|
+
): boolean {
|
|
20
|
+
const inspect = createRouteInspector(
|
|
21
|
+
source,
|
|
22
|
+
graph,
|
|
23
|
+
original,
|
|
24
|
+
measurer,
|
|
25
|
+
refinement,
|
|
26
|
+
);
|
|
27
|
+
return (
|
|
28
|
+
!inspect(candidate).length &&
|
|
29
|
+
!addsCrossings(source, graph, original, candidate, measurer, refinement)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function addsCrossings(
|
|
34
|
+
source: SourceMap,
|
|
35
|
+
graph: LayoutGraph,
|
|
36
|
+
original: LayoutResult,
|
|
37
|
+
candidate: LayoutResult,
|
|
38
|
+
measurer: Measurer,
|
|
39
|
+
refinement?: RouteRefinement,
|
|
40
|
+
): boolean {
|
|
41
|
+
const resolved = (result: LayoutResult) =>
|
|
42
|
+
placeAnnotations(resolveMeasured(source, graph, result, measurer));
|
|
43
|
+
const before = resolved(original);
|
|
44
|
+
const after = resolved(candidate);
|
|
45
|
+
const report = check(source, after);
|
|
46
|
+
if (
|
|
47
|
+
report.constraints.some(
|
|
48
|
+
(constraint) => constraint.strength === "required" && !constraint.holds,
|
|
49
|
+
)
|
|
50
|
+
)
|
|
51
|
+
return true;
|
|
52
|
+
if (report.crossings.count > check(source, before).crossings.count)
|
|
53
|
+
return true;
|
|
54
|
+
if (refinement) {
|
|
55
|
+
const narrow = (result: typeof before) =>
|
|
56
|
+
refine(result, atWidth(refinement.measurer, result, refinement.widthEm));
|
|
57
|
+
if (
|
|
58
|
+
check(source, narrow(after)).crossings.count >
|
|
59
|
+
check(source, narrow(before)).crossings.count
|
|
60
|
+
)
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { LayoutBox, LayoutGraph, LayoutResult } from "./types.js";
|
|
2
|
+
import { routeBounds, routeLayout } from "./route/channel-layout.js";
|
|
3
|
+
|
|
4
|
+
function* portPairs(from: LayoutBox, to: LayoutBox) {
|
|
5
|
+
const right = 2 * from[0] + from[2] < 2 * to[0] + to[2];
|
|
6
|
+
const down = 2 * from[1] + from[3] < 2 * to[1] + to[3];
|
|
7
|
+
yield {
|
|
8
|
+
from: down ? "bottom" : "top",
|
|
9
|
+
to: right ? "left" : "right",
|
|
10
|
+
} as const;
|
|
11
|
+
yield {
|
|
12
|
+
from: right ? "right" : "left",
|
|
13
|
+
to: down ? "top" : "bottom",
|
|
14
|
+
} as const;
|
|
15
|
+
for (const side of ["top", "right", "bottom", "left"] as const)
|
|
16
|
+
yield { from: side, to: side };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function* shortcuts(graph: LayoutGraph, current: LayoutResult) {
|
|
20
|
+
const bounds = routeBounds(current.nodes);
|
|
21
|
+
for (const edge of graph.edges) {
|
|
22
|
+
if (current.edges[edge.id].points.length < 4) continue;
|
|
23
|
+
for (const pair of portPairs(
|
|
24
|
+
current.nodes[edge.from],
|
|
25
|
+
current.nodes[edge.to],
|
|
26
|
+
)) {
|
|
27
|
+
const route = routeLayout(
|
|
28
|
+
{ ...graph, edges: [edge] },
|
|
29
|
+
current,
|
|
30
|
+
current.nodes,
|
|
31
|
+
bounds,
|
|
32
|
+
{
|
|
33
|
+
[edge.id]: pair,
|
|
34
|
+
},
|
|
35
|
+
);
|
|
36
|
+
if (route) yield route;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Keep at most two single-arrow changes on the cheapest safe order gain. */
|
|
42
|
+
export function shortenArrows(
|
|
43
|
+
graph: LayoutGraph,
|
|
44
|
+
candidate: LayoutResult,
|
|
45
|
+
cap: number,
|
|
46
|
+
bends: (route: LayoutResult) => number,
|
|
47
|
+
safe: (base: LayoutResult, route: LayoutResult) => boolean,
|
|
48
|
+
) {
|
|
49
|
+
let current = candidate;
|
|
50
|
+
for (let step = 0; step < 2; step++) {
|
|
51
|
+
let best = current;
|
|
52
|
+
let count = bends(current);
|
|
53
|
+
for (const option of shortcuts(graph, current)) {
|
|
54
|
+
const turns = bends(option);
|
|
55
|
+
if (turns < count && safe(current, option)) {
|
|
56
|
+
best = option;
|
|
57
|
+
count = turns;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (count <= cap) return best;
|
|
61
|
+
if (best === current) break;
|
|
62
|
+
current = best;
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
@@ -33,40 +33,68 @@ function sideHolds(
|
|
|
33
33
|
);
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
function pinVerdict(
|
|
37
|
+
hint: Extract<StatedHint, { kind: "pin" }>,
|
|
38
|
+
pins: Extract<StatedHint, { kind: "pin" }>[],
|
|
39
|
+
resolved: ResolvedMap,
|
|
40
|
+
axis: number,
|
|
41
|
+
): ConstraintVerdict {
|
|
42
|
+
const box = resolved.nodes[hint.node]?.box;
|
|
43
|
+
const distance = box
|
|
44
|
+
? Math.hypot(...hint.at.map((at, i) => box[i] + box[i + 2] / 2 - at))
|
|
45
|
+
: Infinity;
|
|
46
|
+
const holds =
|
|
47
|
+
hint.strength === "required"
|
|
48
|
+
? distance <= 1e-6
|
|
49
|
+
: !box ||
|
|
50
|
+
pins.some((other) => {
|
|
51
|
+
const target = resolved.nodes[other.node]?.box;
|
|
52
|
+
const order = Math.sign(hint.at[1 - axis] - other.at[1 - axis]);
|
|
53
|
+
return (
|
|
54
|
+
Math.round(other.at[axis] * 64) ===
|
|
55
|
+
Math.round(hint.at[axis] * 64) &&
|
|
56
|
+
(box[axis] !== target?.[axis] ||
|
|
57
|
+
(order &&
|
|
58
|
+
Math.sign(box[1 - axis] - target![1 - axis]) !== order))
|
|
59
|
+
);
|
|
60
|
+
})
|
|
61
|
+
? false
|
|
62
|
+
: null;
|
|
63
|
+
return {
|
|
64
|
+
...hint,
|
|
65
|
+
holds,
|
|
66
|
+
message: `${constraintName(hint)}: distance: ${distance}`,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
36
70
|
/** Geometry is authoritative; rank exclusion cannot be inferred from it. */
|
|
37
71
|
export function checkHints(
|
|
38
72
|
source: SourceMap | ResolvedMap,
|
|
39
73
|
resolved: ResolvedMap,
|
|
40
74
|
): ConstraintVerdict[] {
|
|
41
75
|
const axis = source.direction === "right" ? 0 : 1;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
)
|
|
50
|
-
: Infinity;
|
|
51
|
-
const message = `${hint.path}: pin (${hint.node}): ${distance} units from box centre`;
|
|
52
|
-
return hint.strength === "required"
|
|
53
|
-
? { ...hint, holds: distance <= 1e-6, message }
|
|
54
|
-
: { ...hint, holds: null, message };
|
|
55
|
-
}
|
|
76
|
+
const hints = statedHints(source);
|
|
77
|
+
const pins = hints.filter(
|
|
78
|
+
(h): h is Extract<StatedHint, { kind: "pin" }> =>
|
|
79
|
+
h.kind === "pin" && h.strength !== "required",
|
|
80
|
+
);
|
|
81
|
+
return hints.map((hint): ConstraintVerdict => {
|
|
82
|
+
if (hint.kind === "pin") return pinVerdict(hint, pins, resolved, axis);
|
|
56
83
|
if (hint.kind === "free")
|
|
57
84
|
return {
|
|
58
85
|
...hint,
|
|
59
86
|
holds: null,
|
|
60
87
|
message: `${constraintName(hint)}: not verifiable from the drawing`,
|
|
61
88
|
};
|
|
62
|
-
const positions =
|
|
63
|
-
hint.kind === "sameRank"
|
|
64
|
-
? hint.nodes.map((id) => resolved.nodes[id]?.box[axis])
|
|
65
|
-
: [];
|
|
66
89
|
const holds =
|
|
67
90
|
hint.kind === "side"
|
|
68
91
|
? sideHolds(hint, source, resolved)
|
|
69
|
-
:
|
|
92
|
+
: hint.nodes
|
|
93
|
+
.map((id) => resolved.nodes[id]?.box[axis])
|
|
94
|
+
.every(
|
|
95
|
+
(position, _index, positions) =>
|
|
96
|
+
position !== undefined && position === positions[0],
|
|
97
|
+
);
|
|
70
98
|
return {
|
|
71
99
|
...hint,
|
|
72
100
|
holds,
|
package/src/layout/width.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { ResolvedMap } from "../schema.js";
|
|
2
2
|
import type { Measurer } from "./ports.js";
|
|
3
3
|
|
|
4
|
+
export function needsWidthPass(map: ResolvedMap, widthEm?: number): boolean {
|
|
5
|
+
return widthEm !== undefined && map.viewBox[0] / widthEm > 10.5;
|
|
6
|
+
}
|
|
7
|
+
|
|
4
8
|
/** Match the text floor of a drawing fitted into a width in label em.
|
|
5
9
|
* The first engine pass supplies a size for reserving space in the second.
|
|
6
10
|
* Final annotation refinement uses that second drawing's actual width;
|