intersection_ray_mesh

intersection_ray_mesh(ray, M)[source]

Compute the intersection(s) between a ray and a mesh.

Parameters:
raytuple of point and vector

A ray represented by a point and a direction vector.

M(list, list)

A mesh represented by a list of vertices and a list of faces.

Returns:
array

The array contains a tuple per intersection of the ray with the mesh. Each tuple contains:

  1. the index of the intersected face

  2. the u coordinate of the intersection in the barycentric coordinates of the face

  3. the u coordinate of the intersection in the barycentric coordinates of the face

  4. the distance between the ray origin and the hit

Examples

>>> import compas
>>> import compas_libigl
>>> from compas.datastructures import Mesh
>>> mesh = Mesh.from_off(compas.get('tubemesh.off'))
>>> mesh.quads_to_triangles()
>>> M = mesh.to_vertices_and_faces()
>>> centroid = mesh.centroid()
>>> ray = [centroid[0], centroid[1], 0], [0, 0, 1.0]
>>> hits = compas_libigl.intersection_ray_mesh(ray, M)
>>> len(hits) == 1
True

To compute the actual intersection point, do

>>> from compas.geometry import add_vectors, scale_vector
>>> point = add_vectors(ray[0], scale_vector(ray[1], hits[0][3]))