use super::*;
pub(super) fn compute_layout_bounds(
clusters: &[LayoutCluster],
nodes: &[LayoutNode],
edges: &[crate::model::LayoutEdge],
) -> Option<Bounds> {
let mut b: Option<Bounds> = None;
let mut include_rect = |min_x: f64, min_y: f64, max_x: f64, max_y: f64| {
if let Some(ref mut cur) = b {
cur.min_x = cur.min_x.min(min_x);
cur.min_y = cur.min_y.min(min_y);
cur.max_x = cur.max_x.max(max_x);
cur.max_y = cur.max_y.max(max_y);
} else {
b = Some(Bounds {
min_x,
min_y,
max_x,
max_y,
});
}
};
for c in clusters {
let hw = c.width / 2.0;
let hh = c.height / 2.0;
include_rect(c.x - hw, c.y - hh, c.x + hw, c.y + hh);
let lhw = c.title_label.width / 2.0;
let lhh = c.title_label.height / 2.0;
include_rect(
c.title_label.x - lhw,
c.title_label.y - lhh,
c.title_label.x + lhw,
c.title_label.y + lhh,
);
}
for n in nodes {
let hw = n.width / 2.0;
let hh = n.height / 2.0;
include_rect(n.x - hw, n.y - hh, n.x + hw, n.y + hh);
}
for e in edges {
for p in &e.points {
include_rect(p.x, p.y, p.x, p.y);
}
for lbl in [
e.label.as_ref(),
e.start_label_left.as_ref(),
e.start_label_right.as_ref(),
e.end_label_left.as_ref(),
e.end_label_right.as_ref(),
]
.into_iter()
.flatten()
{
let hw = lbl.width / 2.0;
let hh = lbl.height / 2.0;
include_rect(lbl.x - hw, lbl.y - hh, lbl.x + hw, lbl.y + hh);
}
}
b
}