Ray ============= An example showing simple ray-mesh queries. .. code:: ipython3 import numpy as np import trimesh .. code:: ipython3 # test on a sphere primitive mesh = trimesh.creation.icosphere() .. code:: ipython3 # create some rays ray_origins = np.array([[0, 0, -3], [2, 2, -3]]) ray_directions = np.array([[0, 0, 1], [0, 0, 1]]) .. code:: ipython3 # check out the docstring for intersects_location queries mesh.ray.intersects_location.__doc__ .. parsed-literal:: '\n Return the location of where a ray hits a surface.\n\n Parameters\n ----------\n ray_origins : (n, 3) float\n Origins of rays\n ray_directions : (n, 3) float\n Direction (vector) of rays\n\n Returns\n ---------\n locations : (m) sequence of (p, 3) float\n Intersection points\n index_ray : (m,) int\n Indexes of ray\n index_tri : (m,) int\n Indexes of mesh.faces\n ' .. code:: ipython3 # run the mesh- ray query locations, index_ray, index_tri = mesh.ray.intersects_location( ray_origins=ray_origins, ray_directions=ray_directions ) .. code:: ipython3 # the rays hit the mesh at coordinateslocations .. code:: ipython3 # the rays with index_ray hit the triangles stored at mesh.faces[index_tri]len(index_ray) .. code:: ipython3 # stack rays into line segments for visualization as Path3D ray_visualize = trimesh.load_path( np.hstack((ray_origins, ray_origins + ray_directions * 5.0)).reshape(-1, 2, 3) ) .. code:: ipython3 # unmerge so viewer doesn't smooth mesh.unmerge_vertices() # make mesh white- ish mesh.visual.face_colors = [255, 255, 255, 255] mesh.visual.face_colors[index_tri] = [255, 0, 0, 255] .. code:: ipython3 # create a visualization scene with rays, hits, and mesh scene = trimesh.Scene([mesh, ray_visualize]) .. code:: ipython3 # show the visualization scene.show() .. raw:: html