Skip to main content

polyscope_rs/
volume_mesh.rs

1//! Volume mesh registration and manipulation.
2//!
3//! Volume meshes represent 3D volumetric data as tetrahedral or hexahedral cells.
4//! Quantities (scalar, vector, color) can be defined on vertices or cells.
5//!
6//! # Example
7//!
8//! ```no_run
9//! use polyscope_rs::*;
10//!
11//! fn main() -> Result<()> {
12//!     init()?;
13//!
14//!     // A single tetrahedron
15//!     let vertices = vec![
16//!         Vec3::new(0.0, 0.0, 0.0),
17//!         Vec3::new(1.0, 0.0, 0.0),
18//!         Vec3::new(0.5, 1.0, 0.0),
19//!         Vec3::new(0.5, 0.5, 1.0),
20//!     ];
21//!     let tets = vec![[0, 1, 2, 3]];
22//!
23//!     let vm = register_tet_mesh("my tet", vertices, tets);
24//!     vm.add_vertex_scalar_quantity("height", vec![0.0, 0.0, 0.0, 1.0]);
25//!
26//!     show();
27//!     Ok(())
28//! }
29//! ```
30
31use crate::{Vec3, VolumeMesh, with_context_mut};
32
33/// Registers a tetrahedral mesh with polyscope.
34pub fn register_tet_mesh(
35    name: impl Into<String>,
36    vertices: Vec<Vec3>,
37    tets: Vec<[u32; 4]>,
38) -> VolumeMeshHandle {
39    let name = name.into();
40    let mesh = VolumeMesh::new_tet_mesh(name.clone(), vertices, tets);
41
42    with_context_mut(|ctx| {
43        ctx.registry
44            .register(Box::new(mesh))
45            .expect("failed to register tet mesh");
46        ctx.update_extents();
47    });
48
49    VolumeMeshHandle { name }
50}
51
52/// Registers a hexahedral mesh with polyscope.
53pub fn register_hex_mesh(
54    name: impl Into<String>,
55    vertices: Vec<Vec3>,
56    hexes: Vec<[u32; 8]>,
57) -> VolumeMeshHandle {
58    let name = name.into();
59    let mesh = VolumeMesh::new_hex_mesh(name.clone(), vertices, hexes);
60
61    with_context_mut(|ctx| {
62        ctx.registry
63            .register(Box::new(mesh))
64            .expect("failed to register hex mesh");
65        ctx.update_extents();
66    });
67
68    VolumeMeshHandle { name }
69}
70
71/// Registers a generic volume mesh with polyscope.
72///
73/// Cells are stored as 8-index arrays. For tetrahedra, indices 4-7 should be `u32::MAX`.
74pub fn register_volume_mesh(
75    name: impl Into<String>,
76    vertices: Vec<Vec3>,
77    cells: Vec<[u32; 8]>,
78) -> VolumeMeshHandle {
79    let name = name.into();
80    let mesh = VolumeMesh::new(name.clone(), vertices, cells);
81
82    with_context_mut(|ctx| {
83        ctx.registry
84            .register(Box::new(mesh))
85            .expect("failed to register volume mesh");
86        ctx.update_extents();
87    });
88
89    VolumeMeshHandle { name }
90}
91
92impl_structure_accessors! {
93    get_fn = get_volume_mesh,
94    with_fn = with_volume_mesh,
95    with_ref_fn = with_volume_mesh_ref,
96    handle = VolumeMeshHandle,
97    type_name = "VolumeMesh",
98    rust_type = VolumeMesh,
99    doc_name = "volume mesh"
100}
101
102/// Handle for a registered volume mesh.
103#[derive(Clone)]
104pub struct VolumeMeshHandle {
105    name: String,
106}
107
108impl VolumeMeshHandle {
109    /// Returns the name of this volume mesh.
110    #[must_use]
111    pub fn name(&self) -> &str {
112        &self.name
113    }
114
115    /// Sets the base color.
116    pub fn set_color(&self, color: Vec3) -> &Self {
117        with_volume_mesh(&self.name, |vm| {
118            vm.set_color(color);
119        });
120        self
121    }
122
123    /// Sets the interior color.
124    pub fn set_interior_color(&self, color: Vec3) -> &Self {
125        with_volume_mesh(&self.name, |vm| {
126            vm.set_interior_color(color);
127        });
128        self
129    }
130
131    /// Sets the edge color.
132    pub fn set_edge_color(&self, color: Vec3) -> &Self {
133        with_volume_mesh(&self.name, |vm| {
134            vm.set_edge_color(color);
135        });
136        self
137    }
138
139    /// Sets the edge width.
140    pub fn set_edge_width(&self, width: f32) -> &Self {
141        with_volume_mesh(&self.name, |vm| {
142            vm.set_edge_width(width);
143        });
144        self
145    }
146
147    /// Adds a vertex scalar quantity.
148    pub fn add_vertex_scalar_quantity(&self, name: impl Into<String>, values: Vec<f32>) -> &Self {
149        let name = name.into();
150        with_volume_mesh(&self.name, |vm| {
151            vm.add_vertex_scalar_quantity(name, values);
152        });
153        self
154    }
155
156    /// Adds a cell scalar quantity.
157    pub fn add_cell_scalar_quantity(&self, name: impl Into<String>, values: Vec<f32>) -> &Self {
158        let name = name.into();
159        with_volume_mesh(&self.name, |vm| {
160            vm.add_cell_scalar_quantity(name, values);
161        });
162        self
163    }
164
165    /// Adds a vertex color quantity.
166    pub fn add_vertex_color_quantity(&self, name: impl Into<String>, colors: Vec<Vec3>) -> &Self {
167        let name = name.into();
168        with_volume_mesh(&self.name, |vm| {
169            vm.add_vertex_color_quantity(name, colors);
170        });
171        self
172    }
173
174    /// Adds a cell color quantity.
175    pub fn add_cell_color_quantity(&self, name: impl Into<String>, colors: Vec<Vec3>) -> &Self {
176        let name = name.into();
177        with_volume_mesh(&self.name, |vm| {
178            vm.add_cell_color_quantity(name, colors);
179        });
180        self
181    }
182
183    /// Adds a vertex vector quantity.
184    pub fn add_vertex_vector_quantity(&self, name: impl Into<String>, vectors: Vec<Vec3>) -> &Self {
185        let name = name.into();
186        with_volume_mesh(&self.name, |vm| {
187            vm.add_vertex_vector_quantity(name, vectors);
188        });
189        self
190    }
191
192    /// Adds a cell vector quantity.
193    pub fn add_cell_vector_quantity(&self, name: impl Into<String>, vectors: Vec<Vec3>) -> &Self {
194        let name = name.into();
195        with_volume_mesh(&self.name, |vm| {
196            vm.add_cell_vector_quantity(name, vectors);
197        });
198        self
199    }
200}