vpin 0.23.5

Rust library for working with Visual Pinball VPX files
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
//! Mesh validation utilities
//!
//! This module provides functions to validate generated meshes for common issues:
//! - Consistent winding order
//! - Valid normals (non-zero, normalized)
//! - Watertight meshes (no holes/non-manifold edges)
//! - Valid indices (within bounds)

use crate::vpx::gameitem::primitive::VertexWrapper;
use crate::vpx::obj::VpxFace;
use std::collections::HashMap;

/// Result of mesh validation
#[derive(Debug, Default)]
pub struct MeshValidationResult {
    /// Total number of vertices
    pub vertex_count: usize,
    /// Total number of faces (triangles)
    pub face_count: usize,
    /// Indices that are out of bounds
    pub invalid_indices: Vec<(usize, i64)>,
    /// Vertices with zero-length normals
    pub zero_normals: Vec<usize>,
    /// Vertices with non-normalized normals (length != 1.0)
    pub non_unit_normals: Vec<(usize, f32)>,
    /// Degenerate triangles (zero area)
    pub degenerate_faces: Vec<usize>,
    /// Edges that appear only once (holes in the mesh)
    pub boundary_edges: Vec<(i64, i64)>,
    /// Edges that appear more than twice (non-manifold)
    pub non_manifold_edges: Vec<(i64, i64)>,
    /// Faces with inconsistent winding (based on edge direction analysis)
    pub inconsistent_winding_faces: Vec<usize>,
}

impl MeshValidationResult {
    /// Returns true if the mesh has no critical issues (valid indices and no degenerate faces)
    #[allow(dead_code)]
    pub fn is_valid(&self) -> bool {
        self.invalid_indices.is_empty()
            && self.zero_normals.is_empty()
            && self.degenerate_faces.is_empty()
    }

    /// Returns true if the mesh is watertight (no boundary edges)
    #[allow(dead_code)]
    pub fn is_watertight(&self) -> bool {
        self.boundary_edges.is_empty() && self.non_manifold_edges.is_empty()
    }

    /// Returns true if all normals are valid (non-zero and approximately unit length)
    #[allow(dead_code)]
    pub fn has_valid_normals(&self) -> bool {
        self.zero_normals.is_empty() && self.non_unit_normals.is_empty()
    }

    /// Returns a human-readable summary of validation issues
    #[allow(dead_code)]
    pub fn summary(&self) -> String {
        let mut issues = Vec::new();

        if !self.invalid_indices.is_empty() {
            issues.push(format!(
                "{} invalid indices (out of bounds)",
                self.invalid_indices.len()
            ));
        }
        if !self.zero_normals.is_empty() {
            issues.push(format!("{} zero-length normals", self.zero_normals.len()));
        }
        if !self.non_unit_normals.is_empty() {
            issues.push(format!("{} non-unit normals", self.non_unit_normals.len()));
        }
        if !self.degenerate_faces.is_empty() {
            issues.push(format!(
                "{} degenerate triangles",
                self.degenerate_faces.len()
            ));
        }
        if !self.boundary_edges.is_empty() {
            issues.push(format!(
                "{} boundary edges (holes)",
                self.boundary_edges.len()
            ));
        }
        if !self.non_manifold_edges.is_empty() {
            issues.push(format!(
                "{} non-manifold edges",
                self.non_manifold_edges.len()
            ));
        }
        if !self.inconsistent_winding_faces.is_empty() {
            issues.push(format!(
                "{} faces with inconsistent winding",
                self.inconsistent_winding_faces.len()
            ));
        }

        if issues.is_empty() {
            format!(
                "Mesh valid: {} vertices, {} faces",
                self.vertex_count, self.face_count
            )
        } else {
            format!(
                "Mesh issues ({} vertices, {} faces): {}",
                self.vertex_count,
                self.face_count,
                issues.join(", ")
            )
        }
    }
}

