python 判断一点是否在模型内部

本文展示了如何通过Python的trimesh库和open3d库来创建一个三维Sphere模型,并利用trimesh的signed_distance函数计算点到模型的有符号距离。代码中,首先用open3d创建了一个Sphere模型,然后转换为trimesh的TriangleMesh对象,最后计算了点([0,0,1])与模型的距离并打印结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值