| |
| import torch |
| import os |
| import numpy as np |
| import trimesh |
| from common.utils.utils_hand import batch_euler2matzxy, coordtrans |
|
|
| from kornia.geometry.conversions import axis_angle_to_rotation_matrix, quaternion_to_rotation_matrix, euler_from_quaternion, rotation_matrix_to_quaternion, quaternion_from_euler, rotation_matrix_to_axis_angle |
|
|
|
|
| euler_coordtrans_RIGHT = np.array([ |
| [ 15, -6, 10, |
| 15, 1, -5, |
| 15, -15, 0, |
| 0, 20, 2, |
| 0, 10, -2, |
| 0, 10, 10, |
| 15, 25, 10, |
| 10, 37, 8, |
| 7, 35, 8, |
| 0, 12, 5, |
| 0, 25, -5, |
| 0, 25, 15, |
| -30, -75, -35, |
| -30, -45, -30, |
| -30, -45, -30]]) / 180 * np.pi |
| euler_coordtrans_LEFT = np.array([ |
| [ 15, 6, -10, |
| 15, -1, 5, |
| 15, 15, 0, |
| 0, -20, -2, |
| 0, -10, 2, |
| 0, -10, -10, |
| 15, -25, -10, |
| 10, -37, -8, |
| 7, -35, -8, |
| 0, -12, -5, |
| 0, -25, 5, |
| 0, -25, -15, |
| -30, 75, 35, |
| -30, 45, 30, |
| -30, 45, 30]]) / 180 * np.pi |
|
|
| |
|
|
| BOF_RIGHT = np.array([ |
| [ |
| 0, -30, -40, |
| 0, 0, 0, |
| 0, 0, -5, |
|
|
| 0, -22.5, -40, |
| 0, 0, 0, |
| 0, 0, -5, |
|
|
| 0, -25, -40, |
| 0, 0, 0, |
| 0, 0, -5, |
| |
| 0, -22.5, -40, |
| 0, 0, 0, |
| 0, 0, -5, |
| |
| -180, -60, -15, |
| -180,-5, -180, |
| -180, -5, -10], |
|
|
| |
| [ |
| |
| 0, 30, 90, |
| 0, 0, 110, |
| 0, 0, 90, |
|
|
| 0, 22.5, 90, |
| 0, 0, 110, |
| 0, 0, 90, |
|
|
| 0, 25, 90, |
| 0, 0, 135, |
| 0, 0, 90, |
|
|
| 0, 22.5, 90, |
| 0, 0, 120, |
| 0, 0, 90, |
|
|
| 180, 60, 90, |
| 180, 5, 80, |
| 180, 5, 80]]) |
|
|
| BOF_LEFT = np.array([ |
| [ |
| |
| 0, -30, -90, |
| 0, 0,-110, |
| 0, 0, -90, |
|
|
| 0, -22.5, -90, |
| 0, 0, -110, |
| 0, 0, -90, |
| |
| 0, -25, -90, |
| 0, 0, -135, |
| 0, 0, -90, |
| |
| 0, -22.5, -90, |
| 0, 0, -120, |
| 0, 0, -90, |
| |
| -180, -60, -90, |
| -180, -5, -80, |
| -180, -5, -80], |
|
|
| [ |
| |
| 0, 30, 40, |
| 0, 0, 0, |
| 0, 0, 5, |
|
|
| 0, 22.5, 40, |
| 0, 0, 0, |
| 0, 0, 5, |
| |
| 0, 25, 40, |
| 0, 0, 0, |
| 0, 0, 5, |
|
|
| 0, 22.5, 40, |
| 0, 0, 0, |
| 0, 0, 5, |
|
|
| 180, 60, 15, |
| 180, 5, 180, |
| 180, 5, 10], |
| ]) |
|
|
| def do_clipping(input_angles, side, rotation_type): |
|
|
| device = input_angles.device |
|
|
| if side == "LEFT": |
| max = torch.tensor(BOF_LEFT[1,:], dtype=torch.float32).reshape(15,3).to(device) |
| min = torch.tensor(BOF_LEFT[0,:], dtype=torch.float32).reshape(15,3).to(device) |
| min_np = torch.deg2rad(min) |
| max_np = torch.deg2rad(max) |
|
|
| elif side == "RIGHT": |
| max = torch.tensor(BOF_RIGHT[1,:], dtype=torch.float32).reshape(15,3).to(device) |
| min = torch.tensor(BOF_RIGHT[0,:], dtype=torch.float32).reshape(15,3).to(device) |
| min_np = torch.deg2rad(min) |
| max_np = torch.deg2rad(max) |
|
|
| input_angles = input_angles.reshape(-1,15,3) |
|
|
| B, J, N = input_angles.shape |
| |
| if rotation_type == "quat": |
|
|
| org_mats = quaternion_to_rotation_matrix(input_angles).float() |
|
|
| elif rotation_type == "aa": |
|
|
| org_mats = axis_angle_to_rotation_matrix(input_angles.reshape(-1, 3)).float() |
|
|
| if len(org_mats.shape)!=4: |
|
|
| org_mats = org_mats.unsqueeze(0) |
|
|
| rotmat_mano = org_mats.clone() |
|
|
| rotmat_mano = rotmat_mano.view(-1,15,3,3) |
|
|
| name = f"euler_coordtrans_{side.upper()}" |
| mat = globals()[name] |
|
|
| local2global = torch.from_numpy(mat).type(torch.float32).to(device) |
|
|
| local2global = batch_euler2matzxy(local2global.view(-1,3)).view(-1,15,3,3).expand(1,-1,-1,-1) |
|
|
| anatom_space_mats = coordtrans(rotmat_mano, local2global, 0) |
|
|
| mat_to_quat = rotation_matrix_to_quaternion(anatom_space_mats) |
|
|
| roll, pitch, yaw = euler_from_quaternion(mat_to_quat[:, :, 0], mat_to_quat[:, :, 1], mat_to_quat[:, :, 2], mat_to_quat[:, :, 3]) |
|
|
| euler_from_quat = torch.stack([roll, pitch, yaw], dim=-1) |
|
|
| euler_from_quat = torch.clip(euler_from_quat, min_np, max_np) |
|
|
| qw, qx, qy, qz = quaternion_from_euler(euler_from_quat[:,:, 0], euler_from_quat[:,:, 1], euler_from_quat[:,:, 2]) |
|
|
| rots_anat = torch.stack([qw, qx, qy, qz], dim=-1) |
|
|
| anat_mats = quaternion_to_rotation_matrix(rots_anat) |
|
|
| corrected_mano_space = coordtrans(anat_mats, local2global, 1) |
|
|
| corrected_aa = rotation_matrix_to_axis_angle(corrected_mano_space).reshape(B, J*3) |
|
|
| return corrected_aa |
|
|
|
|