/// Validate a mesh for common issues
///
/// # Arguments
/// * `vertices` - The mesh vertices
/// * `faces` - The mesh faces (triangles)
///
/// # Returns
/// A `MeshValidationResult` containing any issues found
#[allow(dead_code)]
pub fn validate_mesh(vertices: &[VertexWrapper], faces: &[VpxFace]) -> MeshValidationResult {
    let mut result = MeshValidationResult {
        vertex_count: vertices.len(),
        face_count: faces.len(),
        ..Default::default()
    };

    // Check for invalid indices
    for (face_idx, face) in faces.iter().enumerate() {
        let vertex_count = vertices.len() as i64;
        if face.i0 < 0 || face.i0 >= vertex_count {
            result.invalid_indices.push((face_idx, face.i0));
        }
        if face.i1 < 0 || face.i1 >= vertex_count {
            result.invalid_indices.push((face_idx, face.i1));
        }
        if face.i2 < 0 || face.i2 >= vertex_count {
            result.invalid_indices.push((face_idx, face.i2));
        }
    }

    // Check normals
    const EPSILON: f32 = 0.0001;
    const UNIT_TOLERANCE: f32 = 0.01;

    for (idx, wrapper) in vertices.iter().enumerate() {
        let v = &wrapper.vertex;
        let normal_length = (v.nx * v.nx + v.ny * v.ny + v.nz * v.nz).sqrt();

        if normal_length < EPSILON {
            result.zero_normals.push(idx);
        } else if (normal_length - 1.0).abs() > UNIT_TOLERANCE {
            result.non_unit_normals.push((idx, normal_length));
        }
    }

    // Check for degenerate triangles (if indices are valid)
    if result.invalid_indices.is_empty() {
        for (face_idx, face) in faces.iter().enumerate() {
            let v0 = &vertices[face.i0 as usize].vertex;
            let v1 = &vertices[face.i1 as usize].vertex;
            let v2 = &vertices[face.i2 as usize].vertex;

            // Check if triangle has zero area using cross product
            let e1 = (v1.x - v0.x, v1.y - v0.y, v1.z - v0.z);
            let e2 = (v2.x - v0.x, v2.y - v0.y, v2.z - v0.z);

            let cross = (
                e1.1 * e2.2 - e1.2 * e2.1,
                e1.2 * e2.0 - e1.0 * e2.2,
                e1.0 * e2.1 - e1.1 * e2.0,
            );

            let area_squared = cross.0 * cross.0 + cross.1 * cross.1 + cross.2 * cross.2;
            if area_squared < EPSILON * EPSILON {
                result.degenerate_faces.push(face_idx);
            }
        }
    }

    // Check edge topology (for watertightness and manifold property)
    if result.invalid_indices.is_empty() {
        let mut edge_counts: HashMap<(i64, i64), i32> = HashMap::new();

        for face in faces {
            // Add edges with consistent ordering (smaller index first for undirected edge)
            let edges = [
                (face.i0.min(face.i1), face.i0.max(face.i1)),
                (face.i1.min(face.i2), face.i1.max(face.i2)),
                (face.i2.min(face.i0), face.i2.max(face.i0)),
            ];

            for edge in edges {
                *edge_counts.entry(edge).or_insert(0) += 1;
            }
        }

        for (edge, count) in edge_counts {
            if count == 1 {
                result.boundary_edges.push(edge);
            } else if count > 2 {
                result.non_manifold_edges.push(edge);
            }
        }
    }

    // Check winding consistency using directed edges
    if result.invalid_indices.is_empty() && result.boundary_edges.is_empty() {
        let mut directed_edge_faces: HashMap<(i64, i64), Vec<usize>> = HashMap::new();

        for (face_idx, face) in faces.iter().enumerate() {
            // Store directed edges (order matters for winding)
            let edges = [(face.i0, face.i1), (face.i1, face.i2), (face.i2, face.i0)];

            for edge in edges {
                directed_edge_faces.entry(edge).or_default().push(face_idx);
            }
        }

        // For a consistent mesh, each directed edge should appear exactly once,
        // and its reverse should also appear exactly once (for closed meshes)
        for ((v0, v1), face_indices) in &directed_edge_faces {
            if face_indices.len() > 1 {
                // Multiple faces share the same directed edge - inconsistent winding
                for &face_idx in face_indices {
                    if !result.inconsistent_winding_faces.contains(&face_idx) {
                        result.inconsistent_winding_faces.push(face_idx);
                    }
                }
            }

            // Check if reverse edge exists (for closed mesh)
            let reverse = (*v1, *v0);
            if !directed_edge_faces.contains_key(&reverse)
                && !result
                    .boundary_edges
                    .contains(&((*v0).min(*v1), (*v0).max(*v1)))
            {
                // This is a boundary edge that wasn't detected earlier
                // (shouldn't happen if we detected boundaries correctly)
            }
        }
    }

    result
}

