
    OgW                   ^   d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
 ddlZddlZddlmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ erdd
lmZmZmZm Z  g dZ!	 d8	 	 	 	 	 d9dZ"d:dZ#	 	 	 d;	 	 	 	 	 	 	 	 	 d<dZ$	 	 d=	 	 	 	 	 	 	 d>dZ%d?dZ&d@dAdZ'dAdZ(dBdZ)dCdDdZ*dCdDdZ+	 dE	 	 	 	 	 	 	 dFdZ,dGdZ-dGdZ.dGdZ/dGdZ0dGdZ1dHdZ2	 dI	 	 	 	 	 	 	 	 	 dJdZ3	 dI	 	 	 	 	 	 	 	 	 	 	 	 	 dKdZ4	 	 	 	 dLd Z5	 	 	 	 	 	 	 	 	 	 dMd!Z6	 	 	 	 	 	 dNd"Z7 G d# d      Z8dOd$Z9	 	 dP	 	 	 	 	 	 	 dQd%Z:	 	 dP	 	 	 	 	 	 	 dQd&Z;	 	 	 	 	 dR	 	 	 	 	 	 	 	 	 	 	 dSd'Z<ejz                  d(z  Z>	 dT	 	 	 	 	 dUd)Z?dTdVd*Z@dWd+ZA	 	 	 	 	 	 	 	 	 	 dXd,ZBe
dYd-       ZCdZd.ZD	 	 	 	 d[d/ZEd0d1d2d3	 	 	 	 	 d\d4ZF G d5 d6      ZGd]d7ZHy)^aG  
B-Splines
=========

https://www.cl.cam.ac.uk/teaching/2000/AGraphHCI/SMEG/node4.html

Rational B-splines
==================

https://www.cl.cam.ac.uk/teaching/2000/AGraphHCI/SMEG/node5.html:

"The NURBS Book" by Les Piegl and Wayne Tiller

https://books.google.at/books/about/The_NURBS_Book.html?id=7dqY5dyAwWkC&redir_esc=y

    )annotations)IterableIteratorSequenceTYPE_CHECKINGOptionalAnyno_type_checkN)	Vec3UVecNULLVECBasis	Evaluatorcreate_t_vectorestimate_end_tangent_magnitudedistance_point_line_3darc_angle_span_deg   )BoundingBox)linalg)DXFValueError)ConstructionArcConstructionEllipseMatrix44Bezier4P)fit_points_to_cad_cvglobal_bspline_interpolation!local_cubic_bspline_interpolationrational_bspline_from_arcrational_bspline_from_ellipsefit_points_to_cubic_bezieropen_uniform_bsplineclosed_uniform_bsplineBSpline*unconstrained_global_bspline_interpolation)global_bspline_interpolation_end_tangentscad_fit_point_interpolation.global_bspline_interpolation_first_derivatives/local_cubic_bspline_interpolation_from_tangentsknots_from_parametrizationaveraged_knots_unconstrainedaveraged_knots_constrainednatural_knots_unconstrainednatural_knots_constraineddouble_knotsrequired_knot_valuesuniform_knot_vectoropen_uniform_knot_vectorrequired_fit_pointsrequired_control_pointsr$   c                V   t        j                  |       }t        |      dk  rt        d      |t	        |      \  }}t        |d|      S t        j                  |      }t        |d      \  }}|d   j                  |      }|d   j                  |      }	t        |d	||	fd
      S )a  Returns a cubic :class:`BSpline` from fit points as close as possible
    to common CAD applications like BricsCAD.

    There exist infinite numerical correct solution for this setup, but some
    facts are known:

    - CAD applications use the global curve interpolation with start- and end
      derivatives if the end tangents are defined otherwise the equation system will
      be completed by setting the second derivatives of the start and end point to 0,
      for more information read this answer on stackoverflow: https://stackoverflow.com/a/74863330/6162864
    - The degree of the B-spline is always 3 regardless which degree is stored in the
      SPLINE entity, this is only valid for B-splines defined by fit points
    - Knot parametrization method is "chord"
    - Knot distribution is "natural"

    Args:
        fit_points: points the spline is passing through
        tangents: start- and end tangent, default is autodetect

       two or more points required    orderknotschordmethodr      )degreetangentsr>   )	r   listlen
ValueErrorr'   r$   r   	normalizer   )

fit_pointsrB   pointscontrol_pointsr;   tm1m2start_tangentend_tangents
             W/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/math/bspline.pyr   r   Z   s    2 YYz"F
6{Q788 ;F C~Qe<<		(A+F7CFBaDNN2&MB%//"%K'-	     c                    t        j                  |       }t        |      dk  rt        d      ddlm}m}  ||      } ||      S )u  Returns a cubic :class:`BSpline` from fit points **without** end
    tangents.

    This function uses the cubic Bèzier interpolation to create multiple Bèzier
    curves and combine them into a single B-spline, this works for short simple
    splines better than the :func:`fit_points_to_cad_cv`, but is worse
    for longer and more complex splines.

    Args:
        fit_points: points the spline is passing through

    r6   r7   r   )cubic_bezier_interpolationbezier_to_bspline)r   rC   rD   rE   
ezdxf.mathrR   rS   )rG   rH   rR   rS   bezier_curvess        rO   r!   r!      sA     YYz"F
6{Q788H.v6M]++rP   c                   t        j                  |       }t        |      }|dz   }|r|dz  }||kD  r|t        d|       t        t	        ||            }|dz  rdnd}|qt        j                  |      }	t        |	      dk(  rt        ||	d   |	d   |||      \  }
}nEt        |	      t        |      k(  rt        ||	||      \  }
}nt        d      t        ||||      \  }
}t        |
||      }|S )	a  `B-spline`_ interpolation by the `Global Curve Interpolation`_.
    Given are the fit points and the degree of the B-spline.
    The function provides 3 methods for generating the parameter vector t:

    - "uniform": creates a uniform t vector, from 0 to 1 evenly spaced, see
      `uniform`_ method
    - "chord", "distance": creates a t vector with values proportional to the
      fit point distances, see `chord length`_ method
    - "centripetal", "sqrt_chord": creates a t vector with values proportional
      to the fit point sqrt(distances), see `centripetal`_ method
    - "arc": creates a t vector with values proportional to the arc length
      between fit points.

    It is possible to constraint the curve by tangents, by start- and end
    tangent if only two tangents are given or by one tangent for each fit point.

    If tangents are given, they represent 1st derivatives and should be
    scaled if they are unit vectors, if only start- and end tangents given the
    function :func:`~ezdxf.math.estimate_end_tangent_magnitude` helps with an
    educated guess, if all tangents are given, scaling by chord length is a
    reasonable choice (Piegl & Tiller).

    Args:
        fit_points: fit points of B-spline, as list of :class:`Vec3` compatible
            objects
        tangents: if only two vectors are given, take the first and the last
            vector as start- and end tangent constraints or if for all fit
            points a tangent is given use all tangents as interpolation
            constraints (optional)
        degree: degree of B-spline
        method: calculation method for parameter vector t

    Returns:
        :class:`BSpline`

    r   r6   z$More fit points required for degree naturalaverager   zoInvalid count of tangents, two tangents as start- and end tangent constrains or one tangent for each fit point.r9   )	r   rC   rD   rE   r   r&   r(   r%   r$   )rG   rA   rB   r>   _fit_pointscountr:   t_vectorknot_generation_method	_tangentsrI   r;   bsplines                rO   r   r      s0   T ))J'KE!E
u})?xHIIOK89H*01*Y)IIh'	y>Q$M!!&%!NE ^s;// ?Y
 H 
 !K+A!
 nE?GNrP   c                    ddl m} t        j                  |       }|rt        j                  |      }n	 |||      }t	        ||      \  }}t        |d|      S )a  `B-spline`_ interpolation by 'Local Cubic Curve Interpolation', which
    creates B-spline from fit points and estimated tangent direction at start-,
    end- and passing points.

    Source: Piegl & Tiller: "The NURBS Book" - chapter 9.3.4

    Available tangent estimation methods:

    - "3-points": 3 point interpolation
    - "5-points": 5 point interpolation
    - "bezier": cubic bezier curve interpolation
    - "diff": finite difference

    or pass pre-calculated tangents, which overrides tangent estimation.

    Args:
        fit_points: all B-spline fit points as :class:`Vec3` compatible objects
        method: tangent estimation method
        tangents: tangents as :class:`Vec3` compatible objects (optional)

    Returns:
        :class:`BSpline`

    r   )estimate_tangentsr8   r9   )parametrizer`   r   rC   r)   r$   )rG   r>   rB   r`   rY   r]   rI   r;   s           rO   r   r      sW    : /))J'KIIh'	%k6:	KYNE >%88rP   c                    t        |      }t        |       dz
  }|dz
  }d|cxk  r|dz   k  st        d       t        d      ||z   dz   S )uv  Returns the count of required knot-values for a B-spline of `order` and
    `count` control points.

    Args:
        count: count of control points, in text-books referred as "n + 1"
        order: order of B-Spline, in text-books referred as "k"

    Relationship:

    "p" is the degree of the B-spline, text-book notation.

    - k = p + 1
    - 2 ≤ k ≤ n + 1

    r   r6   zInvalid count/order combination)intr   )rZ   r:   knps        rO   r0   r0     s_      	E
