B
     9c`O                 @   s   d Z ddlmZ ddlmZ ddlZddlZdgZdZdZ	dZ
G d	d
 d
eZG dd deZG dd deZyddk W n ek
r   dd ZY n
X dd Zdd Zdd ZG dd deddZeddddddZdd Zdd ZdS )zAffine transformation matrices

The Affine package is derived from Casey Duncan's Planar package. See the
copyright statement below.
    )division)
namedtupleNAffinezSean Gilliesz2.3.1gh㈵>c               @   s   e Zd ZdS )AffineErrorN)__name__
__module____qualname__ r	   r	   R/home/ankuromar296_gmail_com/.local/lib/python3.7/site-packages/affine/__init__.pyr   1   s   r   c               @   s   e Zd ZdZdS )TransformNotInvertibleErrorz#The transform could not be invertedN)r   r   r   __doc__r	   r	   r	   r
   r   5   s   r   c               @   s   e Zd ZdZdS )UndefinedRotationErrorz;The rotation angle could not be computed for this transformN)r   r   r   r   r	   r	   r	   r
   r   9   s   r       c             C   s   t S )z#Assert that a and b are unorderable)NotImplemented)abr	   r	   r
   assert_unorderableD   s    r   c             C   s    t dt| jt|jf dS )z#Assert that a and b are unorderablezunorderable types: %s and %sN)	TypeErrortyper   )r   r   r	   r	   r
   r   K   s    c                s.    j } j}|f fdd	}||_t||dS )zSpecial property decorator that caches the computed
    property value in the object's instance dict the first
    time it is accessed.
    c                s6   y
| j | S  tk
r0    |  | j |< }|S X d S )N)__dict__KeyError)selfnamevalue)funcr	   r
   getterY   s
    
zcached_property.<locals>.getter)doc)r   r   	func_nameproperty)r   r   r   r   r	   )r   r
   cached_propertyQ   s
    r    c             C   sJ   | d } | dkrdS | dkr dS | dkr,dS t | }t |t |fS )zReturn the cosine and sin for the given angle in degrees.

    With special-case handling of multiples of 90 for perfect right
    angles.
    g     v@g     V@)g        g      ?g     f@)g      r   g     p@)r   g      )mathradianscossin)degZradr	   r	   r
   cos_sin_degc   s    
r&   c               @   s  e Zd ZdZeZdd Zedd Zedd Z	edd	 Z
ed
d ZedHddZedIddZedd Zdd Zdd Zdd Zdd Zedd Zedd Zed d! Zed"d# Zed$d% Zed&d' Zed(d) Zed*d+ Zed,d- Zed.d/ Zed0d1 Zed2d3 Z ed4d5 Z!efd6d7Z"d8d9 Z#e# Z$ Z%Z&d:d; Z'e'Z(d<d= Z)d>d? Z*d@dA Z+dBdC Z,dDdE Z-e.j/Z/dFdG Z0dS )Jr   a  Two dimensional affine transform for 2D linear mapping.

    Parameters
    ----------
    a, b, c, d, e, f : float
        Coefficients of an augmented affine transformation matrix

        | x' |   | a  b  c | | x |
        | y' | = | d  e  f | | y |
        | 1  |   | 0  0  1 | | 1 |

        `a`, `b`, and `c` are the elements of the first row of the
        matrix. `d`, `e`, and `f` are the elements of the second row.

    Attributes
    ----------
    a, b, c, d, e, f, g, h, i : float
        The coefficients of the 3x3 augumented affine transformation
        matrix

        | x' |   | a  b  c | | x |
        | y' | = | d  e  f | | y |
        | 1  |   | g  h  i | | 1 |

        `g`, `h`, and `i` are always 0, 0, and 1.

    The Affine package is derived from Casey Duncan's Planar package.
    See the copyright statement below.  Parallel lines are preserved by
    these transforms. Affine transforms can perform any combination of
    translations, scales/flips, shears, and rotations.  Class methods
    are provided to conveniently compose transforms from these
    operations.

    Internally the transform is stored as a 3x3 transformation matrix.
    The transform may be constructed directly by specifying the first
    two rows of matrix values as 6 floats. Since the matrix is an affine
    transform, the last row is always ``(0, 0, 1)``.

    N.B.: multiplication of a transform and an (x, y) vector *always*
    returns the column vector that is the matrix multiplication product
    of the transform and (x, y) as a column vector, no matter which is
    on the left or right side. This is obviously not the case for
    matrices and vectors in general, but provides a convenience for
    users of this class.

    c             C   s0   dd ||||||gD dddg }t | |S )zCreate a new object

        Parameters
        ----------
        a, b, c, d, e, f : float
            Elements of an augmented affine transformation matrix.
        c             S   s   g | ]}|d  qS )g      ?r	   ).0xr	   r	   r
   
<listcomp>   s    z"Affine.__new__.<locals>.<listcomp>g        g      ?)tuple__new__)clsr   r   cdefmat3x3r	   r	   r
   r+      s    $zAffine.__new__c       	      C   s4   ||||||g}dd |D dddg }t | |S )zUse same coefficient order as GDAL's GetGeoTransform().

        :param c, a, b, f, d, e: 6 floats ordered by GDAL.
        :rtype: Affine
        c             S   s   g | ]}|d  qS )g      ?r	   )r'   r(   r	   r	   r
   r)      s    z$Affine.from_gdal.<locals>.<listcomp>g        g      ?)r*   r+   )	r,   r-   r   r   r0   r.   r/   membersr1   r	   r	   r
   	from_gdal   s    zAffine.from_gdalc             C   s   t S )z?Return the identity transform.

        :rtype: Affine
        )identity)r,   r	   r	   r
   r4      s    zAffine.identityc             C   s   t | dd|dd|dddf	S )zCreate a translation transform from an offset vector.

        :param xoff: Translation x offset.
        :type xoff: float
        :param yoff: Translation y offset.
        :type yoff: float
        :rtype: Affine
        g      ?g        )r*   r+   )r,   xoffyoffr	   r	   r
   translation   s
    
zAffine.translationc             G   sD   t |dkrt|d  }}n|\}}t| |ddd|ddddf	S )a3  Create a scaling transform from a scalar or vector.

        :param scaling: The scaling factor. A scalar value will
            scale in both dimensions equally. A vector scaling
            value scales the dimensions independently.
        :type scaling: float or sequence
        :rtype: Affine
           r   g        g      ?)lenfloatr*   r+   )r,   scalingsxZsyr	   r	   r
   scale   s    
zAffine.scaler   c             C   s>   t t |}t t |}t| d|d|dddddf	S )a  Create a shear transform along one or both axes.

        :param x_angle: Shear angle in degrees parallel to the x-axis.
        :type x_angle: float
        :param y_angle: Shear angle in degrees parallel to the y-axis.
        :type y_angle: float
        :rtype: Affine
        g      ?g        )r!   tanr"   r*   r+   )r,   Zx_angleZy_angleZmxZmyr	   r	   r
   shear   s    
zAffine.shearNc             C   s   t |\}}|dkr4t| || d||ddddf	S |\}}t| || |||  ||  |||||  ||  dddf	S dS )a  Create a rotation transform at the specified angle.

        A pivot point other than the coordinate system origin may be
        optionally specified.

        :param angle: Rotation angle in degrees, counter-clockwise
            about the pivot point.
        :type angle: float
        :param pivot: Point to rotate about, if omitted the rotation is
            about the origin.
        :type pivot: sequence
        :rtype: Affine
        Ng        g      ?)r&   r*   r+   )r,   ZangleZpivotcasaZpxpyr	   r	   r
   rotation   s    zAffine.rotationc             G   s   t | dS )zCreate the permutation transform

        For 2x2 matrices, there is only one permutation matrix that is
        not the identity.

        :rtype: Affine
        )	g        g      ?g        g      ?g        g        g        g        g      ?)r*   r+   )r,   r;   r	   r	   r
   permutation  s    
zAffine.permutationc             C   s   d|  S )zConcise string representation.z;|% .2f,% .2f,% .2f|
|% .2f,% .2f,% .2f|
|% .2f,% .2f,% .2f|r	   )r   r	   r	   r
   __str__(  s    zAffine.__str__c             C   s   d| dd  S )zPrecise string representation.z%Affine(%r, %r, %r,
       %r, %r, %r)N   r	   )r   r	   r	   r
   __repr__.  s    zAffine.__repr__c             C   s   | j | j| j| j| j| jfS )zZReturn same coefficient order as GDAL's SetGeoTransform().

        :rtype: tuple
        )r-   r   r   r0   r.   r/   )r   r	   r	   r
   to_gdal3  s    zAffine.to_gdalc             C   s   | j | j| j| j| j| jfS )zReturn an affine transformation matrix compatible with shapely

        Shapely's affinity module expects an affine transformation matrix
        in (a,b,d,e,xoff,yoff) order.

        :rtype: tuple
        )r   r   r.   r/   r5   r6   )r   r	   r	   r
   
to_shapely:  s    zAffine.to_shapelyc             C   s   | j S )zAlias for 'c')r-   )r   r	   r	   r
   r5   D  s    zAffine.xoffc             C   s   | j S )zAlias for 'f')r0   )r   r	   r	   r
   r6   I  s    zAffine.yoffc       
   	   C   s&   | \	}}}}}}}}}	|| ||  S )zThe determinant of the transform matrix.

        This value is equal to the area scaling factor when the
        transform is applied to a shape.
        r	   )
r   r   r   r-   r.   r/   r0   ghir	   r	   r
   determinantN  s    zAffine.determinantc          	   C   s   | \	}}}}}}}}}|d |d  |d  |d  }|| ||  d }|d d | }|dk rfd}t |d t | }	t |d t | }
|	|
fS )zThe absolute scaling factors of the transformation.

        This tuple represents the absolute value of the scaling factors of the
        transformation, sorted from bigger to smaller.
              g-q=r   )r!   sqrt)r   r   r   _r.   r/   traceZdetdeltal1l2r	   r	   r
   _scalingX  s     zAffine._scalingc             C   s$   | j \}}t|d |d  | S )zThe eccentricity of the affine transformation.

        This value represents the eccentricity of an ellipse under
        this affine transformation.

        Raises NotImplementedError for improper transformations.
        rN   )rV   r!   rP   )r   rT   rU   r	   r	   r
   eccentricityo  s    	
zAffine.eccentricityc       	   	   C   s\   | \	}}}}}}}}}| j s"| jrT| j\}}|| ||  }}t||d tj S tdS )aC  The rotation angle in degrees of the affine transformation.

        This is the rotation angle in degrees of the affine transformation,
        assuming it is in the form M = R S, where R is a rotation and S is a
        scaling.

        Raises UndefinedRotationError for improper and degenerate transformations.
           N)	is_properis_degeneraterV   r!   atan2pir   )	r   r   r   rQ   r-   r.   rT   yr(   r	   r	   r
   rotation_angle{  s    

zAffine.rotation_anglec             C   s   | t kp| t | jS )z[True if this transform equals the identity matrix,
        within rounding limits.
        )r4   almost_equals	precision)r   r	   r	   r
   is_identity  s    zAffine.is_identityc       
   	   C   sN   | \	}}}}}}}}}	t || jk r2t || jk pLt || jk oLt || jk S )zTrue if the transform is rectilinear.

        i.e., whether a shape would remain axis-aligned, within rounding
        limits, after applying the transform.
        )absr`   )
