o
    i                     @   s   d dl Z d dlm  mZ 	dddZ	dddZde jde jfdd	Zd
e jde jfddZ	de jde jfddZ
de jde jfddZdd ZdS )    Nc                 C   s   | ddddddddf }| dddddddf }t |}|\}}dt|d |d   }dt|d |d   }	tj|||d |	d gdd }
|
S )	zDConvert camera extrinsics and intrinsics to a compact pose encoding.N      .   r   .r   r   .Ndim)mat_to_quattorchatancatfloat)
extrinsics
intrinsicsimage_size_hwRTquatHWfov_hfov_wpose_encoding r   D/data/cameron/da3_repo/src/depth_anything_3/model/utils/transform.pyextri_intri_to_pose_encoding   s    	"r   c                 C   s   | dddf }| dddf }| d }| d }t |}tj||d gdd	}|\}}	|d
 tt|d
 d }
|	d
 tt|d
 d }tj| jdd d | jd}||d< |
|d< |	d |d< |d |d< d|d< ||fS )zAConvert a pose encoding back to camera extrinsics and intrinsics..Nr      ).r   ).   r   r   r	          @gư>r   r   r   )devicer   r   ).r   r   ).r   r         ?).r   r   )quat_to_matr   r   clamptanzerosshaper"   )r   r   r   r   r   r   r   r   r   r   fyfxr   r   r   r   pose_encoding_to_extri_intri)   s    r+   quaternionsreturnc                 C   s   t | d\}}}}d| |  d }t d||| ||    ||| ||   ||| ||   ||| ||   d||| ||    ||| ||   ||| ||   ||| ||   d||| ||    f	d}|| jdd d S )a,  
    Quaternion Order: XYZW or say ijkr, scalar-last

    Convert rotations given as quaternions to rotation matrices.
    Args:
        quaternions: quaternions with real part last,
            as tensor of shape (..., 4).

    Returns:
        Rotation matrices as tensor of shape (..., 3, 3).
    r   r    r   Nr!   )r   unbindsumstackreshaper(   )r,   ijkrZtwo_sor   r   r   r$   D   s    r$   matrixc              
   C   s  |  ddks|  ddkrtd| j d| jdd }tj| |d dd\	}}}}}}}}	}
ttjd	| | |
 d	| | |
 d	| | |
 d	| | |
 gdd}tjtj|d
 d |	| || || gddtj|	| |d d || || gddtj|| || |d d ||	 gddtj|| || |	| |d d gddgdd}tdj	|j
|jd}|d|d |  }|tj|jdddddkddf |d }|dg df }t|}|S )a  
    Convert rotations given as rotation matrices to quaternions.

    Args:
        matrix: Rotation matrices as tensor of shape (..., 3, 3).

    Returns:
        quaternions with real part last, as tensor of shape (..., 4).
        Quaternion Order: XYZW or say ijkr, scalar-last
    r   r   zInvalid rotation matrix shape .N)	   r	   r#   ).r   r   ).r   ).r   ).r   g?)dtyper"   r    r      )num_classesg      ?)r<   .)r   r   r   r   )size
ValueErrorr(   r   r.   r1   _sqrt_positive_partr0   tensortor;   r"   maxFone_hotargmaxstandardize_quaternion)r7   	batch_dimm00m01m02m10m11m12m20m21m22Zq_absZquat_by_rijkZflrZquat_candidatesoutr   r   r   r   d   s@   ((((
&r   xc                 C   sH   t | }| dk}t  rt | | ||< |S t |t | |}|S )z[
    Returns torch.sqrt(torch.max(0, x))
    but with a zero subgradient where x is 0.
    r   )r   
zeros_likeis_grad_enabledsqrtwhere)rS   retZpositive_maskr   r   r   r@      s   
r@   c                 C   s    t | dddf dk |  | S )a  
    Convert a unit quaternion to a standard form: one in which the real
    part is non negative.

    Args:
        quaternions: Quaternions with real part last,
            as tensor of shape (..., 4).

    Returns:
        Standardized quaternions as tensor of shape (..., 4).
    .r   r<   r   )r   rW   )r,   r   r   r   rG      s    rG   c                 C   s   | j d d \}}tj| dddf | dddf | dddf | dddf gdd}|dd}t|||dd}|dd dd df }t||}|ddd}	t|	}
|
||d}|S )	Nr   .r   r<   r   r   r   r	   )r(   r   r   r1   r$   matmulr   )Zcam_quat_xyzwc2wbnZcam_quat_wxyzZcam_quat_wxyz_flatZ
rotmat_camZ
rotmat_c2wZrotmat_worldZrotmat_world_flatZworld_quat_wxyz_flatZworld_quat_wxyzr   r   r    cam_quat_xyzw_to_world_quat_wxyz   s"   
r]   )N)r   Ztorch.nn.functionalnn
functionalrD   r   r+   Tensorr$   r   r@   rG   r]   r   r   r   r   <module>   s   

 7