use halfedge::{
ValidationError, build_icosphere, build_mesh_from_vertices_and_faces, check_topology,
validate_topology,
};
fn main() {
let mesh = build_icosphere(1);
let errors = validate_topology(&mesh);
println!(
"[干净 icosphere(1)] 校验错误数 = {}(应为 0)",
errors.len()
);
println!(
" check_topology: {}",
if check_topology(&mesh).is_ok() {
"OK"
} else {
"FAIL"
}
);
let (mut mesh, hes) = build_two_triangle_mesh();
let h0 = hes[0];
mesh.get_halfedge_mut(h0).unwrap().twin = Some(h0);
let errors = validate_topology(&mesh);
let has_twin_mismatch = errors
.iter()
.any(|e| matches!(e, ValidationError::TwinMismatch { .. }));
let has_self_loop = errors
.iter()
.any(|e| matches!(e, ValidationError::SelfLoopHalfEdge(_)));
println!(
"\n[twin 不匹配] 检测到 TwinMismatch = {}, SelfLoop = {}",
has_twin_mismatch, has_self_loop
);
let (mut mesh, hes) = build_two_triangle_mesh();
let h0 = hes[0];
use halfedge::ids::VertexId;
let bad_v = VertexId::default();
mesh.get_halfedge_mut(h0).unwrap().vertex = bad_v;
let errors = validate_topology(&mesh);
let has_dangling = errors
.iter()
.any(|e| matches!(e, ValidationError::HalfEdgeDanglingVertex { .. }));
println!(
"\n[悬空顶点] 检测到 HalfEdgeDanglingVertex = {}",
has_dangling
);
let vertices = [
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[2.0, 0.0, 0.0], ];
let faces = [[0, 1, 2]];
let mesh = build_mesh_from_vertices_and_faces(&vertices, &faces).unwrap();
let errors = validate_topology(&mesh);
let has_degenerate = errors
.iter()
.any(|e| matches!(e, ValidationError::DegenerateFace { .. }));
println!(
"\n[退化面] 共线三角形检测到 DegenerateFace = {}",
has_degenerate
);
let (mut mesh, hes) = build_two_triangle_mesh();
let h1 = hes[1]; let v0 = mesh.vertex_ids().next().unwrap();
mesh.get_vertex_mut(v0).unwrap().halfedge = Some(h1);
let errors = validate_topology(&mesh);
let has_inconsistent = errors
.iter()
.any(|e| matches!(e, ValidationError::VertexHalfEdgeInconsistent { .. }));
println!(
"\n[入口不一致] 检测到 VertexHalfEdgeInconsistent = {}",
has_inconsistent
);
println!("\n[完整错误列表示例]");
let (mut mesh, hes) = build_two_triangle_mesh();
let h0 = hes[0];
mesh.get_halfedge_mut(h0).unwrap().twin = Some(h0);
let errors = validate_topology(&mesh);
for (i, e) in errors.iter().enumerate() {
println!(" 错误 {}: {}", i + 1, e);
}
println!(" 共 {} 个错误", errors.len());
}
fn build_two_triangle_mesh() -> (halfedge::MeshStorage, Vec<halfedge::HalfEdgeId>) {
let vertices = [
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
];
let faces = [[0, 1, 2], [0, 2, 3]];
let mesh = build_mesh_from_vertices_and_faces(&vertices, &faces).unwrap();
let hes: Vec<_> = mesh.halfedge_ids().collect();
(mesh, hes)
}