
    OgZ#                        d dl mZ d dlmZmZ d dlmZ d dlZd dlZ	d dl
mZmZmZmZ dgZ	  G d d      ZddZ ed	      dd
       Zy)    )annotations)IterableSequence)	lru_cacheN)Vec3NULLVECMatrix44UVecBezierc                  ~    e Zd ZdZddZedd       ZdddZdddZddZ	ddZ
ddZdd	Z	 	 	 	 dd
ZddZddZy)r   uC  Generic `Bézier curve`_ of any degree.

    A `Bézier curve`_ is a parametric curve used in computer graphics and
    related fields. Bézier curves are used to model smooth curves that can be
    scaled indefinitely. "Paths", as they are commonly referred to in image
    manipulation programs, are combinations of linked Bézier curves.
    Paths are not bound by the limits of rasterized images and are intuitive to
    modify. (Source: Wikipedia)

    This is a generic implementation which works with any count of definition
    points greater than 2, but it is a simple and slow implementation. For more
    performance look at the specialized :class:`Bezier4P` and :class:`Bezier3P`
    classes.

    Objects are immutable.

    Args:
        defpoints: iterable of definition points as :class:`Vec3` compatible objects.

    c                8    t        j                  |      | _        y N)r   tuple
_defpoints)self	defpointss     V/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/math/bezier.py__init__zBezier.__init__b   s    *.**Y*?    c                    | j                   S )z1Control points as tuple of :class:`Vec3` objects.)r   r   s    r   control_pointszBezier.control_pointse   s     r   c                B    | j                  | j                  |            S )zhApproximates curve by vertices as :class:`Vec3` objects, vertices
        count = segments + 1.
        )pointsparamsr   segmentss     r   approximatezBezier.approximatej   s     {{4;;x011r   c              #     K   d fdd|z  }d} j                   d   }| |dk  r\||z   }t        j                  |d      r j                   d   }d}n j                  |      } ||||      E d{    |}|}|dk  r[yy7 w)a8  Adaptive recursive flattening. The argument `segments` is the
        minimum count of approximation segments, if the distance from the center
        of the approximation segment to the curve is bigger than `distance` the
        segment will be subdivided.

        Args:
            distance: maximum distance from the center of the curve (Cn)
                to the center of the linear (C1) curve between two
                approximation points to determine if a segment should be
                subdivided.
            segments: minimum segment count

        c              3     K   ||z   dz  }j                  |      }| j                  |      }|j                  |      k  r| y  	| |||      E d {     	||||      E d {    y 7 7 w)Ng      ?)pointlerpdistance)
start_point	end_pointstart_tend_tmid_t	mid_point	chk_pointr#   r   subdivs
          r   r+   z!Bezier.flattening.<locals>.subdiv   s     u_+E

5)I#((3I !!),x7!+y'5III!)YuEEE JEs$   AA2A.A2(A0)A20A2      ?        r   N)r&   floatr'   r/   )r   mathiscloser!   )	r   r#   r   dtt0r$   t1r%   r+   s	   ``      @r   
flatteningzBezier.flatteningp   s     
	F 8^ooa(3hbB||B$ OOB/	 JJrN	k9b"===B#K 3h >s   A4B9B
:BBc              #  T   K   t        j                  dd|dz         E d{    y7 w)zCYield evenly spaced parameters from 0 to 1 for given segment count.r-   r,      N)nplinspacer   s     r   r   zBezier.params   s      ;;sCA666s   (&(c                    |dk  s|dkD  rt        d      d|z
  dk  rd}t        }| j                  }t        |      }t	        |      D ]  }|t        |dz
  ||      ||   z  z  } |S )z[Returns a point for parameter `t` in range [0, 1] as :class:`Vec3`
        object.
        r-   r,   Parameter t not in range [0, 1]h㈵>r7   )
