+
    
åûjÊÓ  ã                  ó  € R t ^ RIHt ^ RIt^ RIt^ RIHt ^ RIt^ RIH	t	 ^ RI
Ht ^ RIHt ^RIHt . R OtRtR	tR
 R ltR R ltR R ltR R ltR R ltR!R R lltR R ltR R ltR R ltR"R R lltR R ltR# )#aQ	  Box functions on plain corner tensors: build, measure, validate, mask, transform and NMS.

A 2D box is four ``(x, y)`` corners in clockwise order, top-left, top-right, bottom-right, bottom-left, so one box per
image is ``(B, 4, 2)``. A 3D box is those four corners at the front followed by the same four at the back, ``(B, 8, 3)``
with ``(x, y, z)`` corners. Corners are inclusive pixel centres: a box covering pixels ``0..9`` has corners at ``0`` and
``9`` and width ``x_max - x_min + 1 = 10``, unlike the exclusive boxes of torchvision, COCO and albumentations
(`#3934 <https://github.com/kornia/kornia/issues/3934>`_). The generators and the shape and mask functions share this
convention; :func:`nms` takes exclusive ``(N, 4)`` ``xyxy`` boxes instead
(`#4008 <https://github.com/kornia/kornia/issues/4008>`_), and :func:`transform_bbox` takes ``xyxy`` or ``xywh`` boxes,
or corners, and adds no ``+1``. From an unbatched :class:`~kornia.geometry.boxes.Boxes`, pass its
``to_tensor('vertices_plus')`` export; the ``'vertices'`` export reads one pixel larger
(`#4009 <https://github.com/kornia/kornia/issues/4009>`_). See
:doc:`Conventions & Pitfalls </get-started/conventions>` for the box formats of the rest of kornia.

Which function to use:

- Corners from a start corner and a size: :func:`bbox_generator` in 2D, :func:`bbox_generator3d` in 3D.
- The size of axis-aligned corners: :func:`infer_bbox_shape` returns heights and widths, :func:`infer_bbox_shape3d`
  depths, heights and widths.
- Whether corners are laid out as a box: :func:`validate_bbox` returns ``False`` when they are not,
  :func:`validate_bbox3d` raises ``AssertionError``.
- A filled mask per box: :func:`bbox_to_mask` returns ``(B, H, W)``, :func:`bbox_to_mask3d` ``(B, 1, D, H, W)``.
- Boxes moved by ``3 x 3`` matrices: :func:`transform_bbox` for ``xyxy`` or ``xywh`` tensors and corners;
  :meth:`~kornia.geometry.boxes.Boxes.transform_boxes` moves all four corners of a box container.
- The highest-scored of overlapping boxes: :func:`nms`, by IoU. The local maxima of a response map are
  :func:`~kornia.geometry.subpix.nms2d` instead.
- Boxes as objects with format conversions, several boxes per image, padding and filtering:
  :class:`~kornia.geometry.boxes.Boxes` and :class:`~kornia.geometry.boxes.Boxes3D`. The image patch inside each box:
  :func:`~kornia.geometry.transform.crop_and_resize`.
)ÚannotationsN)ÚOptional)Ú_emit_deprecation_warning©ÚKORNIA_CHECK_SHAPE)Úis_compiling)Útransform_pointszý`validate_bbox` returned False for an invalid box. Returning False is deprecated since kornia 0.9.1: a future release raises AssertionError instead, as `validate_bbox3d` does. To work with both, treat an AssertionError like a False result (kornia#4013).a  `validate_bbox3d` returned False for a non-finite coordinate. Returning False is deprecated since kornia 0.9.1: a future release raises AssertionError, as it already does for every other invalid box. To work with both, treat an AssertionError like a False result (kornia#4013).c               ó    € V ^8„  d   QhRRRR/# ©é   Úboxesútorch.TensorÚreturnÚbool© )Úformats   "ÚPC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\geometry\bbox.pyÚ__annotate__r   X   s   € ÷ Añ A˜ð A¨$ñ Aó    c                óN  € \        V P                  4      R9   d+   V P                  RR \        P                  ! ^^.4      8X  g|   \        P                  P                  4       '       gV   \        P                  P                  4       '       g2   \        4       '       g"   \        P                  ! \        \        ^R7       R# \        V P                  4      ^8X  d   V P                  R^^4      p V R	,          V R
,          r!V R,          V R,          rCV R,          V R,          reV R,          V R,          r‡W1,
          ^,           WW,
          ^,           r©WB,
          ^,           Wh,
          ^,           rË\        P                  ! \        P                  ! V 4      P                  4       ( \        P                   ! Wš,
          4      R8„  P#                  4       \        P                   ! W¼,
          4      R8„  P#                  4       .4      P%                  4       p\'        VP#                  4       4      '       d|   \        P                  P                  4       '       gV   \        P                  P                  4       '       g2   \        4       '       g"   \        P                  ! \        \        ^R7       R# R# )ag  Check that 2D box corners have equal top and bottom edge vectors, returning a bool.

``ok = validate_bbox(boxes)`` takes corners ``(B, 4, 2)`` or ``(B, N, 4, 2)`` and returns ``True`` when every
coordinate is finite and, in every box, the top edge (top-left to top-right) equals the bottom edge (bottom-left
to bottom-right) within ``1e-4`` on each axis. Otherwise it returns ``False``, without raising, and an eager call
warns with a ``FutureWarning``. It is a check of the corner layout only; for 3D corners, use
:func:`validate_bbox3d`, which raises instead.

Convention:
    Vertices are inclusive, in clockwise top-left, top-right, bottom-right, bottom-left order (see
    :func:`infer_bbox_shape`). Returns ``False`` for any other shape too. Right angles, positive area and
    direction are not checked, so any parallelogram in cyclic vertex order passes. An empty batch passes.

.. warning::
    Returning ``False`` is deprecated; a future release raises ``AssertionError``
    (`#4013 <https://github.com/kornia/kornia/issues/4013>`_).

Args:
    boxes: a tensor containing the coordinates of the bounding boxes to be extracted. The tensor must have the shape
        of ``(B, 4, 2)`` or ``(B, N, 4, 2)``, where each box is defined in the
        following ``clockwise`` order: top-left, top-right, bottom-right, bottom-left. The coordinates must be in
        the x, y order.

Returns:
    ``True`` for valid boxes, ``False`` otherwise.

See Also:
    - :func:`validate_bbox3d`: the check for ``(B, 8, 3)`` corners, which raises ``AssertionError`` instead of
      returning ``False``.
    - :func:`infer_bbox_shape`: the inclusive height and width of corners that pass.
    - :meth:`~kornia.geometry.boxes.Boxes.from_tensor`: imports boxes into a container, rejecting non-finite
      coordinates, and non-positive extents in the ``'xy*'`` modes, but not checking the corner layout.

N©Ú
stacklevelFg-Cëâ6?T©é   é   éþÿÿÿéÿÿÿÿ).é    r   ).r   é   ).r   r   ).r   r   ).r   r   ).r   r   ).r   r   ).r   r   )ÚlenÚshapeÚtorchÚSizeÚjitÚis_scriptingÚ
is_tracingr   ÚwarningsÚwarnÚ_VALIDATE_BBOX_FALSE_MESSAGEÚFutureWarningÚreshapeÚstackÚisfiniteÚallÚabsÚanyÚcpur   )r   Úx_tlÚy_tlÚx_trÚy_trÚx_brÚy_brÚx_blÚy_blÚwidth_tÚwidth_bÚheight_tÚheight_bÚflagss   &             r   Úvalidate_bboxr>   X   sË  € ôF �—‘Ó Ô&¨5¯;©;°r°sÐ+;¼u¿zºzÈ1ÈaÈ&Ó?QÔ+QÜ�y‰y×%Ñ%×'Ò'´·	±	×0DÑ0D×0FÒ0FÌ|Ï~Ê~Ü�MŠMÔ6¼ÐRSÕTÙä
ˆ5�;‰;Ó˜1ÔØ—‘˜b ! QÓ'ˆà�yÕ! 5¨Õ#3ˆ$Ø�yÕ! 5¨Õ#3ˆ$Ø�yÕ! 5¨Õ#3ˆ$Ø�yÕ! 5¨Õ#3ˆ$à•{ Q•¨­°a­ˆWØ� q�¨$­+¸­/ˆhô �KŠKä�^Š^˜EÓ"×&Ñ&Ó(Ð(Ü�YŠY�wÕ(Ó)¨DÑ0×5Ñ5Ó7Ü�YŠY�xÕ*Ó+¨dÑ2×7Ñ7Ó9ð	
ó÷ 
�cƒeð 
ô ˆE�I‰I‹K×ÒÜ�y‰y×%Ñ%×'Ò'´·	±	×0DÑ0D×0FÒ0FÌ|Ï~Ê~Ü�MŠMÔ6¼ÐRSÕTÙár   c               ó    € V ^8„  d   QhRRRR/# r
   r   )r   s   "r   r   r   œ   s   € ÷ *ñ *˜<ð *¨Dñ *r   c                ó  € \        V 4      '       d   R# \        P                  P                  4       '       gV   \        P                  P	                  4       '       g2   \        4       '       g"   \        P                  ! \        \        ^R7       R# )aU	  Check that 3D box corners measure one extent per axis, raising if not.

