solvastro 0.1.1

A Rust blind plate solver
Documentation
use fixed::types::extra::{U11, U16};
use fixed::{FixedI16, FixedU16};
use nalgebra::{Vector2, Vector3};
use serde::{Deserialize, Serialize};
use std::f64::consts::PI;

const TWO_PI: f64 = PI + PI;

// Fixed, range -2 >= x < 2
pub type StarAx = FixedI16<U11>;

// Fixed, range -2 >= x < 2
pub type QuadAx = FixedU16<U16>;

#[derive(
    rkyv_08::Archive,
    rkyv_08::Serialize,
    rkyv_08::Deserialize,
    Serialize,
    Deserialize,
    PartialEq,
    Debug,
    Clone,
    Copy,
)]
#[rkyv(crate=rkyv_08)]
pub struct CoordCelestial {
    pub ra: f64,  // radians
    pub dec: f64, // radians
}

pub type CoordCartesian = Vector2<f64>;
pub type CoordUnitSphere = Vector3<f64>;
pub type CoordUnitSphereFP = [StarAx; 3];

impl From<CoordUnitSphere> for CoordCelestial {
    fn from(spherical: CoordUnitSphere) -> Self {
        let ra: f64 = spherical.y.atan2(spherical.x);
        let ra: f64 = if ra < 0f64 { ra + TWO_PI } else { ra };
        let dec: f64 = spherical.z.asin();

        CoordCelestial { ra, dec }
    }
}

impl From<&CoordUnitSphere> for CoordCelestial {
    fn from(spherical: &CoordUnitSphere) -> Self {
        let ra: f64 = spherical.y.atan2(spherical.x);
        let ra: f64 = if ra < 0f64 { ra + TWO_PI } else { ra };
        let dec: f64 = spherical.z.asin();

        CoordCelestial { ra, dec }
    }
}

impl Into<CoordUnitSphere> for CoordCelestial {
    fn into(self) -> CoordUnitSphere {
        CoordUnitSphere::new(
            self.dec.cos() * self.ra.cos(),
            self.dec.cos() * self.ra.sin(),
            self.dec.sin(),
        )
    }
}

pub fn coord_unit_sphere_from_radec(ra: f64, dec: f64) -> CoordUnitSphere {
    (CoordCelestial { ra, dec }).into()
}

pub fn get_midpoint(a: &CoordUnitSphere, b: &CoordUnitSphere) -> CoordUnitSphere {
    (a + b).normalize()

    /*
    // since we are working on a unit sphere, we want to end up with a vector of unit
    // length, not a simple midpoint. So, first we add the two vectors together -
    // this gives us a vector pointing in the right direction, but the wrong length.
    let sum = a + b;

    // now we normalise it to unit length
    let inv_len = 1f64 / (sum.0 * sum.0 + sum.1 * sum.1 + sum.2 * sum.2).sqrt();

    if inv_len == 0f64 {
        None
    } else {
        Some(CoordUnitSphere::new(
            sum.0 * inv_len,
            sum.1 * inv_len,
            sum.2 * inv_len,
        ))
    }*/
}

/**
     s: point to project
     r: reference point of projection (centre / tangent point)
*/
pub fn tan_project(s: &CoordUnitSphere, r: &CoordUnitSphere) -> Option<CoordCartesian> {
    let s_dot_r = s.dot(&r);
    if s_dot_r < 0f64 {
        return None;
    } // on opposite side of sky

    Some(if (r.z - 1f64).abs() < f64::EPSILON {
        CoordCartesian::new(s.x / s.z, s.y / s.z)
    } else if (r.z + 1f64).abs() < f64::EPSILON {
        CoordCartesian::new(-s.x / s.z, s.y / s.z)
    } else {
        // eta is a vector perpendicular to r pointing in the direction
        // of increasing RA.  eta_z = 0 by definition.
        let eta = Vector3::new(-r.y, r.x, 0f64);

        // note this is NOT the same as just normalizing eta - see the y term
        let eta_scale_inv = eta.x * eta.x + (1f64 / (eta.y * eta.y)).sqrt();
        let eta = eta * eta_scale_inv;

        // xi = r cross eta, a vector pointing northwards,
        // in direction of increasing DEC
        let xi = r.cross(&eta);

        // project s-r onto eta and xi. No need to subtract r from s, though,
        // since eta and xi are orthogonal to r by construction.
        // Apply TAN (s_dot_r) scaling
        let inv_s_dot_r = 1f64 / s_dot_r;

        CoordCartesian::new(s.dot(&eta) * inv_s_dot_r, s.dot(&xi) * inv_s_dot_r)
    })
}