Ë
    £èhÎ  ã                  ó€   — d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
mZmZmZmZ erddlmZ ddlmZ  G d„ d	«      Zy
)aÏ	  
DXF R12 Splines
===============

DXF R12 supports 2d B-splines, but Autodesk do not document the usage in the
DXF Reference. The base entity for splines in DXF R12 is the POLYLINE entity.

Transformed Into 3D Space
-------------------------

The spline itself is always in a plane, but as any 2D entity, the spline can be
transformed into the 3D object by elevation, extrusion and thickness/width.

Open Quadratic Spline with Fit Vertices
-------------------------------------

Example: 2D_SPLINE_QUADRATIC.dxf
expected knot vector: open uniform
degree: 2
order: 3

POLYLINE:
flags (70): 4 = SPLINE_FIT_VERTICES_ADDED
smooth type (75): 5 = QUADRATIC_BSPLINE

Sequence of VERTEX
flags (70): SPLINE_VERTEX_CREATED = 8  # Spline vertex created by spline-fitting

This vertices are the curve vertices of the spline (fitted).

Frame control vertices appear after the curve vertices.

Sequence of VERTEX
flags (70): SPLINE_FRAME_CONTROL_POINT = 16

No control point at the starting point, but a control point at the end point,
last control point == last fit vertex

Closed Quadratic Spline with Fit Vertices
-----------------------------------------

Example: 2D_SPLINE_QUADRATIC_CLOSED.dxf
expected knot vector: closed uniform
degree: 2
order: 3

POLYLINE:
flags (70): 5 = CLOSED | SPLINE_FIT_VERTICES_ADDED
smooth type (75): 5 = QUADRATIC_BSPLINE

Sequence of VERTEX
flags (70): SPLINE_VERTEX_CREATED = 8  # Spline vertex created by spline-fitting

Frame control vertices appear after the curve vertices.

Sequence of VERTEX
flags (70): SPLINE_FRAME_CONTROL_POINT = 16


Open Cubic Spline with Fit Vertices
-----------------------------------

Example: 2D_SPLINE_CUBIC.dxf
expected knot vector: open uniform
degree: 3
order: 4

POLYLINE:
flags (70): 4 = SPLINE_FIT_VERTICES_ADDED
smooth type (75): 6 = CUBIC_BSPLINE

Sequence of VERTEX
flags (70): SPLINE_VERTEX_CREATED = 8  # Spline vertex created by spline-fitting

This vertices are the curve vertices of the spline (fitted).

Frame control vertices appear after the curve vertices.

Sequence of VERTEX
flags (70): SPLINE_FRAME_CONTROL_POINT = 16

No control point at the starting point, but a control point at the end point,
last control point == last fit vertex

Closed Curve With Extra Vertices Created
----------------------------------------

Example: 2D_FIT_CURVE_CLOSED.dxf

POLYLINE:
flags (70): 3 = CLOSED | CURVE_FIT_VERTICES_ADDED

Vertices with bulge values:

flags (70): 1 = EXTRA_VERTEX_CREATED
Vertex 70=0, Vertex 70=1, Vertex 70=0, Vertex 70=1

