Files
massive-cloud/alg/point_porj/archive/relative_code/test5.py

26 lines
962 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import trimesh
import numpy as np
import open3d as o3d
import time
# 1. 读取数据
mesh_trimesh = trimesh.load("E:\point_distance\data\\40.stl")
pcd_o3d = o3d.io.read_point_cloud("E:\point_distance\data\\40_noisy.ply")
points = np.asarray(pcd_o3d.points)
start_time = time.time()
# ========== 1. 读取数据 ==========
t0 = time.time()
# 2. 使用 trimesh 计算每个点到 Mesh 的最近距离
# trimesh 的 nearest 方法非常高效且精确
closest_points, distances, triangle_ids = mesh_trimesh.nearest.on_surface(points)
t1=time.time()
# distances 即为所求的距离数组
print(f"平均距离: {np.mean(distances)}")
print(f"最大距离: {np.max(distances)}")
print(f" 距离计算: {t1 - t0:.4f} 秒")
# 如果需要将结果存回 Open3D 点云进行可视化
dists_o3d = o3d.utility.DoubleVector(distances.tolist())
# 注意:Open3D 点云对象本身不直接存储标量距离字段用于内置可视化着色