object-assembler / code /cube3d /training /absolute_position.py
0xZohar's picture
Add code/cube3d/training/absolute_position.py
19c667c verified
import os
max_x, max_y, max_z = float('-inf'), float('-inf'), float('-inf')
# 设置根目录路径,所有文件夹都位于此目录下
root_dir = '/public/home/wangshuo/gap/assembly/data' # 替换为你的根目录路径
# 遍历根目录下的每个文件夹
for folder_name in os.listdir(root_dir):
folder_path = os.path.join(root_dir, folder_name)
if os.path.isdir(folder_path):
# 设置原始 LDR 文件路径
original_file_path = os.path.join(folder_path, f'{folder_name}.ldr')
# 检查文件是否存在
if os.path.exists(original_file_path):
# 打开并读取原始 LDR 文件
with open(original_file_path, "r") as file:
lines = file.readlines()
if len(lines) > 310:
continue
# 初始化最小坐标值
min_x, min_y, min_z = float('inf'), float('inf'), float('inf')
# 第一遍遍历文件找到最小坐标值
for line in lines:
parts = line.split() # 按空格分割行数据
if len(parts) == 15: # 检查是否为零件数据行
# 提取 x, y, z 坐标
x, y, z = float(parts[2]), float(parts[3]), float(parts[4])
# 更新最小坐标值
min_x = min(min_x, x)
min_y = min(min_y, y)
min_z = min(min_z, z)
# 新的 LDR 文件内容
new_lines = []
# 第二遍遍历文件,更新坐标
for line in lines:
parts = line.split() # 按空格分割行数据
if len(parts) == 15: # 检查是否为零件数据行
# 提取 x, y, z 坐标
x, y, z = float(parts[2]), float(parts[3]), float(parts[4])
# 计算新的坐标
x_new = round(x - min_x)
y_new = round(y - min_y)
z_new = round(z - min_z)
# 更新坐标
parts[2] = str(x_new)
parts[3] = str(y_new)
parts[4] = str(z_new)
max_x = max(max_x, x_new)
max_y = max(max_y, y_new)
max_z = max(max_z, z_new)
# 将修改后的行添加到新文件内容中
new_lines.append(" ".join(parts) + "\n")
else:
# 如果是非零件行,直接添加到新文件中
new_lines.append(line)
# 设置新的 LDR 文件路径
modified_file_path = os.path.join(folder_path, f'modified_{folder_name}.ldr')
# 将修改后的内容写入新的 LDR 文件
with open(modified_file_path, "w") as file:
file.writelines(new_lines)
print(f"LDR file '{original_file_path}' has been modified and saved as '{modified_file_path}'")
print(f"Maximum coordinates found across all LDR files:")
print(f"Max X: {max_x}")
print(f"Max Y: {max_y}")
print(f"Max Z: {max_z}")
# import os
# # 设置data目录路径
# data_dir = '/public/home/wangshuo/gap/assembly/data/car_1k/ldr/car_81' # 替换为你的data目录路径
# # 设置原始LDR文件和保存的新LDR文件的路径
# original_file_path = os.path.join(data_dir, 'car_81.ldr')
# modified_file_path = os.path.join(data_dir, 'modified_model.ldr')
# # 打开并读取原始 LDR 文件
# with open(original_file_path, "r") as file:
# lines = file.readlines()
# # 新的 LDR 文件内容
# new_lines = []
# # 处理每一行
# for line in lines:
# parts = line.split() # 按空格分割行数据
# if len(parts) == 15: # 检查是否为零件数据行
# # 提取 x, y, z 坐标
# x, y, z = float(parts[2]), float(parts[3]), float(parts[4])
# # 对坐标进行加法操作
# x_new = x + 6000
# y_new = y + 150
# z_new = z + 2000
# # 更新坐标
# parts[2] = str(x_new)
# parts[3] = str(y_new)
# parts[4] = str(z_new)
# # 将修改后的行添加到新文件内容中
# new_lines.append(" ".join(parts) + "\n")
# else:
# # 如果是非零件行,直接添加到新文件中
# new_lines.append(line)
# # 将修改后的内容写入新的 LDR 文件
# with open(modified_file_path, "w") as file:
# file.writelines(new_lines)
# print(f"LDR file has been modified and saved as '{modified_file_path}'")