é    )Úannotations)ÚTYPE_CHECKINGÚIterableÚOptional)Úconst)ÚBSplineÚclosed_uniform_bsplineÚVec3ÚUCSÚUVec)Ú
BaseLayout)ÚPolylinec                  ó\   — e Zd ZdZ	 	 d	 	 	 	 	 dd„Z	 d	 	 	 	 	 d	d„Z	 	 	 d
	 	 	 	 	 	 	 dd„Zy)Ú	R12Splineaê  DXF R12 supports 2D B-splines, but Autodesk do not document the usage
    in the DXF Reference. The base entity for splines in DXF R12 is the POLYLINE
    entity. The spline itself is always in a plane, but as any 2D entity, the
    spline can be transformed into the 3D object by elevation and extrusion
    (:ref:`OCS`, :ref:`UCS`).

    This way it was possible to store the spline parameters in the DXF R12 file,
    to allow CAD applications to modify the spline parameters and rerender the
    B-spline afterward again as polyline approximation. Therefore, the result is
    not better than an approximation by the :class:`Spline` class, it is also
    just a POLYLINE entity, but maybe someone need exact this tool in the
    future.

    c                óT   — t        j                  |«      | _        || _        || _        y)zÄ
        Args:
            control_points: B-spline control frame vertices
            degree: degree of B-spline, only 2 and 3 is supported
            closed: ``True`` for closed curve

        N)r
   ÚlistÚcontrol_pointsÚdegreeÚclosed)Úselfr   r   r   s       ú6lib/python3.12/site-packages/ezdxf/render/r12spline.pyÚ__init__zR12Spline.__init__   s#   € ô #Ÿi™i¨Ó7ˆÔØˆŒØˆ�ó    Nc                ó   ‡— | j                   r%t        | j                  | j                  dz   ¬«      }n$t	        | j                  | j                  dz   ¬«      }|j                  |«      }‰�ˆfd„|D «       }t        |«      S )al  Approximate the B-spline by a polyline with `segments` line segments.
        If `ucs` is not ``None``, ucs defines an :class:`~ezdxf.math.UCS`, to
        transform the curve into :ref:`OCS`. The control points are placed
        xy-plane of the UCS, don't use z-axis coordinates, if so make sure all
        control points are in a plane parallel to the OCS base plane
        (UCS xy-plane), else the result is unpredictable and depends on the CAD
        application used to open the DXF file - it may crash.

        Args:
            segments: count of line segments for approximation, vertex count is
                `segments` + 1
            ucs: :class:`~ezdxf.math.UCS` definition, control points in ucs
                coordinates

        Returns:
            list of vertices in :class:`~ezdxf.math.OCS` as
            :class:`~ezdxf.math.Vec3` objects

        é   )Úorderc              3  ó@   •K  — | ]  }‰j                  |«      –— Œ y ­w)N)Úto_ocs)Ú.0ÚvertexÚucss     €r   Ú	<genexpr>z(R12Spline.approximate.<locals>.<genexpr>®   s   øè ø€ ÐB¹¨v˜Ÿ
™
 6×*¹ùs   ƒ)r   r	   r   r   r   Úapproximater   )r   Úsegmentsr!   ÚsplineÚverticess     `  r   r#   zR12Spline.approximate�   sn   ø€ ð, �;Š;Ü+Ø×#Ñ#¨4¯;©;¸©?ô‰Fô ˜T×0Ñ0¸¿¹Àa¹ÔHˆFØ×%Ñ% hÓ/ˆØˆ?ÛB¹ÓBˆHÜ�H‹~Ðr   c                ó  — |j                  g |¬«      }|j                  }| j                  r||j                  z  }||j                  _        | j                  dk(  r|j                  }n'| j                  dk(  r|j                  }nt        d«      ‚||j                  _
        |�|j                  |j                  _        |j                  | j                  ||«      |j                  j                  t         j"                  dœ¬«       | j$                  }|�;t'        |j)                  |«      «      }dd|d   j*                  f|j                  _        |j                  ||j                  j                  t         j.                  dœ¬«       |S )aT  Renders the B-spline into `layout` as 2D :class:`~ezdxf.entities.Polyline`
        entity. Use an :class:`~ezdxf.math.UCS` to place the 2D spline in the
        3D space, see :meth:`approximate` for more information.

        Args:
            layout: :class:`~ezdxf.layouts.BaseLayout` object
            segments: count of line segments for approximation, vertex count is
                `segments` + 1
            ucs: :class:`~ezdxf.math.UCS` definition, control points in ucs
                coordinates.
            dxfattribs: DXF attributes for :class:`~ezdxf.entities.Polyline`

        )ÚpointsÚ
dxfattribsé   é   zinvalid degree of spline)ÚlayerÚflags)r)   r   )Úadd_polyline2dÚSPLINE_FIT_VERTICES_ADDEDr   ÚCLOSEDÚdxfr-   r   ÚQUADRATIC_BSPLINEÚCUBIC_BSPLINEÚ
ValueErrorÚsmooth_typeÚuzÚ	extrusionÚappend_verticesr#   r,   r   ÚVTX_SPLINE_VERTEX_CREATEDr   r   Úpoints_to_ocsÚzÚ	elevationÚVTX_SPLINE_FRAME_CONTROL_POINT)	r   Úlayoutr$   r!   r)   Úpolyliner-   r5   r   s	            r   ÚrenderzR12Spline.render±   s_  € ð( ×(Ñ(°¸zÐ(ÓJˆØ×2Ñ2ˆØ�;Š;Ø�X—_‘_Ñ$ˆEØ"ˆ�‰Ôà�;‰;˜!ÒØ"×4Ñ4‰KØ�[‰[˜AÒØ"×0Ñ0‰KäÐ7Ó8Ð8Ø#.ˆ�‰Ô ð ˆ?Ø%(§V¡VˆH�L‰LÔ"ð 	× Ñ Ø×Ñ˜X sÓ+à!Ÿ™×+Ñ+Ü×8Ñ8ñð 	!ô 	
ð ×,Ñ,ˆØˆ?Ü! #×"3Ñ"3°NÓ"CÓDˆNØ&'¨¨N¸1Ñ,=×,?Ñ,?Ð%@ˆH�L‰LÔ"Ø× Ñ Øà!Ÿ™×+Ñ+Ü×=Ñ=ñð 	!ô 	
ð ˆr   )r*   T)r   zIterable[UVec]r   Úintr   Úbool)é(   N)r$   rA   r!   úOptional[UCS]Úreturnz
list[UVec])rC   NN)r>   r   r$   rA   r!   rD   rE   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r@   © r   r   r   r   o   s‰   „ ñð$ Øð	à&ðð ðð ó	ð$ 8<ðØðØ'4ðà	óðH Ø!Øð;àð;ð ð;ð ð	;ð 
ô;r   r   N)rI   Ú
__future__r   Útypingr   r   r   Úezdxf.lldxfr   Ú
ezdxf.mathr   r	   r
   r   r   Úezdxf.layoutsr   Úezdxf.entitiesr   r   rJ   r   r   Ú<module>rQ      s5   ðñaõD #ß 4Ñ 4Ý ß GÕ GáÝ(Ý'÷}ò }r   