1pub mod image;
15pub mod line;
16pub mod mesh;
17pub mod quads;
18pub mod scatter;
19pub mod surface;
20pub mod triangles;
21
22pub use image::*;
23pub use line::*;
24pub use mesh::*;
25pub use quads::*;
26pub use scatter::*;
27pub use surface::*;
28pub use triangles::*;
29
30#[derive(Debug, Clone, PartialEq)]
32pub enum Plot3DError {
33 EmptyData,
34 DataLengthMismatch {
35 a: usize,
36 b: usize,
37 what: &'static str,
38 },
39 NotMultipleOf {
40 len: usize,
41 k: usize,
42 what: &'static str,
43 },
44 NativeCountOutOfRange {
45 what: &'static str,
46 },
47 MeshIndexOutOfBounds {
48 index: u32,
49 vertex_count: usize,
50 },
51 GridSizeMismatch {
52 x_count: usize,
53 y_count: usize,
54 expected: usize,
55 z_len: usize,
56 },
57 GridSizeOverflow {
58 x_count: usize,
59 y_count: usize,
60 },
61 GridPointCountOutOfRange {
62 x_count: usize,
63 y_count: usize,
64 point_count: usize,
65 },
66 InvalidSurfaceScale,
67 NonFiniteSurfaceHeight {
68 index: usize,
69 },
70 StringConversion(&'static str),
71}
72
73impl std::fmt::Display for Plot3DError {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 match self {
76 Plot3DError::EmptyData => write!(f, "data is empty"),
77 Plot3DError::DataLengthMismatch { a, b, what } => {
78 write!(f, "length mismatch for {}: {} vs {}", what, a, b)
79 }
80 Plot3DError::NotMultipleOf { len, k, what } => {
81 write!(f, "length of {} = {} is not multiple of {}", what, len, k)
82 }
83 Plot3DError::NativeCountOutOfRange { what } => {
84 write!(f, "{what} exceeded ImPlot3D's i32 range")
85 }
86 Plot3DError::MeshIndexOutOfBounds {
87 index,
88 vertex_count,
89 } => {
90 write!(
91 f,
92 "mesh index {index} is out of bounds for {vertex_count} vertices"
93 )
94 }
95 Plot3DError::GridSizeMismatch {
96 x_count,
97 y_count,
98 expected,
99 z_len,
100 } => write!(
101 f,
102 "grid mismatch: x={} y={} => expected z_len={}, got {}",
103 x_count, y_count, expected, z_len
104 ),
105 Plot3DError::GridSizeOverflow { x_count, y_count } => write!(
106 f,
107 "surface grid size overflow: x_count={} y_count={}",
108 x_count, y_count
109 ),
110 Plot3DError::GridPointCountOutOfRange {
111 x_count,
112 y_count,
113 point_count,
114 } => write!(
115 f,
116 "surface grid x_count={} y_count={} has {} points, exceeding ImPlot3D's i32 range",
117 x_count, y_count, point_count
118 ),
119 Plot3DError::InvalidSurfaceScale => f.write_str(
120 "surface color scale endpoints must be finite or both NaN for automatic scaling",
121 ),
122 Plot3DError::NonFiniteSurfaceHeight { index } => {
123 write!(f, "surface height at sample {index} must be finite")
124 }
125 Plot3DError::StringConversion(what) => write!(f, "string conversion error: {}", what),
126 }
127 }
128}
129
130impl std::error::Error for Plot3DError {}
131
132pub trait Plot3D {
134 fn label(&self) -> &str;
135 fn try_plot(&self, ui: &crate::Plot3DScope<'_>) -> Result<(), Plot3DError>;
136 fn plot(&self, ui: &crate::Plot3DScope<'_>) {
137 let _ = self.try_plot(ui);
138 }
139}
140
141#[inline]
142pub(crate) fn validate_nonempty<T>(a: &[T]) -> Result<(), Plot3DError> {
143 if a.is_empty() {
144 Err(Plot3DError::EmptyData)
145 } else {
146 Ok(())
147 }
148}
149
150#[inline]
151pub(crate) fn validate_lengths<T, U>(
152 a: &[T],
153 b: &[U],
154 what: &'static str,
155) -> Result<(), Plot3DError> {
156 if a.len() != b.len() {
157 Err(Plot3DError::DataLengthMismatch {
158 a: a.len(),
159 b: b.len(),
160 what,
161 })
162 } else {
163 Ok(())
164 }
165}
166
167#[inline]
168pub(crate) fn primitive_vertex_count(
169 len: usize,
170 k: usize,
171 what: &'static str,
172 layout: crate::Plot3DDataLayout,
173 draw_lines: bool,
174) -> Result<i32, Plot3DError> {
175 if len == 0 {
176 return Err(Plot3DError::EmptyData);
177 }
178 if !len.is_multiple_of(k) {
179 return Err(Plot3DError::NotMultipleOf { len, k, what });
180 }
181 if draw_lines && len > i32::MAX as usize / 2 {
183 return Err(Plot3DError::NativeCountOutOfRange { what });
184 }
185 layout
186 .sample_count(len)
187 .ok_or(Plot3DError::NativeCountOutOfRange { what })
188}
189
190#[cfg(test)]
191mod tests {
192 use super::*;
193 use crate::Plot3DDataLayout;
194
195 #[test]
196 fn complete_faces_fit_the_selected_native_renderer() {
197 let layout = Plot3DDataLayout::DEFAULT;
198 assert!(matches!(
199 primitive_vertex_count(4, 3, "triangles", layout, true),
200 Err(Plot3DError::NotMultipleOf { .. })
201 ));
202 let count = (i32::MAX as usize / 2 + 1).next_multiple_of(3);
203 assert!(primitive_vertex_count(count, 3, "triangles", layout, true).is_err());
204 assert_eq!(
205 primitive_vertex_count(count, 3, "triangles", layout, false),
206 Ok(count as i32)
207 );
208 }
209}