o
    ॵiiF                     @  s  d dl mZ d dlmZmZmZmZmZmZ d dl	Z
d dlZejeddfd>ddZe
jde
jdZg dg dg dged< g dg dg dged< g dg dg dged< g dg dg dged< g dg dg d ged!< g d"g d g dged#< g d g d"g dged$< g d g d%g dged&< g d"g d g d'ged(< g d)g dg d ged*< ed+d+d,Zeeae
d-Zg d.g d/g d0g d1geddddd f< g d2g d.g d3g d0geddddd4f< g d5g d1g d.g d2geddddd6f< g d3g d5g d/g d.geddddd7f< eddd4dddf ZeeaG d8d9 d9ZG d:d; d;ZG d<d= d=ZdS )?    )annotations)AnyCallableIterableOptionalSequenceTupleNcpuF
batch_dims
Tuple[int]dtypeOptional[torch.dtype]deviceOptional[torch.device]requires_gradboolreturntorch.Tensorc                 C  s    t jg | dR |||d}|S )N   r   r   r   )torchzeros)r
   r   r   r   trans r   c/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/science/unifold/modules/frame.pyzero_translation   s   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   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   )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   r   r   )r   r   r   r   r   r   c                   @  s2  e Zd ZdPddZeejeddfdQddZedRddZ	edSddZ
