use g_math::fixed_point::imperative::fused;
use g_math::fixed_point::imperative::manifold::{
EuclideanSpace, HyperbolicSpace, Manifold, Sphere,
};
use g_math::fixed_point::imperative::FixedVector;
use crate::FixedPoint;
use crate::ToponError;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ManifoldType {
Sphere,
Euclidean,
Hyperbolic,
}
pub fn hyperboloid_lift(spatial: &[FixedPoint]) -> Result<FixedVector<FixedPoint>, ToponError> {
let mut components = Vec::with_capacity(spatial.len() + 1);
components.push(FixedPoint::one());
components.extend_from_slice(spatial);
components[0] = fused::sqrt_sum_sq(&components);
Ok(FixedVector::from_slice(&components))
}
pub fn project_to_manifold(
raw: &FixedVector<FixedPoint>,
manifold_type: ManifoldType,
) -> Result<FixedVector<FixedPoint>, ToponError> {
match manifold_type {
ManifoldType::Sphere => {
let norm_sq = raw.length_squared();
if norm_sq.is_zero() {
return Err(ToponError::DegenerateVector);
}
let inv_norm = norm_sq.try_inv_sqrt()?;
Ok(raw.map(|v| v * inv_norm))
}
ManifoldType::Euclidean => Ok(raw.clone()),
ManifoldType::Hyperbolic => {
let spatial: Vec<FixedPoint> = raw.iter().copied().collect();
hyperboloid_lift(&spatial)
}
}
}
pub fn geodesic_distance(
a: &FixedVector<FixedPoint>,
b: &FixedVector<FixedPoint>,
manifold_type: ManifoldType,
) -> Result<FixedPoint, ToponError> {
let d = match manifold_type {
ManifoldType::Sphere => Sphere { dim: a.len().saturating_sub(1) }.distance(a, b)?,
ManifoldType::Euclidean => EuclideanSpace { dim: a.len() }.distance(a, b)?,
ManifoldType::Hyperbolic => {
HyperbolicSpace { dim: a.len().saturating_sub(1) }.distance(a, b)?
}
};
Ok(d)
}
pub fn raw_components(dimension: usize, manifold_type: ManifoldType) -> usize {
match manifold_type {
ManifoldType::Sphere => dimension + 1,
ManifoldType::Euclidean | ManifoldType::Hyperbolic => dimension,
}
}