You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert.py 2.0 kB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """TODO: Add docstring."""
  2. import numpy as np
  3. from scipy.spatial.transform import Rotation
  4. def convert_quaternion_to_euler(quat):
  5. """Convert Quaternion (xyzw) to Euler angles (rpy)."""
  6. # Normalize
  7. quat = quat / np.linalg.norm(quat)
  8. return Rotation.from_quat(quat).as_euler("xyz")
  9. def convert_euler_to_quaternion(euler):
  10. """Convert Euler angles (rpy) to Quaternion (xyzw)."""
  11. return Rotation.from_euler("xyz", euler).as_quat()
  12. def convert_euler_to_rotation_matrix(euler):
  13. """Convert Euler angles (rpy) to rotation matrix (3x3)."""
  14. return Rotation.from_euler("xyz", euler).as_matrix()
  15. def convert_rotation_matrix_to_euler(rotmat):
  16. """Convert rotation matrix (3x3) to Euler angles (rpy)."""
  17. r = Rotation.from_matrix(rotmat)
  18. return r.as_euler("xyz", degrees=False)
  19. def normalize_vector(v):
  20. """TODO: Add docstring."""
  21. v_mag = np.linalg.norm(v, axis=-1, keepdims=True)
  22. v_mag = np.maximum(v_mag, 1e-8)
  23. return v / v_mag
  24. def cross_product(u, v):
  25. """TODO: Add docstring."""
  26. i = u[:, 1] * v[:, 2] - u[:, 2] * v[:, 1]
  27. j = u[:, 2] * v[:, 0] - u[:, 0] * v[:, 2]
  28. k = u[:, 0] * v[:, 1] - u[:, 1] * v[:, 0]
  29. return np.stack((i, j, k), axis=1)
  30. def compute_rotation_matrix_from_ortho6d(ortho6d):
  31. """TODO: Add docstring."""
  32. x_raw = ortho6d[:, 0:3]
  33. y_raw = ortho6d[:, 3:6]
  34. x = normalize_vector(x_raw)
  35. z = cross_product(x, y_raw)
  36. z = normalize_vector(z)
  37. y = cross_product(z, x)
  38. x = x.reshape(-1, 3, 1)
  39. y = y.reshape(-1, 3, 1)
  40. z = z.reshape(-1, 3, 1)
  41. return np.concatenate((x, y, z), axis=2)
  42. def compute_ortho6d_from_rotation_matrix(matrix):
  43. # The ortho6d represents the first two column vectors a1 and a2 of the
  44. # rotation matrix: [ | , |, | ]
  45. # [ a1, a2, a3]
  46. # [ | , |, | ]
  47. """TODO: Add docstring."""
  48. return matrix[:, :, :2].transpose(0, 2, 1).reshape(matrix.shape[0], -1)