1use egui::Color32;
5
6use super::super::animation::LegendHoverConfig;
7use super::super::chart_config::DirectionArrowConfig;
8use super::super::config::{AxisConfig, PlotColors, TitleAlign};
9use super::super::series::{LineStyle, Marker};
10use super::super::types::LegendPosition;
11use super::camera::Projection;
12
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14#[derive(Clone, Debug, Default, PartialEq)]
16pub struct Series3DStyle {
17 pub name: String,
19 pub color: Option<Color32>,
21 pub width: Option<f32>,
23 pub line_style: Option<LineStyle>,
25 pub marker: Option<Marker>,
27 pub marker_size: Option<f32>,
29 pub fill_opacity: Option<f32>,
31 pub direction_arrow: Option<bool>,
33}
34
35impl Series3DStyle {
36 pub fn is_empty(&self) -> bool {
38 self.color.is_none()
39 && self.width.is_none()
40 && self.line_style.is_none()
41 && self.marker.is_none()
42 && self.marker_size.is_none()
43 && self.fill_opacity.is_none()
44 && self.direction_arrow.is_none()
45 }
46}
47
48#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
49#[derive(Clone, Copy, Debug, PartialEq)]
51pub struct Surface3DConfig {
52 pub wireframe: bool,
54 pub opacity: f32,
56 pub color_scale: bool,
58}
59
60impl Default for Surface3DConfig {
61 fn default() -> Self {
62 Self {
63 wireframe: true,
64 opacity: 0.92,
65 color_scale: true,
66 }
67 }
68}
69
70#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
72#[derive(Clone, Debug, PartialEq)]
73pub struct Plot3DConfig {
74 pub title: Option<String>,
76 pub x: AxisConfig,
78 pub y: AxisConfig,
80 pub z: AxisConfig,
82 pub title_size: f32,
84 pub title_color: Option<Color32>,
86 pub title_align: TitleAlign,
88 pub label_size: f32,
90 pub tick_size: f32,
92 pub legend_size: f32,
94 pub frame_width: f32,
96 pub grid_width: f32,
98 pub grid_major_opacity: f32,
100 pub grid_minor_opacity: f32,
102 pub tick_length: f32,
104 pub line_scale: f32,
106 pub marker_scale: f32,
108 pub legend: LegendPosition,
110 pub legend_frame: bool,
112 pub toolbar: bool,
114 pub hover_guides: bool,
116 pub hover_popup: bool,
118 pub legend_hover: LegendHoverConfig,
120 pub direction: DirectionArrowConfig,
122 pub surface: Surface3DConfig,
124 pub projection: Projection,
126 pub yaw: f32,
128 pub pitch: f32,
130 pub fov: f32,
132 pub colors: PlotColors,
134 pub series: Vec<Series3DStyle>,
136 pub renderer: super::super::Renderer,
138}
139
140impl Default for Plot3DConfig {
141 fn default() -> Self {
142 Self {
143 title: None,
144 x: AxisConfig::default(),
145 y: AxisConfig::default(),
146 z: AxisConfig::default(),
147 title_size: 14.0,
148 title_color: None,
149 title_align: TitleAlign::Center,
150 label_size: 12.0,
151 tick_size: 11.0,
152 legend_size: 11.5,
153 frame_width: 1.0,
154 grid_width: 1.0,
155 grid_major_opacity: 0.5,
156 grid_minor_opacity: 0.18,
157 tick_length: 4.0,
158 line_scale: 1.0,
159 marker_scale: 1.0,
160 legend: LegendPosition::TopLeft,
161 legend_frame: true,
162 toolbar: true,
163 hover_guides: true,
164 hover_popup: true,
165 legend_hover: LegendHoverConfig::default(),
166 direction: DirectionArrowConfig::default(),
167 surface: Surface3DConfig::default(),
168 projection: Projection::Perspective,
169 yaw: -0.72,
170 pitch: 0.42,
171 fov: 45.0,
172 colors: PlotColors::default(),
173 series: Vec::new(),
174 renderer: super::super::Renderer::Cpu,
175 }
176 }
177}
178
179impl Plot3DConfig {
180 pub fn series_style(&self, name: &str) -> Option<&Series3DStyle> {
182 self.series.iter().find(|style| style.name == name)
183 }
184
185 pub fn series_style_mut(&mut self, name: &str) -> &mut Series3DStyle {
187 if let Some(index) = self.series.iter().position(|style| style.name == name) {
188 return &mut self.series[index];
189 }
190 self.series.push(Series3DStyle {
191 name: name.to_owned(),
192 ..Series3DStyle::default()
193 });
194 let last = self.series.len() - 1;
195 &mut self.series[last]
196 }
197
198 pub fn sanitize(&mut self) {
200 self.title_size = self.title_size.clamp(6.0, 48.0);
201 self.label_size = self.label_size.clamp(6.0, 36.0);
202 self.tick_size = self.tick_size.clamp(6.0, 30.0);
203 self.legend_size = self.legend_size.clamp(6.0, 30.0);
204 self.frame_width = self.frame_width.clamp(0.0, 6.0);
205 self.grid_width = self.grid_width.clamp(0.5, 6.0);
206 self.grid_major_opacity = self.grid_major_opacity.clamp(0.0, 1.0);
207 self.grid_minor_opacity = self.grid_minor_opacity.clamp(0.0, 1.0);
208 self.tick_length = self.tick_length.clamp(0.0, 16.0);
209 self.line_scale = self.line_scale.clamp(0.25, 8.0);
210 self.marker_scale = self.marker_scale.clamp(0.25, 8.0);
211 self.legend_hover.period = self.legend_hover.period.clamp(0.2, 10.0);
212 self.direction.points = self.direction.points.clamp(2, 64);
213 self.direction.length = self.direction.length.clamp(4.0, 160.0);
214 self.direction.width = self.direction.width.clamp(0.5, 8.0);
215 self.direction.head = self.direction.head.clamp(2.0, 40.0);
216 self.surface.opacity = self.surface.opacity.clamp(0.0, 1.0);
217 if !self.yaw.is_finite() {
218 self.yaw = Plot3DConfig::default().yaw;
219 }
220 if !self.pitch.is_finite() {
221 self.pitch = Plot3DConfig::default().pitch;
222 }
223 self.pitch = self.pitch.clamp(-1.5, 1.5);
224 self.fov = if self.fov.is_finite() {
225 self.fov.clamp(15.0, 120.0)
226 } else {
227 45.0
228 };
229 for axis in [&mut self.x, &mut self.y, &mut self.z] {
230 axis.label_size = axis.label_size.map(|size| size.clamp(6.0, 36.0));
231 if let (Some(min), Some(max)) = (axis.min, axis.max)
232 && min >= max
233 {
234 axis.max = None;
235 }
236 if axis.scale == super::super::axis::Scale::Log10 {
237 axis.min = axis.min.filter(|min| *min > 0.0);
238 axis.max = axis.max.filter(|max| *max > 0.0);
239 }
240 axis.major_step = axis.major_step.filter(|step| step.is_finite() && *step > 0.0);
241 if let super::super::config::Minor::Divisions(divisions) = axis.minor {
242 axis.minor = super::super::config::Minor::Divisions(divisions.clamp(2, 20));
243 }
244 axis.decimals = axis.decimals.map(|decimals| decimals.min(10));
245 }
246 for style in &mut self.series {
247 style.width = style.width.map(|width| width.clamp(0.25, 12.0));
248 style.marker_size = style.marker_size.map(|size| size.clamp(0.5, 20.0));
249 style.fill_opacity = style.fill_opacity.map(|opacity| opacity.clamp(0.0, 1.0));
250 }
251 }
252}