@metronforge/concept-map 0.4.0 → 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 +2 -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 -82
- 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 +1 -1
- package/dist/layout/place-sketch.d.ts.map +1 -1
- package/dist/layout/place-sketch.js +5 -1
- package/dist/layout/place-sketch.js.map +1 -1
- package/dist/layout/relations/sketch-order.d.ts +3 -2
- package/dist/layout/relations/sketch-order.d.ts.map +1 -1
- package/dist/layout/relations/sketch-order.js +4 -3
- package/dist/layout/relations/sketch-order.js.map +1 -1
- package/dist/layout/relations/sketch-placement.d.ts +4 -3
- package/dist/layout/relations/sketch-placement.d.ts.map +1 -1
- package/dist/layout/relations/sketch-placement.js +8 -8
- package/dist/layout/relations/sketch-placement.js.map +1 -1
- 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-route.d.ts +2 -1
- package/dist/layout/sketch-route.d.ts.map +1 -1
- package/dist/layout/sketch-route.js +6 -6
- package/dist/layout/sketch-route.js.map +1 -1
- 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/package.json +1 -1
- package/src/layout/hints.ts +2 -2
- package/src/layout/index.ts +1 -148
- package/src/layout/layout.ts +147 -0
- package/src/layout/place-sketch.ts +14 -9
- package/src/layout/relations/sketch-order.ts +12 -1
- package/src/layout/relations/sketch-placement.ts +11 -5
- package/src/layout/sketch-conflicts.ts +69 -0
- package/src/layout/sketch-route.ts +8 -5
- package/src/layout/stages/hints.ts +47 -19
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { LayoutProblem } from "./problem.js";
|
|
2
|
+
import type { UnsupportedHint } from "./types.js";
|
|
3
|
+
import { sketchRanks } from "./relations/sketch-ranks.js";
|
|
4
|
+
import { hintRelations } from "./relations/rank.js";
|
|
5
|
+
import type { RecordSketchConflict } from "./relations/sketch-order.js";
|
|
6
|
+
|
|
7
|
+
/** Translate checked solver cycles to the author's pin and lane paths. */
|
|
8
|
+
function conflictPaths(
|
|
9
|
+
problem: LayoutProblem,
|
|
10
|
+
pins: ReadonlyMap<string, { path?: string }>,
|
|
11
|
+
) {
|
|
12
|
+
const pairs = new Map<string, readonly string[]>();
|
|
13
|
+
for (const from of pins.keys())
|
|
14
|
+
for (const to of pins.keys()) pairs.set(`sketch.${from}:${to}`, [from, to]);
|
|
15
|
+
const direction = problem.constraints.find(
|
|
16
|
+
(hint) => hint.kind === "direction",
|
|
17
|
+
);
|
|
18
|
+
for (const relation of hintRelations(
|
|
19
|
+
sketchRanks(problem, direction?.direction === "right" ? 0 : 1),
|
|
20
|
+
))
|
|
21
|
+
pairs.set(relation.id, [relation.from, relation.to]);
|
|
22
|
+
const paths = new Map(
|
|
23
|
+
[...pairs].map(([id, nodes]) => [
|
|
24
|
+
id,
|
|
25
|
+
nodes.map((node) => pins.get(node)!.path!),
|
|
26
|
+
]),
|
|
27
|
+
);
|
|
28
|
+
for (const node of problem.nodes) {
|
|
29
|
+
if (!node.lane) continue;
|
|
30
|
+
paths.set(`lane.start.${node.id}`, [`lanes.${node.lane}`]);
|
|
31
|
+
paths.set(`lane.end.${node.id}`, [`lanes.${node.lane}`]);
|
|
32
|
+
}
|
|
33
|
+
return paths;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function sketchConflicts(problem: LayoutProblem) {
|
|
37
|
+
const pins = new Map(
|
|
38
|
+
problem.constraints.flatMap((hint) =>
|
|
39
|
+
hint.kind === "pin" && hint.strength === "preferred" && hint.path
|
|
40
|
+
? [[hint.node, hint] as const]
|
|
41
|
+
: [],
|
|
42
|
+
),
|
|
43
|
+
);
|
|
44
|
+
const paths = conflictPaths(problem, pins);
|
|
45
|
+
const hints: UnsupportedHint[] = [];
|
|
46
|
+
const seen = new Set<string>();
|
|
47
|
+
const record: RecordSketchConflict = (relation, cycle) => {
|
|
48
|
+
const conflicts = cycle.flatMap((id) => paths.get(id) ?? [id]);
|
|
49
|
+
for (const node of [relation.from, relation.to]) {
|
|
50
|
+
const pin = pins.get(node);
|
|
51
|
+
if (!pin) continue;
|
|
52
|
+
const hint: UnsupportedHint = {
|
|
53
|
+
kind: "pin",
|
|
54
|
+
node,
|
|
55
|
+
at: [...pin.position],
|
|
56
|
+
strength: pin.strength,
|
|
57
|
+
origin: "stated",
|
|
58
|
+
path: pin.path!,
|
|
59
|
+
reason: "contradiction",
|
|
60
|
+
conflicts,
|
|
61
|
+
};
|
|
62
|
+
const key = JSON.stringify(hint);
|
|
63
|
+
if (seen.has(key)) continue;
|
|
64
|
+
seen.add(key);
|
|
65
|
+
hints.push(hint);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
return { hints, record };
|
|
69
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { RecordSketchConflict } from "./relations/sketch-order.js";
|
|
1
2
|
import { needsWidthPass } from "./width.js";
|
|
2
3
|
import { sketchOrder, meritsSketchCheck } from "./sketch-agreement.js";
|
|
3
4
|
import type { ResolvedMap, SourceMap } from "../schema.js";
|
|
@@ -15,7 +16,6 @@ import { checkHard } from "./stages/hard-check.js";
|
|
|
15
16
|
import { resolvedBends, bendCount } from "./sketch-bends.js";
|
|
16
17
|
import { safeSketch } from "./sketch-safety.js";
|
|
17
18
|
export { safeSketch } from "./sketch-safety.js";
|
|
18
|
-
|
|
19
19
|
/** Keep sketch order gains with at most four extra bends and no new hard
|
|
20
20
|
* contacts or crossings, before and after width refinement. */
|
|
21
21
|
export function routeSketch(
|
|
@@ -26,6 +26,7 @@ export function routeSketch(
|
|
|
26
26
|
measurer: Measurer,
|
|
27
27
|
refinement?: RouteRefinement,
|
|
28
28
|
sizingWidthEm?: number,
|
|
29
|
+
record?: RecordSketchConflict,
|
|
29
30
|
): LayoutResult {
|
|
30
31
|
const checks = {
|
|
31
32
|
draw: (result: LayoutResult) =>
|
|
@@ -39,6 +40,7 @@ export function routeSketch(
|
|
|
39
40
|
original,
|
|
40
41
|
checks,
|
|
41
42
|
sizingWidthEm,
|
|
43
|
+
record,
|
|
42
44
|
);
|
|
43
45
|
return chooseDetour(source, problem, graph, placed, measurer, checks);
|
|
44
46
|
}
|
|
@@ -74,7 +76,6 @@ function chooseDetour(
|
|
|
74
76
|
}
|
|
75
77
|
return best;
|
|
76
78
|
}
|
|
77
|
-
|
|
78
79
|
interface SketchChecks {
|
|
79
80
|
draw(result: LayoutResult): ResolvedMap;
|
|
80
81
|
safe(base: LayoutResult, route: LayoutResult): boolean;
|
|
@@ -86,8 +87,9 @@ function choosePlacement(
|
|
|
86
87
|
original: LayoutResult,
|
|
87
88
|
{ draw, safe }: SketchChecks,
|
|
88
89
|
sizingWidthEm?: number,
|
|
90
|
+
record?: RecordSketchConflict,
|
|
89
91
|
): LayoutResult {
|
|
90
|
-
const candidates = placementRoutes(problem, graph, original);
|
|
92
|
+
const candidates = placementRoutes(problem, graph, original, record);
|
|
91
93
|
const bends = (result: LayoutResult) => resolvedBends(draw(result));
|
|
92
94
|
const originalBends = bends(original);
|
|
93
95
|
let best = original;
|
|
@@ -121,6 +123,7 @@ function placementRoutes(
|
|
|
121
123
|
problem: LayoutProblem,
|
|
122
124
|
graph: LayoutGraph,
|
|
123
125
|
original: LayoutResult,
|
|
126
|
+
record?: RecordSketchConflict,
|
|
124
127
|
) {
|
|
125
128
|
const rerouted = routeLayout(
|
|
126
129
|
graph,
|
|
@@ -128,8 +131,8 @@ function placementRoutes(
|
|
|
128
131
|
original.nodes,
|
|
129
132
|
routeBounds(original.nodes),
|
|
130
133
|
);
|
|
131
|
-
const candidates = sketchPlacements(problem, original).flatMap(
|
|
132
|
-
sketchRoutes(problem, graph, original, nodes),
|
|
134
|
+
const candidates = sketchPlacements(problem, original, record).flatMap(
|
|
135
|
+
(nodes) => sketchRoutes(problem, graph, original, nodes),
|
|
133
136
|
);
|
|
134
137
|
return [rerouted, ...candidates];
|
|
135
138
|
}
|
|
@@ -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,
|