Struct fdg_sim::Simulation
source · 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)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() {
// initialize a graph
let mut graph: ForceGraph<(), ()> = ForceGraph::default();
// add nodes to it
let one = graph.add_force_node("one", ());
let two = graph.add_force_node("two", ());
let _three = graph.add_force_node("three", ());
graph.add_edge(one, two, ());
// create a simulation from the graph
let mut simulation = Simulation::from_graph(graph, SimulationParameters::default());
// your event/render loop
for frame in 0..50 {
// update the nodes positions based on force algorithm
simulation.update(0.035);
// render (print) your nodes new locations.
println!("---- frame {frame} ----");
for node in simulation.get_graph().node_weights() {
println!("\"{}\" - {:?}", node.name, node.location);
}
}
}
More examples
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)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() {
// initialize a graph
let mut graph: ForceGraph<(), ()> = ForceGraph::default();
// add nodes to it
let one = graph.add_force_node("one", ());
let two = graph.add_force_node("two", ());
let _three = graph.add_force_node("three", ());
graph.add_edge(one, two, ());
// create a simulation from the graph
let mut simulation = Simulation::from_graph(graph, SimulationParameters::default());
// your event/render loop
for frame in 0..50 {
// update the nodes positions based on force algorithm
simulation.update(0.035);
// render (print) your nodes new locations.
println!("---- frame {frame} ----");
for node in simulation.get_graph().node_weights() {
println!("\"{}\" - {:?}", node.name, node.location);
}
}
}
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)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() {
// initialize a graph
let mut graph: ForceGraph<(), ()> = ForceGraph::default();
// add nodes to it
let one = graph.add_force_node("one", ());
let two = graph.add_force_node("two", ());
let _three = graph.add_force_node("three", ());
graph.add_edge(one, two, ());
// create a simulation from the graph
let mut simulation = Simulation::from_graph(graph, SimulationParameters::default());
// your event/render loop
for frame in 0..50 {
// update the nodes positions based on force algorithm
simulation.update(0.035);
// render (print) your nodes new locations.
println!("---- frame {frame} ----");
for node in simulation.get_graph().node_weights() {
println!("\"{}\" - {:?}", node.name, node.location);
}
}
}
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
.