oxihuman-mesh 0.2.1

Mesh processing, topology, and geometry algorithms for OxiHuman
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Geodesic distance computation on mesh surfaces using Dijkstra's algorithm.
//!
//! Edge weights are the Euclidean distances between vertex positions.

use std::collections::BinaryHeap;

use crate::mesh::MeshBuffers;

// ---------------------------------------------------------------------------
// Ordered distance wrapper (min-heap via reverse ordering)
// ---------------------------------------------------------------------------

#[derive(PartialEq)]
struct Dist(f32);

impl Eq for Dist {}

impl Ord for Dist {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Reverse ordering so that BinaryHeap becomes a min-heap.
        other
            .0
            .partial_cmp(&self.0)
            .unwrap_or(std::cmp::Ordering::Equal)
    }
}

impl PartialOrd for Dist {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

// ---------------------------------------------------------------------------
// Public result type
// ---------------------------------------------------------------------------

/// Geodesic distance result from a single source vertex (or multi-source).
#[allow(dead_code)]
pub struct GeodesicResult {
    /// Distance from the source to each vertex. `f32::INFINITY` for unreachable vertices.
    pub distances: Vec<f32>,
    /// Source vertex index (for multi-source, index of the first source).
    pub source: usize,
}

impl GeodesicResult {
    /// Maximum finite distance in the field.
    #[allow(dead_code)]
    pub fn max_distance(&self) -> f32 {
        self.distances
            .iter()
            .copied()
            .filter(|d| d.is_finite())
            .fold(0.0f32, f32::max)
    }

    /// All vertices within a given distance radius (inclusive).
    #[allow(dead_code)]
    pub fn vertices_within(&self, radius: f32) -> Vec<usize> {
        self.distances
            .iter()
            .enumerate()
            .filter(|(_, &d)| d <= radius)
            .map(|(i, _)| i)
            .collect()
    }

    /// Nearest vertex to the source (other than the source itself).
    #[allow(dead_code)]
    pub fn nearest_vertex(&self) -> Option<usize> {
        self.distances
            .iter()
            .enumerate()
            .filter(|&(i, &d)| i != self.source && d.is_finite())
            .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(i, _)| i)
    }

    /// Reconstruct the approximate shortest path from source to target vertex
    /// using the distance field and edge adjacency (greedy descent on distances).
    ///
    /// Returns a list of vertex indices from source to target (inclusive).
    /// If target is unreachable, returns an empty vec.
    #[allow(dead_code)]
    pub fn path_to(&self, target: usize, mesh: &MeshBuffers) -> Vec<usize> {
        if !self.distances[target].is_finite() {
            return Vec::new();
        }
        if target == self.source {
            return vec![self.source];
        }

        let adj = build_adjacency(mesh);
        let mut path = vec![target];
        let mut current = target;

        // Walk backwards along steepest descent in the distance field.
        loop {
            if current == self.source {
                break;
            }
            let best = adj[current]
                .iter()
                .copied()
                .filter(|&nb| self.distances[nb] < self.distances[current])
                .min_by(|&a, &b| {
                    self.distances[a]
                        .partial_cmp(&self.distances[b])
                        .unwrap_or(std::cmp::Ordering::Equal)
                });

            match best {
                Some(prev) => {
                    path.push(prev);
                    current = prev;
                }
                None => break, // should not happen for reachable vertices
            }
        }

        path.reverse();
        path
    }