``ok = validate_bbox3d(boxes)`` takes corners ``(B, 8, 3)`` or ``(B, N, 8, 3)`` and returns ``True`` when, in every
box, the ``x`` extents of the four left-to-right edges agree, the ``y`` extents of the two diagonals of the front
face and of the back face agree, and the ``z`` extents of the four front-to-back edges agree, to the default
tolerances of :func:`torch.isclose`; for a box with right angles these are its edge extents. Another shape or a
disagreement raises ``AssertionError``; a non-finite coordinate returns ``False``, with a ``FutureWarning`` on an
eager call. For 2D corners, use :func:`validate_bbox`, which returns ``False`` instead of raising.

Convention:
    Vertices are inclusive, front-top-left, front-top-right, front-bottom-right, front-bottom-left, then the
    same four back vertices. Right angles and positive extent are not checked as such: a box whose right face is
    shifted in ``y`` raises, because its face diagonals then differ in ``y``, while one whose bottom face is
    shifted in ``x`` or ``z``, whose back face is shifted in ``x`` or ``y``, or whose right face is shifted in
    ``z`` passes. Under graph capture only the shape is checked.

.. warning::
    Returning ``False`` is deprecated; a future release raises ``AssertionError``
    (`#4013 <https://github.com/kornia/kornia/issues/4013>`_).

Args:
    boxes: a tensor containing the coordinates of the bounding boxes to be extracted. The tensor must have the shape
        of ``(B, 8, 3)`` or ``(B, N, 8, 3)``, where each box is defined in the following ``clockwise``
        order: front-top-left, front-top-right, front-bottom-right, front-bottom-left, back-top-left,
        back-top-right, back-bottom-right, back-bottom-left. The coordinates must be in the x, y, z order.

Returns:
    ``True``, or ``False`` when any coordinate is non-finite. Invalid input otherwise raises instead of
    returning ``False``.

See Also:
    - :func:`validate_bbox`: the check for ``(B, 4, 2)`` corners, which returns ``False`` instead of raising.
    - :func:`infer_bbox_shape3d`: runs this check, then returns the depths, heights and widths of the corners.
    - :meth:`~kornia.geometry.boxes.Boxes3D.from_tensor`: imports ``(N, 6)`` boxes into a container, rejecting
      non-finite coordinates and non-positive extents.

Tr   F)
Ú_validate_bbox3dr!   r#   r$   r%   r   r&   r'   Ú_VALIDATE_BBOX3D_FALSE_MESSAGEr)   )r   s   &r   Úvalidate_bbox3drC   œ   sS   € ôL ˜×ÒÙÜ�9‰9×!Ñ!×#Ò#¬E¯I©I×,@Ñ,@×,BÒ,BÌ<Ï>Ê>Ü�ŠÔ4´mÐPQÕRÙr   c               ó    € V ^8„  d   QhRRRR/# r
   r   )r   s   "r   r   r   É   s   € ÷ )ñ )˜Lð )¨Tñ )r   c                ó"  € \        V P                  4      R	9   d+   V P                  R
