solvastro 0.1.1

A Rust blind plate solver
Documentation
use super::numeric_traits::Flt;
use fixed::types::extra::{U11, U16};
use fixed::{FixedI16, FixedU16};
use nalgebra::{ComplexField, Vector2, Vector3};
// 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,
    serde::Serialize,
    PartialEq,
    Debug,
    Clone,
    Copy,
)]
#[rkyv(crate=rkyv_08)]
pub struct CoordCelestial<F>
where
    F: Flt + serde::Serialize + for<'de2> serde::Deserialize<'de2>,
{
    pub ra: F,  // radians
    pub dec: F, // radians
}

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

impl<F: Flt> From<CoordUnitSphere<F>> for CoordCelestial<F> {
    fn from(spherical: CoordUnitSphere<F>) -> Self {
        let ra: F = spherical.y.atan2(spherical.x);
        let ra: F = if ra < F::zero() {
            ra + F::pi() + F::pi()
        } else {
            ra
        };
        let dec: F = num_traits::Float::asin(spherical.z);

        CoordCelestial { ra, dec }
    }
}

impl<F: Flt> From<&CoordUnitSphere<F>> for CoordCelestial<F> {
    fn from(spherical: &CoordUnitSphere<F>) -> Self {
        let ra: F = spherical.y.atan2(spherical.x);
        let ra: F = if ra < F::zero() {
            ra + (F::pi() + F::pi())
        } else {
            ra
        };
        let dec: F = num_traits::Float::asin(spherical.z);

        CoordCelestial { ra, dec }
    }
}

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

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

pub fn get_midpoint<F: Flt + ComplexField>(
    a: &CoordUnitSphere<F>,
    b: &CoordUnitSphere<F>,
) -> CoordUnitSphere<F> {
    (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<F: Flt>(
    s: &CoordUnitSphere<F>,
    r: &CoordUnitSphere<F>,
) -> Option<CoordCartesian<F>> {
    let s_dot_r = s.dot(&r);
    if s_dot_r < F::zero() {
        return None;
    } // on opposite side of sky

    Some(if num_traits::Float::abs(r.z - F::one()) < F::epsilon() {
        CoordCartesian::new(s.x / s.z, s.y / s.z)
    } else if num_traits::Float::abs(r.z + F::one()) < F::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, F::zero());

        // note this is NOT the same as just normalizing eta - see the y term
        let eta_scale_inv = eta.x * eta.x + num_traits::Float::sqrt(F::one() / (eta.y * eta.y));
        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 = F::one() / s_dot_r;

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