Avoid automatic diagram label collisions with stable bounded placement
This commit is contained in:
+102
-15
@@ -345,6 +345,7 @@ fn route_diagram_impl(
|
||||
ordered.sort_by_key(|e| e.id);
|
||||
let mut edges = BTreeMap::new();
|
||||
let mut previous = Vec::new();
|
||||
let mut previous_labels = Vec::new();
|
||||
let mut budget = VIEW_SEARCH_BUDGET;
|
||||
let mut bounds = BoundsBuilder::default();
|
||||
for node in &view.nodes {
|
||||
@@ -370,6 +371,7 @@ fn route_diagram_impl(
|
||||
}
|
||||
if !cached.label.text.is_empty() {
|
||||
bounds.rect(cached.label.bounds.position, cached.label.bounds.size);
|
||||
previous_labels.push(cached.label.bounds);
|
||||
}
|
||||
record_segments(&mut previous, &cached.points);
|
||||
edges.insert(edge.id, cached.clone());
|
||||
@@ -415,12 +417,14 @@ fn route_diagram_impl(
|
||||
&updated.points,
|
||||
route.and_then(|r| r.label.as_ref()),
|
||||
&obstacles,
|
||||
&previous_labels,
|
||||
);
|
||||
for &point in &updated.points {
|
||||
bounds.point(point);
|
||||
}
|
||||
if !updated.label.text.is_empty() {
|
||||
bounds.rect(updated.label.bounds.position, updated.label.bounds.size);
|
||||
previous_labels.push(updated.label.bounds);
|
||||
}
|
||||
record_segments(&mut previous, &updated.points);
|
||||
edges.insert(edge.id, updated);
|
||||
@@ -464,12 +468,14 @@ fn route_diagram_impl(
|
||||
&points,
|
||||
route.and_then(|r| r.label.as_ref()),
|
||||
&obstacles,
|
||||
&previous_labels,
|
||||
);
|
||||
for &point in &points {
|
||||
bounds.point(point);
|
||||
}
|
||||
if !label.text.is_empty() {
|
||||
bounds.rect(label.bounds.position, label.bounds.size);
|
||||
previous_labels.push(label.bounds);
|
||||
}
|
||||
record_segments(&mut previous, &points);
|
||||
edges.insert(
|
||||
@@ -1021,6 +1027,7 @@ fn make_label(
|
||||
points: &[Position],
|
||||
authored: Option<&crate::EdgeLabel>,
|
||||
obstacles: &[Rect],
|
||||
previous_labels: &[RoutingBounds],
|
||||
) -> EdgeLabelGeometry {
|
||||
let text = label_text(project, edge);
|
||||
let size = Size {
|
||||
@@ -1031,8 +1038,8 @@ fn make_label(
|
||||
},
|
||||
height: if text.is_empty() { 0.0 } else { 24.0 },
|
||||
};
|
||||
let offset = authored.map_or(pos(0.0, -18.0), |l| l.offset);
|
||||
let geometry = |fraction| {
|
||||
let mut offset = authored.map_or(pos(0.0, -18.0), |l| l.offset);
|
||||
let geometry = |fraction, offset: Position| {
|
||||
let anchor = point_along_polyline(points, fraction).unwrap_or(pos(0.0, 0.0));
|
||||
let center = pos(anchor.x + offset.x, anchor.y + offset.y);
|
||||
(
|
||||
@@ -1045,21 +1052,19 @@ fn make_label(
|
||||
)
|
||||
};
|
||||
let mut fraction = authored.map_or(0.5, |l| l.fraction);
|
||||
// Automatic labels prefer the middle, then clear segment centres. An
|
||||
// explicitly positioned label is exact, even if its author overlaps a card.
|
||||
if authored.is_none()
|
||||
&& !text.is_empty()
|
||||
&& obstacles.iter().any(|r| r.overlaps(geometry(fraction).2))
|
||||
{
|
||||
// Keep authored placement exact. Automatic labels share the stable-ID
|
||||
// prefix used for route scoring, so previews and settled solves agree.
|
||||
if authored.is_none() && !text.is_empty() {
|
||||
let total = total_length(points);
|
||||
let mut traversed = 0.0;
|
||||
let mut alternatives = Vec::new();
|
||||
let mut alternatives = vec![0.5];
|
||||
if total > 0.0 {
|
||||
for pair in points.windows(2) {
|
||||
let len = length(pair[0], pair[1]);
|
||||
alternatives.push((traversed + len / 2.0) / total);
|
||||
traversed += len;
|
||||
}
|
||||
alternatives.extend((1..20).map(|i| i as f32 / 20.0));
|
||||
}
|
||||
alternatives.sort_by(|a, b| {
|
||||
(a - 0.5)
|
||||
@@ -1067,14 +1072,50 @@ fn make_label(
|
||||
.total_cmp(&(b - 0.5).abs())
|
||||
.then_with(|| a.total_cmp(b))
|
||||
});
|
||||
if let Some(clear) = alternatives
|
||||
.into_iter()
|
||||
.find(|f| !obstacles.iter().any(|r| r.overlaps(geometry(*f).2)))
|
||||
{
|
||||
fraction = clear;
|
||||
alternatives.dedup();
|
||||
alternatives.truncate(32);
|
||||
// Prefer another position on the existing path at the normal offset.
|
||||
// Only then try a small bounded set of offsets, never move the route.
|
||||
let offsets = [
|
||||
offset,
|
||||
pos(0.0, 18.0),
|
||||
pos(0.0, -46.0),
|
||||
pos(0.0, 46.0),
|
||||
pos(0.0, -74.0),
|
||||
pos(0.0, 74.0),
|
||||
pos(size.width / 2.0 + 18.0, 0.0),
|
||||
pos(-size.width / 2.0 - 18.0, 0.0),
|
||||
];
|
||||
let mut best = None;
|
||||
// Bound work independently per label, including pathological coincident
|
||||
// edges. A preview sees the same stable prefix and the same budget.
|
||||
let mut comparisons = 8_192;
|
||||
'candidate: for candidate_offset in offsets {
|
||||
for &candidate_fraction in &alternatives {
|
||||
let candidate = geometry(candidate_fraction, candidate_offset).2;
|
||||
let Some(score) = label_collisions(
|
||||
candidate,
|
||||
obstacles,
|
||||
previous_labels,
|
||||
best.as_ref().map(|(score, _, _)| *score),
|
||||
&mut comparisons,
|
||||
) else {
|
||||
break 'candidate;
|
||||
};
|
||||
if best.as_ref().is_none_or(|(old, _, _)| score < *old) {
|
||||
best = Some((score, candidate_fraction, candidate_offset));
|
||||
}
|
||||
if score == (0, 0) {
|
||||
break 'candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some((_, chosen_fraction, chosen_offset)) = best {
|
||||
fraction = chosen_fraction;
|
||||
offset = chosen_offset;
|
||||
}
|
||||
}
|
||||
let (anchor, center, bounds) = geometry(fraction);
|
||||
let (anchor, center, bounds) = geometry(fraction, offset);
|
||||
EdgeLabelGeometry {
|
||||
text,
|
||||
fraction,
|
||||
@@ -1084,6 +1125,52 @@ fn make_label(
|
||||
}
|
||||
}
|
||||
|
||||
fn label_collisions(
|
||||
bounds: RoutingBounds,
|
||||
obstacles: &[Rect],
|
||||
labels: &[RoutingBounds],
|
||||
best: Option<(usize, usize)>,
|
||||
comparisons: &mut usize,
|
||||
) -> Option<(usize, usize)> {
|
||||
let mut nodes = 0;
|
||||
for obstacle in obstacles {
|
||||
if *comparisons == 0 {
|
||||
return None;
|
||||
}
|
||||
*comparisons -= 1;
|
||||
if obstacle.overlaps(bounds) {
|
||||
nodes += 1;
|
||||
}
|
||||
if best.is_some_and(|score| nodes > score.0) {
|
||||
return Some((nodes, usize::MAX));
|
||||
}
|
||||
}
|
||||
// Once a candidate is worse than the current best, additional collisions
|
||||
// cannot change the decision. This bounds dense coincident-label work.
|
||||
let label_limit = best
|
||||
.filter(|score| nodes == score.0)
|
||||
.map_or(usize::MAX, |score| score.1.saturating_add(1));
|
||||
let mut collisions = 0;
|
||||
for other in labels {
|
||||
if *comparisons == 0 {
|
||||
return None;
|
||||
}
|
||||
*comparisons -= 1;
|
||||
// Four diagram units between text boxes also keeps hit regions legible.
|
||||
if bounds.position.x < other.position.x + other.size.width + 4.0
|
||||
&& bounds.position.x + bounds.size.width > other.position.x - 4.0
|
||||
&& bounds.position.y < other.position.y + other.size.height + 4.0
|
||||
&& bounds.position.y + bounds.size.height > other.position.y - 4.0
|
||||
{
|
||||
collisions += 1;
|
||||
if collisions >= label_limit {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some((nodes, collisions))
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct BoundsBuilder {
|
||||
extents: Option<(f32, f32, f32, f32)>,
|
||||
|
||||
Reference in New Issue
Block a user