通过trimesh
库的signed_distance
函数判断,函数的官方注释如下:
trimesh.proximity.signed_distance(mesh, points)
Find the signed distance from a mesh to a list of points.
Points OUTSIDE the mesh will have NEGATIVE distance
Points within tol.merge of the surface will have POSITIVE distance
Points INSIDE the mesh will have POSITIVE distance
Parameters:
mesh (trimesh.Trimesh) – Mesh to query.
points ((n, 3) float) – Points in space
Returns:
signed_distance – Signed distance from point to mesh
Return type:
(n,) float
程序需要读取一个mesh模型文件,或者自己创建,这里使用open3d
创建一个TriangleMesh
模型,并用该模型的顶点和表面初始化一个Trimesh
模型,然后判断点与模型的关系,代码如下:
import trimesh
import numpy as np
import open3d as o3d
mesh1 = o3d.geometry.TriangleMesh.create_sphere(radius=0.1, resolution=10) # 创建open3d模型
vertices = np.asarray(mesh1.vertices)
faces = np.asarray(mesh1.triangles)
mesh = trimesh.Trimesh(vertices=vertices, faces=faces) # 创建trimesh模型
dist = trimesh.proximity.signed_distance(mesh, [[0, 0, 1],])
print('dist =', dist)
mesh.show()