Struct 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>

Source

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}
Source

pub fn reset_node_placement(&mut self)

Randomly place the nodes within the starting square. In practice, this restarts the simulation.

Source

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}
Source

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.

Source

pub fn visit_nodes(&self, cb: &mut impl Fn(&Node<N>))

Run a callback on all the nodes.

Source

pub fn visit_edges(&self, cb: &mut impl Fn(&Node<N>, &Node<N>))

Run a callback on all of the edges.

Source

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}
Source

pub fn get_graph_mut(&mut self) -> &mut ForceGraph<N, E, Ty>

Retrieve a mutable reference to the internal ForceGraph.

Source

pub fn set_graph(&mut self, graph: ForceGraph<N, E, Ty>)

Set the internal ForceGraph.

Source

pub fn parameters(&self) -> &SimulationParameters<N, E, Ty>

Retrieve a reference to the internal SimulationParameters.

Source

pub fn parameters_mut(&mut self) -> &mut SimulationParameters<N, E, Ty>

Retreive a mutable reference to the internal SimulationParameters.

Source

pub fn find(&self, query: Vec3, radius: f32) -> Option<NodeIndex>

Retreive a node from the graph based on a query.

Trait Implementations§

Source§

impl<N: Clone, E: Clone, Ty: Clone> Clone for Simulation<N, E, Ty>

Source§

fn clone(&self) -> Simulation<N, E, Ty>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<N, E, Ty: EdgeType> Default for Simulation<N, E, Ty>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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>
where Ty: Send, E: Send, N: Send,

§

impl<N, E, Ty> Sync for Simulation<N, E, Ty>
where Ty: Sync, E: Sync, N: Sync,

§

impl<N, E, Ty> Unpin for Simulation<N, E, Ty>
where Ty: Unpin, E: Unpin, N: Unpin,

§

impl<N, E, Ty> UnwindSafe for Simulation<N, E, Ty>
where Ty: UnwindSafe, E: UnwindSafe, N: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.