AE
QA	AAq1u=>> =>>q519rP   c                (    |r| dz  } t        | d      S )zReturns the count of required fit points to calculate the spline
    control points.

    Args:
        order: spline order (degree + 1)
        tangents: start- and end tangent are given or estimated

    r6   max)r:   rB   s     rO   r3   r3   7  s      	
ua=rP   c                    t        | d      S )zReturns the required count of control points for a valid B-spline.

    Args:
        order: spline order (degree + 1)

    Required condition: 2 <= order <= count, therefore:  count >= order

    r6   rh   )r:   s    rO   r4   r4   I  s     ua=rP   c                R    | d   }| d   |z
  }| D cg c]
  }||z
  |z   c}S c c}w )z(Normalize knot vector into range [0, 1].r   r?    )r;   min_valmax_valvs       rO   normalize_knotsrp   U  s6    AhGBi'!G-23Q[G#333s   $c                v    |rt        | |z   dz
        }nd}t        | |z         D cg c]  }||z  	 c}S c c}w )a  Returns an uniform knot vector for a B-spline of `order` and `count`
    control points.

    `order` = degree + 1

    Args:
        count: count of control points
        order: spline order
        normalize: normalize values in range [0, 1] if ``True``

    r         ?)floatrange)rZ   r:   rF   	max_value
knot_values        rO   r1   r1   \  sA     %%-!+,		5:55=5IJzJ"JJJs   6c                    | |z
  }|rt        | |z
  dz         dg|z  }ndd|z   g|z  }dg|z  }|j                  fdt        |      D               |j                  |       |S )a  Returns an open (clamped) uniform knot vector for a B-spline of `order`
    and `count` control points.

    `order` = degree + 1

    Args:
        count: count of control points
        order: spline order
        normalize: normalize values in range [0, 1] if ``True``

    r   rr           c              3  .   K   | ]  }d |z   z    yw)rr   Nrl   ).0ro   ru   s     rO   	<genexpr>z+open_uniform_knot_vector.<locals>.<genexpr>  s     91#'Y&9   )rs   extendrt   )rZ   r:   rF   rd   tailr;   ru   s         @rO   r2   r2   o  sv     	A%%-!+,	uu}	ay5 EEME	LL9a99	LLLrP   c                v   t        |dz         }|| dz   kD  rt        d      |D cg c]  }t        |       }}|d   dk7  st        j                  |d   d      st        d      |dk(  r|rt        | ||      S t        | ||      S |d	k(  r|rt        | ||      S t        | ||      S t        d
|       c c}w )a  Returns a 'clamped' knot vector for B-splines. All knot values are
    normalized in the range [0, 1].

    Args:
        n: count fit points - 1
        p: degree of spline
        t: parametrization vector, length(t_vector) == n, normalized [0, 1]
        method: "average", "natural"
        constrained: ``True`` for B-spline constrained by end derivatives

    Returns:
        List of n+p+2 knot values as floats

    r   z2Invalid n/p combination, more fit points required.r   rx   r?   rr   z.Parametrization vector t has to be normalized.rX   rW   z Unknown knot generation method: )
rc   r   rs   mathiscloserE   r,   r+   r.   r-   )re   rf   rJ   r>   constrainedr:   ro   s          rO   r*   r*     s    " AJEAPQQaqAts{$,,quc2IJJ  'q!Q/	
 .aA6	

 
9	  &aA.	
 -Q15	
 ;F8DEE# 	s   B6c                   d   dk(  sJ t        j                  d   d      sJ dgdz   z  }|j                  fdt        d| z
  dz         D               |d   dkD  rt	        d      |j                  dgdz   z         |S )zReturns an averaged knot vector from parametrization vector `t` for an
    unconstrained B-spline.

    Args:
        n: count of control points - 1
        p: degree
        t: parametrization vector, normalized [0, 1]

    r   rx   r?   rr   r   c              3  F   K   | ]  }t        ||z          z    y wNsumrz   jrf   rJ   s     rO   r{   z/averaged_knots_unconstrained.<locals>.<genexpr>  s%     D1Qq1q5\"Q&Ds   !z!Normalized [0, 1] values required)r   r   r}   rt   rE   re   rf   rJ   r;   s    `` rO   r+   r+     s     Q43;;<<"s###EQUOE	LLDaQ0CDDRy3<==	LL#!a%!LrP   c                    d   dk(  sJ t        j                  d   d      sJ dgdz   z  }|j                  fdt        | z
        D               |j                  dgdz   z         |S )zReturns an averaged knot vector from parametrization vector `t` for a
    constrained B-spline.

    Args:
        n: count of control points - 1
        p: degree
        t: parametrization vector, normalized [0, 1]

    r   rx   r?   rr   r   c              3  L   K   | ]  }t        ||z   d z
         z    yw)r   Nr   r   s     rO   r{   z-averaged_knots_constrained.<locals>.<genexpr>  s*     AqQq1q519%&*As   !$)r   r   r}   rt   r   s    `` rO   r,   r,     sq     Q43;;<<"s###EQUOE	LLAE!a%LAA	LL#!a%!LrP   c                    |d   dk(  sJ t        j                  |d   d      sJ dg|dz   z  }|j                  |d| |z
  dz           |j                  dg|dz   z         |S )zReturns a 'natural' knot vector from parametrization vector `t` for an
    unconstrained B-spline.

    Args:
        n: count of control points - 1
        p: degree
        t: parametrization vector, normalized [0, 1]

    r   rx   r?   rr   r   r6   r   r   r}   r   s       rO   r-   r-     q     Q43;;<<"s###EQUOE	LL1q1uqy!"	LL#!a%!LrP   c                    |d   dk(  sJ t        j                  |d   d      sJ dg|dz   z  }|j                  |d| |z
  dz           |j                  dg|dz   z         |S )zReturns a 'natural' knot vector from parametrization vector `t` for a
    constrained B-spline.

    Args:
        n: count of control points - 1
        p: degree
        t: parametrization vector, normalized [0, 1]

    r   rx   r?   rr   r   r   r   s       rO   r.   r.     r   rP   c                   |d   dk(  sJ t        j                  |d   d      sJ dg|dz   z  }d}g }|dd D ]  }|dk(  r)|j                  ||z   dz         |j                  |       nN|dk(  r|j                  |dz         n4|j                  d|z  |z   dz         |j                  |d|z  z   dz         |} |j                  |d	| dz  |z
          |j                  |d
   dz   dz         |j                  dg|dz   z         |S )a0  Returns a knot vector from parametrization vector `t` for B-spline
    constrained by first derivatives at all fit points.

    Args:
        n: count of fit points - 1
        p: degree of spline
        t: parametrization vector, first value has to be 0.0 and last value has
            to be 1.0

    r   rx   r?   rr   r   r6          @      @N)r   r   appendr}   )re   rf   rJ   uprev_tu1t1s          rO   r/   r/      s!    Q43;;<<"s###	QAF	B"g 6IIv{c)*IIbM}		"q&! 		1v:?c12		6AF?c12 HHR!a%!)_HHaeckS !HHcUa!e_HrP   c                L   t        | t        j                        st        j                  |       } | j                  dk  rt        j                  | j
                        S t        j                  | d      \  }}t        j                  | ||      }t        j                  |||      S )zmReturns best suited linear equation solver depending on matrix configuration and
    python interpreter.
       F)	check_all)	
isinstancer   MatrixnrowsNumpySolvermatrixdetect_banded_matrixcompact_banded_matrixBandedMatrixLU)r   rA   rK   rL   As        rO   _get_best_solverr   &  s     ffmm,v&||b!!&--00 ,,VuEB((R8$$QB//rP   c                   t        t        |       dz
  |||d      }t        ||dz   t        |             }t        |D cg c]  }|j	                  |       c}|      }t        j                  | t
        j                        }|j                  |      }	t        j                  |	j                               |fS c c}w )ar  Interpolates the control points for a B-spline by global interpolation
    from fit points without any constraints.

    Source: Piegl & Tiller: "The NURBS Book" - chapter 9.2.1

    Args:
        fit_points: points the B-spline has to pass
        degree: degree of spline >= 2
        t_vector: parametrization vector, first value has to be 0 and last
            value has to be 1
        knot_generation_method: knot generation method from parametrization
            vector, "average" or "natural"

    Returns:
        2-tuple of control points as list of Vec3 objects and the knot vector
        as list of floats

    r   Fr   r;   r:   rZ   dtype)r*   rD   r   r   basis_vectornparrayfloat64solve_matrixr   rC   rows)
