polyscope_core/quantity.rs
1//! Quantity trait and related types.
2//!
3//! A [`Quantity`] represents data associated with a structure, such as scalar values,
4//! vector fields, or colors.
5
6use std::any::Any;
7
8/// The kind of quantity (for categorization and UI).
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum QuantityKind {
11 /// Scalar values (single float per element).
12 Scalar,
13 /// Vector values (Vec3 per element).
14 Vector,
15 /// Color values (RGB or RGBA per element).
16 Color,
17 /// Parameterization values (UV coordinates).
18 Parameterization,
19 /// Other/custom quantity type.
20 Other,
21}
22
23/// Data associated with a structure that can be visualized.
24///
25/// Quantities are attached to structures and represent data like:
26/// - Scalar fields (temperature, pressure, etc.)
27/// - Vector fields (velocity, normals, etc.)
28/// - Colors
29/// - UV parameterizations
30pub trait Quantity: Any + Send + Sync {
31 /// Returns a reference to self as `Any` for downcasting.
32 fn as_any(&self) -> &dyn Any;
33
34 /// Returns a mutable reference to self as `Any` for downcasting.
35 fn as_any_mut(&mut self) -> &mut dyn Any;
36
37 /// Returns the name of this quantity.
38 fn name(&self) -> &str;
39
40 /// Returns the name of the parent structure.
41 fn structure_name(&self) -> &str;
42
43 /// Returns the kind of this quantity.
44 fn kind(&self) -> QuantityKind;
45
46 /// Returns whether this quantity is currently enabled/visible.
47 fn is_enabled(&self) -> bool;
48
49 /// Sets the enabled state of this quantity.
50 fn set_enabled(&mut self, enabled: bool);
51
52 /// Builds the `ImGui` UI controls for this quantity.
53 fn build_ui(&mut self, ui: &dyn std::any::Any);
54
55 /// Refreshes GPU resources after data changes.
56 fn refresh(&mut self);
57
58 /// Returns the number of data elements.
59 fn data_size(&self) -> usize;
60
61 /// Clears GPU render resources so they can be re-initialized with a new device.
62 fn clear_gpu_resources(&mut self) {
63 // Default no-op; quantity types with GPU resources override this
64 }
65}
66
67/// Marker trait for quantities defined on vertices.
68pub trait VertexQuantity: Quantity {}
69
70/// Marker trait for quantities defined on faces.
71pub trait FaceQuantity: Quantity {}
72
73/// Marker trait for quantities defined on edges.
74pub trait EdgeQuantity: Quantity {}
75
76/// Marker trait for quantities defined on cells (for volume meshes).
77pub trait CellQuantity: Quantity {}
78
79/// Visualization style for parameterization quantities.
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
81pub enum ParamVizStyle {
82 /// Two-color checker pattern over UV space.
83 #[default]
84 Checker,
85 /// Two-color grid lines over UV space.
86 Grid,
87 /// Checkerboard overlay on radial colormap centered at (0,0).
88 LocalCheck,
89 /// Distance stripes over radial colormap centered at (0,0).
90 LocalRad,
91}
92
93/// How to interpret UV coordinates.
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
95pub enum ParamCoordsType {
96 /// Coordinates in `[0,1]` range.
97 #[default]
98 Unit,
99 /// Coordinates scaled like world-space mesh positions.
100 World,
101}