R \        P                  ! ^^.4      8X  g   \	        RV P                   R24      h\        V P                  4      ^8X  d   V P                  R^^4      p \        P                  P                  4       '       g   \        4       '       d   R# V R,          V R,          r!\        P                  ! VR,          VR,          ,
          VR,          VR,          ,
          .^4      ^,           p\        P                  ! VR,          VR,          ,
          VR,          VR,          ,
          .^4      ^,           pV R,          V R,          ,
          ^,           p\        P                  ! \        P                  ! V 4      P                  4       ( \        P                  ! VP                  ^^ 4      VR,          4      P                  4       ( \        P                  ! VP                  ^^ 4      VR,          4      P                  4       ( \        P                  ! VP                  ^^ 4      VR,          4      P                  4       ( .4      P                  4       p\!        V^ ,          4      '       d   R# \!        V^,          4      '       d   \	        RV R24      h\!        V^,          4      '       d   \	        RV R24      h\!        V^,          4      '       d   \	        RV R24      hR# )r   Nz1Box shape must be (B, 8, 3) or (B, N, 8, 3). Got Ú.TFz4Boxes must have be cube, while get different widths z5Boxes must have be cube, while get different heights z4Boxes must have be cube, while get different depths r   r   r   ©.r   ©.r   )ºNNN:r   r   N)rI   :r   r   r   )rI   :é   é   N)rI   :r   é   r   )rI   :r   r   N)rI   :r   r   N)rI   :é   rL   N)rI   :r   rM   N©rI   :r   NNr   ©rI   :Nr   Nr   ©rI   r   )r   r    r!   r"   ÚAssertionErrorr*   r#   r$   r   Úcatr+   r,   r-   ÚiscloseÚpermuter0   r   )r   ÚxÚyÚwidthsÚheightsÚdepthsr=   s   &      r   rA   rA   É   s\  € ô �—‘Ó Ô&¨5¯;©;°r°sÐ+;¼u¿zºzÈ1ÈaÈ&Ó?QÔ+QÜÐPÐQV×Q\ÑQ\ÐP]Ð]^Ð_Ó`Ð`ä
ˆ5�;‰;Ó˜1ÔØ—‘˜b ! QÓ'ˆô �9‰9×!Ñ!×#Ò#¬¯ªÙð ��=˜% �-€qÜ�YŠY˜˜&�	 A h¥KÕ/°°6µ¸Q¸x½[Õ1HÐIÈ1ÓMÐPQÕQ€FÜ�iŠi˜˜6� Q v¥YÕ.°°&µ	¸A¸f½IÕ0EÐFÈÓJÈQÕN€GØ�8�_˜u X�Õ.°Õ2€Fô
 �KŠKä�^Š^˜EÓ"×&Ñ&Ó(Ð(Ü�]Š]˜6Ÿ>™>¨!¨QÓ/°¸µÓ>×BÑBÓDÐDÜ�]Š]˜7Ÿ?™?¨1¨aÓ0°'¸$µ-Ó@×DÑDÓFÐFÜ�]Š]˜6Ÿ>™>¨!¨QÓ/°¸µÓ>×BÑBÓDÐDð		
ó÷ 
�cƒeð 
ô ˆE�!�H‡~‚~ÙÜˆE�!�H‡~‚~ÜÐSÐTZÐS[Ð[\Ð]Ó^Ð^ÜˆE�!�H‡~‚~ÜÐTÐU\ÐT]Ð]^Ð_Ó`Ð`ÜˆE�!�H‡~‚~ÜÐSÐTZÐS[Ð[\Ð]Ó^Ð^ár   c               ó    € V ^8„  d   QhRRRR/# )r   r   r   r   z!tuple[torch.Tensor, torch.Tensor]r   )r   s   "r   r   r   õ   s   € ÷ >ñ >˜Lð >Ð-Nñ >r   c                óœ   € \        V . RO4       V R,          V R,          ,
          ^,           pV R,          V R,          ,
          ^,           pW!3# )a
  Read the inclusive heights and widths of (N, 4, 2) box corners from fixed vertices.

``heights, widths = infer_bbox_shape(boxes)`` takes ``(N, 4, 2)`` corners and returns two ``(N,)`` tensors in the
dtype of ``boxes``, the inclusive heights ``y_br - y_tl + 1`` and widths ``x_tr - x_tl + 1`` read from the
top-left, top-right and bottom-right vertices. It does not validate the corners. For ``(B, N, 4, 2)`` corners,
flatten them to ``(B * N, 4, 2)`` first; for corners in any order, such as a rotated box, use
:meth:`~kornia.geometry.boxes.Boxes.get_boxes_shape`, which measures the axis-aligned bounds; for 3D corners,
:func:`infer_bbox_shape3d`.

Convention:
    Vertices are **inclusive**: a box covering pixels ``0..9`` has corners at ``0`` and ``9`` and width
    ``10``. The vertex-based helpers of this module share it; :func:`nms` takes exclusive ``xyxy`` and
    :func:`transform_bbox` converts ``'xywh'`` exclusively (``xmax = xmin + width``).
    The order is clockwise top-left, top-right, bottom-right, bottom-left. The result is
    ``(heights, widths)``, read from fixed vertex indices, ``width = boxes[:, 1, 0] - boxes[:, 0, 0] + 1`` and
    ``height = boxes[:, 2, 1] - boxes[:, 0, 1] + 1``, not from a ``max - min`` reduction, so a box in
    another vertex order (such as a rotated polygon from :func:`transform_bbox`) can give a negative extent.
    :meth:`kornia.geometry.boxes.Boxes.get_boxes_shape` is reduction based.

.. warning::
    The inclusive ``+1`` arithmetic differs from torchvision, COCO and albumentations, which are exclusive:
    `#3934 <https://github.com/kornia/kornia/issues/3934>`_.

Args:
    boxes: a tensor containing the coordinates of the bounding boxes to be extracted. The tensor must have shape
        ``(N, 4, 2)``, where each box is defined in the following ``clockwise`` order: top-left, top-right,
        bottom-right, bottom-left. The coordinates must be in the x, y order.

Raises:
    ShapeError: if ``boxes`` does not have shape ``(N, 4, 2)``.

Returns:
    - Bounding box heights, shape of ``(N,)``.
    - Bounding box widths, shape of ``(N,)``.

See Also:
    - :func:`infer_bbox_shape3d`: the depths, heights and widths of ``(B, 8, 3)`` corners, which it validates first.
    - :meth:`~kornia.geometry.boxes.Boxes.get_boxes_shape`: the inclusive size of the axis-aligned bounds of the
      boxes in a container, for corners in any order.
    - :func:`bbox_generator`: the inverse, corners from a start corner and this height and width.

Example:
    >>> boxes = torch.tensor([[
    ...     [1., 1.],
    ...     [2., 1.],
    ...     [2., 2.],
    ...     [1., 2.],
    ... ], [
    ...     [1., 1.],
    ...     [3., 1.],
    ...     [3., 2.],
    ...     [1., 2.],
    ... ]])  # 2x4x2
    >>> infer_bbox_shape(boxes)
    (tensor([2., 2.]), tensor([2., 3.]))

)ÚNÚ4Ú2©rI   r   r   ©rI   r   r   ©rI   r   r   ©rI   r   r   r   )r   ÚwidthÚheights   &  r   Úinfer_bbox_shapere   õ   sE   € ôt �ušoÔ.à �.¨5°­>Õ9¸AÕ=€EØ  �>¨E°'­NÕ:¸QÕ>€FØˆ=Ðr   c               ó    € V ^8„  d   QhRRRR/# )r   r   r   r   z/tuple[torch.Tensor, torch.Tensor, torch.Tensor]r   )r   s   "r   r   r   6  s   € ÷ R#ñ R#˜lð R#Ð/^ñ R#r   c           
     ó€  € \        V . RO4       \        V 4      '       g   \        R4      h\        P                  ! V ^\        P
                  ! . ROV P                  \        P                  R7      4      R,          p\        P                  ! V ^\        P
                  ! . ROV P                  \        P                  R7      4      R,          pW,
          ^,           R,          p\        P                  ! V ^\        P
                  ! . ROV P                  \        P                  R7      4      R	,          p\        P                  ! V ^\        P
                  ! . R
OV P                  \        P                  R7      4      R	,          pWE,
          ^,           R,          pV R,          V R,          ,
          ^,           R,          pWvV3# )au  Read the inclusive depths, heights and widths of (B, 8, 3) box corners, validating them.

``depths, heights, widths = infer_bbox_shape3d(boxes)`` takes ``(B, 8, 3)`` corners, four at the front and then
four at the back, and returns three ``(B,)`` tensors in the dtype of ``boxes``, the inclusive extent of each box
along ``z``, ``y`` and ``x``. It first checks the corners as :func:`validate_bbox3d` does and raises
``AssertionError`` where that check finds extents that disagree, such as for a box whose right face is shifted in
``y``, or a non-finite coordinate; another shape than ``(B, 8, 3)`` raises
:class:`~kornia.core.exceptions.ShapeError`. For ``(N, 4, 2)`` corners, use :func:`infer_bbox_shape`, which does
not validate.

Convention:
    Vertices are inclusive (see :func:`infer_bbox_shape`), in :func:`validate_bbox3d` order. The result is
    ``(depths, heights, widths)``, each read from fixed vertex indices like :func:`infer_bbox_shape` (for
    example ``width = boxes[:, 1, 0] - boxes[:, 0, 0] + 1``), so another vertex order can give a negative
    extent. Pass the ``'vertices_plus'``
    export of :class:`~kornia.geometry.boxes.Boxes3D`; ``'vertices'`` reads one larger per axis.
    ``(B, N, 8, 3)`` input raises :class:`~kornia.core.exceptions.ShapeError`; flatten it first.

.. warning::
    The ``+1`` is `#3934 <https://github.com/kornia/kornia/issues/3934>`_ and the exclusive-export trap
    `#4009 <https://github.com/kornia/kornia/issues/4009>`_. An invalid box, including a non-finite
    coordinate, raises ``AssertionError`` rather than being reported:
    `#4013 <https://github.com/kornia/kornia/issues/4013>`_.

Args:
    boxes: a tensor containing the coordinates of the bounding boxes to be extracted. The tensor must have the shape
        of ``(B, 8, 3)``, where each box is defined in the following ``clockwise`` order: front-top-left,
        front-top-right, front-bottom-right, front-bottom-left, back-top-left, back-top-right, back-bottom-right,
        back-bottom-left. The coordinates must be in the x, y, z order.

Returns:
    - Bounding box depths, shape of ``(B,)``.
    - Bounding box heights, shape of ``(B,)``.
    - Bounding box widths, shape of ``(B,)``.

See Also:
    - :func:`infer_bbox_shape`: the heights and widths of ``(N, 4, 2)`` corners, without validation.
    - :meth:`~kornia.geometry.boxes.Boxes3D.get_boxes_shape`: the same three sizes for the boxes of a container,
      from the axis-aligned bounds of the corners.
    - :func:`bbox_generator3d`: the inverse, corners from a start corner and these sizes.

Example:
    >>> boxes = torch.tensor([[[ 0,  1,  2],
    ...         [10,  1,  2],
    ...         [10, 21,  2],
    ...         [ 0, 21,  2],
    ...         [ 0,  1, 32],
    ...         [10,  1, 32],
    ...         [10, 21, 32],
    ...         [ 0, 21, 32]],
    ...        [[ 3,  4,  5],
    ...         [43,  4,  5],
    ...         [43, 54,  5],
    ...         [ 3, 54,  5],
    ...         [ 3,  4, 65],
    ...         [43,  4, 65],
    ...         [43, 54, 65],
    ...         [ 3, 54, 65]]]) # 2x8x3
    >>> infer_bbox_shape3d(boxes)
    (tensor([31, 61]), tensor([21, 51]), tensor([11, 41]))

ú:Boxes must have finite coordinates, got non-finite values.©ÚdeviceÚdtype)r\   Ú8Ú3)r   r   rJ   rM   ©rI   rI   r   )r   r   r   rK   rP   )r   r   rM   rK   ©rI   rI   r   )r   r   r   rJ   rN   rO   )r   rA   rQ   r!   Úindex_selectÚtensorrj   Úlong)r   ÚleftÚrightrW   ÚbotÚupperrX   rY   s   &       r   Úinfer_bbox_shape3drw   6  s8  € ôF �ušoÔ.ô ˜E×"Ò"ÜÐYÓZÐZä×Ò˜e Q¬¯ª²\È%Ï,É,Ô^c×^hÑ^hÔ(iÓjÐkrÕs€DÜ×Ò˜u a¬¯ª²lÈ5Ï<É<Ô_d×_iÑ_iÔ)jÓkÐlsÕt€EØ�l˜QÕ Õ%€Fä
×
Ò
˜U A¤u§|¢|²LÈÏÉÔ]b×]gÑ]gÔ'hÓ
iÐjqÕ
r€CÜ×Ò˜u a¬¯ª²lÈ5Ï<É<Ô_d×_iÑ_iÔ)jÓkÐlsÕt€EØ�{˜Q� Õ%€Gà�H�o  h¥Õ/°!Õ3°TÕ:€FØ˜FÐ"Ð"r   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   r   r   rc   zOptional[int]rd   ÚsizezOptional[tuple[int, int]]r   r   )r   s   "r   r   r   ‹  sA   € ÷ w ñ w Øðw àðw ð ðw ð $ð	w ð
 ñw r   c                ó0  € Ve   Vf   Ve   \        R4      hVw  rEMHVe:   Ve6   \        P                  P                  4       '       g   \	        RRRR4       Y!rTM\        R4      h\        V . RO4       \        P                  ! W@P                  \        P                  R7      P                  V^4      p\        P                  ! WPP                  \        P                  R7      P                  ^V4      pV R	,          P                  R
