nearest
In [1]:
"""
An example showing nearest point queries, sampling the
volume of box primitives generated from the oriented bounds
and using PointCloud objects for visualization.
"""
Out[1]:
'\nAn example showing nearest point queries, sampling the\nvolume of box primitives generated from the oriented bounds\nand using PointCloud objects for visualization.\n'
In [2]:
import trimesh
import numpy as np
In [3]:
# load a large- ish PLY model with colors
mesh = trimesh.load('../models/cycloidal.ply')
In [4]:
# we can sample the volume of Box primitives
points = mesh.bounding_box_oriented.sample_volume(count=10)
In [5]:
# find the closest point on the mesh to each random point
(closest_points,
distances,
triangle_id) = mesh.nearest.on_surface(points)
print('Distance from point to surface of mesh:\n{}'.format(distances))
Distance from point to surface of mesh: [0.04705422 0.04053529 0.00202093 0.02160855 0.00693678 0.04498782 0.03595419 0.39641115 0.13882475 0.1574983 ]
In [6]:
# create a PointCloud object out of each (n,3) list of points
cloud_original = trimesh.points.PointCloud(points)
cloud_close = trimesh.points.PointCloud(closest_points)
# create a unique color for each point
cloud_colors = np.array([trimesh.visual.random_color() for i in points])
# set the colors on the random point and its nearest point to be the same
cloud_original.vertices_color = cloud_colors
cloud_close.vertices_color = cloud_colors
# create a scene containing the mesh and two sets of points
scene = trimesh.Scene([mesh,
cloud_original,
cloud_close])
# show the scene wusing
scene.show()
Out[6]: