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
#![doc = include_str!("../README.md")]

use std::error::Error;

use fdg_sim::{
    force::Force,
    glam::Vec3,
    petgraph::visit::{EdgeRef, IntoEdgeReferences},
    ForceGraph, Simulation, SimulationParameters,
};
use plotters::prelude::*;

/// Parameters for drawing the SVG image.
pub struct Settings {
    pub iterations: usize,
    pub dt: f32,
    pub node_size: u32,
    pub node_color: (u8, u8, u8),
    pub edge_size: u32,
    pub edge_color: (u8, u8, u8),
    pub background_color: (u8, u8, u8),
    pub print_progress: bool,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            iterations: 2000,
            dt: 0.035,
            node_size: 1,
            node_color: (0, 0, 0),
            edge_size: 3,
            edge_color: (255, 0, 0),
            background_color: (255, 255, 255),
            print_progress: true,
        }
    }
}

/// Generate an image from a graph and a force.
pub fn gen_image<N: Clone, E: Clone>(
    graph: &ForceGraph<N, E>,
    force: &Force<N, E>,
    settings: Option<Settings>,
) -> Result<String, Box<dyn Error>> {
    let settings = match settings {
        Some(settings) => settings,
        None => Settings::default(),
    };

    let mut sim = Simulation::from_graph(graph, SimulationParameters::default());
    sim.parameters_mut().set_force(force.clone());

    for i in 0..settings.iterations {
        if settings.print_progress && i % 10 == 0 {
            println!("{}/{}", i, settings.iterations);
        }
        sim.update(settings.dt);
    }

    // get the size of the graph (avg of width and height to account for oddly shaped graphs)
    let graph_size: f32 = {
        let mut top = 0.0;
        let mut bottom = 0.0;
        let mut left = 0.0;
        let mut right = 0.0;

        for node in sim.get_graph().node_weights() {
            let loc = node.location;

            if loc.x > right {
                right = loc.x;
            }

            if loc.x < left {
                left = loc.x;
            }

            if loc.y > top {
                top = loc.y
            }

            if loc.y < bottom {
                bottom = loc.y;
            }
        }

        let sum = (right - left) + (top - bottom);

        sum / 2.0
    };

    let f = 1.5;

    let image_size = ((graph_size * f) as u32, (graph_size * f) as u32);

    // translate all points to center
    let mut location_sum = Vec3::ZERO;
    for node in sim.get_graph().node_weights() {
        location_sum += node.location;
    }

    let avg_vec = location_sum / sim.get_graph().node_count() as f32;
    for node in sim.get_graph_mut().node_weights_mut() {
        node.location -= avg_vec;
    }

    // translate all the points over into image coordinate space
    for node in sim.get_graph_mut().node_weights_mut() {
        node.location.x += (image_size.0 / 2) as f32;
        node.location.y += (image_size.1 / 2) as f32;
    }

    let mut buffer = String::new();

    let backend = SVGBackend::with_string(&mut buffer, image_size).into_drawing_area();

    backend
        .fill(&RGBAColor(
            settings.background_color.0,
            settings.background_color.1,
            settings.background_color.2,
            1.0,
        ))
        .unwrap();

    for edge in sim.get_graph().edge_references() {
        let source = &sim.get_graph()[edge.source()].location;
        let target = &sim.get_graph()[edge.target()].location;

        backend.draw(&PathElement::new(
            vec![
                (source.x as i32, source.y as i32),
                (target.x as i32, target.y as i32),
            ],
            ShapeStyle {
                color: RGBAColor(
                    settings.edge_color.0,
                    settings.edge_color.1,
                    settings.edge_color.2,
                    1.0,
                ),
                filled: true,
                stroke_width: settings.edge_size,
            },
        ))?;
    }

    for node in sim.get_graph().node_weights() {
        backend.draw(&Circle::new(
            (node.location.x as i32, node.location.y as i32),
            settings.node_size * 10,
            ShapeStyle {
                color: RGBAColor(
                    settings.node_color.0,
                    settings.node_color.1,
                    settings.node_color.2,
                    1.0,
                ),
                filled: true,
                stroke_width: 1,
            },
        ))?;
    }

    drop(backend);

    Ok(buffer)
}