solvastro 0.1.1

A Rust blind plate solver
Documentation
use super::coords::{CoordCelestial, CoordUnitSphere, CoordUnitSphereFP, StarAx};
use super::numeric_traits::Flt;
use fixed::traits::ToFixed;
use nalgebra::{ArrayStorage, Vector3};
use serde::de::DeserializeOwned;

pub type StarPoint<F> = [F; 3];

// #[derive(rkyv::Archive,rkyv::Serialize,rkyv::Deserialize)]
// #[derive(Serialize, Deserialize, PartialEq, Debug)]
#[derive(
    rkyv_08::Archive,
    rkyv_08::Serialize,
    rkyv_08::Deserialize,
    serde::Serialize,
    // serde::Deserialize,
    PartialEq,
    Debug,
)]
#[rkyv(crate=rkyv_08)]
pub struct Star<F>
where
    F: Flt + serde::Serialize + DeserializeOwned,
{
    pub pos_celestial: CoordCelestial<F>,
    pub pos_spherical: [[F; 3]; 1],
    pub mag_v: F,
    pub name_common: Option<String>,
    pub identifier: String,
}

impl<F: Flt> Star<F> {
    pub fn new(
        ra: F,
        dec: F,
        mag_v: F,
        name_common: Option<String>,
        identifier: String,
    ) -> Star<F> {
        let pos_celestial = CoordCelestial { ra, dec };
        let pos_spherical: CoordUnitSphere<F> = pos_celestial.clone().into();
        Star {
            pos_celestial,
            pos_spherical: pos_spherical.data.0,
            mag_v,
            name_common,
            identifier,
        }
    }

    pub fn mag(&self) -> F {
        self.mag_v
    }

    pub fn spherical(&self) -> CoordUnitSphere<F> {
        Vector3::from_data(ArrayStorage(self.pos_spherical))
        //&self.pos_spherical
    }
}

impl<F: Flt + ToFixed> Star<F> {
    pub fn spherical_fp(&self) -> CoordUnitSphereFP {
        [
            StarAx::from_num(self.pos_spherical[0][0]),
            StarAx::from_num(self.pos_spherical[0][1]),
            StarAx::from_num(self.pos_spherical[0][2]),
        ]
    }
}

impl<F: Flt> Star<F> {
    pub fn spherical_array(&self) -> StarPoint<F> {
        self.pos_spherical[0]
    }

    pub fn name_common(&self) -> &Option<String> {
        &self.name_common
    }

    pub fn name_or_identifier(&self) -> &String {
        if self.name_common.is_some() {
            &self.name_common.as_ref().unwrap()
        } else {
            &self.identifier
        }
    }

    pub fn dist_to(&self, other: &Star<F>) -> F {
        num_traits::Float::acos(
            Vector3::from_data(ArrayStorage(self.pos_spherical)).dot(&other.spherical()),
        )
        //self.pos_spherical.dot(other.spherical()).acos()
    }

    pub fn dist_sq_to(&self, other: &Star<F>) -> F {
        let dist = self.dist_to(other);
        dist * dist
    }

    // Unused
    pub fn approx_dist_sq_to(&self, other: &Star<F>) -> F {
        (self.pos_spherical[0][0] - other.pos_spherical[0][0])
            * (self.pos_spherical[0][0] - other.pos_spherical[0][0])
            + (self.pos_spherical[0][1] - other.pos_spherical[0][1])
                * (self.pos_spherical[0][1] - other.pos_spherical[0][1])
            + (self.pos_spherical[0][2] - other.pos_spherical[0][2])
                * (self.pos_spherical[0][2] - other.pos_spherical[0][2])
        // (self.pos_spherical.x - other.pos_spherical.x)
        //     * (self.pos_spherical.x - other.pos_spherical.x)
        //     + (self.pos_spherical.y - other.pos_spherical.y)
        //     * (self.pos_spherical.y - other.pos_spherical.y)
        //     + (self.pos_spherical.z - other.pos_spherical.z)
        //     * (self.pos_spherical.z - other.pos_spherical.z)
    }
}