
    OgR                    ^   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 erd dlmZ d dlmZ g dZd ej2                  dz  ej2                  ej2                  d	z  gZej2                  d
z  Z G d d      ZddZddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZddZ ddZ!ddZ"ddZ#y)    )annotations)TYPE_CHECKINGIterableAnyN)
Vec3UVecNULLVECX_AXISZ_AXISOCSMatrix44arc_angle_span_raddistance_point_line_3denclosing_angles)
BaseLayoutEllipse)ConstructionEllipseangle_to_paramparam_to_anglerytz_axis_construction      ?g      ?       @c                  (   e Zd ZdZeeeddej                  df	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	e
ededddf	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Zd Zedd	       Zedd
       ZddZddZddZedd       ZddZd dZd!d"dZd#dZd dZd$dZd%d&dZd'dZy)(r   a  Construction tool for 3D ellipsis.

    Args:
        center: 3D center point
        major_axis: major axis as 3D vector
        extrusion: normal vector of ellipse plane
        ratio: ratio of minor axis to major axis
        start_param: start param in radians
        end_param: end param in radians
        ccw: is counter-clockwise flag - swaps start- and end param if ``False``

       r   Tc                   t        |      | _        t        |      | _        | j                  j                  t              rt        d      t        |      | _        | j                  j                  t              rt        d      t        |      | _        t        |      | _	        t        |      | _
        |s#| j                  | j                  c| _	        | _
        t        | j                  | j                  | j                        | _        y )Nz!Invalid major axis (null vector).z'Invalid extrusion vector (null vector).)r   center
major_axisiscloser	   
ValueError	extrusionfloatratiostart_param	end_param
minor_axis)selfr   r   r!   r#   r$   r%   ccws           W/var/www/html/public_html/myphp/venv/lib/python3.12/site-packages/ezdxf/math/ellipse.py__init__zConstructionEllipse.__init__1   s     6lz*??""7+@BBi??""7+FHH5\
 -y)/3~~t?O?O,Ddn$T__dnndjjQ    ih  c                f   t        |      }t        j                  |      rt        dt	        |             d}t        |      }|j                  |      }|j                  t        |dd            }	t        j                  |      }
t        j                  |      } | ||	|||
|t        |            S )a  Returns :class:`ConstructionEllipse` from arc or circle.

        Arc and Circle parameters defined in OCS.

        Args:
             center: center in OCS
             radius: arc or circle radius
             extrusion: OCS extrusion vector
             start_angle: start angle in degrees
             end_angle: end angle in degrees
             ccw: arc curve goes counter clockwise from start to end if ``True``
        zInvalid extrusion:       ?r   )absr	   r   r    strr   to_wcsr   mathradiansbool)clsr   radiusr!   start_angle	end_angler(   r#   ocsr   r$   r%   s               r)   from_arczConstructionEllipse.from_arcI   s    , V??9%23y>2BCDD)nF#ZZVQ 23
ll;/LL+	I
 	
r+   c                    | j                  | j                  | j                  | j                  | j                  | j
                  | j                        S N)	__class__r   r   r!   r#   r$   r%   r'   s    r)   __copy__zConstructionEllipse.__copy__t   s?    ~~KKOONNJJNN
 	
r+   c                    t        | j                  | j                  | j                  | j                  | j
                        S )z'Returns start point of ellipse as Vec3.)vertexr$   r   r&   r   r#   r=   s    r)   start_pointzConstructionEllipse.start_point~   s6     OOOOKKJJ
 	
r+   c                    t        | j                  | j                  | j                  | j                  | j
                        S )z%Returns end point of ellipse as Vec3.)r@   r%   r   r&   r   r#   r=   s    r)   	end_pointzConstructionEllipse.end_point   s4     NNOOOOKKJJ
 	
r+   c                   | j                   dkD  r!| j                         }|j                          n| }|j                  |j                  |j
                  t        |j                   d      |j                  |j                  dS )zReturns required DXF attributes to build an ELLIPSE entity.

        Entity ELLIPSE has always a ratio in range from 1e-6 to 1.

        r   ư>)r   r   r!   r#   r$   r%   )	r#   r>   	swap_axisr   r   r!   maxr$   r%   )r'   es     r)   
