# import numpy as np # def calculate_rotation_and_center_of_mass(angle_x, angle_y, angle_z, points): # """计算某一组旋转角度后的重心""" # # 计算绕X轴、Y轴和Z轴的旋转矩阵 # R_x = np.array([ # [1, 0, 0], # [0, np.cos(np.radians(angle_x)), -np.sin(np.radians(angle_x))], # [0, np.sin(np.radians(angle_x)), np.cos(np.radians(angle_x))] # ]) # # R_y = np.array([ # [np.cos(np.radians(angle_y)), 0, np.sin(np.radians(angle_y))], # [0, 1, 0], # [-np.sin(np.radians(angle_y)), 0, np.cos(np.radians(angle_y))] # ]) # # R_z = np.array([ # [np.cos(np.radians(angle_z)), -np.sin(np.radians(angle_z)), 0], # [np.sin(np.radians(angle_z)), np.cos(np.radians(angle_z)), 0], # [0, 0, 1] # ]) # # # 综合旋转矩阵 # R = R_z @ R_y @ R_x # # # 执行旋转 # rotated_points = points @ R.T # # # 计算最小z值 # min_z = np.min(rotated_points[:, 2]) # # # 计算平移向量,将最小Z值平移到0 # translation_vector = np.array([0, 0, -min_z]) # rotated_points += translation_vector # # # 计算重心 # center_of_mass = np.mean(rotated_points, axis=0) # # return center_of_mass[2], angle_x, angle_y, angle_z # # def parallel_rotation(points, angle_step=3): # """顺序计算最优旋转角度(单线程)""" # min_center_of_mass_y = float('inf') # best_angle_x, best_angle_y, best_angle_z = 0, 0, 0 # # # 遍历所有角度组合 # for angle_x in range(0, 360, angle_step): # for angle_y in range(0, 360, angle_step): # for angle_z in range(0, 360, angle_step): # center_of_mass_z, ax, ay, az = calculate_rotation_and_center_of_mass( # angle_x, angle_y, angle_z, points # ) # if center_of_mass_z < min_center_of_mass_y: # min_center_of_mass_y = center_of_mass_z # best_angle_x, best_angle_y, best_angle_z = ax, ay, az # # return best_angle_x, best_angle_y, best_angle_z, min_center_of_mass_y import bpy import time import os from pathlib import Path def clear_scene(): """清空当前场景中的所有对象""" bpy.ops.object.select_all(action='SELECT') bpy.ops.object.delete() def test_bpy_io(obj_path, output_path): """测试 bpy 的 OBJ 读写性能""" # 读取 OBJ start_time = time.time() bpy.ops.import_scene.obj(filepath=obj_path) read_time = time.time() - start_time # 确保场景中有对象 if not bpy.context.scene.objects: raise ValueError("未成功导入 OBJ 文件") # 写入 OBJ start_time = time.time() bpy.ops.export_scene.obj( filepath=output_path, use_selection=False, # 导出所有对象 use_materials=False, # 不导出材质(加快速度) ) write_time = time.time() - start_time # 清理场景 clear_scene() return write_time, read_time def test_folder_objs_with_bpy(folder_path, output_folder="output_objs_bpy"): """测试文件夹中所有 OBJ 文件的读写性能(使用 bpy)""" # 确保输出文件夹存在 Path(output_folder).mkdir(exist_ok=True) # 收集所有 OBJ 文件 obj_files = [f for f in os.listdir(folder_path) if f.lower().endswith('.obj')] if not obj_files: print(f"在文件夹 {folder_path} 中未找到 OBJ 文件") return print(f"找到 {len(obj_files)} 个 OBJ 文件,开始测试...") results = [] for obj_file in obj_files: input_path = os.path.join(folder_path, obj_file) output_path = os.path.join(output_folder, f"bpy_{obj_file}") print(f"\n测试文件: {obj_file}") try: write_time, read_time = test_bpy_io(input_path, output_path) file_size = os.path.getsize(input_path) / (1024 * 1024) # MB print(f" 文件大小: {file_size:.2f} MB") print(f" bpy 读取时间: {read_time:.3f}s") print(f" bpy 写入时间: {write_time:.3f}s") results.append({ "filename": obj_file, "size_mb": file_size, "read_time": read_time, "write_time": write_time, }) except Exception as e: print(f" 处理 {obj_file} 时出错: {e}") # 计算平均时间 if results: avg_read = sum(r["read_time"] for r in results) / len(results) avg_write = sum(r["write_time"] for r in results) / len(results) print("\n=== 汇总结果 ===") print(f"平均读取时间: {avg_read:.3f}s") print(f"平均写入时间: {avg_write:.3f}s") if __name__ == "__main__": # 设置包含OBJ文件的文件夹路径 obj_folder = "/data/datasets_20t/9_big/" # 替换为你的OBJ文件夹路径 # 运行测试 test_folder_objs_with_bpy(obj_folder)