nearest.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. """Find the closest point
  2. on the mesh to each random point
  3. """
  4. import numpy as np
  5. from vedo import show, Arrows, download
  6. import trimesh
  7. plyfile = download('https://github.com/mikedh/trimesh/blob/main/models/cycloidal.ply')
  8. mesh = trimesh.load(plyfile)
  9. points = mesh.bounding_box_oriented.sample_volume(count=30)
  10. # find the closest point on the mesh to each random point
  11. closest_points, distances, triangle_id = mesh.nearest.on_surface(points)
  12. #print('Distance from point to surface of mesh:\n{}'.format(distances))
  13. # create a PointCloud object out of each (n,3) list of points
  14. cloud_original = trimesh.points.PointCloud(points)
  15. cloud_close = trimesh.points.PointCloud(closest_points)
  16. # create a unique color for each point
  17. cloud_colors = np.array([trimesh.visual.random_color() for i in points])
  18. # set the colors on the random point and its nearest point to be the same
  19. cloud_original.vertices_color = cloud_colors
  20. cloud_close.vertices_color = cloud_colors
  21. arrs = Arrows(cloud_original.vertices, cloud_close.vertices, c='w')
  22. ## create a scene containing the mesh and two sets of points
  23. show(mesh, cloud_original, cloud_close, arrs, __doc__, bg='bb', axes=1, viewup='z').close()