a
    d                     @   s   d dl mZmZ d dlZd dlmZmZmZmZ d dl	m
Z
 d dlmZmZmZ ddgZG dd	 d	ZG d
d deZdeee edddZdS )    )OptionalUnionN)Module	ParameterTensor	normalize)squared_norm)KORNIA_CHECKKORNIA_CHECK_IS_TENSORKORNIA_CHECK_SHAPEParametrizedLinefit_linec                   @   s   e Zd ZdZdS )
HyperplanezeNot implemented yet: https://gitlab.com/libeigen/eigen/-/blob/master/Eigen/src/Geometry/Hyperplane.h.N)__name__
__module____qualname____doc__ r   r   ]/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/geometry/line.pyr      s   r   c                       s   e Zd ZdZeedd fddZedddZedd	d
ZedddZ	dd Z
eedddZeedddZedddZed dddZeed dddZeeef edddZeedddZeedd d!Zeedd"d#Z  ZS )$r   zClass that describes a parametrize line.

    A parametrized line is defined by an origin point :math:`o` and a unit
    direction vector :math:`d` such that the line corresponds to the set

    .. math::

        l(t) = o + t * d
    N)origin	directionreturnc                    s"   t    t|| _t|| _dS )al  Initializes a parametrized line of direction and origin.

        Args:
            origin: any point on the line of any dimension.
            direction: the normalized vector direction of any dimension.

        Example:
            >>> o = torch.tensor([0.0, 0.0])
            >>> d = torch.tensor([1.0, 1.0])
            >>> l = ParametrizedLine(o, d)
        N)super__init__r   _origin
_direction)selfr   r   	__class__r   r   r       s    

zParametrizedLine.__init__)r   c                 C   s   d| j  d| j S )NzOrigin: z
Direction: r   r   r   r   r   r   __str__0   s    zParametrizedLine.__str__c                 C   s   t | S N)strr    r   r   r   __repr__3   s    zParametrizedLine.__repr__c                 C   s   |dkr| j S | jS )Nr   r   )r   idxr   r   r   __getitem__6   s    zParametrizedLine.__getitem__c                 c   s   | j | jfE d H  d S r"   r   r    r   r   r   __iter__9   s    zParametrizedLine.__iter__c                 C   s   | j S )zReturn the line origin point.)r   r    r   r   r   r   <   s    zParametrizedLine.originc                 C   s   | j S )z!Return the line direction vector.)r   r    r   r   r   r   A   s    zParametrizedLine.directionc                 C   s   | j jd S )z-Return the dimension in which the line holds.)r   shaper    r   r   r   dimF   s    zParametrizedLine.dimc                 C   s   t |t|| dddS )a  Constructs a parametrized line going from a point :math:`p0` to :math:`p1`.

        Args:
            p0: tensor with first point :math:`(B, D)` where `D` is the point dimension.
            p1: tensor with second point :math:`(B, D)` where `D` is the point dimension.

        Example:
            >>> p0 = torch.tensor([0.0, 0.0])
            >>> p1 = torch.tensor([1.0, 1.0])
            >>> l = ParametrizedLine.through(p0, p1)
           r(   )pr*   )r   r   )clsZp0p1r   r   r   throughJ   s    zParametrizedLine.through)planer   c                 C   s   t d| dd S )NzPlane not implemented yet .)NotImplementedError)r-   r0   r   r   r   from_hyperplaneY   s    z ParametrizedLine.from_hyperplane)tr   c                 C   s   | j | j|  S )a_  The point at :math:`t` along this line.

        Args:
            t: step along the line.

        Return:
            tensor with the point.

        Example:
            >>> p0 = torch.tensor([0.0, 0.0])
            >>> p1 = torch.tensor([1.0, 1.0])
            >>> l = ParametrizedLine.through(p0, p1)
            >>> p2 = l.point_at(0.1)
        r   )r   r4   r   r   r   point_at]   s    zParametrizedLine.point_at)pointr   c                 C   s   | j | j|| j   | j  S )zuReturn the projection of a point onto the line.

        Args:
            point: the point to be projected.
        r   r   r6   r   r   r   
projectionn   s    zParametrizedLine.projectionc                 C   s"   || j  }t|| j| | j  S )zReturn the squared distance of a point to its projection onte the line.

        Args:
            point: the point to calculate the distance onto the line.
        )r   r   r   )r   r6   diffr   r   r   squared_distancew   s    
z!ParametrizedLine.squared_distancec                 C   s   |  | S )zReturn the distance of a point to its projections onto the line.

        Args:
            point: the point to calculate the distance into the line.
        )r:   sqrtr7   r   r   r   distance   s    zParametrizedLine.distance)r   r   r   r   r   r   r#   r!   r$   r&   r'   propertyr   r   intr*   classmethodr/   r   r3   r   floatr5   r8   r:   r<   __classcell__r   r   r   r   r      s&   
	
)pointsweightsr   c                 C   s   t | d t| g d | dd}| | }|dur~t |d t|dg t| jd |jd k |dd	t| | }n|dd	| }tj	|\}}}|d
dddf }|d
dddf }t
||S )af  Fit a line from a set of points.

    Args:
        points: tensor containing a batch of sets of n-dimensional points. The  expected
            shape of the tensor is :math:`(B,N,D)`.
        weights: weights to use to solve the equations system. The  expected
            shape of the tensor is :math:`(B,N)`.

    Return:
        A tensor containing the direction of the fited line of shape :math:`(B,D)`.

    Example:
        >>> points = torch.rand(2, 10, 3)
        >>> weights = torch.ones(2, 10)
        >>> line = fit_line(points, weights)
        >>> line.direction.shape
        torch.Size([2, 3])
    zpoints must be a tensor)BNDTNzweights must be a tensorrD   r   r(   .)r
   r   meanr	   r)   Z	transposetorchZ
diag_embedZlinalgZsvdr   )rB   rC   rH   A_Vr   r   r   r   r   r      s    

)N)typingr   r   rI   Zkornia.corer   r   r   r   Zkornia.geometry.linalgr   Zkornia.testingr	   r
   r   __all__r   r   r   r   r   r   r   <module>   s   z