dTddZdUddZdVd d!ZdWd#d$Zed%d& ZedXd'd(ZdYd)d*ZdZd,d-ZdZd.d/Zed[d1d2Zed\d4d5Zed]d7d8Zed^d9d:Zd_d=d>Zd`dAdBZedadEdFZdYdGdHZdbdIdJZdcdKdLZdYdMdNZdOS )dRotationmatr   c                 C  s,   |j dd  dkrtd|j  || _d S )Nr!   r    zincorrect rotation shape: )shape
ValueError_mat)selfr$   r   r   r   __init__>   s   
zRotation.__init__r	   Fr   r   r   r   r   r   r   c                 C  sR   t jd|||d}|jg dt|  ddR  }|jg | ddR  }t|S )Nr   r   r   r   )r   eyeviewlenexpandr#   )r%   r   r   r   r$   r   r   r   identityF   s    zRotation.identityabc                 C  s   |   |   | jS N)floattyper   )r0   r1   r   r   r   mat_mul_matS   s   zRotation.mat_mul_matrtc                 C  s$   |   |  d d|jS Nr   )r3   	unsqueezesqueezer4   r   )r6   r7   r   r   r   mat_mul_vecW   s   $zRotation.mat_mul_vecindexr   c                 C  s0   t |ts|f}t| j|td td f  dS Nr$   )
isinstancetupler#   r'   slicer(   r<   r   r   r   __getitem__[   s   
 zRotation.__getitem__rightc                 C  sR   t |ttfrt| j| dS t |tjrt| j|d  dS tdt| d)Nr>   ).NNz/multiplicand must be a tensor or a number, got .)	r?   intr3   r#   r'   r   Tensor	TypeErrorr4   )r(   rD   r   r   r   __mul__`   s   zRotation.__mul__leftc                 C  
   |  |S r2   rI   r(   rJ   r   r   r   __rmul__j   s   
zRotation.__rmul__otherc                 C  s   t | j|j}t |dS r=   )r#   r5   rot_mat)r(   rO   new_matr   r   r   
__matmul__m   s   
zRotation.__matmul__c                 C  s   | j ddS )Nr   r!   )r'   	transposer(   r   r   r   _inv_matq   s   zRotation._inv_matc                 C     | j S r2   )r'   rT   r   r   r   rP   u   s   zRotation.rot_matc                 C  s   t | jdS r=   )r#   rU   rT   r   r   r   inverty   s   zRotation.invertptsc                 C     t | j|S r2   )r#   r;   r'   r(   rX   r   r   r   apply|      zRotation.applyc                 C  rY   r2   )r#   r;   rU   rZ   r   r   r   invert_apply   r\   zRotation.invert_apply
torch.Sizec                 C     | j jd d }|S )Nr!   )r'   r%   r(   sr   r   r   r%         zRotation.shapetorch.dtypec                 C     | j jS r2   )r'   r   rT   r   r   r   r         zRotation.dtypetorch.devicec                 C  rd   r2   )r'   r   rT   r   r   r   r      re   zRotation.devicec                 C  rd   r2   )r'   r   rT   r   r   r   r      re   zRotation.requires_graddimrF   c                 C  s<   |t | jkrtd| j|dkr|n|d }t|dS )NInvalid dimensionr   r   r>   )r-   r%   r&   r'   r9   r#   )r(   rg   rot_matsr   r   r   r9      s   
zRotation.unsqueezefn&Callable[[torch.Tensor], torch.Tensor]c                 C  s`   | j | j jd d d }tjtt|tj|dddd}||jd d d }t|dS )Nr!   )r"   r   rg   r    r>   )	r'   r,   r%   r   stacklistmapunbindr#   )r(   rj   r$   r   r   r   map_tensor_fn   s   "
zRotation.map_tensor_fnrsSequence[Rotation]c                 C  s6   dd | D }t j||dkr|n|d d}t|dS )Nc                 S     g | ]}|j qS r   )rP   ).0r6   r   r   r   
<listcomp>       z Rotation.cat.<locals>.<listcomp>r   r   rl   r>   )r   catr#   )rr   rg   ri   r   r   r   rx      s   
zRotation.catc                 C     t | j dS r=   )r#   r'   cudarT   r   r   r   rz         zRotation.cudac                 C  s   t | jj||ddS )N)r   r   r>   )r#   r'   to)r(   r   r   r   r   r   r|      s   zRotation.toc                 C  s   t | j|dS r=   )r#   r'   r4   r(   r   r   r   r   r4         zRotation.typec                 C  ry   r=   )r#   r'   detachrT   r   r   r   r      r{   zRotation.detachN)r$   r   )r   r   r   r   r   r   r   r#   )r0   r   r1   r   r   r   )r6   r   r7   r   r   r   )r<   r   r   r#   )rD   r   r   r#   )rJ   r   r   r#   )rO   r#   r   r#   r   r   r   r#   rX   r   r   r   r   r^   r   rc   r   rf   )r   r   )rg   rF   r   r#   )rj   rk   r   r#   )rr   rs   rg   rF   r   r#   )r   r   r   r   r   r#   )r   r   r   r#   )__name__
__module____qualname__r)   staticmethodr   r3   r   r/   r5   r;   rC   rI   rN   rR   propertyrU   rP   rW   r[   r]   r%   r   r   r9   rq   rx   rz   r|   r4   r   r   r   r   r   r#   <   sL    














r#   c                   @  sN  e Zd ZdbddZeejeddfdcddZddddZ	deddZ
dfddZedgd d!Zedhd#d$Zdid&d'Zdjd(d)Zdkd+d,Zdld.d/Zdld0d1Zdmd2d3Zdnd6d7Zdjd8d9Zedod;d<Ze	=dpdqdCdDZdrdGdHZedsdKdLZdtdNdOZdndPdQZdudSdTZdmdUdVZedvdXdYZdmdZd[Zedwd]d^Z dmd_d`Z!daS )xFramerotationOptional[Rotation]translationOptional[torch.Tensor]c                 C  s   |d u r|d u rt d}td}n$|d u r"t|j|j|j|j}n|d u r6t |jd d |j|j|j}|j|jd d ksF|j|jkrJtd|| _|| _	d S )N)r   r   z+RotationMatrix and translation incompatible)
r#   r/   r   r%   r   r   r   r&   _r_t)r(   r   r   r   r   r   r)      s*   



