Spaces:
Paused
Paused
Upload pytorch3d_stub/pytorch3d/renderer/__init__.py with huggingface_hub
Browse files
pytorch3d_stub/pytorch3d/renderer/__init__.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""pytorch3d.renderer stub – look_at_view_transform only."""
|
| 2 |
+
import torch
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
def look_at_view_transform(dist=1.0, elev=0.0, azim=0.0, degrees=True, eye=None,
|
| 6 |
+
at=((0, 0, 0),), up=((0, 1, 0),), device="cpu"):
|
| 7 |
+
"""Compute R, T for look-at camera transform (pytorch3d convention)."""
|
| 8 |
+
if eye is not None:
|
| 9 |
+
if not isinstance(eye, torch.Tensor):
|
| 10 |
+
eye = torch.tensor(eye, dtype=torch.float32, device=device)
|
| 11 |
+
if eye.dim() == 1:
|
| 12 |
+
eye = eye.unsqueeze(0)
|
| 13 |
+
else:
|
| 14 |
+
if degrees:
|
| 15 |
+
elev_r = math.radians(elev) if isinstance(elev, (int, float)) else torch.deg2rad(torch.tensor(elev, dtype=torch.float32))
|
| 16 |
+
azim_r = math.radians(azim) if isinstance(azim, (int, float)) else torch.deg2rad(torch.tensor(azim, dtype=torch.float32))
|
| 17 |
+
else:
|
| 18 |
+
elev_r = elev
|
| 19 |
+
azim_r = azim
|
| 20 |
+
if isinstance(elev_r, (int, float)):
|
| 21 |
+
x = dist * math.cos(elev_r) * math.sin(azim_r)
|
| 22 |
+
y = dist * math.sin(elev_r)
|
| 23 |
+
z = dist * math.cos(elev_r) * math.cos(azim_r)
|
| 24 |
+
eye = torch.tensor([[x, y, z]], dtype=torch.float32, device=device)
|
| 25 |
+
else:
|
| 26 |
+
x = dist * torch.cos(elev_r) * torch.sin(azim_r)
|
| 27 |
+
y = dist * torch.sin(elev_r)
|
| 28 |
+
z = dist * torch.cos(elev_r) * torch.cos(azim_r)
|
| 29 |
+
eye = torch.stack([x, y, z], dim=-1).unsqueeze(0).to(device)
|
| 30 |
+
|
| 31 |
+
if not isinstance(at, torch.Tensor):
|
| 32 |
+
at = torch.tensor(at, dtype=torch.float32, device=device)
|
| 33 |
+
if at.dim() == 1:
|
| 34 |
+
at = at.unsqueeze(0)
|
| 35 |
+
if not isinstance(up, torch.Tensor):
|
| 36 |
+
up = torch.tensor(up, dtype=torch.float32, device=device)
|
| 37 |
+
if up.dim() == 1:
|
| 38 |
+
up = up.unsqueeze(0)
|
| 39 |
+
|
| 40 |
+
z_axis = eye - at
|
| 41 |
+
z_axis = z_axis / z_axis.norm(dim=-1, keepdim=True).clamp(min=1e-8)
|
| 42 |
+
x_axis = torch.cross(up.expand_as(z_axis), z_axis, dim=-1)
|
| 43 |
+
x_axis = x_axis / x_axis.norm(dim=-1, keepdim=True).clamp(min=1e-8)
|
| 44 |
+
y_axis = torch.cross(z_axis, x_axis, dim=-1)
|
| 45 |
+
|
| 46 |
+
R = torch.stack([x_axis, y_axis, z_axis], dim=-1) # (N, 3, 3)
|
| 47 |
+
T = -torch.bmm(R.transpose(1, 2), eye.unsqueeze(-1)).squeeze(-1) # (N, 3)
|
| 48 |
+
return R, T
|