use g_math::fixed_point::imperative::FixedVector;
use crate::FixedPoint;
use g_math::fixed_point::imperative::manifold::{HyperbolicSpace, Manifold};
use crate::core::constants::constant_bank;
use crate::core::wrapping::{wrapping_add, wrapping_from_int, wrapping_mul};
use crate::core::manifold::hyperboloid_lift;
use crate::ToponError;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LabelOrder {
RootLast,
RootFirst,
}
#[derive(Clone, Copy, Debug)]
pub struct WalkLimits {
pub max_depth: usize,
pub radius_budget: FixedPoint,
}
impl Default for WalkLimits {
fn default() -> Self {
WalkLimits {
max_depth: 8,
radius_budget: FixedPoint::from_int(12),
}
}
}
#[derive(Clone)]
pub struct GeodesicWalk {
point: FixedVector<FixedPoint>,
depth: usize,
used_radius: FixedPoint,
}
impl GeodesicWalk {
pub fn point(&self) -> &FixedVector<FixedPoint> {
&self.point
}
pub fn depth(&self) -> usize {
self.depth
}
pub fn used_radius(&self) -> FixedPoint {
self.used_radius
}
}
pub struct GeodesicMixer {
constants: Vec<FixedPoint>,
separator: u8,
order: LabelOrder,
limits: WalkLimits,
}
impl GeodesicMixer {
pub fn new(dimension: usize, separator: u8) -> Result<Self, ToponError> {
Self::with_order_and_limits(dimension, separator, LabelOrder::RootLast, WalkLimits::default())
}
pub fn with_order_and_limits(
dimension: usize,
separator: u8,
order: LabelOrder,
limits: WalkLimits,
) -> Result<Self, ToponError> {
Ok(GeodesicMixer {
constants: constant_bank(dimension)?,
separator,
order,
limits,
})
}
pub fn limits(&self) -> &WalkLimits {
&self.limits
}
fn label_to_direction(
&self,
label: &[u8],
depth: usize,
dim: usize,
) -> Result<Vec<FixedPoint>, ToponError> {
let n = self.constants.len();
let mut direction = vec![FixedPoint::ZERO; dim];
for (pos, &byte) in label.iter().enumerate() {
let byte_fp = wrapping_from_int(byte as i32 + 1);
let pos_fp = wrapping_from_int(pos as i32 + 1);
for (d, component) in direction.iter_mut().enumerate() {
let c = self.constants[(pos + d + depth * 37) % n];
*component = wrapping_add(*component, wrapping_mul(wrapping_mul(byte_fp, c), pos_fp));
}
}
for component in direction.iter_mut() {
*component = component.try_sin()?;
}
Ok(direction)
}
fn build_tangent(
&self,
point: &FixedVector<FixedPoint>,
spatial: &[FixedPoint],
step_size: FixedPoint,
dim: usize,
manifold: &HyperbolicSpace,
) -> FixedVector<FixedPoint> {
let mut spatial_dot = FixedPoint::ZERO;
for d in 0..dim {
spatial_dot += spatial[d] * point[d + 1];
}
let v0 = spatial_dot / point[0];
let mut tangent_components = Vec::with_capacity(dim + 1);
tangent_components.push(v0);
tangent_components.extend_from_slice(spatial);
let tangent = FixedVector::from_slice(&tangent_components);
let norm = manifold.norm(point, &tangent);
if norm.is_zero() {
return tangent;
}
&tangent * (step_size / norm)
}
pub fn walk_origin(&self, dimension: usize) -> GeodesicWalk {
let mut origin = vec![FixedPoint::ZERO; dimension + 1];
origin[0] = FixedPoint::one();
GeodesicWalk {
point: FixedVector::from_slice(&origin),
depth: 0,
used_radius: FixedPoint::ZERO,
}
}
pub fn walk_step(
&self,
walk: &GeodesicWalk,
label: &[u8],
dimension: usize,
) -> Result<GeodesicWalk, ToponError> {
if walk.depth >= self.limits.max_depth {
return Err(ToponError::DepthExceeded {
max_depth: self.limits.max_depth,
});
}
let manifold = HyperbolicSpace { dim: dimension };
let spatial = self.label_to_direction(label, walk.depth, dimension)?;
let mut step_size = FixedPoint::one()
/ FixedPoint::from_int(walk.depth as i32 + 1);
let remaining = self.limits.radius_budget - walk.used_radius;
if remaining <= FixedPoint::ZERO {
return Ok(GeodesicWalk {
point: walk.point.clone(),
depth: walk.depth + 1,
used_radius: walk.used_radius,
});
}
if step_size > remaining {
step_size = remaining;
}
let tangent = self.build_tangent(&walk.point, &spatial, step_size, dimension, &manifold);
let point = manifold.exp_map(&walk.point, &tangent)?;
Ok(GeodesicWalk {
point,
depth: walk.depth + 1,
used_radius: walk.used_radius + step_size,
})
}
pub fn finish(&self, walk: &GeodesicWalk, dimension: usize) -> Result<FixedVector<FixedPoint>, ToponError> {
let spatial: Vec<FixedPoint> = (1..=dimension).map(|d| walk.point[d]).collect();
hyperboloid_lift(&spatial)
}
pub fn mix_hierarchical(
&self,
input: &[u8],
dimension: usize,
) -> Result<FixedVector<FixedPoint>, ToponError> {
let mut labels: Vec<&[u8]> = input.split(|&b| b == self.separator).collect();
if self.order == LabelOrder::RootLast {
labels.reverse();
}
let mut walk = self.walk_origin(dimension);
for label in labels {
if label.is_empty() {
continue;
}
walk = self.walk_step(&walk, label, dimension)?;
}
self.finish(&walk, dimension)
}
}