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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;

use crate::mesh::MeshBuffers;

// ---------------------------------------------------------------------------
// Public data types
// ---------------------------------------------------------------------------

/// Statistics about mesh connectivity.
#[derive(Debug, Clone)]
pub struct ConnectivityStats {
    pub vertex_count: usize,
    pub face_count: usize,
    pub edge_count: usize,
    pub boundary_edge_count: usize,
    /// Edges shared by exactly 2 faces.
    pub manifold_edge_count: usize,
    /// Edges shared by 3 or more faces.
    pub non_manifold_edge_count: usize,
    pub connected_component_count: usize,
    /// V - E + F
    pub euler_characteristic: i64,
    /// No boundary edges.
    pub is_closed: bool,
    /// No non-manifold edges.
    pub is_manifold: bool,
}

// ---------------------------------------------------------------------------
// Internal union-find (disjoint set union)
// ---------------------------------------------------------------------------

struct Dsu {
    parent: Vec<usize>,
    rank: Vec<usize>,
}

impl Dsu {
    fn new(n: usize) -> Self {
        Dsu {
            parent: (0..n).collect(),
            rank: vec![0; n],
        }
    }

    fn find(&mut self, mut x: usize) -> usize {
        while self.parent[x] != x {
            self.parent[x] = self.parent[self.parent[x]]; // path halving
            x = self.parent[x];
        }
        x
    }

    fn union(&mut self, a: usize, b: usize) {
        let ra = self.find(a);
        let rb = self.find(b);
        if ra == rb {
            return;
        }
        match self.rank[ra].cmp(&self.rank[rb]) {
            std::cmp::Ordering::Less => self.parent[ra] = rb,
            std::cmp::Ordering::Greater => self.parent[rb] = ra,
            std::cmp::Ordering::Equal => {
                self.parent[rb] = ra;
                self.rank[ra] += 1;
            }
        }
    }
}

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

/// Build a map from canonical edge (min, max) -> face-reference count.
fn build_edge_map(mesh: &MeshBuffers) -> HashMap<(u32, u32), u32> {
    let mut map: HashMap<(u32, u32), u32> = HashMap::new();
    let indices = &mesh.indices;
    let face_count = indices.len() / 3;
    for f in 0..face_count {
        let a = indices[f * 3];
        let b = indices[f * 3 + 1];
        let c = indices[f * 3 + 2];
        for &(p, q) in &[(a, b), (b, c), (c, a)] {
            let key = (p.min(q), p.max(q));
            *map.entry(key).or_insert(0) += 1;
        }
    }
    map
}

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

/// Compute connectivity statistics for a mesh.
#[allow(dead_code)]
pub fn connectivity_stats(mesh: &MeshBuffers) -> ConnectivityStats {
    let vertex_count = mesh.vertex_count();
    let face_count = mesh.face_count();

    let edge_map = build_edge_map(mesh);
    let edge_count = edge_map.len();

    let mut boundary_edge_count = 0usize;
    let mut manifold_edge_count = 0usize;
    let mut non_manifold_edge_count = 0usize;

    for &count in edge_map.values() {
        match count {
            1 => boundary_edge_count += 1,
            2 => manifold_edge_count += 1,
            _ => non_manifold_edge_count += 1,
        }
    }

    let connected_component_count = connected_component_count(mesh);
    let euler_characteristic = vertex_count as i64 - edge_count as i64 + face_count as i64;
    let is_closed = boundary_edge_count == 0;
    let is_manifold = non_manifold_edge_count == 0;

    ConnectivityStats {
        vertex_count,
        face_count,
        edge_count,
        boundary_edge_count,
        manifold_edge_count,
        non_manifold_edge_count,
        connected_component_count,
        euler_characteristic,
        is_closed,
        is_manifold,
    }
}

/// Find all connected components using union-find with path compression.
///
/// Returns a Vec where `component_id[vertex]` is the component index (0-based,
/// densely packed).
#[allow(dead_code)]
pub fn find_connected_components(mesh: &MeshBuffers) -> Vec<usize> {
    let n = mesh.vertex_count();
    if n == 0 {
        return Vec::new();
    }

    let mut dsu = Dsu::new(n);
    let indices = &mesh.indices;
    let face_count = indices.len() / 3;

    for f in 0..face_count {
        let a = indices[f * 3] as usize;
        let b = indices[f * 3 + 1] as usize;
        let c = indices[f * 3 + 2] as usize;
        dsu.union(a, b);
        dsu.union(b, c);
    }

    // Compress root ids to a dense range.
    let mut root_to_id: HashMap<usize, usize> = HashMap::new();
    let mut next_id = 0usize;
    let mut component_id = vec![0usize; n];

    for (v, slot) in component_id.iter_mut().enumerate() {
        let root = dsu.find(v);
        let id = root_to_id.entry(root).or_insert_with(|| {
            let id = next_id;
            next_id += 1;
            id
        });
        *slot = *id;
    }

    component_id
}

