In [ ]:
"""
ray.ipynb
----------------

Demonstrate simple ray- mesh queries
"""

import trimesh
import numpy as np

In [ ]:
# test on a sphere primitive
mesh = trimesh.creation.icosphere()

In [ ]:
# create some rays
ray_origins = np.array([[0, 0, -3],
                        [2, 2, -3]])
ray_directions = np.array([[0, 0, 1],
                           [0, 0, 1]])

In [ ]:
# check out the docstring for intersects_location queries
print(mesh.ray.intersects_location.__doc__)

In [ ]:
# run the mesh- ray query
locations, index_ray, index_tri = mesh.ray.intersects_location(
        ray_origins=ray_origins,
        ray_directions=ray_directions)

In [ ]:
print('The rays hit the mesh at coordinates:\n', locations)

In [ ]:
print('The rays with index: {} hit the triangles stored at mesh.faces[{}]'.format(index_ray, index_tri))

In [ ]:
# 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))

In [ ]:
# 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]

In [ ]:
# create a visualization scene with rays, hits, and mesh
scene = trimesh.Scene([mesh,
                       ray_visualize])

In [ ]:
# show the visualization
scene.show()