Ë
    Jÿi|*  ã                  ó  — d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z ddlmZ ddlmZ ddlmZ dd	lmZmZmZm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# erddl$m%Z%m&Z&  G d„ de e   «      Z'	 	 	 	 	 	 dd„Z(y)aø  
build123d topology

name: zero_d.py
by:   Gumyr
date: January 07, 2025

desc:

This module provides the foundational implementation for zero-dimensional geometry in the build123d
CAD system, focusing on the `Vertex` class and its related operations. A `Vertex` represents a
single point in 3D space, serving as the cornerstone for more complex geometric structures such as
edges, wires, and faces. It is directly integrated with the OpenCascade kernel, enabling precise
modeling and manipulation of 3D objects.

Key Features:
- **Vertex Class**:
  - Supports multiple constructors, including Cartesian coordinates, iterable inputs, and
    OpenCascade `TopoDS_Vertex` objects.
  - Offers robust arithmetic operations such as addition and subtraction with other vertices,
    vectors, or tuples.
  - Provides utility methods for transforming vertices, converting to tuples, and iterating over
    coordinate components.

- **Intersection Utilities**:
  - Includes `topo_explore_common_vertex`, a utility to identify shared vertices between edges,
    facilitating advanced topological queries.

- **Integration with Shape Hierarchy**:
  - Extends the `Shape` base class, inheriting essential features such as transformation matrices
    and bounding box computations.

This module plays a critical role in defining precise geometric points and their interactions,
serving as the building block for complex 3D models in the build123d library.

license:

    Copyright 2025 Gumyr

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