^^4      pV R,          P                  R
^^4      p	V R,          P                  R
^^4      p
V R,          P                  R
^^4      pWx8¬  Wz8*  ,          pWi8¬  Wk8*  ,          pWÍ,          pVP                  V P                  4      # )aS  Draw (B, 4, 2) inclusive box corners as filled (B, H, W) masks.

``mask = bbox_to_mask(boxes, size=(H, W))`` takes one box per image, ``(B, 4, 2)`` corners, and returns
``(B, H, W)`` masks in the dtype of ``boxes``: ``1`` on each pixel ``(x, y)`` with ``xmin <= x <= xmax`` and
``ymin <= y <= ymax``, read from the top-left and bottom-right corners, and ``0`` elsewhere, with no gradient path.
The bounds are compared inclusively and unrounded, and a box partly outside the image fills only its part inside.
For several boxes per image, use :meth:`~kornia.geometry.boxes.Boxes.to_mask`, which returns ``(N, H, W)`` for
unbatched boxes and ``(B, N, H, W)`` for batched ones; for volumes, :func:`bbox_to_mask3d`.

Convention:
    The size is ``size=(height, width)``, as for :func:`bbox_to_mask3d` and
    :meth:`~kornia.geometry.boxes.Boxes.to_mask`; the deprecated ``width, height`` arguments come in the
    opposite order. Only vertices 0 (top-left) and 2 (bottom-right) are read. Pass the ``'vertices_plus'`` export
    of :class:`~kornia.geometry.boxes.Boxes`. ``(B, N, 4, 2)`` raises :class:`~kornia.core.exceptions.ShapeError`.

.. warning::
    The ``(width, height)`` form is deprecated in favour of ``size=(height, width)``:
    `#4014 <https://github.com/kornia/kornia/issues/4014>`_. For fractional
    coordinates the raw comparison differs from the rounding of :meth:`~kornia.geometry.boxes.Boxes.to_mask`
    and the truncation of :func:`bbox_to_mask3d`: `#4015 <https://github.com/kornia/kornia/issues/4015>`_.
    The exclusive-export trap is `#4009 <https://github.com/kornia/kornia/issues/4009>`_.

Args:
    boxes: a tensor containing the coordinates of the bounding boxes to be extracted. The tensor must have the shape
        of ``(B, 4, 2)``, where each box is defined in the following ``clockwise`` order: top-left, top-right,
        bottom-right and bottom-left. The coordinates must be in the x, y order.
    width: deprecated since 0.9.1; pass ``size=(height, width)`` instead. Width of the masked image.
    height: deprecated since 0.9.1; pass ``size=(height, width)`` instead. Height of the masked image.
    size: (height, width) of the masked image.

Returns:
    the output mask tensor, shape of ``(B, height, width)`` and dtype of ``boxes``.

Raises:
    ShapeError: if ``boxes`` does not have shape ``(B, 4, 2)``.
    ValueError: if ``size`` is passed together with ``width`` or ``height``.
    TypeError: if neither ``size`` nor both ``width`` and ``height`` are given.

See Also:
    - :func:`bbox_to_mask3d`: the 3D form, ``(B, 1, D, H, W)`` masks of ``(B, 8, 3)`` corners, whose bounds are
      truncated toward zero.
    - :meth:`~kornia.geometry.boxes.Boxes.to_mask`: masks of several boxes per image, ``(N, H, W)`` for unbatched
      boxes and ``(B, N, H, W)`` for batched ones, filled over the rounded exclusive bounds.
    - :func:`infer_bbox_shape`: the inclusive height and width of the same corners.

Note:
    The pixel-position grid is built in ``float32`` regardless of ``boxes.dtype``, so
    ``float16``/``bfloat16`` boxes are masked exactly even on images wider or taller than
    the integer-exact range of those dtypes (2048 px for ``float16``, 256 px for
    ``bfloat16``). The result is still returned in ``boxes.dtype``.

Examples:
    >>> boxes = torch.tensor([[
    ...        [1., 1.],
    ...        [3., 1.],
    ...        [3., 2.],
    ...        [1., 2.],
    ...   ]])  # 1x4x2
    >>> bbox_to_mask(boxes, size=(5, 5))
    tensor([[[0., 0., 0., 0., 0.],
             [0., 1., 1., 1., 0.],
             [0., 1., 1., 1., 0.],
             [0., 0., 0., 0., 0.],
             [0., 0., 0., 0., 0.]]])

zNPass either size=(height, width) or the deprecated width and height, not both.z"bbox_to_mask(boxes, width, height)z)bbox_to_mask(boxes, size=(height, width))z0.9.1z¡The `width` and `height` arguments will be removed in a future release; note that `size` is `(height, width)`, the order of `bbox_to_mask3d` and `Boxes.to_mask`.z@bbox_to_mask() missing required argument 'size' (height, width).ri   )ÚBr]   r^   r`   r   rb   ©rI   r   r   ra   )Ú
ValueErrorr!   r#   r$   r   Ú	TypeErrorr   Úarangerj   Úfloat32ÚviewÚtork   )r   rc   rd   ry   Úmask_heightÚ
mask_widthÚyyÚxxÚx_minÚy_minÚx_maxÚy_maxÚx_inÚy_inÚmasks   &&&&           r   Úbbox_to_maskrŽ   ‹  sf  € ðV ÒØÒ Ò 2ÜÐmÓnÐnØ"&Ñˆ�ZØ	Ò	˜vÒ1Ü�y‰y×%Ñ%×'Ò'Ü%Ø4Ø;ØðXô	ð #)‘ZäÐZÓ[Ð[ä�ušoÔ.ô 
�Š�k¯,©,¼e¿m¹mÔ	L×	QÑ	QÐR]Ð_`Ó	a€BÜ	�Š�j¯©¼U¿]¹]Ô	K×	PÑ	PÐQRÐT^Ó	_€BØ�'�N×Ñ  A qÓ)€EØ�'�N×Ñ  A qÓ)€EØ�'�N×Ñ  A qÓ)€EØ�'�N×Ñ  A qÓ)€Eð ‰K˜B™KÕ(€DØ‰K˜B™KÕ(€DØ�;€DØ�7‰7�5—;‘;ÓÐr   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r   r   ry   ztuple[int, int, int]r   r   )r   s   "r   r   r     s'   € ÷ oñ o˜,ð oÐ.Bð oÀ|ñ or   c                ó
  € \        V . RO4       \        V 4      '       g   \        R4      hVw  r#pV R,          P                  4       pV R,          P                  4       pV R,          P                  4       pV R,          P                  4       pV R,          P                  4       p	V R	,          P                  4       p
\        P
                  ! W P                  \        P                  R7      p\        P
                  ! W0P                  \        P                  R7      p\        P
                  ! W@P                  \        P                  R7      pVR
,          VR,          8¬  VR
,          VR,          8*  ,          R,          VR
,          VR,          8¬  VR
,          VR,          8*  ,          R,          ,          VR
,          V	R,          8¬  VR
,          V
R,          8*  ,          R,          ,          pVP                  V P                  4      # )a—  Draw (B, 8, 3) inclusive 3D box corners as filled (B, 1, D, H, W) masks.

``mask = bbox_to_mask3d(boxes, (D, H, W))`` takes one box per volume, ``(B, 8, 3)`` corners, and returns
``(B, 1, D, H, W)`` masks in the dtype of ``boxes``: ``1`` on the voxels inside each box and ``0`` elsewhere, with
no gradient path. It first checks the corners as :func:`validate_bbox3d` does and raises ``AssertionError`` where
that check finds extents that disagree, or for a non-finite coordinate. For several boxes per volume, use
:meth:`~kornia.geometry.boxes.Boxes3D.to_mask`; for images, :func:`bbox_to_mask`.

Convention:
    ``size`` is ``(depth, height, width)`` and the mask ``(B, 1, depth, height, width)`` in the input
    dtype, with a channel axis that :func:`bbox_to_mask` does not have
    (:meth:`kornia.geometry.boxes.Boxes3D.to_mask` returns ``(N, depth, height, width)`` for unbatched boxes and
    ``(B, N, depth, height, width)`` for batched ones). The bounds are truncated toward zero and compared
    inclusively: pass the ``'vertices_plus'`` export. ``(B, N, 8, 3)`` input raises
    :class:`~kornia.core.exceptions.ShapeError`.

    The bounds come from fixed vertices, ``x`` from the first two, ``y`` from the second and third and ``z``
    from the first and fifth, and fill axis-aligned ranges. A sheared box that :func:`validate_bbox3d` accepts,
    such as one whose back face is shifted in ``x``, is masked as the axis-aligned box of those vertices, not
    rasterized.

.. warning::
    For fractional coordinates the truncation differs from :func:`bbox_to_mask` and
    :meth:`~kornia.geometry.boxes.Boxes3D.to_mask`: `#4015 <https://github.com/kornia/kornia/issues/4015>`_.
    An invalid box, including a non-finite coordinate, raises ``AssertionError``:
    `#4013 <https://github.com/kornia/kornia/issues/4013>`_.

