mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
2.95k stars 574 forks source link

Calculate distance vectors of vertices between 2 meshes #1486

Open huonghld opened 2 years ago

huonghld commented 2 years ago

Hello, Thank you so much for your awesome work!

Is there any way to calculate the difference between 2 meshes by storing the distance between vertices? I think I should need to find the closest point of each vertex, but I'm new to python and trimesh, Could you give an example of this?

Thanks a lot!

YuanWenqing commented 2 years ago

One solution is cKDTree:

from scipy.spatial import cKDTree

tree = cKDTree(mesh1.vertices)
dists,neighbors=tree.query(mesh2.vertices,k=1)
for v,i,d in zip(mesh2.vertices,neighbors,dists):
    print(f'vertex={v}, neighbor={mesh1.vertices[i]}, dist={d}')

Another way via trimesh directly:

dists,neighbors=obj1.kdtree.query(obj2.vertices)
for v,i,d in zip(obj2.vertices,neighbors,dists):
    print(f'vertex={v}, neighbor={obj1.vertices[i]}, dist={d}')