Skip to main content

hypergraph_rs/viz/
mod.rs

1//! Scene-graph serializer: the matplotlib-free rendering contract (spec §5.3).
2//!
3//! A `SceneGraph` is a `LayoutResult` enriched with attrs + style, ready for
4//! JSON export, SVG emit, or consumption by a WASM/React renderer.
5
6use serde::{Deserialize, Serialize};
7
8use crate::core::hypergraph::Hypergraph;
9use crate::layout::{compute_layout, LayoutStrategy};
10
11pub mod svg;
12
13/// Re-export so `viz::HyperedgeGeometry` resolves (spec §5.3 places it here).
14pub use crate::layout::HyperedgeGeometry;
15
16/// Visual style for a node glyph.
17#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
18pub struct NodeStyle {
19    pub fill: String,
20    pub stroke: String,
21    pub stroke_width: f64,
22}
23
24impl Default for NodeStyle {
25    fn default() -> Self {
26        Self {
27            fill: "#1f77b4".to_string(),
28            stroke: "#000000".to_string(),
29            stroke_width: 1.0,
30        }
31    }
32}
33
34/// Visual style for a hyperedge glyph.
35#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
36pub struct HyperedgeStyle {
37    pub fill: String,
38    pub stroke: String,
39    pub opacity: f64,
40}
41
42impl Default for HyperedgeStyle {
43    fn default() -> Self {
44        Self {
45            fill: "#ff7f0e".to_string(),
46            stroke: "#ff7f0e".to_string(),
47            opacity: 0.4,
48        }
49    }
50}
51
52/// A rendered node.
53#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
54pub struct NodeGlyph {
55    pub id: String,
56    pub x: f64,
57    pub y: f64,
58    pub radius: f64,
59    pub attrs: serde_json::Value,
60    pub style: NodeStyle,
61}
62
63/// A rendered hyperedge.
64#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
65pub struct HyperedgeGlyph {
66    pub id: String,
67    pub member_ids: Vec<String>,
68    pub geometry: HyperedgeGeometry,
69    pub attrs: serde_json::Value,
70    pub style: HyperedgeStyle,
71}
72
73/// The scene-graph: the contract between Rust and every renderer.
74#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
75pub struct SceneGraph {
76    pub nodes: Vec<NodeGlyph>,
77    pub hyperedges: Vec<HyperedgeGlyph>,
78    pub bounding_box: (f64, f64, f64, f64),
79    pub metadata: serde_json::Map<String, serde_json::Value>,
80}
81
82/// Default rendered node radius (layout units).
83pub const DEFAULT_NODE_RADIUS: f64 = 0.02;
84
85impl<M> Hypergraph<serde_json::Value, serde_json::Value, M> {
86    /// Build a `SceneGraph` from a layout strategy (spec §5.3). Concrete impl:
87    /// clones `serde_json::Value` node/edge attrs into the glyphs.
88    pub fn render(&self, strategy: LayoutStrategy) -> SceneGraph {
89        let layout = compute_layout(self, strategy);
90
91        let nodes: Vec<NodeGlyph> = layout
92            .node_positions
93            .iter()
94            .map(|(id, (x, y))| {
95                let attrs = self
96                    .node_attrs(id)
97                    .cloned()
98                    .unwrap_or(serde_json::Value::Null);
99                NodeGlyph {
100                    id: id.clone(),
101                    x: *x,
102                    y: *y,
103                    radius: DEFAULT_NODE_RADIUS,
104                    attrs,
105                    style: NodeStyle::default(),
106                }
107            })
108            .collect();
109
110        let hyperedges: Vec<HyperedgeGlyph> = layout
111            .edge_geometries
112            .iter()
113            .map(|(eid, geom)| {
114                let attrs = self
115                    .edge_attrs(eid)
116                    .cloned()
117                    .unwrap_or(serde_json::Value::Null);
118                let member_ids = self.members(eid).unwrap_or_default();
119                HyperedgeGlyph {
120                    id: eid.clone(),
121                    member_ids,
122                    geometry: geom.clone(),
123                    attrs,
124                    style: HyperedgeStyle::default(),
125                }
126            })
127            .collect();
128
129        SceneGraph {
130            nodes,
131            hyperedges,
132            bounding_box: layout.bounding_box,
133            metadata: serde_json::Map::new(),
134        }
135    }
136
137    /// Serialize the rendered scene-graph to JSON (spec §5.3).
138    pub fn render_to_json(&self, strategy: LayoutStrategy) -> String {
139        let scene = self.render(strategy);
140        serde_json::to_string(&scene).unwrap_or_else(|_| "{}".to_string())
141    }
142
143    /// Render the scene-graph to a self-contained SVG string (spec §5.3).
144    pub fn render_to_svg(&self, strategy: LayoutStrategy) -> String {
145        let scene = self.render(strategy);
146        svg::scene_to_svg(&scene)
147    }
148}