proof-engine 0.1.1

A mathematical rendering engine for Rust. Every visual is the output of a mathematical function.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// topology/geodesic.rs — Geodesic pathfinding on various surfaces

use glam::Vec3;
use std::f32::consts::PI;

// ─── Surface Type ──────────────────────────────────────────────────────────

/// The type of surface on which to compute geodesics.
pub enum GeodesicSurface {
    Plane,
    Sphere { radius: f32 },
    Torus { major_r: f32, minor_r: f32 },
    Hyperbolic,
    /// Custom surface defined by a parametric function (u, v) -> Vec3
    /// and its partial derivatives.
    Custom {
        surface_fn: Box<dyn Fn(f32, f32) -> Vec3>,
    },
}

/// Compute a geodesic path on a given surface between two points.
/// Returns `steps` evenly spaced points along the geodesic.
pub fn geodesic_on_surface(surface: &GeodesicSurface, start: Vec3, end: Vec3, steps: usize) -> Vec<Vec3> {
    match surface {
        GeodesicSurface::Plane => geodesic_plane(start, end, steps),
        GeodesicSurface::Sphere { radius } => shortest_path_sphere_pts(start, end, *radius, steps),
        GeodesicSurface::Torus { major_r, minor_r } => {
            shortest_path_torus_pts(start, end, *major_r, *minor_r, steps)
        }
        GeodesicSurface::Hyperbolic => geodesic_hyperbolic(start, end, steps),
        GeodesicSurface::Custom { surface_fn } => geodesic_custom(surface_fn, start, end, steps),
    }
}

fn geodesic_plane(start: Vec3, end: Vec3, steps: usize) -> Vec<Vec3> {
    if steps < 2 {
        return vec![start, end];
    }
    (0..steps)
        .map(|i| {
            let t = i as f32 / (steps - 1) as f32;
            start.lerp(end, t)
        })
        .collect()
}

// ─── Sphere Geodesic ───────────────────────────────────────────────────────

/// Shortest path on a sphere (great circle arc).
pub fn shortest_path_sphere(a: Vec3, b: Vec3, steps: usize) -> Vec<Vec3> {
    let r = a.length();
    shortest_path_sphere_pts(a, b, r, steps)
}

fn shortest_path_sphere_pts(a: Vec3, b: Vec3, radius: f32, steps: usize) -> Vec<Vec3> {
    if steps < 2 {
        return vec![a, b];
    }
    let na = a.normalize();
    let nb = b.normalize();
    let dot = na.dot(nb).clamp(-1.0, 1.0);
    let omega = dot.acos();

    if omega.abs() < 1e-8 {
        return vec![a; steps];
    }

    let sin_omega = omega.sin();
    (0..steps)
        .map(|i| {
            let t = i as f32 / (steps - 1) as f32;
            let a_coeff = ((1.0 - t) * omega).sin() / sin_omega;
            let b_coeff = (t * omega).sin() / sin_omega;
            (na * a_coeff + nb * b_coeff) * radius
        })
        .collect()
}

// ─── Torus Geodesic ────────────────────────────────────────────────────────

/// Shortest path on a torus (approximate via parameter-space interpolation).
pub fn shortest_path_torus(a: Vec3, b: Vec3, major_r: f32, minor_r: f32, steps: usize) -> Vec<Vec3> {
    shortest_path_torus_pts(a, b, major_r, minor_r, steps)
}

fn shortest_path_torus_pts(a: Vec3, b: Vec3, major_r: f32, minor_r: f32, steps: usize) -> Vec<Vec3> {
    if steps < 2 {
        return vec![a, b];
    }

    // Convert to torus parameters (u, v)
    let (u_a, v_a) = cartesian_to_torus_params(a, major_r, minor_r);
    let (u_b, v_b) = cartesian_to_torus_params(b, major_r, minor_r);

    // Interpolate in parameter space (taking shortest path around each circle)
    let du = shortest_angle_diff(u_a, u_b);
    let dv = shortest_angle_diff(v_a, v_b);

    (0..steps)
        .map(|i| {
            let t = i as f32 / (steps - 1) as f32;
            let u = u_a + du * t;
            let v = v_a + dv * t;
            torus_point(major_r, minor_r, u, v)
        })
        .collect()
}

fn cartesian_to_torus_params(p: Vec3, major_r: f32, _minor_r: f32) -> (f32, f32) {
    let u = p.y.atan2(p.x);
    let center_x = major_r * u.cos();
    let center_y = major_r * u.sin();
    let dx = p.x - center_x;
    let dy = p.y - center_y;
    let dz = p.z;
    let r_in_plane = (dx * dx + dy * dy).sqrt();
    let v = dz.atan2(r_in_plane - 0.0); // approximate
    (u, v)
}

fn torus_point(major_r: f32, minor_r: f32, u: f32, v: f32) -> Vec3 {
    Vec3::new(
        (major_r + minor_r * v.cos()) * u.cos(),
        (major_r + minor_r * v.cos()) * u.sin(),
        minor_r * v.sin(),
    )
}