r   r   r   r-   r.   r/   r0   rJ   rK   rL   r	   r	   r
   is_rectilinear  s    zAffine.is_rectilinearc       
   	   C   s0   | \	}}}}}}}}}	t || ||  | jk S )zTrue if the transform is conformal.

        i.e., if angles between points are preserved after applying the
        transform, within rounding limits.  This implies that the
        transform has no effective shear.
        )rb   r`   )
r   r   r   r-   r.   r/   r0   rJ   rK   rL   r	   r	   r
   is_conformal  s    zAffine.is_conformalc       
   	   C   sX   | \	}}}}}}}}}	| j oVtd|| ||   | jk oVtd|| ||   | jk S )ay  True if the transform is orthonormal.

        Which means that the transform represents a rigid motion, which
        has no effective scaling or shear. Mathematically, this means
        that the axis vectors of the transform matrix are perpendicular
        and unit-length.  Applying an orthonormal transform to a shape
        always results in a congruent shape.
        g      ?)rd   rb   r`   )
r   r   r   r-   r.   r/   r0   rJ   rK   rL   r	   r	   r
   is_orthonormal  s    
zAffine.is_orthonormalc             C   s
   | j dkS )zTrue if this transform is degenerate.

        Which means that it will collapse a shape to an effective area
        of zero. Degenerate transforms cannot be inverted.
        g        )rM   )r   r	   r	   r
   rZ     s    zAffine.is_degeneratec             C   s
   | j dkS )zdTrue if this transform is proper.

        Which means that it does not include reflection.
        g        )rM   )r   r	   r	   r
   rY     s    zAffine.is_properc          	   C   s,   | \	}}}}}}}}}||f||f||ffS )z6The values of the transform as three 2D column vectorsr	   )r   r   r   r-   r.   r/   r0   rQ   r	   r	   r
   column_vectors  s    zAffine.column_vectorsc             C   s.   x(dD ] }t | | ||  |krdS qW dS )a
  Compare transforms for approximate equality.

        :param other: Transform being compared.
        :type other: Affine
        :return: True if absolute difference between each element
            of each respective transform matrix < ``self.precision``.
        )r   r8   rN   r   rO      FT)rb   )r   otherr`   rL   r	   r	   r
   r_     s    
zAffine.almost_equalsc             C   s
   t | |S )N)r   )r   rh   r	   r	   r
   __gt__  s    zAffine.__gt__c             C   s   t dd S )NzOperation not supported)r   )r   rh   r	   r	   r
   __add__  s    zAffine.__add__c             C   s   | \	}}}}}}}}}t |tr|\	}	}
}}}}}}}t| j||	 ||  ||
 ||  || ||  | ||	 ||  ||
 ||  || ||  | dddf	S y0|\}}|| ||  | || ||  | fS  ttfk
r   tS X dS )a  Multiplication

        Apply the transform using matrix multiplication, creating
        a resulting object of the same type.  A transform may be applied
        to another transform, a vector, vector array, or shape.

        :param other: The object to transform.
        :type other: Affine, :class:`~planar.Vec2`,
            :class:`~planar.Vec2Array`, :class:`~planar.Shape`
        :rtype: Same as ``other``
        g        g      ?N)
isinstancer   r*   r+   	__class__
ValueErrorr   r   )r   rh   rA   sbscsdsesfrQ   ZoaobocZodZoeZofZvxZvyr	   r	   r
   __mul__  s    
..(zAffine.__mul__c             C   s(   t jdtdd t|trt| |S )a|  Right hand multiplication

        .. deprecated:: 2.3.0
            Right multiplication will be prohibited in version 3.0. This method
            will raise AffineError.

        Notes
        -----
        We should not be called if other is an affine instance This is
        just a guarantee, since we would potentially return the wrong
        answer in that case.
        z6Right multiplication will be prohibited in version 3.0rN   )
stacklevel)warningswarnDeprecationWarningrk   r   AssertionErrorru   )r   rh   r	   r	   r
   __rmul__  s    
zAffine.__rmul__c             C   s&   t |tst |tr| |S tS d S )N)rk   r   r*   ru   r   )r   rh   r	   r	   r
   __imul__  s    
zAffine.__imul__c          	   C   sp   | t k	rl| t krl| \	}}}}}}}}}xDt|D ]8\}	\}
}|
| ||  | |
| ||  | f||	< q0W dS )zTransform a sequence of points or vectors in place.

        :param seq: Mutable sequence of :class:`~planar.Vec2` to be
            transformed.
        :returns: None, the input sequence is mutated in place.
        N)r4   	enumerate)r   seqrA   rn   ro   rp   rq   rr   rQ   rL   r(   r]   r	   r	   r
   
itransform  s    zAffine.itransformc             C   s   | j rtdd| j }| \	}}}}}}}}}|| }	| | }
| | }|| }t| j|	|
| |	 ||
  ||| | ||  dddf	S )zReturn the inverse transform.

        :raises: :except:`TransformNotInvertible` if the transform
            is degenerate.
        z"Cannot invert degenerate transformg      ?g        )rZ   r   rM   r*   r+   rl   )r   ZidetrA   rn   ro   rp   rq   rr   rQ   rarbrdrer	   r	   r
   
__invert__*  s    


zAffine.__invert__c             C   s   | j | j| j| j| j| jfS )a  Pickle protocol support

        Notes
        -----
        Normal unpickling creates a situation where __new__ receives all
        9 elements rather than the 6 that are required for the
        constructor.  This method ensures that only the 6 are provided.
        )r   r   r-   r.   r/   r0   )r   r	   r	   r
   __getnewargs__A  s    	zAffine.__getnewargs__)r   r   )N)1r   r   r   r   EPSILONr`   r+   classmethodr3   r4   r7   r=   r?   rC   rD   rE   rG   rH   rI   r   r5   r6   r    rM   rV   rW   r^   ra   rc   rd   re   rZ   rY   rf   r_   ri   __ge____lt____le__rj   __iadd__ru   r{   r|   r   r   r*   __hash__r   r	   r	   r	   r
   r   t   sR   /

	)	r   r   r-   r.   r/   r0   rJ   rK   rL   r8   c       	      C   s~   t | dstd|  }t|dkr6tdt| dd |D \}}}}}}tt||||||dddg	}|td	d	 S )
