
    OgYC                    \   d dl mZ d dl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mZmZ erd dlmZ d Zd Zdd	ej2                  d
z  df	 	 	 	 	 	 	 	 	 ddZdd	ej2                  dz  ej2                  dz  df	 	 	 	 	 	 	 	 	 	 	 ddZ G d d      Z G d d      Z G d d      Zy)    )annotations)TYPE_CHECKINGIterableOptionalN)Vec3Vec2UVecMatrix44perlinBezier4Pglobal_bspline_interpolationBSplineopen_uniform_bsplineclosed_uniform_bsplineEulerSpiral)
BaseLayoutc                <    | dz  t        j                          | z  z
  S N       @)random)	max_values    X/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/render/curves.pyrndr      s    s?V]]_y888    c                l    t        j                  |j                  |j                        }| dz  || z  z
  S r   )r   snoise2xy)r   walkerrs      r   
rnd_perlinr!      s-    vxx*As?Q]**r   d   g      ?      c              #  0  K   || z  fd}t        dd      } |       }t        |       D ]f  }||z  dk(  r
| |       z   }||z
  j                  }|t        ||      z   }	|t	        j                         z  }
|t        j
                  |	|
      z   }| h yw)u]  Returns a random 2D path as iterable of :class:`~ezdxf.math.Vec2`
    objects.

    Args:
        steps: count of vertices to generate
        max_step_size: max step size
        max_heading: limit heading angle change per step to ± max_heading/2 in
            radians
        retarget: specifies steps before changing global walking target

    c                 B    t        t               t               f      S N)r   r   max_s   r   next_global_targetz*random_2d_path.<locals>.next_global_target5   s    SYD	*++r   r   N)r   rangeangler!   r   
from_angle)stepsmax_step_sizemax_headingretargetr*   r   targetir,   headinglengthr)   s              @r   random_2d_pathr6   "   s     " 5 D, !QZF!F5\ x<1022F&''*[&990$//'6::s   BBr   g       @c              #    K   || z  fd}t               } |       }t        |       D ]  }||z  dk(  r
| |       z   }||z
  j                  }	|t        j                         z  }
|	t	        ||      z   }t        j
                  ||
      }t	        ||      }|t        j                  |      j                  |      z  }|  yw)u  Returns a random 3D path as iterable of :class:`~ezdxf.math.Vec3`
    objects.

    Args:
        steps: count of vertices to generate
        max_step_size: max step size
        max_heading: limit heading angle change per step to ± max_heading/2,
            rotation about the z-axis in radians
        max_pitch: limit pitch angle change per step to ± max_pitch/2, rotation
            about the x-axis in radians
        retarget: specifies steps before changing global walking target

    c                 V    t        t               t               t               f      S r'   )r   r   r(   s   r   r*   z*random_3d_path.<locals>.next_global_targetZ   s!    SYD	3t9566r   r   N)	r   r+   r,   r   r!   r-   r
   x_rotate	transform)r.   r/   r0   	max_pitchr1   r*   r   r2   r3   r,   r5   heading_angle	next_steppitch_angler)   s                 @r   random_3d_pathr?   D   s     ( 5 D7 VF!F5\ 	x<1022F&''0
; ??OOM6:	 F3(##K0::9EE	s   CCc                  p    e Zd ZdZ G d d      Zd
dZddZ	 	 d	 	 	 	 	 	 	 ddZddZ	 	 d	 	 	 	 	 dd	Z	y)Beziera>  Render a bezier curve as 2D/3D :class:`~ezdxf.entities.Polyline`.

    The :class:`Bezier` class is implemented with multiple segments, each
    segment is an optimized 4 point bezier curve, the 4 control points of the
    curve are: the start point (1) and the end point (4), point (2) is start
    point + start vector and point (3) is end point + end vector. Each segment
    has its own approximation count.

    .. seealso::

        The new :mod:`ezdxf.path` package provides many advanced construction tools
        based on the :class:`~ezdxf.path.Path` class.

    c                  0    e Zd Z	 	 	 	 	 	 	 	 	 	 ddZddZy)Bezier.Segmentc                    t        |      | _        t        |      | _        t        |      | _        t        |      | _        || _        y r'   )r   startendstart_tangentend_tangentsegments)selfrE   rF   rG   rH   rI   s         r   __init__zBezier.Segment.__init__|   s@     eDJCyDH!%"D  $K0D$DMr   c                    | j                   | j                   | j                  z   | j                  | j                  z   | j                  g}t	        |      }|j                  | j                        S r'   )rE   rG   rF   rH   r   approximaterI   )rJ   control_pointsbeziers      r   rM   zBezier.Segment.approximate   s\    