rG   rA   r[   r\   r;   NrJ   solvermat_BrI   s
             rO   r%   r%   =  s    2 'J!E 	E!3z?CA(CQq~~a0CVLFHHZrzz2E((/N99^((*+U22 Ds    B;c                6   t        |       dz
  }|}|dkD  rd}t        |dz   |||d      }t        ||dz   |dz         }	|D 
cg c]  }
|	j                  |
       }}
dg|dz   z  }|j	                  dd	d
g|z          |j	                  d|d	d
gz          | j	                  d|||dz      |z  z         | j	                  d|d
||dz       z
  |z  z         t        ||      }|j                  |       }t        j                  |j                               |fS c c}
w )a  Calculates the control points for a B-spline by global interpolation
    from fit points and the 1st derivative of the start- and end point as constraints.
    These 'tangents' are 1st derivatives and not unit vectors, if an estimation
    of the magnitudes is required use the :func:`estimate_end_tangent_magnitude`
    function.

    Source: Piegl & Tiller: "The NURBS Book" - chapter 9.2.2

    Args:
        fit_points: points the B-spline has to pass
        start_tangent: 1st derivative as start constraint
        end_tangent: 1st derivative as end constrain
        degree: degree of spline >= 2
        t_vector: parametrization vector, first value has to be 0 and last
            value has to be 1
        knot_generation_method: knot generation method from parametrization
            vector, "average" or "natural"

    Returns:
        2-tuple of control points as list of Vec3 objects and the knot vector
        as list of floats

    r   r@   rW   r6   Tr   r   rx         rr   r?   )
rD   r*   r   r   insertr   r   r   rC   r   )rG   rM   rN   rA   r[   r\   re   rf   r;   r   r   r   spacingr   rI   s                  rO   r&   r&   d  s:   > 	J!AAz "+&	Aq(2E 	EQa!e4A'/0!ANN10D0eq1uoGKKD$<')*KKGtTl*+a%A,*:;<b+#Ah*?1)DEFdF+F((4N99^((*+U22 1s   Dc                l   dfd}dfd}t        t        | d            }t        |       dz
  }dt        |dz   |dd	      t	        dz   |dz   
      }|D cg c]  }|j                  |       }}dg|z  }|j                  d |       |z          |j                  d| |       z          | j                  dt        ddd             | j                  dt        ddd             t        |      }	|	j                  |       }
t        j                   |
j                               fS c c}w )a  Calculates the control points for a B-spline by global interpolation
    from fit points without any constraints in the same way as AutoCAD and BricsCAD.

    Source: https://stackoverflow.com/a/74863330/6162864

    Args:
        fit_points: points the B-spline has to pass

    Returns:
        2-tuple of control points as list of Vec3 objects and the knot vector
        as list of floats

    c                 j    dz      } dz      }dz
  z  | z  }|| z  | | |z   z  | |z  z  ||z  gS )z*Returns the coefficients for equation [1].r   r6   rl   )up1up2fr;   rf   s      rO   coefficients1z2cad_fit_point_interpolation.<locals>.coefficients1  s`     AElAElQK#GB#)c	*G
 	
rP   c                     t              dz
  } | z
  dz
     }| z
  dz
     }dz
  z  d|z
  z  }|d|z
  z  | d|z
  |z
  z  d|z
  z  d|z
  z  |d|z
  z  gS )z,Returns the coefficients for equation [n-1].r   r6   rr   r   rD   )mump1ump2r   r;   rf   s       rO   coefficients2z2cad_fit_point_interpolation.<locals>.coefficients2  s     JNQUQYQUQYQK3:&tB#*t#$d
3sTzBt
 	
rP   r<   r   r@   r6   rW   T)r>   r   r   rx   r?   r   )returnlist[float])rC   r   rD   r*   r   r   r   r   r   r   r   )rG   r   r   r[   re   r   r   r   r   r   rI   r;   rf   s              @@rO   r'   r'     s%   "


 OJ89HJ!A	A&	Aq(9$E 	EQa!e4A'/0!ANN10D0eaiGKK=?W,-KKGmo-. aaA'b$q!Q-(dA&F((4N99^((*+U22 1s   !D1c           	     b   dfd|t        |       dz
  }t        ||      }t        |       dz  t        |dz         dgdgdz
  z  z   ddgdgdz
  z  z   g}t        |d         }fd	|dd
 D        D ]$  }|j                  |D 	cg c]  }	|	d| 	 c}	       & |j	                  dgdz
  z  ddgz          |j	                  dgdz
  z  dgz          t        t        d |D                    dk(  sJ d       g }
t        | |      D ]  }|
j                  |        |
d   |
d
   c|
d
<   |
d<   |
dxx   |dz      z  z  cc<   |
dxx   d|dz       z
  z  z  cc<   t        ||      }|j                  |
      }t        j                  |j                               |fS c c}	w )a  Interpolates the control points for a B-spline by a global
    interpolation from fit points and 1st derivatives as constraints.

    Source: Piegl & Tiller: "The NURBS Book" - chapter 9.2.4

    Args:
        fit_points: points the B-spline has to pass
        derivatives: 1st derivatives as constrains, not unit vectors!
            Scaling by chord length is a reasonable choice (Piegl & Tiller).
        degree: degree of spline >= 2
        t_vector: parametrization vector, first value has to be 0 and last
            value has to be 1

    Returns:
        2-tuple of control points as list of Vec3 objects and the knot vector
        as list of floats

    c              3     K   j                  |       }|z
  }z   dz   |z
  }j                  || d      D ]  }dg|z  |z   dg|z  z     y w)Nr   re   rx   )	find_spanbasis_funcs_derivatives)rJ   spanfrontbackbasisr   rZ   rf   s        rO   nbasisz>global_bspline_interpolation_first_derivatives.<locals>.nbasis  sl     {{1~qqy1}t#..tQ!.< 	7E%%-%'3%$,66	7s   AAr   r6   r   rr   rx   r   r   c              3  .   K   | ]  } |        y wr   rl   )rz   rJ   r   s     rO   r{   zAglobal_bspline_interpolation_first_derivatives.<locals>.<genexpr>  s     0AfQi0r|   r?   Nc              3  2   K   | ]  }t        |        y wr   r   )rz   rows     rO   r{   zAglobal_bspline_interpolation_first_derivatives.<locals>.<genexpr>
  s     )3s8)s   zinhomogeneous matrix detectedr   )rJ   rs   )rD   r/   r   r}   r   setzipr   r   r   rC   r   )rG   derivativesrA   r[   re   r;   r   ncolsr   r   Br   r   rI   r   rZ   r   rf   s                 @@@@rO   r(   r(     s   27 	AJ!AAx(E
OaEEQe4A	##	tu	**	A !IE0!B0 -	+##fu++,- HHcUeai D$</0HHcUeai D6)*s)q))*a/P1PP/ AJ, 	 R5!B%LAbE1R5 aDE!a%L1DbEcEAE(O#q((Ea(F((+N99^((*+U22' ,s   F,
c                L   t        |       t        |      k(  sJ t        |       dkD  sJ d}|dz   }| d   g}d}g }t        t        |       dz
        D ]  }| |   }| |dz      }	||   }
||dz      }d|
|z   j                  z
  }d|	|z
  j                  |
|z         z  }d|	|z
  j                  z  }	 t	        j
                  |||      \  }}|||
z  d	z  z   }|	||z  d	z  z
  }|j                  ||f       |d	||z
  j                  z  z  }|j                  |        |j                  | d
          dg|z  }|d
   }|dd
 D ]  }||z  }|j                  ||f        |j                  dgdz         t        |      t        t        |      |      k(  sJ ||fS # t        $ r Y Pw xY w)aG  Interpolates the control points for a cubic B-spline by local
    interpolation from fit points and tangents as unit vectors for each fit
    point. Use the :func:`estimate_tangents` function to estimate end tangents.

    Source: Piegl & Tiller: "The NURBS Book" - chapter 9.3.4

    Args:
        fit_points: curve definition points - curve has to pass all given fit
            points
        tangents: one tangent vector for each fit point as unit vectors

    Returns:
        2-tuple of control points as list of Vec3 objects and the knot vector
        as list of floats

    r6   r@   r   r   rx   g      0@g      (@g      Br   r?   Nrr   r8   )rD   rt   magnitude_squaredotr   quadratic_equationrE   r}   	magnituder   r0   )rG   rB   rA   r:   rI   r   paramsip0p3t0t3abc
alpha_plusalpha_minusp1p2r;   max_uro   knots                          rO   r)   r)     s   & z?c(m+++z?QFQJE m_NAF3z?Q&' ]Aa[a!e_BG---BG==b))R"W...	&,&?&?1a&H#J *r/C''*r/C''r2h'	SBG&&&&a!" *R.)EEME2JECR[ #5ydD\"# 
LL#u:-c..A5IIII5  #  		s   .F	F#"F#c                     e Zd ZdZdZ	 	 	 d-	 	 	 	 	 	 	 d.dZd Zed/d       Zed0d       Z	ed1d       Z
ed0d	       Zed0d
       Zed2d       Zed        Zed        Zed3d4d       Zed5d6d       Zed5d7d       Zed8d       Zed9d       Zed:d       Zd:dZd;dZd;dZd<d=dZd>dZd?d@dZdAdZdBdZdCdDdZdCdEdZ dFdZ!dFdZ"dGd Z#dHd!Z$dId"Z%	 dJ	 	 	 	 	 dKd#Z&dLdMd$Z'dNd%Z(d&d'd(d)	 	 	 dOd*Z)dPdQd+Z*dRd,Z+y)Sr$   a  B-spline construction tool.

    Internal representation of a `B-spline`_ curve. The default configuration
    of the knot vector is a uniform open `knot`_ vector ("clamped").

    Factory functions:

        - :func:`fit_points_to_cad_cv`
        - :func:`fit_points_to_cubic_bezier`
        - :func:`open_uniform_bspline`
        - :func:`closed_uniform_bspline`
        - :func:`rational_bspline_from_arc`
        - :func:`rational_bspline_from_ellipse`
        - :func:`global_bspline_interpolation`
        - :func:`local_cubic_bspline_interpolation`

    Args:
        control_points: iterable of control points as :class:`Vec3` compatible
            objects
        order: spline order (degree + 1)
        knots: iterable of knot values
        weights: iterable of weight values

    )_control_points_basis_clampedNc                   t        j                  |      | _        t        | j                        }t	        |      }||kD  rt        d| d| d|       |t        ||d      }nKt        |      }||z   }t        |      |k7  rt        | dt        |       d      |d   d	k7  rt        |      }t        ||||
      | _
        t        t        |d |             dk(  xr t        t        || d              dk(  | _        y )Nzgot z control points, need z or more for order of TrF   z knot values required, got .r   rx   )weightsr   )r   tupler   rD   rc   r   r2   rE   rp   r   r   r   r   )selfrI   r:   r;   r   rZ   required_knot_counts          rO   __init__zBSpline.__init__q  s     $zz.9D(()E
5=ug3E7:PQVPWX  =,UETJE%LE"'%-5z00 *++Fs5zlRST  Qx3'.E5%ACfu./14VSvw=P9QUV9VrP   c                    d| j                    d| j                   dt        | j                                dt        | j	                                d	S )NzBSpline degree=z, z control points, z knot values, z weights)rA   rZ   rD   r;   r   r   s    rO   __str__zBSpline.__str__  sL    dkk]"TZZL 9"4::<014<<>"#8-	
rP   c                    | j                   S )z4Control points as tuple of :class:`~ezdxf.math.Vec3`)r   r   s    rO   rI   zBSpline.control_points  s     ###rP   c                ,    t        | j                        S )z7Count of control points, (n + 1 in text book notation).)rD   r   r   s    rO   rZ   zBSpline.count  s     4''((rP   c                .    | j                   j                  S )zBiggest `knot`_ value.)r   max_tr   s    rO   r   zBSpline.max_t       {{   rP   c                .    | j                   j                  S )zOrder (k) of B-spline = p + 1)r   r:   r   s    rO   r:   zBSpline.order  r   rP   c                .    | j                   j                  S )z"Degree (p) of B-spline = order - 1)r   rA   r   s    rO   rA   zBSpline.degree  s     {{!!!rP   c                B    t        | j                  | j                        S r   )r   r   r   r   s    rO   	evaluatorzBSpline.evaluator  s    d&:&:;;rP   c                .    | j                   j                  S )z?Returns ``True`` if curve is a rational B-spline. (has weights))r   is_rationalr   s    rO   r  zBSpline.is_rational  s     {{&&&rP   c                    | j                   S )z7Returns ``True`` if curve is a clamped (open) B-spline.)r   r   s    rO   
is_clampedzBSpline.is_clamped  s     }}rP   c                    t        | ||      S )z/Returns :class:`BSpline` defined by fit points.r=   )r   )rH   rA   r>   s      rO   from_fit_pointszBSpline.from_fit_points  s     ,FF6JJrP   c                X    t        | j                  | j                  |            d      S )zaReturns an ellipse approximation as :class:`BSpline` with `num`
        control points.

        r6   rA   )r   verticesr   )ellipsenums     rO   ellipse_approximationzBSpline.ellipse_approximation  s*     ,W^^C01!
 	
rP   c                X    t        | j                  | j                  |            d      S )z]Returns an arc approximation as :class:`BSpline` with `num`
        control points.

        r6   r  )r   r  angles)arcr  s     rO   arc_approximationzBSpline.arc_approximation  s#     ,CLLC,IRSTTrP   c                    t        | d      S )zoReturns the ellipse as :class:`BSpline` of 2nd degree with as few
        control points as possible.

        r   segments)r    )r  s    rO   from_ellipsezBSpline.from_ellipse  s     -WqAArP   c                r    t        | j                  | j                  | j                  | j                  d      S )zkReturns the arc as :class:`BSpline` of 2nd degree with as few control
        points as possible.

        r   r  )r   centerradiusstart_angle	end_angle)r  s    rO   from_arczBSpline.from_arc  s,     )JJ

COOS]]Q
 	
rP   c                p    t        | j                  | j                  | j                  | j                        S )zInterface to the `NURBS-Python <https://pypi.org/project/geomdl/>`_
        package.

        Returns a :class:`BSpline` object from a :class:`geomdl.BSpline.Curve`
        object.

        rI   r:   r;   r   )r$   ctrlptsr:   
