Skip to main content

polyscope_structures/volume_mesh/
vector_quantity.rs

1//! Vector quantities for volume meshes.
2
3use glam::{Vec3, Vec4};
4use polyscope_core::quantity::{CellQuantity, Quantity, QuantityKind, VertexQuantity};
5
6/// A vector quantity defined at mesh vertices.
7pub struct VolumeMeshVertexVectorQuantity {
8    name: String,
9    structure_name: String,
10    vectors: Vec<Vec3>,
11    enabled: bool,
12    vector_length_scale: f32,
13    vector_radius: f32,
14    vector_color: Vec4,
15}
16
17impl VolumeMeshVertexVectorQuantity {
18    pub fn new(
19        name: impl Into<String>,
20        structure_name: impl Into<String>,
21        vectors: Vec<Vec3>,
22    ) -> Self {
23        Self {
24            name: name.into(),
25            structure_name: structure_name.into(),
26            vectors,
27            enabled: false,
28            vector_length_scale: 1.0,
29            vector_radius: 0.01,
30            vector_color: Vec4::new(0.1, 0.1, 0.8, 1.0),
31        }
32    }
33
34    #[must_use]
35    pub fn vectors(&self) -> &[Vec3] {
36        &self.vectors
37    }
38
39    pub fn set_length_scale(&mut self, scale: f32) -> &mut Self {
40        self.vector_length_scale = scale;
41        self
42    }
43
44    pub fn set_radius(&mut self, radius: f32) -> &mut Self {
45        self.vector_radius = radius;
46        self
47    }
48
49    pub fn set_color(&mut self, color: Vec3) -> &mut Self {
50        self.vector_color = color.extend(1.0);
51        self
52    }
53
54    pub fn build_egui_ui(&mut self, ui: &mut egui::Ui) {
55        ui.horizontal(|ui| {
56            let mut enabled = self.enabled;
57            if ui.checkbox(&mut enabled, "").changed() {
58                self.enabled = enabled;
59            }
60            ui.label(&self.name);
61            ui.label("(vertex vector)");
62        });
63
64        if self.enabled {
65            ui.horizontal(|ui| {
66                ui.label("Length:");
67                ui.add(
68                    egui::DragValue::new(&mut self.vector_length_scale)
69                        .speed(0.01)
70                        .range(0.001..=10.0),
71                );
72            });
73        }
74    }
75}
76
77impl Quantity for VolumeMeshVertexVectorQuantity {
78    fn name(&self) -> &str {
79        &self.name
80    }
81    fn structure_name(&self) -> &str {
82        &self.structure_name
83    }
84    fn kind(&self) -> QuantityKind {
85        QuantityKind::Vector
86    }
87    fn is_enabled(&self) -> bool {
88        self.enabled
89    }
90    fn set_enabled(&mut self, enabled: bool) {
91        self.enabled = enabled;
92    }
93    fn data_size(&self) -> usize {
94        self.vectors.len()
95    }
96    fn build_ui(&mut self, _ui: &dyn std::any::Any) {}
97    fn refresh(&mut self) {}
98    fn as_any(&self) -> &dyn std::any::Any {
99        self
100    }
101    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
102        self
103    }
104}
105
106impl VertexQuantity for VolumeMeshVertexVectorQuantity {}
107
108/// A vector quantity defined at mesh cells.
109pub struct VolumeMeshCellVectorQuantity {
110    name: String,
111    structure_name: String,
112    vectors: Vec<Vec3>,
113    enabled: bool,
114    vector_length_scale: f32,
115    vector_radius: f32,
116    vector_color: Vec4,
117}
118
119impl VolumeMeshCellVectorQuantity {
120    pub fn new(
121        name: impl Into<String>,
122        structure_name: impl Into<String>,
123        vectors: Vec<Vec3>,
124    ) -> Self {
125        Self {
126            name: name.into(),
127            structure_name: structure_name.into(),
128            vectors,
129            enabled: false,
130            vector_length_scale: 1.0,
131            vector_radius: 0.01,
132            vector_color: Vec4::new(0.1, 0.1, 0.8, 1.0),
133        }
134    }
135
136    #[must_use]
137    pub fn vectors(&self) -> &[Vec3] {
138        &self.vectors
139    }
140
141    pub fn set_length_scale(&mut self, scale: f32) -> &mut Self {
142        self.vector_length_scale = scale;
143        self
144    }
145
146    pub fn set_radius(&mut self, radius: f32) -> &mut Self {
147        self.vector_radius = radius;
148        self
149    }
150
151    pub fn set_color(&mut self, color: Vec3) -> &mut Self {
152        self.vector_color = color.extend(1.0);
153        self
154    }
155
156    pub fn build_egui_ui(&mut self, ui: &mut egui::Ui) {
157        ui.horizontal(|ui| {
158            let mut enabled = self.enabled;
159            if ui.checkbox(&mut enabled, "").changed() {
160                self.enabled = enabled;
161            }
162            ui.label(&self.name);
163            ui.label("(cell vector)");
164        });
165
166        if self.enabled {
167            ui.horizontal(|ui| {
168                ui.label("Length:");
169                ui.add(
170                    egui::DragValue::new(&mut self.vector_length_scale)
171                        .speed(0.01)
172                        .range(0.001..=10.0),
173                );
174            });
175        }
176    }
177}
178
179impl Quantity for VolumeMeshCellVectorQuantity {
180    fn name(&self) -> &str {
181        &self.name
182    }
183    fn structure_name(&self) -> &str {
184        &self.structure_name
185    }
186    fn kind(&self) -> QuantityKind {
187        QuantityKind::Vector
188    }
189    fn is_enabled(&self) -> bool {
190        self.enabled
191    }
192    fn set_enabled(&mut self, enabled: bool) {
193        self.enabled = enabled;
194    }
195    fn data_size(&self) -> usize {
196        self.vectors.len()
197    }
198    fn build_ui(&mut self, _ui: &dyn std::any::Any) {}
199    fn refresh(&mut self) {}
200    fn as_any(&self) -> &dyn std::any::Any {
201        self
202    }
203    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
204        self
205    }
206}
207
208impl CellQuantity for VolumeMeshCellVectorQuantity {}