solvastro 0.1.1

A Rust blind plate solver
Documentation
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use flate2::Compression;
use rayon::prelude::*;
use serde::de::DeserializeOwned;
use solvastro::structs_f64::quad::Quad;
use solvastro::structs_f64::star::Star;
use std::error::Error;
use std::fs::File;
use std::path::Path;
use std::time::Instant;

pub mod csv_record;
pub mod csv_record_hygv3;
pub mod util;
use util::parse_csv_file;

// Squared chord (Euclidean) distance between two points on the unit sphere is
// 2 - 2*cos(theta), where theta is the great-circle angle between them. This
// is strictly monotonic in theta over [0, pi], as is the area of the
// spherical cap subtended by a circle of angular radius theta/2 (the circle
// on which A and B, the widest-separated pair in the quad by construction,
// sit as the diameter). So this proxy preserves the same ordering as true
// subtended area, without needing any trig.
fn area_proxy(star_a: &Star, star_b: &Star) -> f64 {
    star_a.approx_dist_sq_to(star_b)
}

#[cfg(not(tarpaulin_include))]
fn main() -> Result<(), Box<dyn Error>> {
    let start = Instant::now();

    let quads: Vec<Quad> =
        read_from_file("hyg-cg-v2-quad-list.bincode.gz").expect("could not load quads");
    println!("Loaded {} quads in {:?}", quads.len(), start.elapsed());

    let csv_file = File::open("hygdata_v3.csv")?;
    let stars = parse_csv_file(csv_file);
    println!(
        "Loaded {} stars ({:?} elapsed)",
        stars.len(),
        start.elapsed()
    );

    let areas: Vec<f64> = quads
        .par_iter()
        .map(|quad| area_proxy(&stars[quad.a], &stars[quad.b]))
        .collect();
    println!(
        "Computed {} area proxies ({:?} elapsed)",
        areas.len(),
        start.elapsed()
    );

    let min = areas.iter().copied().fold(f64::INFINITY, f64::min);
    let max = areas.iter().copied().fold(f64::NEG_INFINITY, f64::max);
    println!("area proxy min: {:?}, max: {:?}", min, max);

    write_to_file("quad-area-proxy.bincode.gz", &areas)?;
    println!(
        "Wrote quad-area-proxy.bincode.gz ({:?} elapsed)",
        start.elapsed()
    );

    Ok(())
}

fn read_from_file<T: DeserializeOwned>(filename: &str) -> Result<T, Box<dyn Error>> {
    let file = File::open(Path::new(filename))?;
    let decompressor = GzDecoder::new(file);
    Ok(bincode::deserialize_from(decompressor)?)
}

fn write_to_file(filename: &str, areas: &[f64]) -> Result<(), Box<dyn Error>> {
    let file = File::create(filename)?;
    let encoder = GzEncoder::new(file, Compression::default());
    bincode::serialize_into(encoder, areas)?;
    Ok(())
}