knotvectorr   )curves    rO   from_nurbs_python_curvezBSpline.from_nurbs_python_curve  s/      ==++""MM	
 	
rP   c           	           fd} j                  t         j                         j                   |        j                  rt         j                                     S d      S )zZReturns a new :class:`BSpline` object with reversed control point
        order.

        c               3  j   K   t        t        j                                     D ]	  } d| z
    y w)Nrr   )reversedrp   r;   )rd   r   s    rO   reverse_knotsz&BSpline.reverse.<locals>.reverse_knots  s/     odjjl;< Ags   03Nr   )	__class__r'  rI   r:   r  r   )r   r(  s   ` rO   reversezBSpline.reverse  sf    	 ~~#D$7$78**/040@0@HT\\^,	  
 	
 GK	  
 	
rP   c                .    | j                   j                  S )zReturns a tuple of `knot`_ values as floats, the knot vector
        **always** has order + count values (n + p + 2 in text book notation).

        )r   r;   r   s    rO   r;   zBSpline.knots  s    
 {{   rP   c                .    | j                   j                  S )zlReturns a tuple of weights values as floats, one for each control
        point or an empty tuple.

        )r   r   r   s    rO   r   zBSpline.weights  s    
 {{"""rP   c                V    | j                   j                  | j                  |            S )ziApproximates curve by vertices as :class:`Vec3` objects, vertices
        count = segments + 1.

        )r  rH   r   r   r  s     rO   approximatezBSpline.approximate  s"    
 ~~$$T[[%:;;rP   c                    | j                         }|| j                  dz
     }|| j                     }t        j                  |||dz         S )z7Yield evenly spaced parameters for given segment count.r   )r;   r:   rZ   r   linspace)r   r  r;   lower_boundupper_bounds        rO   r   zBSpline.params  sF     

DJJN+DJJ'{{;X\BBrP   c              #    K   dfd| j                   t        j                  t        j                  | j	                                     }t        j
                  |      }|d   }j                  |      }| |dd D ][  }||z
  |z  }||k  s||z   }	t        j                  |	|      r|}	j                  |	      }
 ||
||	      E d{    |	}|
}||k  rK] y7 w)a  Adaptive recursive flattening. The argument `segments` is the
        minimum count of approximation segments between two knots, 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 projected curve point onto the
                segment chord.
            segments: minimum segment count between two knots

        c              3     K   ||z   dz  }j                  |      }	 t        || |      }|k  r| y  	| |||      E d {     	||||      E d {    y # t        $ r d}Y >w xY w7 )7 w)Ng      ?r   )pointr   ZeroDivisionError)
sestart_tend_tmid_tr   _distdistancer  subdivs
          rO   r?  z"BSpline.flattening.<locals>.subdiv0  s     u_+E&A.q!Q7 x!!Q777!!Qu555 % 
 85sE   A1A A1A-A1A/A1A*'A1)A**A1/A1r   r   N)r8  r   r9  r   r:  rs   r;  rs   )r  r   uniquer   r;   r   r6  r   )r   r>  r  r;   seg_f64rJ   start_pointr   deltanext_t	end_pointr  r?  s    `         @@rO   
flatteningzBSpline.flattening#  s     	6 NN			"((4::<01**X&!Hooa() 		(B!Vw&Eb&U::fb)F%OOF3	!+y!VDDD' b&		( Es   BC%<C%C#C% C%c                8    | j                   j                  |      S )zgReturns point  for parameter `t`.

        Args:
            t: parameter in range [0, max_t]

        )r  r6  r   rJ   s     rO   r6  zBSpline.pointN  s     ~~##A&&rP   c                8    | j                   j                  |      S )znYields points for parameter vector `t`.

        Args:
            t: parameters in range [0, max_t]

        )r  rH   rH  s     rO   rH   zBSpline.pointsW  s     ~~$$Q''rP   c                :    | j                   j                  ||      S )a8  Return point and derivatives up to `n` <= degree for parameter `t`.

        e.g. n=1 returns point and 1st derivative.

        Args:
            t: parameter in range [0, max_t]
            n: compute all derivatives up to n <= degree

        Returns:
            n+1 values as :class:`Vec3` objects

        )r  
derivativer   rJ   re   s      rO   rK  zBSpline.derivative`  s     ~~((A..rP   c                :    | j                   j                  ||      S )aQ  Yields points and derivatives up to `n` <= degree for parameter
        vector `t`.

        e.g. n=1 returns point and 1st derivative.

        Args:
            t: parameters in range [0, max_t]
            n: compute all derivatives up to n <= degree

        Returns:
            List of n+1 values as :class:`Vec3` objects

        )r  r   rL  s      rO   r   zBSpline.derivativeso  s     ~~))!Q//rP   c                D   | j                   j                  r| j                        S t        | j                   j                        t        | j
                        | j                  dfd}dk  s| j                  k\  rt        d      | j                   j                        }|k  rt        d      t        |z
  dz   |dz         D cg c]
  } ||       c}|z
  dz   | j                  |dz          t        | j                        S c c}w )zInsert an additional knot, without altering the shape of the curve.
        Returns a new :class:`BSpline` object.

        Args:
            t: position of new knot 0 < t < max_t

        c                `    |    z
  | z      |    z
  z  }| dz
     d|z
  z  |    |z  z   S Nr   rl   )indexr   cpointsr;   rf   rJ   s     rO   	new_pointz&BSpline.insert_knot.<locals>.new_point  sN    U5\!eEAI&6u&EFA519%Q/'%.12DDDrP   rx   Invalid position tr   )rQ  rc   r   r   )r   r  _insert_knot_rationalrC   r;   r   rA   r   r   r   rt   r   r$   r:   )r   rJ   rS  rd   r   rR  r;   rf   s    `   @@@rO   insert_knotzBSpline.insert_knot  s    ;;""--a00T[[&&'t++,KK	E 	E 8qDJJ 455KK!!!$q5 4558=a!eaiQ8O!P1)A,!PA	AQUAw

E22 "Qs   Dc                d  	
 | j                   j                  st        d      t        | j                   j                        	t        |       | j                  
d	
fd}dk  s| j                  k\  rt        d      | j                   j                        }|
k  rt        d      j                         }t        |
z
  dz   |dz         D cg c]
  } ||       c}||
z
  dz   | t        |      \  }}	j                  |dz          t        || j                  	|      S c c}w )z&Knot insertion for rational B-splines.zRequires a rational B-splines.c                `    |    z
  | z      |    z
  z  }| dz
     d|z
  z  |    |z  z   S rP  rl   )rQ  r   	hg_pointsr;   rf   rJ   s     rO   rS  z0BSpline._insert_knot_rational.<locals>.new_point  sO    E%L(U519-=e-LMAUQY'1q51Ie4Dq4HHHrP   rx   rT  r   r;   r   )rQ  rc   r   np.typing.NDArray)r   r  	TypeErrorrC   r;   to_homogeneous_pointsrA   r   r   r   tolistrt   from_homogeneous_pointsr   r$   r:   )r   rJ   rS  rd   
new_pointsr   rH   r   rY  r;   rf   s    `      @@@rO   rU  zBSpline._insert_knot_rational  s   {{&&<==!$++"3"34'<T'B		I 	I 8qDJJ 455&&q)q5 455#**,
;@QAPQE;R$SaYq\$S
1q519q!1*=QUAvtzzHH	 %Ts   D-c                :    | }|D ]  }|j                  |      } |S )zInsert multiple knots, without altering the shape of the curve.
        Returns a new :class:`BSpline` object.

        Args:
            u: vector of new knots t and for each t: 0 < t  < max_t

        )rV  )r   r   splinerJ   s       rO   knot_refinementzBSpline.knot_refinement  s-      	+A''*F	+rP   c                    |j                  | j                        }t        || j                  | j	                         | j                               S )zqReturns a new :class:`BSpline` object transformed by a
        :class:`Matrix44` transformation matrix.

        )transform_verticesrI   r$   r:   r;   r   )r   r   rR  s      rO   	transformzBSpline.transform  s;    
 &&t':':;w

DJJL$,,.IIrP   c              #  h  K   | j                   j                  rt        d      | j                  st        d      | j                  dz
  }| j
                  }t        j                  | j                   j                        }t        j                  | j                        }t        j                  t        |            }||z   dz   }|}|dz   }|d|dz    }	||k  rXt        j                  |dz   df      }
|}||k  rIt        j                  ||dz      ||         r*|dz  }||k  r t        j                  ||dz      ||         r*||z
  dz   }||k  r||   ||   z
  }t        ||d      D ]  }||||z      ||   z
  z  |||z
  dz
  <    ||z
  }t        d|dz         D ]Q  }||z
  }||z   }t        ||dz
  d      D ]$  }|||z
     }|	|   |z  |	|dz
     d|z
  z  z   |	|<   & ||k  sJ|	|   |
|<   S t        j                  |	       ||k  r|||z
  |dz    |
||z
  |dz    |}|dz  }|
}	||k  rWy	y	w)
u  Decompose a non-rational B-spline into multiple Bézier curves.

        This is the preferred method to represent the most common non-rational
        B-splines of 3rd degree by cubic Bézier curves, which are often supported
        by render backends.

        Returns:
            Yields control points of Bézier curves, each Bézier segment
            has degree+1 control points e.g. B-spline of 3rd degree yields
            cubic Bézier curves of 4 control points.

        z!Rational B-splines not supported.zClamped B-Spline required.r   r   r@   shaper?   rr   N)r   r  r\  r  rZ   rA   r   r   r;   r   zerosrD   r   rt   r   rC   )r   re   rf   r;   rI   alphasr   r   r   bezier_pointsnext_bezier_pointsr   multnumerr   rsaver8  rd   alphas                       rO   bezier_decompositionzBSpline.bezier_decomposition  s     ;;""?@@899JJNKK**+$"6"67#e*%EAIE&q1q51!e!#Q
!;Aa%BJJuQU|U1X>Q a%BJJuQU|U1X>q519Dax$Qx%(2q$+ MA+0E!a%L584K+LF1t8a<(MHq!a% 	DAq5DqA"1a!eR0 * &q1u+8+;e+CmEG 5[G* ,*a(*
 1u3@3C*40	D ))M**1u 8Fa$hQRUVQV7W"1t8a!e4Q 2= !es   EH2B!H2&AH20H2c                    |*t        | j                  | j                  |                  }nt        | j                  |            }ddlm}  ||      S )u  Approximate arbitrary B-splines (degree != 3 and/or rational) by
        multiple segments of cubic Bézier curves. The choice of cubic Bézier
        curves is based on the widely support of this curves by many render
        backends. For cubic non-rational B-splines, which is maybe the most
        common used B-spline, is :meth:`bezier_decomposition` the better choice.

        1. approximation by `level`: an educated guess, the first level of
           approximation segments is based on the count of control points
           and their distribution along the B-spline, every additional level
           is a subdivision of the previous level.

        E.g. a B-Spline of 8 control points has 7 segments at the first level,
        14 at the 2nd level and 28 at the 3rd level, a level >= 3 is recommended.

        2. approximation by a given count of evenly distributed approximation
           segments.

        Args:
            level: subdivision level of approximation segments (ignored if
                argument `segments` is not ``None``)
            segments: absolute count of approximation segments

        Returns:
            Yields control points of cubic Bézier curves as :class:`Bezier4P`
            objects

        r   )rR   )rC   rH   approximation_paramsr/  bezier_interpolationrR   )r   levelr  rH   rR   s        rO   cubic_bezier_approximationz"BSpline.cubic_bezier_approximation  sK    < $++d&?&?&FGHF$**845FD)&11rP   c                   t        t        | j                  d            }t        |      dk(  r|S | j                  dk7  r| j                  }|D cg c]  }||z  	 }}t        |dz
        D ]  }t        t        |            } |S c c}w )a}  Returns an educated guess, the first level of approximation
        segments is based on the count of control points and their distribution
        along the B-spline, every additional level is a subdivision of the
        previous level.

        E.g. a B-Spline of 8 control points has 7 segments at the first level,
        14 at the 2nd level and 28 at the 3rd level.

        r<   r   rr   r   )rC   r   r   rD   r   rt   subdivide_params)r   rw  r   r   rf   _s         rO   ru  zBSpline.approximation_params1  s     od&:&:GDEv;!M::JJE)/0Aa%i0F0uqy! 	4A*623F	4 1s   Bc                    t        | |      S )a/  Returns a new :class:`BSpline` with a t-times elevated degree.

        Degree elevation increases the degree of a curve without changing the shape of the
        curve. This method implements the algorithm A5.9 of the "The NURBS Book" by
        Piegl & Tiller.

        .. versionadded:: 1.4

        )degree_elevationrH  s     rO   r}  zBSpline.degree_elevationE  s      a((rP   :0yE>d      epsilonmax_iterationsinitc               4    t        | t        |      |||      S )aO  Returns the parameter t for a point on the curve that is closest to the input
        point.

        This is an iterative search using Newton's method, so there is no guarantee
        of success, especially for splines with many turns.

        Args:
            point(UVec): point on the curve or near the curve
            epsilon(float): desired precision (distance input point to point on curve)
            max_iterations(int): max iterations for Newton's method
            init(int): number of points to calculate in the initialization phase

        .. versionadded:: 1.4

        r  )point_inversionr   )r   r6  r  r  r  s        rO   r  zBSpline.point_inversionQ  s!    $ $u+w~TX
 	
rP   c                    t        | |      S )zReturns a B-spline measurement tool.

        All measurements are based on the approximated curve.

        Args:
            segments: count of segments for B-spline approximation.

        .. versionadded:: 1.4

        )Measurementr.  s     rO   measurezBSpline.measureg  s     4**rP   c                    t        | |      S )zSplits the B-spline at parameter `t` and returns two new B-splines.

        Raises:
            ValuerError: t out of range 0 < t < max_t

        .. versionadded:: 1.4

        )split_bsplinerH  s     rO   splitzBSpline.splitt  s     T1%%rP   )r8   NN)rI   Iterable[UVec]r:   rc   r;   Optional[Iterable[float]]r   r  )r   zSequence[Vec3])r   rc   r   rs   )r   r   )r@   r<   )rH   r  r   r$   )   )r  r   r  rc   r   r$   )r  r   r  rc   r   r$   )r  r   r   r$   )r  r   r   r$   )r   r$   )r   Sequence[float])r   )r  rc   r   Iterable[Vec3])r  rc   r   Iterable[float])r8   )r>  rs   r  rc   r   zIterator[Vec3])rJ   rs   r   r   )rJ   r  r   r  )r6   )rJ   rs   re   rc   r   
list[Vec3])rJ   r  re   rc   r   Iterable[list[Vec3]])rJ   rs   r   r$   )r   r  r   r$   )r   r   r   r$   )r   r  )r@   N)rw  rc   r  zOptional[int]r   zIterable[Bezier4P])r@   )rw  rc   r   r  )rJ   rc   r   r$   )r6  r   r   rs   )r  )r  rc   r   r  )rJ   rs   r   tuple[BSpline, BSpline]),__name__
__module____qualname____doc__	__slots__r   r   propertyrI   rZ   r   r:   rA   r  r  r  staticmethodr
  r  r  r  r  r$  r*  r;   r   r/  r   rF  r6  rH   rK  r   rV  rU  rc  rf  rs  rx  ru  r}  r  r  r  rl   rP   rO   r$   r$   U  s   2 :I
 +/-1W&W W )	W
 +W:
 $ $ ) ) ! ! ! ! " " < < ' '   K K 
 
 U U B B 
 
 
 

"!#<C)(V'(/0 3<I6J<3~ 9=$2$2(5$2	$2L(
) '+3Q

	
,+
&rP   c              #     K   t        t        |       dz
        D ]  }| |    | |   | |dz      z   dz    | d    y w)Nr   r   r?   )rt   rD   )rf   r   s     rO   rz  rz    sQ     3q6A: &d
taAh#%%& B%Ks   ?Ac                x    t        j                  |       }t        t        |      |d      }t	        | |||      S )ak  Creates an open uniform (periodic) `B-spline`_ curve (`open curve`_).

    This is an unclamped curve, which means the curve passes none of the
    control points.

    Args:
        control_points: iterable of control points as :class:`Vec3` compatible
            objects
        order: spline order (degree + 1)
        weights: iterable of weight values

    Fr   )r:   r;   r   )r   r   r1   rD   r$   )rI   r:   r   r   r;   s        rO   r"   r"     s6    " jj0OO 4euME>eWMMrP   c                    t        j                  |       }|j                  |d|dz
          |"t        |      }|j                  |d|dz
          t        |||      S )aJ  Creates a closed uniform (periodic) `B-spline`_ curve (`open curve`_).

    This B-spline does not pass any of the control points.

    Args:
        control_points: iterable of control points as :class:`Vec3` compatible
            objects
        order: spline order (degree + 1)
        weights: iterable of weight values

    Nr   )r   rC   r}   r"   )rI   r:   r   r   s       rO   r#   r#     s`      ii/O?;UQY78w-w{+,@@rP   c                     t                t              t        j                  |dz        }|t        j                  t	        ||            z   }t        |||      \  }}}	t         fd|D        ||	d      S )a  Returns a rational B-splines for a circular 2D arc.

    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 spline segments, at least one segment for each
            quarter (90 deg), default is 1, for as few as needed.

    h  c              3  .   K   | ]  }|z  z     y wr   rl   )rz   rf   r  r  s     rO   r{   z,rational_bspline_from_arc.<locals>.<genexpr>  s     F!!f*-Fr|   r@   rI   r   r;   r:   )r   rs   r   radiansr   nurbs_arc_parametersr$   )
r  r  r  r  r  	start_radend_radrI   r   r;   s
   ``        rO   r   r     sx    $ &\F6]F[3./I$,,'9+y'QRRG%9)Wh%W"NGUF~F	 rP   r   c                      j                   t        j                  z  }| j                  z   }d fd}t	        |||      \  }}t         |       ||d      S )u/  Returns a rational B-splines for an elliptic arc.

    Args:
        ellipse: ellipse parameters as :class:`~ezdxf.math.ConstructionEllipse`
            object
        segments: count of spline segments, at least one segment for each
            quarter (π/2), default is 1, for as few as needed.

    c               3     K   t        j                        } j                  }j                  }D ]&  }| ||j                  z  z   ||j
                  z  z    ( y wr   )r   r  
major_axis
minor_axisxy)r  x_axisy_axisrf   rI   r  s       rO   transform_control_pointsz?rational_bspline_from_ellipse.<locals>.transform_control_points  sZ     gnn%#### 	7A6ACC<'&133,66	7s   AAr@   r  )r   r  )start_paramr   tau
param_spanr  r$   )r  r  r  r  r  r   r;   rI   s   `      @rO   r    r      sf     %%0Kg000I7 &:Y&"NGU /1	 rP   c           	        |dk  rt        d      || z
  }t        t        j                  |t        z        |      }||z  }|dz  }t        j
                  |      }t        t        j
                  |       t        j                  |             g}dg}	| }
dt        j
                  |dz        z  }t        |      D ]  }|
|z  }
|j                  t        t        j
                  |
      |z  t        j                  |
      |z               |	j                  |       |
|z  }
|j                  t        t        j
                  |
      t        j                  |
                   |	j                  d        g d}dt        t        |      dz   d      dz
  dz  dz   z  }|}|dk  r|j                  ||f       ||z  }|dk  r|j                  dgt        t        |      d      t        |      z
  z         ||	|fS )	uU  Returns a rational B-spline parameters for a circular 2D arc with center
    at (0, 0) and a radius of 1.

    Args:
        start_angle: start angle in radians
        end_angle: end angle in radians
        segments: count of segments, at least one segment for each quarter (π/2)

    Returns:
        control_points, weights, knots

    r   z!Invalid argument segments (>= 1).r6   rr   r   )rx   rx   rx   r8   r@   )rE   ri   r   ceilPI_2cosr   sinrt   r   rD   r}   r0   )r  r  r  delta_angle	arc_countsegment_anglesegment_angle_2
arc_weightrI   r   angledr{  r;   stepgs                   rO   r  r    s    !|<==k)KDIIkD018<I)+M#a'O/*J 488K0$((;2GHINeGEdhh}s*++A9 	 d488E?Q#6!8KLMz" 	 d488E?DHHUODEs	 E3s>*Q.2Q6#=CDDA
c'aV	T	 c' 
LL#.s>/BAFUSTU7E))rP   c                l     i t                d fd t        |      t        |            S )aC  B-spline basis_vector function.

    Simple recursive implementation for testing and comparison.

    Args:
        u: curve parameter in range [0, max(knots)]
        index: index of control point
        degree: degree of B-spline
        knots: knots vector

    Returns:
        float: basis_vector value N_i,p(u)

    c                V   	 | |f   S # t         $ r |dk(  r|    	cxk  r| dz      k  rn ndnd}nh| |z      |    z
  }|r	|    z
  |z   | |dz
        z  nd}| |z   dz      | dz      z
  }|r!| |z   dz      	z
  |z   | dz   |dz
        z  nd}||z   }|| |f<   |cY S w xY w)Nr   r   rx   )KeyError)
r   rf   retval	dominatorf1f2r   cacher;   r   s
         rO   r   zbspline_basis.<locals>.NB  s    	!Q=  	Av#Ah!:eAEl:!!a%L583	AJa%(li/!Aq1u+=PS!!a%!),uQU|;	 ! 1q519%)Y61q5!a%H  b"E1a&MM!	s   
 BB('B()r   rc   rf   rc   r   rs   )rs   rc   )r   rQ  rA   r;   r   r  s   `  `@@rO   bspline_basisr  0  s4     +-EaA * SZV%%rP   c           	         t        |      ||z   dz   k(  sJ t        |      D cg c]  }t        | |||       }}t        j                  | |d         rd|d<   |S c c}w )a  Create basis_vector vector at parameter u.

    Used with the bspline_basis() for testing and comparison.

    Args:
        u: curve parameter in range [0, max(knots)]
        count: control point count (n + 1)
        degree: degree of B-spline (order = degree + 1)
        knots: knot vector

    Returns:
        list[float]: basis_vector vector, len(basis_vector) == count

    r   r?   rr   )rD   rt   r  r   r   )r   rZ   rA   r;   rQ  r   s         rO   bspline_basis_vectorr  Z  so    " u:%&.1,---<A%L38a.E  ||AuRy!b	Ls   Ac           	        t        |      }|dk  r| S | j                  }| j                  rd}t        |       }n!d}t	        j
                  | j                        }t	        j
                  | j                               }t        |      dz
  }||z   dz   }||z   }|dz  }	t	        j                  t        |      d|z   z  |f      }
t	        j                  |d|z   z        }t	        j                  ||z   dz   |dz   f      }t	        j                  |dz   |f      }t	        j                  ||z   dz   |f      }t	        j                  |dz
  |f      }t	        j                  |dz
        }d|d<   d|||f<   t        j                  }t        d|	dz         D ]Y  }d |||      z  }t        ||      }t        t        d||z
        |dz         D ]   }| |||      z   ||||z
        z  |||f<   " [ t        |	dz   |      D ]A  }t        ||      }t        t        d||z
        |dz         D ]  }|||z
  ||z
  f   |||f<    C |}|dz   }d	}|}|dz   }d}|d   }|d   |
d<   ||d
|dz    |d
|dz    |d
|dz    ||k  r|}||k  rIt        j                   ||   ||dz            r*|dz  }||k  r t        j                   ||   ||dz            r*||z
  dz   }||z   |z   }||   }|}||z
  }|dkD  r	|dz   dz  } nd} |dkD  r||dz   dz  z
  }!n|}!|dkD  r||z
  }"t        ||d	      D ]  }#|"|||#z      |z
  z  ||#|z
  dz
  <    t        d|dz         D ]O  }||z
  }$||z   }%t        ||%dz
  d	      D ](  }#||#|%z
     ||#   z  d||#|%z
     z
  ||#dz
     z  z   ||#<   * ||   ||$<   Q t        | |dz         D ]J  }d||<   t        ||      }t        t        d||z
        |dz         D ]  }||   |||f   ||   z  z   ||<    L |dkD  r|dz
  }&|}'||z
  }(|||dz
     z
  |(z  })t        d|      D ]  }*|&}|'}||z
  dz   }+||z
  |*kD  r||k  r+|||   z
  |||   z
  z  },|,|
|   z  d|,z
  |
|dz
     z  z   |
|<   || k\  rQ||*z
  ||z
  |z   k  r)||||*z
     z
  |(z  }-|-||+   z  d|-z
  ||+dz      z  z   ||+<   n|)||+   z  d|)z
  ||+dz      z  z   ||+<   |dz  }|dz  }|+dz  }+||z
  |*kD  r|&dz  }&|'dz  }' ||k7  r||z
  }|||||z    ||z  }t        | |!dz         D ]  }||   |
|<   |dz  } ||k  r&|d
| |d
| |||z
  |z   |dz    |||dz    |}|dz  }|}n|||||z   dz    ||k  r||z
  dz
  }.|.dz   }/|dz   }0d
}1|
d
|/d
df   }2|dk(  r:|
d
|/df   }1t#        |2|1      D 3cg c]
  \  }}3||3z   }2}}3|1j%                         }1t'        |2|0|1|d
|/|0z          S c c}3}w )a  Returns a new :class:`BSpline` with a t-times elevated degree.

    Degree elevation increases the degree of a curve without changing the shape of the
    curve. This function implements the algorithm A5.9 of the "The NURBS Book" by
    Piegl & Tiller.

    .. versionadded:: 1.4

    r   r8   r@   r6   rh  rr   r   r   r   r?   Nrx   )r:   r   r;   )rc   rA   r  r]  r   r   rI   r;   rD   rj  r   binomial_coefficientrt   minri   r   r   r   r^  r$   )4rb  rJ   rf   dimPwUre   r   phph2QwUhbezalfsbptsebptsNextbptsalfsbinomr   invmpir   mhkindrp  r   r   cinduamuluboldrlbzrbzro  rd   rq  r8  firstlastdenbettrkjalfgamnh
count_cptsr:   r   rR  ws4                                                       rO   r}  r}  u  s    	AA1uA"6* XXf++, 	 A 	B!A	A	A	
QB
'C 
RAE*C0	1B 
!q1u+	B hha!eaiQ/0G 881q5#,'D HHAEAIs+,E xxq1ucl+H 88AE?DGDMGBEN''E1cAg @E"aL !Qis1a!e}cAg. 	@A%1+-aQ?GAqDM	@@
 37B 3!Qis1a!e}cAg. 	3A#BFAEM2GAqDM	33 
B6D
A	A	AAD	
1BqEBqE BxaL
 wQKD1q5M
a%1u4<<!aAh7FA 1u4<<!aAh7!eai#X\qTG!8!8/CCq5A!|#CCq5GE1c2& <$)Qq1uX]$;QWq[!<1a!e_ )1u!Gq!a%, XA"1q5kDG3sT!a%[7HDQRUVQVK6WWDGX!%a) sBF# 	>A E!Ha)C3q!a%=#'2 > 8gadmd1g&==a>	> !81HEDr'C4!8$+CAtn X\!ebj4x!BqEzb2a5j9 #besSyBq1uI.E E1Cxr6TBY%55#%1r6
?c"9C(+eBi39bSTf:U(UE"I(+eBi39bSTf:U(UE"IFAFA!GB !ebj 
	), 6 T	A"$BtdQhAIDsC!G$ 	AQxBtHAID	 q5  |D!H !QQU3DQUOAFAB (*BtdRi!m$w a%z 
b1BaJFEG*bqb!G
ax[j[!^$%('%:;TQ1q5;;.."ugR8L*u:L5M  <s   Wc           
        t        j                  | j                         t         j                        }t	        |      st        j
                  | j                        }t        j                  t        | j                  |      D cg c]2  \  }}|j                  |z  |j                  |z  |j                  |z  |f4 c}}      S c c}}w )Nr   )r   r   r   r   rD   onesrZ   r   rI   r  r  z)rb  r   ro   r  s       rO   r]  r]  9  s    hhv~~'rzz:Gw<''&,,'88478M8Mw4WXDAq!##'1337ACC!GQ	'X Xs   7C
c                    g }g }| D ]A  }t        |d         }|j                  t        |d d       |z         |j                  |       C ||fS )Nr@   )rs   r   r   )rY  rH   r   r6  r  s        rO   r_  r_  C  s]     FG %(Od5!9o)*q 7?rP   r~  r  r  r  c               P   | j                   }t        d      }|dz  }t        j                  d||d      }t	        | j                  |            }	t        ||	      D ])  \  }
}|j                  |      }||k  st        |
      }|}+ d}t        |      D ]  }| j                  |d      \  }}||z
  }|j                  }||k  r |S t        j                  ||      r|dz  }|dkD  r |S d}|}||j                  |      |j                  |      z  z  }|dk  rd}||kD  s|} |S )	aF  Returns the parameter t for a point on the curve that is closest to the input
    point.

    This is an iterative search using Newton's method, so there is no guarantee
    of success, especially for splines with many turns.

    Args:
        spline(BSpline): curve
        point(Vec3): point on the curve or near the curve
        epsilon(float): desired precision (distance input point to point on curve)
        max_iterations(int): max iterations for Newton's method
        init(int): number of points to calculate in the initialization phase

    .. versionadded:: 1.4

    infr6   r   Tendpointr   r   rx   )r   rs   r   r1  rC   rH   r   r>  rt   rK  r   r   r   r   )rb  r6  r  r  r  r   prev_distancer   rJ   
chk_pointsr   rf   r>  no_progress_counter	iterationdpdudiffs                    rO   r  r  O  s_   & LLE%LM	A 	AudT2AfmmA&'JQ
# %A>>!$m#b	A$M	%  !>* 	##A#+4 5y >>g" H! <<x01$"Q& H #$  	
TXXd^dhhtn,, s7AYA56 HrP   c                  T    e Zd ZdZd	dZed
d       Zedd       ZddZ	ddZ
ddZy)r  zvB-spline measurement tool.

    All measurements are based on the approximated curve.

    .. versionadded:: 1.4

    c                   t        |t              st        dt        |             t	        |      }|dk  rt        d|       || _        t        j                  d|j                  |dz   d      | _
        t        | j                  j                  | j                              }t        |      }|j                  | _        |j                  | _        | j!                  |      | _        t        j$                  | j"                        | _        y )NzBSpline instance expected, got r   zinvalid segment count: rx   Tr  )r   r$   r\  typerc   rE   _spliner   r1  r   _parametersrC   rH   r   extminextmax_measure
_distancescumsum_offsets)r   rb  r  rH   bboxs        rO   r   zMeasurement.__init__  s    &'*=d6l^LMMx=a<6xjABB;;sFLL(Q,QUVdll))$*:*:;<6"kkkk--/		$//2rP   c                    | d   }t        j                  t        |       t         j                        }t	        |       D ]  \  }}|j                  |      ||<   |} |S )Nr   r   )r   rj  rD   r   	enumerater>  )rH   prev	distancesrQ  r6  s        rO   r
  zMeasurement._measure  s[    ayHHS[

;	%f- 	LE5#}}U3IeD	 rP   c                2    t        | j                  d         S )z0Returns the approximated length of the B-spline.r?   )rs   r  r   s    rO   lengthzMeasurement.length  s     T]]2&''rP   c                ,   |dk  ry|| j                   j                  k\  r| j                  S | j                  }t	        j
                  ||d      }||   }||dz      }| j                  |dz      ||z
  z  ||z
  z  }t        | j                  |   |z         S )zfReturns the distance along the curve from the start point for then given
        parameter t.
        rx   rightsider   )	r  r   r  r  r   searchsortedr  rs   r  )r   rJ   r   rQ  r   t2r>  s          rO   r>  zMeasurement.distance  s     8""";;!!8E]EAI??519-R8BGDT]]5)H455rP   c                <   t        j                  | j                  |d      }|dk  ry| j                  }|t	        |      k\  r| j
                  j                  S |dz
  }|| j                  |   z
  }||   }||   }t        |||z
  || j                  |   z  z  z         S )zcReturns the parameter t for a given distance along the curve from the
        start point.
        r  r  r   rx   r   )	r   r  r  r  rD   r  r   rs   r  )r   r>  rQ  r   r  r  r   r  s           rO   param_atzMeasurement.param_at  s     xgFA:!!CK<<%%%qy 4==#66D\E]R27}tu7M'MNNOOrP   c                   |dk  rt        d|       | j                  }||z  }|}| j                  j                  }g }||k  rC| j	                  |      }t        j                  ||      s|j                  |       ||z  }||k  rC|S )av  Returns the interpolated B-spline parameters for dividing the curve into
        `count` segments of equal length.

        The method yields only the dividing parameters, the first (0.0) and last parameter
        (max_t) are not included e.g. dividing a B-spline by 3 yields two parameters
        [t1, t2] that divides the B-spline into 3 parts of equal length.

        r   zinvalid count: )rE   r  r  r   r  r   r   r   )r   rZ   total_length
