| 12 | |
| 13 | |
| 14 | class RigidMotion: |
| 15 | def __init__(self, R: np.ndarray = None, t: np.ndarray = None, H: np.ndarray = None): |
| 16 | if H is not None: |
| 17 | R = H[:3, :3] |
| 18 | t = H[:3, 3] |
| 19 | if R is None: |
| 20 | R = np.eye(3) |
| 21 | if t is None: |
| 22 | t = np.zeros(3) |
| 23 | |
| 24 | self.R = copy.deepcopy(R) # 3*3 rotation matrix |
| 25 | self.t = copy.deepcopy(np.reshape(t, (3, 1))) # 3*1 position |
| 26 | |
| 27 | def homogeneous_matrix(self): |
| 28 | """returns the 4*4 homogeneous matrix.""" |
| 29 | H = np.zeros((4, 4)) |
| 30 | H[0:3, 0:3] = self.R |
| 31 | H[0:3, 3:4] = self.t |
| 32 | H[3, 3] = 1 |
| 33 | return H |
| 34 | |
| 35 | @staticmethod |
| 36 | def invert_homogeneous_matrix(H: np.ndarray) -> np.ndarray: |
| 37 | """returns the inverse of a homegeneous matrix.""" |
| 38 | H_rm = RigidMotion(H=H) |
| 39 | return RigidMotion.inverse(H_rm).homogeneous_matrix() |
| 40 | |
| 41 | @staticmethod |
| 42 | def inverse(H): |
| 43 | """Returns H^-1 given RigidMotion H""" |
| 44 | inv_R = (H.R).T |
| 45 | inv_t = -1.0 * (inv_R @ H.t) |
| 46 | return RigidMotion(R=inv_R, t=inv_t) |
| 47 | |
| 48 | @staticmethod |
| 49 | def exp_skew_symmetric(S: np.ndarray, t: float = 1.0, theta: float = None) -> np.ndarray: |
| 50 | """Returns exp(t*S) of a 3*3 skew symmetric matrix to get a rotation matrix.""" |
| 51 | |
| 52 | if (S ** 2).sum() < 1e-8: |
| 53 | # S is all zero |
| 54 | return np.eye(3) |
| 55 | |
| 56 | if theta is None: |
| 57 | s = np.array([S[2, 1], S[0, 2], S[1, 0]]) |
| 58 | theta = np.sqrt(np.sum(s ** 2.0)) |
| 59 | |
| 60 | angle = t * theta |
| 61 | theta_square = theta * theta |
| 62 | |
| 63 | R = np.eye(3) + np.sin(angle) / theta * S + (1 - np.cos(angle)) / theta_square * (S @ S) |
| 64 | return R |
| 65 | |
| 66 | @staticmethod |
| 67 | def log_rotation(R: np.ndarray): |
| 68 | """Return the log(R) where R is a rotation matrix. So it returns a skew symmetric matrix.""" |
| 69 | arg = 0.5 * (R[0, 0] + R[1, 1] + R[2, 2] - 1) # in [-1, 1] |
| 70 | if arg > -1: |
| 71 | if arg < 1: |
no outgoing calls
no test coverage detected