U
    Z-dl                     @   s   d dl Z d dlZd dlZd dlZejddkr>d dlm	Z	 nd dl
m	Z	 d dlmZ dd Zdd	 Zd
d ZdddZdddZdd ZdddZdS )    NUSERmike)quaternion_from_euler)URDFc                 C   s  t tjd }t j| dd t jd }t ||}||k rJt dS |t	
d| 9 }t ||}t jd|d  |d  |d	 |d
  |d |d  df|d	 |d
  d|d  |d  |d |d  df|d |d  |d |d  d|d  |d  dfdft jdS )z_Return homogeneous rotation matrix from quaternion.
    Copied from 2006, Christoph Gohlke
    g      @N   dtypeg       @      ?)   r
   )   r   )r   r
   r      )r   r   r
   r           )r   r   )r
   r   r   r   )r   r   r   r	   )npfinfofloatepsarrayfloat64copydotidentitymathsqrtouter)Z
quaternionZ_EPSZq_Znqq r   ;/home/daniela/catkin_ws/src/hpe/scripts/utils/transforms.pyquaternionMatrix   s    
222r    c                 C   s8   t |}| d |d< | d |d< | d |d< d|d< |S )Nr   r   r
   r   r   r   )r   r   )r    )transquatmatrixr   r   r    translationQuaternionToTransform"   s    r$   c           	      C   s   t | }i }|jD ]~}|j}|j}|jj}|jj}t||}i ||< ||| d< ||| d< ||| d< t	t
|d |d |d dd|| d	< q|S )
Nchildparentr!   r   r
   r   Zsxyz)axesr"   )r   Zfrom_xml_filejointsr%   r&   originxyzrpygenerateKeylistr   )	fileZ	xml_robotdictjointr%   r&   r*   r+   keyr   r   r   get_transform_tree_dict+   s    


*r2    c                 C   s   | d | | S )N-r   )r&   r%   suffixr   r   r   r,   >   s    r,   Fc                 C   s   g }t  }| D ]\}}||d |d  q|dkr\t j|dd ddlm} |  t || |}	t	dt
|	d D ]0}
|	|
 }|	|
d  }|||t||d q||S )	a*   Gets a chain of transforms given two reference frames and a se of transformations. Computes a graph from the
    set of transforms, and then finds a path in the graph between the two given links.
    @param from_frame: initial frame
    @param to_frame: final frame
    @param transform_pool: a dictionary containing several transforms
    @return:  a chain of transforms The standard we have is to use a list of dictionaries, each containing
    # information about the transform: [{'parent': parent, 'child': child, 'key': 'parent-child'}, {...}]
    r&   r%   T)Zwith_labelsr   Nr
   )r&   r%   r1   )nxZGraphitemsZadd_edgedrawmatplotlib.pyplotpyplotshowshortest_pathrangelenappendr,   )
from_frameto_frameZtransform_poolr8   chaingraphZtransform_key	transformpltpathidxr&   r%   r   r   r   getChainA   s    	rH   c           	      C   s   t jdt jd}| D ]}t|d |d }t|d |d }|| krl|| d }|| d }t||}nX|| kr|| d }|| d }t jt||}n td|d  d |d  d	 t 	||}q|S )
z Multiplies local transforms in a chain to get the global transform of the chain
    @param chain: a list of transforms
    @param transforms: a pool of transformations
    @return: the global transformation (4x4 homogeneous)
    r   r   r&   r%   r!   r"   zTransform from z to z does not exist.)
r   eyer   r,   keysr$   linalginv
ValueErrorr   )	rB   
transformsrD   linkr1   Zinverse_keyr!   r"   Zparent_T_childr   r   r   getAggregateTransform`   s     rP   c                 C   s   t | |||}t||S )z Gets a transformation between any two frames
    @param from_frame: Starting frame
    @param to_frame: Ending frame
    @param transforms: dictionary of several transforms
    @return: the global transformation (4x4 homogeneous)
    )rH   rP   )r@   rA   rN   r8   rB   r   r   r   getTransform   s    rQ   )r3   )F)F)r   osnumpyr   Znetworkxr6   environgetZtf_transformationsr   Ztf.transformationsZurdf_parser_py.urdfr   r    r$   r2   r,   rH   rP   rQ   r   r   r   r   <module>   s   	

