Skip to main content

dear_implot3d/
surface_builder.rs

1use std::borrow::Cow;
2
3use crate::item_style::{Plot3DItemStyle, plot3d_spec_with_style};
4use crate::plots::{
5    Plot3DError, SurfaceGrid, SurfaceGridShape, SurfaceLabel, submit_surface_grid,
6    submit_surface_raw,
7};
8use crate::{Item3DFlags, Plot3DDataLayout, Plot3DScope, Surface3DFlags};
9
10/// Surface (grid) plot builder (f32 variant).
11pub struct Surface3DBuilder<'ui> {
12    pub(crate) _ui: &'ui Plot3DScope<'ui>,
13    pub(crate) label: Cow<'ui, str>,
14    pub(crate) xs: &'ui [f32],
15    pub(crate) ys: &'ui [f32],
16    pub(crate) zs: &'ui [f32],
17    pub(crate) scale_min: f64,
18    pub(crate) scale_max: f64,
19    pub(crate) flags: Surface3DFlags,
20    pub(crate) item_flags: Item3DFlags,
21    pub(crate) style: Plot3DItemStyle,
22}
23
24impl<'ui> Surface3DBuilder<'ui> {
25    /// Set the color range. Two zeros (the default) or two NaNs select the grid's Z range.
26    pub fn scale(mut self, min: f64, max: f64) -> Self {
27        self.scale_min = min;
28        self.scale_max = max;
29        self
30    }
31
32    pub fn flags(mut self, flags: Surface3DFlags) -> Self {
33        self.flags = flags;
34        self
35    }
36
37    /// Submit the surface after validating its complete grid shape.
38    #[must_use = "surface plot errors must be handled"]
39    pub fn plot(self) -> Result<(), Plot3DError> {
40        let Surface3DBuilder {
41            _ui,
42            label,
43            xs,
44            ys,
45            zs,
46            scale_min,
47            scale_max,
48            flags,
49            item_flags,
50            style,
51        } = self;
52
53        let label = SurfaceLabel::checked(label.as_ref())?;
54        let grid = SurfaceGrid::from_axes(xs, ys, zs, flags)?;
55        submit_surface_grid(_ui, label, &grid, scale_min, scale_max, || {
56            plot3d_spec_with_style(
57                &_ui.next_item,
58                style,
59                flags.bits() | item_flags.bits(),
60                Plot3DDataLayout::DEFAULT,
61            )
62        })
63    }
64}
65
66impl<'ui> Plot3DScope<'ui> {
67    /// Start a surface plot (f32) from X/Y grid axes. Filled surfaces require finite Z samples.
68    pub fn surface_f32(
69        &'ui self,
70        label: impl Into<Cow<'ui, str>>,
71        xs: &'ui [f32],
72        ys: &'ui [f32],
73        zs: &'ui [f32],
74    ) -> Surface3DBuilder<'ui> {
75        Surface3DBuilder {
76            _ui: self,
77            label: label.into(),
78            xs,
79            ys,
80            zs,
81            scale_min: 0.0,
82            scale_max: 0.0,
83            flags: Surface3DFlags::NONE,
84            item_flags: Item3DFlags::NONE,
85            style: Plot3DItemStyle::default(),
86        }
87    }
88
89    /// Submit a surface from already flattened, contiguous per-vertex arrays.
90    ///
91    /// `xs_flat`, `ys_flat`, and `zs` must each contain exactly `x_count * y_count` values. The
92    /// contiguous path does not accept an offset or stride; use [`Self::surface_f32_raw`] when
93    /// the native layout is intentionally non-contiguous.
94    /// Filled surfaces require finite Z samples. Pass two zeros for automatic color scaling over their range.
95    ///
96    /// ```no_run
97    /// use dear_implot3d::{Plot3DError, Plot3DScope, Surface3DFlags};
98    ///
99    /// fn submit(plot_ui: &Plot3DScope<'_>) -> Result<(), Plot3DError> {
100    ///     let xs = [0.0, 1.0, 0.0, 1.0];
101    ///     let ys = [0.0, 0.0, 1.0, 1.0];
102    ///     let zs = [0.0, 1.0, 1.0, 2.0];
103    ///     plot_ui.surface_f32_flat(
104    ///         "surface",
105    ///         &xs,
106    ///         &ys,
107    ///         &zs,
108    ///         2,
109    ///         2,
110    ///         0.0,
111    ///         0.0,
112    ///         Surface3DFlags::NONE,
113    ///     )
114    /// }
115    /// ```
116    #[must_use = "surface plot errors must be handled"]
117    pub fn surface_f32_flat<S: AsRef<str>>(
118        &self,
119        label: S,
120        xs_flat: &[f32],
121        ys_flat: &[f32],
122        zs: &[f32],
123        x_count: usize,
124        y_count: usize,
125        scale_min: f64,
126        scale_max: f64,
127        flags: Surface3DFlags,
128    ) -> Result<(), Plot3DError> {
129        let label = SurfaceLabel::checked(label.as_ref())?;
130        let grid = SurfaceGrid::from_flattened(xs_flat, ys_flat, zs, x_count, y_count, flags)?;
131        submit_surface_grid(self, label, &grid, scale_min, scale_max, || {
132            self.next_item.spec(flags.bits(), Plot3DDataLayout::DEFAULT)
133        })
134    }
135
136    /// Submit a surface with an explicitly arbitrary native data layout.
137    /// Filled surfaces require finite Z samples.
138    ///
139    /// # Safety
140    ///
141    /// For each of the `x_count * y_count` samples, `layout` must address an initialized,
142    /// properly aligned, readable `f32` in each coordinate allocation for the duration of the
143    /// call. The slices need not be exactly `x_count * y_count` elements because a custom stride
144    /// may address a larger allocation.
145    ///
146    /// Calling this arbitrary-layout API without an `unsafe` block is rejected:
147    ///
148    /// ```compile_fail
149    /// use dear_implot3d::{Plot3DDataLayout, Plot3DScope, Surface3DFlags};
150    ///
151    /// fn submit(plot_ui: &Plot3DScope<'_>) {
152    ///     let values = [0.0; 4];
153    ///     let _ = plot_ui.surface_f32_raw(
154    ///         "surface",
155    ///         &values,
156    ///         &values,
157    ///         &values,
158    ///         2,
159    ///         2,
160    ///         0.0,
161    ///         0.0,
162    ///         Surface3DFlags::NONE,
163    ///         Plot3DDataLayout::DEFAULT,
164    ///     );
165    /// }
166    /// ```
167    #[must_use = "surface plot errors must be handled"]
168    pub unsafe fn surface_f32_raw<S: AsRef<str>>(
169        &self,
170        label: S,
171        xs: &[f32],
172        ys: &[f32],
173        zs: &[f32],
174        x_count: usize,
175        y_count: usize,
176        scale_min: f64,
177        scale_max: f64,
178        flags: Surface3DFlags,
179        layout: Plot3DDataLayout,
180    ) -> Result<(), Plot3DError> {
181        let label = SurfaceLabel::checked(label.as_ref())?;
182        let shape = SurfaceGridShape::checked(x_count, y_count)?.validate_layout(flags, layout)?;
183        unsafe {
184            submit_surface_raw(
185                self, label, xs, ys, zs, shape, scale_min, scale_max, flags, layout,
186            )
187        }
188    }
189}