o
    oi!                     @  sf   d dl mZ d dlZd dlmZmZ d dlmZmZm	Z	m
Z
 d dlmZ ddddZG dd dZdS )    )annotationsN)Tensoreye)Se2Se3So2So3)
QuaternionRmatrixr   matrix_typestrreturnNonec                 C  s   g }|dkrddgddgg}n|dkrddgddgg}t | jdks4t | jdk s4t| jdd |vrRt| d|d	  d
|d	  d|d  d
|d  d| j dS )z"Verify matrix shape based on type.r
         RT   Nz must be either r   xz or                  z, got )lenshapelist
ValueError)r   r   target_shapes r   H/home/ubuntu/.local/lib/python3.10/site-packages/kornia/geometry/pose.pycheck_matrix_shape   s    2r   c                   @  s   e Zd ZdZd,d-d
dZd.ddZd/ddZed0ddZed1ddZ	ed2ddZ
ed.ddZed.ddZe		d,d3d"d#Ze	d,d4d%d&Zd5d'd(Zd6d*d+ZdS )7	NamedPosea  Class to represent a named pose between two frames.

    Internally represented by either Se2 or Se3.

    Example:
        >>> b_from_a = NamedPose(Se3.identity(), frame_src="frame_a", frame_dst="frame_b")
        >>> b_from_a
        NamedPose(dst_from_src=rotation: tensor([1., 0., 0., 0.])
        translation: x: 0.0
        y: 0.0
        z: 0.0,
        frame_src: frame_a -> frame_dst: frame_b)

    Ndst_from_src	Se2 | Se3	frame_src
str | None	frame_dstr   r   c                 C  s*   || _ |p	t j| _|pt j| _dS )zConstruct NamedPose.

        Args:
            dst_from_src: Pose from source frame to destination frame.
            frame_src: Name of frame a.
            frame_dst: Name of frame b.

        N)_dst_from_srcuuiduuid4hex
_frame_src
_frame_dst)selfr    r"   r$   r   r   r   __init__9   s   	zNamedPose.__init__r   c                 C  s   d| j  d| j d| j dS )NzNamedPose(dst_from_src=z,
frame_src: z -> frame_dst: ))r%   r)   r*   r+   r   r   r   __repr__F   s   
zNamedPose.__repr__otherc                 C  s   | j |jkrtd|  d| t|jtr#t| j|j|j | jS t|jt	r6t| j
|j|j | jS tdt| j )aG  Compose two NamedPoses.

        Args:
            other: NamedPose to compose with.

        Returns:
            Composed NamedPose.

        Example:
            >>> b_from_a = NamedPose(Se3.identity(), frame_src="frame_a", frame_dst="frame_b")
            >>> c_from_b = NamedPose(Se3.identity(), frame_src="frame_b", frame_dst="frame_c")
            >>> c_from_b * b_from_a
            NamedPose(dst_from_src=rotation: tensor([1., 0., 0., 0.])
            translation: x: 0.0
            y: 0.0
            z: 0.0,
            frame_src: frame_a -> frame_dst: frame_c)

        zCannot compose z with z$Pose must be either Se2 or Se3, got )r)   r*   r   
