0xZohar commited on
Commit
19c667c
·
verified ·
1 Parent(s): 6277b99

Add code/cube3d/training/absolute_position.py

Browse files
code/cube3d/training/absolute_position.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ max_x, max_y, max_z = float('-inf'), float('-inf'), float('-inf')
4
+
5
+ # 设置根目录路径,所有文件夹都位于此目录下
6
+ root_dir = '/public/home/wangshuo/gap/assembly/data' # 替换为你的根目录路径
7
+
8
+ # 遍历根目录下的每个文件夹
9
+ for folder_name in os.listdir(root_dir):
10
+ folder_path = os.path.join(root_dir, folder_name)
11
+
12
+ if os.path.isdir(folder_path):
13
+ # 设置原始 LDR 文件路径
14
+ original_file_path = os.path.join(folder_path, f'{folder_name}.ldr')
15
+
16
+ # 检查文件是否存在
17
+ if os.path.exists(original_file_path):
18
+ # 打开并读取原始 LDR 文件
19
+ with open(original_file_path, "r") as file:
20
+ lines = file.readlines()
21
+
22
+ if len(lines) > 310:
23
+ continue
24
+ # 初始化最小坐标值
25
+ min_x, min_y, min_z = float('inf'), float('inf'), float('inf')
26
+
27
+ # 第一遍遍历文件找到最小坐标值
28
+ for line in lines:
29
+ parts = line.split() # 按空格分割行数据
30
+ if len(parts) == 15: # 检查是否为零件数据行
31
+ # 提取 x, y, z 坐标
32
+ x, y, z = float(parts[2]), float(parts[3]), float(parts[4])
33
+
34
+ # 更新最小坐标值
35
+ min_x = min(min_x, x)
36
+ min_y = min(min_y, y)
37
+ min_z = min(min_z, z)
38
+
39
+ # 新的 LDR 文件内容
40
+ new_lines = []
41
+
42
+ # 第二遍遍历文件,更新坐标
43
+ for line in lines:
44
+ parts = line.split() # 按空格分割行数据
45
+ if len(parts) == 15: # 检查是否为零件数据行
46
+ # 提取 x, y, z 坐标
47
+ x, y, z = float(parts[2]), float(parts[3]), float(parts[4])
48
+
49
+ # 计算新的坐标
50
+ x_new = round(x - min_x)
51
+ y_new = round(y - min_y)
52
+ z_new = round(z - min_z)
53
+
54
+ # 更新坐标
55
+ parts[2] = str(x_new)
56
+ parts[3] = str(y_new)
57
+ parts[4] = str(z_new)
58
+
59
+ max_x = max(max_x, x_new)
60
+ max_y = max(max_y, y_new)
61
+ max_z = max(max_z, z_new)
62
+
63
+ # 将修改后的行添加到新文件内容中
64
+ new_lines.append(" ".join(parts) + "\n")
65
+ else:
66
+ # 如果是非零件行,直接添加到新文件中
67
+ new_lines.append(line)
68
+
69
+ # 设置新的 LDR 文件路径
70
+ modified_file_path = os.path.join(folder_path, f'modified_{folder_name}.ldr')
71
+
72
+ # 将修改后的内容写入新的 LDR 文件
73
+ with open(modified_file_path, "w") as file:
74
+ file.writelines(new_lines)
75
+
76
+ print(f"LDR file '{original_file_path}' has been modified and saved as '{modified_file_path}'")
77
+
78
+ print(f"Maximum coordinates found across all LDR files:")
79
+ print(f"Max X: {max_x}")
80
+ print(f"Max Y: {max_y}")
81
+ print(f"Max Z: {max_z}")
82
+
83
+ # import os
84
+
85
+ # # 设置data目录路径
86
+ # data_dir = '/public/home/wangshuo/gap/assembly/data/car_1k/ldr/car_81' # 替换为你的data目录路径
87
+
88
+ # # 设置原始LDR文件和保存的新LDR文件的路径
89
+ # original_file_path = os.path.join(data_dir, 'car_81.ldr')
90
+ # modified_file_path = os.path.join(data_dir, 'modified_model.ldr')
91
+
92
+ # # 打开并读取原始 LDR 文件
93
+ # with open(original_file_path, "r") as file:
94
+ # lines = file.readlines()
95
+
96
+ # # 新的 LDR 文件内容
97
+ # new_lines = []
98
+
99
+ # # 处理每一行
100
+ # for line in lines:
101
+ # parts = line.split() # 按空格分割行数据
102
+ # if len(parts) == 15: # 检查是否为零件数据行
103
+ # # 提取 x, y, z 坐标
104
+ # x, y, z = float(parts[2]), float(parts[3]), float(parts[4])
105
+
106
+ # # 对坐标进行加法操作
107
+ # x_new = x + 6000
108
+ # y_new = y + 150
109
+ # z_new = z + 2000
110
+
111
+ # # 更新坐标
112
+ # parts[2] = str(x_new)
113
+ # parts[3] = str(y_new)
114
+ # parts[4] = str(z_new)
115
+
116
+ # # 将修改后的行添加到新文件内容中
117
+ # new_lines.append(" ".join(parts) + "\n")
118
+ # else:
119
+ # # 如果是非零件行,直接添加到新文件中
120
+ # new_lines.append(line)
121
+
122
+ # # 将修改后的内容写入新的 LDR 文件
123
+ # with open(modified_file_path, "w") as file:
124
+ # file.writelines(new_lines)
125
+
126
+ # print(f"LDR file has been modified and saved as '{modified_file_path}'")
127
+