You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在trimesh中根据点位置与三角网格获取三角形ID

使用trimesh查找点对应的三角网格三角形ID

Trimesh 提供了nearest.on_surface()方法,可以直接定位点所在的三角形,无论该点位于三角形的顶点、边还是内部区域。以下是完整的实现代码:

import trimesh
import numpy as np

mesh = trimesh.load('mesh.off')
# 将点转为numpy数组,确保计算兼容性
pt = np.array([2.7605, 2.996054, 0.263109])

# 调用on_surface方法获取结果,注意传入的是点数组(单个点也要用列表包裹)
closest_projection, distances, triangle_ids = mesh.nearest.on_surface([pt])

# 提取目标三角形ID
target_tri_id = triangle_ids[0]
print(f"点对应的三角形ID: {target_tri_id}")

方法说明

mesh.nearest.on_surface()返回三个核心值:

  • closest_projection:原始点在网格表面的最近投影点(若点本身就在表面上,结果与原坐标一致)
  • distances:原坐标到投影点的距离(点在表面上时距离为0)
  • triangle_ids:每个输入点对应的三角形索引数组

额外验证(可选)

如果需要确认点确实位于该三角形的边界或内部,可以用trimesh.triangles.point_in_triangle()做二次校验:

# 获取目标三角形的三个顶点坐标
target_triangle = mesh.triangles[target_tri_id]
# 检查点是否在三角形内(包含顶点和边)
is_contained = trimesh.triangles.point_in_triangle(pt, target_triangle, boundary=True)
print(f"点是否在该三角形内(含边界): {is_contained}")

内容的提问来源于stack exchange,提问作者user27665

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 12:58:13