fn shortest_angle_diff(from: f32, to: f32) -> f32 {
    let mut diff = to - from;
    while diff > PI {
        diff -= 2.0 * PI;
    }
    while diff < -PI {
        diff += 2.0 * PI;
    }
    diff
}

// ─── Hyperbolic Geodesic (in 3D embedding) ─────────────────────────────────

fn geodesic_hyperbolic(start: Vec3, end: Vec3, steps: usize) -> Vec<Vec3> {
    // Use the hyperboloid model: geodesics are intersections of the hyperboloid with planes
    // through the origin.
    // For simplicity, use Minkowski-space slerp.
    if steps < 2 {
        return vec![start, end];
    }

    // Minkowski inner product: -x0*y0 + x1*y1 + x2*y2
    let minkowski_dot = |a: Vec3, b: Vec3| -> f32 { -a.x * b.x + a.y * b.y + a.z * b.z };

    let dot = -minkowski_dot(start, end).max(1.0);
    let dist = acosh(dot);

    if dist.abs() < 1e-8 {
        return vec![start; steps];
    }

    let sinh_dist = dist.sinh();
    (0..steps)
        .map(|i| {
            let t = i as f32 / (steps - 1) as f32;
            let a_coeff = ((1.0 - t) * dist).sinh() / sinh_dist;
            let b_coeff = (t * dist).sinh() / sinh_dist;
            start * a_coeff + end * b_coeff
        })
        .collect()
}

fn acosh(x: f32) -> f32 {
    (x + (x * x - 1.0).max(0.0).sqrt()).ln()
}

// ─── Custom Surface Geodesic ───────────────────────────────────────────────

fn geodesic_custom(
    _surface_fn: &dyn Fn(f32, f32) -> Vec3,
    start: Vec3,
    end: Vec3,
    steps: usize,
) -> Vec<Vec3> {
    // For a general surface, approximate using iterative projection.
    // Start with a straight line and project each point onto the surface.
    // This is a simple relaxation approach.
    if steps < 2 {
        return vec![start, end];
    }

    // Simple linear interpolation as a baseline (exact geodesic on custom
    // surfaces requires solving the geodesic equation numerically).
    let mut path: Vec<Vec3> = (0..steps)
        .map(|i| {
            let t = i as f32 / (steps - 1) as f32;
            start.lerp(end, t)
        })
        .collect();

    // Smoothing iterations: pull midpoints toward the surface
    for _iter in 0..10 {
        let old = path.clone();
        for i in 1..(steps - 1) {
            let mid = (old[i - 1] + old[i + 1]) * 0.5;
            path[i] = mid;
        }
    }

    path
}

// ─── Geodesic Curvature ────────────────────────────────────────────────────

/// Compute the geodesic curvature along a discrete path.
/// Returns a curvature value for each interior point.
pub fn geodesic_curvature(path: &[Vec3]) -> Vec<f32> {
    if path.len() < 3 {
        return vec![];
    }

    let mut curvatures = Vec::with_capacity(path.len() - 2);
    for i in 1..(path.len() - 1) {
        let prev = path[i - 1];
        let curr = path[i];
        let next = path[i + 1];

        let d1 = curr - prev;
        let d2 = next - curr;
        let l1 = d1.length();
        let l2 = d2.length();

        if l1 < 1e-10 || l2 < 1e-10 {
            curvatures.push(0.0);
            continue;
        }

        let t1 = d1 / l1;
        let t2 = d2 / l2;
        let dt = t2 - t1;
        let ds = (l1 + l2) / 2.0;
        let kappa = dt.length() / ds;
        curvatures.push(kappa);
    }
    curvatures
}

// ─── Parallel Transport ────────────────────────────────────────────────────

/// Transport a vector along a geodesic path using Schild's ladder approximation.
/// Returns the transported vector at each point along the path.
pub fn parallel_transport(vector: Vec3, path: &[Vec3]) -> Vec<Vec3> {
    if path.is_empty() {
        return vec![];
    }
    if path.len() == 1 {
        return vec![vector];
    }

    let mut result = Vec::with_capacity(path.len());
    let mut v = vector;
    result.push(v);

    for i in 1..path.len() {
        let tangent = (path[i] - path[i - 1]).normalize_or_zero();
        // Project out the tangent component to keep the vector "parallel"
        // on a surface, the transported vector should remain tangent to the surface.
        // Simple approximation: remove the component along the change in tangent direction.
        if i >= 2 {
            let prev_tangent = (path[i - 1] - path[i - 2]).normalize_or_zero();
            let tangent_change = tangent - prev_tangent;
            // Remove the component of v along the tangent change
            let tc_len_sq = tangent_change.length_squared();
            if tc_len_sq > 1e-10 {
                v = v - tangent_change * (v.dot(tangent_change) / tc_len_sq);
            }
        }

        // Ensure the transported vector maintains its length
        let orig_len = vector.length();
        let curr_len = v.length();
        if curr_len > 1e-10 {
            v = v * (orig_len / curr_len);
        }

        result.push(v);
    }

    result
}

