use std::{
cell::{Cell, RefCell},
rc::Rc,
};
use cranpose_core::NodeId;
use cranpose_ui_graphics::{
Point, Rect, Size,
space::{Camera, Mat4, MeshSurface, Ray, SurfaceHit, Vec3},
};
use crate::{
graph::ProjectiveTransform,
scene_store::LayerRef,
space::{EyeView, SpaceTree},
};
const PAST_DOORWAY: f32 = 1.0e-4;
pub struct ScenePick {
scene: Option<NodeId>,
camera: Camera,
size: Size,
window_to_scene: ProjectiveTransform,
scene_to_window: ProjectiveTransform,
surfaces: Vec<PickSurface>,
seen: Vec<PickSpace>,
scenes: Vec<NodeId>,
seen_scenes: Vec<NodeId>,
last: Cell<Option<LastPick>>,
holds: SurfaceHolds,
}
#[derive(Clone, Default)]
pub struct SurfaceHolds(Rc<Holds>);
#[derive(Default)]
struct Holds {
holds: RefCell<Vec<Hold>>,
collection: Cell<u32>,
}
#[derive(Clone, Copy)]
struct Hold {
layer: NodeId,
mesh: usize,
triangle: u32,
collection: u32,
}
impl SurfaceHolds {
pub fn collect_again(&self) {
let last = self.0.collection.get();
self.0
.holds
.borrow_mut()
.retain(|hold| hold.collection == last);
self.0.collection.set(last.wrapping_add(1));
}
fn keep(&self, layer: NodeId) {
let now = self.0.collection.get();
if let Some(hold) = self
.0
.holds
.borrow_mut()
.iter_mut()
.find(|hold| hold.layer == layer)
{
hold.collection = now;
}
}
fn of(&self, layer: NodeId) -> Option<Hold> {
self.0
.holds
.borrow()
.iter()
.find(|hold| hold.layer == layer)
.copied()
}
fn hold(&self, layer: NodeId, mesh: usize, triangle: u32) {
let hold = Hold {
layer,
mesh,
triangle,
collection: self.0.collection.get(),
};
let mut holds = self.0.holds.borrow_mut();
match holds.iter_mut().find(|held| held.layer == layer) {
Some(held) => *held = hold,
None => holds.push(hold),
}
}
}
struct PickSpace {
through: Option<usize>,
to_door: Mat4,
}
impl PickSpace {
fn meet(&self, ray: Ray) -> Option<f32> {
let origin = self.to_door.transform_point(ray.origin);
let direction = self.to_door.transform_vector(ray.direction);
if direction.z.abs() < f32::EPSILON {
return None;
}
let distance = -origin.z / direction.z;
let at = origin + direction * distance;
let inside = (0.0..=1.0).contains(&at.x) && (0.0..=1.0).contains(&at.y);
(inside && distance > 0.0).then_some(distance)
}
}
type LastPick = (Point, Option<(usize, SurfaceHit)>);
struct PickSurface {
node_id: Option<NodeId>,
surface: MeshSurface,
mesh: usize,
content: Rect,
seen: usize,
}
impl PickSurface {
fn in_layer(&self, point: Point) -> Point {
Point::new(point.x + self.content.x, point.y + self.content.y)
}
fn cast(&self, ray: Ray) -> Option<SurfaceHit> {
let mut hit = self.surface.cast(ray, self.content.size())?;
hit.point = self.in_layer(hit.point);
Some(hit)
}
}
impl ScenePick {
pub fn of(
layer: LayerRef<'_>,
to_window: ProjectiveTransform,
holds: Option<&SurfaceHolds>,
) -> Option<Self> {
let tree = SpaceTree::of(layer)?;
let holds = holds.cloned().unwrap_or_default();
let bounds = layer.local_bounds();
let to_window = ProjectiveTransform::translation(bounds.x, bounds.y).then(to_window);
let window_to_scene = to_window.inverse()?;
let view = EyeView::of(&tree, bounds.size());
let seen_scenes = view
.seen
.iter()
.filter_map(|seen| tree.cells[seen.cell].layer.node_id())
.collect();
let seen = view
.seen
.iter()
.map(|space| PickSpace {
through: space.through,
to_door: space
.door
.and_then(|door| door.quad.inverse())
.unwrap_or(Mat4::IDENTITY),
})
.collect();
let surfaces = view
.surfaces
.into_iter()
.map(|seen| {
let node_id = seen.layer.node_id();
if let Some(layer) = node_id.filter(|_| seen.mesh == 0) {
holds.keep(layer);
}
PickSurface {
node_id,
surface: seen.surface,
mesh: seen.mesh,
content: seen.content,
seen: seen.seen,
}
})
.collect();
Some(Self {
scene: layer.node_id(),
camera: view.camera,
size: view.size,
window_to_scene,
scene_to_window: to_window,
surfaces,
seen,
scenes: tree
.cells
.iter()
.filter_map(|cell| cell.layer.node_id())
.collect(),
seen_scenes,
last: Cell::new(None),
holds,
})
}
pub fn scene(&self) -> Option<NodeId> {
self.scene
}
pub fn holds_scene(&self, layer: LayerRef<'_>) -> bool {
layer
.node_id()
.is_some_and(|node_id| self.scenes.contains(&node_id))
}
pub fn sees_scene(&self, layer: LayerRef<'_>) -> bool {
layer
.node_id()
.is_some_and(|node_id| self.seen_scenes.contains(&node_id))
}
pub fn index_of(&self, layer: LayerRef<'_>) -> Option<usize> {
let node_id = layer.node_id()?;
self.surfaces
.iter()
.position(|surface| surface.node_id == Some(node_id))
}
pub fn content_point(&self, window: Point, index: usize) -> Option<Point> {
let (nearest, hit) = self.nearest(window)?;
(self.owner(nearest) == index).then_some(hit.point)
}
pub fn content_point_along(&self, ray: Ray, index: usize) -> Option<Point> {
let (nearest, hit) = self.nearest_along(ray)?;
(self.owner(nearest) == index).then_some(hit.point)
}
pub fn drag_point(&self, window: Point, index: usize) -> Option<Point> {
self.drag_point_along(self.ray(window)?, index)
}
pub fn drag_point_along(&self, ray: Ray, index: usize) -> Option<Point> {
let layer = self.surfaces.get(index)?.node_id;
let Some(held) = layer.and_then(|layer| self.holds.of(layer)) else {
let (target, hit) = self.nearest_mesh(ray, index)?;
self.hold(target, hit.triangle);
return Some(hit.point);
};
let target = index + held.mesh;
let surface = self
.surfaces
.get(target)
.filter(|surface| surface.mesh == held.mesh)?;
if let Some(hit) = surface.cast(ray) {
self.hold(target, hit.triangle);
return Some(hit.point);
}
surface
.surface
.cast_plane(ray, held.triangle, surface.content.size())
.map(|point| surface.in_layer(point))
}
pub fn window_point(&self, index: usize, content: Point) -> Option<Point> {
let target = self.surfaces.get(index)?;
let from_corner = Point::new(content.x - target.content.x, content.y - target.content.y);
let at = target
.surface
.point_at(from_corner, target.content.size())?;
let shown = self.camera.project(at, self.size)?;
Some(self.scene_to_window.map_point(shown))
}
pub fn window_point_of(&self, point: Vec3) -> Option<Point> {
let shown = self.camera.project(point, self.size)?;
Some(self.scene_to_window.map_point(shown))
}
fn ray(&self, window: Point) -> Option<Ray> {
let local = self.window_to_scene.map_point(window);
self.camera.ray(local, self.size)
}
fn owner(&self, index: usize) -> usize {
index - self.surfaces[index].mesh
}
fn hold(&self, index: usize, triangle: u32) {
let surface = &self.surfaces[index];
if let Some(layer) = surface.node_id {
self.holds.hold(layer, surface.mesh, triangle);
}
}
fn nearest_mesh(&self, ray: Ray, index: usize) -> Option<(usize, SurfaceHit)> {
self.surfaces[index..]
.iter()
.enumerate()
.take_while(|(mesh, surface)| surface.mesh == *mesh)
.filter_map(|(mesh, surface)| surface.cast(ray).map(|hit| (index + mesh, hit)))
.min_by(|(_, a), (_, b)| a.distance.total_cmp(&b.distance))
}
fn nearest(&self, window: Point) -> Option<(usize, SurfaceHit)> {
if let Some((point, nearest)) = self.last.get()
&& point == window
{
return nearest;
}
let nearest = self.ray(window).and_then(|ray| self.nearest_along(ray));
self.last.set(Some((window, nearest)));
nearest
}
fn nearest_along(&self, ray: Ray) -> Option<(usize, SurfaceHit)> {
let nearest = self.nearest_in(ray, 0, f32::NEG_INFINITY);
if let Some((index, hit)) = nearest {
self.hold(index, hit.triangle);
}
nearest
}
fn nearest_in(&self, ray: Ray, seen: usize, beyond: f32) -> Option<(usize, SurfaceHit)> {
let surface = self
.surfaces
.iter()
.enumerate()
.filter(|(_, target)| target.seen == seen)
.filter_map(|(index, target)| {
target
.cast(ray)
.filter(|hit| hit.distance > beyond)
.map(|hit| (index, hit))
})
.min_by(|(_, a), (_, b)| a.distance.total_cmp(&b.distance));
let doorway = self
.seen
.iter()
.enumerate()
.filter(|(_, space)| space.through == Some(seen))
.filter_map(|(index, space)| {
space
.meet(ray)
.filter(|distance| *distance > beyond)
.map(|distance| (index, distance))
})
.min_by(|(_, a), (_, b)| a.total_cmp(b));
let Some((door, distance)) = doorway.filter(|(_, distance)| {
surface.is_none_or(|(_, hit)| *distance <= hit.distance * (1.0 + PAST_DOORWAY))
}) else {
return surface;
};
self.nearest_in(ray, door, distance * (1.0 + PAST_DOORWAY))
.or(surface)
}
}