Skip to main content

hypergraph_rs/layout/
mod.rs

1//! Deterministic hypergraph layout: node positions + per-hyperedge geometry.
2//!
3//! All strategies are hand-rolled — `rustworkx-core` 0.18 exposes NO layout
4//! functions (empirically confirmed 2026-07-18). Randomised layouts seed
5//! `rand_pcg::Pcg64Mcg`; non-randomised layouts (Bipartite, Radial) are pure
6//! placement functions. Positions/geometries are emitted in the hypergraph's
7//! insertion order (Constitution III.7 parity).
8
9use 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
23/// The default deterministic seed used when a strategy provides no explicit
24/// seed (or `seed = None`). XGI's `seed=None` is non-deterministic (numpy global
25/// RNG); we substitute a fixed seed for Constitution III.7 determinism.
26pub const DEFAULT_LAYOUT_SEED: u64 = 0;
27
28/// Column/row alignment for the bipartite layout.
29#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
30pub enum BipartiteAlign {
31    /// Agents in a left column, hyperedges in a right column (x separates roles).
32    Vertical,
33    /// Agents in a bottom row, hyperedges in a top row (y separates roles).
34    Horizontal,
35}
36
37/// The four layout strategies (spec §5.2).
38#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
39pub enum LayoutStrategy {
40    /// Fruchterman–Reingold force-directed layout on the 1-skeleton.
41    Spring {
42        iterations: usize,
43        seed: Option<u64>,
44    },
45    /// Two-column/row bipartite placement of agents and hyperedge markers.
46    Bipartite { align: BipartiteAlign },
47    /// Spring positions for agents; each hyperedge is the padded convex hull of
48    /// its members (a colored halo).
49    HyperedgeAware {
50        iterations: usize,
51        halo_padding: f64,
52    },
53    /// Agents on concentric rings by membership count; hyperedges as arcs.
54    Radial { rings: usize },
55}
56
57/// The geometry of a single hyperedge in a laid-out scene.
58///
59/// Defined here (not in `viz`) so that `viz` can depend on `layout` acyclically;
60/// `viz` re-exports it as `viz::HyperedgeGeometry` (spec §5.3).
61#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
62pub enum HyperedgeGeometry {
63    /// Convex hull around member nodes (halo style).
64    ConvexHull { points: Vec<(f64, f64)> },
65    /// Closed spline / polyline through members (elliptical style).
66    Spline { control_points: Vec<(f64, f64)> },
67    /// Vertical stripe (bipartite layout).
68    Stripe { x: f64, y_min: f64, y_max: f64 },
69    /// Radial arc (radial layout).
70    Arc {
71        center: (f64, f64),
72        radius: f64,
73        angle_range: (f64, f64),
74    },
75}
76
77/// The result of a layout computation: insertion-ordered node positions and
78/// per-hyperedge geometry, plus the overall bounding box (min_x, min_y, max_x,
79/// max_y).
80#[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
87/// Fold node positions + geometry extents into a bounding box
88/// (min_x, min_y, max_x, max_y). Empty input yields the zero box.
89pub(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
144/// Dispatch a `LayoutStrategy` to its implementation (spec §5.2).
145pub 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}