dxfattribszConstructionEllipse.dxfattribs   sc     ::>AKKMAhh,,$'==
 	
r+   c              #     K   | j                   }| j                  }t        D ]I  }t        |||      st	        || j
                  | j                  | j                  | j                         K yw)zZYields main axis points of ellipse in the range from start- to end
        param.
        N)	r$   r%   QUARTER_PARAMSr   r@   r   r&   r   r#   )r'   startendparams       r)   main_axis_pointsz$ConstructionEllipse.main_axis_points   sc        nn# 	Euc2OOOOKKJJ 	s
   /A.<A.c           	        |j                  | j                        }| j                  x}}| j                  x}}t	        | j
                  | j                  | j                        }|j                  | j
                  |f      \  }}	|j                         j                  |	j                               }
t        |
      dkD  r2t        ||	      \  }}	|j                  |	      j                         }d}nG|	j                  |j                  z  |j                  |	      j                         }t	        ||      }	d}|rt        j                   ||d      s|j                         |	j                         ||z
  t        j"                  z  }dfd}|j                  t%        || j
                  || j                  | j                              }|j                  t%        || j
                  || j                  | j                              } |||z
        } |||z
        }t        j                   |t        j&                  d      s3||z
  t        j"                  z  }t        j                   ||d      s{||}}nv|j                  t%        t)        ||      | j
                  || j                  | j                              }t%        t)        ||      ||	|      }|j!                  |d      s||}}dkD  rkt	        ||      }dz  t	        ||      }	t        j                   |d	      r$t        j                   |t        j"                        s|t*        z  }|t*        z  }|t        j"                  z  }|t        j"                  z  }t        j                   ||      rd
}t        j"                  }|| _        || _        |	| _        || _        | _        || _        || _        y)z8Transform ellipse in place by transformation matrix `m`.rE   TF&.>abs_tolc                    j                  |       z  }j                  |       }t        j                  ||      t        j                  z  S r;   )dotr1   atan2tau)vecdydx	new_ratiox_axisy_axiss      r)   rN   z,ConstructionEllipse.transform.<locals>.param   s<    ZZ_y0ZZ_zz"b)DHH44r+   r   r-   r           N)rX   z'Vec3'returnr"   )	transformr   r$   r%   r&   r   r!   r#   transform_directions	normalizerU   r.   r   cross	magnituder1   r   rW   r@   pi	mid_paramHALF_PI)r'   m
new_centerold_start_paramr$   old_end_paramr%   old_minor_axisnew_major_axisnew_minor_axisdot_productnew_extrusionadjust_paramsold_param_spanrN   rA   rC   new_param_spanold_chk_pointnew_chk_pointr[   r\   r]   s                       @@@r)   r`   zConstructionEllipse.transform   sp   [[-
(,(8(88+$(NN2	#DOOT^^TZZP)*)?)?__n-*
& %..044^5M5M5OP{d"8N95NNI +00@JJLM M '00>3K3KKI*00@JJLM'yQN!Mk9d!S#--/F#--/F (+5AN5 ++OO"KKJJK OO"KKJJI  j 89Ki*45I <<F
 #,k"9TXX!E||NNDQ-6K !"!/=A&

! !'k95""! %,,]D,I-6Kq='yQNiI'yQNLLa0T\\)TXX5Vw&W$	 "DHH,(	<<Y/KI ((&
&"r+   c                B    t        | j                  | j                        S )zReturns the counter-clockwise params span from start- to end param,
        see also :func:`ezdxf.math.ellipse_param_span` for more information.

        )r   r$   r%   r=   s    r)   
param_spanzConstructionEllipse.param_span-  s     "$"2"2DNNCCr+   c              #  b   K   t        | j                  | j                  |      E d{    y7 w)u   Returns `num` params from start- to end param in counter-clockwise
        order.

        All params are normalized in the range from [0, 2π).

        N)
get_paramsr$   r%   )r'   nums     r)   paramszConstructionEllipse.params5  s$      d..DDDs   %/-/c              #  h  K   | j                   }| j                  }| j                  j                         }| j                  j                         }| j                  j
                  }||z  }|D ]B  }t        j                  |      |z  |z  }	t        j                  |      |z  |z  }
||	z   |
z    D yw)u  Yields vertices on ellipse for iterable `params` in WCS.

        Args:
            params: param values in the range from [0, 2π) in radians,
                param goes counter-clockwise around the extrusion vector,
                major_axis = local x-axis = 0 rad.

        N)	r   r#   r   rb   r&   rd   r1   cossin)r'   r{   r   r#   r\   r]   radius_xradius_yrN   xys              r)   verticeszConstructionEllipse.vertices>  s      

**,**,??,,e# 	!E(*V3A(*V3A1*q. 	!s   B0B2c              #     	
K   d	
 fddfd j                   j                          j                  j                          j                   j                  		 j                  z  
 j
                  |z  }|dk(  ry j                  t        j                  z  }t        j                   j                  t        j                        rt        j                  }n j                  t        j                  z  }t        j                  ||      ry||kD  r|t        j                  z  } |      }| ||k  rC||z   }t        j                  ||      r|} |      } ||||      E d{    |}|}||k  rByy7 w)a  Adaptive recursive flattening. The argument `segments` is the
        minimum count of approximation segments, if the distance from the center
        of the approximation segment to the curve is bigger than `distance` the
        segment will be subdivided. Returns a closed polygon for a full ellipse:
        start vertex == end vertex.

        Args:
            distance: maximum distance from the projected curve point onto the
                segment chord.
            segments: minimum segment count

        c                    t        j                  |       z  z  }t        j                  |       z  z  }j                  |z   |z   S r;   )r1   r}   r~   r   )pr   r   r   r   r'   r\   r]   s      r)   vertex_z/ConstructionEllipse.flattening.<locals>.vertex_a  sD    h&/Ah&/A;;?Q&&r+   c              3     K   ||z   dz  } 	|      }t        || |      }|k  r| y  | |||      E d {     ||||      E d {    y 7 7 w)Nr   )r   )
srH   s_parame_paramm_paramrh   ddistancesubdivr   s
          r)   r   z.ConstructionEllipse.flattening.<locals>.subdivf  sk     (C/G A&q!Q/A8|!!Q999!!Q999 :9s!   6AAAAAAr^   N)r   r"   r_   r   )r   r   rH   r   r   r"   r   r"   )r   rb   r&   rd   r#   rw   r$   r1   rW   r   r%   )r'   r   segmentsdeltarN   r%   rA   next_end_paramrC   r   r   r   r   r\   r]   s   ``       @@@@@@r)   
flatteningzConstructionEllipse.flatteningS  sH    	' 	'
	: **,**,??,,djj((*C<  488+<<1I1I<<y)Y!Ieni"U]N||NI6!*/Ik9e^LLL"E#K i
 Ms   E-F
7F8F
F
c              #  v  K   | j                   j                         }| j                  j                         }| j                  }| j                  }t        j                  |      D ]Q  }||z  }t        j                  |j                  |      |z  |j                  |            t        j                  z   S yw)u8  Yields ellipse params for all given `vertices`.

        The vertex don't have to be exact on the ellipse curve or in the range
        from start- to end param or even in the ellipse plane. Param is
        calculated from the intersection point of the ray projected on the
        ellipse plane from the center of the ellipse through the vertex.

        .. warning::

            An input for start- and end vertex at param 0 and 2π return
            unpredictable results because of floating point inaccuracy,
            sometimes 0 and sometimes 2π.

        N)r   rb   r&   r#   r   r   generater1   rV   rU   rW   )r'   r   r\   r]   r#   r   vs          r)   params_from_verticesz(ConstructionEllipse.params_from_vertices  s      **,**,

x( 	NAKA**VZZ]U2FJJqMBTXXMM	Ns   B7B9c              #  ,  K   | j                   }| j                  j                         }| j                  j                         }|D ]K  }t	        j
                  |       |z  }t	        j                  |      |z  |z  }||z   j                          M yw)u3  Yields tangents on ellipse for iterable `params` in WCS as direction
        vectors.

        Args:
            params: param values in the range from [0, 2π] in radians, param
                goes counter-clockwise around the extrusion vector,
                major_axis = local x-axis = 0 rad.

        N)r#   r   rb   r&   r1   r~   r}   )r'   r{   r#   r\   r]   rN   r   r   s           r)   tangentszConstructionEllipse.tangents  s      

**,**, 	&E% 6)A%'&0Aq5##%%	&s   BBc                   | j                   | _        d| j                  z  }t        |d      | _        t        | j                  | j                  | j                        | _         | j
                  }| j                  }t        j                  |d      r%t        j                  |t        j                        ry|t        z
  t        j                  z  | _        |t        z
  t        j                  z  | _        y)z.Swap axis and adjust start- and end parameter.r-   rE   r   N)r&   r   r#   rG   r!   r$   r%   r1   r   rW   rg   )r'   r#   r$   r%   s       r)   rF   zConstructionEllipse.swap_axis  s    //djj %
$T__dnndjjQ&&NN	<<Q'DLLDHH,M''1TXX=#g-9r+   Nc                    ddl m} t        |xs i       }|j                  | j	                                 |j
                  ||j                        }|j                  |       |S )a  Add ellipse as DXF :class:`~ezdxf.entities.Ellipse` entity to a
        layout.

        Args:
            layout: destination layout as :class:`~ezdxf.layouts.BaseLayout`
                object
            dxfattribs: additional DXF attributes for the ELLIPSE entity

        r   r   )rI   doc)ezdxf.entitiesr   dictupdaterI   newr   
add_entity)r'   layoutrI   r   rH   s        r)   add_to_layoutz!ConstructionEllipse.add_to_layout  sT     	+**+
$//+,GKK:6::>!r+   c                   t        | j                        }| j                  |j                  | j                        |j                  | j
                        j                  d      | j                  | j                  | j                        S )zqReturns ellipse parameters as OCS representation.

        OCS elevation is stored in :attr:`center.z`.

        r^   )z)r   r   r#   r$   r%   )
r   r!   r<   from_wcsr   r   replacer#   r$   r%   )r'   r8   s     r)   to_ocszConstructionEllipse.to_ocs  sm     $..!~~<<,||DOO4<<s<C**((nn  
 	
r+   )r   r   r   r   r!   r   r#   r"   r$   r"   r%   r"   r(   r3   )r   r   r5   r"   r!   r   r6   r"   r7   r"   r(   r3   r_   r   )r_   r   )r_   zdict[str, Any])r_   Iterable[Vec3])rh   r   r_   None)r_   r"   )rz   intr_   Iterable[float])r{   r   r_   r   )   )r   r"   r   r   r_   r   )r   zIterable[UVec]r_   r   )r_   r   r;   )r   r   r_   r   )r_   r   )__name__
__module____qualname____doc__r	   r
   r   r1   rW   r*   classmethodr9   r>   propertyrA   rC   rI   rO   r`   rw   r{   r   r   r   r   rF   r   r    r+   r)   r   r   #   s_    ! 88RR R 	R
 R R R R0   (
(
 (
 	(

 (
 (
 (
 
(
 (
T
 
 
 
 

( s#j D DE!*:$xN.&&:$
r+   r   c                B    || k  r|t         j                  z  }| |z   dz  S )Nr   )r1   rW   )rL   rM   s     r)   rf   rf     s%    
U{txxCK3r+   c                \    |j                  |       j                  | j                  |z        S r;   )rc   rb   rd   )r   r!   r#   s      r)   r&   r&     s'    ??:&001E1E1MNNr+   c                    |j                         }|j                         }|j                  }||z  }t        j                  |       |z  |z  }	t        j                  |       |z  |z  }
||	z   |
z   S r;   )rb   rd   r1   r}   r~   )rN   r   r&   r   r#   r\   r]   r   r   r   r   s              r)   r@   r@     sq     !!#F!!#F##H%H("V+A("V+AA:>r+   c              #     K   |dk  rt        d      || k  r|t        j                  z  }t        j                  | ||      D ]  }|t        j                  z    yw)u   Returns `num` params from start- to end param in counter-clockwise order.

    All params are normalized in the range from [0, 2π).

       znum >= 2N)r    r1   rW   nplinspace)rL   rM   rz   rN   s       r)   ry   ry     sY      Qw$$
