fdg_sim/
graph.rs

1use super::Node;
2use glam::Vec3;
3use petgraph::{graph::NodeIndex, stable_graph::StableGraph, EdgeType, Undirected};
4
5/// A helper type that creates a [`StableGraph`] with our custom [`Node`] as the weight.
6pub type ForceGraph<N, E, Ty = Undirected> = StableGraph<Node<N>, E, Ty>;
7
8/// Syntactic sugar to make adding [`Node`]s to a [`ForceGraph`] easier.
9pub trait ForceGraphHelper<N, E, Ty> {
10    /// Add a [`Node`] to the graph with only the name and arbitrary data.
11    fn add_force_node(&mut self, name: impl AsRef<str>, data: N) -> NodeIndex;
12    /// Add a [`Node`] to the graph with the name, arbitrary data, and a custom location.
13    fn add_force_node_with_coords(
14        &mut self,
15        name: impl AsRef<str>,
16        data: N,
17        location: Vec3,
18    ) -> NodeIndex;
19}
20
21impl<N, E, Ty: EdgeType> ForceGraphHelper<N, E, Ty> for ForceGraph<N, E, Ty> {
22    fn add_force_node(&mut self, name: impl AsRef<str>, data: N) -> NodeIndex {
23        self.add_node(Node::new(name, data))
24    }
25
26    fn add_force_node_with_coords(
27        &mut self,
28        name: impl AsRef<str>,
29        data: N,
30        location: Vec3,
31    ) -> NodeIndex {
32        self.add_node(Node::new_with_coords(name, data, location))
33    }
34}