forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
84 lines (65 loc) · 2.46 KB
/
main.rs
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
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: MIT
use plotters::prelude::*;
use slint::SharedPixelBuffer;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
mod wasm_backend;
slint::slint! {
export { MainWindow } from "plotter.slint";
}
fn pdf(x: f64, y: f64, a: f64) -> f64 {
const SDX: f64 = 0.1;
const SDY: f64 = 0.1;
let x = x as f64 / 10.0;
let y = y as f64 / 10.0;
a * (-x * x / 2.0 / SDX / SDX - y * y / 2.0 / SDY / SDY).exp()
}
fn render_plot(pitch: f32, yaw: f32, amplitude: f32) -> slint::Image {
let mut pixel_buffer = SharedPixelBuffer::new(640, 480);
let size = (pixel_buffer.width(), pixel_buffer.height());
let backend = BitMapBackend::with_buffer(pixel_buffer.make_mut_bytes(), size);
// Plotters requires TrueType fonts from the file system to draw axis text - we skip that for
// WASM for now.
#[cfg(target_arch = "wasm32")]
let backend = wasm_backend::BackendWithoutText { backend };
let root = backend.into_drawing_area();
root.fill(&WHITE).expect("error filling drawing area");
let mut chart = ChartBuilder::on(&root)
.build_cartesian_3d(-3.0..3.0, 0.0..6.0, -3.0..3.0)
.expect("error building coordinate system");
chart.with_projection(|mut p| {
p.pitch = pitch as f64;
p.yaw = yaw as f64;
p.scale = 0.7;
p.into_matrix() // build the projection matrix
});
chart.configure_axes().draw().expect("error drawing");
chart
.draw_series(
SurfaceSeries::xoz(
(-15..=15).map(|x| x as f64 / 5.0),
(-15..=15).map(|x| x as f64 / 5.0),
|x, y| pdf(x, y, amplitude as f64),
)
.style_func(&|&v| {
(&HSLColor(240.0 / 360.0 - 240.0 / 360.0 * v / 5.0, 1.0, 0.7)).into()
}),
)
.expect("error drawing series");
root.present().expect("error presenting");
drop(chart);
drop(root);
slint::Image::from_rgb8(pixel_buffer)
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub fn main() {
// This provides better error messages in debug mode.
// It's disabled in release mode so it doesn't bloat up the file size.
#[cfg(all(debug_assertions, target_arch = "wasm32"))]
console_error_panic_hook::set_once();
let main_window = MainWindow::new().unwrap();
main_window.on_render_plot(render_plot);
main_window.run().unwrap();
}