use nalgebra::{ArrayStorage, Vector3};
use serde::{Deserialize, Serialize};
use crate::coords::{CoordCelestial, CoordUnitSphere, CoordUnitSphereFP, StarAx};
pub type StarPoint = [f64; 3];
#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct StarOld {
pub pos_celestial: CoordCelestial,
pub pos_spherical: CoordUnitSphere,
pub mag_v: f64,
pub name_common: Option<String>,
pub identifier: String,
}
#[derive(
rkyv_08::Archive,
rkyv_08::Serialize,
rkyv_08::Deserialize,
Serialize,
Deserialize,
PartialEq,
Debug,
Clone,
)]
#[rkyv(crate=rkyv_08)]
pub struct Star {
pub pos_celestial: CoordCelestial,
pub pos_spherical: [[f64; 3]; 1],
pub mag_v: f64,
pub name_common: Option<String>,
pub identifier: String,
}
impl Star {
pub fn new(
ra: f64,
dec: f64,
mag_v: f64,
name_common: Option<String>,
identifier: String,
) -> Star {
let pos_celestial = CoordCelestial { ra, dec };
let pos_spherical: CoordUnitSphere = pos_celestial.into();
Star {
pos_celestial,
pos_spherical: pos_spherical.data.0,
mag_v,
name_common,
identifier,
}
}
pub fn mag(&self) -> f64 {
self.mag_v
}
pub fn spherical(&self) -> CoordUnitSphere {
Vector3::from_data(ArrayStorage(self.pos_spherical))
}
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]),
]
}
pub fn spherical_array(&self) -> StarPoint {
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) -> f64 {
Vector3::from_data(ArrayStorage(self.pos_spherical))
.dot(&other.spherical())
.acos()
}
pub fn dist_sq_to(&self, other: &Star) -> f64 {
let dist = self.dist_to(other);
dist * dist
}
pub fn approx_dist_sq_to(&self, other: &Star) -> f64 {
(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])
}
}
impl ArchivedStar {
pub fn spherical(&self) -> CoordUnitSphere {
Vector3::from_data(ArrayStorage(unsafe {
std::mem::transmute(self.pos_spherical)
}))
}
pub fn name_or_identifier(&self) -> &str {
if self.name_common.is_some() {
&self.name_common.as_ref().unwrap()
} else {
&self.identifier
}
}
}