Spaces:
Paused
Paused
Add code/cube3d/render/render_obj4view.py
Browse files
code/cube3d/render/render_obj4view.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pymeshlab as ml
|
| 3 |
+
|
| 4 |
+
input_dir = "/public/home/wangshuo/gap/assembly/data/car_1k/obj"
|
| 5 |
+
output_base_dir = "/public/home/wangshuo/gap/assembly/data/car_1k/pose_4view"
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import pymeshlab as ml
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# 四个视角的旋转角度
|
| 13 |
+
rotations = [
|
| 14 |
+
{"x": -90, "y": 0},
|
| 15 |
+
{"x": -90, "y": 90},
|
| 16 |
+
{"x": -90, "y": 180},
|
| 17 |
+
{"x": -90, "y": 270},
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
# 旋转矩阵计算函数
|
| 21 |
+
def get_rotation_matrix(x_angle, y_angle):
|
| 22 |
+
# 计算绕 x 轴的旋转矩阵
|
| 23 |
+
x_rot_matrix = np.array([
|
| 24 |
+
[1, 0, 0],
|
| 25 |
+
[0, np.cos(np.radians(x_angle)), -np.sin(np.radians(x_angle))],
|
| 26 |
+
[0, np.sin(np.radians(x_angle)), np.cos(np.radians(x_angle))]
|
| 27 |
+
])
|
| 28 |
+
|
| 29 |
+
# 计算绕 y 轴的旋转矩阵
|
| 30 |
+
y_rot_matrix = np.array([
|
| 31 |
+
[np.cos(np.radians(y_angle)), 0, np.sin(np.radians(y_angle))],
|
| 32 |
+
[0, 1, 0],
|
| 33 |
+
[-np.sin(np.radians(y_angle)), 0, np.cos(np.radians(y_angle))]
|
| 34 |
+
])
|
| 35 |
+
|
| 36 |
+
# 合成旋转矩阵(先绕 x 轴旋转,再绕 y 轴旋转)
|
| 37 |
+
rotation_matrix = np.dot(y_rot_matrix, x_rot_matrix)
|
| 38 |
+
|
| 39 |
+
return rotation_matrix
|
| 40 |
+
|
| 41 |
+
# 遍历每个 obj 文件
|
| 42 |
+
for filename in os.listdir(input_dir):
|
| 43 |
+
if filename.endswith(".obj"):
|
| 44 |
+
obj_path = os.path.join(input_dir, filename)
|
| 45 |
+
base_name = os.path.splitext(filename)[0]
|
| 46 |
+
|
| 47 |
+
# 创建渲染输出目录
|
| 48 |
+
for i, rotation in enumerate(rotations):
|
| 49 |
+
output_dir = os.path.join(output_base_dir, f"view_{i+1}")
|
| 50 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 51 |
+
|
| 52 |
+
# 加载网格
|
| 53 |
+
ms = ml.MeshSet()
|
| 54 |
+
ms.load_new_mesh(obj_path)
|
| 55 |
+
|
| 56 |
+
# 获取旋转矩阵
|
| 57 |
+
rot_matrix = get_rotation_matrix(rotation["x"], rotation["y"])
|
| 58 |
+
|
| 59 |
+
# 应用旋转矩阵
|
| 60 |
+
ms.apply_filter("transform_apply", matrix=rot_matrix.flatten().tolist())
|
| 61 |
+
|
| 62 |
+
# 渲染并保存图片
|
| 63 |
+
output_image_path = os.path.join(output_dir, f"{base_name}.png")
|
| 64 |
+
ms.save_current_mesh_image(output_image_path, image_width=800, image_height=600)
|
| 65 |
+
|
| 66 |
+
print(f"Rendered {filename} at rotation x={rotation['x']}°, y={rotation['y']}° -> {output_image_path}")
|
| 67 |
+
|
| 68 |
+
print("All OBJ files rendered.")
|