ValueErrorr   r   lenrangebernstein_basis)r   tr!   ptsnis         r   r!   zBezier.point   s}     s7a#g>??!GtAooHq 	;A_QUAq1CF::E	;r   c              #  @   K   |D ]  }| j                  |        yw)zYields multiple points for parameters in vector `t` as :class:`Vec3`
        objects. Parameters have to be in range [0, 1].
        N)r!   r   rA   us      r   r   zBezier.points   s%       	 A**Q-	    c                |   |dk  s|dkD  rt        d      d|z
  dk  rd}| j                  }t        |      }|dz
  }t        }t        }t        }||z  }|dz
  }	|dk(  r(||d   |d   z
  z  }||	z  |d   d|d   z  z
  |d   z   z  }t	        |      D ]  }
t        ||
|      }||||
   z  z  }d|cxk  rdk  rPn nMd|z
  }|
||z  z
  }||||z  z  |z  ||
   z  z  }|||z  ||z  z
  |
dd|z  z
  z  z
  ||z  |z  z  |z  ||
   z  z  }|dk(  s||||   ||	   z
  z  }||	z  ||   d||	   z  z
  ||dz
     z   z  } |||fS )	zReturns (point, 1st derivative, 2nd derivative) tuple for parameter `t`
        in range [0, 1] as :class:`Vec3` objects.
        r-   r,   r;   r<   r7   r   g       @   )r=   r   r>   r   r?   r@   )r   rA   rB   rC   n0r!   d1d2t2n0_1rD   tmp_bas_1_ti_n0_ts                 r   
derivativezBezier.derivative   s    s7a#g>??!GtAooHUUAv8s1vA'Bdc!fsSV|3c!f<=Bq 	IA%b!Q/GWs1v%%EQ}}QwR!VfD)G3c!f<<f_rBw.cC!Gm1DDDy4') !f Cx3r7SY./$Y#b'AD	M"9CQK"GH	I  b"}r   c              #  @   K   |D ]  }| j                  |        yw)zReturns multiple (point, 1st derivative, 2nd derivative) tuples for
        parameter vector  `t` as :class:`Vec3` objects.
        Parameters in range [0, 1]
        N)rS   rF   s      r   derivativeszBezier.derivatives   s&       	%A//!$$	%rH   c                P    t        t        t        | j                                    S )u>   Returns a new Bèzier-curve with reversed control point order.)r   listreversedr   r   s    r   reversezBezier.reverse   s    d8D$7$789::r   c                `    t        |j                  | j                              }t        |      S )zGeneral transformation interface, returns a new :class:`Bezier` curve.

        Args:
             m: 4x4 transformation matrix (:class:`ezdxf.math.Matrix44`)

        )r   transform_verticesr   r   )r   mr   s      r   	transformzBezier.transform   s*     !..t/B/BCD	i  r   N)r   zIterable[UVec])returnzSequence[Vec3])   )r   intr^   Iterable[Vec3])   )r#   r/   r   r`   r^   ra   )r   r`   r^   Iterable[float])rA   r/   r^   r   )rA   rc   r^   ra   )rA   r/   r^   ztuple[Vec3, Vec3, Vec3])rA   rc   r^   z!Iterable[tuple[Vec3, Vec3, Vec3]])r^   r   )r\   r	   r^   r   )__name__
__module____qualname____doc__r   propertyr   r   r5   r   r!   r   rS   rU   rY   r]    r   r   r   r   L   s`    *@  2($T7  $L% %	*%;!r   c                    |dk(  r|dk(  rd}nt        ||      }| |k(  r|dk(  rd}nt        d|z
  | |z
        }t        |       t        |      t        | |z
        z  z  }||z  |z  S )Nr-   r   r,   )pow	factorial)rC   rD   rA   titniNis         r   r@   r@      ss    CxAFAYAv!s(37a!e%	11	!a%(88	9B7S=r   )maxsizec                ,    t        j                  |       S r   )r0   rl   )rC   s    r   rl   rl     s    >>!r   )rC   r`   rD   r`   rA   r/   r^   r/   )rC   r`   )
__future__r   typingr   r   	functoolsr   r0   numpyr8   
ezdxf.mathr   r   r	   r
   __all__r   r@   rl   ri   r   r   <module>rx      sV    # %    4 4 *:ze! e!P 4 r   