use crate::{diagram2d::DiagramLayout, mesh_cache::fnv1a_64};
use glam::Vec3;
use indicatrix::geometry::{GpuFacetPlane, StoneGeometry, ToolPrimitive};
use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CameraPose {
pub yaw: f32,
pub pitch: f32,
pub distance: f32,
}
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct FacetOverlay {
pub hovered: Option<u32>,
pub selected_facet: Option<u32>,
pub multi_selected: Vec<u32>,
pub provisional: Vec<u32>,
pub moved: Vec<u32>,
}
#[derive(Debug, Clone)]
pub struct FrameGeometry {
pub corner_points: Arc<Vec<Vec3>>,
pub facet_centroids: Arc<Vec<Option<Vec3>>>,
pub bounding_radius: f64,
pub camera: CameraPose,
pub size: (u32, u32),
pub diagram: Option<Arc<DiagramLayout>>,
pub visible_tiers: Option<Arc<Vec<bool>>>,
}
#[derive(Debug, Clone)]
pub struct PickBuffer {
pub width: u32,
pub height: u32,
pub pick: Vec<u32>,
}
impl PickBuffer {
#[must_use]
pub fn facet_at(&self, x: u32, y: u32) -> Option<u32> {
if x >= self.width || y >= self.height {
return None;
}
let v = self.pick[(y * self.width + x) as usize];
(v != 0).then(|| v - 1)
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct StoneGeometryBuf {
pub planes: Vec<GpuFacetPlane>,
pub tools: Vec<ToolPrimitive>,
pub placements: Vec<(usize, usize)>,
}
impl StoneGeometryBuf {
#[must_use]
pub const fn planes_only(planes: Vec<GpuFacetPlane>) -> Self {
Self {
planes,
tools: Vec::new(),
placements: Vec::new(),
}
}
#[must_use]
pub fn from_halfspaces(planes: &[(Vec3, f32)]) -> Self {
Self::planes_only(
planes
.iter()
.map(|&(normal, m)| GpuFacetPlane {
normal: normal.to_array(),
d: -m,
})
.collect(),
)
}
#[must_use]
pub fn halfspaces(&self) -> Vec<(Vec3, f32)> {
self.planes
.iter()
.map(|p| (Vec3::from_array(p.normal), -p.d))
.collect()
}
#[must_use]
pub fn as_geometry(&self) -> StoneGeometry<'_> {
StoneGeometry {
planes: &self.planes,
tools: &self.tools,
}
}
#[must_use]
pub fn cache_key(&self) -> u64 {
let plane_bytes = self.planes.iter().flat_map(|p| {
[p.normal[0], p.normal[1], p.normal[2], -p.d]
.into_iter()
.flat_map(|component| component.to_bits().to_le_bytes())
});
let tool_bytes = self.tools.iter().flat_map(|t| {
let words = [t.kind, t.sweep_kind, t._pad[0], t._pad[1]]
.into_iter()
.flat_map(u32::to_le_bytes);
let floats = t
.origin
.into_iter()
.chain(t.axis)
.chain(t.profile)
.chain(t.sweep_dir)
.flat_map(|component| component.to_bits().to_le_bytes());
words.chain(floats)
});
fnv1a_64(plane_bytes.chain(tool_bytes))
}
}