Download uika/models/rendering/camera.py from vegagoto123/UIKA: direct link, hf CLI and curl.
- Browser
- Download file 2.76 kB
-
https://huggingface.co/vegagoto123/UIKA/resolve/main/uika/models/rendering/camera.py
- Command line
-
hf download hf://vegagoto123/UIKA/uika/models/rendering/camera.py
-
curl -L -o camera.py https://huggingface.co/vegagoto123/UIKA/resolve/main/uika/models/rendering/camera.py
2.76 kB
| import math | |
| import torch | |
| import numpy as np | |
| def getWorld2View2(R, t, translate=np.array([.0, .0, .0]), scale=1.0): | |
| Rt = np.zeros((4, 4)) | |
| Rt[:3, :3] = R.transpose() | |
| Rt[:3, 3] = t | |
| Rt[3, 3] = 1.0 | |
| C2W = np.linalg.inv(Rt) | |
| cam_center = C2W[:3, 3] | |
| cam_center = (cam_center + translate) * scale | |
| C2W[:3, 3] = cam_center | |
| Rt = np.linalg.inv(C2W) | |
| return np.float32(Rt) | |
| def getProjectionMatrix(znear, zfar, fovX, fovY, K, h, w): | |
| tanHalfFovY = math.tan((fovY / 2)) | |
| tanHalfFovX = math.tan((fovX / 2)) | |
| if K is None: | |
| # print("using colmap style") | |
| top = tanHalfFovY * znear | |
| bottom = -top | |
| right = tanHalfFovX * znear | |
| left = -right | |
| else: | |
| assert h is not None and w is not None | |
| near_fx = znear / K[0, 0] | |
| near_fy = znear / K[1, 1] | |
| left = - (w - K[0, 2]) * near_fx | |
| right = K[0, 2] * near_fx | |
| bottom = (K[1, 2] - h) * near_fy | |
| top = K[1, 2] * near_fy | |
| P = torch.zeros(4, 4) | |
| z_sign = 1.0 | |
| P[0, 0] = 2.0 * znear / (right - left) | |
| P[1, 1] = 2.0 * znear / (top - bottom) | |
| P[0, 2] = (right + left) / (right - left) | |
| P[1, 2] = (top + bottom) / (top - bottom) | |
| P[3, 2] = z_sign | |
| P[2, 2] = z_sign * zfar / (zfar - znear) | |
| P[2, 3] = -(zfar * znear) / (zfar - znear) | |
| return P | |
| def intrinsic_to_fov(intrinsic, w, h): | |
| fx, fy = intrinsic[0, 0], intrinsic[1, 1] | |
| fov_x = 2 * torch.arctan2(w, 2 * fx) | |
| fov_y = 2 * torch.arctan2(h, 2 * fy) | |
| return fov_x, fov_y | |
| class Camera: | |
| def __init__(self, w2c, intrinsic, FoVx, FoVy, height, width, trans=np.array([0.0, 0.0, 0.0]), scale=1.0) -> None: | |
| self.FoVx = FoVx | |
| self.FoVy = FoVy | |
| self.height = int(height) | |
| self.width = int(width) | |
| self.world_view_transform = w2c.transpose(0, 1) | |
| self.intrinsic = intrinsic | |
| self.zfar = 100.0 | |
| self.znear = 0.01 | |
| self.trans = trans | |
| self.scale = scale | |
| self.projection_matrix = getProjectionMatrix( | |
| znear=self.znear, zfar=self.zfar, fovX=self.FoVx, fovY=self.FoVy, | |
| K=self.intrinsic, h=self.height, w=self.width | |
| ).transpose(0, 1).to(w2c.device) | |
| self.full_proj_transform = (self.world_view_transform.unsqueeze(0).bmm(self.projection_matrix.unsqueeze(0))).squeeze(0) | |
| self.camera_center = self.world_view_transform.inverse()[3, :3] | |
| def from_c2w(c2w, intrinsic, height, width): | |
| w2c = torch.inverse(c2w) | |
| FoVx, FoVy = intrinsic_to_fov(intrinsic, w=torch.tensor(width, device=w2c.device), h=torch.tensor(height, device=w2c.device)) | |
| return Camera(w2c=w2c, intrinsic=intrinsic, FoVx=FoVx, FoVy=FoVy, height=height, width=width) | |