Args:
    boxes: a tensor containing the coordinates of the bounding boxes to be extracted. The tensor must have the shape
        of ``(B, 8, 3)``, where each box is defined in the following ``clockwise`` order: front-top-left,
        front-top-right, front-bottom-right, front-bottom-left, back-top-left, back-top-right, back-bottom-right,
        back-bottom-left. The coordinates must be in the x, y, z order.
    size: depth, height and width of the masked image.

Returns:
        the output mask tensor, shape of ``(B, 1, depth, height, width)`` and dtype of ``boxes``.

See Also:
    - :func:`bbox_to_mask`: the 2D form, ``(B, H, W)`` masks of ``(B, 4, 2)`` corners with no channel axis, whose
      bounds are compared unrounded.
    - :meth:`~kornia.geometry.boxes.Boxes3D.to_mask`: masks of several boxes per volume, ``(N, D, H, W)`` for
      unbatched boxes and ``(B, N, D, H, W)`` for batched ones, filled over the rounded exclusive bounds.
    - :func:`infer_bbox_shape3d`: the inclusive depth, height and width of the same corners.

Examples:
    >>> boxes = torch.tensor([[
    ...     [1., 1., 1.],
    ...     [2., 1., 1.],
    ...     [2., 2., 1.],
    ...     [1., 2., 1.],
    ...     [1., 1., 2.],
    ...     [2., 1., 2.],
    ...     [2., 2., 2.],
    ...     [1., 2., 2.],
    ... ]])  # 1x8x3
    >>> bbox_to_mask3d(boxes, (4, 5, 5))
    tensor([[[[[0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.]],
    <BLANKLINE>
              [[0., 0., 0., 0., 0.],
               [0., 1., 1., 0., 0.],
               [0., 1., 1., 0., 0.],
               [0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.]],
    <BLANKLINE>
              [[0., 0., 0., 0., 0.],
               [0., 1., 1., 0., 0.],
               [0., 1., 1., 0., 0.],
               [0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.]],
    <BLANKLINE>
              [[0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0.]]]]])

rh   ri   )r{   rl   rm   )rI   r   r   )rI   r   r   )rI   r   r   ra   r`   r_   )NrI   )rI   N)rI   NrI   NN)rI   NNrI   N)rI   NNNrI   )	r   rA   rQ   rr   r!   r   rj   r‚   rk   )r   ry   ÚD0ÚD1ÚD2Úz_minÚz_maxrˆ   rŠ   r‡   r‰   ÚzrV   rU   Úms   &&             r   Úbbox_to_mask3dr˜     sƒ  € ôh �ušoÔ.Ü˜E×"Ò"ÜÐYÓZÐZØ�J€BˆBà�'�N×ÑÓ!€EØ�'�N×ÑÓ!€EØ�'�N×ÑÓ!€EØ�'�N×ÑÓ!€EØ�'�N×ÑÓ!€EØ�'�N×ÑÓ!€Eä�Š�R§¡´E·J±JÔ?€AÜ�Š�R§¡´E·J±JÔ?€AÜ�Š�R§¡´E·J±JÔ?€Að ˆG�*˜˜g�Ñ
&¨1¨W­:¸¸w½Ñ+GÕ	HÐJ`ÕaØˆg�J˜% �.Ñ(¨Q¨w­Z¸5À½>Ñ-IÕJÐLbÕ
cõ	dàˆg�J˜% �.Ñ(¨Q¨w­Z¸5À½>Ñ-IÕJÐLbÕ
cõ	dð ð
 �4‰4�—‘ÓÐr   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )r   Úx_startr   Úy_startrc   rd   r   r   )r   s   "r   r   r   w  s;   € ÷ Uñ UØðUØ$0ðUØ9EðUØO[ðUàñUr   c                óº  € V P                   VP                   8X  d   V P                  4       R9   g   \        RV  RV R24      hVP                   VP                   8X  d   VP                  4       R9   g   \        RV RV R24      hV P                  VP                  u;8X  d$   VP                  u;8X  d   VP                  8X  gC   M \        RV P                   RV P                   RVP                   RVP                   R	2	4      hV P                  VP                  u;8X  d$   VP                  u;8X  d   VP                  8X  gC   M \        R
V P                   RV P                   RVP                   RVP                   R	2	4      hV P                  R4      pVP                  R4      pWBP                  R4      ,           ^,
          pWSP                  R4      ,           ^,
          p\        P                  ! \        P                  ! WE.RR7      \        P                  ! We.RR7      \        P                  ! Wg.RR7      \        P                  ! WG.RR7      .RR7      # )aé	  Build (B, 4, 2) inclusive box corners from top-left starts, widths and heights.

``boxes = bbox_generator(x_start, y_start, width, height)`` takes ``(B,)`` or scalar tensors and returns
``(B, 4, 2)`` corners in clockwise order from the top-left one, with the far corner at ``start + size - 1``, in
the dtype of the inputs and with a gradient path to them. These are the corners
:meth:`~kornia.geometry.boxes.Boxes.from_tensor` stores for the same boxes in ``'xywh'`` mode, which also
validates them and wraps them in a container; to crop the boxes from images, pass the result to
:func:`~kornia.geometry.transform.crop_and_resize`. For 3D boxes, use :func:`bbox_generator3d`.

Convention:
    The far corner is at ``start + size - 1`` (inclusive, see :func:`infer_bbox_shape`), so
    :func:`infer_bbox_shape` reads back ``width`` and ``height``; a zero size puts the far corner one before
    the start. Scalar inputs give a batch of one, and scalar sizes broadcast against ``(B,)`` starts; scalar
    starts with ``(B,)`` sizes, ``B > 1``, raise ``RuntimeError``. The four tensors must share dtype and device,
    otherwise ``AssertionError``.

Args:
    x_start: a tensor containing the x coordinates of the bounding boxes to be extracted. Shape must be a scalar
        tensor or ``(B,)``.
    y_start: a tensor containing the y coordinates of the bounding boxes to be extracted. Shape must be a scalar
        tensor or ``(B,)``.
    width: widths of the bounding boxes. Shape must be a scalar tensor or ``(B,)``.
    height: heights of the bounding boxes. Shape must be a scalar tensor or ``(B,)``.

Returns:
    the bounding box tensor, shape of ``(B, 4, 2)``.

See Also:
    - :func:`bbox_generator3d`: the 3D form, ``(B, 8, 3)`` corners from a start corner and three sizes.
    - :func:`infer_bbox_shape`: the inverse, the height and width of the corners.
    - :meth:`~kornia.geometry.boxes.Boxes.from_tensor`: the same corners from ``'xywh'`` boxes, validated and kept
      in a container with format conversions.
    - :func:`~kornia.geometry.transform.crop_and_resize`: crops one such box per image and resizes it.