seg_lengthr>  r   resultrJ   s           rO   dividezMeasurement.divide  s     19ug677"kk!E)
"" %h'A<<q)a 
"H	 %
 rP   N)rb  r$   r  rc   r   None)rH   r  r   r[  r  )rJ   rs   r   rs   )r>  rs   r   rs   )rZ   rc   r   r   )r  r  r  r  r   r  r
  r  r  r>  r  r!  rl   rP   rO   r  r    sD    3   ( (6P rP   r  c                t   d}||k  rt        d      || j                  |z
  kD  rt        d      | j                  }t        j                  ||      }| j                  |      } t        j                  | j                         t        j                        }t        j                  ||d      }|d| }||d }t        j                  ||f      }| j                  }	t        |      |z
  }
|	d|
 }|	|
d }g }g }| j                         }t        |      r
|d|
 }||
d }t        ||||      t        ||||      fS )	zSplits a B-spline at a parameter t.

    Raises:
        ValuerError: t out of range 0 < t < max_t

    .. versionadded:: 1.4

    g-q=zt must be greater than 0zt must be smaller than max_tr   r  r  NrZ  )rE   r   r:   r   fullrc  r   r;   r   r  concatenaterI   rD   r   r$   )rb  rJ   tolr:   r   r;   r   knots1knots2rH   split_indexpoints1points2weights1weights2r   s                   rO   r  r    sJ    C3w3446<<#788LLE 	qA##A&FHHV\\^2::6E??5!'2D 5D\F45\F ^^QK(F ""Ff+%K\k"G[\"G "H "HnnG
