solvastro 0.1.1

A Rust blind plate solver
Documentation
use std::error::Error;

use image::{DynamicImage, Rgba};
use imageproc::drawing::{draw_hollow_circle_mut, draw_line_segment_mut, draw_text_mut};
use rusttype::{Font, Scale};

use solvastro::structs_f64::query::Query;
use solvastro::structs_f64::star::Star;
use solvastro::k4::star_index::StarIndex;
use solvastro::k4::verified_solution::{ItemMatchResult, VerifiedSolution};

const ANNOTATED_IMG_MARKER_SRC_RADIUS: i32 = 10;
const CRPIX_CROSS_SIZE: f32 = 20.0;

pub fn generate_output_artifacts(
    index: &StarIndex,
    img_query_annotated: &DynamicImage,
    best_solution: Option<VerifiedSolution>,
) -> Result<(), Box<dyn Error>> {
    if let Some(best_solution) = best_solution {
        println!(
            "soln: logodds = {:}, match count = {:}",
            &best_solution.logodds,
            &best_solution.matched_cat_indices.len()
        );

        let img_solution_annotated =
            annotate_image_solution(&img_query_annotated, &best_solution, &index);
        img_solution_annotated.save("solution-annotated.png")?;

        best_solution.save("verified-solution-iter.json")?;

        best_solution
            .solution
            .as_ref()
            .unwrap()
            .wcs
            .write_to_file("verified-solution-wcs.json")?;
    } else {
        println!("No good solns");
    }

    Ok(())
}

pub fn annotate_image_query(img: &DynamicImage, query: &Query) -> DynamicImage {
    let red = Rgba([255u8, 0u8, 0u8, 255u8]);

    let font = Vec::from(include_bytes!("DejaVuSans.ttf") as &[u8]);
    let font = Font::try_from_vec(font).unwrap();

    let height = 18f32;
    let scale = Scale {
        x: height,
        y: height,
    };

    let mut new_img = img.clone();

    for (idx, &point) in query.points.iter().enumerate() {
        let centre = (point.x as i32, point.y as i32);
        draw_hollow_circle_mut(&mut new_img, centre, ANNOTATED_IMG_MARKER_SRC_RADIUS, red);

        draw_text_mut(
            &mut new_img,
            red,
            (point.x as i32).saturating_sub(5i32),
            (point.y as i32).saturating_sub(30i32),
            scale,
            &font,
            idx.to_string().as_str(),
        );
    }

    new_img
}

pub fn annotate_image_solution(
    img: &DynamicImage,
    solution: &VerifiedSolution,
    index: &StarIndex,
) -> DynamicImage {
    let green = Rgba([0u8, 255u8, 0u8, 255u8]);
    let cyan = Rgba([0u8, 255u8, 255u8, 255u8]);
    let magenta = Rgba([255u8, 0u8, 255u8, 255u8]);

    let font = Vec::from(include_bytes!("DejaVuSans.ttf") as &[u8]);
    let font = Font::try_from_vec(font).unwrap();

    let height = 18f32;
    let scale = Scale {
        x: height,
        y: height,
    };

    let mut new_img = img.clone();

    for img_match in &solution.matches {
        if let ItemMatchResult::Match {
            index_x,
            index_y,
            index_name,
            star_idx,
            ..
        } = img_match
        {
            let centre = (*index_x as i32, *index_y as i32);
            draw_hollow_circle_mut(&mut new_img, centre, ANNOTATED_IMG_MARKER_SRC_RADIUS, green);
            draw_text_mut(
                &mut new_img,
                green,
                (*index_x as i32).saturating_add(15i32),
                (*index_y as i32).saturating_sub(7i32),
                scale,
                &font,
                &index_name,
            );

            let star: &Star = index.stars.get(*star_idx).unwrap();
            let star_world = star.spherical();
            let centre_no_sip = solution
                .solution
                .as_ref()
                .unwrap()
                .wcs
                .world_2_pix_no_sip(&star_world)
                .unwrap();

            draw_hollow_circle_mut(
                &mut new_img,
                (centre_no_sip.x as i32, centre_no_sip.y as i32),
                ANNOTATED_IMG_MARKER_SRC_RADIUS,
                cyan,
            );
        }
    }

    let crpix = &solution.solution.as_ref().unwrap().wcs.crpix;
    draw_line_segment_mut(
        &mut new_img,
        (crpix.x as f32 - CRPIX_CROSS_SIZE, crpix.y as f32),
        (crpix.x as f32 + CRPIX_CROSS_SIZE, crpix.y as f32),
        magenta,
    );
    draw_line_segment_mut(
        &mut new_img,
        (crpix.x as f32, crpix.y as f32 - CRPIX_CROSS_SIZE),
        (crpix.x as f32, crpix.y as f32 + CRPIX_CROSS_SIZE),
        magenta,
    );

    new_img
}