zReturns Affine from the contents of a world file string.

    This method also translates the coefficients from from center- to
    corner-based coordinates.

    :param s: str with 6 floats ordered in a world file.
    :rtype: Affine
    splitzCannot split input stringrF   z!Expected 6 coefficients, found %dc             S   s   g | ]}t |qS r	   )r:   )r'   r(   r	   r	   r
   r)   a  s    zloadsw.<locals>.<listcomp>g        g      ?g      )	hasattrr   r   r9   rm   r*   r+   r   r7   )	sZcoeffsr   r.   r   r/   r-   r0   centerr	   r	   r
   loadswS  s    	
r   c                s0   | t dd  d fddtdD d S )zReturn string for a world file.

    This method also translates the coefficients from from corner- to
    center-based coordinates.

    :rtype: str
    g      ?
c             3   s   | ]}t t |V  qd S )N)reprgetattr)r'   r(   )r   r	   r
   	<genexpr>o  s    zdumpsw.<locals>.<genexpr>Zadbecf)r   r7   joinlist)objr	   )r   r
   dumpswf  s    r   )r   
__future__r   collectionsr   r!   rw   __all__
__author____version__r   	Exceptionr   r   r   r   r   r    r&   r   r4   r   r   r	   r	   r	   r
   <module>   s4   
   [