
    j              	           d dl Z d dlZd dlmZ ddgZdej        deeeeef         ee	e	e	e	f         z  dej        fdZ
d	ej        d
eeef         dee	e	f         fdZ	 	 	 dd
eeef         dej        fdZdS )    N)rotate_abs_geoms
crop_boxescreate_shadow_maskboxescrop_boxreturnc                 H   |                                  dk    }t          |          dk    }||z  rt          d          |\  }}}}| ddddgf                             ||          |z
  | ddddgf<   | ddddgf                             ||          |z
  | ddddgf<   |r2| ddddgfxx         ||z
  z  cc<   | ddddgfxx         ||z
  z  cc<   t          j        | dddf         | dddf         k     | dddf         | dddf         k               }| |         S )zCrop localization boxes

    Args:
        boxes: ndarray of shape (N, 4) in relative or abs coordinates
        crop_box: box (xmin, ymin, xmax, ymax) to crop the image, in the same coord format that the boxes

    Returns:
        the cropped boxes
       zGboth the boxes and the crop need to have the same coordinate conventionNr         )maxAssertionErrorclipnplogical_and)	r   r   
is_box_relis_crop_relxminyminxmaxymaxis_valids	            c/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/transforms/functional/base.pyr   r      s    !Jh--1$KK hfggg%D$dQQQAY',,T4884?E!!!aV)QQQAY',,T4884?E!!!aV) (aaa!QiD4K'aaa!QiD4K' ~eAAAqDkE!!!Q$K7qqq!tuQQQPQT{9RSSH?    linetarget_shapec                    t          d t          | d         |ddd                   D                       r| d         S | d         | d         z
  }|dk    }|dk    }|d         dk    r| d         df| d         |d         fg}n|d         dk    rd| d         f|d         | d         fg}n[|d         |d         z  }| d         || d	         z  z
  }d|f| |z  df|d         ||d         z  |z   f|d         |z
  |z  |d         fg}|D ]f}t          d
 t          ||ddd                   D                       r3t          d t          || d         ||          D                       r|c S gt          )a  Expands a 2-point line, so that the first is on the edge. In other terms, we extend the line in
    the same direction until we meet one of the edges.

    Args:
        line: array of shape (2, 2) of the point supposed to be on one edge, and the shadow tip.
        target_shape: the desired mask shape

    Returns:
        2D coordinates of the first point once we extended the line (on one of the edges)
    c              3   4   K   | ]\  }}|d k    p||k    V  dS r   N ).0coordsizes      r   	<genexpr>zexpand_line.<locals>.<genexpr><   s3      
[
[;5$5A:&$
[
[
[
[
[
[r   r   Nr
   r   r   r   r
   r
   r
   r
   r   c              3   4   K   | ]\  }}|d k     p||k    V  dS r   r    )r!   valr#   s      r   r$   zexpand_line.<locals>.<genexpr>c   s3      UUdsQw$#*UUUUUUr   c              3   L   K   | ]\  }}}}|r||k    n|r||k     n||k    V   d S )Nr    )r!   r+   ref_dir_sames        r   r$   zexpand_line.<locals>.<genexpr>e   s\       
 
%S$  GC3JJ4&FcCiiS3Y
 
 
 
 
 
r   )anyzipall
ValueError)	r   r   _tmp
_direction_flat	solutionsalphabetapoints	            r   expand_liner;   1   s    
[
[#d1g|TXTXVXTXGY:Z:Z
[
[
[[[ Aw7T!WDJAIEAw!|| $ZO$Za)	
		 
aA T
O!_d4j)	
		 Q$q'!DzEDJ..
 IUU]A!_el1o5<=1o$-|A?	
	   UUc%ddPRdAS6T6TUUUUU 	 
 
),UDGZ)O)O
 
 
 
 
 	 LLL		
 r   333333?      ?c           	         t           j                            d          }|d|z
  |d         z  z   }|dz  d|z
  |d         z  z   }t          |d         |z  | d         z  | d         z  |          }|dz  d|z
  |d         z  z   }|d         |z  }	|	dz  d|	z
  |d         z  z   }
||k     }t          j        ||dz  z
  dg||dz  z   dg||dz  z   |r|
|	dz  z   n|
|	dz  z
  g||dz  z
  |r|
|	dz  z
  n|
|	dz  z   ggt           j                  }t          j        |d	d	df         | d         z  |d	d	df         | d         z  fd
                                                              t           j	                  }t           j                            d          }t          |d         d|d         z  | d          d                                                             t           j	                  }t          |d         dz            }|dz  dk    rWt          j        t          j        | d                   d	d	d	f         | d         d          | d         dz
  z  }|dk    rd|z
  }nVt          j        t          j        | d                   d	d	d	f         | d         d          | d         dz
  z  }|dk    rd|z
  }|                                }t          |ddg         |           |d<   t          |ddg         |           |d<   t          j        |d         |d         k              st#          |d         |d                   | d         k     rdn| d         }t#          |d         |d                   | d         k     rdn| d         }t          j        ||g          }t          j        |d	d         |d         |dd	         fd          }t          j        g | dR t           j                  }t+          j        ||gdt*          j                  d         }|dz                      t           j                                      dd          |                    t           j                  z  S )an  Creates a random shadow mask

    Args:
        target_shape: the target shape (H, W)
        min_base_width: the relative minimum shadow base width
        max_tip_width: the relative maximum shadow tip width
        max_tip_height: the relative maximum shadow tip height

    Returns:
        a numpy ndarray of shape (H, W, 1) with values in the range [0, 1]
       r
   r   r   r         )dtypeNr%   )axis)N.ih  F)expandg      ?r&   r)   r'   r(   )   )lineType).r   rE   )r   randomrandminarrayfloat32stackroundastypeint32r   intrepeatarangecopyr;   r0   r   concatenatezerosuint8cv2fillPolyLINE_AAr   )r   min_base_widthmax_tip_widthmax_tip_height_params
base_widthbase_center	tip_width
tip_center
tip_heighttip_mid_ordercontourabs_contourrotated_contourquad_idxintensity_maskfinal_contourcorner_xcorner_ycornermasks                         r   r   r   m   s   $ innQG1~#5"CCJq.A
Ngaj#@@KGAJ+l1o=QOQ^__IQ!i-71:!==Jn,J1nJ'!*<<G+%F(:>)1-:>)1-)a-'V)i:>)A)AQX[ehi[iQij)a-'V)i:>)A)AQX[ehi[iQij		
 j  G 		QQQT]\!_,gaaadml1o.MN	
 	
 	

 
			  innQG	"'!*		
 	
 	

 	 
			  71:$%%H!|q29\!_#=#=aaag#FUV^_```dpqrdsvwdwxq==/N 29\!_#=#=dAAAg#FUV^_```dpqrdsvwdwxq==/N $((**M"=!Q#8,GGM!"=!Q#8,GGM!6-"mA&6677 jM$/t1DEEUVWW11]ijk]lM$/t1DEEUVWW11]ijk]lXx&:;;bqb(96);Lm\]\^\^N_'`ghiii x 2, 2 2 2"(CCCD<}ovLLLVTD3Jrz**//1558M8Mbj8Y8YYYr   )r<   r=   r<   )rW   numpyr   doctr.utils.geometryr   __all__ndarraytuplerP   floatr   r;   r   r    r   r   <module>ru      s'   


     1 1 1 1 1 1-
.:Cc3&'%ueU0J*KK Z   D9bj 9c3h 9E%QV,DW 9 9 9 9| 	XZ XZS/XZ
 ZXZ XZ XZ XZ XZ XZr   