Skip to main content

qualia_core_db/poet_host/invoke/manifold/
distance.rs

1//! 10-D tensor distance — the geometric kernel behind 3D/4D manifolds.
2
3use super::super::args;
4use crate::tensor::Tensor10D;
5use vibe::{Diagnostic, Span, Value};
6
7/// VibeScript seam: manifold distance.
8pub fn distance(args_v: &Value, span: Span) -> Result<Value, Diagnostic> {
9    let a = tensor(args_v, "a", span)?;
10    let b = tensor(args_v, "b", span)?;
11    Ok(Value::F64(a.full_distance(&b) as f64))
12}
13
14/// Builds `?` from `list`.
15pub fn from_list(xs: &[f64]) -> Option<Tensor10D> {
16    if xs.len() != 10 {
17        return None;
18    }
19    Some(Tensor10D::new(
20        xs[0] as f32,
21        xs[1] as f32,
22        xs[2] as f32,
23        xs[3] as f32,
24        xs[4] as f32,
25        xs[5] as f32,
26        xs[6] as f32,
27        xs[7] as f32,
28        xs[8] as f32,
29        xs[9] as f32,
30    ))
31}
32
33fn tensor(args_v: &Value, key: &str, span: Span) -> Result<Tensor10D, Diagnostic> {
34    let xs = args::rec(args_v, key)
35        .and_then(args::f64s)
36        .ok_or_else(|| args::bad(span, format!("{key} needs [q,v,w,x,y,z,t,α,μ,σ]")))?;
37    from_list(&xs).ok_or_else(|| args::bad(span, format!("{key} needs 10 numbers")))
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use std::collections::BTreeMap;
44
45    #[test]
46    fn origin_to_unit_x() {
47        let mut m = BTreeMap::new();
48        m.insert(
49            "a".into(),
50            args::f64_list_value([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0]),
51        );
52        m.insert(
53            "b".into(),
54            args::f64_list_value([0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0]),
55        );
56        match distance(&Value::Record(m), Span { start: 0, end: 0 }).unwrap() {
57            Value::F64(d) => assert!((d - 1.0).abs() < 1e-5),
58            other => panic!("{other:?}"),
59        }
60    }
61}