é    )ÚannotationsN)ÚoverloadÚTYPE_CHECKING)ÚIterable)Ú	BRep_Tool)ÚBRepBuilderAPI_MakeVertex)ÚTopExp_Explorer)ÚTopoDSÚTopoDS_ShapeÚTopoDS_VertexÚTopoDS_Edge)Úgp_Pnt)ÚMatrixÚVectorÚ
VectorLike)ÚSelfé   )ÚShapeÚ	ShapeListÚdowncastÚ	shapetype)ÚEdgeÚWirec                  ó  ‡ — e Zd ZdZdZed„ «       Zedd„«       Zedd„«       Zedd„«       Zˆ fd„Zedd„«       Zedd	„«       Z	e
dd
„«       Ze
dd„«       Z	 	 	 	 dd„Zd„ Zd„ Zd„ Zdd„Zd d„Zd!d„Zd"d„Zd#d„Zd$d„Zd%d„Zˆ xZS )&ÚVertexaç  A Vertex in build123d represents a zero-dimensional point in the topological
    data structure. It marks the endpoints of edges within a 3D model, defining precise
    locations in space. Vertices play a crucial role in defining the geometry of objects
    and the connectivity between edges, facilitating accurate representation and
    manipulation of 3D shapes. They hold coordinate information and are essential
    for constructing complex structures like wires, faces, and solids.ç        c                 ó   — y)zDefault Vertext at the originN© ©Úselfs    új/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/build123d/topology/zero_d.pyÚ__init__zVertex.__init__X   ó   � ó    c                 ó   — y)z%Vertex from OCCT TopoDS_Vertex objectNr   )r    Úocp_vxs     r!   r"   zVertex.__init__\   r#   r$   c                 ó   — y)zVertex from three float valuesNr   )r    ÚXÚYÚZs       r!   r"   zVertex.__init__`   r#   r$   c                 ó   — y)z%Vertex from Vector or other iteratorsNr   )r    Úvs     r!   r"   zVertex.__init__d   r#   r$   c           	     óê  •— d| _         |j                  dd «      }|j                  dd «      }|j                  dd«      }|j                  dd«      }|j                  dd«      }|r+t        ddj                  |j	                  «       «      › �«      ‚|rMt        |d   t        «      r|d   }n4t        |d   t        «      r|d   }n|d d	 d
d	t        |«      z
  z  z   \  }}}|�0t        j                  t        j                  |g d¢«      d	«      \  }}}|€-t        t        t        |||«      «      j                  «       «      n|}t         ‰| �E  |«       | j%                  «       \  | _        | _        | _        y )Nr   r&   r,   r(   r)   r*   zUnexpected argument(s): ú, é   )r   )r   r   r   )Úvertex_indexÚpopÚ
ValueErrorÚjoinÚkeysÚ
isinstancer   r   ÚlenÚ	itertoolsÚisliceÚchainr   r   r   r   Úsuperr"   Úto_tupler(   r)   r*   )	r    ÚargsÚkwargsr&   r,   ÚxÚyÚzÚ	__class__s	           €r!   r"   zVertex.__init__h   sV  ø€ ØˆÔà—‘˜H dÓ+ˆØ�J‰J�s˜DÓ!ˆØ�J‰J�s˜AÓˆØ�J‰J�s˜AÓˆØ�J‰J�s˜AÓˆñ ÜÐ7¸¿	¹	À&Ç+Á+Ã-Ó8PÐ7QÐRÓSÐSáÜ˜$˜q™'¤=Ô1Ø˜a™‘Ü˜D ™G¤XÔ.Ø˜‘G‘à˜r ˜( T¨Q´°T³©]Ñ%;Ñ;‘��1�aàˆ=Ü×&Ñ&¤y§¡°qº)Ó'DÀaÓH‰GˆAˆq�!ð ˆ~ô Ô.¬v°a¸¸A«Ó?×FÑFÓHÔIàð 	ô 	‰Ñ˜Ô Ø!%§¡£ÑˆŒ�”˜�r$   c                 ó   — y)Nr   r   r   s    r!   Ú_dimzVertex._dim‹   s   € àr$   c                 ó   — y)z8volume - the volume of this Vertex, which is always zeror   r   r   s    r!   ÚvolumezVertex.volume�   s   € ð r$   c                ól   — t         j                  t        i}t        |«      } ||   t	        |«      «      S )z6Returns the right type of wrapper, given a OCCT object)ÚtaÚTopAbs_VERTEXr   r   r   )ÚclsÚobjÚconstructor_lutÚ
shape_types       r!   ÚcastzVertex.cast–   s7   € ô ×Ñœfð
ˆô ˜s“^ˆ
à*ˆ˜zÑ*¬8°C«=Ó9Ð9r$   c                ó   — t        d«      ‚)z&extrude - invalid operation for Vertexz&Vertices can't be created by extrusion©ÚNotImplementedError)rI   rJ   Ú	directions      r!   ÚextrudezVertex.extrude£   s   € ô "Ð"JÓKÐKr$   c                óÊ  — t        |t        «      rTt        | j                  |j                  z   | j                  |j                  z   | j                  |j                  z   «      }|S t        |t
        t        f«      r_t        |«      }t        | j                  |j                  z   | j                  |j                  z   | j                  |j                  z   «      }|S t        d«      ‚)aé  Add

        Add to a Vertex with a Vertex, Vector or Tuple

        Args:
            other: Value to add

        Raises:
            TypeError: other not in [Tuple,Vector,Vertex]

        Returns:
            Result

        Example:
            part.faces(">z").vertices("<y and <x").val() + (0, 0, 15)

            which creates a new Vertex 15 above one extracted from a part. One can add or
            subtract a `Vertex` , `Vector` or `tuple` of float values to a Vertex.
        zPVertex addition only supports Vertex,Vector or tuple(float,float,float) as input©r5   r   r(   r)   r*   r   ÚtupleÚ	TypeError©r    ÚotherÚ
new_vertexÚ	new_others       r!   Ú__add__zVertex.__add__ª   s¶   € ô, �eœVÔ$Ü §¡¨¯©Ñ 0°$·&±&¸5¿7¹7Ñ2BÀDÇFÁFÈUÏWÉWÑDTÓUˆJð Ðô ˜¤¬˜Ô/Ü˜u›ˆIÜØ—‘˜Ÿ™Ñ$ d§f¡f¨y¯{©{Ñ&:¸D¿F¹FÀYÇ[Á[Ñ<PóˆJð Ðô Øbóð r$   c                ó   — t        d«      ‚)zintersect operator +zVertices can't be intersectedrO   )r    r<   r=   s      r!   Ú__and__zVertex.__and__Í   s   € ä!Ð"AÓBÐBr$   c                ó   — d| _         | S )zInitialize to beginningr   )r0   r   s    r!   Ú__iter__zVertex.__iter__Ñ   s   € àˆÔØˆr$   c                ó:  — | j                   dk(  r#| xj                   dz  c_         | j                  }|S | j                   dk(  r#| xj                   dz  c_         | j                  }|S | j                   dk(  r#| xj                   dz  c_         | j                  }|S t        ‚)zreturn the next valuer   r   é   )r0   r(   r)   r*   ÚStopIteration)r    Úvalues     r!   Ú__next__zVertex.__next__Ö   s›   € à×Ñ Ò!Ø×Ò Ñ"ÕØ—F‘FˆEð ˆð ×Ñ !Ò#Ø×Ò Ñ"ÕØ—F‘FˆEð ˆð ×Ñ !Ò#Ø×Ò Ñ"ÕØ—F‘FˆEð ˆô  Ðr$   c                óV   — d| j                   › d| j                  › d| j                  › d�S )zoTo String

        Convert Vertex to String for display

        Returns:
            Vertex as String
        zVertex(r.   Ú))r(   r)   r*   r   s    r!   Ú__repr__zVertex.__repr__å   s+   € ð ˜Ÿ™˜  4§6¡6 (¨"¨T¯V©V¨H°AÐ6Ð6r$   c                óÊ  — t        |t        «      rTt        | j                  |j                  z
  | j                  |j                  z
  | j                  |j                  z
  «      }|S t        |t
        t        f«      r_t        |«      }t        | j                  |j                  z
  | j                  |j                  z
  | j                  |j                  z
  «      }|S t        d«      ‚)aS  Subtract

        Substract a Vertex with a Vertex, Vector or Tuple from self

        Args:
            other: Value to add

        Raises:
            TypeError: other not in [Tuple,Vector,Vertex]

        Returns:
            Result

        Example:
            part.faces(">z").vertices("<y and <x").val() - Vector(10, 0, 0)
        zJVertex subtraction only supports Vertex,Vector or tuple(float,float,float)rT   rW   s       r!   Ú__sub__zVertex.__sub__ï   s¶   € ô" �eœVÔ$Ü §¡¨¯©Ñ 0°$·&±&¸5¿7¹7Ñ2BÀDÇFÁFÈUÏWÉWÑDTÓUˆJð Ðô ˜¤¬˜Ô/Ü˜u›ˆIÜØ—‘˜Ÿ™Ñ$ d§f¡f¨y¯{©{Ñ&:¸D¿F¹FÀYÇ[Á[Ñ<PóˆJð Ðô Ø\óð r$   c                ó   — t        | «      S )z!The center of a vertex is itself!)r   r   s    r!   ÚcenterzVertex.center  s   € ä�d‹|Ðr$   c                óž   — t        j                  | j                  «      }|j                  «       |j	                  «       |j                  «       fS )z&Return vertex as three tuple of floats)r   ÚPnt_sÚwrappedr(   r)   r*   )r    Ú
geom_points     r!   r;   zVertex.to_tuple  s3   € ä—_‘_ T§\¡\Ó2ˆ
Ø—‘“ 
§¡£°
·±³Ð?Ð?r$   c                óB   — t        |j                  t        | «      «      Ž S )aó  Apply affine transform without changing type

        Transforms a copy of this Vertex by the provided 3D affine transformation matrix.
        Note that not all transformation are supported - primarily designed for translation
        and rotation.  See :transform_geometry: for more comprehensive transformations.

        Args:
            t_matrix (Matrix): affine transformation matrix

        Returns:
            Vertex: copy of transformed shape with all objects keeping their type
        )r   Úmultiplyr   )r    Út_matrixs     r!   Útransform_shapezVertex.transform_shape  s   € ô �x×(Ñ(¬°«Ó6Ð7Ð7r$   c                ó   — | S )zReturn the Vertexr   r   s    r!   ÚvertexzVertex.vertex%  s   € àˆr$   c                ó   — t        | f«      S )z)vertices - all the vertices in this Shape)r   r   s    r!   ÚverticeszVertex.vertices)  s   € ä˜$˜Ó!Ð!r$   )r&   r   )r(   Úfloatr)   rx   r*   rx   )r,   zIterable[float])ÚreturnÚint)ry   rx   )rJ   r   ry   r   )rJ   r   rQ   r   ry   r   )rX   z,Vertex | Vector | tuple[float, float, float]ry   r   )ry   Ústr)rX   zVertex | Vector | tuplery   r   )ry   r   )ry   ztuple[float, float, float])rr   r   ry   r   )ry   r   )ry   zShapeList[Vertex])Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úorderr   r"   ÚpropertyrC   rE   ÚclassmethodrM   rR   r[   r]   r_   rd   rg   ri   rk   r;   rs   ru   rw   Ú__classcell__)rA   s   @r!   r   r   M   s  ø„ ñJð €Eð ñ,ó ð,ð ò4ó ð4ð ò-ó ð-ð ò4ó ð4ô1ðF òó ðð òó ðð ò
:ó ð
:ð òLó ðLð!ØAð!à	ó!òFCòò
ó7óó<ó@ó
8ó÷"r$   r   c                ód  — t        | t        «      r| n| j                  }t        |t        «      r|n|j                  }|�|€t        d«      ‚t	        |t
        j                  «      }|j                  «       r»|j                  «       }t	        |t
        j                  «      }|j                  «       r`|j                  «       }|j                  |«      rt        t        j                  |«      «      S |j                  «        |j                  «       rŒ`|j                  «        |j                  «       rŒ»y)z'Given two edges, find the common vertexNzedge is empty)r5   r   rn   r2   r	   rG   rH   ÚMoreÚCurrentÚIsSamer   r
   ÚVertex_sÚNext)Úedge1Úedge2Útopods_edge1Útopods_edge2Úvert_expÚvertex1Ú	explorer2Úvertex2s           r!   Útopo_explore_common_vertexr’   .  sì   € ô ' u¬kÔ:‘5ÀÇÁ€LÜ& u¬kÔ:‘5ÀÇÁ€LàÐ˜|Ð3Ü˜Ó)Ð)ô ˜|¬R×-=Ñ-=Ó>€HØ
�-‰-Œ/Ø×"Ñ"Ó$ˆô $ L´"×2BÑ2BÓCˆ	Ø�n‰nÔØ×'Ñ'Ó)ˆGð �~‰~˜gÔ&ÜœfŸo™o¨gÓ6Ó7Ð7à�N‰NÔð �n‰nÕð 	�‰Œð �-‰-�/ð r$   )rŠ   úEdge | TopoDS_Edger‹   r“   ry   zVertex | None))r   Ú
__future__r   r7   Útypingr   r   Úcollections.abcr   Ú
OCP.TopAbsÚTopAbsrG   ÚOCP.BRepr   ÚOCP.BRepBuilderAPIr   Ú
OCP.TopExpr	   Ú
OCP.TopoDSr
   r   r   r   ÚOCP.gpr   Úbuild123d.geometryr   r   r   Útyping_extensionsr   Ú
shape_corer   r   r   r   Úone_dr   r   r   r’   r   r$   r!   Ú<module>r¢      sv   ðñ3õj #ã ß *å $å Ý Ý 8Ý &ß GÓ GÝ ß 9Ñ 9Ý "ç =Ó =ñ ß!ô^"ˆU�=Ñ!ô ^"ðBØðØ&8ðàôr$   