/// Validate that face normals match vertex normals (approximately)
///
/// This checks if the vertex normals are consistent with the geometric normals
/// computed from the face vertices.
///
/// # Arguments
/// * `vertices` - The mesh vertices
/// * `faces` - The mesh faces (triangles)
/// * `tolerance` - Dot product threshold (1.0 = perfect match, 0.0 = perpendicular)
///
/// # Returns
/// List of face indices where normals don't match
#[allow(dead_code)]
pub fn check_normal_consistency(
    vertices: &[VertexWrapper],
    faces: &[VpxFace],
    tolerance: f32,
) -> Vec<usize> {
    let mut inconsistent = Vec::new();

    for (face_idx, face) in faces.iter().enumerate() {
        if face.i0 < 0
            || face.i0 >= vertices.len() as i64
            || face.i1 < 0
            || face.i1 >= vertices.len() as i64
            || face.i2 < 0
            || face.i2 >= vertices.len() as i64
        {
            continue;
        }

        let v0 = &vertices[face.i0 as usize].vertex;
        let v1 = &vertices[face.i1 as usize].vertex;
        let v2 = &vertices[face.i2 as usize].vertex;

        // Compute face normal from vertices
        let e1 = (v1.x - v0.x, v1.y - v0.y, v1.z - v0.z);
        let e2 = (v2.x - v0.x, v2.y - v0.y, v2.z - v0.z);

        let face_normal = (
            e1.1 * e2.2 - e1.2 * e2.1,
            e1.2 * e2.0 - e1.0 * e2.2,
            e1.0 * e2.1 - e1.1 * e2.0,
        );

        let face_normal_len = (face_normal.0 * face_normal.0
            + face_normal.1 * face_normal.1
            + face_normal.2 * face_normal.2)
            .sqrt();

        if face_normal_len < 0.0001 {
            continue; // Degenerate face
        }

        let face_normal = (
            face_normal.0 / face_normal_len,
            face_normal.1 / face_normal_len,
            face_normal.2 / face_normal_len,
        );

        // Check each vertex normal against the face normal
        for v in [v0, v1, v2] {
            let vertex_normal_len = (v.nx * v.nx + v.ny * v.ny + v.nz * v.nz).sqrt();
            if vertex_normal_len < 0.0001 {
                continue;
            }

            let dot = (face_normal.0 * v.nx + face_normal.1 * v.ny + face_normal.2 * v.nz)
                / vertex_normal_len;

            // If dot product is negative, normals point in opposite directions
            // (which could indicate wrong winding or inverted normals)
            if dot < tolerance {
                if !inconsistent.contains(&face_idx) {
                    inconsistent.push(face_idx);
                }
                break;
            }
        }
    }

    inconsistent
}

