|
| 1 | +from common.quaternion import * |
| 2 | +import scipy.ndimage.filters as filters |
| 3 | + |
| 4 | +class Skeleton(object): |
| 5 | + def __init__(self, offset, kinematic_tree, device): |
| 6 | + self.device = device |
| 7 | + self._raw_offset_np = offset.numpy() |
| 8 | + self._raw_offset = offset.clone().detach().to(device).float() |
| 9 | + self._kinematic_tree = kinematic_tree |
| 10 | + self._offset = None |
| 11 | + self._parents = [0] * len(self._raw_offset) |
| 12 | + self._parents[0] = -1 |
| 13 | + for chain in self._kinematic_tree: |
| 14 | + for j in range(1, len(chain)): |
| 15 | + self._parents[chain[j]] = chain[j-1] |
| 16 | + |
| 17 | + def njoints(self): |
| 18 | + return len(self._raw_offset) |
| 19 | + |
| 20 | + def offset(self): |
| 21 | + return self._offset |
| 22 | + |
| 23 | + def set_offset(self, offsets): |
| 24 | + self._offset = offsets.clone().detach().to(self.device).float() |
| 25 | + |
| 26 | + def kinematic_tree(self): |
| 27 | + return self._kinematic_tree |
| 28 | + |
| 29 | + def parents(self): |
| 30 | + return self._parents |
| 31 | + |
| 32 | + # joints (batch_size, joints_num, 3) |
| 33 | + def get_offsets_joints_batch(self, joints): |
| 34 | + assert len(joints.shape) == 3 |
| 35 | + _offsets = self._raw_offset.expand(joints.shape[0], -1, -1).clone() |
| 36 | + for i in range(1, self._raw_offset.shape[0]): |
| 37 | + _offsets[:, i] = torch.norm(joints[:, i] - joints[:, self._parents[i]], p=2, dim=1)[:, None] * _offsets[:, i] |
| 38 | + |
| 39 | + self._offset = _offsets.detach() |
| 40 | + return _offsets |
| 41 | + |
| 42 | + # joints (joints_num, 3) |
| 43 | + def get_offsets_joints(self, joints): |
| 44 | + assert len(joints.shape) == 2 |
| 45 | + _offsets = self._raw_offset.clone() |
| 46 | + for i in range(1, self._raw_offset.shape[0]): |
| 47 | + # print(joints.shape) |
| 48 | + _offsets[i] = torch.norm(joints[i] - joints[self._parents[i]], p=2, dim=0) * _offsets[i] |
| 49 | + |
| 50 | + self._offset = _offsets.detach() |
| 51 | + return _offsets |
| 52 | + |
| 53 | + # face_joint_idx should follow the order of right hip, left hip, right shoulder, left shoulder |
| 54 | + # joints (batch_size, joints_num, 3) |
| 55 | + def inverse_kinematics_np(self, joints, face_joint_idx, smooth_forward=False): |
| 56 | + assert len(face_joint_idx) == 4 |
| 57 | + '''Get Forward Direction''' |
| 58 | + l_hip, r_hip, sdr_r, sdr_l = face_joint_idx |
| 59 | + across1 = joints[:, r_hip] - joints[:, l_hip] |
| 60 | + across2 = joints[:, sdr_r] - joints[:, sdr_l] |
| 61 | + across = across1 + across2 |
| 62 | + across = across / np.sqrt((across**2).sum(axis=-1))[:, np.newaxis] |
| 63 | + # print(across1.shape, across2.shape) |
| 64 | + |
| 65 | + # forward (batch_size, 3) |
| 66 | + forward = np.cross(np.array([[0, 1, 0]]), across, axis=-1) |
| 67 | + if smooth_forward: |
| 68 | + forward = filters.gaussian_filter1d(forward, 20, axis=0, mode='nearest') |
| 69 | + # forward (batch_size, 3) |
| 70 | + forward = forward / np.sqrt((forward**2).sum(axis=-1))[..., np.newaxis] |
| 71 | + |
| 72 | + '''Get Root Rotation''' |
| 73 | + target = np.array([[0,0,1]]).repeat(len(forward), axis=0) |
| 74 | + root_quat = qbetween_np(forward, target) |
| 75 | + |
| 76 | + '''Inverse Kinematics''' |
| 77 | + # quat_params (batch_size, joints_num, 4) |
| 78 | + # print(joints.shape[:-1]) |
| 79 | + quat_params = np.zeros(joints.shape[:-1] + (4,)) |
| 80 | + # print(quat_params.shape) |
| 81 | + root_quat[0] = np.array([[1.0, 0.0, 0.0, 0.0]]) |
| 82 | + quat_params[:, 0] = root_quat |
| 83 | + # quat_params[0, 0] = np.array([[1.0, 0.0, 0.0, 0.0]]) |
| 84 | + for chain in self._kinematic_tree: |
| 85 | + R = root_quat |
| 86 | + for j in range(len(chain) - 1): |
| 87 | + # (batch, 3) |
| 88 | + u = self._raw_offset_np[chain[j+1]][np.newaxis,...].repeat(len(joints), axis=0) |
| 89 | + # print(u.shape) |
| 90 | + # (batch, 3) |
| 91 | + v = joints[:, chain[j+1]] - joints[:, chain[j]] |
| 92 | + v = v / np.sqrt((v**2).sum(axis=-1))[:, np.newaxis] |
| 93 | + # print(u.shape, v.shape) |
| 94 | + rot_u_v = qbetween_np(u, v) |
| 95 | + |
| 96 | + R_loc = qmul_np(qinv_np(R), rot_u_v) |
| 97 | + |
| 98 | + quat_params[:,chain[j + 1], :] = R_loc |
| 99 | + R = qmul_np(R, R_loc) |
| 100 | + |
| 101 | + return quat_params |
| 102 | + |
| 103 | + # Be sure root joint is at the beginning of kinematic chains |
| 104 | + def forward_kinematics(self, quat_params, root_pos, skel_joints=None, do_root_R=True): |
| 105 | + # quat_params (batch_size, joints_num, 4) |
| 106 | + # joints (batch_size, joints_num, 3) |
| 107 | + # root_pos (batch_size, 3) |
| 108 | + if skel_joints is not None: |
| 109 | + offsets = self.get_offsets_joints_batch(skel_joints) |
| 110 | + if len(self._offset.shape) == 2: |
| 111 | + offsets = self._offset.expand(quat_params.shape[0], -1, -1) |
| 112 | + joints = torch.zeros(quat_params.shape[:-1] + (3,)).to(self.device) |
| 113 | + joints[:, 0] = root_pos |
| 114 | + for chain in self._kinematic_tree: |
| 115 | + if do_root_R: |
| 116 | + R = quat_params[:, 0] |
| 117 | + else: |
| 118 | + R = torch.tensor([[1.0, 0.0, 0.0, 0.0]]).expand(len(quat_params), -1).detach().to(self.device) |
| 119 | + for i in range(1, len(chain)): |
| 120 | + R = qmul(R, quat_params[:, chain[i]]) |
| 121 | + offset_vec = offsets[:, chain[i]] |
| 122 | + joints[:, chain[i]] = qrot(R, offset_vec) + joints[:, chain[i-1]] |
| 123 | + return joints |
| 124 | + |
| 125 | + # Be sure root joint is at the beginning of kinematic chains |
| 126 | + def forward_kinematics_np(self, quat_params, root_pos, skel_joints=None, do_root_R=True): |
| 127 | + # quat_params (batch_size, joints_num, 4) |
| 128 | + # joints (batch_size, joints_num, 3) |
| 129 | + # root_pos (batch_size, 3) |
| 130 | + if skel_joints is not None: |
| 131 | + skel_joints = torch.from_numpy(skel_joints) |
| 132 | + offsets = self.get_offsets_joints_batch(skel_joints) |
| 133 | + if len(self._offset.shape) == 2: |
| 134 | + offsets = self._offset.expand(quat_params.shape[0], -1, -1) |
| 135 | + offsets = offsets.numpy() |
| 136 | + joints = np.zeros(quat_params.shape[:-1] + (3,)) |
| 137 | + joints[:, 0] = root_pos |
| 138 | + for chain in self._kinematic_tree: |
| 139 | + if do_root_R: |
| 140 | + R = quat_params[:, 0] |
| 141 | + else: |
| 142 | + R = np.array([[1.0, 0.0, 0.0, 0.0]]).repeat(len(quat_params), axis=0) |
| 143 | + for i in range(1, len(chain)): |
| 144 | + R = qmul_np(R, quat_params[:, chain[i]]) |
| 145 | + offset_vec = offsets[:, chain[i]] |
| 146 | + joints[:, chain[i]] = qrot_np(R, offset_vec) + joints[:, chain[i - 1]] |
| 147 | + return joints |
| 148 | + |
| 149 | + def forward_kinematics_cont6d_np(self, cont6d_params, root_pos, skel_joints=None, do_root_R=True): |
| 150 | + # cont6d_params (batch_size, joints_num, 6) |
| 151 | + # joints (batch_size, joints_num, 3) |
| 152 | + # root_pos (batch_size, 3) |
| 153 | + if skel_joints is not None: |
| 154 | + skel_joints = torch.from_numpy(skel_joints) |
| 155 | + offsets = self.get_offsets_joints_batch(skel_joints) |
| 156 | + if len(self._offset.shape) == 2: |
| 157 | + offsets = self._offset.expand(cont6d_params.shape[0], -1, -1) |
| 158 | + offsets = offsets.numpy() |
| 159 | + joints = np.zeros(cont6d_params.shape[:-1] + (3,)) |
| 160 | + joints[:, 0] = root_pos |
| 161 | + for chain in self._kinematic_tree: |
| 162 | + if do_root_R: |
| 163 | + matR = cont6d_to_matrix_np(cont6d_params[:, 0]) |
| 164 | + else: |
| 165 | + matR = np.eye(3)[np.newaxis, :].repeat(len(cont6d_params), axis=0) |
| 166 | + for i in range(1, len(chain)): |
| 167 | + matR = np.matmul(matR, cont6d_to_matrix_np(cont6d_params[:, chain[i]])) |
| 168 | + offset_vec = offsets[:, chain[i]][..., np.newaxis] |
| 169 | + # print(matR.shape, offset_vec.shape) |
| 170 | + joints[:, chain[i]] = np.matmul(matR, offset_vec).squeeze(-1) + joints[:, chain[i-1]] |
| 171 | + return joints |
| 172 | + |
| 173 | + def forward_kinematics_cont6d(self, cont6d_params, root_pos, skel_joints=None, do_root_R=True): |
| 174 | + # cont6d_params (batch_size, joints_num, 6) |
| 175 | + # joints (batch_size, joints_num, 3) |
| 176 | + # root_pos (batch_size, 3) |
| 177 | + if skel_joints is not None: |
| 178 | + # skel_joints = torch.from_numpy(skel_joints) |
| 179 | + offsets = self.get_offsets_joints_batch(skel_joints) |
| 180 | + if len(self._offset.shape) == 2: |
| 181 | + offsets = self._offset.expand(cont6d_params.shape[0], -1, -1) |
| 182 | + joints = torch.zeros(cont6d_params.shape[:-1] + (3,)).to(cont6d_params.device) |
| 183 | + joints[..., 0, :] = root_pos |
| 184 | + for chain in self._kinematic_tree: |
| 185 | + if do_root_R: |
| 186 | + matR = cont6d_to_matrix(cont6d_params[:, 0]) |
| 187 | + else: |
| 188 | + matR = torch.eye(3).expand((len(cont6d_params), -1, -1)).detach().to(cont6d_params.device) |
| 189 | + for i in range(1, len(chain)): |
| 190 | + matR = torch.matmul(matR, cont6d_to_matrix(cont6d_params[:, chain[i]])) |
| 191 | + offset_vec = offsets[:, chain[i]].unsqueeze(-1) |
| 192 | + # print(matR.shape, offset_vec.shape) |
| 193 | + joints[:, chain[i]] = torch.matmul(matR, offset_vec).squeeze(-1) + joints[:, chain[i-1]] |
| 194 | + return joints |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
0 commit comments