a
    ßÁdÜ  ã                   @   sp   d Z ddlmZmZmZmZ ddlZddlZg d¢Z	dd„ Z
dd„ Zddd„Zddd„Zddd„Zddd„ZdS )zBAffine transforms, both in general and specific, named transforms.é    )ÚcosÚpiÚsinÚtanN)Úaffine_transformÚrotateÚscaleÚskewÚ	translatec                    s  t |ƒdkrDd}|\}}}}}}| jr„d}d}	d }
 } } }}n@t |ƒdkr|d}|\}}}
}}}}}}	}}}| js„d}ntdƒ‚|dkrºtj||g||ggtd�‰ tj||gtd�‰n:tj|||
g|||g|||	ggtd�‰ tj|||gtd�‰‡ ‡fd	d
„}tj| ||dkd�S )a$  Return a transformed geometry using an affine transformation matrix.

    The coefficient matrix is provided as a list or tuple with 6 or 12 items
    for 2D or 3D transformations, respectively.

    For 2D affine transformations, the 6 parameter matrix is::

        [a, b, d, e, xoff, yoff]

    which represents the augmented matrix::

        [x']   / a  b xoff \ [x]
        [y'] = | d  e yoff | [y]
        [1 ]   \ 0  0   1  / [1]

    or the equations for the transformed coordinates::

        x' = a * x + b * y + xoff
        y' = d * x + e * y + yoff

    For 3D affine transformations, the 12 parameter matrix is::

        [a, b, c, d, e, f, g, h, i, xoff, yoff, zoff]

    which represents the augmented matrix::

        [x']   / a  b  c xoff \ [x]
        [y'] = | d  e  f yoff | [y]
        [z']   | g  h  i zoff | [z]
        [1 ]   \ 0  0  0   1  / [1]

    or the equations for the transformed coordinates::

        x' = a * x + b * y + c * z + xoff
        y' = d * x + e * y + f * z + yoff
        z' = g * x + h * y + i * z + zoff
    é   é   é   ç      ð?ç        é   z,'matrix' expects either 6 or 12 coefficients)Zdtypec                    s   t  ˆ | j¡jˆ S )N)ÚnpÚmatmulÚT)Úcoords©ÚAÚoff© úS/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/shapely/affinity.pyÚ_affine_coordsG   s    z(affine_transform.<locals>._affine_coords)Z	include_z)ÚlenZhas_zÚ
ValueErrorr   ÚarrayÚfloatÚshapelyZ	transform)ÚgeomÚmatrixÚndimÚaÚbÚdÚeÚxoffÚyoffÚiÚcÚfÚgÚhÚzoffr   r   r   r   r      s(    &&r   c                 C   sÀ   |dkr0| j \}}}}|| d || d f}nL|dkrF| jjd }n6t|tƒrbtd|›d�ƒ‚nt|ddƒd	kr||jd }t|ƒd
vr�tdƒ‚|dkr¤|dd… S t|ƒdkr¸|d S |S dS )a6  Returns interpreted coordinate tuple for origin parameter.

    This is a helper function for other transform functions.

    The point of origin can be a keyword 'center' for the 2D bounding box
    center, 'centroid' for the geometry's 2D centroid, a Point object or a
    coordinate tuple (x0, y0, z0).
    Úcenterg       @Úcentroidr   z'origin' keyword z is not recognizedZ	geom_typeNZPoint)r   r   z8Expected number of items in 'origin' to be either 2 or 3r   )r   )Zboundsr0   r   Ú
isinstanceÚstrr   Úgetattrr   )r    Úoriginr"   ZminxZminyZmaxxZmaxyr   r   r   Úinterpret_originM   s     


