Skip to main content

hypergraph_rs/layout/
place.rs

1//! Deterministic placement layouts (no RNG): bipartite two-column and radial.
2
3use crate::core::hypergraph::Hypergraph;
4
5use super::{bounding_box_of, BipartiteAlign, HyperedgeGeometry, LayoutResult};
6use std::f64::consts::PI;
7
8/// Evenly spread index `i` of `count` items into [0, 1]; a single item sits at
9/// the midpoint 0.5.
10fn spread(count: usize, i: usize) -> f64 {
11    if count <= 1 {
12        0.5
13    } else {
14        i as f64 / (count as f64 - 1.0)
15    }
16}
17
18/// Bipartite layout: agents in one column/row, hyperedge markers in the other.
19/// Deterministic — no RNG. Each hyperedge emits a `Stripe` spanning the
20/// (oriented) y-range of its members at the marker's x. `Horizontal` align
21/// swaps the role axis; see the module deviation note (its stripe is degenerate).
22pub fn bipartite_layout<N, E, M>(h: &Hypergraph<N, E, M>, align: BipartiteAlign) -> LayoutResult {
23    let node_ids = h.node_ids();
24    let edge_ids = h.edge_ids();
25
26    let orient = |x: f64, y: f64| -> (f64, f64) {
27        match align {
28            BipartiteAlign::Vertical => (x, y),
29            BipartiteAlign::Horizontal => (y, x),
30        }
31    };
32
33    let node_positions: Vec<(String, (f64, f64))> = node_ids
34        .iter()
35        .enumerate()
36        .map(|(i, id)| (id.clone(), orient(0.0, spread(node_ids.len(), i))))
37        .collect();
38
39    let pos_of = |id: &str| -> (f64, f64) {
40        node_positions
41            .iter()
42            .find(|(n, _)| n == id)
43            .map(|(_, p)| *p)
44            .unwrap_or((0.0, 0.0))
45    };
46
47    let edge_geometries: Vec<(String, HyperedgeGeometry)> = edge_ids
48        .iter()
49        .enumerate()
50        .map(|(i, eid)| {
51            let (ex, _ey) = orient(1.0, spread(edge_ids.len(), i));
52            let members = h.members(eid).unwrap_or_default();
53            let (y_min, y_max) = if members.is_empty() {
54                (0.0, 0.0)
55            } else {
56                let mut lo = f64::INFINITY;
57                let mut hi = f64::NEG_INFINITY;
58                for m in &members {
59                    let (_mx, my) = pos_of(m);
60                    if my < lo {
61                        lo = my;
62                    }
63                    if my > hi {
64                        hi = my;
65                    }
66                }
67                (lo, hi)
68            };
69            (
70                eid.clone(),
71                HyperedgeGeometry::Stripe {
72                    x: ex,
73                    y_min,
74                    y_max,
75                },
76            )
77        })
78        .collect();
79
80    let bounding_box = bounding_box_of(&node_positions, &edge_geometries);
81    LayoutResult {
82        node_positions,
83        edge_geometries,
84        bounding_box,
85    }
86}
87
88/// Radial layout: agents on concentric rings bucketed by membership count
89/// (higher membership ⇒ outer ring), evenly spread by angle in insertion order.
90/// Each hyperedge emits an `Arc` at the mean radius of its members spanning
91/// their angular range. Deterministic — no RNG.
92pub fn radial_layout<N, E, M>(h: &Hypergraph<N, E, M>, rings: usize) -> LayoutResult {
93    let node_ids = h.node_ids();
94    let rings = rings.max(1);
95    let n = node_ids.len();
96
97    let degree = |id: &str| -> usize { h.memberships(id).map(|m| m.len()).unwrap_or(0) };
98    let max_deg = node_ids.iter().map(|id| degree(id)).max().unwrap_or(0);
99
100    let node_positions: Vec<(String, (f64, f64))> = node_ids
101        .iter()
102        .enumerate()
103        .map(|(i, id)| {
104            let ring = (degree(id) * (rings - 1)).checked_div(max_deg).unwrap_or(0);
105            let radius = (ring as f64 + 1.0) / (rings as f64 + 1.0);
106            let angle = if n == 0 {
107                0.0
108            } else {
109                2.0 * PI * (i as f64) / (n as f64)
110            };
111            (id.clone(), (radius * angle.cos(), radius * angle.sin()))
112        })
113        .collect();
114
115    let pos_of = |id: &str| -> (f64, f64) {
116        node_positions
117            .iter()
118            .find(|(nn, _)| nn == id)
119            .map(|(_, p)| *p)
120            .unwrap_or((0.0, 0.0))
121    };
122
123    let edge_geometries: Vec<(String, HyperedgeGeometry)> = h
124        .edge_ids()
125        .iter()
126        .map(|eid| {
127            let members = h.members(eid).unwrap_or_default();
128            let (radius, a_min, a_max) = if members.is_empty() {
129                (0.0, 0.0, 0.0)
130            } else {
131                let mut rsum = 0.0;
132                let mut amin = f64::INFINITY;
133                let mut amax = f64::NEG_INFINITY;
134                for m in &members {
135                    let (x, y) = pos_of(m);
136                    rsum += (x * x + y * y).sqrt();
137                    let ang = y.atan2(x);
138                    if ang < amin {
139                        amin = ang;
140                    }
141                    if ang > amax {
142                        amax = ang;
143                    }
144                }
145                (rsum / members.len() as f64, amin, amax)
146            };
147            (
148                eid.clone(),
149                HyperedgeGeometry::Arc {
150                    center: (0.0, 0.0),
151                    radius,
152                    angle_range: (a_min, a_max),
153                },
154            )
155        })
156        .collect();
157
158    let bounding_box = bounding_box_of(&node_positions, &edge_geometries);
159    LayoutResult {
160        node_positions,
161        edge_geometries,
162        bounding_box,
163    }
164}