o
    *jiF                     @  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   p/var/www/html/Deteccion_Ine/venv/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   	unsqueezeZ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(   rC   r   r   r   __mul__`   s   zRotation.__mul__leftc                 C  
   |  |S r2   rH   r(   rI   r   r   r   __rmul__j   s   
zRotation.__rmul__otherc                 C  s   t | j|j}t |dS r<   )r#   r5   rot_mat)r(   rN   Z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'   rR   r   r   r   rO   u   s   zRotation.rot_matc                 C  s   t | jdS r<   )r#   rS   rR   r   r   r   inverty   s   zRotation.invertptsc                 C     t | j|S r2   )r#   r:   r'   r(   rV   r   r   r   apply|      zRotation.applyc                 C  rW   r2   )r#   r:   rS   rX   r   r   r   invert_apply   rZ   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   rR   r   r   r   r         zRotation.dtypetorch.devicec                 C  rb   r2   )r'   r   rR   r   r   r   r      rc   zRotation.devicec                 C  rb   r2   )r'   r   rR   r   r   r   r      rc   zRotation.requires_graddimrE   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(   re   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   re   r    r=   )	r'   r,   r%   r   stacklistmapunbindr#   )r(   rh   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   )rO   ).0r6   r   r   r   
<listcomp>       z Rotation.cat.<locals>.<listcomp>r   r   rj   r=   )r   catr#   )rp   re   rg   r   r   r   rv      s   
zRotation.catc                 C     t | j dS r<   )r#   r'   cudarR   r   r   r   rx         zRotation.cudac                 C  s   t | jj||ddS )N)r   r   r=   )r#   r'   to)r(   r   r   r   r   r   rz      s   zRotation.toc                 C  s   t | j|dS r<   )r#   r'   r4   r(   r   r   r   r   r4         zRotation.typec                 C  rw   r<   )r#   r'   detachrR   r   r   r   r}      ry   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#   )rC   r   r   r#   )rI   r   r   r#   )rN   r#   r   r#   r   r   r   r#   rV   r   r   r   r   r\   r   ra   r   rd   )r   r   )re   rE   r   r#   )rh   ri   r   r#   )rp   rq   re   rE   r   r#   )r   r   r   r   r   r#   )r   r   r   r#   )__name__
__module____qualname__r)   staticmethodr   r3   r   r/   r5   r:   rB   rH   rM   rP   propertyrS   rO   rU   rY   r[   r%   r   r   r9   ro   rv   rx   rz   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   r@   rA   r   r   r   rB      s   zFrame.__getitem__rC   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   rF   rG   r   r   r   )r(   rC   new_rots	new_transr   r   r   rH      s
   

zFrame.__mul__rI   c                 C  rJ   r2   rK   rL   r   r   r   rM      s   
zFrame.__rmul__r\   c                 C  r]   r8   )r   r%   r^   r   r   r   r%      r`   zFrame.shaperd   c                 C  rb   r2   )r   r   rR   r   r   r   r     rc   zFrame.devicer#   c                 C  rT   r2   r   rR   r   r   r   get_rots	     zFrame.get_rotsc                 C  rT   r2   r   rR   r   r   r   	get_trans  r   zFrame.get_transrN   c                 C  s*   | j |j  }| j |j| j }t||S r2   )r   rY   r   r   )r(   rN   Znew_rotr   r   r   r   compose  s   
zFrame.composerV   c                 C  s   | j |}|| j S r2   )r   rY   r   )r(   rV   Zrotatedr   r   r   rY     s   
zFrame.applyc                 C  s   || j  }| j|S r2   )r   r   r[   rX   r   r   r   r[     s   
zFrame.invert_applyc                 C  s$   | j  }|| j}t|d| S r8   )r   rU   rY   r   r   )r(   Zrot_invZtrn_invr   r   r   rU   "  s   
zFrame.invertrh   ri   c                 C  s:   | j |}tjtt|tj| jdddd}t||S )Nr   rj   )	r   ro   r   rk   rl   rm   rn   r   r   )r(   rh   r   r   r   r   r   ro   (  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   rO   )r(   Z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   rj   c                 S     g | ]\}}|| qS r   r   rs   c1c2r   r   r   rt   L      z'Frame.from_3_points.<locals>.<listcomp>c                 S  r   r   r   r   r   r   r   rt   M  r   c                 s      | ]}|| V  qd S r2   r   rs   cr   r   r   	<genexpr>O      z&Frame.from_3_points.<locals>.<genexpr>c                      g | ]}|  qS r   r   r   denomr   r   rt   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   rt   R  s    c                 s  r   r2   r   r   r   r   r   r   S  r   c                   r   r   r   r   r   r   r   rt   T  r   r   r   r   c                 S  s   g | ]	}|D ]}|qqS r   r   )rs   tupr   r   r   r   rt   [  s    r    r=   )
r   rn   zipsqrtsumrk   reshaper%   r#   r   )	r   r   r   r   Ze0e1e2r   rot_objr   )r   r   r   from_3_pointsA  s&    
zFrame.from_3_pointsre   rE   c                 C  sH   |t | jkrtd| j|}| j|dkr|n|d }t||S )Nrf   r   r   )r-   r%   r&   r   r9   r   r   )r(   re   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  rr   r   r   rs   Tr   r   r   rt   r  ru   zFrame.cat.<locals>.<listcomp>c                 S  rr   r   r   r   r   r   r   rt   s  ru   r   r   rj   )r#   rv   r   r   )r   re   r   r   r   r   r   rv   m  s   (
z	Frame.catCallable[[Rotation], Rotation]c                 C  s   t || j| jS r2   r   r   r   r(   rh   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   rh        z#Frame.scale_translation.<locals>.fn)r   )r(   r   rh   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   rh     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   rs   i)c_xyzr   r   rt     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   rt     r   ).r   r   ).r   r   r!   r=   )r   r3   ranger   r   r   r%   r#   r5   r:   rQ   r4   r   )r   Zca_xyzr   r   Zinput_dtypeZc_xZc_yZd_pairnormZsin_c1Zcos_c1Zc1_rotsZsin_c2Zcos_c2Zc2_rotsZc_rots_Zn_yZn_zZsin_nZcos_nZ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   rx   r   rR   r   r   r   rx     s   z
Frame.cudara   c                 C  s   | j j| jjks
J | j jS r2   )r   r   r   rR   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   )rC   r   r   r   )rI   r   r   r   r   r   r   r~   )rN   r   r   r   r   )r   r   )rh   ri   r   r   )r7   r   r   r   )r   )
r   r   r   r   r   r   r   r3   r   r   )re   rE   r   r   )r   r   re   rE   r   r   )rh   r   r   r   )r   r3   r   r   )r   r   )"r   r   r   r)   r   r   r3   r   r/   rB   rH   rM   r   r%   r   r   r   rY   r[   rU   ro   r   r   r   r9   rv   r   r   r   r   r   rx   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   Zno_gradr   )r%   r   r   r   r   quatsr   r   r   r/     s   


zQuaternion.identityc                 C  rT   r2   )r   rR   r   r   r   	get_quats  r   zQuaternion.get_quatsc                 C  rT   r2   r   rR   r   r   r   r     r   zQuaternion.get_transc                 C  s   |   }t|}|S r2   )r   r   quat_to_rot)r(   r   rg   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!   rj   r   r   )
r   r3   _QUAT_TO_ROT_tensorr   rz   r   r   r4   r,   r%   )Znormalized_quatr   Z
rot_tensorr   r   r   r     s   
"zQuaternion.quat_to_rotc                 C  s2   | j }|  } | tjj| ddd } | |} | S )Nr   T)re   Zkeepdim)r   r3   r   Z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   rj   )r   r3   _QUAT_MULTIPLY_BY_VEC_tensorr   rz   r,   r-   r%   r   r   r4   )ZquatZvecr   r$   Z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   rY   r   r   )r(   r   r   Zq_vecZt_vecr   Z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   rR   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   Zfloat32Z_QUAT_TO_ROTr   Z
from_numpyr   Z_QUAT_MULTIPLYZ_QUAT_MULTIPLY_BY_VECr   r#   r   r   r   r   r   r   <module>   sR    


{  