Files
pipelines/app/lib/features/graph/routing/edge_label_placement.dart
T

203 lines
5.6 KiB
Dart

import 'dart:math' as math;
import '../layout/layout_models.dart';
import 'route_geometry.dart';
import 'route_models.dart';
/// Places one measured edge label without overlapping a node or another label.
///
/// Candidates are tried in a deterministic order: the midpoint of each
/// horizontal run (above, then below), then each vertical run (right, then
/// left), sliding along the run before giving up. A run too narrow for the
/// box pushes the label to a free shelf below the drawing, which expands the
/// routing bounds instead of covering a node.
EdgeLabelBox placeEdgeLabel({
required EdgeRoute route,
required LabelExtent extent,
required List<GraphNodeBox> nodes,
required List<EdgeLabelBox> placed,
required List<EdgeRoute> routes,
required RoutingConfig config,
required double floorY,
}) {
final width = extent.width;
final height = extent.height;
for (final candidate in _candidates(route, width, height, config.labelGap)) {
final box = EdgeLabelBox(
edgeId: route.id,
x: candidate.x,
y: candidate.y,
width: width,
height: height,
);
if (_fits(box, nodes, placed, routes, config)) return box;
}
// The fallback shelf is below every route and every existing label, so it
// cannot cover a node. Growing it down expands the returned bounds.
var top = math.max(
floorY,
placed.fold<double>(0, (bottom, box) => math.max(bottom, box.bottom)),
);
top += config.labelGap;
final centre = _routeCentreX(route);
final step = math.max(height + config.labelGap, 1.0);
for (var attempt = 0; attempt < 100000; attempt += 1) {
final x = math.max(0.0, centre - width / 2);
final box = EdgeLabelBox(
edgeId: route.id,
x: x,
y: top,
width: width,
height: height,
);
if (_fits(box, nodes, placed, routes, config)) return box;
top += step;
}
// A zero-sized label cannot collide; this is unreachable for real extents.
return EdgeLabelBox(
edgeId: route.id,
x: math.max(0.0, centre - width / 2),
y: top,
width: width,
height: height,
);
}
bool _fits(
EdgeLabelBox box,
List<GraphNodeBox> nodes,
List<EdgeLabelBox> placed,
List<EdgeRoute> routes,
RoutingConfig config,
) {
if (box.x < 0 || box.y < 0) return false;
// A label clear of nodes can still sit across its self-loop's vertical leg
// or another edge. Keep its measured text rectangle clear of every wire.
final left = box.x - config.labelGap;
final right = box.right + config.labelGap;
final top = box.y - config.labelGap;
final bottom = box.bottom + config.labelGap;
for (final route in routes) {
for (var index = 1; index < route.points.length; index++) {
final a = route.points[index - 1], b = route.points[index];
if (a.x == b.x &&
a.x > left &&
a.x < right &&
math.max(a.y, b.y) > top &&
math.min(a.y, b.y) < bottom) {
return false;
}
if (a.y == b.y &&
a.y > top &&
a.y < bottom &&
math.max(a.x, b.x) > left &&
math.min(a.x, b.x) < right) {
return false;
}
}
}
for (final node in nodes) {
if (boxIntersectsNode(
x: box.x,
y: box.y,
width: box.width,
height: box.height,
node: node,
gap: config.labelGap,
)) {
return false;
}
}
for (final other in placed) {
if (boxesOverlap(
ax: box.x,
ay: box.y,
aw: box.width,
ah: box.height,
bx: other.x,
by: other.y,
bw: other.width,
bh: other.height,
gap: config.labelGap,
)) {
return false;
}
}
return true;
}
List<_LabelCandidate> _candidates(
EdgeRoute route,
double width,
double height,
double gap,
) {
const fractions = <double>[0.5, 0.35, 0.65, 0.2, 0.8, 0.08, 0.92];
final horizontals = <_Run>[];
final verticals = <_Run>[];
for (var index = 1; index < route.points.length; index += 1) {
final a = route.points[index - 1];
final b = route.points[index];
if (a.y == b.y) {
horizontals.add(_Run(a, b, (b.x - a.x).abs(), index));
} else if (a.x == b.x) {
verticals.add(_Run(a, b, (b.y - a.y).abs(), index));
}
}
horizontals.sort((a, b) {
final length = b.length.compareTo(a.length);
return length != 0 ? length : a.order.compareTo(b.order);
});
verticals.sort((a, b) {
final length = b.length.compareTo(a.length);
return length != 0 ? length : a.order.compareTo(b.order);
});
final candidates = <_LabelCandidate>[];
for (final run in horizontals) {
for (final fraction in fractions) {
final x = run.a.x + (run.b.x - run.a.x) * fraction;
final left = x - width / 2;
candidates.add(_LabelCandidate(left, run.a.y - gap - height));
candidates.add(_LabelCandidate(left, run.a.y + gap));
}
}
for (final run in verticals) {
for (final fraction in fractions) {
final y = run.a.y + (run.b.y - run.a.y) * fraction;
final top = y - height / 2;
candidates.add(_LabelCandidate(run.a.x + gap, top));
candidates.add(_LabelCandidate(run.a.x - gap - width, top));
}
}
return candidates;
}
double _routeCentreX(EdgeRoute route) {
if (route.points.isEmpty) return 0;
var lo = route.points.first.x;
var hi = lo;
for (final point in route.points) {
lo = math.min(lo, point.x);
hi = math.max(hi, point.x);
}
return (lo + hi) / 2;
}
class _Run {
const _Run(this.a, this.b, this.length, this.order);
final RoutePoint a;
final RoutePoint b;
final double length;
final int order;
}
class _LabelCandidate {
const _LabelCandidate(this.x, this.y);
final double x;
final double y;
}