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