Examples:
    >>> x_start = torch.tensor([0, 1])
    >>> y_start = torch.tensor([1, 0])
    >>> width = torch.tensor([5, 3])
    >>> height = torch.tensor([7, 4])
    >>> bbox_generator(x_start, y_start, width, height)
    tensor([[[0, 1],
             [4, 1],
             [4, 7],
             [0, 7]],
    <BLANKLINE>
            [[1, 0],
             [3, 0],
             [3, 3],
             [1, 3]]])

z6`x_start` and `y_start` must be a scalar or (B,). Got ú, rF   z3`width` and `height` must be a scalar or (B,). Got ú5All tensors must be in the same dtype. Got `x_start`(ú), `y_start`(ú), `width`(ú), `height`(ú).ú6All tensors must be in the same device. Got `x_start`(©Údim©r   r   r   r   )r    r¥   rQ   rk   rj   r�   r!   r+   )rš   r›   rc   rd   Úx0Úy0Úx1Úy1s   &&&&    r   Úbbox_generatorr«   w  sù  € ðl �M‰M˜WŸ]™]Ô*¨w¯{©{«}ÀÔ/FÜÐUÐV]ÐU^Ð^`ÐahÐ`iÐijÐkÓlÐlØ�K‰K˜6Ÿ<™<Ô'¨E¯I©I«K¸6Ô,AÜÐRÐSXÐRYÐY[Ð\bÐ[cÐcdÐeÓfÐfØ�=‰=˜GŸM™MÖH¨U¯[©[ÖH¸F¿L¹LÖHÜðØ Ÿ™˜ }°W·]±]°OÀ;ÈuÏ{É{ÈmÐ[gÐhn×htÑhtÐguÐuwðyó
ð 	
ð �>‰>˜WŸ^™^ÖL¨u¯|©|ÖL¸v¿}¹}ÖLÜðØ Ÿ™Ð(¨°g·n±nÐ5Eð FØ—|‘|�n L°·±°¸rðCó
ð 	
ð 
�‰�bÓ	€BØ	�‰�bÓ	€BØ	�j‰j˜‹nÕ	˜qÕ	 €BØ	�k‰k˜"‹oÕ	 Õ	!€BÜ�;Š;ä�KŠK˜˜ bÔ)Ü�KŠK˜˜ bÔ)Ü�KŠK˜˜ bÔ)Ü�KŠK˜˜ bÔ)ð		
ð ôð r   c               ó4   € V ^8„  d   QhRRRRRRRRRRRRRR/# )	r   rš   r   r›   Úz_startrc   rd   Údepthr   r   )r   s   "r   r   r   Ï  sW   € ÷ j/ñ j/Øðj/àðj/ð ðj/ð ð	j/ð
 ðj/ð ðj/ð ñj/r   c                óâ  € V P                   VP                   u;8X  d   VP                   8X  d   M MV P                  4       R9   g   \        RV  RV RV R24      hVP                   VP                   u;8X  d   VP                   8X  d   M MVP                  4       R9   g   \        RV RV RV R24      hV P                  VP                  u;8X  dH   VP                  u;8X  d6   VP                  u;8X  d$   VP                  u;8X  d   VP                  8X  g]   M \        RV P                   RV P                   RV P                   RVP                   R	VP                   R
VP                   R24      hV P                  VP                  u;8X  dH   VP                  u;8X  d6   VP                  u;8X  d$   VP                  u;8X  d   VP                  8X  g]   M \        RV P                   RV P                   RV P                   RVP                   R	VP                   R
VP                   R24      h\
        P                  ! V P                  4       ^^3V P                  V P                  R7      pVR;;,          V P                  R^4      ,          uu&   VR;;,          VP                  R^4      ,          uu&   VR;;,          VP                  R^4      ,          uu&   VR;;,          VP                  R4      ^,
          ,          uu&   VR;;,          VP                  R4      ^,
          ,          uu&   VR;;,          VP                  R4      ^,
          ,          uu&   VR;;,          VP                  R4      ^,
          ,          uu&   VP                  4       pVR;;,          VP                  R^4      ^,
          P                  R^4      ,          uu&   \
        P                  ! Wg.^R7      # )a6  Build (B, 8, 3) inclusive 3D box corners from start corners and sizes.

``boxes = bbox_generator3d(x_start, y_start, z_start, width, height, depth)`` takes ``(B,)`` or scalar tensors
and returns ``(B, 8, 3)`` corners: four at the front, at ``z_start``, in clockwise order from the top-left one,
then the same four at the back, at ``z_start + depth - 1``. They are in the dtype of the inputs, with a gradient
path to them. These are the corners :meth:`~kornia.geometry.boxes.Boxes3D.from_tensor` stores for the same boxes
in ``'xyzwhd'`` mode. For 2D boxes, use :func:`bbox_generator`.

Convention:
    The four front vertices precede the four back vertices. Scalar inputs give a batch of one, and scalar sizes
    broadcast against ``(B,)`` starts; scalar starts with ``(B,)`` sizes, ``B > 1``, raise ``RuntimeError``. The
    six tensors must share dtype and device, otherwise ``AssertionError``.

    The far corner is at ``start + size - 1`` (inclusive, see :func:`infer_bbox_shape3d`), so
    :func:`infer_bbox_shape3d` reads back ``size`` on every axis, matching :func:`bbox_generator`.

Args:
    x_start: a tensor containing the x coordinates of the bounding boxes to be extracted. Shape must be a scalar
        tensor or ``(B,)``.
    y_start: a tensor containing the y coordinates of the bounding boxes to be extracted. Shape must be a scalar
        tensor or ``(B,)``.
    z_start: a tensor containing the z coordinates of the bounding boxes to be extracted. Shape must be a scalar
        tensor or ``(B,)``.
    width: widths of the bounding boxes. Shape must be a scalar tensor or ``(B,)``.
    height: heights of the bounding boxes. Shape must be a scalar tensor or ``(B,)``.
    depth: depths of the bounding boxes. Shape must be a scalar tensor or ``(B,)``.

Returns:
    the 3d bounding box tensor ``(B, 8, 3)``.

See Also:
    - :func:`bbox_generator`: the 2D form, ``(B, 4, 2)`` corners from a start corner, a width and a height.
    - :func:`infer_bbox_shape3d`: the inverse, the depth, height and width of the corners.
    - :meth:`~kornia.geometry.boxes.Boxes3D.from_tensor`: the same corners from ``'xyzwhd'`` boxes, validated and
      kept in a container with format conversions.
    - :func:`~kornia.geometry.transform.crop_and_resize3d`: crops one such box per volume and resizes it; pass
      ``align_corners=True`` there.

Examples:
    >>> x_start = torch.tensor([0, 3])
    >>> y_start = torch.tensor([1, 4])
    >>> z_start = torch.tensor([2, 5])
    >>> width = torch.tensor([10, 40])
    >>> height = torch.tensor([20, 50])
    >>> depth = torch.tensor([30, 60])
    >>> bbox_generator3d(x_start, y_start, z_start, width, height, depth)
    tensor([[[ 0,  1,  2],
             [ 9,  1,  2],
             [ 9, 20,  2],
             [ 0, 20,  2],
             [ 0,  1, 31],
             [ 9,  1, 31],
             [ 9, 20, 31],
             [ 0, 20, 31]],
    <BLANKLINE>
            [[ 3,  4,  5],
             [42,  4,  5],
             [42, 53,  5],
             [ 3, 53,  5],
             [ 3,  4, 64],
             [42,  4, 64],
             [42, 53, 64],
             [ 3, 53, 64]]])