7|<K(;<(fh?fh? rP   r   )rG   r  rB   Optional[Iterable[UVec]]r   r$   )rG   r  r   r$   )r@   Nr<   )
rG   r  rA   rc   rB   r.  r>   strr   r$   )z5-pointsN)rG   r  r>   r/  rB   r.  r   r$   )rZ   rc   r:   rc   r   rc   )T)r:   rc   r   rc   )r;   r  r   r   )F)rZ   rc   r:   rc   r   r   )rX   F)re   rc   rf   rc   rJ   r  r   r   )re   rc   rf   rc   rJ   r  r   r   )r   zlist | linalg.MatrixrA   rc   r   zlinalg.Solver)rX   )
rG   zSequence[UVec]rA   rc   r[   r  r\   r/  r   tuple[list[Vec3], list[float]])rG   r  rM   r   rN   r   rA   rc   r[   r  r\   r/  r   r0  )rG   r  r   r0  )
rG   r  r   r  rA   rc   r[   r  r   r0  )rG   r  rB   r  r   r0  )rf   r   r   r  )r8   N)rI   r  r:   rc   r   r  r   r$   )r  r   r   r  r   )r  r   r  rs   r  rs   r  rs   r  rc   r   r$   )r   )r  r   r  rc   r   r$   )r  rs   r  rs   r  rc   )
r   rs   rQ  rc   rA   rc   r;   r  r   rs   )
r   rs   rZ   rc   rA   rc   r;   r  r   r   )rb  r$   rJ   rc   r   r$   )rb  r$   r   r[  )rY  zIterable[Sequence[float]]r   r0  )rb  r$   r6  r   r   rs   )rb  r$   rJ   rs   r   r  )Ir  
__future__r   typingr   r   r   r   r   r	   r
   r   numpyr   rT   r   r   r   r   r   r   r   r   r   r  r   r   ezdxf.lldxf.constr   r   r   r   r   __all__r   r!   r   r   r0   r3   r4   rp   r1   r2   r*   r+   r,   r-   r.   r/   r   r%   r&   r'   r(   r)   r$   rz  r"   r#   r   pir  r    r  r  r  r}  r]  r_  r  r  r  rl   rP   rO   <module>r7     s    #    
 
 
   + H *.**&* *Z,2 )-	SSS 'S 	S
 Sp )-'9'9'9 ''9 	'9T2$	4K&6 GL&F
&F&F&&F&FR*&&&#L06 #,	$3$3$3 $3  	$3
 $$3Z #,333333 33 	33
 33  33 $33l>3>3#>3B>3>3>3 >3 	>3
 $>3B6!6!&06!#6!ri& i&X )-N"NN 'N 	N0 )-A"AA 'A 	A2   	
  @ ww} 34 ,/B3*l'&T"%.=6 @ @F	(	#	 .2#A<< <
<~Z Zz-rP   