pose.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # LICENSE HEADER MANAGED BY add-license-header
  2. #
  3. # Copyright 2018 Kornia Team
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. from __future__ import annotations
  18. import uuid
  19. from kornia.core import Tensor, eye
  20. from kornia.geometry.liegroup import Se2, Se3, So2, So3
  21. from kornia.geometry.quaternion import Quaternion
  22. def check_matrix_shape(matrix: Tensor, matrix_type: str = "R") -> None:
  23. """Verify matrix shape based on type."""
  24. target_shapes = []
  25. if matrix_type == "R":
  26. target_shapes = [[2, 2], [3, 3]]
  27. elif matrix_type == "RT":
  28. target_shapes = [[3, 3], [4, 4]]
  29. if len(matrix.shape) > 3 or len(matrix.shape) < 2 or list(matrix.shape[-2:]) not in target_shapes:
  30. raise ValueError(
  31. f"{matrix_type} must be either {target_shapes[0]}x{target_shapes[0]} or \
  32. {target_shapes[1]}x{target_shapes[1]}, got {matrix.shape}"
  33. )
  34. class NamedPose:
  35. r"""Class to represent a named pose between two frames.
  36. Internally represented by either Se2 or Se3.
  37. Example:
  38. >>> b_from_a = NamedPose(Se3.identity(), frame_src="frame_a", frame_dst="frame_b")
  39. >>> b_from_a
  40. NamedPose(dst_from_src=rotation: tensor([1., 0., 0., 0.])
  41. translation: x: 0.0
  42. y: 0.0
  43. z: 0.0,
  44. frame_src: frame_a -> frame_dst: frame_b)
  45. """
  46. def __init__(self, dst_from_src: Se2 | Se3, frame_src: str | None = None, frame_dst: str | None = None) -> None:
  47. """Construct NamedPose.
  48. Args:
  49. dst_from_src: Pose from source frame to destination frame.
  50. frame_src: Name of frame a.
  51. frame_dst: Name of frame b.
  52. """
  53. self._dst_from_src = dst_from_src
  54. self._frame_src = frame_src or uuid.uuid4().hex
  55. self._frame_dst = frame_dst or uuid.uuid4().hex
  56. def __repr__(self) -> str:
  57. return (
  58. f"NamedPose(dst_from_src={self._dst_from_src},\n"
  59. f"frame_src: {self._frame_src} -> frame_dst: {self._frame_dst})"
  60. )
  61. def __mul__(self, other: NamedPose) -> NamedPose:
  62. """Compose two NamedPoses.
  63. Args:
  64. other: NamedPose to compose with.
  65. Returns:
  66. Composed NamedPose.
  67. Example:
  68. >>> b_from_a = NamedPose(Se3.identity(), frame_src="frame_a", frame_dst="frame_b")
  69. >>> c_from_b = NamedPose(Se3.identity(), frame_src="frame_b", frame_dst="frame_c")
  70. >>> c_from_b * b_from_a
  71. NamedPose(dst_from_src=rotation: tensor([1., 0., 0., 0.])
  72. translation: x: 0.0
  73. y: 0.0
  74. z: 0.0,
  75. frame_src: frame_a -> frame_dst: frame_c)
  76. """
  77. if self._frame_src != other._frame_dst:
  78. raise ValueError(f"Cannot compose {self} with {other}")
  79. if isinstance(other.pose, Se2):
  80. return NamedPose(self._dst_from_src._mul_se2(other.pose), other._frame_src, self._frame_dst)
  81. elif isinstance(other.pose, Se3):
  82. return NamedPose(self._dst_from_src._mul_se3(other.pose), other._frame_src, self._frame_dst)
  83. else:
  84. raise ValueError(f"Pose must be either Se2 or Se3, got {type(self._dst_from_src)}")
  85. @property
  86. def pose(self) -> Se2 | Se3:
  87. """Pose from source frame to destination frame ."""
  88. return self._dst_from_src
  89. @property
  90. def rotation(self) -> So3 | So2:
  91. """Rotation part of the pose."""
  92. return self._dst_from_src.rotation
  93. @property
  94. def translation(self) -> Tensor:
  95. """Translation part of the pose."""
  96. return self._dst_from_src.translation
  97. @property
  98. def frame_src(self) -> str:
  99. """Name of the source frame."""
  100. return self._frame_src
  101. @property
  102. def frame_dst(self) -> str:
  103. """Name of the destination frame."""
  104. return self._frame_dst
  105. @classmethod
  106. def from_rt(
  107. cls,
  108. rotation: So3 | So2 | Tensor | Quaternion,
  109. translation: Tensor,
  110. frame_src: str | None = None,
  111. frame_dst: str | None = None,
  112. ) -> NamedPose | None:
  113. """Construct NamedPose from rotation and translation.
  114. Args:
  115. rotation: Rotation part of the pose.
  116. translation: Translation part of the pose.
  117. frame_src: Name of the source frame.
  118. frame_dst: Name of the destination frame.
  119. Returns:
  120. NamedPose constructed from rotation and translation.
  121. Example:
  122. >>> b_from_a_rot = So3.identity()
  123. >>> b_from_a_trans = torch.tensor([1., 2., 3.])
  124. >>> b_from_a = NamedPose.from_rt(b_from_a_rot, b_from_a_trans, frame_src="frame_a", frame_dst="frame_b")
  125. >>> b_from_a
  126. NamedPose(dst_from_src=rotation: tensor([1., 0., 0., 0.])
  127. translation: Parameter containing:
  128. tensor([1., 2., 3.], requires_grad=True),
  129. frame_src: frame_a -> frame_dst: frame_b)
  130. """
  131. if isinstance(rotation, (So3, Quaternion)):
  132. return cls(Se3(rotation, translation), frame_src, frame_dst)
  133. elif isinstance(rotation, So2):
  134. return cls(Se2(rotation, translation), frame_src, frame_dst)
  135. elif isinstance(rotation, Tensor):
  136. check_matrix_shape(rotation)
  137. dim = rotation.shape[-1]
  138. RT = eye(dim + 1, device=rotation.device, dtype=rotation.dtype)
  139. RT[..., :dim, :dim] = rotation
  140. RT[..., :dim, dim] = translation
  141. if dim == 2:
  142. return cls(Se2.from_matrix(RT), frame_src, frame_dst)
  143. elif dim == 3:
  144. return cls(Se3.from_matrix(RT), frame_src, frame_dst)
  145. else:
  146. raise ValueError(f"R must be either So2, So3, Quaternion, or Tensor, got {type(rotation)}")
  147. return None
  148. @classmethod
  149. def from_matrix(
  150. cls, matrix: Tensor, frame_src: str | None = None, frame_dst: str | None = None
  151. ) -> NamedPose | None:
  152. """Construct NamedPose from a matrix.
  153. Args:
  154. matrix: Matrix representation of the pose.
  155. frame_src: Name of the source frame.
  156. frame_dst: Name of the destination frame.
  157. Returns:
  158. NamedPose constructed from a matrix.
  159. Example:
  160. >>> b_from_a_matrix = Se3.identity().matrix()
  161. >>> b_from_a = NamedPose.from_matrix(b_from_a_matrix, frame_src="frame_a", frame_dst="frame_b")
  162. >>> b_from_a
  163. NamedPose(dst_from_src=rotation: tensor([1., 0., 0., 0.])
  164. translation: Parameter containing:
  165. tensor([0., 0., 0.], requires_grad=True),
  166. frame_src: frame_a -> frame_dst: frame_b)
  167. """
  168. check_matrix_shape(matrix, matrix_type="RT")
  169. dim = matrix.shape[-1]
  170. if dim == 3:
  171. return cls(Se2.from_matrix(matrix), frame_src, frame_dst)
  172. elif dim == 4:
  173. return cls(Se3.from_matrix(matrix), frame_src, frame_dst)
  174. return None
  175. def inverse(self) -> NamedPose:
  176. """Inverse of the NamedPose.
  177. Returns:
  178. Inverse of the NamedPose.
  179. Example:
  180. >>> b_from_a = NamedPose(Se3.identity(), frame_src="frame_a", frame_dst="frame_b")
  181. >>> b_from_a.inverse()
  182. NamedPose(dst_from_src=rotation: tensor([1., -0., -0., -0.])
  183. translation: x: 0.0
  184. y: 0.0
  185. z: 0.0,
  186. frame_src: frame_b -> frame_dst: frame_a)
  187. """
  188. return NamedPose(self._dst_from_src.inverse(), self._frame_dst, self._frame_src)
  189. def transform_points(self, points_in_src: Tensor) -> Tensor:
  190. """Transform points from source frame to destination frame.
  191. Args:
  192. points_in_src: Points in source frame.
  193. Returns:
  194. Points in destination frame.
  195. Example:
  196. >>> b_from_a = NamedPose(Se3.identity(), frame_src="frame_a", frame_dst="frame_b")
  197. >>> b_from_a.transform_points(torch.tensor([1., 2., 3.]))
  198. tensor([1., 2., 3.])
  199. """
  200. return self._dst_from_src * points_in_src