Add a plotter example using plotters

This is kind of an interactive port of
https://github.com/38/plotters/blob/master/examples/3d-plot2.rs
This commit is contained in:
Simon Hausmann 2021-08-05 14:57:29 +02:00 committed by Simon Hausmann
parent 8a3a68a4fa
commit 1d83837228
5 changed files with 170 additions and 1 deletions

View file

@ -0,0 +1,35 @@
[package]
name = "plotter"
version = "0.1.0"
authors = ["SixtyFPS <info@sixtyfps.io>"]
edition = "2018"
publish = false
license = "GPL-3.0-only"
[[bin]]
path = "main.rs"
name = "plotter"
[dependencies]
sixtyfps = { path = "../../api/sixtyfps-rs" }
#plotters = { version = "0.3.1", default-features = false, features = ["bitmap"] }
plotters = { version = "0.3.1", default-features = false, features = ["bitmap_backend", "surface_series"] }
imgref = "1"
rgb = "0.8.27"
[build-dependencies]
sixtyfps-build = { path = "../../api/sixtyfps-rs/sixtyfps-build" }
# Remove the `#wasm#` to uncomment the wasm build.
# This is commented out by default because we don't want to build it as a library by default
# The CI has a script that does sed "s/#wasm# //" to generate the wasm build.
#wasm# [lib]
#wasm# path = "main.rs"
#wasm# crate-type = ["cdylib"]
#wasm# [target.'cfg(target_arch = "wasm32")'.dependencies]
#wasm# wasm-bindgen = { version = "0.2" }
#wasm# web-sys = { version = "0.3", features=["console"] }
#wasm# console_error_panic_hook = "0.1.5"
#wasm# getrandom = { version = "0.2.2", features = ["js"] }

92
examples/plotter/main.rs Normal file
View file

@ -0,0 +1,92 @@
/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2021 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2021 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
use plotters::prelude::*;
use rgb::ComponentBytes;
use sixtyfps::SharedPixelBuffer;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
sixtyfps::sixtyfps! {
import { MainWindow } from "plotter.60";
}
fn pdf(x: f64, y: f64) -> f64 {
const SDX: f64 = 0.1;
const SDY: f64 = 0.1;
const A: f64 = 5.0;
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) -> sixtyfps::Image {
let mut pixel_buffer = SharedPixelBuffer::<rgb::RGB8>::new(640, 480);
let size = (pixel_buffer.width() as u32, pixel_buffer.height() as u32);
let root = BitMapBackend::with_buffer(pixel_buffer.as_mut_slice().as_bytes_mut(), size)
.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 = 1.57 - (1.57 - pitch as f64 / 50.0).abs();
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),
pdf,
)
.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);
sixtyfps::SharedImageBuffer::RGB8(pixel_buffer).into()
}
#[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();
main_window.on_redraw({
let main_window_weak = main_window.as_weak();
move || {
let main_window = main_window_weak.upgrade().unwrap();
let plot = render_plot(main_window.get_pitch());
main_window.set_plot_image(plot);
}
});
main_window.invoke_redraw();
main_window.run();
}

View file

@ -0,0 +1,40 @@
/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2021 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2021 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
import { Slider, GroupBox } from "sixtyfps_widgets.60";
export MainWindow := Window {
title: "SixtyFPS Plotter Integration Example";
preferred-width: 800px;
preferred-height: 600px;
property plot_image <=> img.source;
property pitch <=> pitch_slider.value;
callback redraw();
VerticalLayout {
Text {
font-size: 20px;
text: "2D Gaussian PDF";
horizontal-alignment: center;
}
img := Image {
}
GroupBox {
title: "Pitch";
pitch_slider := Slider {
minimum: 0;
maximum: 180;
value: 42;
changed => { root.redraw(); }
}
}
}
}