Skip to main content

runmat_runtime/builtins/plotting/
mod.rs

1//! Plotting builtins backed by the runmat-plot renderer.
2
3#[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/contourf.rs"]
44pub(crate) mod contourf;
45#[path = "ops/drawnow.rs"]
46pub(crate) mod drawnow;
47#[path = "ops/errorbar.rs"]
48pub(crate) mod errorbar;
49#[path = "ops/figure.rs"]
50pub(crate) mod figure;
51#[path = "ops/gca.rs"]
52pub(crate) mod gca;
53#[path = "ops/gcf.rs"]
54pub(crate) mod gcf;
55#[path = "ops/get.rs"]
56pub(crate) mod get;
57#[path = "ops/heatmap.rs"]
58pub(crate) mod heatmap;
59#[path = "ops/hist.rs"]
60pub mod hist;
61#[path = "ops/histogram.rs"]
62pub(crate) mod histogram;
63#[path = "ops/hold.rs"]
64pub(crate) mod hold;
65#[path = "ops/image.rs"]
66pub(crate) mod image;
67#[path = "ops/imagesc.rs"]
68pub(crate) mod imagesc;
69#[path = "ops/imshow.rs"]
70pub(crate) mod imshow;
71#[path = "ops/isgraphics.rs"]
72pub(crate) mod isgraphics;
73#[path = "ops/ishandle.rs"]
74pub(crate) mod ishandle;
75#[path = "ops/legend.rs"]
76pub(crate) mod legend;
77#[path = "ops/loglog.rs"]
78pub(crate) mod loglog;
79#[path = "ops/mesh.rs"]
80pub(crate) mod mesh;
81#[path = "ops/meshc.rs"]
82pub(crate) mod meshc;
83#[path = "ops/common/mod.rs"]
84pub(crate) mod op_common;
85#[path = "ops/pie.rs"]
86pub(crate) mod pie;
87#[path = "ops/plot.rs"]
88pub(crate) mod plot;
89#[path = "ops/plot3.rs"]
90pub(crate) mod plot3;
91#[path = "ops/quiver.rs"]
92pub(crate) mod quiver;
93#[path = "ops/scatter.rs"]
94pub(crate) mod scatter;
95#[path = "ops/scatter3.rs"]
96pub(crate) mod scatter3;
97#[path = "ops/semilogx.rs"]
98pub(crate) mod semilogx;
99#[path = "ops/semilogy.rs"]
100pub(crate) mod semilogy;
101#[path = "ops/set.rs"]
102pub(crate) mod set;
103#[path = "ops/sgtitle.rs"]
104pub(crate) mod sgtitle;
105#[path = "ops/stairs.rs"]
106pub(crate) mod stairs;
107#[path = "ops/stem.rs"]
108pub(crate) mod stem;
109#[path = "ops/subplot.rs"]
110pub(crate) mod subplot;
111#[path = "ops/surf.rs"]
112pub(crate) mod surf;
113#[path = "ops/surfc.rs"]
114pub(crate) mod surfc;
115#[path = "ops/text.rs"]
116pub(crate) mod text;
117#[path = "ops/title.rs"]
118pub(crate) mod title;
119#[path = "ops/view.rs"]
120pub(crate) mod view;
121#[path = "ops/xlabel.rs"]
122pub(crate) mod xlabel;
123#[path = "ops/xlim.rs"]
124pub(crate) mod xlim;
125#[path = "ops/ylabel.rs"]
126pub(crate) mod ylabel;
127#[path = "ops/ylim.rs"]
128pub(crate) mod ylim;
129#[path = "ops/zlabel.rs"]
130pub(crate) mod zlabel;
131#[path = "ops/zlim.rs"]
132pub(crate) mod zlim;
133
134pub use perf::{set_scatter_target_points, set_surface_vertex_budget};
135pub use properties::resolve_plot_handle;
136pub use state::{
137    clear_figure, clone_figure, close_figure, configure_subplot, current_axes_state,
138    current_figure_handle, figure_handles, import_figure, install_figure_observer,
139    new_figure_handle, reset_hold_state_for_run, reset_plot_state, reset_recent_figures,
140    select_axes_for_figure, select_figure, set_hold, take_recent_figures, FigureAxesState,
141    FigureError, FigureEventKind, FigureEventView, FigureHandle, HoldMode,
142};
143use std::collections::HashMap;
144use std::sync::{Mutex, OnceLock};
145use web::present_figure_on_surface as web_present_figure_on_surface;
146pub use web::{
147    bind_surface_to_figure, clear_closed_figure_surfaces, detach_surface, fit_surface_extents,
148    get_surface_camera_state, install_surface, invalidate_surface_revisions, present_surface,
149    render_current_scene, reset_surface_camera, resize_surface, set_plot_theme_config,
150    set_surface_camera_state, web_renderer_ready, PlotCameraProjection, PlotCameraState,
151    PlotSurfaceCameraState,
152};
153
154#[cfg(all(target_arch = "wasm32", feature = "plot-web"))]
155pub use web::handle_plot_surface_event;
156
157pub(crate) fn plotting_error(builtin: &str, message: impl Into<String>) -> crate::RuntimeError {
158    crate::build_runtime_error(message)
159        .with_builtin(builtin)
160        .build()
161}
162
163pub(crate) fn plotting_error_with_source(
164    builtin: &str,
165    message: impl Into<String>,
166    source: impl std::error::Error + Send + Sync + 'static,
167) -> crate::RuntimeError {
168    crate::build_runtime_error(message)
169        .with_builtin(builtin)
170        .with_source(source)
171        .build()
172}
173
174#[cfg(feature = "plot-core")]
175pub fn export_figure_scene(handle: FigureHandle) -> crate::BuiltinResult<Option<Vec<u8>>> {
176    let Some(figure) = clone_figure(handle) else {
177        return Ok(None);
178    };
179    let scene = runmat_plot::event::FigureScene::capture(&figure);
180    crate::replay::export_figure_scene_payload(&scene).map(Some)
181}
182
183#[cfg(feature = "plot-core")]
184pub fn import_figure_scene(bytes: &[u8]) -> crate::BuiltinResult<Option<FigureHandle>> {
185    let scene = crate::replay::import_figure_scene_payload(bytes)?;
186    let figure = scene.into_figure().map_err(|err| {
187        crate::replay_error_with_source(
188            crate::ReplayErrorKind::ImportRejected,
189            "invalid figure scene content",
190            std::io::Error::new(std::io::ErrorKind::InvalidData, err),
191        )
192    })?;
193    let handle = import_figure(figure);
194    register_imported_figure(handle.as_u32());
195    Ok(Some(handle))
196}
197
198#[cfg(feature = "plot-core")]
199pub async fn import_figure_scene_async(bytes: &[u8]) -> crate::BuiltinResult<Option<FigureHandle>> {
200    let scene = crate::replay::import_figure_scene_payload_async(bytes).await?;
201    let figure = scene.into_figure().map_err(|err| {
202        crate::replay_error_with_source(
203            crate::ReplayErrorKind::ImportRejected,
204            "invalid figure scene content",
205            std::io::Error::new(std::io::ErrorKind::InvalidData, err),
206        )
207    })?;
208    let handle = import_figure(figure);
209    register_imported_figure(handle.as_u32());
210    Ok(Some(handle))
211}
212
213#[cfg(feature = "plot-core")]
214pub async fn import_figure_scene_from_path_async(
215    path: &str,
216) -> crate::BuiltinResult<Option<FigureHandle>> {
217    let bytes = runmat_filesystem::read_async(path).await.map_err(|err| {
218        crate::replay_error_with_source(
219            crate::ReplayErrorKind::ImportRejected,
220            format!("failed to read figure scene payload '{path}'"),
221            err,
222        )
223    })?;
224    import_figure_scene_async(&bytes).await
225}
226
227pub fn present_figure_on_surface(surface_id: u32, handle: u32) -> crate::BuiltinResult<()> {
228    web_present_figure_on_surface(surface_id, handle)?;
229    if take_imported_figure(handle) {
230        let _ = reset_surface_camera(surface_id);
231    }
232    Ok(())
233}
234
235type ImportedFigureRegistry = Mutex<HashMap<u32, ()>>;
236
237fn imported_figure_registry() -> &'static ImportedFigureRegistry {
238    static REGISTRY: OnceLock<ImportedFigureRegistry> = OnceLock::new();
239    REGISTRY.get_or_init(|| Mutex::new(HashMap::new()))
240}
241
242fn register_imported_figure(handle: u32) {
243    if let Ok(mut map) = imported_figure_registry().lock() {
244        map.insert(handle, ());
245    }
246}
247
248fn take_imported_figure(handle: u32) -> bool {
249    imported_figure_registry()
250        .lock()
251        .ok()
252        .and_then(|mut map| map.remove(&handle))
253        .is_some()
254}
255
256#[cfg(feature = "plot-core")]
257pub use engine::{
258    render_figure_png_bytes, render_figure_png_bytes_with_axes_cameras,
259    render_figure_png_bytes_with_camera, render_figure_rgba_bytes,
260    render_figure_rgba_bytes_with_axes_cameras, render_figure_rgba_bytes_with_camera,
261    render_figure_snapshot, render_figure_snapshot_with_camera_state,
262};
263
264pub mod ops {
265    pub use super::hist;
266}
267
268#[cfg(test)]
269pub(crate) mod tests {
270    use super::state;
271    use std::sync::Once;
272
273    pub(crate) fn ensure_plot_test_env() {
274        static INIT: Once = Once::new();
275        INIT.call_once(|| {
276            state::disable_rendering_for_tests();
277        });
278    }
279
280    pub(crate) fn lock_plot_registry() -> state::PlotTestLockGuard {
281        state::lock_plot_test_registry()
282    }
283}