use crate::{Vec3, Vec4, with_context, with_context_mut};
pub fn add_slice_plane(name: impl Into<String>) -> SlicePlaneHandle {
let name = name.into();
with_context_mut(|ctx| {
let length_scale = ctx.length_scale;
let center = (ctx.bounding_box.0 + ctx.bounding_box.1) * 0.5;
let plane = ctx.add_slice_plane(&name);
plane.set_plane_size(length_scale * 0.25);
plane.set_origin(center);
});
SlicePlaneHandle { name }
}
pub fn add_slice_plane_with_pose(
name: impl Into<String>,
origin: Vec3,
normal: Vec3,
) -> SlicePlaneHandle {
let name = name.into();
with_context_mut(|ctx| {
let plane = ctx.add_slice_plane(&name);
plane.set_pose(origin, normal);
});
SlicePlaneHandle { name }
}
pub fn add_slice_plane_auto() -> SlicePlaneHandle {
let name = with_context_mut(|ctx| {
let mut i = 0usize;
let candidate = loop {
let c = format!("Scene Slice Plane {i}");
if !ctx.has_slice_plane(&c) {
break c;
}
i += 1;
};
let length_scale = ctx.length_scale;
let center = (ctx.bounding_box.0 + ctx.bounding_box.1) * 0.5;
let plane = ctx.add_slice_plane(&candidate);
plane.set_plane_size(length_scale * 0.25);
plane.set_origin(center);
candidate
});
SlicePlaneHandle { name }
}
#[must_use]
pub fn get_slice_plane(name: &str) -> Option<SlicePlaneHandle> {
with_context(|ctx| {
if ctx.has_slice_plane(name) {
Some(SlicePlaneHandle {
name: name.to_string(),
})
} else {
None
}
})
}
pub fn remove_slice_plane(name: &str) {
with_context_mut(|ctx| {
ctx.remove_slice_plane(name);
});
}
pub fn remove_all_slice_planes() {
with_context_mut(|ctx| {
ctx.slice_planes.clear();
});
}
#[must_use]
pub fn get_all_slice_planes() -> Vec<String> {
with_context(|ctx| {
ctx.slice_plane_names()
.into_iter()
.map(std::string::ToString::to_string)
.collect()
})
}
#[derive(Clone)]
pub struct SlicePlaneHandle {
name: String,
}
impl SlicePlaneHandle {
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
pub fn remove(self) {
remove_slice_plane(&self.name);
}
pub fn set_pose(&self, origin: Vec3, normal: Vec3) -> &Self {
with_context_mut(|ctx| {
if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
plane.set_pose(origin, normal);
}
});
self
}
pub fn set_origin(&self, origin: Vec3) -> &Self {
with_context_mut(|ctx| {
if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
plane.set_origin(origin);
}
});
self
}
#[must_use]
pub fn origin(&self) -> Vec3 {
with_context(|ctx| {
ctx.get_slice_plane(&self.name)
.map_or(Vec3::ZERO, polyscope_core::SlicePlane::origin)
})
}
pub fn set_normal(&self, normal: Vec3) -> &Self {
with_context_mut(|ctx| {
if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
plane.set_normal(normal);
}
});
self
}
#[must_use]
pub fn normal(&self) -> Vec3 {
with_context(|ctx| {
ctx.get_slice_plane(&self.name)
.map_or(Vec3::Y, polyscope_core::SlicePlane::normal)
})
}
pub fn set_enabled(&self, enabled: bool) -> &Self {
with_context_mut(|ctx| {
if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
plane.set_enabled(enabled);
}
});
self
}
#[must_use]
pub fn is_enabled(&self) -> bool {
with_context(|ctx| {
ctx.get_slice_plane(&self.name)
.is_some_and(polyscope_core::SlicePlane::is_enabled)
})
}
pub fn set_draw_plane(&self, draw: bool) -> &Self {
with_context_mut(|ctx| {
if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
plane.set_draw_plane(draw);
}
});
self
}
#[must_use]
pub fn draw_plane(&self) -> bool {
with_context(|ctx| {
ctx.get_slice_plane(&self.name)
.is_some_and(polyscope_core::SlicePlane::draw_plane)
})
}
pub fn set_draw_widget(&self, draw: bool) -> &Self {
with_context_mut(|ctx| {
if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
plane.set_draw_widget(draw);
}
});
self
}
#[must_use]
pub fn draw_widget(&self) -> bool {
with_context(|ctx| {
ctx.get_slice_plane(&self.name)
.is_some_and(polyscope_core::SlicePlane::draw_widget)
})
}
pub fn set_color(&self, color: Vec3) -> &Self {
with_context_mut(|ctx| {
if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
plane.set_color(color);
}
});
self
}
#[must_use]
pub fn color(&self) -> Vec4 {
with_context(|ctx| {
ctx.get_slice_plane(&self.name).map_or(
Vec4::new(0.5, 0.5, 0.5, 1.0),
polyscope_core::SlicePlane::color,
)
})
}
pub fn set_transparency(&self, transparency: f32) -> &Self {
with_context_mut(|ctx| {
if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
plane.set_transparency(transparency);
}
});
self
}
#[must_use]
pub fn transparency(&self) -> f32 {
with_context(|ctx| {
ctx.get_slice_plane(&self.name)
.map_or(0.3, polyscope_core::SlicePlane::transparency)
})
}
pub fn set_plane_size(&self, size: f32) -> &Self {
with_context_mut(|ctx| {
if let Some(plane) = ctx.get_slice_plane_mut(&self.name) {
plane.set_plane_size(size);
}
});
self
}
#[must_use]
pub fn plane_size(&self) -> f32 {
with_context(|ctx| {
ctx.get_slice_plane(&self.name)
.map_or(0.1, polyscope_core::SlicePlane::plane_size)
})
}
}