parry icon indicating copy to clipboard operation
parry copied to clipboard

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

Open wlinna opened this issue 3 years ago • 1 comments

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);
}

wlinna avatar Feb 12 '22 09:02 wlinna

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

GitGhillie avatar Apr 17 '24 15:04 GitGhillie