Load mesh files as o3d meshes, randomly combine meshes to form more complex meshes. Args: mesh_filenames: list of filenames Returns: list of o3d meshes
(
mesh_filenames: T.Union[str, T.List[str]],
mesh_scale: float = 1.,
min_num_mesh: int = 1,
max_num_mesh: int = 2,
radius_scale: float = 2,
total_combined: int = None,
printout: bool = True,
)
| 346 | |
| 347 | |
| 348 | def load_and_mix_mesh_filename( |
| 349 | mesh_filenames: T.Union[str, T.List[str]], |
| 350 | mesh_scale: float = 1., |
| 351 | min_num_mesh: int = 1, |
| 352 | max_num_mesh: int = 2, |
| 353 | radius_scale: float = 2, |
| 354 | total_combined: int = None, |
| 355 | printout: bool = True, |
| 356 | ) -> T.List[structures.Mesh]: |
| 357 | """ |
| 358 | Load mesh files as o3d meshes, randomly combine meshes to form |
| 359 | more complex meshes. |
| 360 | |
| 361 | Args: |
| 362 | mesh_filenames: |
| 363 | list of filenames |
| 364 | Returns: |
| 365 | list of o3d meshes |
| 366 | """ |
| 367 | |
| 368 | if isinstance(mesh_filenames, str): # str, list, or tuple |
| 369 | mesh_filenames = [mesh_filenames] |
| 370 | |
| 371 | if total_combined is None: |
| 372 | total_combined = len(mesh_filenames) |
| 373 | |
| 374 | # randomly mix and match meshes |
| 375 | mixed_mesh_filenames: T.List[T.List[str]] = [] |
| 376 | for i in range(total_combined): |
| 377 | num = np.random.randint(low=min_num_mesh, high=max_num_mesh+1) |
| 378 | fns = np.random.choice(mesh_filenames, size=(num,)) |
| 379 | mixed_mesh_filenames.append(fns) |
| 380 | |
| 381 | all_meshes = [] |
| 382 | for i in range(len(mixed_mesh_filenames)): |
| 383 | sub_mesh_filenames = mixed_mesh_filenames[i] |
| 384 | |
| 385 | if printout: |
| 386 | print(f'Loading {i}/{len(mixed_mesh_filenames)} meshes: {sub_mesh_filenames}...', flush=True) |
| 387 | |
| 388 | meshes = [] |
| 389 | for j in range(len(sub_mesh_filenames)): |
| 390 | mesh_filename = sub_mesh_filenames[j] |
| 391 | mesh = o3d.io.read_triangle_mesh(mesh_filename, enable_post_processing=True) |
| 392 | # preprocess the mesh after reading mesh, before building mesh dataset |
| 393 | mesh = mesh_utils.preprocess_mesh( |
| 394 | mesh, |
| 395 | scale=mesh_scale, |
| 396 | ) # mesh_scale, centered |
| 397 | meshes.append(mesh) |
| 398 | |
| 399 | # rotate and move meshes to different centers |
| 400 | for j in range(len(meshes)): |
| 401 | euler_angles = np.random.rand(3) * 2 * np.pi |
| 402 | R = meshes[j].get_rotation_matrix_from_xyz(euler_angles) |
| 403 | meshes[j].rotate(R, center=(0, 0, 0)) |
| 404 | |
| 405 | c_w = (np.random.rand(3) - 0.5) * 2 * mesh_scale * radius_scale |