    /// Normalize distances to [0, 1] range based on the maximum finite distance.
    #[allow(dead_code)]
    pub fn normalized(&self) -> Vec<f32> {
        let max = self.max_distance();
        if max == 0.0 {
            return vec![0.0; self.distances.len()];
        }
        self.distances
            .iter()
            .map(|&d| if d.is_finite() { d / max } else { 1.0 })
            .collect()
    }
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Build an adjacency list (vertex -> list of neighbour vertex indices) from
/// the mesh triangle indices.
fn build_adjacency(mesh: &MeshBuffers) -> Vec<Vec<usize>> {
    let n = mesh.vertex_count();
    let mut adj: Vec<Vec<usize>> = vec![Vec::new(); n];
    let idx = &mesh.indices;
    let face_count = idx.len() / 3;
    for f in 0..face_count {
        let a = idx[f * 3] as usize;
        let b = idx[f * 3 + 1] as usize;
        let c = idx[f * 3 + 2] as usize;
        for (p, q) in [(a, b), (b, a), (b, c), (c, b), (c, a), (a, c)] {
            if !adj[p].contains(&q) {
                adj[p].push(q);
            }
        }
    }
    adj
}

/// Euclidean distance between two vertex positions.
#[inline]
fn edge_weight(mesh: &MeshBuffers, a: usize, b: usize) -> f32 {
    let pa = mesh.positions[a];
    let pb = mesh.positions[b];
    let dx = pa[0] - pb[0];
    let dy = pa[1] - pb[1];
    let dz = pa[2] - pb[2];
    (dx * dx + dy * dy + dz * dz).sqrt()
}

/// Core Dijkstra's algorithm from a set of seed entries `(distance, vertex)`.
fn dijkstra_core(mesh: &MeshBuffers, seeds: Vec<(f32, usize)>, n: usize) -> Vec<f32> {
    let adj = build_adjacency(mesh);
    let mut dist = vec![f32::INFINITY; n];
    let mut heap: BinaryHeap<(Dist, usize)> = BinaryHeap::new();

    for (d, v) in seeds {
        dist[v] = d;
        heap.push((Dist(d), v));
    }

    while let Some((Dist(d), u)) = heap.pop() {
        if d > dist[u] {
            continue; // stale entry
        }
        for &nb in &adj[u] {
            let w = edge_weight(mesh, u, nb);
            let nd = d + w;
            if nd < dist[nb] {
                dist[nb] = nd;
                heap.push((Dist(nd), nb));
            }
        }
    }

    dist
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Compute geodesic distances from a single source vertex using Dijkstra's
/// algorithm. Edge weights are the Euclidean distances between vertex positions.
#[allow(dead_code)]
pub fn geodesic_from_vertex(mesh: &MeshBuffers, source: usize) -> GeodesicResult {
    let n = mesh.vertex_count();
    let distances = dijkstra_core(mesh, vec![(0.0, source)], n);
    GeodesicResult { distances, source }
}

/// Compute geodesic distances from multiple source vertices (multi-source
/// Dijkstra). Each vertex gets the distance to its nearest source.
///
/// The `source` field in the result is set to `sources[0]` (or `0` if the
/// slice is empty).
#[allow(dead_code)]
pub fn geodesic_from_vertices(mesh: &MeshBuffers, sources: &[usize]) -> GeodesicResult {
    let n = mesh.vertex_count();
    let seeds: Vec<(f32, usize)> = sources.iter().map(|&v| (0.0, v)).collect();
    let distances = dijkstra_core(mesh, seeds, n);
    let source = sources.first().copied().unwrap_or(0);
    GeodesicResult { distances, source }
}

/// Compute all-pairs shortest geodesic distances.
///
/// Returns a `Vec<GeodesicResult>`, one per vertex.
///
/// **WARNING**: O(V * (V + E) log V) — only call for small meshes (< 1000
/// vertices).
#[allow(dead_code)]
pub fn geodesic_all_pairs(mesh: &MeshBuffers) -> Vec<GeodesicResult> {
    let n = mesh.vertex_count();
    (0..n).map(|v| geodesic_from_vertex(mesh, v)).collect()
}

/// Find the diameter of the mesh: the pair of vertices with maximum geodesic
/// distance. Returns `(vertex_a, vertex_b, distance)`.
///
/// Returns `(0, 0, 0.0)` for meshes with fewer than 2 vertices.
#[allow(dead_code)]
pub fn mesh_diameter(mesh: &MeshBuffers) -> (usize, usize, f32) {
    let n = mesh.vertex_count();
    if n < 2 {
        return (0, 0, 0.0);
    }

    let all = geodesic_all_pairs(mesh);
    let mut best = (0usize, 0usize, 0.0f32);
    for (a, result) in all.iter().enumerate() {
        for (b, &d) in result.distances.iter().enumerate() {
            if b != a && d.is_finite() && d > best.2 {
                best = (a, b, d);
            }
        }
    }
    best
}

/// Compute a geodesic heat map: normalizes distances to [0, 1] for
/// visualization, mapping:
/// - near (0.0) → blue  `[0, 0, 255]`
/// - mid  (0.5) → green `[0, 255, 0]`
/// - far  (1.0) → red   `[255, 0, 0]`
#[allow(dead_code)]
pub fn geodesic_heat_map(result: &GeodesicResult) -> Vec<[u8; 3]> {
    let normalized = result.normalized();
    normalized
        .iter()
        .map(|&t| {
            // t in [0, 1]
            // Blue -> Green in first half; Green -> Red in second half.
            if t <= 0.5 {
                let s = t * 2.0; // 0..1 over blue->green
                let r = 0u8;
                let g = (s * 255.0).round() as u8;
                let b = ((1.0 - s) * 255.0).round() as u8;
                [r, g, b]
            } else {
                let s = (t - 0.5) * 2.0; // 0..1 over green->red
                let r = (s * 255.0).round() as u8;
                let g = ((1.0 - s) * 255.0).round() as u8;
                let b = 0u8;
                [r, g, b]
            }
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use oxihuman_morph::engine::MeshBuffers as MB;

    /// Helper: build a MeshBuffers from raw positions and indices.
    fn make_mesh(positions: Vec<[f32; 3]>, indices: Vec<u32>) -> MeshBuffers {
        let n = positions.len();
        MeshBuffers::from_morph(MB {
            positions,
            normals: vec![[0.0, 0.0, 1.0]; n],
            uvs: vec![[0.0, 0.0]; n],
            indices,
            has_suit: false,
        })
    }

    /// Single triangle with vertices at (0,0,0), (1,0,0), (0,1,0).
    fn single_triangle() -> MeshBuffers {
        make_mesh(
            vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
            vec![0, 1, 2],
        )
    }

    /// Two triangles sharing edge (1,2):
    ///   verts: (0,0,0), (1,0,0), (0,1,0), (1,1,0)
    ///   faces: 0-1-2 and 1-3-2
    fn two_triangles() -> MeshBuffers {
        make_mesh(
            vec![
                [0.0, 0.0, 0.0],
                [1.0, 0.0, 0.0],
                [0.0, 1.0, 0.0],
                [1.0, 1.0, 0.0],
            ],
            vec![0, 1, 2, 1, 3, 2],
        )
    }

    /// Disconnected mesh: two separate triangles (verts 0-2 and 3-5).
    fn disconnected_mesh() -> MeshBuffers {
        make_mesh(
            vec![
                [0.0, 0.0, 0.0],
                [1.0, 0.0, 0.0],
                [0.0, 1.0, 0.0],
                [10.0, 0.0, 0.0],
                [11.0, 0.0, 0.0],
                [10.0, 1.0, 0.0],
            ],
            vec![0, 1, 2, 3, 4, 5],
        )
    }

    // -----------------------------------------------------------------------

    #[test]
    fn geodesic_single_triangle_source_zero() {
        let mesh = single_triangle();
        let result = geodesic_from_vertex(&mesh, 0);
        assert_eq!(result.source, 0);
        assert_eq!(result.distances[0], 0.0);
    }

    #[test]
    fn geodesic_single_triangle_distances_positive() {
        let mesh = single_triangle();
        let result = geodesic_from_vertex(&mesh, 0);
        // Distances to vertices 1 and 2 must be strictly positive.
        assert!(result.distances[1] > 0.0);
        assert!(result.distances[2] > 0.0);
        // Distance to vertex 1 should be 1.0 (along x-axis).
        assert!((result.distances[1] - 1.0).abs() < 1e-5);
        // Distance to vertex 2 should be 1.0 (along y-axis).
        assert!((result.distances[2] - 1.0).abs() < 1e-5);
    }

    #[test]
    fn geodesic_from_vertices_multi_source_correct() {
        let mesh = two_triangles();
        // Sources at vertices 0 and 3 (opposite corners of the quad).
        let result = geodesic_from_vertices(&mesh, &[0, 3]);
        // Vertex 0 is a source: distance = 0.
        assert_eq!(result.distances[0], 0.0);
        // Vertex 3 is a source: distance = 0.
        assert_eq!(result.distances[3], 0.0);
        // Vertices 1 and 2 should be at most distance 1 from their nearest source.
        assert!(result.distances[1].is_finite());
        assert!(result.distances[2].is_finite());
        assert!(result.distances[1] <= 1.0 + 1e-5);
        assert!(result.distances[2] <= 1.0 + 1e-5);
    }

    #[test]
    fn geodesic_max_distance_positive() {
        let mesh = single_triangle();
        let result = geodesic_from_vertex(&mesh, 0);
        let max = result.max_distance();
        assert!(max > 0.0);
    }

    #[test]
    fn geodesic_vertices_within_radius() {
        let mesh = single_triangle();
        let result = geodesic_from_vertex(&mesh, 0);
        // All vertices are within distance 2.0.
        let within2 = result.vertices_within(2.0);
        assert_eq!(within2.len(), 3);
        // Only source is within distance 0.5.
        let within_half = result.vertices_within(0.5);
        assert_eq!(within_half, vec![0]);
    }

    #[test]
    fn geodesic_nearest_vertex_is_adjacent() {
        let mesh = single_triangle();
        let result = geodesic_from_vertex(&mesh, 0);
        let nearest = result.nearest_vertex();
        // Nearest should be vertex 1 or 2 (both at distance 1.0).
        assert!(nearest == Some(1) || nearest == Some(2));
    }

    #[test]
    fn geodesic_normalized_range_0_1() {
        let mesh = two_triangles();
        let result = geodesic_from_vertex(&mesh, 0);
        let norm = result.normalized();
        for &v in &norm {
            assert!((0.0 - 1e-6..=1.0 + 1e-6).contains(&v), "out of range: {v}");
        }
        // Source vertex should have normalized distance 0.
        assert!((norm[0]).abs() < 1e-6);
    }

    #[test]
    fn geodesic_path_to_adjacent_vertex() {
        let mesh = single_triangle();
        let result = geodesic_from_vertex(&mesh, 0);
        let path = result.path_to(1, &mesh);
        // Shortest path from 0 to 1 is [0, 1].
        assert_eq!(path, vec![0, 1]);
    }

    #[test]
    fn geodesic_path_to_same_vertex_length_one() {
        let mesh = single_triangle();
        let result = geodesic_from_vertex(&mesh, 0);
        let path = result.path_to(0, &mesh);
        assert_eq!(path.len(), 1);
        assert_eq!(path[0], 0);
    }

    #[test]
    fn mesh_diameter_returns_positive_distance() {
        let mesh = two_triangles();
        let (a, b, dist) = mesh_diameter(&mesh);
        assert!(dist > 0.0, "diameter must be positive");
        assert_ne!(a, b, "diameter endpoints must differ");
    }

    #[test]
    fn geodesic_heat_map_length_matches_vertices() {
        let mesh = two_triangles();
        let result = geodesic_from_vertex(&mesh, 0);
        let hmap = geodesic_heat_map(&result);
        assert_eq!(hmap.len(), mesh.vertex_count());
    }

    #[test]
    fn geodesic_all_pairs_small_mesh() {
        let mesh = single_triangle();
        let all = geodesic_all_pairs(&mesh);
        assert_eq!(all.len(), 3);
        // Each result should have source == its vertex index.
        for (i, res) in all.iter().enumerate() {
            assert_eq!(res.source, i);
            assert_eq!(res.distances[i], 0.0);
        }
        // Distance matrix should be symmetric (within floating-point tolerance).
        for i in 0..3 {
            for j in 0..3 {
                let d_ij = all[i].distances[j];
                let d_ji = all[j].distances[i];
                assert!(
                    (d_ij - d_ji).abs() < 1e-5,
                    "asymmetric: d[{i}][{j}]={d_ij}, d[{j}][{i}]={d_ji}"
                );
            }
        }
    }

    #[test]
    fn geodesic_unreachable_vertex_is_infinity() {
        let mesh = disconnected_mesh();
        let result = geodesic_from_vertex(&mesh, 0);
        // Vertices 3, 4, 5 are in a separate component: must be unreachable.
        assert_eq!(result.distances[3], f32::INFINITY);
        assert_eq!(result.distances[4], f32::INFINITY);
        assert_eq!(result.distances[5], f32::INFINITY);
        // Vertices 0, 1, 2 must be finite.
        assert!(result.distances[0].is_finite());
        assert!(result.distances[1].is_finite());
        assert!(result.distances[2].is_finite());
    }
}