1use serde::{Deserialize, Serialize};
10
11pub mod hull;
12pub mod orderings;
13pub mod place;
14pub mod spring;
15
16pub use hull::convex_hull;
17pub use orderings::{levi_ego_tree, paoh_ordering, LeviKind, LeviNode};
18pub use place::bipartite_layout;
19pub use place::radial_layout;
20pub use spring::hyperedge_aware_layout;
21pub use spring::spring_layout;
22
23pub const DEFAULT_LAYOUT_SEED: u64 = 0;
27
28#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
30pub enum BipartiteAlign {
31 Vertical,
33 Horizontal,
35}
36
37#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
39pub enum LayoutStrategy {
40 Spring {
42 iterations: usize,
43 seed: Option<u64>,
44 },
45 Bipartite { align: BipartiteAlign },
47 HyperedgeAware {
50 iterations: usize,
51 halo_padding: f64,
52 },
53 Radial { rings: usize },
55}
56
57#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
62pub enum HyperedgeGeometry {
63 ConvexHull { points: Vec<(f64, f64)> },
65 Spline { control_points: Vec<(f64, f64)> },
67 Stripe { x: f64, y_min: f64, y_max: f64 },
69 Arc {
71 center: (f64, f64),
72 radius: f64,
73 angle_range: (f64, f64),
74 },
75}
76
77#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
81pub struct LayoutResult {
82 pub node_positions: Vec<(String, (f64, f64))>,
83 pub edge_geometries: Vec<(String, HyperedgeGeometry)>,
84 pub bounding_box: (f64, f64, f64, f64),
85}
86
87pub(crate) fn bounding_box_of(
90 positions: &[(String, (f64, f64))],
91 geometries: &[(String, HyperedgeGeometry)],
92) -> (f64, f64, f64, f64) {
93 let mut bb = (
94 f64::INFINITY,
95 f64::INFINITY,
96 f64::NEG_INFINITY,
97 f64::NEG_INFINITY,
98 );
99 fn fold(bb: &mut (f64, f64, f64, f64), x: f64, y: f64) {
100 if x < bb.0 {
101 bb.0 = x;
102 }
103 if y < bb.1 {
104 bb.1 = y;
105 }
106 if x > bb.2 {
107 bb.2 = x;
108 }
109 if y > bb.3 {
110 bb.3 = y;
111 }
112 }
113 for (_, (x, y)) in positions {
114 fold(&mut bb, *x, *y);
115 }
116 for (_, g) in geometries {
117 match g {
118 HyperedgeGeometry::ConvexHull { points }
119 | HyperedgeGeometry::Spline {
120 control_points: points,
121 } => {
122 for (x, y) in points {
123 fold(&mut bb, *x, *y);
124 }
125 }
126 HyperedgeGeometry::Stripe { x, y_min, y_max } => {
127 fold(&mut bb, *x, *y_min);
128 fold(&mut bb, *x, *y_max);
129 }
130 HyperedgeGeometry::Arc { center, radius, .. } => {
131 fold(&mut bb, center.0 - radius, center.1 - radius);
132 fold(&mut bb, center.0 + radius, center.1 + radius);
133 }
134 }
135 }
136 if !bb.0.is_finite() {
137 return (0.0, 0.0, 0.0, 0.0);
138 }
139 bb
140}
141
142use crate::core::hypergraph::Hypergraph;
143
144pub fn compute_layout<N, E, M>(h: &Hypergraph<N, E, M>, strategy: LayoutStrategy) -> LayoutResult {
146 match strategy {
147 LayoutStrategy::Spring { iterations, seed } => spring::spring_layout(h, iterations, seed),
148 LayoutStrategy::Bipartite { align } => place::bipartite_layout(h, align),
149 LayoutStrategy::HyperedgeAware {
150 iterations,
151 halo_padding,
152 } => spring::hyperedge_aware_layout(h, iterations, halo_padding),
153 LayoutStrategy::Radial { rings } => place::radial_layout(h, rings),
154 }
155}