solvastro 0.1.1

A Rust blind plate solver
Documentation
// use generator::{done, Generator, Gn};
use crate::dynamic_index::star_index_container::ReadableStarIndexContainer;
use crate::structs_f64::quad::{Quad, QuadCode};
use crate::structs_f64::query::{Query, QueryPoint};

// checked through some solved image solutions to see what range of code tols we got.
// highest good was 0.0187. Lowest bad was 0.0210 (With SqEuc, need to update numbers
// for Manhattan dist)

// NOTE: increasing this to 0.02 can give better matches, at the expense
// of longer exec time
// const CODE_TOL: f64 = 0.013;

// TODO: TUNE ME now that Manhattan is being used
const CODE_TOL: f64 = 0.02;

#[derive(Debug)]
pub struct QuadMatch {
    pub query_quad: Quad,
    pub starindex_quad_idx: usize,
    pub code_dist: f64,
}

pub fn matcher<'a>(
    candidates: &'a mut dyn Iterator<Item = Quad>,
    query: &'a Query,
    index: &'a ReadableStarIndexContainer,
) -> impl Iterator<Item = QuadMatch> + 'a {
    let points: &Vec<QueryPoint> = &query.points;

    candidates
        // for each candidate quad, return a tuple of (Quad, QuadCode)
        .map(move |quad| (quad, calc_code(&quad, points)))
        // For each (Quad, QuadCode), find all matching quads in the index
        // flatten them into a single list of QuadMatches
        .flat_map(move |(quad, search_code)| {
            // TODO: prevent the allocation here
            index
                .quad_codes_within_radius(&search_code, CODE_TOL)
                .iter()
                .map(move |nn| QuadMatch {
                    query_quad: quad,
                    starindex_quad_idx: nn.item as usize,
                    code_dist: nn.distance.into(),
                })
                .collect::<Vec<_>>()
        })
}

// pub fn match_generator<'a>(
//     candidates: &'a mut dyn Iterator<Item = Quad>,
//     query: &'a Query,
//     index: &'a StarIndex,
// )-> Generator<'a, (), QuadMatch> {
//     let points: &Vec<QueryPoint> = &query.points;
//     log::trace!("starting match_generator");
//
//     Gn::new_scoped(move |mut s| {
//         for quad in candidates {
//             log::trace!("calculating code for quad {:?}", &quad);
//             let code = calc_code(&quad, points);
//
//             log::trace!("finding matches for code {:?}", &code);
//             for nn in index.kd_codes.within_unsorted_iter::<Manhattan>(&code, CODE_TOL) {
//                 log::trace!("yielding a quad: {:?}", &quad);
//                 s.yield_(
//                     QuadMatch {
//                         query_quad: quad,
//                         starindex_quad_idx: nn.item as usize,
//                         code_dist: nn.distance.into(),
//                     }
//                 );
//             }
//         }
//
//         done!();
//     })
// }

pub fn calc_code(quad: &Quad, points: &[QueryPoint]) -> QuadCode {
    let a: &QueryPoint = &points[quad.a];
    let b: &QueryPoint = &points[quad.b];
    let c: &QueryPoint = &points[quad.c];
    let d: &QueryPoint = &points[quad.d];

    // translate to place origin at a
    let ab = (b.x - a.x, b.y - a.y);
    let ac = (c.x - a.x, c.y - a.y);
    let ad = (d.x - a.x, d.y - a.y);

    // rescale so that len AB = 1?
    // (nb len AB = sqrt 2 in fig 1 of Lang 2009?)
    let inv_scale: f64 = 1f64 / (ab.0 * ab.0 + ab.1 * ab.1);

    // create factors that combine the required rotation and scaling
    let cos_theta: f64 = (ab.1 + ab.0) * inv_scale;
    let sin_theta: f64 = (ab.1 - ab.0) * inv_scale;

    // rotate-and-scale
    let mut code = [
        ac.0 * cos_theta + ac.1 * sin_theta,
        -ac.0 * sin_theta + ac.1 * cos_theta,
        ad.0 * cos_theta + ad.1 * sin_theta,
        -ad.0 * sin_theta + ad.1 * cos_theta,
    ];

    // handle parity flipping
    let mut rodeos = 0;
    while rodeos <= 2 && (code[2] > code[0] || code[0] + code[2] > 1f64) {
        rodeos += 1;
        if code[2] > code[0] {
            code = [code[2], code[3], code[0], code[1]];
        }

        if code[0] + code[2] > 1f64 {
            code = [
                1f64 - code[0],
                1f64 - code[1],
                1f64 - code[2],
                1f64 - code[3],
            ];
        }
    }
    assert!(code[2] <= code[0] && code[0] + code[2] <= 1f64);

    code
}