graphics_utils.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #
  2. # Copyright (C) 2023, Inria
  3. # GRAPHDECO research group, https://team.inria.fr/graphdeco
  4. # All rights reserved.
  5. #
  6. # This software is free for non-commercial, research and evaluation use
  7. # under the terms of the LICENSE.md file.
  8. #
  9. # For inquiries contact george.drettakis@inria.fr
  10. #
  11. import torch
  12. import math
  13. import numpy as np
  14. from typing import NamedTuple
  15. class BasicPointCloud(NamedTuple):
  16. points : np.array
  17. colors : np.array
  18. normals : np.array
  19. def geom_transform_points(points, transf_matrix):
  20. P, _ = points.shape
  21. ones = torch.ones(P, 1, dtype=points.dtype, device=points.device)
  22. points_hom = torch.cat([points, ones], dim=1)
  23. points_out = torch.matmul(points_hom, transf_matrix.unsqueeze(0))
  24. denom = points_out[..., 3:] + 0.0000001
  25. return (points_out[..., :3] / denom).squeeze(dim=0)
  26. def getWorld2View(R, t):
  27. Rt = np.zeros((4, 4))
  28. Rt[:3, :3] = R.transpose()
  29. Rt[:3, 3] = t
  30. Rt[3, 3] = 1.0
  31. return np.float32(Rt)
  32. def getWorld2View2(R, t, translate=np.array([.0, .0, .0]), scale=1.0):
  33. Rt = np.zeros((4, 4))
  34. Rt[:3, :3] = R.transpose()
  35. Rt[:3, 3] = t
  36. Rt[3, 3] = 1.0
  37. C2W = np.linalg.inv(Rt)
  38. cam_center = C2W[:3, 3]
  39. cam_center = (cam_center + translate) * scale
  40. C2W[:3, 3] = cam_center
  41. Rt = np.linalg.inv(C2W)
  42. return np.float32(Rt)
  43. def getProjectionMatrix(znear, zfar, fovX, fovY):
  44. tanHalfFovY = math.tan((fovY / 2))
  45. tanHalfFovX = math.tan((fovX / 2))
  46. top = tanHalfFovY * znear
  47. bottom = -top
  48. right = tanHalfFovX * znear
  49. left = -right
  50. P = torch.zeros(4, 4)
  51. z_sign = 1.0
  52. P[0, 0] = 2.0 * znear / (right - left)
  53. P[1, 1] = 2.0 * znear / (top - bottom)
  54. P[0, 2] = (right + left) / (right - left)
  55. P[1, 2] = (top + bottom) / (top - bottom)
  56. P[3, 2] = z_sign
  57. P[2, 2] = z_sign * zfar / (zfar - znear)
  58. P[2, 3] = -(zfar * znear) / (zfar - znear)
  59. return P
  60. def fov2focal(fov, pixels):
  61. return pixels / (2 * math.tan(fov / 2))
  62. def focal2fov(focal, pixels):
  63. return 2*math.atan(pixels/(2*focal))