calib_targets_core/
rectify.rs

1use crate::{GrayImage, Homography};
2use nalgebra::Point2;
3
4pub struct RectifiedView {
5    pub rect: GrayImage,
6    pub px_per_square: f32,
7    pub cells_x: usize,
8    pub cells_y: usize,
9    pub rect_to_img: RectToImgMapper, // enum for GlobalH | Mesh
10}
11
12pub enum RectToImgMapper {
13    Global {
14        h_img_from_rect: Homography,
15    },
16    Mesh {
17        cells_x: usize,
18        cells_y: usize,
19        px_per_square: f32,
20        cell_h: Vec<Option<Homography>>,
21    },
22}
23
24impl RectToImgMapper {
25    pub fn map(&self, p_rect: Point2<f32>) -> Option<Point2<f32>> {
26        match self {
27            RectToImgMapper::Global { h_img_from_rect } => Some(h_img_from_rect.apply(p_rect)),
28            RectToImgMapper::Mesh {
29                cells_x,
30                cells_y,
31                px_per_square,
32                cell_h,
33            } => {
34                let s = *px_per_square;
35                let ci = (p_rect.x / s).floor() as i32;
36                let cj = (p_rect.y / s).floor() as i32;
37                if ci < 0 || cj < 0 || ci >= *cells_x as i32 || cj >= *cells_y as i32 {
38                    return None;
39                }
40                let idx = cj as usize * (*cells_x) + ci as usize;
41                let h = cell_h[idx].as_ref()?;
42                let local = Point2::new(p_rect.x - ci as f32 * s, p_rect.y - cj as f32 * s);
43                Some(h.apply(local))
44            }
45        }
46    }
47}