dimforge / parry

2D and 3D collision-detection library for Rust.
https://parry.rs
Apache License 2.0
534 stars 93 forks source link

Using TriMesh::cast_local_ray_and_get_normal FeatureId out of bounds if the face is a backface #71

Open wlinna opened 2 years ago

wlinna commented 2 years ago

TriMesh::cast_local_ray_and_get_normal requires reading source code to use correctly. The issue is that if the ray intersects with a backface, the function sets feature to FeatureId::Face(best + self.indices().len() as u32). When I unwrap the index and attempt to read the triangle (using TriMesh::triangle), I get an index out of bounds error.

This behavior isn't documented clearly enough. Later I learned that I can use TriMesh::is_backface to check whether it's a backface, but even then I can't know how to get the "real index", except by reading the source code and relying on an implementation detail.

Here's a pseudocode to demonstrate the problem

let mesh = TriMesh::new( ... );
let ray = ..;
if let Some(intersection) = mesh.cast_local_ray_and_get_normal(&ray, 1000.0, false) {
    let tri_index = intersection.feature.unwrap_face();
    // if the face is a backface, the following line panics
    let tri = mesh.triangle(tri_index);
}
GitGhillie commented 3 months ago

Also ran into this. Thank you for opening the issue, made it easier to troubleshoot.