
    Og-                    D   U d dl mZ d dlmZmZmZmZmZmZ d dl	Z	ddl
mZmZ ddlmZ ddlmZ erd dlmZ d d	lmZ g d
Z edee      Z G d dee         Z	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZe	j2                  dz  Zded<   	 d	 	 	 	 	 ddZdZdZeZ	 d	 	 	 	 	 	 	 ddZ y)    )annotations)TYPE_CHECKINGIterableIteratorSequenceTypeVarGenericN   )Vec3Vec2)Matrix44)arc_angle_span_deg)UVec)ConstructionEllipse)Bezier4Pcubic_bezier_arc_parameterscubic_bezier_from_arccubic_bezier_from_ellipseTc                  z    e Zd ZdZdZddZedd       ZddZddZ	ddZ
dddZdd	Zdd
ZdddZddZddZy)r   u  Implements an optimized cubic `Bézier curve`_ for exact 4 control points.

    A `Bézier curve`_ is a parametric curve, parameter `t` goes from 0 to 1,
    where 0 is the first control point and 1 is the fourth control point.

    The class supports points of type :class:`Vec2` and :class:`Vec3` as input, the
    class instances are immutable.

    Args:
        defpoints: sequence of definition points as :class:`Vec2` or
            :class:`Vec3` compatible objects.

    _control_points_offsetc                    t        |      dk7  rt        d      |d   j                  }|j                  dvrt	        d|j                         |d   | _        t        fd|D              | _        y )N   zFour control points required.r   )r   r   zinvalid point type: c              3  (   K   | ]	  }|z
    y wN ).0poffsets     Y/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/math/_bezier4p.py	<genexpr>z$Bezier4P.__init__.<locals>.<genexpr>=   s     3R1AJ3Rs   )len
ValueError	__class____name__	TypeErrorr   tupler   )self	defpoints
point_typer!   s      @r"   __init__zBezier4P.__init__2   sx    y>Q<==q\++
""&662:3F3F2GHII aL .33R	3R.R    c                Z    | j                   \  }}}}| j                  }|||z   ||z   ||z   fS )zBControl points as tuple of :class:`Vec3` or :class:`Vec2` objects.r   )r*   p0p1p2p3r!   s         r"   control_pointszBezier4P.control_points?   s;     --BBrF{BKf<<r.   c                h    d|cxk  rdk  st        d       t        d      | j                  |      S )u   Returns direction vector of tangent for location `t` at the
        Bèzier-curve.

        Args:
            t: curve position in the range ``[0, 1]``

        r         ?t not in range [0 to 1])r%   _get_curve_tangentr*   ts     r"   tangentzBezier4P.tangentG   s=     Q#677 677&&q))r.   c                h    d|cxk  rdk  st        d       t        d      | j                  |      S )u   Returns point for location `t` at the Bèzier-curve.

        Args:
            t: curve position in the range ``[0, 1]``

        r   r6   r7   )r%   _get_curve_pointr9   s     r"   pointzBezier4P.pointS   s=     Q#677 677$$Q''r.   c              #     K   |dk  rt        |      d|z  }| j                  }|d    t        d|      D ]  }| j                  ||z          |d    yw)u   Approximate `Bézier curve`_ by vertices, yields `segments` + 1
        vertices as ``(x, y[, z])`` tuples.

        Args:
            segments: count of segments for approximation

        r
   r6   r      N)r%   r4   ranger=   )r*   segmentsdelta_tcpsegments        r"   approximatezBezier4P.approximate^   sl      a<X&&.  eQ) 	;G'''(9::	;es   AAc              #    K   g }d|z  }d}| j                   }|d   }| |dk  r||z   }t        j                  |d      r|d   }	d}n| j                  |      }		 ||z   dz  }
| j                  |
      }|j	                  |	      }|j                  |      }||k  r|	 |}|	}|r|j                         \  }}	nn|j                  ||	f       |
}|}	w|dk  ryyw)a>  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 cubic (C3)
                curve to the center of the linear (C1) curve between two
                approximation points to determine if a segment should be
                subdivided.
            segments: minimum segment count

        r6           r   r@   g      ?N)r4   mathiscloser=   lerpdistancepopappend)r*   rL   rB   stackdtt0rD   start_pointt1	end_pointmid_t	mid_point	chk_pointds                 r"   
flatteningzBezier4P.flatteningo   s     (*(N  A 3hbB||B$qE	 11"5	 "R3#44U;	*//	:	&&y1x<#OB"+K(-		ILL"i1B )I#  3hs   CCCc                    | j                   \  }}}}||z  }d|z
  }d|z  |z  |z  }d|z  |z  }	||z  }
||z  ||	z  z   ||
z  z   | j                  z   S )Nr6         @r   )r*   r:   _r1   r2   r3   t2
_1_minus_tbcrX   s              r"   r=   zBezier4P._get_curve_point   sy     ,,2r2U1W
*z)A-*r!FAvQa'$,,66r.   c                    | j                   \  }}}}||z  }ddd|z  z
  d|z  z   z  }d|z  dd|z  z
  z  }d|z  }	||z  ||z  z   ||	z  z   S )Nr[   r6         @       @)r   )
