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