a
    d                  	   @   s   d dl mZ d dlZd dlmZmZ d dlmZ dedejfe	e	e
eej ejeddd	Zdedejfe	e	e	e
eej ejed
ddZdS )    )OptionalN)Tensorstack)torch_meshgridTcpu)heightwidthnormalized_coordinatesdevicedtypereturnc                 C   s   t jd|d |||d}t jd| d | ||d}|r\||d  d d }|| d  d d }tt||gdddd	}|ddddS )
an  Generate a coordinate grid for an image.

    When the flag ``normalized_coordinates`` is set to True, the grid is
    normalized to be in the range :math:`[-1,1]` to be consistent with the pytorch
    function :py:func:`torch.nn.functional.grid_sample`.

    Args:
        height: the image height (rows).
        width: the image width (cols).
        normalized_coordinates: whether to normalize
          coordinates in the range :math:`[-1,1]` in order to be consistent with the
          PyTorch function :py:func:`torch.nn.functional.grid_sample`.
        device: the device on which the grid will be generated.
        dtype: the data type of the generated grid.

    Return:
        grid tensor with shape :math:`(1, H, W, 2)`.

    Example:
        >>> create_meshgrid(2, 2)
        tensor([[[[-1., -1.],
                  [ 1., -1.]],
        <BLANKLINE>
                 [[-1.,  1.],
                  [ 1.,  1.]]]])

        >>> create_meshgrid(2, 2, normalized_coordinates=False)
        tensor([[[[0., 0.],
                  [1., 0.]],
        <BLANKLINE>
                 [[0., 1.],
                  [1., 1.]]]])
    r      r
   r         ?   ijZindexingZdimtorchZlinspacer   r   ZpermuteZ	unsqueeze)r   r   r	   r
   r   xsys	base_grid r   Z/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/utils/grid.pycreate_meshgrid	   s    (	r   )depthr   r   r	   r
   r   r   c           
      C   s   t jd|d |||d}t jd|d |||d}t jd| d | ||d}|r||d  d d }||d  d d }|| d  d d }tt|||gdddd	}	|	dddd
dS )a  Generate a coordinate grid for an image.

    When the flag ``normalized_coordinates`` is set to True, the grid is
    normalized to be in the range :math:`[-1,1]` to be consistent with the pytorch
    function :py:func:`torch.nn.functional.grid_sample`.

    Args:
        depth: the image depth (channels).
        height: the image height (rows).
        width: the image width (cols).
        normalized_coordinates: whether to normalize
          coordinates in the range :math:`[-1,1]` in order to be consistent with the
          PyTorch function :py:func:`torch.nn.functional.grid_sample`.
        device: the device on which the grid will be generated.
        dtype: the data type of the generated grid.

    Return:
        grid tensor with shape :math:`(1, D, H, W, 3)`.
    r   r   r   r   r   r   r   r   r      r   )
r   r   r   r	   r
   r   r   r   Zzsr   r   r   r   create_meshgrid3dC   s    r   )typingr   r   r   r   Zkornia.utils._compatr   r
   float32intboolr   r   r   r   r   r   r   <module>   s4   >