pub struct Simulation<N, E, Ty = Undirected> { /* private fields */ }
Expand description
A simulation for managing the main event loop and forces.
Implementations§
Source§impl<N, E, Ty: EdgeType> Simulation<N, E, Ty>
impl<N, E, Ty: EdgeType> Simulation<N, E, Ty>
Sourcepub fn from_graph(
graph: ForceGraph<N, E, Ty>,
parameters: SimulationParameters<N, E, Ty>,
) -> Self
pub fn from_graph( graph: ForceGraph<N, E, Ty>, parameters: SimulationParameters<N, E, Ty>, ) -> Self
Create a simulation from a ForceGraph
.
Examples found in repository?
examples/basic.rs (line 14)
3fn main() {
4 // initialize a graph
5 let mut graph: ForceGraph<(), ()> = ForceGraph::default();
6
7 // add nodes to it
8 let one = graph.add_force_node("one", ());
9 let two = graph.add_force_node("two", ());
10 let _three = graph.add_force_node("three", ());
11 graph.add_edge(one, two, ());
12
13 // create a simulation from the graph
14 let mut simulation = Simulation::from_graph(graph, SimulationParameters::default());
15
16 // your event/render loop
17 for frame in 0..50 {
18 // update the nodes positions based on force algorithm
19 simulation.update(0.035);
20
21 // render (print) your nodes new locations.
22 println!("---- frame {frame} ----");
23 for node in simulation.get_graph().node_weights() {
24 println!("\"{}\" - {:?}", node.name, node.location);
25 }
26 }
27}
Sourcepub fn reset_node_placement(&mut self)
pub fn reset_node_placement(&mut self)
Randomly place the nodes within the starting square. In practice, this restarts the simulation.
Sourcepub fn update(&mut self, dt: f32)
pub fn update(&mut self, dt: f32)
Update the graph’s node’s positions for a given interval.
Examples found in repository?
examples/basic.rs (line 19)
3fn main() {
4 // initialize a graph
5 let mut graph: ForceGraph<(), ()> = ForceGraph::default();
6
7 // add nodes to it
8 let one = graph.add_force_node("one", ());
9 let two = graph.add_force_node("two", ());
10 let _three = graph.add_force_node("three", ());
11 graph.add_edge(one, two, ());
12
13 // create a simulation from the graph
14 let mut simulation = Simulation::from_graph(graph, SimulationParameters::default());
15
16 // your event/render loop
17 for frame in 0..50 {
18 // update the nodes positions based on force algorithm
19 simulation.update(0.035);
20
21 // render (print) your nodes new locations.
22 println!("---- frame {frame} ----");
23 for node in simulation.get_graph().node_weights() {
24 println!("\"{}\" - {:?}", node.name, node.location);
25 }
26 }
27}
Sourcepub fn update_custom(&mut self, force: &Force<N, E, Ty>, dt: f32)
pub fn update_custom(&mut self, force: &Force<N, E, Ty>, dt: f32)
Update the graph’s node’s positions for a given interval with a custom Force
.
Sourcepub fn visit_nodes(&self, cb: &mut impl Fn(&Node<N>))
pub fn visit_nodes(&self, cb: &mut impl Fn(&Node<N>))
Run a callback on all the nodes.
Sourcepub fn visit_edges(&self, cb: &mut impl Fn(&Node<N>, &Node<N>))
pub fn visit_edges(&self, cb: &mut impl Fn(&Node<N>, &Node<N>))
Run a callback on all of the edges.
Sourcepub fn get_graph(&self) -> &ForceGraph<N, E, Ty>
pub fn get_graph(&self) -> &ForceGraph<N, E, Ty>
Retrieve a reference to the internal ForceGraph
.
Examples found in repository?
examples/basic.rs (line 23)
3fn main() {
4 // initialize a graph
5 let mut graph: ForceGraph<(), ()> = ForceGraph::default();
6
7 // add nodes to it
8 let one = graph.add_force_node("one", ());
9 let two = graph.add_force_node("two", ());
10 let _three = graph.add_force_node("three", ());
11 graph.add_edge(one, two, ());
12
13 // create a simulation from the graph
14 let mut simulation = Simulation::from_graph(graph, SimulationParameters::default());
15
16 // your event/render loop
17 for frame in 0..50 {
18 // update the nodes positions based on force algorithm
19 simulation.update(0.035);
20
21 // render (print) your nodes new locations.
22 println!("---- frame {frame} ----");
23 for node in simulation.get_graph().node_weights() {
24 println!("\"{}\" - {:?}", node.name, node.location);
25 }
26 }
27}
Sourcepub fn get_graph_mut(&mut self) -> &mut ForceGraph<N, E, Ty>
pub fn get_graph_mut(&mut self) -> &mut ForceGraph<N, E, Ty>
Retrieve a mutable reference to the internal ForceGraph
.
Sourcepub fn set_graph(&mut self, graph: ForceGraph<N, E, Ty>)
pub fn set_graph(&mut self, graph: ForceGraph<N, E, Ty>)
Set the internal ForceGraph
.
Sourcepub fn parameters(&self) -> &SimulationParameters<N, E, Ty>
pub fn parameters(&self) -> &SimulationParameters<N, E, Ty>
Retrieve a reference to the internal SimulationParameters
.
Sourcepub fn parameters_mut(&mut self) -> &mut SimulationParameters<N, E, Ty>
pub fn parameters_mut(&mut self) -> &mut SimulationParameters<N, E, Ty>
Retreive a mutable reference to the internal SimulationParameters
.
Trait Implementations§
Auto Trait Implementations§
impl<N, E, Ty> Freeze for Simulation<N, E, Ty>
impl<N, E, Ty> RefUnwindSafe for Simulation<N, E, Ty>
impl<N, E, Ty> Send for Simulation<N, E, Ty>
impl<N, E, Ty> Sync for Simulation<N, E, Ty>
impl<N, E, Ty> Unpin for Simulation<N, E, Ty>
impl<N, E, Ty> UnwindSafe for Simulation<N, E, Ty>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more