/// Count the number of connected components.
#[allow(dead_code)]
pub fn connected_component_count(mesh: &MeshBuffers) -> usize {
    let ids = find_connected_components(mesh);
    if ids.is_empty() {
        return 0;
    }
    let max_id = ids.iter().copied().max().unwrap_or(0);
    max_id + 1
}

/// Find boundary edges (edges belonging to exactly 1 face).
///
/// Returns `Vec<(vertex_a, vertex_b)>` in canonical (min, max) order.
#[allow(dead_code)]
pub fn find_boundary_edges(mesh: &MeshBuffers) -> Vec<(u32, u32)> {
    let edge_map = build_edge_map(mesh);
    edge_map
        .into_iter()
        .filter(|&(_, count)| count == 1)
        .map(|(edge, _)| edge)
        .collect()
}

/// Find boundary loops: sequences of connected boundary edges forming closed loops.
///
/// Returns `Vec<Vec<u32>>` where each inner Vec is a sequence of vertex indices
/// that forms a closed loop (last vertex connects back to first).
#[allow(dead_code)]
pub fn find_boundary_loops(mesh: &MeshBuffers) -> Vec<Vec<u32>> {
    let boundary = find_boundary_edges(mesh);
    if boundary.is_empty() {
        return Vec::new();
    }

    // Build adjacency: vertex -> list of boundary neighbours.
    let mut adj: HashMap<u32, Vec<u32>> = HashMap::new();
    for (a, b) in &boundary {
        adj.entry(*a).or_default().push(*b);
        adj.entry(*b).or_default().push(*a);
    }

    let mut visited_edges: HashMap<(u32, u32), bool> = HashMap::new();
    for &(a, b) in &boundary {
        visited_edges.insert((a, b), false);
        visited_edges.insert((b, a), false);
    }

    let mut loops: Vec<Vec<u32>> = Vec::new();

    // Walk from each unvisited boundary vertex.
    let mut visited_verts: HashMap<u32, bool> = boundary
        .iter()
        .flat_map(|&(a, b)| [(a, false), (b, false)])
        .collect();

    for start in boundary
        .iter()
        .flat_map(|&(a, b)| [a, b])
        .collect::<Vec<_>>()
    {
        if visited_verts.get(&start).copied().unwrap_or(true) {
            continue;
        }

        let mut chain: Vec<u32> = vec![start];
        let mut current = start;
        *visited_verts.entry(start).or_insert(true) = true;

        while let Some(neighbours) = adj.get(&current).cloned() {
            let mut next_opt = None;
            for &nb in &neighbours {
                let edge = (current, nb);
                if !visited_edges.get(&edge).copied().unwrap_or(true) {
                    next_opt = Some(nb);
                    break;
                }
            }

            match next_opt {
                None => break,
                Some(next) => {
                    visited_edges.insert((current, next), true);
                    visited_edges.insert((next, current), true);
                    if next == start {
                        // Closed loop
                        loops.push(chain.clone());
                        break;
                    }
                    if visited_verts.get(&next).copied().unwrap_or(false) {
                        // Hit an already-visited vertex — partial chain, not a
                        // closed loop in this walk; store as open chain.
                        chain.push(next);
                        loops.push(chain.clone());
                        break;
                    }
                    *visited_verts.entry(next).or_insert(true) = true;
                    chain.push(next);
                    current = next;
                }
            }
        }
    }

    loops
}

/// Compute vertex valence: number of edges incident to each vertex.
///
/// Returns a `Vec<usize>` of length `vertex_count`.
#[allow(dead_code)]
pub fn vertex_valence(mesh: &MeshBuffers) -> Vec<usize> {
    let n = mesh.vertex_count();
    let mut valence = vec![0usize; n];
    let edge_map = build_edge_map(mesh);
    for &(a, b) in edge_map.keys() {
        valence[a as usize] += 1;
        valence[b as usize] += 1;
    }
    valence
}

/// Find non-manifold edges (shared by 3 or more faces).
///
/// Returns `Vec<(vertex_a, vertex_b)>` in canonical (min, max) order.
#[allow(dead_code)]
pub fn find_non_manifold_edges(mesh: &MeshBuffers) -> Vec<(u32, u32)> {
    let edge_map = build_edge_map(mesh);
    edge_map
        .into_iter()
        .filter(|&(_, count)| count >= 3)
        .map(|(edge, _)| edge)
        .collect()
}

