
    ^j`                    b    d dl mZ d dlZd dlZd dlmZ 	 	 d	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZ	y)	    )annotationsNc                    ||| S | D cg c]  }t        j                  |       }}t        | |      D cg c]  \  }}|||k\  r	|||k  r| c}}S c c}w c c}}w )aM  
    Filters a list of polygons based on their area.

    Args:
        polygons: A list of polygons, where each polygon is
            represented by a NumPy array of shape `(N, 2)`,
            containing the `x`, `y` coordinates of the points.
        min_area: The minimum area threshold.
            Only polygons with an area greater than or equal to this value
            will be included in the output. If set to None,
            no minimum area constraint will be applied.
        max_area: The maximum area threshold.
            Only polygons with an area less than or equal to this value
            will be included in the output. If set to None,
            no maximum area constraint will be applied.

    Returns:
        A new list of polygons containing only those with
            areas within the specified thresholds.
    )cv2contourAreazip)polygonsmin_areamax_areapolygonaresareas         o/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/supervision/detection/utils/polygons.pyfilter_polygons_by_arear      s|    2 H,4<=COOG$=D= !40GT 0!1 	  >s
   AAc                   |dk  s|dk\  rt        d      |dk  rt        d      t        t        t        |       d|z
  z        d      }t        |       |k  r| S d}| }t        |      |kD  rT||z  }t	        j
                  t        j                  | |d      d      }t        |      dk  r	 |S |}t        |      |kD  rT|S )	u  
    Approximates a given polygon by reducing a certain percentage of points.

    This function uses the Ramer-Douglas-Peucker algorithm to simplify the input
    polygon by reducing the number of points while preserving the general shape.

    Args:
        polygon: A 2D NumPy array of shape `(N, 2)` containing
            the `x`, `y` coordinates of the input polygon's points.
        percentage: The percentage of points to be removed from the
            input polygon, in the range `[0, 1)`.
        epsilon_step: Approximation accuracy step, must be positive.
            Epsilon is the maximum distance between the original curve
            and its approximation.

    Returns:
        A new 2D NumPy array of shape `(M, 2)` containing the `x`, `y`
            coordinates of the approximated polygon's points. `M` is at most
            `floor(N * (1 - percentage))` (minimum 3). Because epsilon
            advances in discrete `epsilon_step` increments, `M` may be
            noticeably smaller than the budget when a single step crosses
            the target band. At least 3 points are always kept; when further
            simplification would collapse the polygon below 3 points the last
            valid approximation is returned and `M` may exceed the budget.

    Raises:
        ValueError: If `percentage` is outside the range `[0, 1)`.
        ValueError: If `epsilon_step` is not positive.

    Examples:
        Reduce a polygon to at most half its original point count:

        >>> import numpy as np
        >>> polygon = np.array([[0, 0], [10, 0], [10, 10], [0, 10],
        ...                     [5, 10], [5, 5], [3, 7], [1, 9]])
        >>> result = approximate_polygon(polygon, percentage=0.5)
        >>> result.shape[1]
        2
        >>> len(result) <= max(int(len(polygon) * 0.5), 3)
        True

        Polygon already at or below target — returned unchanged:

        >>> tiny = np.array([[0, 0], [5, 0], [2, 4]])
        >>> approximate_polygon(tiny, percentage=0.5) is tiny
        True
    r      z'Percentage must be in the range [0, 1).zepsilon_step must be positive.   T)closed)axis)
ValueErrormaxintlennpsqueezer   approxPolyDP)r   
percentageepsilon_steptarget_pointsepsilonapproximated_points	candidates          r   approximate_polygonr"   ,   s    f A~qBCCq9::CLA
N;<a@M
7|}$G!
!
"]
2<JJs//NUVW	 y>A  ( !
"]
2     )NN)r   list[npt.NDArray[np.number]]r	   float | Noner
   r%   returnr$   )g?)r   npt.NDArray[np.number]r   floatr   r(   r&   r'   )

__future__r   r   numpyr   numpy.typingtypingnptr   r"    r#   r   <module>r/      st    " 
  
 "!!*!! ! "	!J OSH#H16HFKHHr#   