printobserver-panels 0.20.1

Camera calibration, drift and alignment checks, and the check image's panels, for printobserver.
Documentation
//! What can stand between the camera and the plane the nozzle prints on: the
//! print head, and the gantry that carries it.
//!
//! The printer reports no place for either while it prints. What a job's file
//! position does say is which moves were sent, and a printer works through a
//! queue of them, so the nozzle is somewhere along the last
//! [`QUEUED_MOVES`] moves sent. Each of those places stands a box for the head
//! around the nozzle and a box for the gantry at the nozzle's Y, in bed
//! millimetres with the nozzle's plane at height 0. A point on that plane is
//! hidden when, wherever along those places the nozzle is, the straight line
//! from it to the camera's centre passes through that place's head or
//! gantry: what the camera cannot see past at every place the head may be.
//! It needs none of the lens's distortion, which bends where a point lands
//! but not which points a ray meets.
//!
//! It is the places' common cover rather than their union because the queue
//! on a small part sweeps the whole layer, and a union then marks visible
//! plastic, and bare sheet beside a part that came loose, as hidden: the
//! check of 29 Oct 2026 read a part moved off its walls as a head in the way.
//! Where the queue sweeps the part nothing is marked, and the turn is told
//! the queued moves' span ([`Occluders::span_mm`]), so it judges the plastic
//! it sees and takes the head it sees as the head.
//!
//! The boxes are the CORE One's, measured against the committed frames: the
//! head stands [`HEAD_HALF_WIDTH_MM`] either side of the nozzle in X, from
//! [`HEAD_FRONT_MM`] in front of it to [`HEAD_BACK_MM`] behind it, and
//! [`HEAD_HEIGHT_MM`] above it; the gantry spans the bed in X,
//! [`GANTRY_HALF_DEPTH_MM`] either side of the nozzle's Y, between
//! [`GANTRY_BOTTOM_MM`] and [`GANTRY_TOP_MM`] above it. Where the boxes are
//! a little larger than the machine they mark a little more as hidden than
//! need be. Panel 1 still draws what they hide, thin and dashed, so a part
//! can be held against its walls there.

use crate::calibration::CameraCalibration;

/// How many moves a printer may still hold queued behind the last one sent:
/// the planner's sixteen blocks and the command queue's lines, with room
/// over.
pub const QUEUED_MOVES: usize = 32;

/// How far either side of the nozzle the head reaches in X, in millimetres.
pub const HEAD_HALF_WIDTH_MM: f64 = 30.0;
/// How far in front of the nozzle the head reaches in Y.
pub const HEAD_FRONT_MM: f64 = 30.0;
/// How far behind the nozzle the head reaches in Y.
pub const HEAD_BACK_MM: f64 = 35.0;
/// How far above the nozzle the head reaches.
pub const HEAD_HEIGHT_MM: f64 = 70.0;
/// How far either side of the nozzle's Y the gantry reaches.
pub const GANTRY_HALF_DEPTH_MM: f64 = 20.0;
/// The gantry's underside, above the nozzle.
pub const GANTRY_BOTTOM_MM: f64 = 95.0;
/// The gantry's top, above the nozzle.
pub const GANTRY_TOP_MM: f64 = 125.0;
/// How far the gantry runs past the bed's sides in X.
const GANTRY_SPAN_MM: [f64; 2] = [-60.0, 320.0];

/// One box, its least and greatest corners.
type Cuboid = [[f64; 3]; 2];

/// What can hide the nozzle's plane from the camera on one frame.
#[derive(Debug, Clone, PartialEq)]
pub struct Occluders {
    /// The camera's centre, in bed millimetres with the nozzle's plane at 0.
    camera: [f64; 3],
    /// The head's box and the gantry's at each place the nozzle may be.
    places: Vec<[Cuboid; 2]>,
    /// The places' least and greatest X and Y, `[[x0, x1], [y0, y1]]`.
    span: [[f64; 2]; 2],
}

impl Occluders {
    /// The head and the gantry at every place in `trail` — where the plan has
    /// had the nozzle over the moves still queued; nothing when the trail is
    /// empty.
    #[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,
        })
    }

    /// Whether the camera's view of the point `[x, y]` on the nozzle's plane
    /// passes through the head or the gantry wherever along the trail the
    /// nozzle is.
    #[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)))
    }

    /// The least and greatest X and Y of the places the nozzle may be,
    /// `[[x0, x1], [y0, y1]]` in bed millimetres.
    #[must_use]
    pub const fn span_mm(&self) -> [[f64; 2]; 2] {
        self.span
    }
}

/// Whether the segment from `from` to `from + along` meets a box: the slab
/// test, each axis narrowing the stretch of the segment inside the box.
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));
    }
}