use eframe::egui;
use siplot::{Colormap, CurveData, ItemHandle, Plot1D, Plot2D, ProfileMode};
const WIDTH: u32 = 128;
const HEIGHT: u32 = 96;
struct LiveProfileApp {
image_plot: Plot2D,
profile_plot: Plot1D,
pixels: Vec<f32>,
profile_handle: ItemHandle,
}
impl LiveProfileApp {
fn new(cc: &eframe::CreationContext<'_>) -> Self {
let rs = cc
.wgpu_render_state
.as_ref()
.expect("eframe must use the wgpu renderer");
let pixels = build_image();
let mut image_plot = Plot2D::new(rs, 0);
image_plot.set_graph_title("Image (hover for live profile)");
image_plot.set_graph_cursor(true);
image_plot.set_default_colormap(Colormap::viridis(0.0, 1.0));
image_plot
.try_add_default_image(WIDTH, HEIGHT, &pixels)
.expect("image dimensions match");
let mut profile_plot = Plot1D::new(rs, 1);
profile_plot.set_graph_title("Profile");
let init_y: Vec<f64> = vec![0.0; WIDTH as usize];
let init_x: Vec<f64> = (0..WIDTH as usize).map(|i| i as f64).collect();
let profile_handle =
profile_plot.add_curve_with_legend(&init_x, &init_y, egui::Color32::YELLOW, "profile");
Self {
image_plot,
profile_plot,
pixels,
profile_handle,
}
}
}
impl eframe::App for LiveProfileApp {
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
let half_h = ui.available_size() * egui::vec2(1.0, 0.5);
let (plot_resp, mode) = ui
.allocate_ui(half_h, |ui| {
let (_, mode) = self.image_plot.show_toolbar_with(ui, |ui, plot| {
ui.separator();
plot.show_profile_toolbar(ui)
});
let resp = self.image_plot.show(ui);
(resp, mode)
})
.inner;
if let Some((x, y)) =
self.image_plot
.profile_at_cursor(&plot_resp, &self.pixels, WIDTH, HEIGHT, mode)
{
let curve = CurveData::new(x, y, egui::Color32::YELLOW);
self.profile_plot
.update_curve_data(self.profile_handle, &curve);
let label = match mode {
ProfileMode::Horizontal => "column",
ProfileMode::Vertical => "row",
ProfileMode::None | ProfileMode::Line | ProfileMode::Rectangle => "index",
};
self.profile_plot.set_graph_x_label(label);
}
ui.allocate_ui(half_h, |ui| {
self.profile_plot.show(ui);
});
}
}
fn build_image() -> Vec<f32> {
let mut pixels = Vec::with_capacity((WIDTH * HEIGHT) as usize);
for row in 0..HEIGHT {
for col in 0..WIDTH {
let cx = (col as f32 - WIDTH as f32 / 2.0) / (WIDTH as f32 / 4.0);
let cy = (row as f32 - HEIGHT as f32 / 2.0) / (HEIGHT as f32 / 4.0);
pixels.push((-0.5 * (cx * cx + cy * cy)).exp());
}
}
pixels
}
fn main() -> eframe::Result {
eframe::run_native(
"siplot: live profile toolbar",
eframe::NativeOptions {
renderer: eframe::Renderer::Wgpu,
..Default::default()
},
Box::new(|cc| Ok(Box::new(LiveProfileApp::new(cc)) as Box<dyn eframe::App>)),
)
}