r5   r/   Fc           	      C   s¢   | j r
| S |s|t d }t|ƒ}t|ƒ}t|ƒdk r:d}t|ƒdk rJd}t| |dƒ\}}|| d||dddd|||  ||  |||  ||  df}t| |ƒS )aæ  Returns a rotated geometry on a 2D plane.

    The angle of rotation can be specified in either degrees (default) or
    radians by setting ``use_radians=True``. Positive angles are
    counter-clockwise and negative are clockwise rotations.

    The point of origin can be a keyword 'center' for the bounding box
    center (default), 'centroid' for the geometry's centroid, a Point object
    or a coordinate tuple (x0, y0).

    The affine transformation matrix for 2D rotation is:

      / cos(r) -sin(r) xoff \
      | sin(r)  cos(r) yoff |
      \   0       0      1  /

    where the offsets are calculated from the origin Point(x0, y0):

        xoff = x0 - x0 * cos(r) + y0 * sin(r)
        yoff = y0 - x0 * sin(r) - y0 * cos(r)
    ç     €f@çVçž¯²<r   r   r   )Úis_emptyr   r   r   Úabsr5   r   )	r    Zangler4   Úuse_radiansZcospZsinpÚx0Úy0r!   r   r   r   r   n   s"    &ýr   r   c           	      C   sZ   | j r
| S t| |dƒ\}}}|ddd|ddd||||  |||  |||  f}t| |ƒS )a·  Returns a scaled geometry, scaled by factors along each dimension.

    The point of origin can be a keyword 'center' for the 2D bounding box
    center (default), 'centroid' for the geometry's 2D centroid, a Point
    object or a coordinate tuple (x0, y0, z0).

    Negative scale factors will mirror or reflect coordinates.

    The general 3D affine transformation matrix for scaling is:

        / xfact  0    0   xoff \
        |   0  yfact  0   yoff |
        |   0    0  zfact zoff |
        \   0    0    0     1  /

    where the offsets are calculated from the origin Point(x0, y0, z0):

        xoff = x0 - x0 * xfact
        yoff = y0 - y0 * yfact
        zoff = z0 - z0 * zfact
    r   r   )r8   r5   r   )	r    ZxfactZyfactZzfactr4   r;   r<   Zz0r!   r   r   r   r   ™   s    ýr   r   c           
      C   s˜   | j r
| S |s&|t d }|t d }t|ƒ}t|ƒ}t|ƒdk rFd}t|ƒdk rVd}t| |dƒ\}}d|d|ddddd| | | | df}	t| |	ƒS )a˜  Returns a skewed geometry, sheared by angles along x and y dimensions.

    The shear angle can be specified in either degrees (default) or radians
    by setting ``use_radians=True``.

    The point of origin can be a keyword 'center' for the bounding box
    center (default), 'centroid' for the geometry's centroid, a Point object
    or a coordinate tuple (x0, y0).

    The general 2D affine transformation matrix for skewing is:

        /   1    tan(xs) xoff \
        | tan(ys)  1     yoff |
        \   0      0       1  /

    where the offsets are calculated from the origin Point(x0, y0):

        xoff = -y0 * tan(xs)
        yoff = -x0 * tan(ys)
    r6   r7   r   r   r   )r8   r   r   r9   r5   r   )
r    ÚxsZysr4   r:   ZtanxZtanyr;   r<   r!   r   r   r   r	   ¼   s$    ýr	   c                 C   s0   | j r
| S ddddddddd|||f}t| |ƒS )zõReturns a translated geometry shifted by offsets along each dimension.

    The general 3D affine transformation matrix for translation is:

        / 1  0  0 xoff \
        | 0  1  0 yoff |
        | 0  0  1 zoff |
        \ 0  0  0   1  /
    r   r   )r8   r   )r    r'   r(   r.   r!   r   r   r   r
   ç   s    
ýr
   )r/   F)r   r   r   r/   )r   r   r/   F)r   r   r   )Ú__doc__Úmathr   r   r   r   Únumpyr   r   Ú__all__r   r5   r   r   r	   r
   r   r   r   r   Ú<module>   s   A!
+
#
+