Skip to main content

polyscope_structures/volume_mesh/
color_quantity.rs

1//! Color quantities for volume meshes.
2
3use glam::{Vec3, Vec4};
4use polyscope_core::quantity::{CellQuantity, Quantity, QuantityKind, VertexQuantity};
5
6/// A color quantity defined at mesh vertices.
7pub struct VolumeMeshVertexColorQuantity {
8    name: String,
9    structure_name: String,
10    colors: Vec<Vec4>,
11    enabled: bool,
12}
13
14impl VolumeMeshVertexColorQuantity {
15    pub fn new(
16        name: impl Into<String>,
17        structure_name: impl Into<String>,
18        colors: Vec<Vec3>,
19    ) -> Self {
20        Self {
21            name: name.into(),
22            structure_name: structure_name.into(),
23            colors: colors.into_iter().map(|c| c.extend(1.0)).collect(),
24            enabled: false,
25        }
26    }
27
28    #[must_use]
29    pub fn colors(&self) -> &[Vec4] {
30        &self.colors
31    }
32
33    pub fn build_egui_ui(&mut self, ui: &mut egui::Ui) {
34        ui.horizontal(|ui| {
35            let mut enabled = self.enabled;
36            if ui.checkbox(&mut enabled, "").changed() {
37                self.enabled = enabled;
38            }
39            ui.label(&self.name);
40            ui.label("(vertex color)");
41        });
42    }
43}
44
45impl Quantity for VolumeMeshVertexColorQuantity {
46    fn name(&self) -> &str {
47        &self.name
48    }
49    fn structure_name(&self) -> &str {
50        &self.structure_name
51    }
52    fn kind(&self) -> QuantityKind {
53        QuantityKind::Color
54    }
55    fn is_enabled(&self) -> bool {
56        self.enabled
57    }
58    fn set_enabled(&mut self, enabled: bool) {
59        self.enabled = enabled;
60    }
61    fn data_size(&self) -> usize {
62        self.colors.len()
63    }
64    fn build_ui(&mut self, _ui: &dyn std::any::Any) {}
65    fn refresh(&mut self) {}
66    fn as_any(&self) -> &dyn std::any::Any {
67        self
68    }
69    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
70        self
71    }
72}
73
74impl VertexQuantity for VolumeMeshVertexColorQuantity {}
75
76/// A color quantity defined at mesh cells.
77pub struct VolumeMeshCellColorQuantity {
78    name: String,
79    structure_name: String,
80    colors: Vec<Vec4>,
81    enabled: bool,
82}
83
84impl VolumeMeshCellColorQuantity {
85    pub fn new(
86        name: impl Into<String>,
87        structure_name: impl Into<String>,
88        colors: Vec<Vec3>,
89    ) -> Self {
90        Self {
91            name: name.into(),
92            structure_name: structure_name.into(),
93            colors: colors.into_iter().map(|c| c.extend(1.0)).collect(),
94            enabled: false,
95        }
96    }
97
98    #[must_use]
99    pub fn colors(&self) -> &[Vec4] {
100        &self.colors
101    }
102
103    pub fn build_egui_ui(&mut self, ui: &mut egui::Ui) {
104        ui.horizontal(|ui| {
105            let mut enabled = self.enabled;
106            if ui.checkbox(&mut enabled, "").changed() {
107                self.enabled = enabled;
108            }
109            ui.label(&self.name);
110            ui.label("(cell color)");
111        });
112    }
113}
114
115impl Quantity for VolumeMeshCellColorQuantity {
116    fn name(&self) -> &str {
117        &self.name
118    }
119    fn structure_name(&self) -> &str {
120        &self.structure_name
121    }
122    fn kind(&self) -> QuantityKind {
123        QuantityKind::Color
124    }
125    fn is_enabled(&self) -> bool {
126        self.enabled
127    }
128    fn set_enabled(&mut self, enabled: bool) {
129        self.enabled = enabled;
130    }
131    fn data_size(&self) -> usize {
132        self.colors.len()
133    }
134    fn build_ui(&mut self, _ui: &dyn std::any::Any) {}
135    fn refresh(&mut self) {}
136    fn as_any(&self) -> &dyn std::any::Any {
137        self
138    }
139    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
140        self
141    }
142}
143
144impl CellQuantity for VolumeMeshCellColorQuantity {}