You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

179 lines
7.2 KiB

import os
import time
import open3d as o3d
import numpy as np
def make_near_dict(base_dir,compact_dir):
""""""
# 用于存储结果的字典
results = {}
# 遍历目录中的所有 .ply 文件
for ply_file in os.listdir(base_dir):
# 检查文件是否为 .ply 格式
if ply_file.endswith('.ply'):
ply_path = os.path.join(base_dir, ply_file)
compact_ply_path = os.path.join(compact_dir, ply_file)
if os.path.exists(compact_ply_path):
ply_read_path = compact_ply_path
else:
ply_read_path = ply_path
# 读取点云
pcd = o3d.io.read_point_cloud(ply_read_path)
# 获取点云的点数据
points = np.asarray(pcd.points)
# 计算质心
centroid = np.mean(points, axis=0)
# 计算 Y 轴最小值
min_y_value = np.min(points[:, 1]) # Y 轴最小值
max_y_value = np.max(points[:, 1])
# 计算 X 轴最小值
min_x_value = np.min(points[:, 0]) # X 轴最小值
max_x_value = np.max(points[:, 0]) # X 轴最小值
#ply_pid = ply_file.split("_")[0]
# 将结果存入字典
results[ply_file] = {
"centroid": centroid,
"min_x_value": min_x_value,
"min_y_value": min_y_value,
"max_x_value": max_x_value,
"max_y_value": max_y_value,
}
# 打印结果
# for ply_file, values in results.items():
# print(f"文件: {ply_file}")
# print(f" 质心: {values['centroid']}")
# print(f" X 轴最小值: {values['min_x_value']}")
# print(f" Y 轴最小值: {values['min_y_value']}")
# 计算每个ply需要触碰检测的
check_touch_dict = {}
for ply_file in results.keys():
print(ply_file)
#ply_pid = ply_file.split("_")[0]
#print(ply_pid)
bounds_min_x = results[ply_file]["min_x_value"]
bounds_min_y = results[ply_file]["min_y_value"]
#bounds_center = results[ply_file]["centroid"]
need_check_list = []
need_values_dict = {}
for ply_file_near in results.keys():
#print(ply_file_near)
if ply_file!= ply_file_near:
bounds_max_x = results[ply_file_near]["max_x_value"]
bounds_max_y = results[ply_file_near]["max_y_value"]
# if ply_file == "151140_9cm_x1=30.578+41.705+90.753.ply":
# print("-"*50)
# print("主::",ply_file)
# print("从::",ply_file_near)
# print(f"center_x{bounds_center[0]}")
# print(f"center_y{bounds_center[1]}")
# print(f"bounds_max_x{bounds_max_x}")
# print(f"bounds_max_y{bounds_max_y}")
# time.sleep(3)
# 235605_12cm_x1=33.774+30.837+120.344.ply
# if bounds_center[0]<bounds_max_x and bounds_center[1]<bounds_max_y:
# print("添加",ply_file_near)
# need_check_list.append(ply_file_near)
#if bounds_near_center[0]>bounds_min_x and bounds_near_center[1]>bounds_min_y:
#print("-"*50)
# print(f"bounds_min_x{bounds_min_x}")
# print(f"bounds_max_x{bounds_max_x}")
x_dis = bounds_min_x - bounds_max_x
y_dis = bounds_min_y - bounds_max_y
#print(f"x_dis=={x_dis}")
#print(f"y_dis=={y_dis}")
#if ply_file=="158040_15cm_x1=80.682+89.345+152.468.ply":
#if ply_file == "235547_4.8cm_x1=29.339+39.528+57.63.ply":
# print("主::",ply_file)
# print("从::",ply_file_near)
#if ply_file == "158040_15cm_x1=80.682+89.345+152.468.ply":
#if ply_file == "151140_9cm_x1=30.578+41.705+90.753.ply":
# print("主::", ply_file)
# print("临近::", ply_file_near)
# time.sleep(3)
if x_dis<-10 and y_dis<-10:
need_check_list.append(ply_file_near)
need_values_dict["need_check_list"] = need_check_list
# need_values_dict["max_x_value"] = bounds_max_x
# need_values_dict["max_y_value"] = bounds_max_y
check_touch_dict[ply_file] = need_values_dict
# print(check_touch_dict)
# print("开始要计算触碰检测的数据")
# for ply_file, values in check_touch_dict.items():
# print("*"*50)
# print(ply_file)
# print(values)
# 去掉离比较远的数据
for check_touch_key,check_touch_values in check_touch_dict.items():
print("-"*50)
#print(check_touch_key)
#print(check_touch_values["need_check_list"])
need_check_list= check_touch_values["need_check_list"]
#print(len(need_check_list))
if len(need_check_list)>2:
ply_A_path = os.path.join(base_dir, check_touch_key)
compact_ply_path = os.path.join(compact_dir, check_touch_key)
if os.path.exists(compact_ply_path):
ply_read_path = compact_ply_path
else:
ply_read_path = ply_A_path
pcd_A = o3d.io.read_point_cloud(ply_read_path)
points_A = np.asarray(pcd_A.points)
distances = []
for i, check_touch in enumerate(need_check_list):
point = results[check_touch]['centroid']
ply_path = os.path.join(base_dir, check_touch)
# 读取当前点云
pcd = o3d.io.read_point_cloud(ply_path)
points = np.asarray(pcd.points)
# 计算点云之间最小点对距离(brute-force)
diff = points_A[:, np.newaxis, :] - points[np.newaxis, :, :] # (N, M, 3)
dists = np.linalg.norm(diff, axis=2) # (N, M)
min_distance = np.min(dists)
#print(f"check_touch: {check_touch}, centroid: {point}, min_distance: {min_distance:.4f}")
distances.append((i, point, min_distance, check_touch))
distances.sort(key=lambda x: x[2])
# 提取最近的 3 个点
nearest_points = distances[:5]
last_elements = [item[-1] for item in nearest_points]
# print(f"nearest_points---------{nearest_points}")
# print(f"check_touch_key--------{check_touch_key}")
# print(f"last_elements--------{last_elements}")
check_touch_dict[check_touch_key]["need_check_list"] = last_elements
return check_touch_dict
# for check_touch_key,check_touch_values in check_touch_dict.items():
# print("*"*50)
# print(check_touch_key)
# print(check_touch_values)
if __name__ == '__main__':
bounds_fix_out_dir = "/data/datasets_20t/type_setting_test_data/print_bounds_fix_data/"
check_touch_dict=make_near_dict(bounds_fix_out_dir)
print(f"check_touch_dict--------------{check_touch_dict}")
"""
{'need_check_list': ['131508_18cm_x1=51.412+87.921+181.446.ply', '239617_12cm_x1=43.987+54.233+120.691.ply']},
"""