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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Mesh feature line extraction: ridges, valleys, silhouettes, and sharp edges.

use std::collections::HashMap;

use crate::mesh::MeshBuffers;

// ---------------------------------------------------------------------------
// Math helpers
// ---------------------------------------------------------------------------

#[inline]
fn sub3(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
    [a[0] - b[0], a[1] - b[1], a[2] - b[2]]
}

#[inline]
fn cross3(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
    [
        a[1] * b[2] - a[2] * b[1],
        a[2] * b[0] - a[0] * b[2],
        a[0] * b[1] - a[1] * b[0],
    ]
}

#[inline]
fn dot3(a: [f32; 3], b: [f32; 3]) -> f32 {
    a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}

#[inline]
fn len3(v: [f32; 3]) -> f32 {
    (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt()
}

#[inline]
fn normalize3(v: [f32; 3]) -> [f32; 3] {
    let l = len3(v);
    if l < 1e-10 {
        [0.0, 0.0, 1.0]
    } else {
        [v[0] / l, v[1] / l, v[2] / l]
    }
}

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

/// Type of feature line.
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FeatureType {
    /// Local maximum of curvature.
    Ridge,
    /// Local minimum of curvature.
    Valley,
    /// Edges where one face points toward view, one away.
    Silhouette,
    /// Edges with dihedral angle > threshold.
    Sharp,
    /// Mesh boundary (open edges).
    Boundary,
    /// User-defined crease edges.
    Crease,
}

/// A single feature edge.
#[allow(dead_code)]
pub struct FeatureEdge {
    pub v0: u32,
    pub v1: u32,
    pub feature_type: FeatureType,
    /// Strength in 0..1 (e.g. |dihedral_angle| / PI for Sharp).
    pub strength: f32,
}

/// Collection of feature lines.
#[allow(dead_code)]
pub struct FeatureLines {
    pub edges: Vec<FeatureEdge>,
}

#[allow(dead_code)]
impl FeatureLines {
    /// Create an empty collection.
    pub fn new() -> Self {
        FeatureLines { edges: Vec::new() }
    }

    /// Add a feature edge.
    pub fn add(&mut self, edge: FeatureEdge) {
        self.edges.push(edge);
    }

    /// Total number of feature edges.
    pub fn count(&self) -> usize {
        self.edges.len()
    }

    /// All edges of a given type.
    pub fn by_type(&self, ft: &FeatureType) -> Vec<&FeatureEdge> {
        self.edges
            .iter()
            .filter(|e| &e.feature_type == ft)
            .collect()
    }

    /// Count of edges of a given type.
    pub fn count_by_type(&self, ft: &FeatureType) -> usize {
        self.edges.iter().filter(|e| &e.feature_type == ft).count()
    }

    /// Chain edges into ordered polylines (greedy).
    pub fn to_polylines(&self) -> Vec<Vec<u32>> {
        let pairs: Vec<(u32, u32)> = self.edges.iter().map(|e| (e.v0, e.v1)).collect();
        chain_edges(&pairs)
    }

    /// Total length of all feature edges using the given positions array.
    pub fn total_length(&self, positions: &[[f32; 3]]) -> f32 {
        self.edges.iter().fold(0.0f32, |acc, e| {
            let i0 = e.v0 as usize;
            let i1 = e.v1 as usize;
            if i0 < positions.len() && i1 < positions.len() {
                acc + len3(sub3(positions[i1], positions[i0]))
            } else {
                acc
            }
        })
    }

    /// Export as SVG path data, projecting onto the XY plane.
    pub fn to_svg_paths(&self, positions: &[[f32; 3]]) -> String {
        let mut svg = String::new();
        for e in &self.edges {
            let i0 = e.v0 as usize;
            let i1 = e.v1 as usize;
            if i0 < positions.len() && i1 < positions.len() {
                let p0 = positions[i0];
                let p1 = positions[i1];
                svg.push_str(&format!(
                    "M {:.4} {:.4} L {:.4} {:.4} ",
                    p0[0], p0[1], p1[0], p1[1]
                ));
            }
        }
        svg.trim_end().to_string()
    }
}

impl Default for FeatureLines {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// Core math utilities
// ---------------------------------------------------------------------------

/// Compute the face normal for triangle `face_i` (zero-based).
/// Returns a unit normal, or `[0,0,1]` on degenerate triangles.
#[allow(dead_code)]
pub fn face_normal(positions: &[[f32; 3]], indices: &[u32], face_i: usize) -> [f32; 3] {
    let base = face_i * 3;
    if base + 2 >= indices.len() {
        return [0.0, 0.0, 1.0];
    }
    let i0 = indices[base] as usize;
    let i1 = indices[base + 1] as usize;
    let i2 = indices[base + 2] as usize;
    if i0 >= positions.len() || i1 >= positions.len() || i2 >= positions.len() {
        return [0.0, 0.0, 1.0];
    }
    let e1 = sub3(positions[i1], positions[i0]);
    let e2 = sub3(positions[i2], positions[i0]);
    normalize3(cross3(e1, e2))
}

/// Compute the dihedral angle (in degrees) between two triangles sharing edge
/// `v0`–`v1`. `v2` is the third vertex of face A, `v3` is the third vertex of face B.
#[allow(dead_code)]
pub fn dihedral_angle(v0: [f32; 3], v1: [f32; 3], v2: [f32; 3], v3: [f32; 3]) -> f32 {
    // Normal of face A: (v1-v0) x (v2-v0)
    let na = normalize3(cross3(sub3(v1, v0), sub3(v2, v0)));
    // Normal of face B: (v1-v0) x (v3-v0)
    let nb = normalize3(cross3(sub3(v1, v0), sub3(v3, v0)));
    let cos_a = dot3(na, nb).clamp(-1.0, 1.0);
    cos_a.acos().to_degrees()
}

// ---------------------------------------------------------------------------
// Build edge-to-face adjacency
// ---------------------------------------------------------------------------

/// Build edge-to-face adjacency: canonical edge `(min, max)` → face indices.
#[allow(dead_code)]
pub fn build_edge_face_map(indices: &[u32]) -> HashMap<(u32, u32), Vec<usize>> {
    let mut map: HashMap<(u32, u32), Vec<usize>> = HashMap::new();
    for (fi, tri) in indices.chunks_exact(3).enumerate() {
        let verts = [tri[0], tri[1], tri[2]];
        for k in 0..3 {
            let a = verts[k];
            let b = verts[(k + 1) % 3];
            let key = if a < b { (a, b) } else { (b, a) };
            map.entry(key).or_default().push(fi);
        }
    }
    map
}

// ---------------------------------------------------------------------------
// Chain edges into polylines
// ---------------------------------------------------------------------------

/// Chain unordered edge segments into polylines (greedy).
#[allow(dead_code)]
pub fn chain_edges(edges: &[(u32, u32)]) -> Vec<Vec<u32>> {
    if edges.is_empty() {
        return Vec::new();
    }

    // Build adjacency: vertex → list of (other_vertex, edge_index)
    let mut adj: HashMap<u32, Vec<(u32, usize)>> = HashMap::new();
    for (ei, &(a, b)) in edges.iter().enumerate() {
        adj.entry(a).or_default().push((b, ei));
        adj.entry(b).or_default().push((a, ei));
    }

    let mut used = vec![false; edges.len()];
    let mut polylines: Vec<Vec<u32>> = Vec::new();

    // Prefer starting from degree-1 endpoints; fall back to any unused edge
    let mut start_candidates: Vec<u32> = adj
        .iter()
        .filter(|(_, nbrs)| nbrs.len() == 1)
        .map(|(&v, _)| v)
        .collect();
    // append all vertices (duplicates filtered out via used[])
    let all_verts: Vec<u32> = adj.keys().cloned().collect();
    start_candidates.extend(all_verts);

    for start in start_candidates {
        // Find an unused edge from start
        let first_unused = adj
            .get(&start)
            .and_then(|nbrs| nbrs.iter().find(|&&(_, ei)| !used[ei]).copied());

        if let Some((mut next, mut ei)) = first_unused {
            used[ei] = true;
            let mut chain = vec![start, next];

            loop {
                let cur = chain[chain.len() - 1];
                let prev = chain[chain.len() - 2];
                let cont = adj.get(&cur).and_then(|nbrs| {
                    nbrs.iter()
                        .find(|&&(v, ei2)| !used[ei2] && v != prev)
                        .copied()
                });
                match cont {
                    Some((nxt, nei)) => {
                        used[nei] = true;
                        next = nxt;
                        ei = nei;
                        chain.push(next);
                        let _ = ei; // suppress unused warning
                    }
                    None => break,
                }
            }
            if chain.len() >= 2 {
                polylines.push(chain);
            }
        }
    }

    polylines
}

// ---------------------------------------------------------------------------
// Extract sharp edges
// ---------------------------------------------------------------------------

/// Extract sharp edges where the dihedral angle exceeds `threshold_deg`.
#[allow(dead_code)]
pub fn extract_sharp_edges(mesh: &MeshBuffers, threshold_deg: f32) -> FeatureLines {
    let mut fl = FeatureLines::new();
    let positions = &mesh.positions;
    let indices = &mesh.indices;

    let edge_map = build_edge_face_map(indices);

    for (&(a, b), faces) in &edge_map {
        if faces.len() != 2 {
            continue; // boundary or non-manifold — skip here
        }
        let fi_a = faces[0];
        let fi_b = faces[1];

        // Third vertices of each face
        let third_vertex = |fi: usize, ea: u32, eb: u32| -> Option<u32> {
            let base = fi * 3;
            if base + 2 >= indices.len() {
                return None;
            }
            for k in 0..3 {
                let v = indices[base + k];
                if v != ea && v != eb {
                    return Some(v);
                }
            }
            None
        };

        let v2 = match third_vertex(fi_a, a, b) {
            Some(v) => v,
            None => continue,
        };
        let v3 = match third_vertex(fi_b, a, b) {
            Some(v) => v,
            None => continue,
        };

        if a as usize >= positions.len()
            || b as usize >= positions.len()
            || v2 as usize >= positions.len()
            || v3 as usize >= positions.len()
        {
            continue;
        }

        let angle = dihedral_angle(
            positions[a as usize],
            positions[b as usize],
            positions[v2 as usize],
            positions[v3 as usize],
        );

        if angle > threshold_deg {
            let strength = (angle / 180.0).clamp(0.0, 1.0);
            fl.add(FeatureEdge {
                v0: a,
                v1: b,
                feature_type: FeatureType::Sharp,
                strength,
            });
        }
    }

    fl
}

// ---------------------------------------------------------------------------
// Extract boundary edges
// ---------------------------------------------------------------------------

/// Extract all boundary (open) edges as feature lines.
#[allow(dead_code)]
pub fn extract_boundary_edges_fl(mesh: &MeshBuffers) -> FeatureLines {
    let mut fl = FeatureLines::new();
    let edge_map = build_edge_face_map(&mesh.indices);

    for (&(a, b), faces) in &edge_map {
        if faces.len() == 1 {
            fl.add(FeatureEdge {
                v0: a,
                v1: b,
                feature_type: FeatureType::Boundary,
                strength: 1.0,
            });
        }
    }

    fl
}

// ---------------------------------------------------------------------------
// Extract silhouette edges
// ---------------------------------------------------------------------------

/// Extract silhouette edges given a view direction.
/// An edge is a silhouette if the two adjacent faces have opposing sign dot
/// products with the view direction (one faces the viewer, one faces away).
#[allow(dead_code)]
pub fn extract_silhouette(mesh: &MeshBuffers, view_dir: [f32; 3]) -> FeatureLines {
    let mut fl = FeatureLines::new();
    let view = normalize3(view_dir);
    let positions = &mesh.positions;
    let indices = &mesh.indices;

    let edge_map = build_edge_face_map(indices);

    for (&(a, b), faces) in &edge_map {
        if faces.len() != 2 {
            continue;
        }
        let n0 = face_normal(positions, indices, faces[0]);
        let n1 = face_normal(positions, indices, faces[1]);

        let d0 = dot3(n0, view);
        let d1 = dot3(n1, view);

        // Silhouette: opposite sides of the viewing direction
        if d0 * d1 < 0.0 {
            let strength = (d0 - d1).abs().min(1.0);
            fl.add(FeatureEdge {
                v0: a,
                v1: b,
                feature_type: FeatureType::Silhouette,
                strength,
            });
        }
    }

    fl
}

// ---------------------------------------------------------------------------
// Extract all features
// ---------------------------------------------------------------------------

/// Extract all feature lines with the given parameters.
/// If `view_dir` is `None`, silhouette extraction is skipped.
#[allow(dead_code)]
pub fn extract_all_features(
    mesh: &MeshBuffers,
    sharp_threshold_deg: f32,
    view_dir: Option<[f32; 3]>,
) -> FeatureLines {
    let mut fl = FeatureLines::new();

    // Sharp edges
    let sharp = extract_sharp_edges(mesh, sharp_threshold_deg);
    for e in sharp.edges {
        fl.add(e);
    }

    // Boundary edges
    let boundary = extract_boundary_edges_fl(mesh);
    for e in boundary.edges {
        fl.add(e);
    }

    // Silhouette edges
    if let Some(vd) = view_dir {
        let sil = extract_silhouette(mesh, vd);
        for e in sil.edges {
            fl.add(e);
        }
    }

    fl
}

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

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

    // ------------------------------------------------------------------
    // Helper: build a minimal MeshBuffers (no morph dependency)
    // ------------------------------------------------------------------
    fn make_mesh(positions: Vec<[f32; 3]>, indices: Vec<u32>) -> MeshBuffers {
        let n = positions.len();
        MeshBuffers {
            positions,
            normals: vec![[0.0, 0.0, 1.0]; n],
            tangents: vec![[1.0, 0.0, 0.0, 1.0]; n],
            uvs: vec![[0.0, 0.0]; n],
            indices,
            colors: None,
            has_suit: false,
        }
    }

    // Two triangles forming a flat quad (shared edge 1-2)
    //   3 --- 2
    //   |   / |
    //   |  /  |
    //   | /   |
    //   0 --- 1
    fn flat_quad_mesh() -> MeshBuffers {
        let positions = vec![
            [0.0f32, 0.0, 0.0], // 0
            [1.0, 0.0, 0.0],    // 1
            [1.0, 1.0, 0.0],    // 2
            [0.0, 1.0, 0.0],    // 3
        ];
        let indices = vec![0, 1, 2, 0, 2, 3];
        make_mesh(positions, indices)
    }

    // A single triangle (open mesh → all 3 edges are boundary)
    fn single_triangle_mesh() -> MeshBuffers {
        let positions = vec![[0.0f32, 0.0, 0.0], [1.0, 0.0, 0.0], [0.5, 1.0, 0.0]];
        let indices = vec![0, 1, 2];
        make_mesh(positions, indices)
    }

    // A "folded" mesh: two triangles sharing an edge but bent 90°
    //   v0=(0,0,0), v1=(1,0,0), v2=(0.5, 0, 1), v3=(0.5, 1, 0)
    fn bent_mesh() -> MeshBuffers {
        let positions = vec![
            [0.0f32, 0.0, 0.0], // 0
            [1.0, 0.0, 0.0],    // 1
            [0.5, 0.0, 1.0],    // 2  (up in Z)
            [0.5, 1.0, 0.0],    // 3  (up in Y)
        ];
        // face A: 0,1,2  face B: 0,1,3
        let indices = vec![0, 1, 2, 0, 1, 3];
        make_mesh(positions, indices)
    }

    // ----------------------------------------------------------------
    // Test 1: FeatureLines::new / count
    // ----------------------------------------------------------------
    #[test]
    fn test_feature_lines_new_and_count() {
        let fl = FeatureLines::new();
        assert_eq!(fl.count(), 0);
    }

    // ----------------------------------------------------------------
    // Test 2: FeatureLines::add
    // ----------------------------------------------------------------
    #[test]
    fn test_feature_lines_add() {
        let mut fl = FeatureLines::new();
        fl.add(FeatureEdge {
            v0: 0,
            v1: 1,
            feature_type: FeatureType::Sharp,
            strength: 0.5,
        });
        assert_eq!(fl.count(), 1);
    }

    // ----------------------------------------------------------------
    // Test 3: by_type and count_by_type
    // ----------------------------------------------------------------
    #[test]
    fn test_by_type_and_count_by_type() {
        let mut fl = FeatureLines::new();
        fl.add(FeatureEdge {
            v0: 0,
            v1: 1,
            feature_type: FeatureType::Sharp,
            strength: 0.5,
        });
        fl.add(FeatureEdge {
            v0: 1,
            v1: 2,
            feature_type: FeatureType::Boundary,
            strength: 1.0,
        });
        fl.add(FeatureEdge {
            v0: 2,
            v1: 3,
            feature_type: FeatureType::Sharp,
            strength: 0.7,
        });

        assert_eq!(fl.count_by_type(&FeatureType::Sharp), 2);
        assert_eq!(fl.count_by_type(&FeatureType::Boundary), 1);
        assert_eq!(fl.by_type(&FeatureType::Silhouette).len(), 0);
    }

    // ----------------------------------------------------------------
    // Test 4: build_edge_face_map
    // ----------------------------------------------------------------
    #[test]
    fn test_build_edge_face_map() {
        let indices = vec![0u32, 1, 2, 0, 2, 3];
        let map = build_edge_face_map(&indices);
        // Shared edge 0-2 (canonical form)
        let shared = map.get(&(0, 2)).expect("should succeed");
        assert_eq!(shared.len(), 2);
        // Boundary edge 0-1
        let boundary = map.get(&(0, 1)).expect("should succeed");
        assert_eq!(boundary.len(), 1);
    }

    // ----------------------------------------------------------------
    // Test 5: face_normal – flat face pointing in +Z
    // ----------------------------------------------------------------
    #[test]
    fn test_face_normal_z() {
        let positions = vec![[0.0f32, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        let indices = vec![0u32, 1, 2];
        let n = face_normal(&positions, &indices, 0);
        assert!((n[2] - 1.0).abs() < 1e-5, "Should point +Z, got {:?}", n);
    }

    // ----------------------------------------------------------------
    // Test 6: dihedral_angle – flat (180°)
    // ----------------------------------------------------------------
    #[test]
    fn test_dihedral_angle_flat() {
        // Two coplanar triangles sharing edge (0,0,0)–(1,0,0)
        let v0 = [0.0f32, 0.0, 0.0];
        let v1 = [1.0, 0.0, 0.0];
        let v2 = [0.5, 1.0, 0.0]; // face A third vertex
        let v3 = [0.5, -1.0, 0.0]; // face B third vertex (opposite side, coplanar)
        let angle = dihedral_angle(v0, v1, v2, v3);
        // Coplanar faces → normals are antiparallel → acos(-1) = 180°
        assert!(
            (angle - 180.0).abs() < 1.0,
            "Flat dihedral should be ~180°, got {angle}"
        );
    }

    // ----------------------------------------------------------------
    // Test 7: dihedral_angle – 90° fold
    // ----------------------------------------------------------------
    #[test]
    fn test_dihedral_angle_90() {
        let v0 = [0.0f32, 0.0, 0.0];
        let v1 = [1.0, 0.0, 0.0];
        let v2 = [0.5, 0.0, 1.0]; // Z-face
        let v3 = [0.5, 1.0, 0.0]; // Y-face
        let angle = dihedral_angle(v0, v1, v2, v3);
        assert!(
            (angle - 90.0).abs() < 1.0,
            "90° dihedral expected, got {angle}"
        );
    }

    // ----------------------------------------------------------------
    // Test 8: extract_boundary_edges_fl – single triangle
    // ----------------------------------------------------------------
    #[test]
    fn test_extract_boundary_single_triangle() {
        let mesh = single_triangle_mesh();
        let fl = extract_boundary_edges_fl(&mesh);
        assert_eq!(
            fl.count(),
            3,
            "Single triangle should have 3 boundary edges"
        );
        assert!(fl
            .edges
            .iter()
            .all(|e| e.feature_type == FeatureType::Boundary));
    }

    // ----------------------------------------------------------------
    // Test 9: extract_boundary_edges_fl – flat quad (1 shared edge)
    // ----------------------------------------------------------------
    #[test]
    fn test_extract_boundary_quad() {
        let mesh = flat_quad_mesh();
        let fl = extract_boundary_edges_fl(&mesh);
        // 4 outer edges are boundary, shared diagonal is interior
        assert_eq!(fl.count(), 4);
    }

    // ----------------------------------------------------------------
    // Test 10: extract_sharp_edges – flat quad has no sharp edges
    // ----------------------------------------------------------------
    #[test]
    fn test_extract_sharp_flat_quad_none() {
        let mesh = flat_quad_mesh();
        let fl = extract_sharp_edges(&mesh, 30.0);
        // All edges are coplanar (dihedral ≈ 180° < threshold for non-sharp)
        // BUT 180° > 30° threshold, so they ARE flagged sharp.
        // Let's verify that the internal (shared) edge is detected.
        // Actually for flat coplanar triangles the dihedral between them is ~180°
        // so with threshold=30 it IS sharp.  With threshold=200 it's not.
        let fl_high = extract_sharp_edges(&mesh, 200.0);
        assert_eq!(
            fl_high.count(),
            0,
            "No edges should be sharp with threshold 200°"
        );
        let _ = fl; // suppress unused
    }

    // ----------------------------------------------------------------
    // Test 11: extract_sharp_edges – bent mesh detects the fold
    // ----------------------------------------------------------------
    #[test]
    fn test_extract_sharp_bent_mesh() {
        let mesh = bent_mesh();
        // 90° bend, threshold 45° → should find the shared edge
        let fl = extract_sharp_edges(&mesh, 45.0);
        assert!(
            fl.count() >= 1,
            "Bent mesh should have at least one sharp edge"
        );
        assert!(fl
            .edges
            .iter()
            .all(|e| e.feature_type == FeatureType::Sharp));
        // Strength should be in 0..1
        for e in &fl.edges {
            assert!(e.strength >= 0.0 && e.strength <= 1.0);
        }
    }

    // ----------------------------------------------------------------
    // Test 12: extract_silhouette
    // ----------------------------------------------------------------
    #[test]
    fn test_extract_silhouette_bent_mesh() {
        let mesh = bent_mesh();
        // bent_mesh face 0 normal is [0,-1,0], face 1 normal is [0,0,1].
        // View direction [0, 1, 1] gives dot products of opposite sign
        // (d0 < 0, d1 > 0) → the shared edge 0-1 is a silhouette.
        let fl = extract_silhouette(&mesh, [0.0, 1.0, 1.0]);
        assert!(fl.count() >= 1, "Expected silhouette edges");
        assert!(fl
            .edges
            .iter()
            .all(|e| e.feature_type == FeatureType::Silhouette));
    }

    // ----------------------------------------------------------------
    // Test 13: extract_all_features combines results
    // ----------------------------------------------------------------
    #[test]
    fn test_extract_all_features() {
        let mesh = bent_mesh();
        let fl = extract_all_features(&mesh, 45.0, Some([0.0, 1.0, 0.0]));
        // Should have boundary + sharp + silhouette edges
        assert!(fl.count() >= 1);
        let has_boundary = fl.count_by_type(&FeatureType::Boundary) > 0;
        let has_sharp = fl.count_by_type(&FeatureType::Sharp) > 0;
        assert!(has_boundary, "Should have boundary edges");
        assert!(has_sharp, "Should have sharp edges");
    }

    // ----------------------------------------------------------------
    // Test 14: chain_edges – simple path
    // ----------------------------------------------------------------
    #[test]
    fn test_chain_edges_simple_path() {
        // 0-1-2-3 as edges
        let edges = vec![(0u32, 1), (1, 2), (2, 3)];
        let chains = chain_edges(&edges);
        // Should produce one chain of 4 vertices
        assert_eq!(chains.len(), 1);
        assert_eq!(chains[0].len(), 4);
    }

    // ----------------------------------------------------------------
    // Test 15: total_length
    // ----------------------------------------------------------------
    #[test]
    fn test_total_length() {
        let positions: Vec<[f32; 3]> = vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]];
        let mut fl = FeatureLines::new();
        fl.add(FeatureEdge {
            v0: 0,
            v1: 1,
            feature_type: FeatureType::Sharp,
            strength: 0.5,
        });
        fl.add(FeatureEdge {
            v0: 1,
            v1: 2,
            feature_type: FeatureType::Sharp,
            strength: 0.5,
        });
        let total = fl.total_length(&positions);
        assert!(
            (total - 2.0).abs() < 1e-5,
            "Total length should be 2.0, got {total}"
        );
    }

    // ----------------------------------------------------------------
    // Test 16: to_svg_paths writes valid-looking SVG data
    // ----------------------------------------------------------------
    #[test]
    fn test_to_svg_paths() {
        use std::fs;
        let positions: Vec<[f32; 3]> = vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]];
        let mut fl = FeatureLines::new();
        fl.add(FeatureEdge {
            v0: 0,
            v1: 1,
            feature_type: FeatureType::Sharp,
            strength: 0.5,
        });
        let svg = fl.to_svg_paths(&positions);
        assert!(svg.starts_with('M'), "SVG should start with M command");
        assert!(svg.contains('L'), "SVG should contain L command");

        // Write to /tmp/ for inspection
        fs::write(std::env::temp_dir().join("test_feature_lines.svg"), &svg).ok();
    }

    // ----------------------------------------------------------------
    // Test 17: to_polylines on a mesh
    // ----------------------------------------------------------------
    #[test]
    fn test_to_polylines() {
        let mesh = single_triangle_mesh();
        let fl = extract_boundary_edges_fl(&mesh);
        let polys = fl.to_polylines();
        // 3 boundary edges of a triangle → 1 closed loop
        assert!(!polys.is_empty());
        // All vertices covered
        let total_verts: usize = polys.iter().map(|p| p.len()).sum();
        assert!(total_verts >= 3);
    }

    // ----------------------------------------------------------------
    // Test 18: FeatureLines default impl
    // ----------------------------------------------------------------
    #[test]
    fn test_feature_lines_default() {
        let fl = FeatureLines::default();
        assert_eq!(fl.count(), 0);
    }

    // ----------------------------------------------------------------
    // Test 19: dihedral_angle strength clamped to 0..1
    // ----------------------------------------------------------------
    #[test]
    fn test_sharp_strength_range() {
        let mesh = bent_mesh();
        let fl = extract_sharp_edges(&mesh, 30.0);
        for e in &fl.edges {
            assert!(
                e.strength >= 0.0 && e.strength <= 1.0,
                "Strength out of range: {}",
                e.strength
            );
        }
    }
}