isinstanceposer   r   r%   _mul_se2r   _mul_se3type)r+   r0   r   r   r   __mul__L   s   zNamedPose.__mul__c                 C     | j S )z-Pose from source frame to destination frame .r%   r.   r   r   r   r2   i      zNamedPose.pose	So3 | So2c                 C     | j jS )zRotation part of the pose.)r%   rotationr.   r   r   r   r<   n      zNamedPose.rotationr   c                 C  r;   )zTranslation part of the pose.)r%   translationr.   r   r   r   r>   s   r=   zNamedPose.translationc                 C  r7   )zName of the source frame.)r)   r.   r   r   r   r"   x   r9   zNamedPose.frame_srcc                 C  r7   )zName of the destination frame.)r*   r.   r   r   r   r$   }   r9   zNamedPose.frame_dstr<   So3 | So2 | Tensor | Quaternionr>   NamedPose | Nonec                 C  s   t |ttfr| t||||S t |tr| t||||S t |trgt| |jd }t	|d |j
|jd}||dd|d|f< ||dd||f< |dkrX| t|||S |dkre| t|||S dS tdt| )	aX  Construct NamedPose from rotation and translation.

        Args:
            rotation: Rotation part of the pose.
            translation: Translation part of the pose.
            frame_src: Name of the source frame.
            frame_dst: Name of the destination frame.

        Returns:
            NamedPose constructed from rotation and translation.

        Example:
            >>> b_from_a_rot = So3.identity()
            >>> b_from_a_trans = torch.tensor([1., 2., 3.])
            >>> b_from_a = NamedPose.from_rt(b_from_a_rot, b_from_a_trans, frame_src="frame_a", frame_dst="frame_b")
            >>> b_from_a
            NamedPose(dst_from_src=rotation: tensor([1., 0., 0., 0.])
            translation: Parameter containing:
            tensor([1., 2., 3.], requires_grad=True),
            frame_src: frame_a -> frame_dst: frame_b)

        r   )devicedtype.Nr   r   z6R must be either So2, So3, Quaternion, or Tensor, got )r1   r   r	   r   r   r   r   r   r   r   rB   rC   from_matrixr   r5   )clsr<   r>   r"   r$   dimr   r   r   r   from_rt   s    


zNamedPose.from_rtr   c                 C  sN   t |dd |jd }|dkr| t|||S |dkr%| t|||S dS )a  Construct NamedPose from a matrix.

        Args:
            matrix: Matrix representation of the pose.
            frame_src: Name of the source frame.
            frame_dst: Name of the destination frame.

        Returns:
            NamedPose constructed from a matrix.

        Example:
            >>> b_from_a_matrix = Se3.identity().matrix()
            >>> b_from_a = NamedPose.from_matrix(b_from_a_matrix, frame_src="frame_a", frame_dst="frame_b")
            >>> b_from_a
            NamedPose(dst_from_src=rotation: tensor([1., 0., 0., 0.])
            translation: Parameter containing:
            tensor([0., 0., 0.], requires_grad=True),
            frame_src: frame_a -> frame_dst: frame_b)

        r   )r   rA   r   r   N)r   r   r   rD   r   )rE   r   r"   r$   rF   r   r   r   rD      s   
zNamedPose.from_matrixc                 C  s   t | j | j| jS )a  Inverse of the NamedPose.

        Returns:
            Inverse of the NamedPose.

        Example:
            >>> b_from_a = NamedPose(Se3.identity(), frame_src="frame_a", frame_dst="frame_b")
            >>> b_from_a.inverse()
            NamedPose(dst_from_src=rotation: tensor([1., -0., -0., -0.])
            translation: x: 0.0
            y: 0.0
            z: 0.0,
            frame_src: frame_b -> frame_dst: frame_a)

        )r   r%   inverser*   r)   r.   r   r   r   rH      s   zNamedPose.inversepoints_in_srcc                 C  s
   | j | S )a  Transform points from source frame to destination frame.

        Args:
            points_in_src: Points in source frame.

        Returns:
            Points in destination frame.

        Example:
            >>> b_from_a = NamedPose(Se3.identity(), frame_src="frame_a", frame_dst="frame_b")
            >>> b_from_a.transform_points(torch.tensor([1., 2., 3.]))
            tensor([1., 2., 3.])

        r8   )r+   rI   r   r   r   transform_points   s   
zNamedPose.transform_points)NN)r    r!   r"   r#   r$   r#   r   r   )r   r   )r0   r   r   r   )r   r!   )r   r:   )r   r   )
r<   r?   r>   r   r"   r#   r$   r#   r   r@   )r   r   r"   r#   r$   r#   r   r@   )r   r   )rI   r   r   r   )__name__
__module____qualname____doc__r,   r/   r6   propertyr2   r<   r>   r"   r$   classmethodrG   rD   rH   rJ   r   r   r   r   r   )   s0    

/
r   )r
   )r   r   r   r   r   r   )
__future__r   r&   kornia.corer   r   kornia.geometry.liegroupr   r   r   r   kornia.geometry.quaternionr	   r   r   r   r   r   r   <module>   s   