use crate::{C_SQUARED, Real, hypot, sqrt};
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "tsify", derive(tsify::Tsify))]
pub struct Position {
pub x: Real,
pub y: Real,
pub z: Real,
}
impl Position {
#[inline]
pub const fn new(x: Real, y: Real, z: Real) -> Position {
Self { x, y, z }
}
pub const ZERO: Self = Self::new(f!(0.0), f!(0.0), f!(0.0));
#[inline]
pub const fn from_au(x: Real, y: Real, z: Real) -> Position {
const AU: Real = f!(1.495978707e11);
Self {
x: x * AU,
y: y * AU,
z: z * AU,
}
}
#[inline]
pub const fn norm(self) -> Real {
hypot(hypot(self.x, self.y), self.z)
}
pub const fn distance_to(self, other: Self) -> Real {
let dx = self.x - other.x;
let dy = self.y - other.y;
let dz = self.z - other.z;
hypot(hypot(dx, dy), dz)
}
#[inline]
pub const fn lerp(self, other: Self, t: Real) -> Position {
Self::new(
self.x * (f!(1.0) - t) + other.x * t,
self.y * (f!(1.0) - t) + other.y * t,
self.z * (f!(1.0) - t) + other.z * t,
)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "tsify", derive(tsify::Tsify))]
pub struct Velocity {
pub vx: Real,
pub vy: Real,
pub vz: Real,
}
impl Velocity {
#[inline]
pub const fn new(vx: Real, vy: Real, vz: Real) -> Velocity {
Self { vx, vy, vz }
}
pub const ZERO: Self = Self::new(f!(0.0), f!(0.0), f!(0.0));
#[inline]
pub const fn from_speed(speed_m_s: Real) -> Velocity {
Self::new(speed_m_s, f!(0.0), f!(0.0))
}
#[inline]
pub const fn norm_squared(self) -> Real {
self.vx * self.vx + self.vy * self.vy + self.vz * self.vz
}
#[inline]
pub const fn speed(self) -> Real {
sqrt(self.norm_squared().max(f!(0.0)))
}
#[inline]
pub const fn beta(self) -> Real {
sqrt((self.norm_squared() / C_SQUARED).max(f!(0.0)))
}
}