rust-galmon-web/src/ui/main/world_geo/cartesian.rs

73 lines
1.2 KiB
Rust

use super::RadianPoint;
#[derive(Clone, Copy, Debug)]
pub struct Cartesian {
pub a: f32,
pub b: f32,
pub c: f32,
}
impl Cartesian {
#[allow(unused)]
pub fn normalize(self) -> Self {
let norm = (self * self).sqrt();
self / norm
}
}
impl From<RadianPoint> for Cartesian {
fn from(p: RadianPoint) -> Self {
let cos_phi = p.phi.cos();
Self {
a: cos_phi * p.lambda.cos(),
b: cos_phi * p.lambda.sin(),
c: p.phi.sin(),
}
}
}
impl std::ops::Add for Cartesian {
type Output = Cartesian;
fn add(self, rhs: Cartesian) -> Self::Output {
Cartesian {
a: self.a + rhs.a,
b: self.b + rhs.b,
c: self.c + rhs.c,
}
}
}
// cross product
impl std::ops::BitXor for Cartesian {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self::Output {
Self {
a: self.b * rhs.c - self.c * rhs.b,
b: self.c * rhs.a - self.a * rhs.c,
c: self.a * rhs.b - self.b * rhs.a,
}
}
}
impl std::ops::Mul for Cartesian {
type Output = f32;
fn mul(self, rhs: Cartesian) -> Self::Output {
self.a * rhs.a + self.b * rhs.b + self.c * rhs.c
}
}
impl std::ops::Div<f32> for Cartesian {
type Output = Cartesian;
fn div(self, rhs: f32) -> Self::Output {
Cartesian {
a: self.a / rhs,
b: self.b / rhs,
c: self.c / rhs,
}
}
}