import numpy as np import open3d as o3d import math,os #矫正模型的方向,目前只支持水平旋转, #55084 55090 55092 55114 55367 55370 55372 55730 55844 55849 56559 56654 56750 75408 def rotate_vertex(x, y, z, axis, angle_degrees): """ 围绕任意轴旋转顶点。 axis是一个表示旋转轴的三元组 (ax, ay, az)。 angle_degrees是旋转角度,以度为单位。 """ # 将角度转换为弧度 angle_radians = math.radians(angle_degrees) # 将旋转轴转换为单位向量 axis = np.array(axis) axis = axis / np.linalg.norm(axis) # 将顶点位置转换为向量 v = np.array([x, y, z]) # 应用罗德里格斯旋转公式 v_rotated = (v * math.cos(angle_radians) + np.cross(axis, v) * math.sin(angle_radians) + axis * np.dot(axis, v) * (1 - math.cos(angle_radians))) return v_rotated.tolist() def transform_obj_file(source_file, target_file, axis, angle_degrees): """ 读取 OBJ 文件,应用旋转,然后将结果保存到另一个文件。 """ with open(source_file, 'r') as src: lines = src.readlines() with open(target_file, 'w') as tgt: for line in lines: if line.startswith('v '): parts = line.split() x, y, z = map(float, parts[1:4]) x_new, y_new, z_new = rotate_vertex(x, y, z, axis, angle_degrees) tgt.write(f"v {x_new} {y_new} {z_new}\n") else: tgt.write(line) def list_files(directory): for root, dirs, files in os.walk(directory): for file in files: yield os.path.join(root, file) # # #遍历文件夹下的所有文件 # folder_path = 'G://obj_fix/print/wrong' # nums = 0 # rotate_axis = (0, 1, 0) # 比如绕X轴旋转 # for file_path in list_files(folder_path): # #print(file_path) G://obj_fix/print/around/preview\9023_preview_117.png # if "_preview.png" in file_path: # continue # if "_right.png" in file_path: # continue # if "_wait.png" in file_path: # continue # #获取pid 从 "G://obj_fix/print/around/preview\9023_preview_117.png" 中获取 9023 he 117 # arrData = file_path.split('\\') # lastData = arrData[-1] # arrLastData = lastData.split('_') # if len(arrLastData) < 3: # print(lastData) # continue # pid = arrLastData[0] # angle = arrLastData[2].split('.')[0] # #判断 angle 第一个数字是否为 0 # source_obj = f'G://obj_fix/print/{pid}/{pid}.obj' # target_obj = f'G://obj_fix/print/{pid}/{pid}_new.obj' # nums +=1 # print(f"当前正在处理{pid},已处理至-{nums}",) # if str(angle[0]) == "0": # rotate_angle = -int(angle) # transform_obj_file(source_obj, target_obj, rotate_axis, rotate_angle) # else: # rotate_angle = int(angle) # transform_obj_file(source_obj, target_obj, rotate_axis, rotate_angle) # print(int(angle)) # print(angle,angle[0]) # # 加载模型 # mesh = o3d.io.read_triangle_mesh(target_obj, enable_post_processing=True) # # 创建渲染窗口并添加模型 # vis = o3d.visualization.Visualizer() # vis.create_window() # vis.add_geometry(mesh) # # 设置渲染参数 # render_option = vis.get_render_option() # render_option.point_size = 1.0 # # 渲染模型 # vis.run() # vis.destroy_window() pid = "90864" # 98894 115123 source_obj = f'G://obj_fix/auto/{pid}/{pid}.obj' target_obj = f'G://obj_fix/auto/{pid}/{pid}_90.obj' #遍历文件夹下的所有文件 /data/datasets/temp/auto_preview_need_around for file_path in list_files('G://obj_fix/auto'): pid = file_path.split('\\')[-1].split('_')[0] # 使用示例: rotate_axis = (1, 0, 0) # 比如绕X轴旋转 0 x 轴 rotate_angle = -90 # 以度为单位 transform_obj_file(source_obj, target_obj, rotate_axis, rotate_angle) # rotate_axis = (0, 1, 0) # 比如绕X轴旋转 0 x 轴 # rotate_angle = 60 # 以度为单位 # transform_obj_file(target_obj, target_obj, rotate_axis, rotate_angle)