// ─── Tests ─────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_plane_geodesic_is_straight() {
        let start = Vec3::new(0.0, 0.0, 0.0);
        let end = Vec3::new(10.0, 0.0, 0.0);
        let path = geodesic_on_surface(&GeodesicSurface::Plane, start, end, 11);
        assert_eq!(path.len(), 11);
        for (i, p) in path.iter().enumerate() {
            let expected_x = i as f32;
            assert!((p.x - expected_x).abs() < 1e-4);
            assert!(p.y.abs() < 1e-4);
            assert!(p.z.abs() < 1e-4);
        }
    }

    #[test]
    fn test_sphere_geodesic_on_sphere() {
        let r = 5.0;
        let a = Vec3::new(r, 0.0, 0.0);
        let b = Vec3::new(0.0, r, 0.0);
        let path = shortest_path_sphere(a, b, 20);
        for p in &path {
            assert!((p.length() - r).abs() < 0.01, "Point not on sphere: len={}", p.length());
        }
    }

    #[test]
    fn test_sphere_geodesic_endpoints() {
        let a = Vec3::new(1.0, 0.0, 0.0);
        let b = Vec3::new(0.0, 0.0, 1.0);
        let path = shortest_path_sphere(a, b, 10);
        assert!((path[0] - a).length() < 1e-4);
        assert!((path[9] - b).length() < 1e-4);
    }

    #[test]
    fn test_torus_geodesic() {
        let a = torus_point(3.0, 1.0, 0.0, 0.0);
        let b = torus_point(3.0, 1.0, PI / 2.0, 0.0);
        let path = shortest_path_torus(a, b, 3.0, 1.0, 20);
        assert_eq!(path.len(), 20);
        // All points should be approximately on the torus
        for p in &path {
            let xy_dist = (p.x * p.x + p.y * p.y).sqrt();
            assert!(xy_dist > 1.5 && xy_dist < 4.5, "Point not near torus: {}", xy_dist);
        }
    }

    #[test]
    fn test_geodesic_curvature_straight_line() {
        let path: Vec<Vec3> = (0..10)
            .map(|i| Vec3::new(i as f32, 0.0, 0.0))
            .collect();
        let curvatures = geodesic_curvature(&path);
        for k in &curvatures {
            assert!(k.abs() < 1e-4, "Straight line should have zero curvature, got {}", k);
        }
    }

    #[test]
    fn test_geodesic_curvature_circle() {
        let n = 100;
        let path: Vec<Vec3> = (0..n)
            .map(|i| {
                let t = 2.0 * PI * i as f32 / n as f32;
                Vec3::new(t.cos(), t.sin(), 0.0)
            })
            .collect();
        let curvatures = geodesic_curvature(&path);
        // Curvature of a unit circle should be approximately 1
        for k in &curvatures {
            assert!((k - 1.0).abs() < 0.2, "Circle curvature should be ~1, got {}", k);
        }
    }

    #[test]
    fn test_parallel_transport_straight() {
        let path: Vec<Vec3> = (0..5)
            .map(|i| Vec3::new(i as f32, 0.0, 0.0))
            .collect();
        let v = Vec3::new(0.0, 1.0, 0.0);
        let transported = parallel_transport(v, &path);
        assert_eq!(transported.len(), 5);
        // Along a straight line, the vector should remain constant
        for tv in &transported {
            assert!((tv.y - 1.0).abs() < 1e-3, "Transport should preserve vector: {:?}", tv);
        }
    }

    #[test]
    fn test_parallel_transport_preserves_length() {
        let n = 50;
        let path: Vec<Vec3> = (0..n)
            .map(|i| {
                let t = PI * i as f32 / n as f32;
                Vec3::new(t.cos(), t.sin(), 0.0)
            })
            .collect();
        let v = Vec3::new(0.0, 0.0, 1.0);
        let transported = parallel_transport(v, &path);
        let orig_len = v.length();
        for tv in &transported {
            assert!((tv.length() - orig_len).abs() < 0.1, "Length not preserved: {}", tv.length());
        }
    }

    #[test]
    fn test_hyperbolic_geodesic() {
        // On the hyperboloid x^2 = 1 + y^2 + z^2
        let a = Vec3::new(1.0, 0.0, 0.0); // on hyperboloid
        let b = Vec3::new((1.0 + 1.0_f32).sqrt(), 1.0, 0.0);
        let path = geodesic_on_surface(&GeodesicSurface::Hyperbolic, a, b, 10);
        assert_eq!(path.len(), 10);
    }

    #[test]
    fn test_shortest_angle_diff() {
        assert!((shortest_angle_diff(0.1, 0.3) - 0.2).abs() < 1e-4);
        // Wrapping case
        let d = shortest_angle_diff(3.0, -3.0);
        assert!(d.abs() < PI + 0.1);
    }
}