def signed_distance ( mesh , points ) : # make sure we have a numpy array points = np . asanyarray ( points , dtype = np . float64 ) # find the closest point on the mesh to the queried points closest , distance , triangle_id = closest_point ( mesh , points ) # we only care about nonzero distances nonzero = distance > tol . merge if not nonzero . any ( ) : return distance inside = mesh . ray . contains_points ( points [ nonzero ] ) sign = ( inside . astype ( int ) * 2 ) - 1 # apply sign to previously computed distance distance [ nonzero ] *= sign return distance
python shortest distance on triangulated mesh
Find the signed distance from a mesh to a list of points .
def 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 object
    points : (n,3) float, list of points in space

    Returns
    ----------
    signed_distance : (n,3) float, signed distance from point to mesh
    """
    # make sure we have a numpy array
    points = np.asanyarray(points, dtype=np.float64)

    # find the closest point on the mesh to the queried points
    closest, distance, triangle_id = closest_point(mesh, points)

    # we only care about nonzero distances
    nonzero = distance > tol.merge

    if not nonzero.any():
        return distance

    inside = mesh.ray.contains_points(points[nonzero])
    sign = (inside.astype(int) * 2) - 1

    # apply sign to previously computed distance
    distance[nonzero] *= sign

    return distance