T///4+++	N n-F%%dmm44r   N)
rE   r	   rF   r	   rG   r	   rH   r	   rI   int)returnIterable[Vec3])__name__
__module____qualname__rK   rM    r   r   SegmentrC   {   s<    	%	% 	%  		%
 	% 	% 	5r   rW   c                    g | _         y r'   )points)rJ   s    r   rK   zBezier.__init__   s      	r   c                T    | j                   j                  t        |      d|df       y)zSet start point and start tangent.

        Args:
            point: start point
            tangent: start tangent as vector, example: (5, 0, 0) means a
                     horizontal tangent with a length of 5 drawing units
        N)rY   appendr   )rJ   pointtangents      r   rE   zBezier.start   s#     	DKw=>r   Nc                    t        |      }|| }nt        |      }| j                  j                  t        |      ||t        |      f       y)a  Append a control point with two control tangents.

        Args:
            point: control point
            tangent1: first tangent as vector "left" of the control point
            tangent2: second tangent as vector "right" of the control point,
                if omitted `tangent2` = `-tangent1`
            segments: count of line segments for the polyline approximation,
                count of line segments from the previous control point to the
                appended control point.

        N)r   rY   r[   rP   )rJ   r\   tangent1tangent2rI   s        r   r[   zBezier.append   sF    & > yHH~HDK8S]KLr   c              #    K   t        | j                        dkD  rct        | j                  d d | j                  dd        D ]9  \  }}|d   }|d   }|d   }|d   }|d   }t        j	                  |||||       ; y t        d      w)N   r   r#      zTwo or more points needed!)lenrY   ziprA   rW   
ValueError)rJ   
from_pointto_pointstart_pointrG   	end_pointrH   counts           r   _build_bezier_segmentszBezier._build_bezier_segments   s     t{{a(+DKK,<dkk!"o(N $
H(m *1$QK	&qk nnM;  9::s   BB	c                    g }| j                         D ]!  }|j                  |j                                # |st        d |D              r|j	                  ||       y|j                  ||       y)a  Render Bezier curve as 2D/3D :class:`~ezdxf.entities.Polyline`.

        Args:
            layout: :class:`~ezdxf.layouts.BaseLayout` object
            force3d: force 3D polyline rendering
            dxfattribs: DXF attributes for :class:`~ezdxf.entities.Polyline`

        c              3  &   K   | ]	  }|d      yw)r#   NrV   ).0ps     r   	<genexpr>z Bezier.render.<locals>.<genexpr>   s     /1!A$/s   
