use serde::{Deserialize, Serialize};
pub mod hull;
pub mod orderings;
pub mod place;
pub mod spring;
pub use hull::convex_hull;
pub use orderings::{levi_ego_tree, paoh_ordering, LeviKind, LeviNode};
pub use place::bipartite_layout;
pub use place::radial_layout;
pub use spring::hyperedge_aware_layout;
pub use spring::spring_layout;
pub const DEFAULT_LAYOUT_SEED: u64 = 0;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum BipartiteAlign {
Vertical,
Horizontal,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum LayoutStrategy {
Spring {
iterations: usize,
seed: Option<u64>,
},
Bipartite { align: BipartiteAlign },
HyperedgeAware {
iterations: usize,
halo_padding: f64,
},
Radial { rings: usize },
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum HyperedgeGeometry {
ConvexHull { points: Vec<(f64, f64)> },
Spline { control_points: Vec<(f64, f64)> },
Stripe { x: f64, y_min: f64, y_max: f64 },
Arc {
center: (f64, f64),
radius: f64,
angle_range: (f64, f64),
},
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct LayoutResult {
pub node_positions: Vec<(String, (f64, f64))>,
pub edge_geometries: Vec<(String, HyperedgeGeometry)>,
pub bounding_box: (f64, f64, f64, f64),
}
pub(crate) fn bounding_box_of(
positions: &[(String, (f64, f64))],
geometries: &[(String, HyperedgeGeometry)],
) -> (f64, f64, f64, f64) {
let mut bb = (
f64::INFINITY,
f64::INFINITY,
f64::NEG_INFINITY,
f64::NEG_INFINITY,
);
fn fold(bb: &mut (f64, f64, f64, f64), x: f64, y: f64) {
if x < bb.0 {
bb.0 = x;
}
if y < bb.1 {
bb.1 = y;
}
if x > bb.2 {
bb.2 = x;
}
if y > bb.3 {
bb.3 = y;
}
}
for (_, (x, y)) in positions {
fold(&mut bb, *x, *y);
}
for (_, g) in geometries {
match g {
HyperedgeGeometry::ConvexHull { points }
| HyperedgeGeometry::Spline {
control_points: points,
} => {
for (x, y) in points {
fold(&mut bb, *x, *y);
}
}
HyperedgeGeometry::Stripe { x, y_min, y_max } => {
fold(&mut bb, *x, *y_min);
fold(&mut bb, *x, *y_max);
}
HyperedgeGeometry::Arc { center, radius, .. } => {
fold(&mut bb, center.0 - radius, center.1 - radius);
fold(&mut bb, center.0 + radius, center.1 + radius);
}
}
}
if !bb.0.is_finite() {
return (0.0, 0.0, 0.0, 0.0);
}
bb
}
use crate::core::hypergraph::Hypergraph;
pub fn compute_layout<N, E, M>(h: &Hypergraph<N, E, M>, strategy: LayoutStrategy) -> LayoutResult {
match strategy {
LayoutStrategy::Spring { iterations, seed } => spring::spring_layout(h, iterations, seed),
LayoutStrategy::Bipartite { align } => place::bipartite_layout(h, align),
LayoutStrategy::HyperedgeAware {
iterations,
halo_padding,
} => spring::hyperedge_aware_layout(h, iterations, halo_padding),
LayoutStrategy::Radial { rings } => place::radial_layout(h, rings),
}
}