zA`x_start`, `y_start` and `z_start` must be a scalar or (B,). Got r�   rF   z<`width`, `height` and `depth` must be a scalar or (B,). Got rž   rŸ   z), `z_start`(r    r¡   z) and `depth`(r¢   r£   ri   r¤   r¦   rn   r   ro   )rI   rI   r   r_   r|   ra   )rI   r   r   )rI   rI   r   )r    r¥   rQ   rk   rj   r!   ÚzerosÚnumelr�   ÚcloneÚexpandrR   )rš   r›   r­   rc   rd   r®   ÚbboxÚ	bbox_backs   &&&&&&  r   Úbbox_generator3dr¶   Ï  s  € ðR �M‰M˜WŸ]™]Ö;¨g¯m©m×;ÀÇÁÃÐQWÔ@WÜØOÐPWÈyÐXZÐ[bÐZcÐceÐfmÐenÐnoÐpó
ð 	
ð �K‰K˜6Ÿ<™<Ö6¨5¯;©;×6¸5¿9¹9»;È&Ô;PÜÐ[Ð\aÐ[bÐbdÐekÐdlÐlnÐotÐnuÐuvÐwÓxÐxØ�=‰=˜GŸM™MÖh¨W¯]©]Öh¸e¿k¹kÖhÈVÏ\É\ÖhÐ]b×]hÑ]hÖhÜðØ$Ÿ]™]˜O¨=¸¿¹¸À}ÐU\×UbÑUbÐTcð dØ—{‘{�m <°·±¨~¸^ÈEÏKÉKÈ=ÐXZð\ó
ð 	
ð
 �>‰>˜WŸ^™^Ön¨w¯~©~ÖnÀÇÁÖnÐQW×Q^ÑQ^ÖnÐbg×bnÑbnÖnÜðØ$Ÿ^™^Ð,¨M¸'¿.¹.Ð9IÈÐW^×WeÑWeÐVfð gØ—|‘|�n L°·±°¸~ÈeÏlÉlÈ^Ð[]ð_ó
ð 	
ô �;Š;˜Ÿ™›¨¨AÐ.°w·~±~ÈWÏ]É]Ô[€Dàˆ‡M�W—\‘\ " aÓ(Õ(ƒMØˆ‡M�W—\‘\ " aÓ(Õ(ƒMØˆ‡M�W—\‘\ " aÓ(Õ(ƒMØˆ‡M�U—Z‘Z “^ aÕ'Õ'ƒMØˆ‡M�U—Z‘Z “^ aÕ'Õ'ƒMØˆ‡M�V—[‘[ “_ qÕ(Õ(ƒMØˆ‡M�V—[‘[ “_ qÕ(Õ(ƒMð —
‘
“€IØˆh×˜EŸJ™J r¨1Ó-°Õ1×9Ñ9¸"¸aÓ@Õ@ÓÜ�9Š9�dÐ&¨AÔ.Ð.r   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   Ú	trans_matr   r   ÚmodeÚstrÚrestore_coordinateszOptional[bool]r   r   )r   s   "r   r   r   <  s;   € ÷ Jñ JØðJØ$0ðJØ8;ðJØ[iðJàñJr   c                óf  € \        V\        4      '       g   \        R\        V4       24      hVR9  d   \	        RV 24      hVR8X  dE   VP                  4       pVR	,          VR
,          ,           VR
&   VR,          VR,          ,           VR&   \        P                  ! VP                  R,          4      ^,          p\        WP                  VP                  ^ ,          V^4      4      pVP                  V4      pVe	   V'       dñ   VP                  RR \        P                  ! ^^.4      8w  dÇ   VP                  4       p\        P                  ! VR^ ^.3,          RR7      ^ ,          VR	&   \        P                  ! VR^^.3,          RR7      ^ ,          VR&   \        P                  ! VR^ ^.3,          RR7      ^ ,          VR
&   \        P                  ! VR^^.3,          RR7      ^ ,          VR&   TpVR8X  d5   VR
,          VR	,          ,
          VR
&   VR,          VR,          ,
          VR&   V# )a-
  Transform xyxy or xywh box tensors by 3x3 matrices through their two endpoints.

``out = transform_bbox(trans_mat, boxes)`` takes ``(B, 3, 3)`` matrices and ``(N, 4)`` or ``(B, N, 4)`` boxes in
``mode`` ``'xyxy'`` (the default) or ``'xywh'``, and returns the transformed boxes in the same mode and shape.
Only the two endpoints ``(xmin, ymin)`` and ``(xmax, ymax)`` are transformed, and by default sorted again so that
``xmin <= xmax`` and ``ymin <= ymax``, so under a rotation or a shear the result is not the bounding box of the
transformed box. ``(B, N, 4, 2)`` corners are transformed vertex by vertex instead. For the bounding box of a
rotated box, use :meth:`~kornia.geometry.boxes.Boxes.transform_boxes`, which transforms all four corners, and
export it with :meth:`~kornia.geometry.boxes.Boxes.to_tensor`.

Convention:
    ``xyxy`` and ``xywh`` inputs are transformed through endpoint pairs. Coordinate
    restoration sorts their endpoints after a flip; polygon vertices retain their
    transformed cyclic order. ``'xywh'`` boxes are converted with ``xmax = xmin + width``, without the
    inclusive ``+1`` of the corner functions of this module. ``boxes`` is not modified.

Args:
    trans_mat: The transformation matrix to be applied, with supported shape ``(B, 3, 3)``.
        For boxes shaped ``(N, 4)``, ``B`` is one or ``N``. For boxes shaped
        ``(B, N, 4)`` or ``(B, N, 4, 2)``, it is one or the boxes' ``B``.
    boxes: The boxes to be transformed with a common shape of ``(N, 4)`` or batched as ``(B, N, 4)``, the
        polygon shape of ``(B, N, 4, 2)`` is also supported.
    mode: The format in which the boxes are provided. If set to 'xyxy' the boxes are assumed to be in the format
        ``xmin, ymin, xmax, ymax``. If set to 'xywh' the boxes are assumed to be in the format
        ``xmin, ymin, width, height``
    restore_coordinates: Reorder endpoints after a flipped ``xyxy`` or ``xywh`` transform.
        Enabled by default (``None`` behaves as ``True``); pass ``False`` to preserve raw
        transformed endpoints. Polygon vertices retain their transformed order.

Returns:
    The transformed boxes in the specified mode.

See Also:
    - :meth:`~kornia.geometry.boxes.Boxes.transform_boxes`: transforms all four corners of the boxes in a
      container, whose ``to_tensor`` export is the bounding box of a rotated box.
    - :func:`~kornia.geometry.linalg.transform_points`: the point transform applied to the endpoints and corners.
    - :func:`~kornia.geometry.transform.warp_perspective`: warps the images by the same source-to-destination
      matrix.

zMode must be a string. Got Úxywhz(Mode must be one of 'xyxy', 'xywh'. Got .ºr   NNNr¤   )Úxyxyr½   rG   ).r   rH   ).r   r   r   )Ú
isinstancerº   r~   Útyper}   r²   ÚmathÚprodr    r   r*   Úview_asr!   r"   ÚminÚmax)r¸   r   r¹   r»   Ú
num_pointsÚtransformed_boxesÚrestored_boxess   &&&&   r   Útransform_bboxrÊ   <  s  € ôV �dœC× Ò ÜÐ5´d¸4³j°\ÐBÓCÐCàÐ#Ô#ÜÐCÀDÀ6ÐJÓKÐKð ˆv„~Ø—‘“ˆØ˜f�¨¨f­Õ5ˆˆf‰Ø˜f�¨¨f­Õ5ˆˆf‰ô —’˜5Ÿ;™; r�?Ó+¨qÕ0€JÜ&6°yÇ-Á-ÐPU×P[ÑP[Ð\]ÕP^Ð`jÐlmÓBnÓ&oÐØ)×1Ñ1°%Ó8ÐàÒ#×':ÀÇÁÈBÈCÐ@PÔTY×T^ÒT^Ð`aÐcdÐ_eÓTfÔ@fØ*×0Ñ0Ó2ˆä!&§¢Ð+<¸SÀ1ÀaÀ&¸[Õ+IÈrÔ!RÐSTÕ!Uˆ�vÑÜ!&§¢Ð+<¸SÀ1ÀaÀ&¸[Õ+IÈrÔ!RÐSTÕ!Uˆ�vÑÜ!&§¢Ð+<¸SÀ1ÀaÀ&¸[Õ+IÈrÔ!RÐSTÕ!Uˆ�vÑÜ!&§¢Ð+<¸SÀ1ÀaÀ&¸[Õ+IÈrÔ!RÐSTÕ!Uˆ�vÑØ*Ðàˆv„~Ø$5°fÕ$=Ð@QÐRXÕ@YÕ$YÐ˜&Ñ!Ø$5°fÕ$=Ð@QÐRXÕ@YÕ$YÐ˜&Ñ!àÐr   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r   r   ÚscoresÚiou_thresholdÚfloatr   r   )r   s   "r   r   r   ‰  s.   € ÷ r3ñ r3ˆ|ð r3 \ð r3À%ð r3ÈLñ r3r   c                óF  € V P                   ^8w  g   V P                  R,          ^8w  d   \        RV P                   R24      h\        VP                  4      ^8w  d   \        RVP                   R24      hV P                  ^ ,          VP                  ^ ,          8w  d&   \        RV P                  VP                  3 R24      hV P                  \
        P                  \
        P                  39   d   V P                  4       p MlV P                  \
        P                  \
        P                  \
        P                  \
        P                  39   d    V P                  \
        P                  4      p V P                  R4      w  r4rVWS,
          Wd,
          ,          pVP!                  RR7      w  r‰. p
