(
xyz,
rgb=None,
path='output.ply',
)
| 396 | |
| 397 | |
| 398 | def write_ply( |
| 399 | xyz, |
| 400 | rgb=None, |
| 401 | path='output.ply', |
| 402 | ) -> None: |
| 403 | if torch.is_tensor(xyz): |
| 404 | xyz = xyz.detach().cpu().numpy() |
| 405 | |
| 406 | if torch.is_tensor(rgb): |
| 407 | rgb = rgb.detach().cpu().numpy() |
| 408 | |
| 409 | if rgb is not None and rgb.max() > 1: |
| 410 | rgb = rgb / 255. |
| 411 | |
| 412 | xyz = rotate_target_dim_to_last_axis(xyz, 3) |
| 413 | xyz = xyz.reshape(-1, 3) |
| 414 | |
| 415 | if rgb is not None: |
| 416 | rgb = rotate_target_dim_to_last_axis(rgb, 3) |
| 417 | rgb = rgb.reshape(-1, 3) |
| 418 | |
| 419 | if rgb is None: |
| 420 | min_coord = np.min(xyz, axis=0) |
| 421 | max_coord = np.max(xyz, axis=0) |
| 422 | normalized_coord = (xyz - min_coord) / (max_coord - min_coord + 1e-8) |
| 423 | |
| 424 | hue = 0.7 * normalized_coord[:,0] + 0.2 * normalized_coord[:,1] + 0.1 * normalized_coord[:,2] |
| 425 | hsv = np.stack([hue, 0.9*np.ones_like(hue), 0.8*np.ones_like(hue)], axis=1) |
| 426 | |
| 427 | c = hsv[:,2:] * hsv[:,1:2] |
| 428 | x = c * (1 - np.abs( (hsv[:,0:1]*6) % 2 - 1 )) |
| 429 | m = hsv[:,2:] - c |
| 430 | |
| 431 | rgb = np.zeros_like(hsv) |
| 432 | cond = (0 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 1) |
| 433 | rgb[cond] = np.hstack([c[cond], x[cond], np.zeros_like(x[cond])]) |
| 434 | cond = (1 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 2) |
| 435 | rgb[cond] = np.hstack([x[cond], c[cond], np.zeros_like(x[cond])]) |
| 436 | cond = (2 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 3) |
| 437 | rgb[cond] = np.hstack([np.zeros_like(x[cond]), c[cond], x[cond]]) |
| 438 | cond = (3 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 4) |
| 439 | rgb[cond] = np.hstack([np.zeros_like(x[cond]), x[cond], c[cond]]) |
| 440 | cond = (4 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 5) |
| 441 | rgb[cond] = np.hstack([x[cond], np.zeros_like(x[cond]), c[cond]]) |
| 442 | cond = (5 <= hsv[:,0]*6%6) & (hsv[:,0]*6%6 < 6) |
| 443 | rgb[cond] = np.hstack([c[cond], np.zeros_like(x[cond]), x[cond]]) |
| 444 | rgb = (rgb + m) |
| 445 | |
| 446 | dtype = [ |
| 447 | ("x", "f4"), |
| 448 | ("y", "f4"), |
| 449 | ("z", "f4"), |
| 450 | ("nx", "f4"), |
| 451 | ("ny", "f4"), |
| 452 | ("nz", "f4"), |
| 453 | ("red", "u1"), |
| 454 | ("green", "u1"), |
| 455 | ("blue", "u1"), |
nothing calls this directly
no test coverage detected