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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Mask Tools Example.
//!
//! Demonstrates the low-level [`MaskToolsWidget`] driving an interactive mask
//! over a bare [`Plot2D`]: pencil / eraser brushes and rectangle / polygon /
//! ellipse shape tools, each with a live cursor preview, shown as a colored
//! overlay (silx `MaskToolsWidget`). While a tool is active the plot is put in
//! [`PlotInteractionMode::MaskDraw`] so the primary drag paints the mask instead
//! of panning / zooming; with no tool selected the plot zooms normally.
//!
//! `MaskToolsWidget::handle_draw` drives the active tool and paints its preview;
//! `apply` uploads the resulting overlay to the plot.
//!
//! Run with: `cargo run --example high_level_mask_tools`
use eframe::egui;
use rsplot::{Colormap, MaskTool, MaskToolsWidget, Plot2D, PlotInteractionMode};
const WIDTH: u32 = 128;
const HEIGHT: u32 = 96;
struct MaskToolsApp {
image_plot: Plot2D,
mask_tools: MaskToolsWidget,
}
impl MaskToolsApp {
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("Interactive Masking (Hover and Drag)");
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 mask_tools = MaskToolsWidget::new(WIDTH, HEIGHT);
// Start on the pencil so the demo is usable on launch (the toolbar
// switches to the eraser / rectangle / polygon / ellipse tools).
mask_tools.active_tool = MaskTool::Pencil;
Self {
image_plot,
mask_tools,
}
}
}
impl eframe::App for MaskToolsApp {
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
ui.vertical(|ui| {
// Tool selector + level / color / brush / undo controls.
self.mask_tools.show_toolbar(ui);
ui.separator();
// Reserve the primary drag for drawing while a tool is active (silx
// mask-draw mode); otherwise leave the plot in its normal zoom mode.
// Set before `show` so this frame's drag is already handled in the
// right mode.
let want = if self.mask_tools.active_tool != MaskTool::None {
PlotInteractionMode::MaskDraw
} else {
PlotInteractionMode::Zoom
};
if self.image_plot.interaction_mode() != want {
self.image_plot.set_interaction_mode(want);
}
let plot_resp = self.image_plot.show(ui);
// Drive the active tool (pencil / eraser / rectangle / polygon /
// ellipse) and paint its live cursor preview, then upload the
// resulting overlay.
self.mask_tools.handle_draw(ui, &plot_resp);
self.mask_tools.apply(&mut self.image_plot);
});
}
}
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(
"rsplot: Mask Tools",
eframe::NativeOptions {
renderer: eframe::Renderer::Wgpu,
..Default::default()
},
Box::new(|cc| Ok(Box::new(MaskToolsApp::new(cc)) as Box<dyn eframe::App>)),
)
}