use crate::calibration::CameraCalibration;
pub const QUEUED_MOVES: usize = 32;
pub const HEAD_HALF_WIDTH_MM: f64 = 30.0;
pub const HEAD_FRONT_MM: f64 = 30.0;
pub const HEAD_BACK_MM: f64 = 35.0;
pub const HEAD_HEIGHT_MM: f64 = 70.0;
pub const GANTRY_HALF_DEPTH_MM: f64 = 20.0;
pub const GANTRY_BOTTOM_MM: f64 = 95.0;
pub const GANTRY_TOP_MM: f64 = 125.0;
const GANTRY_SPAN_MM: [f64; 2] = [-60.0, 320.0];
type Cuboid = [[f64; 3]; 2];
#[derive(Debug, Clone, PartialEq)]
pub struct Occluders {
camera: [f64; 3],
places: Vec<[Cuboid; 2]>,
span: [[f64; 2]; 2],
}
impl Occluders {
#[must_use]
pub fn along(calibration: &CameraCalibration, trail: &[[f64; 2]]) -> Option<Self> {
let first = trail.first()?;
let places = trail
.iter()
.map(|[x, y]| {
[
[
[x - HEAD_HALF_WIDTH_MM, y - HEAD_FRONT_MM, 0.0],
[x + HEAD_HALF_WIDTH_MM, y + HEAD_BACK_MM, HEAD_HEIGHT_MM],
],
[
[
GANTRY_SPAN_MM[0],
y - GANTRY_HALF_DEPTH_MM,
GANTRY_BOTTOM_MM,
],
[GANTRY_SPAN_MM[1], y + GANTRY_HALF_DEPTH_MM, GANTRY_TOP_MM],
],
]
})
.collect();
let span = trail.iter().fold(
[[first[0], first[0]], [first[1], first[1]]],
|[[x0, x1], [y0, y1]], [x, y]| [[x0.min(*x), x1.max(*x)], [y0.min(*y), y1.max(*y)]],
);
Some(Self {
camera: calibration.camera_centre_mm(),
places,
span,
})
}
#[must_use]
pub fn hides(&self, [x, y]: [f64; 2]) -> bool {
let from = [x, y, 0.0];
let along = [self.camera[0] - x, self.camera[1] - y, self.camera[2]];
self.places
.iter()
.all(|place| place.iter().any(|cuboid| meets(from, along, cuboid)))
}
#[must_use]
pub const fn span_mm(&self) -> [[f64; 2]; 2] {
self.span
}
}
fn meets(from: [f64; 3], along: [f64; 3], [low, high]: &Cuboid) -> bool {
let (mut enter, mut leave) = (0.0_f64, 1.0_f64);
for axis in 0..3 {
if along[axis].abs() < 1e-12 {
if from[axis] < low[axis] || from[axis] > high[axis] {
return false;
}
continue;
}
let a = (low[axis] - from[axis]) / along[axis];
let b = (high[axis] - from[axis]) / along[axis];
enter = enter.max(a.min(b));
leave = leave.min(a.max(b));
if enter > leave {
return false;
}
}
true
}
#[cfg(test)]
mod tests {
use super::meets;
#[test]
fn a_ray_meets_a_box_it_crosses_and_misses_one_beside_it() {
let cuboid = [[0.0, 0.0, 10.0], [10.0, 10.0, 20.0]];
assert!(meets([5.0, 5.0, 0.0], [0.0, 0.0, 100.0], &cuboid));
assert!(!meets([50.0, 5.0, 0.0], [0.0, 0.0, 100.0], &cuboid));
assert!(
!meets([5.0, 5.0, 0.0], [0.0, 0.0, 5.0], &cuboid),
"it stops short"
);
assert!(meets([-10.0, 5.0, 0.0], [20.0, 0.0, 30.0], &cuboid));
}
}