| from scipy.spatial.transform import Rotation as R |
| import torch, numpy as np |
|
|
| BOF_body = np.array([ |
| [-120.0, -130.0, -80.0, |
| -120.0, 0.0, -80.0, |
| -180.0, -160.0, -180.0, |
| -180.0, 0.0, -180.0, |
| -120.0, -50.0, -90.0, |
| -120.0, -50.0, -90.0, |
| ], |
| [90.0, 0.0, 80.0, |
| 90.0, 130.0, 80.0, |
| 180.0, 0.0, 180.0, |
| 180.0, 160.0, 180.0, |
| 90.0, 50.0, 90.0, |
| 90.0, 50.0, 90.0]]) / 180 * np.pi |
|
|
|
|
| def _to_numpy_flat_last3(x: torch.Tensor): |
| dev, dt = x.device, x.dtype |
| x_np = x.detach().cpu().numpy().reshape(-1, 3) |
| return x_np, x.shape, dev, dt |
|
|
| def _from_numpy(x_np: np.ndarray, shape, dev, dt): |
| y = torch.from_numpy(x_np.reshape(*shape)) |
| if dt in (torch.float32, torch.float64): y = y.to(dt) |
| return y.to(dev) |
|
|
|
|
|
|
| def euler_XYZ_to_axis_angle_scipy(e: torch.Tensor, degrees: bool = False) -> torch.Tensor: |
| e_np, shape, dev, dt = _to_numpy_flat_last3(e) |
| aa_np = R.from_euler('XYZ', e_np, degrees=degrees).as_rotvec() |
| return _from_numpy(aa_np, shape, dev, dt) |
|
|
| def axis_angle_to_euler_XYZ_scipy(aa: torch.Tensor): |
| aa_np, shape, dev, dt = _to_numpy_flat_last3(aa) |
| e_np = R.from_rotvec(aa_np).as_euler('XYZ', degrees=False) |
| return _from_numpy(e_np, shape, dev, dt) |
|
|
|
|
|
|
|
|
| def apply_angular_constraints(body_pose): |
| device = body_pose.device |
| B = body_pose.shape[0] |
| body_pose = body_pose.view(B, 21, 3) |
|
|
| |
| minC = torch.tensor(BOF_body[0], dtype=body_pose.dtype, device=device).view(6,3) |
| maxC = torch.tensor(BOF_body[1], dtype=body_pose.dtype, device=device).view(6,3) |
|
|
| aa_arms = body_pose[:, 15:, :] |
| |
| e_arms = axis_angle_to_euler_XYZ_scipy(aa_arms) |
|
|
| e_clamp = torch.clamp(e_arms, minC, maxC) |
|
|
| aa_new = euler_XYZ_to_axis_angle_scipy(e_clamp) |
|
|
| body_pose[:, 15:, :] = aa_new |
| |
| return body_pose.view(B, -1) |
|
|
|
|
|
|
|
|