/// Split a mesh into its connected components.
///
/// Returns one `MeshBuffers` per component, each with remapped indices.
#[allow(dead_code)]
pub fn split_components(mesh: &MeshBuffers) -> Vec<MeshBuffers> {
    let n = mesh.vertex_count();
    if n == 0 {
        return Vec::new();
    }

    let component_id = find_connected_components(mesh);
    let num_components = if component_id.is_empty() {
        0
    } else {
        component_id.iter().copied().max().unwrap_or(0) + 1
    };

    // Map: component -> (old_vertex -> new_vertex)
    let mut comp_vertex_map: Vec<HashMap<u32, u32>> = vec![HashMap::new(); num_components];
    let mut comp_positions: Vec<Vec<[f32; 3]>> = vec![Vec::new(); num_components];
    let mut comp_normals: Vec<Vec<[f32; 3]>> = vec![Vec::new(); num_components];
    let mut comp_tangents: Vec<Vec<[f32; 4]>> = vec![Vec::new(); num_components];
    let mut comp_uvs: Vec<Vec<[f32; 2]>> = vec![Vec::new(); num_components];
    let mut comp_colors: Vec<Option<Vec<[f32; 4]>>> = vec![None; num_components];

    // Pre-allocate color buffers if source has colors.
    if mesh.colors.is_some() {
        for c in comp_colors.iter_mut() {
            *c = Some(Vec::new());
        }
    }

    // Assign vertices to components.
    for (v, &comp) in component_id.iter().enumerate() {
        let new_idx = comp_positions[comp].len() as u32;
        comp_vertex_map[comp].insert(v as u32, new_idx);
        comp_positions[comp].push(mesh.positions[v]);
        comp_normals[comp].push(mesh.normals[v]);
        comp_tangents[comp].push(mesh.tangents[v]);
        comp_uvs[comp].push(mesh.uvs[v]);
        if let (Some(src_colors), Some(dst_colors)) =
            (mesh.colors.as_ref(), comp_colors[comp].as_mut())
        {
            dst_colors.push(src_colors[v]);
        }
    }

    // Build index buffers per component.
    let mut comp_indices: Vec<Vec<u32>> = vec![Vec::new(); num_components];
    let face_count = mesh.indices.len() / 3;
    for f in 0..face_count {
        let a = mesh.indices[f * 3];
        let b = mesh.indices[f * 3 + 1];
        let c = mesh.indices[f * 3 + 2];
        // All three vertices of a face belong to the same component.
        let comp = component_id[a as usize];
        let map = &comp_vertex_map[comp];
        comp_indices[comp].push(map[&a]);
        comp_indices[comp].push(map[&b]);
        comp_indices[comp].push(map[&c]);
    }

    // Assemble MeshBuffers.
    (0..num_components)
        .map(|comp| MeshBuffers {
            positions: comp_positions[comp].clone(),
            normals: comp_normals[comp].clone(),
            tangents: comp_tangents[comp].clone(),
            uvs: comp_uvs[comp].clone(),
            indices: comp_indices[comp].clone(),
            colors: comp_colors[comp].clone(),
            has_suit: mesh.has_suit,
        })
        .collect()
}

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

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

    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: 3 verts, 1 face, 3 edges (all boundary).
    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],
        )
    }

    /// Tetrahedron: 4 verts, 4 faces, 6 edges (all manifold, closed).
    fn tetrahedron() -> MeshBuffers {
        make_mesh(
            vec![
                [1.0, 1.0, 1.0],
                [-1.0, -1.0, 1.0],
                [-1.0, 1.0, -1.0],
                [1.0, -1.0, -1.0],
            ],
            vec![0, 1, 2, 0, 2, 3, 0, 3, 1, 1, 3, 2],
        )
    }

    /// Two separate triangles (no shared vertices).
    fn two_separate_triangles() -> MeshBuffers {
        make_mesh(
            vec![
                [0.0, 0.0, 0.0],
                [1.0, 0.0, 0.0],
                [0.0, 1.0, 0.0],
                [5.0, 0.0, 0.0],
                [6.0, 0.0, 0.0],
                [5.0, 1.0, 0.0],
            ],
            vec![0, 1, 2, 3, 4, 5],
        )
    }

    /// Two triangles sharing an edge -> connected quad (1 component, 1 manifold edge).
    fn connected_quad() -> 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],
        )
    }

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

    #[test]
    fn single_triangle_stats() {
        let mesh = single_triangle();
        let s = connectivity_stats(&mesh);
        assert_eq!(s.vertex_count, 3);
        assert_eq!(s.face_count, 1);
        assert_eq!(s.edge_count, 3);
        assert_eq!(s.boundary_edge_count, 3);
        assert_eq!(s.manifold_edge_count, 0);
        assert_eq!(s.non_manifold_edge_count, 0);
        assert_eq!(s.connected_component_count, 1);
        assert!(!s.is_closed);
        assert!(s.is_manifold);
    }

    #[test]
    fn two_separate_triangles_two_components() {
        let mesh = two_separate_triangles();
        assert_eq!(connected_component_count(&mesh), 2);
    }

    #[test]
    fn connected_quad_one_component() {
        let mesh = connected_quad();
        assert_eq!(connected_component_count(&mesh), 1);
    }

    #[test]
    fn boundary_edges_of_triangle() {
        let mesh = single_triangle();
        let boundary = find_boundary_edges(&mesh);
        assert_eq!(boundary.len(), 3);
        // All three edges of the triangle must be present.
        let mut sorted = boundary.clone();
        sorted.sort();
        assert!(sorted.contains(&(0, 1)));
        assert!(sorted.contains(&(0, 2)));
        assert!(sorted.contains(&(1, 2)));
    }

    #[test]
    fn closed_mesh_no_boundary() {
        let mesh = tetrahedron();
        let boundary = find_boundary_edges(&mesh);
        assert!(
            boundary.is_empty(),
            "tetrahedron should have no boundary edges"
        );
        let s = connectivity_stats(&mesh);
        assert!(s.is_closed);
    }

    #[test]
    fn vertex_valence_triangle() {
        let mesh = single_triangle();
        let val = vertex_valence(&mesh);
        // In a single triangle every vertex touches 2 edges.
        assert_eq!(val, vec![2, 2, 2]);
    }

    #[test]
    fn non_manifold_edge_detection() {
        // Build a mesh where edge (0,1) is shared by 3 faces.
        let mesh = make_mesh(
            vec![
                [0.0, 0.0, 0.0],
                [1.0, 0.0, 0.0],
                [0.0, 1.0, 0.0],
                [0.0, -1.0, 0.0],
                [1.0, 1.0, 0.0],
            ],
            // Three triangles sharing edge (0,1)
            vec![0, 1, 2, 0, 1, 3, 0, 1, 4],
        );
        let nm = find_non_manifold_edges(&mesh);
        assert!(!nm.is_empty(), "should detect non-manifold edge (0,1)");
        assert!(nm.contains(&(0, 1)));
    }

    #[test]
    fn split_components_count_matches() {
        let mesh = two_separate_triangles();
        let parts = split_components(&mesh);
        assert_eq!(parts.len(), 2);
    }

    #[test]
    fn split_components_each_connected() {
        let mesh = two_separate_triangles();
        let parts = split_components(&mesh);
        for part in &parts {
            assert_eq!(
                connected_component_count(part),
                1,
                "each split component should itself be connected"
            );
        }
    }

    #[test]
    fn euler_characteristic_sphere_is_2() {
        // Tetrahedron is topologically a sphere: V - E + F = 4 - 6 + 4 = 2.
        let mesh = tetrahedron();
        let s = connectivity_stats(&mesh);
        assert_eq!(
            s.euler_characteristic, 2,
            "tetrahedron Euler characteristic should be 2, got {}",
            s.euler_characteristic
        );
    }

    #[test]
    fn boundary_loop_forms_closed_chain() {
        // A single triangle has one boundary loop of 3 vertices.
        let mesh = single_triangle();
        let loops = find_boundary_loops(&mesh);
        assert_eq!(loops.len(), 1, "expected exactly one boundary loop");
        assert_eq!(loops[0].len(), 3, "boundary loop should have 3 vertices");
    }

    #[test]
    fn connected_component_id_consistent() {
        let mesh = two_separate_triangles();
        let ids = find_connected_components(&mesh);
        // Vertices 0..=2 share a component, 3..=5 share another.
        assert_eq!(ids[0], ids[1]);
        assert_eq!(ids[1], ids[2]);
        assert_eq!(ids[3], ids[4]);
        assert_eq!(ids[4], ids[5]);
        assert_ne!(ids[0], ids[3]);
    }

    #[test]
    fn is_manifold_true_for_clean_mesh() {
        let mesh = tetrahedron();
        let s = connectivity_stats(&mesh);
        assert!(s.is_manifold, "tetrahedron should be manifold");
        assert_eq!(s.non_manifold_edge_count, 0);
    }
}