
    i                         d dl Z d dlmc mZ 	 ddZ	 ddZde j        de j        fdZde j        de j        fdZ	d	e j        de j        fd
Z
de j        de j        fdZd ZdS )    Nc                 v   | ddddddddf         }| dddddddf         }t          |          }|\  }}dt          j        |dz  |d         z            z  }dt          j        |dz  |d         z            z  }	t          j        |||d         |	d         g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_encodings              J/home/robot-lab/lab/da3_repo/src/depth_anything_3/model/utils/transform.pyextri_intri_to_pose_encodingr      s     	111aaa!RaR< A111aaa!Q;Aq>>DDAq
AEZ	%::;;;E
AEZ	%::;;;EIq$i(8%	:JKQSTTTZZ\\M    c                    | dddf         }| dddf         }| d         }| d         }t          |          }t          j        ||d         gd	          }|\  }}	|d
z  t          j        t          j        |d
z            d          z  }
|	d
z  t          j        t          j        |d
z            d          z  }t          j        | j        dd         dz   | j                  }||d<   |
|d<   |	dz  |d<   |dz  |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   s                r   pose_encoding_to_extri_intrir.   )   s,    	c2A2gAac"D&!E&!EDAAq|,"555JDAq
c'U[53;!7!7>>	>B
c'U[53;!7!7>>	>B]0!4v=mFZ[[[JJyJyEJyEJyJyz!!r   quaternionsreturnc                    t          j        | d          \  }}}}d| | z                      d          z  }t          j        d|||z  ||z  z   z  z
  |||z  ||z  z
  z  |||z  ||z  z   z  |||z  ||z  z   z  d|||z  ||z  z   z  z
  |||z  ||z  z
  z  |||z  ||z  z
  z  |||z  ||z  z   z  d|||z  ||z  z   z  z
  f	d          }|                    | j        dd         dz             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rtwo_sos          r   r'   r'   D   s=    k2..JAq!Q;,11"555EQQ''QUQU]#QUQU]#QUQU]#QQ''QUQU]#QUQU]#QUQU]#QQ''
	
 		 	A 99[&ss+f4555r   matrixc                    |                      d          dk    s|                      d          dk    rt          d| j         d          | j        dd         }t          j        |                     |dz             d          \	  }}}}}}}}	}
t          t          j        d	|z   |z   |
z   d	|z   |z
  |
z
  d	|z
  |z   |
z
  d	|z
  |z
  |
z   gd                    }t          j        t          j        |d
         dz  |	|z
  ||z
  ||z
  gd          t          j        |	|z
  |d         dz  ||z   ||z   gd          t          j        ||z
  ||z   |d         dz  ||	z   gd          t          j        ||z
  ||z   |	|z   |d         dz  gd          gd          }t          j        d          	                    |j
        |j                  }|d|d                             |          z  z  }|t          j        |                    d          d          dk    ddf                             |dz             }|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      ?)rB   .)r   r   r   r   )size
ValueErrorr+   r   r2   r5   _sqrt_positive_partr4   tensortorA   r%   maxFone_hotargmaxstandardize_quaternion)r<   	batch_dimm00m01m02m10m11m12m20m21m22q_absquat_by_rijkflrquat_candidatesouts                   r   r   r   d   s    {{2!v{{2!33I&,IIIJJJSbS!I27,y4'((b3 3 3/Cc3S#sC  c	C#%c	C#%c	C#%c	C#%	 	
 	
 	

 
E ;Kv!+S3Yc	39MSUVVVKsE&MQ$6c	39MSUVVVKsC#IuV}/A39MSUVVVKsC#IsSy%-1:LMSUVVV		
   L ,s



U[

F
FC"cE),<,@,@,E,E&EFO
!)ELLRL$8$8aHHH3NPQPQPQQ
R
Z
ZD C c<<<
 C
 
%
%CJr   xc                     t          j        |           }| dk    }t          j                    rt          j        | |                   ||<   n(t          j        |t          j        |           |          }|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)r]   retpositive_masks      r   rF   rF      sj    
 
1

CEM ="Z-(899Mk-A<<Jr   c                 L    t          j        | 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   rB   r   )r   rb   )r/   s    r   rM   rM      s,     ;{3!8,q0;,LLLr   c           	         | 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          }t	          |                              ||dd          }|dd dd df         }t          j        ||          }|                    ddd          }	t          |	          }
|
                    ||d          }|S )	Nr   .r   rB   r   r   r
   r   )r+   r   r   r5   r'   matmulr   )cam_quat_xyzwc2wbncam_quat_wxyzcam_quat_wxyz_flat
rotmat_cam
rotmat_c2wrotmat_worldrotmat_world_flatworld_quat_wxyz_flatworld_quat_wxyzs               r    cam_quat_xyzw_to_world_quat_wxyzrt      s     rr"DAqI#qs(##qs(##qs(##qs(#		
   M '..r155/0088Aq!DDJS"1"bqb[!J<
J77L$,,RA66&'899*221a;;Or   )N)r   torch.nn.functionalnn
functionalrJ   r   r.   Tensorr'   r   rF   rM   rt    r   r   <module>rz      s                0 " " " "66U\ 6el 6 6 6 6@4 4 4 4 4 4n5< EL    M M M M M M    r   