
    3j'                     X   S SK JrJrJrJr  S SKrS SKJs  Jr	  S SKJr  S SK
JrJrJr  S SKJr  S SKJr  S SKJr  SS	/r " S
 S\R*                  5      rS\R.                  S\4S jrS\R.                  S\R.                  S\4S jrSS\R.                  S\\R.                     S\4S jjrg)    )IteratorOptionalTupleUnionN)nn)KORNIA_CHECKKORNIA_CHECK_IS_TENSORKORNIA_CHECK_SHAPE)_torch_svd_cast)batched_dot_product)
HyperplaneParametrizedLinefit_linec            	         ^  \ rS rSrSrS\R                  S\R                  SS4U 4S jjrS\4S jr	S\4S	 jr
S
\S\R                  4S jrS\\R                     4S jr\S\R                  4S j5       r\S\R                  4S j5       rS\4S jr\S\R                  S\R                  SS 4S j5       rS\\\R                  4   S\R                  4S jrS\R                  S\R                  4S jrS\R                  S\R                  4S jrS\R                  S\R                  4S jrSS\S\S\\R                  \R                  4   4S jjrSrU =r$ )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
origin	directionreturnNc                    > [         TU ]  5         [        R                  " U5      U l        [        R                  " U5      U l        g)a,  Initialize 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   	Parameter_origin
_direction)selfr   r   	__class__s      N/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/geometry/line.pyr   ParametrizedLine.__init__-   s0     	||F+,,y1    c                 :    SU R                    SU R                   3$ )NzOrigin: z
Direction: r   r   r   s    r   __str__ParametrizedLine.__str__>   s    $++mDNN3CDDr   c                     [        U 5      $ N)strr"   s    r   __repr__ParametrizedLine.__repr__A   s    4yr   idxc                 >    US:X  a  U R                   $ U R                  $ )Nr   r!   )r   r*   s     r   __getitem__ParametrizedLine.__getitem__D   s    !Qht{{:DNN:r   c              #   P   #    U R                   U R                  4 S h  vN   g  N7fr&   r!   r"   s    r   __iter__ParametrizedLine.__iter__G   s     KK000s   &$&c                     U R                   $ )zReturn the line origin point.)r   r"   s    r   r   ParametrizedLine.originJ   s     ||r   c                     U R                   $ )z!Return the line direction vector.)r   r"   s    r   r   ParametrizedLine.directionO   s     r   c                 4    U R                   R                  S   $ )z-Return the dimension in which the line holds.)r   shaper"   s    r   dimParametrizedLine.dimT   s    ~~##B''r   p0p1c           	      F    [        U[        R                  " X!-
  SSS95      $ )ay  Construct 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)

   r6   )pr8   )r   F	normalize)clsr:   r;   s      r   throughParametrizedLine.throughX   s       AKKQB$GHHr   tc                 :    U R                   U R                  U-  -   $ )a  Get 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   rD   s     r   point_atParametrizedLine.point_ath   s      {{T^^a///r   pointc                 l    U R                   U R                  XR                   -
  -  U R                  -  -   $ )z^Return the projection of a point onto the line.

Args:
    point: the point to be projected.

r!   r   rH   s     r   
projectionParametrizedLine.projectionz   s,     {{dnn0CDVVVr   c                     XR                   -
  n[        R                  " X R                  -  SS9n[        R                  " X"-  SS9nXCU-  -
  $ )zReturn the squared distance of a point to its projection onte the line.

Args:
    point: the point to calculate the distance onto the line.
r6   r8   )r   torchsumr   )r   rH   dproj	sq_norm_ds        r   squared_distance!ParametrizedLine.squared_distance   sF     KKyy^^+4IIae,	$;&&r   c                 @    U R                  U5      R                  5       $ )zReturn the distance of a point to its projections onto the line.

Args:
    point: the point to calculate the distance into the line.
)rT   sqrtrJ   s     r   distanceParametrizedLine.distance   s     $$U+0022r   planeepsc                    [        UR                  R                  U R                  R                  5      nUR	                  5       U:  n[
        R                  " UUR                  [        UR                  R                  U R                  R                  5      -   * U-  [
        R                  " U5      5      nU R                  U5      nXV4$ )a  Return the intersection point between the line and a given plane.

Args:
    plane: the plane to compute the intersection point.
    eps: epsilon for numerical stability.

Return:
    - the lambda value used to compute the look at point.
    - the intersected point.

)r   normaldatar   absrO   whereoffsetr   
empty_likerF   )r   rZ   r[   dot_proddot_prod_mask
res_lambda	res_points          r   	intersectParametrizedLine.intersect   s     'u||'8'8$..:M:MN #- [[ll01B1BDKKDTDTUUVYaaX&

 MM*-	$$r   )r   r   )gư>) __name__