e|txxUC- dhhs   AAc                    t        j                  t        j                  |      | z  t        j                  |            t         j                  z  S )uK  Returns ellipse parameter for argument `angle`.

    Args:
        ratio: minor axis to major axis ratio as stored in the ELLIPSE entity
            (always <= 1).
        angle: angle between major axis and line from center to point on the
            ellipse

    Returns:
        the ellipse parameter in the range [0, 2π)
    )r1   rV   r~   r}   rW   )r#   angles     r)   r   r     s3     ::dhhuo-txx?$((JJr+   c                    t        j                  t        j                  |      | z  t        j                  |            S )uV  Returns circle angle from ellipse parameter for argument `angle`.

    Args:
        ratio: minor axis to major axis ratio as stored in the ELLIPSE entity
            (always <= 1).
        param: ellipse parameter between major axis and point on the ellipse
            curve

    Returns:
        the circle angle in the range [0, 2π)
    )r1   rV   r~   r}   )r#   rN   s     r)   r   r     s*     ::dhhuo-txx??r+   c                0   t        |       }t        j                  | j                  dd      r>t        j                  |j                  dd      rt        |      j	                  d      }n;| j                  |      }|j                  |      j                  |j                        }|j                  |      }|j                  }||z
  j                  |      }||z
  }||z   }	|j                  t              s|	j                  t              rt        d      ||z
  j                  }
