forked from vkoskiv/c-ray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_obj.py
90 lines (78 loc) · 2.69 KB
/
merge_obj.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import trimesh
import numpy as np
import json
def uniform_scale_matrix(size: float):
return np.array([
[size, 0.0, 0.0, 0.0],
[0.0, size, 0.0, 0.0],
[0.0, 0.0, size, 0.0],
[0.0, 0.0, 0.0, 1.0]
])
def rotation_x_matrix(theta: float):
rad = np.deg2rad(theta)
return np.array([
[1.0, 0.0, 0.0, 0.0],
[0.0, np.cos(rad), - np.sin(rad), 0.0],
[1.0, np.sin(rad), np.cos(rad), 0.0],
[0.0, 0.0, 0.0, 1.0]
])
def rotation_y_matrix(theta: float):
rad = np.deg2rad(theta)
return np.array([
[np.cos(rad), 0.0, np.sin(rad), 0.0],
[0.0, 1.0, 0.0, 0.0],
[- np.sin(rad), 0.0, np.cos(rad), 0.0],
[0.0, 0.0, 0.0, 1.0]
])
def rotation_z_matrix(theta: float):
rad = np.deg2rad(theta)
return np.array([
[np.cos(rad), - np.sin(rad), 0.0, 0.0],
[np.sin(rad), np.cos(rad), 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]
])
def translation_matrix(x, y, z):
return np.array([
[1.0, 0.0, 0.0, x],
[0.0, 1.0, 0.0, y],
[0.0, 0.0, 1.0, z],
[0.0, 0.0, 0.0, 1.0]
])
with open('input/volcanic_archipelago.json', 'r') as f:
existing_data = json.load(f)
scene = trimesh.Scene()
for m in existing_data["scene"]["meshes"]:
fileName = "input/" + m["fileName"]
transforms = m["pick_instances"][0]["transforms"]
mesh = trimesh.load(fileName)
if m["fileName"] == "volcano.obj" or m["fileName"] == "water_surface.obj":
mesh.invert()
t = np.array([
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]
])
for i in range(len(transforms)):
if transforms[i]["type"] == "translate":
x, y, z = transforms[i]["x"], transforms[i]["y"], transforms[i]["z"]
t = np.matmul(t, translation_matrix(x, y, z))
for i in range(len(transforms)):
if transforms[i]["type"] == "rotateX":
deg = transforms[i]["degrees"]
t = np.matmul(t, rotation_x_matrix(deg))
elif transforms[i]["type"] == "rotateY":
deg = transforms[i]["degrees"]
t = np.matmul(t, rotation_y_matrix(deg))
elif transforms[i]["type"] == "rotateZ":
deg = transforms[i]["degrees"]
t = np.matmul(t, rotation_z_matrix(deg))
for i in range(len(transforms)):
if transforms[i]["type"] == "scaleUniform":
scale = transforms[i]["scale"]
t = np.matmul(t, uniform_scale_matrix(scale))
transformed_mesh = mesh.apply_transform(t)
scene.add_geometry(transformed_mesh)
with open("scene.obj", 'w+') as file:
file.write(trimesh.exchange.obj.export_obj(scene))