__module____qualname____firstlineno____doc__rO   Tensorr   r'   r#   r(   intr,   r   r/   propertyr   r   r8   classmethodrB   r   floatrF   rK   rT   rX   r   r   rg   __static_attributes____classcell__)r   s   @r   r   r   "   s   2u|| 2 2 2"E E# ;s ;u|| ;1(5<<0 1    5<<  (S ( I I5<< I<N I I0%u|| 34 0 0$W W W	'ell 	'u|| 	'3ell 3u|| 3%z % %u||UZUaUaGaAb % %r   pointsr   c           	      t   U S   nU S   nUR                  SSS9nUR                  SSS9nX-
  nX$-
  nXU-  R                  SSS9n[        R                  " US:  XV-  R                  SSS9U-  [        R                  " U5      5      n[        R                  " US:  [        R
                  " [        R                  " U5      U/SS9[        R                  " SS	/U R                  S
9R                  U R                  S   S5      5      n	XR                  SSS9-  n	[        R
                  " X4/SS9n
[        X5      $ )N.r   .   r6   Tr8   keepdim:0yE>rN                 ?)devicer   r=   )meanrP   rO   r`   
zeros_likecat	ones_liketensorr   expandr7   normr   )ru   xyx_meany_meandxdydenomsloper   r   s              r   _fit_line_ols_2dr      s$   vAvAVVDV)FVVDV)F	
B	
BWMMb$M/EKKrwmmDm&IE&QSXScScdiSjkE 		5??5)51r:c3Z6==fll1oqQI NNr4N@@IYY'R0FF..r   weightsc                    U S   nU S   nUR                  SSS9nX-  R                  SSS9U-  nX-  R                  SSS9U-  nX%-
  nX6-
  nX-  U-  n	X-  U-  n
U	R                  SSS9nU
R                  SSS9U-  n[        R                  " [        R                  " U5      U[        R                  " U5      5      nUS:*  n[        R
                  " [        R                  " U5      U/SS9n[        R                  " SS	/U R                  U R                  S
9nXUR                  S5      '   XR                  SSS9-  n[        R
                  " XV/SS9n[        UU5      $ )Nrw   rx   r6   Trz   r|   rN   r}   r~   )r   dtype)rP   rO   r`   isfiniter   r   r   r   r   r   squeezer   r   )ru   r   r   r   w_sumr   r   r   r   weighted_dx2weighted_dxdyr   r   is_verticalr   replacementr   s                    r   _fit_line_weighted_ols_2dr      sj   vAvAKKBK-Ek2t4u<Fk2t4u<F	
B	
B<"$LL2%MT2E"d3e;E KKu-ue6F6Fu6MNE 4-K		5??5159rBI,,Sz&--v||TK)4k!!"%&NNr4N@@IYY'R0FFI..r   c                    [        U S5        [        U / SQ5        U R                  u  p#nUS:X  a[  UbM  [        US5        [        USS/5        [        U R                  S   UR                  S   :H  5        [	        X5      $ [        U 5      $ U R                  S	S
5      nX-
  nUbo  [        US5        [        USS/5        [        U R                  S   UR                  S   :H  5        UR                  S	S5      [        R                  " U5      -  U-  nOUR                  S	S5      U-  n[        U5      u    pxUR                  S	S5      nUSSSS24   n	USSSS24   n
[        X5      $ )a1  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 fitted 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Dr=   Nzweights must be a tensorr   r   r   Tr6   .)r	   r
   r7   r   r   r   r   	transposerO   
diag_embedr   r   )ru   r   _B_Nr   r   A_Vr   r   s              r   r   r      s[   & 6#<=v/IBA 	Av"7,FGwc
3aGMM!,<<=,V==#F++;;r4 DAw(BC7S#J/V\\!_a(889KKB%"2"27";;a?KKB!# a GAq	BA #q!)I#q!)_FF..r   r&   )typingr   r   r   r   rO   torch.nn.functionalr   
functionalr?   kornia.core.checkr   r	   r
   kornia.core.utilsr   kornia.geometry.linalgr   kornia.geometry.planer   __all__Moduler   rn   r   r   r    r   r   <module>r      s   ( 4 3     V V - 6 ,z
*Q%ryy Q%h/U\\ /.> /./ell /U\\ /N^ /@5/U\\ 5/HU\\,B 5/N^ 5/r   