Skip to main content

polyscope_structures/volume_mesh/
scalar_quantity.rs

1//! Scalar quantities for volume meshes.
2
3use polyscope_core::quantity::{CellQuantity, Quantity, QuantityKind, VertexQuantity};
4
5/// A scalar quantity defined at mesh vertices.
6pub struct VolumeMeshVertexScalarQuantity {
7    name: String,
8    structure_name: String,
9    values: Vec<f32>,
10    enabled: bool,
11    color_map: String,
12    data_min: f32,
13    data_max: f32,
14}
15
16impl VolumeMeshVertexScalarQuantity {
17    /// Creates a new vertex scalar quantity.
18    pub fn new(
19        name: impl Into<String>,
20        structure_name: impl Into<String>,
21        values: Vec<f32>,
22    ) -> Self {
23        let (data_min, data_max) = Self::compute_range(&values);
24        Self {
25            name: name.into(),
26            structure_name: structure_name.into(),
27            values,
28            enabled: false,
29            color_map: "viridis".to_string(),
30            data_min,
31            data_max,
32        }
33    }
34
35    fn compute_range(values: &[f32]) -> (f32, f32) {
36        let mut min = f32::MAX;
37        let mut max = f32::MIN;
38        for &v in values {
39            if v.is_finite() {
40                min = min.min(v);
41                max = max.max(v);
42            }
43        }
44        if min > max { (0.0, 1.0) } else { (min, max) }
45    }
46
47    /// Returns the values.
48    #[must_use]
49    pub fn values(&self) -> &[f32] {
50        &self.values
51    }
52
53    /// Gets the color map name.
54    #[must_use]
55    pub fn color_map(&self) -> &str {
56        &self.color_map
57    }
58
59    /// Sets the color map name.
60    pub fn set_color_map(&mut self, name: impl Into<String>) -> &mut Self {
61        self.color_map = name.into();
62        self
63    }
64
65    /// Gets the data range.
66    #[must_use]
67    pub fn data_range(&self) -> (f32, f32) {
68        (self.data_min, self.data_max)
69    }
70
71    /// Sets the data range.
72    pub fn set_data_range(&mut self, min: f32, max: f32) -> &mut Self {
73        self.data_min = min;
74        self.data_max = max;
75        self
76    }
77
78    /// Builds egui UI for this quantity.
79    pub fn build_egui_ui(&mut self, ui: &mut egui::Ui) {
80        ui.horizontal(|ui| {
81            let mut enabled = self.enabled;
82            if ui.checkbox(&mut enabled, "").changed() {
83                self.enabled = enabled;
84            }
85            ui.label(&self.name);
86            ui.label(format!("[{:.3}, {:.3}]", self.data_min, self.data_max));
87        });
88    }
89}
90
91impl Quantity for VolumeMeshVertexScalarQuantity {
92    fn name(&self) -> &str {
93        &self.name
94    }
95    fn structure_name(&self) -> &str {
96        &self.structure_name
97    }
98    fn kind(&self) -> QuantityKind {
99        QuantityKind::Scalar
100    }
101    fn is_enabled(&self) -> bool {
102        self.enabled
103    }
104    fn set_enabled(&mut self, enabled: bool) {
105        self.enabled = enabled;
106    }
107    fn data_size(&self) -> usize {
108        self.values.len()
109    }
110    fn build_ui(&mut self, _ui: &dyn std::any::Any) {}
111    fn refresh(&mut self) {}
112    fn as_any(&self) -> &dyn std::any::Any {
113        self
114    }
115    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
116        self
117    }
118}
119
120impl VertexQuantity for VolumeMeshVertexScalarQuantity {}
121
122/// A scalar quantity defined at mesh cells.
123pub struct VolumeMeshCellScalarQuantity {
124    name: String,
125    structure_name: String,
126    values: Vec<f32>,
127    enabled: bool,
128    color_map: String,
129    data_min: f32,
130    data_max: f32,
131}
132
133impl VolumeMeshCellScalarQuantity {
134    /// Creates a new cell scalar quantity.
135    pub fn new(
136        name: impl Into<String>,
137        structure_name: impl Into<String>,
138        values: Vec<f32>,
139    ) -> Self {
140        let (data_min, data_max) = Self::compute_range(&values);
141        Self {
142            name: name.into(),
143            structure_name: structure_name.into(),
144            values,
145            enabled: false,
146            color_map: "viridis".to_string(),
147            data_min,
148            data_max,
149        }
150    }
151
152    fn compute_range(values: &[f32]) -> (f32, f32) {
153        let mut min = f32::MAX;
154        let mut max = f32::MIN;
155        for &v in values {
156            if v.is_finite() {
157                min = min.min(v);
158                max = max.max(v);
159            }
160        }
161        if min > max { (0.0, 1.0) } else { (min, max) }
162    }
163
164    /// Returns the values.
165    #[must_use]
166    pub fn values(&self) -> &[f32] {
167        &self.values
168    }
169
170    /// Gets the color map name.
171    #[must_use]
172    pub fn color_map(&self) -> &str {
173        &self.color_map
174    }
175
176    /// Sets the color map name.
177    pub fn set_color_map(&mut self, name: impl Into<String>) -> &mut Self {
178        self.color_map = name.into();
179        self
180    }
181
182    /// Gets the data range.
183    #[must_use]
184    pub fn data_range(&self) -> (f32, f32) {
185        (self.data_min, self.data_max)
186    }
187
188    /// Sets the data range.
189    pub fn set_data_range(&mut self, min: f32, max: f32) -> &mut Self {
190        self.data_min = min;
191        self.data_max = max;
192        self
193    }
194
195    /// Builds egui UI for this quantity.
196    pub fn build_egui_ui(&mut self, ui: &mut egui::Ui) {
197        ui.horizontal(|ui| {
198            let mut enabled = self.enabled;
199            if ui.checkbox(&mut enabled, "").changed() {
200                self.enabled = enabled;
201            }
202            ui.label(&self.name);
203            ui.label(format!("[{:.3}, {:.3}]", self.data_min, self.data_max));
204        });
205    }
206}
207
208impl Quantity for VolumeMeshCellScalarQuantity {
209    fn name(&self) -> &str {
210        &self.name
211    }
212    fn structure_name(&self) -> &str {
213        &self.structure_name
214    }
215    fn kind(&self) -> QuantityKind {
216        QuantityKind::Scalar
217    }
218    fn is_enabled(&self) -> bool {
219        self.enabled
220    }
221    fn set_enabled(&mut self, enabled: bool) {
222        self.enabled = enabled;
223    }
224    fn data_size(&self) -> usize {
225        self.values.len()
226    }
227    fn build_ui(&mut self, _ui: &dyn std::any::Any) {}
228    fn refresh(&mut self) {}
229    fn as_any(&self) -> &dyn std::any::Any {
230        self
231    }
232    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
233        self
234    }
235}
236
237impl CellQuantity for VolumeMeshCellScalarQuantity {}