summaryrefslogtreecommitdiff
path: root/src/plot/gnuplot.rs
blob: ea52a18157b6a9091e125b3533414789f7b89516 (plain)
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
use std::io;
use std::io::Write;
use std::path::PathBuf;
use std::process;
use std::process::{Command, Stdio};

pub struct Gnuplot;

impl Gnuplot {
    fn exec_plot(
        data: &[[f64; 6]],
        chunk_size: u32,
        start: usize,
        end: usize,
        script: &str,
    ) -> Result<process::Child, io::Error> {
        let mut path = PathBuf::from("gnuplot");
        path.push(script);
        let mut file = std::fs::File::create("data").expect("could not create data file");
        let process = Command::new("gnuplot")
            .args(&[
                "-e",
                format!("states={}; start={}; end={}", chunk_size, start, end).as_ref(),
                "-c",
                path.to_str().unwrap_or(script),
            ])
            .stdin(process::Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()
            .expect("Failed to spawn child process");

        //let stdin = process.stdin.as_mut().expect("Failed to open stdin");
        for line in data {
            let line = format!(
                "{}; {}; {}; {}; {}; {}\n",
                line[0], line[1], line[2], line[3], line[4], line[5]
            );
            file.write_all(line.as_bytes())
                .expect("Failed to write to stdin");
        }
        Ok(process)
    }
}

impl super::Graph for Gnuplot {
    fn plot3d(data: &[[f64; 6]], chunk_size: u32) {
        let iterations = data.len() / chunk_size as usize;
        match Gnuplot::exec_plot(data, chunk_size, 1, iterations, "anim3d.plt") {
            Ok(mut process) => println!("opening gnuplot {:?}", process.wait()),
            Err(err) => println!("failed to start gnuplot: {}", err),
        }
    }
    fn animate3d(data: &[[f64; 6]], chunk_size: u32, start: usize, end: usize) {
        match Gnuplot::exec_plot(data, chunk_size, start, end, "anim3d.plt") {
            Ok(mut process) => println!("opening gnuplot {:?}", process.wait()),
            Err(err) => println!("failed to start gnuplot: {}", err),
        }
    }
    fn plot2d(data: &[[f64; 6]], chunk_size: u32) {
        let iterations = data.len() / chunk_size as usize;
        match Gnuplot::exec_plot(data, chunk_size, 1, iterations, "anim2d.plt") {
            Ok(mut process) => println!("opening gnuplot {:?}", process.wait()),
            Err(err) => println!("failed to start gnuplot: {}", err),
        }
    }
    fn animate2d(data: &[[f64; 6]], chunk_size: u32, start: usize, end: usize) {
        match Gnuplot::exec_plot(data, chunk_size, start, end, "anim2d.plt") {
            Ok(mut process) => println!("opening gnuplot {:?}", process.wait()),
            Err(err) => println!("failed to start gnuplot: {}", err),
        }
    }
}