use crate::{Vec3, VolumeGrid, with_context_mut};
use polyscope_core::structure::HasQuantities;
use polyscope_structures::volume_grid::{
VolumeGridCellScalarQuantity, VolumeGridNodeScalarQuantity, VolumeGridVizMode,
};
pub fn register_volume_grid(
name: impl Into<String>,
node_dim: glam::UVec3,
bound_min: Vec3,
bound_max: Vec3,
) -> VolumeGridHandle {
let name = name.into();
let grid = VolumeGrid::new(name.clone(), node_dim, bound_min, bound_max);
with_context_mut(|ctx| {
ctx.registry
.register(Box::new(grid))
.expect("failed to register volume grid");
ctx.update_extents();
});
VolumeGridHandle { name }
}
pub fn register_volume_grid_uniform(
name: impl Into<String>,
dim: u32,
bound_min: Vec3,
bound_max: Vec3,
) -> VolumeGridHandle {
register_volume_grid(name, glam::UVec3::splat(dim), bound_min, bound_max)
}
impl_structure_accessors! {
get_fn = get_volume_grid,
with_fn = with_volume_grid,
with_ref_fn = with_volume_grid_ref,
handle = VolumeGridHandle,
type_name = "VolumeGrid",
rust_type = VolumeGrid,
doc_name = "volume grid"
}
#[derive(Clone)]
pub struct VolumeGridHandle {
name: String,
}
impl VolumeGridHandle {
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
pub fn set_edge_color(&self, color: Vec3) -> &Self {
with_volume_grid(&self.name, |vg| {
vg.set_edge_color(color);
});
self
}
pub fn set_edge_width(&self, width: f32) -> &Self {
with_volume_grid(&self.name, |vg| {
vg.set_edge_width(width);
});
self
}
pub fn add_node_scalar_quantity(&self, name: &str, values: Vec<f32>) -> &Self {
with_volume_grid(&self.name, |vg| {
vg.add_node_scalar_quantity(name, values);
});
self
}
pub fn add_cell_scalar_quantity(&self, name: &str, values: Vec<f32>) -> &Self {
with_volume_grid(&self.name, |vg| {
vg.add_cell_scalar_quantity(name, values);
});
self
}
pub fn set_cube_size_factor(&self, factor: f32) -> &Self {
with_volume_grid(&self.name, |vg| {
vg.set_cube_size_factor(factor);
});
self
}
pub fn set_quantity_enabled(&self, quantity_name: &str, enabled: bool) -> &Self {
with_volume_grid(&self.name, |vg| {
if let Some(q) = vg.get_quantity_mut(quantity_name) {
q.set_enabled(enabled);
}
});
self
}
pub fn set_node_scalar_viz_mode(&self, quantity_name: &str, mode: VolumeGridVizMode) -> &Self {
with_volume_grid(&self.name, |vg| {
if let Some(q) = vg.get_quantity_mut(quantity_name) {
if let Some(nsq) = q
.as_any_mut()
.downcast_mut::<VolumeGridNodeScalarQuantity>()
{
nsq.set_viz_mode(mode);
}
}
});
self
}
pub fn set_isosurface_level(&self, quantity_name: &str, level: f32) -> &Self {
with_volume_grid(&self.name, |vg| {
if let Some(q) = vg.get_quantity_mut(quantity_name) {
if let Some(nsq) = q
.as_any_mut()
.downcast_mut::<VolumeGridNodeScalarQuantity>()
{
nsq.set_isosurface_level(level);
}
}
});
self
}
pub fn set_isosurface_color(&self, quantity_name: &str, color: Vec3) -> &Self {
with_volume_grid(&self.name, |vg| {
if let Some(q) = vg.get_quantity_mut(quantity_name) {
if let Some(nsq) = q
.as_any_mut()
.downcast_mut::<VolumeGridNodeScalarQuantity>()
{
nsq.set_isosurface_color(color);
}
}
});
self
}
pub fn set_color_map(&self, quantity_name: &str, color_map: &str) -> &Self {
with_volume_grid(&self.name, |vg| {
if let Some(q) = vg.get_quantity_mut(quantity_name) {
if let Some(nsq) = q
.as_any_mut()
.downcast_mut::<VolumeGridNodeScalarQuantity>()
{
nsq.set_color_map(color_map);
} else if let Some(csq) = q
.as_any_mut()
.downcast_mut::<VolumeGridCellScalarQuantity>()
{
csq.set_color_map(color_map);
}
}
});
self
}
}