runmat_runtime/builtins/plotting/
mod.rs1#[path = "core/common.rs"]
4pub(crate) mod common;
5#[path = "core/context.rs"]
6pub mod context;
7#[path = "core/engine.rs"]
8pub(crate) mod engine;
9#[path = "core/gpu_helpers.rs"]
10pub(crate) mod gpu_helpers;
11#[path = "core/perf.rs"]
12pub(crate) mod perf;
13#[path = "core/point.rs"]
14pub(crate) mod point;
15#[path = "core/properties.rs"]
16pub(crate) mod properties;
17#[path = "core/state.rs"]
18pub(crate) mod state;
19#[path = "core/style.rs"]
20pub(crate) mod style;
21#[path = "core/web.rs"]
22pub mod web;
23
24#[path = "type_resolvers.rs"]
25pub(crate) mod type_resolvers;
26
27#[path = "ops/area.rs"]
28pub(crate) mod area;
29#[path = "ops/bar.rs"]
30pub(crate) mod bar;
31#[path = "ops/caxis.rs"]
32pub(crate) mod caxis;
33#[path = "ops/clf.rs"]
34pub(crate) mod clf;
35#[path = "ops/clim.rs"]
36pub(crate) mod clim;
37#[path = "ops/close.rs"]
38pub(crate) mod close;
39#[path = "ops/cmds.rs"]
40pub(crate) mod cmds;
41#[path = "ops/contour.rs"]
42pub(crate) mod contour;
43#[path = "ops/contour3.rs"]
44pub(crate) mod contour3;
45#[path = "ops/contourf.rs"]
46pub(crate) mod contourf;
47#[path = "ops/drawnow.rs"]
48pub(crate) mod drawnow;
49#[path = "ops/errorbar.rs"]
50pub(crate) mod errorbar;
51#[path = "ops/figure.rs"]
52pub(crate) mod figure;
53#[path = "ops/fill3.rs"]
54pub(crate) mod fill3;
55#[path = "ops/gca.rs"]
56pub(crate) mod gca;
57#[path = "ops/gcf.rs"]
58pub(crate) mod gcf;
59#[path = "ops/get.rs"]
60pub(crate) mod get;
61#[path = "ops/heatmap.rs"]
62pub(crate) mod heatmap;
63#[path = "ops/hist.rs"]
64pub mod hist;
65#[path = "ops/histogram.rs"]
66pub(crate) mod histogram;
67#[path = "ops/hold.rs"]
68pub(crate) mod hold;
69#[path = "ops/image.rs"]
70pub(crate) mod image;
71#[path = "ops/imagesc.rs"]
72pub(crate) mod imagesc;
73#[path = "ops/imshow.rs"]
74pub(crate) mod imshow;
75#[path = "ops/isgraphics.rs"]
76pub(crate) mod isgraphics;
77#[path = "ops/ishandle.rs"]
78pub(crate) mod ishandle;
79#[path = "ops/legend.rs"]
80pub(crate) mod legend;
81#[path = "ops/loglog.rs"]
82pub(crate) mod loglog;
83#[path = "ops/mesh.rs"]
84pub(crate) mod mesh;
85#[path = "ops/meshc.rs"]
86pub(crate) mod meshc;
87#[path = "ops/common/mod.rs"]
88pub(crate) mod op_common;
89#[path = "ops/patch.rs"]
90pub(crate) mod patch;
91#[path = "ops/pie.rs"]
92pub(crate) mod pie;
93#[path = "ops/plot.rs"]
94pub(crate) mod plot;
95#[path = "ops/plot3.rs"]
96pub(crate) mod plot3;
97#[path = "ops/print.rs"]
98pub(crate) mod print;
99#[path = "ops/quiver.rs"]
100pub(crate) mod quiver;
101#[path = "ops/scatter.rs"]
102pub(crate) mod scatter;
103#[path = "ops/scatter3.rs"]
104pub(crate) mod scatter3;
105#[path = "ops/scatterplot.rs"]
106pub(crate) mod scatterplot;
107#[path = "ops/semilogx.rs"]
108pub(crate) mod semilogx;
109#[path = "ops/semilogy.rs"]
110pub(crate) mod semilogy;
111#[path = "ops/set.rs"]
112pub(crate) mod set;
113#[path = "ops/sgtitle.rs"]
114pub(crate) mod sgtitle;
115#[path = "ops/stairs.rs"]
116pub(crate) mod stairs;
117#[path = "ops/stem.rs"]
118pub(crate) mod stem;
119#[path = "ops/subplot.rs"]
120pub(crate) mod subplot;
121#[path = "ops/suptitle.rs"]
122pub(crate) mod suptitle;
123#[path = "ops/surf.rs"]
124pub(crate) mod surf;
125#[path = "ops/surfc.rs"]
126pub(crate) mod surfc;
127#[path = "ops/text.rs"]
128pub(crate) mod text;
129#[path = "ops/title.rs"]
130pub(crate) mod title;
131#[path = "ops/view.rs"]
132pub(crate) mod view;
133#[path = "ops/xlabel.rs"]
134pub(crate) mod xlabel;
135#[path = "ops/xlim.rs"]
136pub(crate) mod xlim;
137#[path = "ops/xline.rs"]
138pub(crate) mod xline;
139#[path = "ops/ylabel.rs"]
140pub(crate) mod ylabel;
141#[path = "ops/ylim.rs"]
142pub(crate) mod ylim;
143#[path = "ops/yline.rs"]
144pub(crate) mod yline;
145#[path = "ops/zlabel.rs"]
146pub(crate) mod zlabel;
147#[path = "ops/zlim.rs"]
148pub(crate) mod zlim;
149
150pub use perf::{set_scatter_target_points, set_surface_vertex_budget};
151pub use properties::resolve_plot_handle;
152pub use state::{
153 clear_figure, clone_figure, close_figure, configure_subplot, current_axes_state,
154 current_figure_handle, figure_handles, import_figure, install_figure_observer,
155 new_figure_handle, reset_hold_state_for_run, reset_plot_state, reset_recent_figures,
156 select_axes_for_figure, select_figure, set_hold, take_recent_figures, FigureAxesState,
157 FigureError, FigureEventKind, FigureEventView, FigureHandle, HoldMode,
158};
159use std::collections::HashMap;
160use std::sync::{Mutex, OnceLock};
161use web::present_figure_on_surface as web_present_figure_on_surface;
162pub use web::{
163 bind_surface_to_figure, clear_closed_figure_surfaces, detach_surface, fit_surface_extents,
164 get_surface_camera_state, install_surface, invalidate_surface_revisions, present_surface,
165 render_current_scene, reset_surface_camera, resize_surface, set_plot_theme_config,
166 set_surface_camera_state, web_renderer_ready, PlotCameraProjection, PlotCameraState,
167 PlotSurfaceCameraState,
168};
169
170#[derive(Debug, Clone, Copy, PartialEq, Eq)]
171pub enum RuntimePlottingMode {
172 Auto,
173 Interactive,
174 Static,
175}
176
177#[cfg(feature = "gui")]
178pub fn set_runtime_plotting_mode(mode: RuntimePlottingMode) {
179 let mapped = match mode {
180 RuntimePlottingMode::Auto => engine::native::RuntimePlottingMode::Auto,
181 RuntimePlottingMode::Interactive => engine::native::RuntimePlottingMode::Interactive,
182 RuntimePlottingMode::Static => engine::native::RuntimePlottingMode::Static,
183 };
184 engine::native::set_runtime_plotting_mode(mapped);
185}
186
187#[cfg(not(feature = "gui"))]
188pub fn set_runtime_plotting_mode(_mode: RuntimePlottingMode) {}
189
190#[cfg(all(target_arch = "wasm32", feature = "plot-web"))]
191pub use web::handle_plot_surface_event;
192
193pub(crate) fn plotting_error(builtin: &str, message: impl Into<String>) -> crate::RuntimeError {
194 crate::build_runtime_error(message)
195 .with_builtin(builtin)
196 .build()
197}
198
199pub(crate) fn plotting_error_with_source(
200 builtin: &str,
201 message: impl Into<String>,
202 source: impl std::error::Error + Send + Sync + 'static,
203) -> crate::RuntimeError {
204 crate::build_runtime_error(message)
205 .with_builtin(builtin)
206 .with_source(source)
207 .build()
208}
209
210#[cfg(feature = "plot-core")]
211pub fn export_figure_scene(handle: FigureHandle) -> crate::BuiltinResult<Option<Vec<u8>>> {
212 let Some(figure) = clone_figure(handle) else {
213 return Ok(None);
214 };
215 let scene = runmat_plot::event::FigureScene::capture(&figure);
216 crate::replay::export_figure_scene_payload(&scene).map(Some)
217}
218
219#[cfg(feature = "plot-core")]
220pub fn import_figure_scene(bytes: &[u8]) -> crate::BuiltinResult<Option<FigureHandle>> {
221 let scene = crate::replay::import_figure_scene_payload(bytes)?;
222 let figure = scene.into_figure().map_err(|err| {
223 crate::replay_error_with_source(
224 crate::ReplayErrorKind::ImportRejected,
225 "invalid figure scene content",
226 std::io::Error::new(std::io::ErrorKind::InvalidData, err),
227 )
228 })?;
229 let handle = import_figure(figure);
230 register_imported_figure(handle.as_u32());
231 Ok(Some(handle))
232}
233
234#[cfg(feature = "plot-core")]
235pub async fn import_figure_scene_async(bytes: &[u8]) -> crate::BuiltinResult<Option<FigureHandle>> {
236 let scene = crate::replay::import_figure_scene_payload_async(bytes).await?;
237 let figure = scene.into_figure().map_err(|err| {
238 crate::replay_error_with_source(
239 crate::ReplayErrorKind::ImportRejected,
240 "invalid figure scene content",
241 std::io::Error::new(std::io::ErrorKind::InvalidData, err),
242 )
243 })?;
244 let handle = import_figure(figure);
245 register_imported_figure(handle.as_u32());
246 Ok(Some(handle))
247}
248
249#[cfg(feature = "plot-core")]
250pub async fn import_figure_scene_from_path_async(
251 path: &str,
252) -> crate::BuiltinResult<Option<FigureHandle>> {
253 let bytes = runmat_filesystem::read_async(path).await.map_err(|err| {
254 crate::replay_error_with_source(
255 crate::ReplayErrorKind::ImportRejected,
256 format!("failed to read figure scene payload '{path}'"),
257 err,
258 )
259 })?;
260 import_figure_scene_async(&bytes).await
261}
262
263pub fn present_figure_on_surface(surface_id: u32, handle: u32) -> crate::BuiltinResult<()> {
264 web_present_figure_on_surface(surface_id, handle)?;
265 if take_imported_figure(handle) {
266 let _ = reset_surface_camera(surface_id);
267 }
268 Ok(())
269}
270
271type ImportedFigureRegistry = Mutex<HashMap<u32, ()>>;
272
273fn imported_figure_registry() -> &'static ImportedFigureRegistry {
274 static REGISTRY: OnceLock<ImportedFigureRegistry> = OnceLock::new();
275 REGISTRY.get_or_init(|| Mutex::new(HashMap::new()))
276}
277
278fn register_imported_figure(handle: u32) {
279 if let Ok(mut map) = imported_figure_registry().lock() {
280 map.insert(handle, ());
281 }
282}
283
284fn take_imported_figure(handle: u32) -> bool {
285 imported_figure_registry()
286 .lock()
287 .ok()
288 .and_then(|mut map| map.remove(&handle))
289 .is_some()
290}
291
292#[cfg(feature = "plot-core")]
293pub use engine::{
294 render_figure_png_bytes, render_figure_png_bytes_with_axes_cameras,
295 render_figure_png_bytes_with_camera, render_figure_rgba_bytes,
296 render_figure_rgba_bytes_with_axes_cameras, render_figure_rgba_bytes_with_camera,
297 render_figure_snapshot, render_figure_snapshot_with_camera_state,
298};
299
300pub mod ops {
301 pub use super::hist;
302}
303
304#[cfg(test)]
305pub(crate) mod tests {
306 use super::state;
307 use std::sync::Once;
308
309 pub(crate) fn ensure_plot_test_env() {
310 static INIT: Once = Once::new();
311 INIT.call_once(|| {
312 state::disable_rendering_for_tests();
313 });
314 }
315
316 pub(crate) fn lock_plot_registry() -> state::PlotTestLockGuard {
317 state::lock_plot_test_registry()
318 }
319}