|	|z
  j                  }t        j                  |
d      st        j                  |d      rt        d      ||
z  }|	j                  |
      }|j                  |      }|||fS )u  The Rytz’s axis construction is a basic method of descriptive Geometry
    to find the axes, the semi-major axis and semi-minor axis, starting from two
    conjugated half-diameters.

    Source: `Wikipedia <https://en.m.wikipedia.org/wiki/Rytz%27s_construction>`_

    Given conjugated diameter `d1` is the vector from center C to point P and
    the given conjugated diameter `d2` is the vector from center C to point Q.
    Center of ellipse is always ``(0, 0, 0)``. This algorithm works for
    2D/3D vectors.

    Args:
        d1: conjugated semi-major axis as :class:`Vec3`
        d2: conjugated semi-minor axis as :class:`Vec3`

    Returns:
         Tuple of (major axis, minor axis, ratio)

    r   rQ   rR   F)r(   z.Conjugated axis required, invalid source data.r^   )r   r1   r   r   
orthogonalrc   rb   rd   lerpr	   ArithmeticError)d1d2QP1r!   Dr5   radius_vectorABmajor_axis_lengthminor_axis_lengthr#   r   r&   s                  r)   r   r   ,  sU   ( 	RA||BDD!T*t||BDD!T/R"X  U +HHRL	__R **2<<8

A[[FV&&v.M	MA	MAyyQYYw/NOOQ))Q))||%s+t||<Ms/SNOO 11E./J./Jz5((r+   )rL   r"   rM   r"   r_   r"   )r   r   r!   r   r#   r"   r_   r   )rN   r"   r   r   r&   r   r   r   r#   r"   r_   r   )rL   r"   rM   r"   rz   r   r_   r   )r#   r"   r   r"   r_   r"   )r#   r"   rN   r"   r_   r"   )r   r   r   r   r_   ztuple[Vec3, Vec3, float])$
__future__r   typingr   r   r   numpyr   r1   
ezdxf.mathr   r   r	   r
   r   r   r   r   r   r   ezdxf.layoutsr   r   r   __all__re   rK   rg   r   rf   r&   r@   ry   r   r   r   r   r+   r)   <module>r      s    # / /     (& TWWs]DGGTWWs];
''C-C
 C
LO		"	04	>B	KP			K@+)r+   