graplot/plots/
plot3d.rs

1use litequad::prelude::Conf;
2
3use crate::{render, Matrix, LineDesc, Desc};
4
5
6pub struct Plot3D {
7    pub xs: Matrix,
8    pub ys: Matrix,
9    pub zs: Matrix,
10    pub line_desc: Vec<LineDesc>,
11    pub desc: Desc,
12}
13
14impl Plot3D {
15    pub fn new<A: Plot3DArg>(args: A) -> Plot3D {
16        args.as_plot()
17    }
18
19    pub fn add<A: Plot3DArg>(&mut self, args: A) {
20        let plot = args.as_plot();
21        self.xs.push(plot.xs[0].clone());
22        self.ys.push(plot.ys[0].clone());
23        self.zs.push(plot.zs[0].clone());
24        self.line_desc.push(plot.line_desc[0])
25    }
26
27    pub fn show(self) {
28        let conf = Conf {
29            window_width: 595,
30            window_height: 595,
31            ..Default::default()
32        };
33        litequad::Window::from_config(conf, render::plot3d::run(self));
34    }
35}
36
37pub trait Plot3DArg {
38    fn as_plot(&self) -> Plot3D;
39}
40
41impl<const N: usize> Plot3DArg for ([f64; N], [f64; N], [f64; N]) {
42    fn as_plot(&self) -> Plot3D {
43        Plot3D {
44            xs: vec![self.0.to_vec()],
45            ys: vec![self.1.to_vec()],
46            zs: vec![self.2.to_vec()],
47            line_desc: vec![Default::default()],
48            desc: Default::default(),
49        }
50    }
51}
52
53impl<const N: usize> Plot3DArg for ([f64; N], [f64; N], [f64; N], &str) {
54    fn as_plot(&self) -> Plot3D {
55        Plot3D {
56            xs: vec![self.0.to_vec()],
57            ys: vec![self.1.to_vec()],
58            zs: vec![self.2.to_vec()],
59            line_desc: vec![self.3.into()],
60            desc: Default::default(),
61        }
62    }
63}
64
65impl<const N: usize> Plot3DArg for (&[f64; N], &[f64; N], &[f64; N], &str) {
66    fn as_plot(&self) -> Plot3D {
67        Plot3D {
68            xs: vec![self.0.to_vec()],
69            ys: vec![self.1.to_vec()],
70            zs: vec![self.2.to_vec()],
71            line_desc: vec![self.3.into()],
72            desc: Default::default(),
73        }
74    }
75}
76
77impl<const N: usize> Plot3DArg for (&[f64; N], &[f64; N], &[f64; N]) {
78    fn as_plot(&self) -> Plot3D {
79        Plot3D {
80            xs: vec![self.0.to_vec()],
81            ys: vec![self.1.to_vec()],
82            zs: vec![self.2.to_vec()],
83            line_desc: vec![Default::default()],
84            desc: Default::default(),
85        }
86    }
87}
88
89impl Plot3DArg for (&[f64], &[f64], &[f64], &str) {
90    fn as_plot(&self) -> Plot3D {
91        Plot3D {
92            xs: vec![self.0.to_vec()],
93            ys: vec![self.1.to_vec()],
94            zs: vec![self.2.to_vec()],
95            line_desc: vec![self.3.into()],
96            desc: Default::default(),
97        }
98    }
99}
100
101impl Plot3DArg for (&[f64], &[f64], &[f64]) {
102    fn as_plot(&self) -> Plot3D {
103        Plot3D {
104            xs: vec![self.0.to_vec()],
105            ys: vec![self.1.to_vec()],
106            zs: vec![self.2.to_vec()],
107            line_desc: vec![Default::default()],
108            desc: Default::default(),
109        }
110    }
111}