a
    hE                     @   s   d dl Z d dl mZ eedddZeedddZeeddd	Zeedd
dZeedddZeedddZeedddZeedddZ	dS )    N)Tensor)boxesreturnc           	      C   s\   |  d\}}}}|d|  }|d|  }|d|  }|d|  }tj||||fdd} | S )ad  
    Converts bounding boxes from (cx, cy, w, h) format to (x1, y1, x2, y2) format.
    (cx, cy) refers to center of bounding box
    (w, h) are width and height of bounding box
    Args:
        boxes (Tensor[N, 4]): boxes in (cx, cy, w, h) format which will be converted.

    Returns:
        boxes (Tensor(N, 4)): boxes in (x1, y1, x2, y2) format.
    g      ?Zdimunbindtorchstack)	r   cxcywhx1y1x2y2 r   J/var/www/auris/lib/python3.9/site-packages/torchvision/ops/_box_convert.py_box_cxcywh_to_xyxy   s    r   c           	      C   sT   |  d\}}}}|| d }|| d }|| }|| }tj||||fdd} | S )ah  
    Converts bounding boxes from (x1, y1, x2, y2) format to (cx, cy, w, h) format.
    (x1, y1) refer to top left of bounding box
    (x2, y2) refer to bottom right of bounding box
    Args:
        boxes (Tensor[N, 4]): boxes in (x1, y1, x2, y2) format which will be converted.

    Returns:
        boxes (Tensor(N, 4)): boxes in (cx, cy, w, h) format.
    r      r   r   )	r   r   r   r   r   r   r   r   r   r   r   r   _box_xyxy_to_cxcywh   s    r   c                 C   s4   |  d\}}}}tj|||| || gdd} | S )aX  
    Converts bounding boxes from (x, y, w, h) format to (x1, y1, x2, y2) format.
    (x, y) refers to top left of bounding box.
    (w, h) refers to width and height of box.
    Args:
        boxes (Tensor[N, 4]): boxes in (x, y, w, h) which will be converted.

    Returns:
        boxes (Tensor[N, 4]): boxes in (x1, y1, x2, y2) format.
    r   r   r   )r   xyr   r   r   r   r   _box_xywh_to_xyxy2   s    r   c                 C   s<   |  d\}}}}|| }|| }tj||||fdd} | S )a]  
    Converts bounding boxes from (x1, y1, x2, y2) format to (x, y, w, h) format.
    (x1, y1) refer to top left of bounding box
    (x2, y2) refer to bottom right of bounding box
    Args:
        boxes (Tensor[N, 4]): boxes in (x1, y1, x2, y2) which will be converted.

    Returns:
        boxes (Tensor[N, 4]): boxes in (x, y, w, h) format.
    r   r   r   )r   r   r   r   r   r   r   r   r   r   _box_xyxy_to_xywhB   s
    r   c                 C   s   | j }|   }| d\}}}}}|tj d }t|t| }	}
||d |	  |d |
  }||d |	  |d |
  }tj|||||fdd} |r|   | 	|} | S )a.  
    Converts rotated bounding boxes from (cx, cy, w, h, r) format to (x1, y1, w, h, r) format.
    (cx, cy) refers to center of bounding box
    (w, h) refers to width and height of rotated bounding box
    (x1, y1) refers to top left of rotated bounding box
    r is rotation angle w.r.t to the box center by :math:`|r|` degrees counter clock wise in the image plan
    Args:
        boxes (Tensor[N, 5]): boxes in (cx, cy, w, h, r) format which will be converted.

    Returns:
        boxes (Tensor(N, 5)): rotated boxes in (x1, y1, w, h, r) format.
    r        f@r   r   
dtypeis_floating_pointr   r	   picossinr
   round_to)r   r   	need_castr   r   r   r   rr_radr!   r"   r   r   r   r   r   _box_cxcywhr_to_xywhrT   s    

r(   c                 C   s   | j }|   }| d\}}}}}|tj d }t|t| }	}
||d |	  |d |
  }||d |
  |d |	  }tj|||||gdd} |r|   | 	|} | S )a  
    Converts rotated bounding boxes from (x1, y1, w, h, r) format to (cx, cy, w, h, r) format.
    (x1, y1) refers to top left of rotated bounding box
    (w, h) refers to width and height of rotated bounding box
    r is rotation angle w.r.t to the box center by :math:`|r|` degrees counter clock wise in the image plan
    Args:
        boxes (Tensor[N, 5]): rotated boxes in (x1, y1, w, h, r) format which will be converted.

    Returns:
        boxes (Tensor[N, 5]): rotated boxes in (cx, cy, w, h, r) format.
    r   r   r   r   r   )r   r   r%   r   r   r   r   r&   r'   r!   r"   r   r   r   r   r   _box_xywhr_to_cxcywhrq   s    

r)   c              	   C   s   | j }|   }| d\}}}}}|tj d }t|t| }	}
|||	  }|||
  }|||
  }|||	  }|||
  }|||	  }tj||||||||fdd} |r|   | 	|} | S )a  
    Converts rotated bounding boxes from (x1, y1, w, h, r) format to (x1, y1, x2, y2, x3, y3, x4, y4) format.
    (x1, y1) refer to top left of bounding box
    (w, h) are width and height of the rotated bounding box
    r is rotation angle w.r.t to the box center by :math:`|r|` degrees counter clock wise in the image plan

    (x1, y1) refer to top left of rotated bounding box
    (x2, y2) refer to top right of rotated bounding box
    (x3, y3) refer to bottom right of rotated bounding box
    (x4, y4) refer to bottom left ofrotated bounding box
    Args:
        boxes (Tensor[N, 5]): rotated boxes in (cx, cy, w, h, r) format which will be converted.

    Returns:
        boxes (Tensor(N, 8)): rotated boxes in (x1, y1, x2, y2, x3, y3, x4, y4) format.
    r   r   r   r   )r   r   r%   r   r   r   r   r&   r'   r!   r"   r   r   x3y3x4y4r   r   r   _box_xywhr_to_xyxyxyxy   s     

r.   c                 C   s   | j }|   }| d\}}}}}}}	}
t|| || }|d tj }|| d || d   }|| d || d   }tj|||||fdd} |r|   | 	|} | S )a  
    Converts rotated bounding boxes from (x1, y1, x2, y2, x3, y3, x4, y4) format to (x1, y1, w, h, r) format.
    (x1, y1) refer to top left of the rotated bounding box
    (x2, y2) refer to bottom left of the rotated bounding box
    (x3, y3) refer to bottom right of the rotated bounding box
    (x4, y4) refer to top right of the rotated bounding box
    (w, h) refers to width and height of rotated bounding box
    r is rotation angle w.r.t to the box center by :math:`|r|` degrees counter clock wise in the image plan

    Args:
        boxes (Tensor(N, 8)): rotated boxes in (x1, y1, x2, y2, x3, y3, x4, y4) format.

    Returns:
        boxes (Tensor[N, 5]): rotated boxes in (x1, y1, w, h, r) format.
    r      r   r   )
r   r   r   r	   atan2r    sqrtr
   r#   r$   )r   r   r%   r   r   r   r   r*   r+   r,   r-   r'   r&   r   r   r   r   r   _box_xyxyxyxy_to_xywhr   s    

r2   )
r	   r   r   r   r   r   r(   r)   r.   r2   r   r   r   r   <module>   s   %