b381290f34
Pure-Dart P2c slice: interval-aware ports and corridors, label-aware lane spacing, measured label placement with fallback shelf, skipped diagnostics and routing bounds. 20 focused tests plus existing suite and web build pass.
266 lines
8.3 KiB
Dart
266 lines
8.3 KiB
Dart
import 'dart:math' as math;
|
|
|
|
import '../domain/graph_definition.dart';
|
|
import '../layout/layout_models.dart';
|
|
import 'corridor_plan.dart';
|
|
import 'route_geometry.dart';
|
|
import 'route_models.dart';
|
|
|
|
/// One routed edge before labels and bounds are assembled.
|
|
///
|
|
/// Internal to the routing library: callers receive the immutable public
|
|
/// [EdgeRoute] instead.
|
|
class PlannedEdgeRoute {
|
|
PlannedEdgeRoute({
|
|
required this.edge,
|
|
required this.kind,
|
|
required this.straight,
|
|
required this.sourcePort,
|
|
required this.targetPort,
|
|
required List<RoutePoint> points,
|
|
}) : points = List.unmodifiable(points);
|
|
|
|
final GraphEdge edge;
|
|
final RouteKind kind;
|
|
final bool straight;
|
|
final RoutePort sourcePort;
|
|
final RoutePort targetPort;
|
|
final List<RoutePoint> points;
|
|
|
|
EdgeRoute toRoute() => EdgeRoute(
|
|
id: edge.id,
|
|
source: edge.source,
|
|
outcome: edge.outcome,
|
|
target: edge.target,
|
|
label: edge.label,
|
|
fallback: edge.fallback,
|
|
kind: kind,
|
|
straight: straight,
|
|
sourcePort: sourcePort,
|
|
targetPort: targetPort,
|
|
points: points,
|
|
);
|
|
}
|
|
|
|
/// Plans orthogonal polylines for every edge whose endpoints have placed boxes.
|
|
///
|
|
/// [edges] must already be filtered to known endpoints and sorted by id. The
|
|
/// plan is deterministic: forward one-rank routes take a straight horizontal
|
|
/// run when the measured boxes and labels leave room, and every other route
|
|
/// gets a unique lane below the node grid plus vertical corridors inside the
|
|
/// column gaps. Corridors and lanes never enter a node column.
|
|
///
|
|
/// All horizontal stubs share one [AxisTrackAllocator], so two different edges
|
|
/// cannot occupy the same run in a column gap even when they touch different
|
|
/// nodes on the same row.
|
|
List<PlannedEdgeRoute> planEdgeRoutes({
|
|
required GraphLayout layout,
|
|
required List<GraphEdge> edges,
|
|
required double maxLabelHeight,
|
|
required RoutingConfig config,
|
|
}) {
|
|
if (edges.isEmpty) return const <PlannedEdgeRoute>[];
|
|
final nodes = <String, GraphNodeBox>{
|
|
for (final node in layout.nodes) node.key: node,
|
|
};
|
|
final kinds = <int, RouteKind>{};
|
|
for (final edge in edges) {
|
|
final source = nodes[edge.source]!;
|
|
final target = nodes[edge.target]!;
|
|
kinds[edge.id] = _kindFor(edge, source, target, layout);
|
|
}
|
|
|
|
final trackSpacing = math.max(
|
|
config.portSpacing,
|
|
maxLabelHeight + config.labelGap,
|
|
);
|
|
final runs = AxisTrackAllocator(trackSpacing);
|
|
final straightY = <int, double>{};
|
|
for (final edge in edges) {
|
|
if (kinds[edge.id] != RouteKind.forward) continue;
|
|
final source = nodes[edge.source]!;
|
|
final target = nodes[edge.target]!;
|
|
if (target.rank != source.rank + 1) continue;
|
|
if (target.x <= source.right) continue;
|
|
final sourceInset = portInset(source, config.portSpacing);
|
|
final targetInset = portInset(target, config.portSpacing);
|
|
final lo = math.max(source.y + sourceInset, target.y + targetInset);
|
|
final hi = math.min(
|
|
source.bottom - sourceInset,
|
|
target.bottom - targetInset,
|
|
);
|
|
if (hi <= lo) continue;
|
|
final y = runs.allocate(
|
|
desired: (lo + hi) / 2,
|
|
min: lo,
|
|
max: hi,
|
|
spanLo: source.right,
|
|
spanHi: target.x,
|
|
);
|
|
if (y != null) straightY[edge.id] = y;
|
|
}
|
|
|
|
final laneStep = math.max(
|
|
config.laneSpacing,
|
|
maxLabelHeight + config.labelGap,
|
|
);
|
|
final contentBottom = layout.nodes.fold<double>(
|
|
0,
|
|
(bottom, node) => math.max(bottom, node.bottom),
|
|
);
|
|
final wrapPlans = <_WrapPlan>[];
|
|
final corridorRequests = <CorridorRequest>[];
|
|
var laneIndex = 0;
|
|
for (final edge in edges) {
|
|
if (straightY.containsKey(edge.id)) continue;
|
|
final source = nodes[edge.source]!;
|
|
final target = nodes[edge.target]!;
|
|
final kind = kinds[edge.id]!;
|
|
final laneY =
|
|
contentBottom + config.nodeClearance + laneStep * (laneIndex + 1);
|
|
laneIndex += 1;
|
|
final sourceGap = (source.rank + 1).clamp(0, layout.rankCount);
|
|
final targetGap = kind == RouteKind.forward
|
|
? target.rank.clamp(0, layout.rankCount)
|
|
: (target.rank + 1).clamp(0, layout.rankCount);
|
|
corridorRequests.add(
|
|
CorridorRequest(
|
|
edgeId: edge.id,
|
|
sourceGap: sourceGap,
|
|
targetGap: targetGap,
|
|
),
|
|
);
|
|
wrapPlans.add(_WrapPlan(edge: edge, kind: kind, laneY: laneY));
|
|
}
|
|
|
|
final corridors = planCorridors(
|
|
layout: layout,
|
|
requests: corridorRequests,
|
|
config: config,
|
|
);
|
|
final sourceTrackX = corridors.sourceX;
|
|
final targetTrackX = corridors.targetX;
|
|
|
|
final sourcePorts = <int, RoutePort>{};
|
|
final targetPorts = <int, RoutePort>{};
|
|
for (final edge in edges) {
|
|
final y = straightY[edge.id];
|
|
if (y == null) continue;
|
|
final source = nodes[edge.source]!;
|
|
final target = nodes[edge.target]!;
|
|
sourcePorts[edge.id] = RoutePort(
|
|
side: RoutePortSide.right,
|
|
point: RoutePoint(source.right, y),
|
|
);
|
|
targetPorts[edge.id] = RoutePort(
|
|
side: RoutePortSide.left,
|
|
point: RoutePoint(target.x, y),
|
|
);
|
|
}
|
|
for (final plan in wrapPlans) {
|
|
final edge = plan.edge;
|
|
final source = nodes[edge.source]!;
|
|
final target = nodes[edge.target]!;
|
|
final sourceTrack = sourceTrackX[edge.id]!;
|
|
final targetTrack = targetTrackX[edge.id]!;
|
|
final sourceInset = portInset(source, config.portSpacing);
|
|
final targetInset = portInset(target, config.portSpacing);
|
|
final sourceY = runs.allocate(
|
|
desired: source.y + source.height / 2,
|
|
min: source.y + sourceInset,
|
|
max: source.bottom - sourceInset,
|
|
spanLo: source.right,
|
|
spanHi: sourceTrack,
|
|
);
|
|
final targetSide = plan.kind == RouteKind.forward
|
|
? RoutePortSide.left
|
|
: RoutePortSide.right;
|
|
final targetBorder = targetSide == RoutePortSide.left
|
|
? target.x
|
|
: target.right;
|
|
final targetY = runs.allocate(
|
|
desired: target.y + target.height / 2,
|
|
min: target.y + targetInset,
|
|
max: target.bottom - targetInset,
|
|
spanLo: math.min(targetBorder, targetTrack),
|
|
spanHi: math.max(targetBorder, targetTrack),
|
|
);
|
|
sourcePorts[edge.id] = RoutePort(
|
|
side: RoutePortSide.right,
|
|
point: RoutePoint(source.right, sourceY ?? source.y + source.height / 2),
|
|
);
|
|
targetPorts[edge.id] = RoutePort(
|
|
side: targetSide,
|
|
point: RoutePoint(targetBorder, targetY ?? target.y + target.height / 2),
|
|
);
|
|
}
|
|
|
|
final planned = <int, PlannedEdgeRoute>{};
|
|
for (final edge in edges) {
|
|
final value = straightY[edge.id];
|
|
if (value != null) {
|
|
planned[edge.id] = PlannedEdgeRoute(
|
|
edge: edge,
|
|
kind: kinds[edge.id]!,
|
|
straight: true,
|
|
sourcePort: sourcePorts[edge.id]!,
|
|
targetPort: targetPorts[edge.id]!,
|
|
points: _collapse(<RoutePoint>[
|
|
sourcePorts[edge.id]!.point,
|
|
targetPorts[edge.id]!.point,
|
|
]),
|
|
);
|
|
}
|
|
}
|
|
for (final plan in wrapPlans) {
|
|
final sourceTrack = sourceTrackX[plan.edge.id]!;
|
|
final targetTrack = targetTrackX[plan.edge.id]!;
|
|
planned[plan.edge.id] = PlannedEdgeRoute(
|
|
edge: plan.edge,
|
|
kind: plan.kind,
|
|
straight: false,
|
|
sourcePort: sourcePorts[plan.edge.id]!,
|
|
targetPort: targetPorts[plan.edge.id]!,
|
|
points: _collapse(<RoutePoint>[
|
|
sourcePorts[plan.edge.id]!.point,
|
|
RoutePoint(sourceTrack, sourcePorts[plan.edge.id]!.point.y),
|
|
RoutePoint(sourceTrack, plan.laneY),
|
|
RoutePoint(targetTrack, plan.laneY),
|
|
RoutePoint(targetTrack, targetPorts[plan.edge.id]!.point.y),
|
|
targetPorts[plan.edge.id]!.point,
|
|
]),
|
|
);
|
|
}
|
|
return <PlannedEdgeRoute>[for (final edge in edges) planned[edge.id]!];
|
|
}
|
|
|
|
RouteKind _kindFor(
|
|
GraphEdge edge,
|
|
GraphNodeBox source,
|
|
GraphNodeBox target,
|
|
GraphLayout layout,
|
|
) {
|
|
if (edge.source == edge.target) return RouteKind.selfLoop;
|
|
final sourceRank = layout.ranks[edge.source] ?? source.rank;
|
|
final targetRank = layout.ranks[edge.target] ?? target.rank;
|
|
if (targetRank > sourceRank) return RouteKind.forward;
|
|
if (targetRank < sourceRank) return RouteKind.back;
|
|
return RouteKind.sameRank;
|
|
}
|
|
|
|
List<RoutePoint> _collapse(List<RoutePoint> points) {
|
|
final result = <RoutePoint>[];
|
|
for (final point in points) {
|
|
if (result.isEmpty || result.last != point) result.add(point);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
class _WrapPlan {
|
|
_WrapPlan({required this.edge, required this.kind, required this.laneY});
|
|
|
|
final GraphEdge edge;
|
|
final RouteKind kind;
|
|
final double laneY;
|
|
}
|