dxfattribsN)rm   extendrM   anyadd_polyline3dadd_polyline2d)rJ   layoutforce3drt   rY   segments         r   renderzBezier.render   sl      224 	1GMM'--/0	1c///!!&Z!@!!&Z!@r   )rQ   None)r\   r	   r]   r	   rQ   r}   )Nr$   )r\   r	   r_   r	   r`   zOptional[UVec]rI   rP   )rQ   zIterable[Segment])FN)ry   r   rz   boolrQ   r}   )
rS   rT   rU   __doc__rW   rK   rE   r[   rm   r|   rV   r   r   rA   rA   k   s    5 56? $(MM M !	M
 M4;" 	AA A
 
Ar   rA   c                      e Zd ZdZ	 d	 	 	 ddZdddZ	 	 	 d	 	 	 	 	 	 	 	 	 ddZeZ	 d	 	 	 	 	 ddZ	 d	 	 	 	 	 ddZ		 d	 	 	 	 	 ddZ
	 	 d	 	 	 	 	 	 	 dd	Z	 	 d	 	 	 	 	 	 	 dd
Z	 	 d	 	 	 	 	 	 	 ddZy)Splinea-  This class can be used to render B-splines into DXF R12 files as
    approximated :class:`~ezdxf.entities.Polyline` entities.
    The advantage of this class over the :class:`R12Spline` class is,
    that this is a real 3D curve, which means that the B-spline vertices do
    have to be located in a flat plane, and no :ref:`UCS` class is needed to
    place the curve in 3D space.

    .. seealso::

        The newer :class:`~ezdxf.math.BSpline` class provides the
        advanced vertex interpolation method :meth:`~ezdxf.math.BSpline.flattening`.

    Nc                `    |g }t        j                  |      | _        t        |      | _        y)z
        Args:
            points: spline definition points
            segments: count of line segments for approximation, vertex count is
                `segments` + 1

        N)r   listrY   rP   rI   )rJ   rY   rI   s      r   rK   zSpline.__init__   s)     >F"&))F"3Hr   c                D    t        | j                        dz
  |z  | _        y)ad  Calculate overall segment count, where segments is the sub-segment
        count, `segments` = 4, means 4 line segments between two definition
        points e.g. 4 definition points and 4 segments = 12 overall segments,
        useful for fit point rendering.

        Args:
            segments: sub-segments count between two definition points

        rb   N)re   rY   rI   )rJ   rI   s     r   	subdividezSpline.subdivide  s     T[[)A-9r   c                    t        | j                  ||      }t        |j                  | j                              }t        d |D              r|j                  ||       y|j                  ||       y)a  Render a B-spline as 2D/3D :class:`~ezdxf.entities.Polyline`, where
        the definition points are fit points.

           - 2D spline vertices uses: :meth:`~ezdxf.layouts.BaseLayout.add_polyline2d`
           - 3D spline vertices uses: :meth:`~ezdxf.layouts.BaseLayout.add_polyline3d`

        Args:
            layout: :class:`~ezdxf.layouts.BaseLayout` object
            degree: degree of B-spline (order = `degree` + 1)
            method: "uniform", "distance"/"chord", "centripetal"/"sqrt_chord" or
                "arc" calculation method for parameter t
            dxfattribs: DXF attributes for :class:`~ezdxf.entities.Polyline`

        )degreemethodc              3  :   K   | ]  }|j                   d k7    yw)g        N)z)rp   vertexs     r   rr   z.Spline.render_as_fit_points.<locals>.<genexpr>(  s     66vxx36s   rs   N)r   rY   r   rM   rI   rv   rw   rx   )rJ   ry   r   r   rt   splineverticess          r   render_as_fit_pointszSpline.render_as_fit_points  sh    * .KKv
 **4==9:6X66!!(z!B!!(z!Br   c                    t        | j                  |dz         }|j                  t        |j	                  | j
                              |       y)aX  Render an open uniform B-spline as 3D :class:`~ezdxf.entities.Polyline`.
        Definition points are control points.

        Args:
            layout: :class:`~ezdxf.layouts.BaseLayout` object
            degree: degree of B-spline (order = `degree` + 1)
            dxfattribs: DXF attributes for :class:`~ezdxf.entities.Polyline`

        rb   orderrs   Nr   rY   rw   r   rM   rI   rJ   ry   r   rt   r   s        r   render_open_bsplinezSpline.render_open_bspline/  sD     FQJ7##DMM23
 	 	
r   c                    t        | j                  |dz         }|j                  t        |j	                  | j
                              |       y)aR  Render a uniform B-spline as 3D :class:`~ezdxf.entities.Polyline`.
        Definition points are control points.

        Args:
            layout: :class:`~ezdxf.layouts.BaseLayout` object
            degree: degree of B-spline (order = `degree` + 1)
            dxfattribs: DXF attributes for :class:`~ezdxf.entities.Polyline`

        rb   r   rs   N)r   rY   rw   r   rM   rI   r   s        r   render_uniform_bsplinezSpline.render_uniform_bspline@  sD     &dkk!D##DMM23
 	 	
r   c                    t        | j                  |dz         }|j                  t        |j	                  | j
                              |       y)aY  Render a closed uniform B-spline as 3D :class:`~ezdxf.entities.Polyline`.
        Definition points are control points.

        Args:
            layout: :class:`~ezdxf.layouts.BaseLayout` object
            degree: degree of B-spline (order = `degree` + 1)
            dxfattribs: DXF attributes for :class:`~ezdxf.entities.Polyline`

        rb   r   rs   Nr   rY   rw   r   rM   rI   r   s        r   render_closed_bsplinezSpline.render_closed_bsplineQ  sD     (6A:F##DMM23
 	 	
r   c                    t        | j                  |dz   |      }|j                  t        |j	                  | j
                              |       y)a  Render a rational open uniform BSpline as 3D :class:`~ezdxf.entities.Polyline`.
        Definition points are control points.

        Args:
            layout: :class:`~ezdxf.layouts.BaseLayout` object
            weights: list of weights, requires a weight value (float) for each
                definition point.
            degree: degree of B-spline (order = `degree` + 1)
            dxfattribs: DXF attributes for :class:`~ezdxf.entities.Polyline`

        rb   r   weightsrs   Nr   rJ   ry   r   r   rt   r   s         r   render_open_rbsplinezSpline.render_open_rbsplineb  sF    $ FQJH##DMM23
 	 	
r   c                    t        | j                  |dz   |      }|j                  t        |j	                  | j
                              |       y)a  Render a rational uniform B-spline as 3D :class:`~ezdxf.entities.Polyline`.
        Definition points are control points.

        Args:
            layout: :class:`~ezdxf.layouts.BaseLayout` object
            weights: list of weights, requires a weight value (float) for each
                definition point.
            degree: degree of B-spline (order = `degree` + 1)
            dxfattribs: DXF attributes for :class:`~ezdxf.entities.Polyline`

        rb   r   rs   Nr   r   s         r   render_uniform_rbsplinezSpline.render_uniform_rbspliney  K    $ (KKvz7
 	##DMM23
 	 	
r   c                    t        | j                  |dz   |      }|j                  t        |j	                  | j
                              |       y)a  Render a rational B-spline as 3D :class:`~ezdxf.entities.Polyline`.
        Definition points are control points.

        Args:
            layout: :class:`~ezdxf.layouts.BaseLayout` object
            weights: list of weights, requires a weight value (float) for each
                definition point.
            degree: degree of B-spline (order = `degree` + 1)
            dxfattribs: DXF attributes for :class:`~ezdxf.entities.Polyline`

        rb   r   rs   Nr   r   s         r   render_closed_rbsplinezSpline.render_closed_rbspline  r   r   )Nr"   )rY   zOptional[Iterable[UVec]]rI   rP   )   )rI   rP   rQ   r}   )rd   chordN)
ry   r   r   rP   r   strrt   zOptional[dict]rQ   r}   )rd   N)ry   r   r   rP   rQ   r}   )ry   r   r   zIterable[float]r   rP   rQ   r}   )rS   rT   rU   r   rK   r   r   r|   r   r   r   r   r   r   rV   r   r   r   r      sw    HK&.&AD&
: %)CC C 	C
 #C 
C< "F ?C
 
*-
	
$ ?C
 
*-
	
$ ?C
 
*-
	
* 

 !
 	
 

6 

 !
 	
 

: 

 !
 	
 

r   r   c                  `    e Zd ZdZdddZ	 	 	 	 d	 	 	 	 	 	 	 d	dZ	 	 	 	 	 d
	 	 	 	 	 	 	 	 	 ddZy)r   zRender an `euler spiral <https://en.wikipedia.org/wiki/Euler_spiral>`_
    as a 3D :class:`~ezdxf.entities.Polyline` or a
    :class:`~ezdxf.entities.Spline` entity.

    This is a parametric curve, which always starts at the origin (0, 0).

    c                6    t        t        |            | _        y)zC
        Args:
            curvature: Radius of curvature

        N)_EulerSpiralfloatspiral)rJ   	curvatures     r   rK   zEulerSpiral.__init__  s     #5#34r   Nc                    | j                   j                  ||      }||j                  |      }|j                  t	        |      |      S )a
  Render curve as :class:`~ezdxf.entities.Polyline`.

        Args:
            layout: :class:`~ezdxf.layouts.BaseLayout` object
            length: length measured along the spiral curve from its initial position
            segments: count of line segments to use, vertex count is `segments` + 1
            matrix: transformation matrix as :class:`~ezdxf.math.Matrix44`
            dxfattribs: DXF attributes for :class:`~ezdxf.entities.Polyline`

        Returns:
            :class:`~ezdxf.entities.Polyline`

        rs   )r   rM   transform_verticesrw   r   )rJ   ry   r5   rI   matrixrt   rY   s          r   render_polylinezEulerSpiral.render_polyline  sJ    * ((:..v6F$$T&\j$IIr   c                    | j                   j                  |||      }|j                  }||j                  |      }|j	                  ||j
                  |j                         |      S )a  
        Render curve as :class:`~ezdxf.entities.Spline`.

        Args:
            layout: :class:`~ezdxf.layouts.BaseLayout` object
            length: length measured along the spiral curve from its initial position
            fit_points: count of spline fit points to use
            degree: degree of B-spline
            matrix: transformation matrix as :class:`~ezdxf.math.Matrix44`
            dxfattribs: DXF attributes for :class:`~ezdxf.entities.Spline`

        Returns:
            :class:`~ezdxf.entities.Spline`

        )r   )rN   r   knotsrt   )r   bsplinerN   r   add_open_spliner   r   )	rJ   ry   r5   
fit_pointsr   r   rt   r   rY   s	            r   render_splinezEulerSpiral.render_spline  sn    0 $$VZ$G&&..v6F%%!==,,.!	 & 
 	
r   )rb   )r   r   )rb   r"   NN)ry   r   r5   r   rI   rP   r   Optional[Matrix44])rb   
   rd   NN)
ry   r   r5   r   r   rP   r   rP   r   r   )rS   rT   rU   r   rK   r   r   rV   r   r   r   r     s    5 %)JJ J 	J
 #J: %)!
!
 !
 	!

 !
 #!
r   r   )
r.   rP   r/   r   r0   r   r1   rP   rQ   zIterable[Vec2])r.   rP   r/   r   r0   r   r;   r   r1   rP   rQ   rR   )
__future__r   typingr   r   r   r   math
ezdxf.mathr   r   r	   r
   r   r   r   r   r   r   r   r   ezdxf.layoutsr   r   r!   pir6   r?   rA   r   rV   r   r   <module>r      s   # 4 4      (9+ 1	  	
 F 3ww}$$$ $ 	$
 $ $NwA wAtD
 D
NL
 L
r   