V	P                  ^ ,          ^ 8”  Ed•   V	^ ,          pV
P#                  V4       \
        P$                  ! W;,          W9R,          ,          4      p\
        P$                  ! WK,          WIR,          ,          4      p\
        P&                  ! W[,          WYR,          ,          4      p\
        P&                  ! Wk,          WiR,          ,          4      p\
        P(                  ! Wì,
          RR	7      p\
        P(                  ! Wý,
          RR	7      pVV,          pW{,          WyR,          ,          ,           V,
          pV\
        P*                  ! V^ 8„  V\
        P,                  ! V4      4      ,          p\
        P*                  ! VV8„  ( 4      ^ ,          pV	V^,           ,          p	EK­  \        V
4      ^ 8”  d   \
        P.                  ! V
4      # V P1                  R\
        P2                  R
7      # )a/  Suppress overlapping (N, 4) xyxy boxes by IoU, keeping the highest-scored ones.

``keep = nms(boxes, scores, iou_threshold)`` takes ``N`` exclusive ``xyxy`` boxes ``(N, 4)`` and their scores
``(N,)``, and returns the ``int64`` indices of the boxes it keeps, highest score first: it keeps the
highest-scored remaining box, drops every remaining box whose IoU with it exceeds ``iou_threshold``, and repeats
until no box remains. It takes one set of boxes per call; see the notes for how it compares with
``torchvision.ops.nms``. To keep the local maxima of a response map rather than boxes, use
:func:`~kornia.geometry.subpix.nms2d`.

Convention:
    Boxes use exclusive ``xyxy`` coordinates. IoU area is
    ``(x_2 - x_1) * (y_2 - y_1)`` and the result contains kept indices rather
    than a boolean mask.

Args:
    boxes: tensor containing exclusive ``xyxy`` bounding boxes with shape
        ``(N, 4)``, ordered as ``(x_1, y_1, x_2, y_2)``.
    scores: tensor containing the scores associated to each bounding box with shape ``(N,)``.
    iou_threshold: the threshold to discard the overlapping boxes.

Returns:
    Indices of the boxes kept from the input set, ordered by descending score.

Note:
    Coordinates in ``float16`` or ``bfloat16`` are computed in ``float32``, and ``uint8``, ``int8``, ``int16``
    or ``int32`` coordinates in ``int64``. In their own dtype the sum of two areas overflows: two 181-pixel
    squares exceed the ``float16`` maximum of 65504, and two 32768-pixel squares exceed ``int32``.
    ``bfloat16`` does not overflow, but its rounding can move an IoU across ``iou_threshold``.

Note:
    For one box or more, ``torch.compile`` cannot capture ``nms`` in one graph: the number of loop iterations and
    the size of the output depend on the box values. ``fullgraph=True`` raises, and a default ``torch.compile``
    runs it eagerly, so it is outside the :ref:`torch.compile promise <export-support-promise>`.

Note:
    For ``float32`` or ``float64`` boxes with finite coordinates, distinct scores and ``iou_threshold >= 0`` it
    keeps the same boxes, in the same order, as ``torchvision.ops.nms``. Equal scores are ordered by
    :func:`torch.sort`, which is not stable, so with ties the order, and which of two overlapping tied boxes is
    kept, can differ from torchvision. A box with a NaN coordinate has a NaN IoU with every other box, which is
    not above ``iou_threshold``: it neither suppresses nor is suppressed, as in ``torchvision.ops.nms``.

Note:
    Two boxes whose union is empty, such as two zero-width boxes, have an IoU of ``0``, so at an ``iou_threshold``
    of at least ``0`` neither suppresses the other, as in ``torchvision.ops.nms``. With a negative threshold the
    lower-scored one is dropped, where torchvision, whose IoU for them is NaN, keeps both.

.. warning::
    This differs from the inclusive coordinate arithmetic used by several other
    :mod:`kornia.geometry.bbox` operations and is documented in
    `#4008 <https://github.com/kornia/kornia/issues/4008>`_.

See Also:
    - :func:`~kornia.geometry.subpix.nms2d`: keeps the strict local maxima of a ``(B, C, H, W)`` response map, not
      boxes.
    - :meth:`~kornia.geometry.boxes.Boxes.to_tensor`: exports the boxes of a container as the exclusive
      ``'xyxy'`` boxes this function takes.

Example:
    >>> boxes = torch.tensor([
    ...     [10., 10., 20., 20.],
    ...     [15., 5., 15., 25.],
    ...     [100., 100., 200., 200.],
    ...     [100., 100., 200., 200.]])
    >>> scores = torch.tensor([0.9, 0.8, 0.7, 0.9])
    >>> nms(boxes, scores, iou_threshold=0.8)
    tensor([0, 3, 1])

zboxes expected as Nx4. Got: rF   zscores expected as N. Got: z+boxes and scores mus have same shape. Got: T)Ú
descendingr¾   g        )rÅ   )rk   r   )r   )Úndimr    r}   r   rk   r!   Úfloat16Úbfloat16rÎ   Úuint8Úint8Úint16Úint32r‚   Úint64ÚunbindÚsortÚappendrÆ   rÅ   ÚclampÚwhereÚ	ones_liker+   Ú	new_emptyrr   )r   rÌ   rÍ   r©   rª   Úx2Úy2ÚareasÚ_ÚorderÚkeepÚiÚxx1Úyy1Úxx2Úyy2ÚwÚhÚinterÚunionÚovrÚindss   &&&                   r   Únmsrñ   ‰  s}  € ðJ ‡z�z�Q„˜%Ÿ+™+ b�/¨QÔ.ÜÐ7¸¿¹°}ÀAÐFÓGÐGä
ˆ6�<‰<Ó˜AÔÜÐ6°v·|±|°nÀAÐFÓGÐGà‡{�{�1…~˜Ÿ™ a�Ô(ÜÐFÀuÇ{Á{ÐTZ×T`ÑT`ÐG`ÐFaÐabÐcÓdÐdà‡{�{”u—}‘}¤e§n¡nÐ5Ô5Ø—‘“‰Ø	�‰œŸ™¤e§j¡j´%·+±+¼u¿{¹{ÐKÔ	KØ—‘œŸ™Ó%ˆà—\‘\ "Ó%�N€BˆBØ�W˜�Õ!€Eà�{‰{ dˆ{Ó+�H€Aà€DØ
�+‰+�a�.˜1Õ
Ø�!�HˆØ�‰�AŒÜ�iŠi˜�˜r¨¥)�}Ó-ˆÜ�iŠi˜�˜r¨¥)�}Ó-ˆÜ�iŠi˜�˜r¨¥)�}Ó-ˆÜ�iŠi˜�˜r¨¥)�}Ó-ˆä�KŠK˜�	 sÔ+ˆÜ�KŠK˜�	 sÔ+ˆØ�A•ˆØ•˜5 r¥Õ+Õ+¨eÕ3ˆð ”e—k’k %¨!¡)¨U´E·O²OÀEÓ4JÓKÕKˆô �{Š{˜S =Ñ0Ð1Ó2°1Õ5ˆØ�d˜Q•h•‹ä
ˆ4ƒy�1„}Ü�{Š{˜4Ó Ð à�?‰?˜4¤u§z¡zˆ?Ó2Ð2r   )
r«   r¶   rŽ   r˜   re   rw   rñ   rÊ   r>   rC   )NNN)r¿   N)Ú__doc__Ú
__future__r   rÂ   r&   Útypingr   r!   Úkornia.core._compatr   Úkornia.core.checkr   Úkornia.core.utilsr   Úlinalgr   Ú__all__r(   rB   r>   rC   rA   re   rw   rŽ   r˜   r«   r¶   rÊ   rñ   r   r   r   Ú<module>rú      sŒ   ðñ$õ> #ã Û Ý ã å 9Ý 0Ý *å $ò€ð8ð ðGð õAõH*õZ)õX>õBR#÷jw õtoõdUõpj/÷ZJ÷Zr3r   