r*   r:   r\   r1   r2   r3   r]   r_   r`   rX   s
             r"   r8   zBezier4P._get_curve_tangent   su     ,,2r2U3q=38+,!GsS1W}%"HAvQa''r.   c                j    d}d}| j                  |      D ]  }|||j                  |      z  }|} |S )u_   Returns estimated length of Bèzier-curve as approximation by line
        `segments`.
        rH   N)rF   rL   )r*   rB   length
prev_pointr>   s        r"   approximated_lengthzBezier4P.approximated_length   sO     
%%h/ 	E%*--e44J	 r.   c                P    t        t        t        | j                                    S )u>   Returns a new Bèzier-curve with reversed control point order.)r   listreversedr4   )r*   s    r"   reversezBezier4P.reverse   s    Xd&9&9:;<<r.   c                    t        j                  | j                        }t        t	        |j                  |                  S )zGeneral transformation interface, returns a new :class:`Bezier4p`
        curve as a 3D curve.

        Args:
             m: 4x4 transformation :class:`Matrix44`

        )r   generater4   r   r)   transform_vertices)r*   mr+   s      r"   	transformzBezier4P.transform   s3     MM$"5"56	a229=>??r.   N)r+   Sequence[T])returnrq   )r:   floatrr   r   )rB   intrr   Iterator[T])r   )rL   rs   rB   rt   rr   ru   )   )rB   rt   rr   rs   )rr   zBezier4P[T])ro   r   rr   zBezier4P[Vec3])r'   
__module____qualname____doc__	__slots__r-   propertyr4   r;   r>   rF   rY   r=   r8   rg   rk   rp   r   r.   r"   r   r   !   sW     /IS = =
*	("0*d7
(
=	@r.   r   rs   c              #    K   t        |       }t        |      }t        ||      }t        |      dk  ry|}t	        j
                  |      t        j                  z  }t	        j
                  ||z         }||kD  r|t        j                  z  }||kD  rt        |||      D ]$  }|D 	cg c]
  }	||	|z  z    }
}	t        |
       & yc c}	w w)u  Returns an approximation for a circular 2D arc by multiple cubic
    Bézier-curves.

    Args:
        center: circle center as :class:`Vec3` compatible object
        radius: circle radius
        start_angle: start angle in degrees
        end_angle: end angle in degrees
        segments: count of Bèzier-curve segments, at least one segment for each
            quarter (90 deg), 1 for as few as possible.

    &.>N)	r   rs   r   absrI   radianstaur   r   )centerradiusstart_angle	end_anglerB   center_
angle_spansr4   r    r+   s              r"   r   r      s     & LG6]F*;	BJ
:A,,q/DHH,KQ^,I
	
!TXX	 	
! 6k9hW "5CDWF
+D	Dy!!"Ds   BCC&C5Crc   PI_2c              #  ,   K    j                   }t        |      dk  ry j                  t        j                  z  }||z   }||kD  r|t        j                  z  }||kD  rd fd}t        |||      D ]  }t        t         ||                     yw)uM  Returns an approximation for an elliptic arc by multiple cubic
    Bézier-curves.

    Args:
        ellipse: ellipse parameters as :class:`~ezdxf.math.ConstructionEllipse`
            object
        segments: count of Bèzier-curve segments, at least one segment for each
            quarter (π/2), 1 for as few as possible.

    r}   Nc              3     K   t        j                        }j                  }j                  }| D ]&  }|||j                  z  z   ||j
                  z  z    ( y wr   )r   r   
major_axis
minor_axisxy)pointsr   x_axisy_axisr    ellipses        r"   rp   z,cubic_bezier_from_ellipse.<locals>.transform  sZ     gnn%)))) 	7A6ACC<'&133,66	7s   AA)r   zIterable[Vec3]rr   zIterator[Vec3])
param_spanr~   start_paramrI   r   r   r   r)   )r   rB   r   r   r   rp   r+   s   `      r"   r   r      s       **J
: ,,txx7K"Z/I
	
!TXX	 	
!7 1iR 4	uYy12334s   AB5BgUUUUUU?gI`Q?c              #  $  K   |dk  rt        d      || z
  }|dkD  r4t        t        j                  |t        j                  z  dz        |      }nt        d      ||z  }t
        t        j                  |dz        z  }| }t        j                  |      }t        |      D ]j  }	|}
||z  }t        j                  |      }|
|
j                   |z  |
j                  |z  fz   }||j                  |z  |j                   |z  fz   }|
|||f l yw)u  Yields cubic Bézier-curve parameters for a circular 2D arc with center
    at (0, 0) and a radius of 1 in the form of [start point, 1. control point,
    2. control point, end point].

    Args:
        start_angle: start angle in radians
        end_angle: end angle in radians (end_angle > start_angle!)
        segments: count of Bèzier-curve segments, at least one segment for each
            quarter (π/2)

    r
   z!Invalid argument segments (>= 1).r   rc   z3Delta angle from start- to end angle has to be > 0.rb   N)r%   maxrI   ceilpiTANGENT_FACTORtanr   
from_anglerA   r   r   )r   r   rB   delta_angle	arc_countsegment_angletangent_lengthanglerT   r\   rR   control_point_1control_point_2s                r"   r   r   )  s(     !|<=="[0KQ		+"7#"=>I	NOO&2M*TXXmc6I-JJNEooe,I9 GOOE*	%]]N^+MMN*)
 
 $KK.([[L>)'
 
 ?OYFFGs   DD))r   r   r   r
   r   ih  r
   )r   r   r   rs   r   rs   r   rs   rB   rt   rr   Iterator[Bezier4P[Vec3]])r
   )r   z'ConstructionEllipse'rB   rt   rr   r   )r   rs   r   rs   rB   rt   rr   z'Iterator[tuple[Vec3, Vec3, Vec3, Vec3]])!
__future__r   typingr   r   r   r   r   r	   rI   _vectorr   r   	_matrix44r   
_constructr   
ezdxf.mathr   ezdxf.math.ellipser   __all__r   r   r   r   r   __annotations__r   DEFAULT_TANGENT_FACTOROPTIMIZED_TANGENT_FACTORr   r   r   r.   r"   <module>r      s#   #      *6 Ctr@wqz r@l !"!"!" !" 	!"
 !" !"H ggme  564"4.144H # -  ( ;<'G'G#('G47'G,'Gr.   