/// Validate that vertex normals are consistent with geometric face normals
/// after the glTF coordinate transform and winding reversal.
///
/// The glTF export transforms VPX coordinates (left-handed, Z-up) to glTF
/// (right-handed, Y-up):
///   - Position/normal: VPX (x, y, z) → glTF (x, z, y)
///   - Winding: swap i1 and i2 per triangle
///
/// If vertex normals disagree with geometric face normals (dot product ≤ 0),
/// Blender Cycles will render the surface black because the shading normal faces
/// away from the light/camera.
///
/// Returns a list of face indices where vertex normals disagree.
#[allow(dead_code)]
pub fn check_normal_consistency_gltf(vertices: &[VertexWrapper], faces: &[VpxFace]) -> Vec<usize> {
    let mut inconsistent = Vec::new();

    for (face_idx, face) in faces.iter().enumerate() {
        if face.i0 < 0
            || face.i0 >= vertices.len() as i64
            || face.i1 < 0
            || face.i1 >= vertices.len() as i64
            || face.i2 < 0
            || face.i2 >= vertices.len() as i64
        {
            continue;
        }

        let v0 = &vertices[face.i0 as usize].vertex;
        let v1 = &vertices[face.i1 as usize].vertex;
        let v2 = &vertices[face.i2 as usize].vertex;

        // Apply glTF coordinate transform: VPX (x,y,z) → glTF (x, z, y)
        // Apply winding reversal: (i0, i1, i2) → (i0, i2, i1)
        let a = [v0.x, v0.z, v0.y];
        let b = [v2.x, v2.z, v2.y]; // swapped
        let c = [v1.x, v1.z, v1.y]; // swapped

        // Geometric face normal: (b-a) × (c-a)
        let e1 = [b[0] - a[0], b[1] - a[1], b[2] - a[2]];
        let e2 = [c[0] - a[0], c[1] - a[1], c[2] - a[2]];
        let geo_nx = e1[1] * e2[2] - e1[2] * e2[1];
        let geo_ny = e1[2] * e2[0] - e1[0] * e2[2];
        let geo_nz = e1[0] * e2[1] - e1[1] * e2[0];

        let geo_len = (geo_nx * geo_nx + geo_ny * geo_ny + geo_nz * geo_nz).sqrt();
        if geo_len < 0.0001 {
            continue; // Degenerate face
        }

        // Vertex normal in glTF space: VPX (nx,ny,nz) → glTF (nx, nz, ny)
        let vn = [v0.nx, v0.nz, v0.ny];

        // Dot product: positive means same hemisphere (normals agree)
        let dot = vn[0] * geo_nx + vn[1] * geo_ny + vn[2] * geo_nz;

        if dot <= 0.0 {
            inconsistent.push(face_idx);
        }
    }

    inconsistent
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vpx::mesh::flippers::build_flipper_mesh;
    use crate::vpx::model::Vertex3dNoTex2;

    fn make_vertex(x: f32, y: f32, z: f32, nx: f32, ny: f32, nz: f32) -> VertexWrapper {
        VertexWrapper::new(
            [0u8; 32],
            Vertex3dNoTex2 {
                x,
                y,
                z,
                nx,
                ny,
                nz,
                tu: 0.0,
                tv: 0.0,
            },
        )
    }

    #[test]
    fn test_valid_triangle() {
        let vertices = vec![
            make_vertex(0.0, 0.0, 0.0, 0.0, 0.0, 1.0),
            make_vertex(1.0, 0.0, 0.0, 0.0, 0.0, 1.0),
            make_vertex(0.0, 1.0, 0.0, 0.0, 0.0, 1.0),
        ];
        let faces = vec![VpxFace {
            i0: 0,
            i1: 1,
            i2: 2,
        }];

        let result = validate_mesh(&vertices, &faces);
        assert!(result.invalid_indices.is_empty());
        assert!(result.zero_normals.is_empty());
        assert!(result.degenerate_faces.is_empty());
    }

    #[test]
    fn test_invalid_index() {
        let vertices = vec![
            make_vertex(0.0, 0.0, 0.0, 0.0, 0.0, 1.0),
            make_vertex(1.0, 0.0, 0.0, 0.0, 0.0, 1.0),
        ];
        let faces = vec![VpxFace {
            i0: 0,
            i1: 1,
            i2: 5, // Invalid
        }];

        let result = validate_mesh(&vertices, &faces);
        assert_eq!(result.invalid_indices.len(), 1);
    }

    #[test]
    fn test_zero_normal() {
        let vertices = vec![
            make_vertex(0.0, 0.0, 0.0, 0.0, 0.0, 0.0), // Zero normal
            make_vertex(1.0, 0.0, 0.0, 0.0, 0.0, 1.0),
            make_vertex(0.0, 1.0, 0.0, 0.0, 0.0, 1.0),
        ];
        let faces = vec![VpxFace {
            i0: 0,
            i1: 1,
            i2: 2,
        }];

        let result = validate_mesh(&vertices, &faces);
        assert_eq!(result.zero_normals.len(), 1);
        assert_eq!(result.zero_normals[0], 0);
    }

    #[test]
    fn test_degenerate_triangle() {
        let vertices = vec![
            make_vertex(0.0, 0.0, 0.0, 0.0, 0.0, 1.0),
            make_vertex(1.0, 0.0, 0.0, 0.0, 0.0, 1.0),
            make_vertex(2.0, 0.0, 0.0, 0.0, 0.0, 1.0), // Collinear
        ];
        let faces = vec![VpxFace {
            i0: 0,
            i1: 1,
            i2: 2,
        }];

        let result = validate_mesh(&vertices, &faces);
        assert_eq!(result.degenerate_faces.len(), 1);
    }

    #[test]
    fn test_watertight_cube() {
        // A simple cube with 8 vertices and 12 triangles (2 per face)
        let vertices = vec![
            make_vertex(0.0, 0.0, 0.0, 0.0, 0.0, -1.0),
            make_vertex(1.0, 0.0, 0.0, 0.0, 0.0, -1.0),
            make_vertex(1.0, 1.0, 0.0, 0.0, 0.0, -1.0),
            make_vertex(0.0, 1.0, 0.0, 0.0, 0.0, -1.0),
            make_vertex(0.0, 0.0, 1.0, 0.0, 0.0, 1.0),
            make_vertex(1.0, 0.0, 1.0, 0.0, 0.0, 1.0),
            make_vertex(1.0, 1.0, 1.0, 0.0, 0.0, 1.0),
            make_vertex(0.0, 1.0, 1.0, 0.0, 0.0, 1.0),
        ];

        // Cube faces (CCW winding when viewed from outside)
        let faces = vec![
            // Bottom (z=0)
            VpxFace {
                i0: 0,
                i1: 2,
                i2: 1,
            },
            VpxFace {
                i0: 0,
                i1: 3,
                i2: 2,
            },
            // Top (z=1)
            VpxFace {
                i0: 4,
                i1: 5,
                i2: 6,
            },
            VpxFace {
                i0: 4,
                i1: 6,
                i2: 7,
            },
            // Front (y=0)
            VpxFace {
                i0: 0,
                i1: 1,
                i2: 5,
            },
            VpxFace {
                i0: 0,
                i1: 5,
                i2: 4,
            },
            // Back (y=1)
            VpxFace {
                i0: 2,
                i1: 3,
                i2: 7,
            },
            VpxFace {
                i0: 2,
                i1: 7,
                i2: 6,
            },
            // Left (x=0)
            VpxFace {
                i0: 0,
                i1: 4,
                i2: 7,
            },
            VpxFace {
                i0: 0,
                i1: 7,
                i2: 3,
            },
            // Right (x=1)
            VpxFace {
                i0: 1,
                i1: 2,
                i2: 6,
            },
            VpxFace {
                i0: 1,
                i1: 6,
                i2: 5,
            },
        ];

        let result = validate_mesh(&vertices, &faces);
        assert!(
            result.is_watertight(),
            "Cube should be watertight: {:?}",
            result.boundary_edges
        );
    }

    #[test]
    fn test_mesh_with_hole() {
        // A plane with 4 vertices and 2 triangles - has boundary edges
        let vertices = vec![
            make_vertex(0.0, 0.0, 0.0, 0.0, 0.0, 1.0),
            make_vertex(1.0, 0.0, 0.0, 0.0, 0.0, 1.0),
            make_vertex(1.0, 1.0, 0.0, 0.0, 0.0, 1.0),
            make_vertex(0.0, 1.0, 0.0, 0.0, 0.0, 1.0),
        ];
        let faces = vec![
            VpxFace {
                i0: 0,
                i1: 1,
                i2: 2,
            },
            VpxFace {
                i0: 0,
                i1: 2,
                i2: 3,
            },
        ];

        let result = validate_mesh(&vertices, &faces);
        assert!(!result.is_watertight(), "Plane should have boundary edges");
        assert!(!result.boundary_edges.is_empty());
    }

    #[test]
    fn test_flipper_mesh_validation() {
        use crate::vpx::gameitem::flipper::Flipper;
        use fake::{Fake, Faker};

        // Create a flipper with randomized values but ensure it's visible
        let mut flipper: Flipper = Faker.fake();
        flipper.name = "TestFlipper".to_string();
        flipper.is_visible = true;
        // Set reasonable values for mesh generation
        flipper.base_radius = 21.5;
        flipper.end_radius = 13.0;
        flipper.flipper_radius_max = 130.0;
        flipper.height = 50.0;
        flipper.rubber_thickness = Some(7.0);
        flipper.rubber_height = Some(19.0);
        flipper.rubber_width = Some(24.0);
        flipper.start_angle = 121.0;

        let (vertices, faces) =
            build_flipper_mesh(&flipper, 0.0).expect("Flipper mesh should be generated");

        let result = validate_mesh(&vertices, &faces);

        // Check for critical issues
        assert!(
            result.invalid_indices.is_empty(),
            "Flipper mesh has invalid indices: {:?}",
            result.invalid_indices
        );
        assert!(
            result.degenerate_faces.is_empty(),
            "Flipper mesh has degenerate faces: {:?}",
            result.degenerate_faces
        );

        // Flipper meshes are typically not fully watertight (they're more like shells)
        // but they shouldn't have non-manifold edges
        assert!(
            result.non_manifold_edges.is_empty(),
            "Flipper mesh has non-manifold edges: {:?}",
            result.non_manifold_edges
        );

        // Check normal consistency - this catches inverted/wrong normals
        // tolerance of 0.0 means normals should point in same hemisphere as face normal
        let inconsistent_normals = check_normal_consistency(&vertices, &faces, 0.0);
        assert!(
            inconsistent_normals.len() < faces.len() / 4, // Allow up to 25% inconsistent (for smooth shading at edges)
            "Flipper mesh has too many faces with inconsistent normals: {} out of {} faces",
            inconsistent_normals.len(),
            faces.len()
        );

        // Report any other issues (not failures, just info)
        if !result.zero_normals.is_empty() {
            eprintln!(
                "Warning: Flipper mesh has {} zero normals",
                result.zero_normals.len()
            );
        }
        if !result.non_unit_normals.is_empty() {
            eprintln!(
                "Warning: Flipper mesh has {} non-unit normals",
                result.non_unit_normals.len()
            );
        }
    }
}