dear_implot3d/plots/
surface.rs1use super::{Plot3D, Plot3DError};
2use crate::{Plot3DDataLayout, Plot3DDataOffset, Plot3DDataStride, Plot3DUi, Surface3DFlags};
3
4pub struct Surface3D<'a> {
5 pub label: &'a str,
6 pub xs: &'a [f32],
7 pub ys: &'a [f32],
8 pub zs: &'a [f32],
9 pub scale_min: f64,
10 pub scale_max: f64,
11 pub flags: Surface3DFlags,
12 pub layout: Plot3DDataLayout,
13}
14
15impl<'a> Surface3D<'a> {
16 pub fn new(label: &'a str, xs: &'a [f32], ys: &'a [f32], zs: &'a [f32]) -> Self {
17 Self {
18 label,
19 xs,
20 ys,
21 zs,
22 scale_min: f64::NAN,
23 scale_max: f64::NAN,
24 flags: Surface3DFlags::NONE,
25 layout: Plot3DDataLayout::DEFAULT,
26 }
27 }
28 pub fn scale(mut self, min: f64, max: f64) -> Self {
29 self.scale_min = min;
30 self.scale_max = max;
31 self
32 }
33 pub fn flags(mut self, flags: Surface3DFlags) -> Self {
34 self.flags = flags;
35 self
36 }
37 pub fn data_layout(mut self, layout: Plot3DDataLayout) -> Self {
38 self.layout = layout;
39 self
40 }
41 pub fn offset(mut self, offset: Plot3DDataOffset) -> Self {
42 self.layout = self.layout.with_offset(offset);
43 self
44 }
45 pub fn stride(mut self, stride: Plot3DDataStride) -> Self {
46 self.layout = self.layout.with_stride(stride);
47 self
48 }
49}
50
51impl<'a> Plot3D for Surface3D<'a> {
52 fn label(&self) -> &str {
53 self.label
54 }
55 fn try_plot(&self, ui: &Plot3DUi<'_>) -> Result<(), Plot3DError> {
56 let x_count = self.xs.len();
57 let y_count = self.ys.len();
58 let expected = x_count
59 .checked_mul(y_count)
60 .ok_or(Plot3DError::GridSizeMismatch {
61 x_count,
62 y_count,
63 z_len: self.zs.len(),
64 })?;
65 if self.zs.len() != expected {
66 return Err(Plot3DError::GridSizeMismatch {
67 x_count,
68 y_count,
69 z_len: self.zs.len(),
70 });
71 }
72 ui.surface_f32_raw(
73 self.label,
74 self.xs,
75 self.ys,
76 self.zs,
77 self.scale_min,
78 self.scale_max,
79 self.flags,
80 self.layout,
81 );
82 Ok(())
83 }
84}