-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathgs_simulation.py
379 lines (330 loc) · 13 KB
/
gs_simulation.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import sys
sys.path.append("gaussian-splatting")
import argparse
import math
import cv2
import torch
import os
import numpy as np
import json
from tqdm import tqdm
# Gaussian splatting dependencies
from utils.sh_utils import eval_sh
from scene.gaussian_model import GaussianModel
from diff_gaussian_rasterization import (
GaussianRasterizationSettings,
GaussianRasterizer,
)
from scene.cameras import Camera as GSCamera
from gaussian_renderer import render, GaussianModel
from utils.system_utils import searchForMaxIteration
from utils.graphics_utils import focal2fov
# MPM dependencies
from mpm_solver_warp.engine_utils import *
from mpm_solver_warp.mpm_solver_warp import MPM_Simulator_WARP
import warp as wp
# Particle filling dependencies
from particle_filling.filling import *
# Utils
from utils.decode_param import *
from utils.transformation_utils import *
from utils.camera_view_utils import *
from utils.render_utils import *
wp.init()
wp.config.verify_cuda = True
ti.init(arch=ti.cuda, device_memory_GB=8.0)
class PipelineParamsNoparse:
"""Same as PipelineParams but without argument parser."""
def __init__(self):
self.convert_SHs_python = False
self.compute_cov3D_python = False
self.debug = False
def load_checkpoint(model_path, sh_degree=3, iteration=-1):
# Find checkpoint
checkpt_dir = os.path.join(model_path, "point_cloud")
if iteration == -1:
iteration = searchForMaxIteration(checkpt_dir)
checkpt_path = os.path.join(
checkpt_dir, f"iteration_{iteration}", "point_cloud.ply"
)
# Load guassians
gaussians = GaussianModel(sh_degree)
gaussians.load_ply(checkpt_path)
return gaussians
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_path", type=str, required=True)
parser.add_argument("--output_path", type=str, default=None)
parser.add_argument("--config", type=str, required=True)
parser.add_argument("--output_ply", action="store_true")
parser.add_argument("--output_h5", action="store_true")
parser.add_argument("--render_img", action="store_true")
parser.add_argument("--compile_video", action="store_true")
parser.add_argument("--white_bg", action="store_true")
parser.add_argument("--debug", action="store_true")
args = parser.parse_args()
if not os.path.exists(args.model_path):
AssertionError("Model path does not exist!")
if not os.path.exists(args.config):
AssertionError("Scene config does not exist!")
if args.output_path is not None and not os.path.exists(args.output_path):
os.makedirs(args.output_path)
# load scene config
print("Loading scene config...")
(
material_params,
bc_params,
time_params,
preprocessing_params,
camera_params,
) = decode_param_json(args.config)
# load gaussians
print("Loading gaussians...")
model_path = args.model_path
gaussians = load_checkpoint(model_path)
pipeline = PipelineParamsNoparse()
pipeline.compute_cov3D_python = True
background = (
torch.tensor([1, 1, 1], dtype=torch.float32, device="cuda")
if args.white_bg
else torch.tensor([0, 0, 0], dtype=torch.float32, device="cuda")
)
# init the scene
print("Initializing scene and pre-processing...")
params = load_params_from_gs(gaussians, pipeline)
init_pos = params["pos"]
init_cov = params["cov3D_precomp"]
init_screen_points = params["screen_points"]
init_opacity = params["opacity"]
init_shs = params["shs"]
# throw away low opacity kernels
mask = init_opacity[:, 0] > preprocessing_params["opacity_threshold"]
init_pos = init_pos[mask, :]
init_cov = init_cov[mask, :]
init_opacity = init_opacity[mask, :]
init_screen_points = init_screen_points[mask, :]
init_shs = init_shs[mask, :]
# rorate and translate object
if args.debug:
if not os.path.exists("./log"):
os.makedirs("./log")
particle_position_tensor_to_ply(
init_pos,
"./log/init_particles.ply",
)
rotation_matrices = generate_rotation_matrices(
torch.tensor(preprocessing_params["rotation_degree"]),
preprocessing_params["rotation_axis"],
)
rotated_pos = apply_rotations(init_pos, rotation_matrices)
if args.debug:
particle_position_tensor_to_ply(rotated_pos, "./log/rotated_particles.ply")
# select a sim area and save params of unslected particles
unselected_pos, unselected_cov, unselected_opacity, unselected_shs = (
None,
None,
None,
None,
)
if preprocessing_params["sim_area"] is not None:
boundary = preprocessing_params["sim_area"]
assert len(boundary) == 6
mask = torch.ones(rotated_pos.shape[0], dtype=torch.bool).to(device="cuda")
for i in range(3):
mask = torch.logical_and(mask, rotated_pos[:, i] > boundary[2 * i])
mask = torch.logical_and(mask, rotated_pos[:, i] < boundary[2 * i + 1])
unselected_pos = init_pos[~mask, :]
unselected_cov = init_cov[~mask, :]
unselected_opacity = init_opacity[~mask, :]
unselected_shs = init_shs[~mask, :]
rotated_pos = rotated_pos[mask, :]
init_cov = init_cov[mask, :]
init_opacity = init_opacity[mask, :]
init_shs = init_shs[mask, :]
transformed_pos, scale_origin, original_mean_pos = transform2origin(rotated_pos)
transformed_pos = shift2center111(transformed_pos)
# modify covariance matrix accordingly
init_cov = apply_cov_rotations(init_cov, rotation_matrices)
init_cov = scale_origin * scale_origin * init_cov
if args.debug:
particle_position_tensor_to_ply(
transformed_pos,
"./log/transformed_particles.ply",
)
# fill particles if needed
gs_num = transformed_pos.shape[0]
device = "cuda:0"
filling_params = preprocessing_params["particle_filling"]
if filling_params is not None:
print("Filling internal particles...")
mpm_init_pos = fill_particles(
pos=transformed_pos,
opacity=init_opacity,
cov=init_cov,
grid_n=filling_params["n_grid"],
max_samples=filling_params["max_particles_num"],
grid_dx=material_params["grid_lim"] / filling_params["n_grid"],
density_thres=filling_params["density_threshold"],
search_thres=filling_params["search_threshold"],
max_particles_per_cell=filling_params["max_partciels_per_cell"],
search_exclude_dir=filling_params["search_exclude_direction"],
ray_cast_dir=filling_params["ray_cast_direction"],
boundary=filling_params["boundary"],
smooth=filling_params["smooth"],
).to(device=device)
if args.debug:
particle_position_tensor_to_ply(mpm_init_pos, "./log/filled_particles.ply")
else:
mpm_init_pos = transformed_pos.to(device=device)
# init the mpm solver
print("Initializing MPM solver and setting up boundary conditions...")
mpm_init_vol = get_particle_volume(
mpm_init_pos,
material_params["n_grid"],
material_params["grid_lim"] / material_params["n_grid"],
unifrom=material_params["material"] == "sand",
).to(device=device)
if filling_params is not None and filling_params["visualize"] == True:
shs, opacity, mpm_init_cov = init_filled_particles(
mpm_init_pos[:gs_num],
init_shs,
init_cov,
init_opacity,
mpm_init_pos[gs_num:],
)
gs_num = mpm_init_pos.shape[0]
else:
mpm_init_cov = torch.zeros((mpm_init_pos.shape[0], 6), device=device)
mpm_init_cov[:gs_num] = init_cov
shs = init_shs
opacity = init_opacity
if args.debug:
print("check *.ply files to see if it's ready for simulation")
# set up the mpm solver
mpm_solver = MPM_Simulator_WARP(10)
mpm_solver.load_initial_data_from_torch(
mpm_init_pos,
mpm_init_vol,
mpm_init_cov,
n_grid=material_params["n_grid"],
grid_lim=material_params["grid_lim"],
)
mpm_solver.set_parameters_dict(material_params)
# Note: boundary conditions may depend on mass, so the order cannot be changed!
set_boundary_conditions(mpm_solver, bc_params, time_params)
mpm_solver.finalize_mu_lam()
# camera setting
mpm_space_viewpoint_center = (
torch.tensor(camera_params["mpm_space_viewpoint_center"]).reshape((1, 3)).cuda()
)
mpm_space_vertical_upward_axis = (
torch.tensor(camera_params["mpm_space_vertical_upward_axis"])
.reshape((1, 3))
.cuda()
)
(
viewpoint_center_worldspace,
observant_coordinates,
) = get_center_view_worldspace_and_observant_coordinate(
mpm_space_viewpoint_center,
mpm_space_vertical_upward_axis,
rotation_matrices,
scale_origin,
original_mean_pos,
)
# run the simulation
if args.output_ply or args.output_h5:
directory_to_save = os.path.join(args.output_path, "simulation_ply")
if not os.path.exists(directory_to_save):
os.makedirs(directory_to_save)
save_data_at_frame(
mpm_solver,
directory_to_save,
0,
save_to_ply=args.output_ply,
save_to_h5=args.output_h5,
)
substep_dt = time_params["substep_dt"]
frame_dt = time_params["frame_dt"]
frame_num = time_params["frame_num"]
step_per_frame = int(frame_dt / substep_dt)
opacity_render = opacity
shs_render = shs
height = None
width = None
for frame in tqdm(range(frame_num)):
current_camera = get_camera_view(
model_path,
default_camera_index=camera_params["default_camera_index"],
center_view_world_space=viewpoint_center_worldspace,
observant_coordinates=observant_coordinates,
show_hint=camera_params["show_hint"],
init_azimuthm=camera_params["init_azimuthm"],
init_elevation=camera_params["init_elevation"],
init_radius=camera_params["init_radius"],
move_camera=camera_params["move_camera"],
current_frame=frame,
delta_a=camera_params["delta_a"],
delta_e=camera_params["delta_e"],
delta_r=camera_params["delta_r"],
)
rasterize = initialize_resterize(
current_camera, gaussians, pipeline, background
)
for step in range(step_per_frame):
mpm_solver.p2g2p(frame, substep_dt, device=device)
if args.output_ply or args.output_h5:
save_data_at_frame(
mpm_solver,
directory_to_save,
frame + 1,
save_to_ply=args.output_ply,
save_to_h5=args.output_h5,
)
if args.render_img:
pos = mpm_solver.export_particle_x_to_torch()[:gs_num].to(device)
cov3D = mpm_solver.export_particle_cov_to_torch()
rot = mpm_solver.export_particle_R_to_torch()
cov3D = cov3D.view(-1, 6)[:gs_num].to(device)
rot = rot.view(-1, 3, 3)[:gs_num].to(device)
pos = apply_inverse_rotations(
undotransform2origin(
undoshift2center111(pos), scale_origin, original_mean_pos
),
rotation_matrices,
)
cov3D = cov3D / (scale_origin * scale_origin)
cov3D = apply_inverse_cov_rotations(cov3D, rotation_matrices)
opacity = opacity_render
shs = shs_render
if preprocessing_params["sim_area"] is not None:
pos = torch.cat([pos, unselected_pos], dim=0)
cov3D = torch.cat([cov3D, unselected_cov], dim=0)
opacity = torch.cat([opacity_render, unselected_opacity], dim=0)
shs = torch.cat([shs_render, unselected_shs], dim=0)
colors_precomp = convert_SH(shs, current_camera, gaussians, pos, rot)
rendering, raddi = rasterize(
means3D=pos,
means2D=init_screen_points,
shs=None,
colors_precomp=colors_precomp,
opacities=opacity,
scales=None,
rotations=None,
cov3D_precomp=cov3D,
)
cv2_img = rendering.permute(1, 2, 0).detach().cpu().numpy()
cv2_img = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB)
if height is None or width is None:
height = cv2_img.shape[0] // 2 * 2
width = cv2_img.shape[1] // 2 * 2
assert args.output_path is not None
cv2.imwrite(
os.path.join(args.output_path, f"{frame}.png".rjust(8, "0")),
255 * cv2_img,
)
if args.render_img and args.compile_video:
fps = int(1.0 / time_params["frame_dt"])
os.system(
f"ffmpeg -framerate {fps} -i {args.output_path}/%04d.png -c:v libx264 -s {width}x{height} -y -pix_fmt yuv420p {args.output_path}/output.mp4"
)