0xZohar commited on
Commit
92f98bd
·
verified ·
1 Parent(s): 5ce2af6

Add code/cube3d/training/translate_batchLdr.py

Browse files
code/cube3d/training/translate_batchLdr.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ from translateLdr import process_ldr, getmin_ldr
4
+
5
+ max_x, max_y, max_z = float('-inf'), float('-inf'), float('-inf')
6
+ all_coordinates = []
7
+
8
+ root_dir = '/public/home/wangshuo/gap/assembly/data/car_1k/subset_bottom_300/ldr_rot_expand_test' # 替换为你的根目录路径
9
+ output_dir = '/public/home/wangshuo/gap/assembly/data/car_1k/subset_bottom_300/ldr_rot_expand_trans_test'
10
+ #import ipdb; ipdb.set_trace()
11
+ sorted_files = sorted(os.listdir(root_dir), key=lambda x: int(x.split('_')[1]))
12
+ # 遍历根目录下的每个文件夹
13
+ for file_name in sorted_files:
14
+ #file_name = 'car_5_rot.ldr'
15
+ if file_name == 'car_1627_rot.ldr':
16
+ continue
17
+ original_file_path = os.path.join(root_dir, file_name)
18
+
19
+ if os.path.exists(original_file_path):
20
+ # 打开并读取原始 LDR 文件
21
+ with open(original_file_path, "r") as file:
22
+ lines = file.readlines()
23
+
24
+ # if len(lines) > 310:
25
+ # continue
26
+ # 初始化最小坐标值
27
+ #min_x, min_y, min_z = float('inf'), float('inf'), float('inf')
28
+
29
+ # # 第一遍遍历文件找到最小坐标值
30
+ # for line in lines:
31
+ # parts = line.split() # 按空格分割行数据
32
+ # if len(parts) == 15: # 检查是否为零件数据行
33
+ # # 提取 x, y, z 坐标
34
+ # x, y, z = float(parts[2]), float(parts[3]), float(parts[4])
35
+
36
+ # # 更新最小坐标值
37
+ # min_x = min(min_x, x)
38
+ # min_y = min(min_y, y)
39
+ # min_z = min(min_z, z)
40
+ min_x, min_y, min_z = getmin_ldr(original_file_path)
41
+ #print(file_name)
42
+ x_new, y_new, z_new = process_ldr(original_file_path, os.path.join(output_dir, f'modified_{file_name}'), -min_x, -min_y, -min_z, False)
43
+ all_coordinates.append([round(x_new), round(y_new), round(z_new)])
44
+
45
+ #if x_new > 200 or y_new > 150 or z_new > 500:
46
+ if x_new > 200:
47
+ print(file_name)
48
+ print(f"Maximum coordinates found this LDR files:")
49
+ print(f"Max X: {x_new}")
50
+ print(f"Max Y: {y_new}")
51
+ print(f"Max Z: {z_new}")
52
+
53
+ max_x = max(max_x, x_new)
54
+ max_y = max(max_y, y_new)
55
+ max_z = max(max_z, z_new)
56
+
57
+
58
+
59
+ #import ipdb; ipdb.set_trace()
60
+ # # 新的 LDR 文件内容
61
+ # new_lines = []
62
+
63
+ # # 第二遍遍历文件,更新坐标
64
+ # for line in lines:
65
+ # parts = line.split() # 按空格分割行数据
66
+ # if len(parts) == 15: # 检查是否为零件数据行
67
+ # # 提取 x, y, z 坐标
68
+ # x, y, z = float(parts[2]), float(parts[3]), float(parts[4])
69
+
70
+ # # 计算新的坐标
71
+ # x_new = round(x - min_x)
72
+ # y_new = round(y - min_y)
73
+ # z_new = round(z - min_z)
74
+
75
+ # # 更新坐标
76
+ # parts[2] = str(x_new)
77
+ # parts[3] = str(y_new)
78
+ # parts[4] = str(z_new)
79
+
80
+ # max_x = max(max_x, x_new)
81
+ # max_y = max(max_y, y_new)
82
+ # max_z = max(max_z, z_new)
83
+
84
+ # # 将修改后的行添加到新文件内容中
85
+ # new_lines.append(" ".join(parts) + "\n")
86
+ # else:
87
+ # # 如果是非零件行,直接添加到新文件中
88
+ # new_lines.append(line)
89
+
90
+ # # 设置新的 LDR 文件路径
91
+ # modified_file_path = os.path.join(folder_path, f'modified_{folder_name}.ldr')
92
+
93
+ # # 将修改后的内容写入新的 LDR 文件
94
+ # with open(modified_file_path, "w") as file:
95
+ # file.writelines(new_lines)
96
+
97
+ # print(f"LDR file '{original_file_path}' has been modified and saved as '{modified_file_path}'")
98
+ coordinates_array = np.array(all_coordinates, dtype=np.int32)
99
+ output_npy_path = os.path.join(os.path.dirname(output_dir), 'all_coordinates_test.npy')
100
+ np.save(output_npy_path, coordinates_array)
101
+
102
+ print(f"Maximum coordinates found across all LDR files:")
103
+ print(f"Max X: {max_x}")
104
+ print(f"Max Y: {max_y}")
105
+ print(f"Max Z: {max_z}")
106
+