zFrame.__init__r	   Fr%   Iterable[int]r   r   r   r   r   r   r   c                 C  s    t t| |||t| |||S r2   )r   r#   r/   r   )r%   r   r   r   r   r   r   r/      s   zFrame.identityr<   r   c                 C  s2   t |tkr	|f}t| j| | j|td f  S r2   )r4   r@   r   r   r   rA   rB   r   r   r   rC      s   zFrame.__getitem__rD   r   c                 C  s6   t |tjs
td| j| }| j|d  }t||S )Nz'The other multiplicand must be a Tensor).N)r?   r   rG   rH   r   r   r   )r(   rD   new_rots	new_transr   r   r   rI      s
   

zFrame.__mul__rJ   c                 C  rK   r2   rL   rM   r   r   r   rN      s   
zFrame.__rmul__r^   c                 C  r_   r8   )r   r%   r`   r   r   r   r%      rb   zFrame.shaperf   c                 C  rd   r2   )r   r   rT   r   r   r   r     re   zFrame.devicer#   c                 C  rV   r2   r   rT   r   r   r   get_rots	     zFrame.get_rotsc                 C  rV   r2   r   rT   r   r   r   	get_trans  r   zFrame.get_transrO   c                 C  s*   | j |j  }| j |j| j }t||S r2   )r   r[   r   r   )r(   rO   new_rotr   r   r   r   compose  s   
zFrame.composerX   c                 C  s   | j |}|| j S r2   )r   r[   r   )r(   rX   rotatedr   r   r   r[     s   
zFrame.applyc                 C  s   || j  }| j|S r2   )r   r   r]   rZ   r   r   r   r]     s   
zFrame.invert_applyc                 C  s$   | j  }|| j}t|d| S r8   )r   rW   r[   r   r   )r(   rot_invtrn_invr   r   r   rW   "  s   
zFrame.invertrj   rk   c                 C  s:   | j |}tjtt|tj| jdddd}t||S )Nr   rl   )	r   rq   r   rm   rn   ro   rp   r   r   )r(   rj   r   r   r   r   r   rq   (  s
   
zFrame.map_tensor_fnc                 C  sV   | j g | jddR }| jj|dd dd df< | j |dd ddf< d|d< |S )Nr   .r   r   ).r   r   )r   	new_zerosr%   r   rP   )r(   tensorr   r   r   to_tensor_4x40  s
   zFrame.to_tensor_4x4r7   c                 C  sR   | j dd  dkrtdt| dd dd df d}| dd ddf }t||S )Nr!   )r   r   zIncorrectly shaped input tensor.r   r>   )r%   r&   r#   r   )r7   rotsr   r   r   r   from_tensor_4x47  s
   
zFrame.from_tensor_4x4:0yE>p_neg_x_axisorigin
p_xy_planeepsr3   c           	        s  t j| dd} t j|dd}t j|dd}dd t|| D }dd t||D }t tdd |D |   fdd|D }td	d t||D fd
dt||D }t tdd |D |   fdd|D }|d |d  |d |d   |d |d  |d |d   |d |d  |d |d   g}t jdd t|||D dd}||jd d d }t|d}t	|t j|ddS )Nr   rl   c                 S     g | ]\}}|| qS r   r   ru   c1c2r   r   r   rv   L      z'Frame.from_3_points.<locals>.<listcomp>c                 S  r   r   r   r   r   r   r   rv   M  r   c                 s      | ]}|| V  qd S r2   r   ru   cr   r   r   	<genexpr>O      z&Frame.from_3_points.<locals>.<genexpr>c                      g | ]}|  qS r   r   r   denomr   r   rv   P      c                 s  s    | ]	\}}|| V  qd S r2   r   r   r   r   r   r   Q  s    c                   s   g | ]
\}}||   qS r   r   r   )dotr   r   rv   R  s    c                 s  r   r2   r   r   r   r   r   r   S  r   c                   r   r   r   r   r   r   r   rv   T  r   r   r   r   c                 S  s   g | ]	}|D ]}|qqS r   r   )ru   tupr   r   r   r   rv   [  s    r    r>   )
r   rp   zipsqrtsumrm   reshaper%   r#   r   )	r   r   r   r   e0e1e2r   rot_objr   )r   r   r   from_3_pointsA  s&    
zFrame.from_3_pointsrg   rF   c                 C  sH   |t | jkrtd| j|}| j|dkr|n|d }t||S )Nrh   r   r   )r-   r%   r&   r   r9   r   r   )r(   rg   r   r   r   r   r   r9   b  s
   
zFrame.unsqueezeTsSequence[Frame]c                 C  sH   t dd | D |}tjdd | D |dkr|n|d d}t||S )Nc                 S  rt   r   r   ru   Tr   r   r   rv   r  rw   zFrame.cat.<locals>.<listcomp>c                 S  rt   r   r   r   r   r   r   rv   s  rw   r   r   rl   )r#   rx   r   r   )r   rg   r   r   r   r   r   rx   m  s   (
z	Frame.catCallable[[Rotation], Rotation]c                 C  s   t || j| jS r2   r   r   r   r(   rj   r   r   r   apply_rot_fnw  r~   zFrame.apply_rot_fnc                 C  s   t | j|| jS r2   r   r   r   r   r   apply_trans_fnz  s   zFrame.apply_trans_fntrans_scale_factorc                   s    fdd}|  |S )Nc                   s   |   S r2   r   )r7   r   r   r   rj        z#Frame.scale_translation.<locals>.fn)r   )r(   r   rj   r   r   r   scale_translation~  s   
zFrame.scale_translationc                 C  s   dd }|  |S )Nc                 S  s   |   S r2   )r   )r6   r   r   r   rj     r   z#Frame.stop_rot_gradient.<locals>.fn)r   r   r   r   r   stop_rot_gradient  s   
zFrame.stop_rot_gradient#B;c                   s$  |j } | }   |  |   fddtdD \}}}t||d  |d  }| | }	|| }
|	g |	jddR }|
|d< d|	 |d< |	|d< |
|d	< d
|d< t||d  |d  |d  }|| }t|d |d  | }|g |jddR }||d< ||d< d
|d	< d| |d< ||d< t||}t	|fddtdD \}}}t||d  |d  }| | }|| }|g |jddR }d
|d< ||d	< d| |d< ||d< ||d< t||}|
dd}t||d}t|||S )Nc                      g | ]} d |f qS .r   ru   i)c_xyzr   r   rv     r   z7Frame.make_transform_from_reference.<locals>.<listcomp>r   r   ).r   r   r   ).r   r   ).r   r   ).r   r   r   ).r   r   ).r   r   ).r   r   c                   r   r   r   r   )n_xyzr   r   rv     r   ).r   r   ).r   r   r!   r>   )r   r3   ranger   r   r   r%   r#   r5   r;   rS   r4   r   )r   ca_xyzr   r   input_dtypec_xc_yd_pairnormsin_c1cos_c1c1_rotssin_c2cos_c2c2_rotsc_rots_n_yn_zsin_ncos_nn_rotsr   r   r   )r   r   r   make_transform_from_reference  sR   
"
z#Frame.make_transform_from_referencec                 C  s   t | j | j S r2   )r   r   rz   r   rT   r   r   r   rz     s   z
Frame.cudarc   c                 C  s   | j j| jjks
J | j jS r2   )r   r   r   rT   r   r   r   r     s   zFrame.dtypec                 C  s   t | j|| j|S r2   )r   r   r4   r   r}   r   r   r   r4     s   z
Frame.typeN)r   r   r   r   )
r%   r   r   r   r   r   r   r   r   r   )r<   r   r   r   )rD   r   r   r   )rJ   r   r   r   r   r   r   r   )rO   r   r   r   r   )r   r   )rj   rk   r   r   )r7   r   r   r   )r   )
r   r   r   r   r   r   r   r3   r   r   )rg   rF   r   r   )r   r   rg   rF   r   r   )rj   r   r   r   )r   r3   r   r   )r   r   )"r   r   r   r)   r   r   r3   r   r/   rC   rI   rN   r   r%   r   r   r   r[   r]   rW   rq   r   r   r   r9   rx   r   r   r   r   r   rz   r   r4   r   r   r   r   r      sP    











	
 
	



5r   c                   @  s   e Zd Zd,ddZeejeddfd-ddZdd Z	dd Z
dd Zedd Zedd Zedd Z	d.d/d"d#Zd0d'd(Zd1d)d*Zd+S )2
Quaternion
quaternionr   r   c                 C  s.   |j d dkrtd|j  || _|| _d S )Nr   r   zincorrect quaternion shape: )r%   r&   _qr   )r(   r   r   r   r   r   r)     s   
zQuaternion.__init__r	   Fr%   r   r   r   r   r   r   r   r   c                 C  sd   t | |||}tjg | dR |||d}t  d|d< W d    n1 s(w   Y  t||S )Nr   r   r   ).r   )r   r   r   no_gradr   )r%   r   r   r   r   quatsr   r   r   r/     s   


zQuaternion.identityc                 C  rV   r2   )r   rT   r   r   r   	get_quats  r   zQuaternion.get_quatsc                 C  rV   r2   r   rT   r   r   r   r     r   zQuaternion.get_transc                 C  s   |   }t|}|S r2   )r   r   quat_to_rot)r(   r   ri   r   r   r   get_rot_mats  s   
zQuaternion.get_rot_matsc                 C  s   | j }|  } tj| jkrt| jatjt| dd d d d f  | dd d d d f  dd}||}|jg |j	d d ddR  }|S )N.r!   rl   r   r   )
r   r3   _QUAT_TO_ROT_tensorr   r|   r   r   r4   r,   r%   )normalized_quatr   
rot_tensorr   r   r   r     s   
"zQuaternion.quat_to_rotc                 C  s2   | j }|  } | tjj| ddd } | |} | S )Nr   T)rg   keepdim)r   r3   r   linalgr   r4   )r   r   r   r   r   normalize_quat  s
   
zQuaternion.normalize_quatc                 C  s   | j }|  } | }tj| jkrt| jat}|dt| jd d  |j }tj	|| dd d d d f  |dd d d d f  dd
|S )Nr*   r   .r   rl   )r   r3   _QUAT_MULTIPLY_BY_VEC_tensorr   r|   r,   r-   r%   r   r   r4   )quatvecr   r$   reshaped_matr   r   r   quat_multiply_by_vec
  s   "*zQuaternion.quat_multiply_by_vecTq_update_vecnormalize_quatsc                 C  s*   |   }|t|| }|rt|}|S r2   )r   r   r   r   )r(   r  r  r   	new_quatsr   r   r   compose_q_update_vec  s   
zQuaternion.compose_q_update_vec
update_vecpre_rot_matr#   c                 C  sJ   |dd df |ddd f }}|  |}||}| j| }t||S )N.r   )r  r[   r   r   )r(   r  r  q_vect_vecr  trans_updater   r   r   r   compose_update_vec$  s
   "



zQuaternion.compose_update_vecc                 C  s   t | j | jS r2   )r   r   r   r   rT   r   r   r   r   1  r~   zQuaternion.stop_rot_gradientN)r   r   r   r   )
r%   r   r   r   r   r   r   r   r   r   )T)r  r   r  r   r   r   )r  r   r  r#   r   r   )r   r   )r   r   r   r)   r   r   r3   r   r/   r   r   r   r   r   r   r  r
  r   r   r   r   r   r     s(    





r   )
r
   r   r   r   r   r   r   r   r   r   )
__future__r   typingr   r   r   r   r   r   numpynpr   r3   r   r   r   float32_QUAT_TO_ROTr   
from_numpyr   _QUAT_MULTIPLY_QUAT_MULTIPLY_BY_VECr   r#   r   r   r   r   r   r   <module>   sR    


{  