1
  2
  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
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use petgraph::{
    stable_graph::{NodeIndex, StableUnGraph},
    visit::{EdgeRef, IntoEdgeReferences},
};

pub type DefaultNodeIdx = NodeIndex<petgraph::stable_graph::DefaultIx>;

const FORCE_CHARGE: f32 = 12000.0;
const FORCE_SPRING: f32 = 0.3;

const FORCE_MAX: f32 = 280.0;
const NODE_SPEED: f32 = 7000.0;
const DAMPING_FACTOR: f32 = 0.95;

pub struct Node<D> {
    pub x: f32,
    pub y: f32,
    vx: f32,
    vy: f32,
    ax: f32,
    ay: f32,
    mass: f32,
    is_anchor: bool,
    pub data: D,
}

impl<D> Node<D> {
    fn apply_force(&mut self, fx: f32, fy: f32, dt: f32) {
        self.ax += fx.max(-FORCE_MAX).min(FORCE_MAX) * dt;
        self.ay += fy.max(-FORCE_MAX).min(FORCE_MAX) * dt;
    }

    fn update(&mut self, dt: f32) {
        self.vx = (self.vx + self.ax * dt * NODE_SPEED) * DAMPING_FACTOR;
        self.vy = (self.vy + self.ay * dt * NODE_SPEED) * DAMPING_FACTOR;
        self.x += self.vx * dt;
        self.y += self.vy * dt;
        self.ax = 0.0;
        self.ay = 0.0;
    }
}

pub struct Edge;

pub struct ForceGraph<D> {
    graph: StableUnGraph<Node<D>, Edge>,
}

impl<D> ForceGraph<D> {
    pub fn new() -> Self {
        ForceGraph {
            graph: StableUnGraph::default(),
        }
    }

    pub fn get_graph(&self) -> &StableUnGraph<Node<D>, Edge> {
        &self.graph
    }

    pub fn add_node(
        &mut self,
        x: f32,
        y: f32,
        data: D,
        mass: f32,
        is_anchor: bool,
    ) -> DefaultNodeIdx {
        self.graph.add_node(Node {
            x,
            y,
            vx: 0.0,
            vy: 0.0,
            ax: 0.0,
            ay: 0.0,
            mass,
            is_anchor,
            data,
        })
    }

    pub fn remove_node(&mut self, idx: DefaultNodeIdx) {
        self.graph.remove_node(idx);
    }

    pub fn add_edge(&mut self, n1_idx: DefaultNodeIdx, n2_idx: DefaultNodeIdx) {
        self.graph.update_edge(n1_idx, n2_idx, Edge);
    }

    pub fn clear(&mut self) {
        self.graph.clear();
    }

    pub fn update(&mut self, dt: f32) {
        if self.graph.node_count() == 0 {
            return;
        }

        let first_node_idx = self
            .graph
            .node_indices()
            .next()
            .expect("first node missing");

        let mut bfs = petgraph::visit::Bfs::new(&self.graph, first_node_idx);

        while let Some(n1) = bfs.next(&self.graph) {
            let mut edges = self.graph.neighbors(n1).detach();
            while let Some(n2) = edges.next_node(&self.graph) {
                let f = attract_nodes(&self.graph[n1], &self.graph[n2]);
                self.graph[n1].apply_force(f.0, f.1, dt);
            }
        }

        let mut bfs1 = petgraph::visit::Bfs::new(&self.graph, first_node_idx);

        while let Some(n1) = bfs1.next(&self.graph) {
            if self.graph[n1].is_anchor {
                continue;
            }

            let mut bfs2 = petgraph::visit::Bfs::new(&self.graph, n1);
            bfs2.next(&self.graph);

            while let Some(n2) = bfs2.next(&self.graph) {
                let f = repel_nodes(&self.graph[n1], &self.graph[n2]);
                self.graph[n1].apply_force(f.0, f.1, dt);
            }

            self.graph[n1].update(dt);
        }
    }

    pub fn visit_nodes<F: FnMut(&Node<D>)>(&self, mut cb: F) {
        for n_idx in self.graph.node_indices() {
            cb(&self.graph[n_idx]);
        }
    }

    pub fn visit_edges<F: FnMut(&Node<D>, &Node<D>)>(&self, mut cb: F) {
        for edge_ref in self.graph.edge_references() {
            let source = &self.graph[edge_ref.source()];
            let target = &self.graph[edge_ref.target()];
            cb(source, target);
        }
    }
}

fn attract_nodes<D>(n1: &Node<D>, n2: &Node<D>) -> (f32, f32) {
    let mut dx = n2.x - n1.x;
    let mut dy = n2.y - n1.y;

    let distance = if dx == 0.0 && dy == 0.0 {
        1.0
    } else {
        (dx * dx + dy * dy).sqrt()
    };

    dx /= distance;
    dy /= distance;

    let strength = 1.0 * FORCE_SPRING * distance * 0.5;
    (dx * strength, dy * strength)
}

fn repel_nodes<D>(n1: &Node<D>, n2: &Node<D>) -> (f32, f32) {
    let mut dx = n2.x - n1.x;
    let mut dy = n2.y - n1.y;

    let distance = if dx == 0.0 && dy == 0.0 {
        1.0
    } else {
        (dx * dx + dy * dy).sqrt()
    };

    dx /= distance;
    dy /= distance;

    let strength = -FORCE_CHARGE * ((n1.mass * n2.mass) / (distance * distance));
    (dx * strength, dy * strength)
}