orientation.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import numpy as np
  2. from collections.abc import Callable
  3. from openpilot.common.transformations.transformations import (ecef_euler_from_ned_single,
  4. euler2quat_single,
  5. euler2rot_single,
  6. ned_euler_from_ecef_single,
  7. quat2euler_single,
  8. quat2rot_single,
  9. rot2euler_single,
  10. rot2quat_single)
  11. def numpy_wrap(function, input_shape, output_shape) -> Callable[..., np.ndarray]:
  12. """Wrap a function to take either an input or list of inputs and return the correct shape"""
  13. def f(*inps):
  14. *args, inp = inps
  15. inp = np.array(inp)
  16. shape = inp.shape
  17. if len(shape) == len(input_shape):
  18. out_shape = output_shape
  19. else:
  20. out_shape = (shape[0],) + output_shape
  21. # Add empty dimension if inputs is not a list
  22. if len(shape) == len(input_shape):
  23. inp.shape = (1, ) + inp.shape
  24. result = np.asarray([function(*args, i) for i in inp])
  25. result.shape = out_shape
  26. return result
  27. return f
  28. euler2quat = numpy_wrap(euler2quat_single, (3,), (4,))
  29. quat2euler = numpy_wrap(quat2euler_single, (4,), (3,))
  30. quat2rot = numpy_wrap(quat2rot_single, (4,), (3, 3))
  31. rot2quat = numpy_wrap(rot2quat_single, (3, 3), (4,))
  32. euler2rot = numpy_wrap(euler2rot_single, (3,), (3, 3))
  33. rot2euler = numpy_wrap(rot2euler_single, (3, 3), (3,))
  34. ecef_euler_from_ned = numpy_wrap(ecef_euler_from_ned_single, (3,), (3,))
  35. ned_euler_from_ecef = numpy_wrap(ned_euler_from_ecef_single, (3,), (3,))
  36. quats_from_rotations = rot2quat
  37. quat_from_rot = rot2quat
  38. rotations_from_quats = quat2rot
  39. rot_from_quat = quat2rot
  40. euler_from_rot = rot2euler
  41. euler_from_quat = quat2euler
  42. rot_from_euler = euler2rot
  43. quat_from_euler = euler2quat