
    ^j/                    (   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
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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,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)Self
deprecated)	BRep_Tool)BRepBuilderAPI_MakeVertex)TopExp_Explorer)TopoDSTopoDS_ShapeTopoDS_VertexTopoDS_Edge)gp_Pnt)MatrixVector
VectorLikeLocationAxisPlane)Keep   )Shape	ShapeListTrimmingTooldowncast	shapetype)EdgeWirec                  Z    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!dZ	 	 	 	 d"dZd Zd Zd#dZd$dZd%dZej*                  fd&dZ ed      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    R/opt/ringagent/.cad-venv/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           	     T   |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        	| A  |       t#        j$                  | j&                        }|j)                         |j+                         |j-                         c| _        | _        | _        y )Nr,   r2   r.   r   r/   r0   zUnexpected argument(s): ,    )r   )r   r   r   )pop
ValueErrorjoinkeys
isinstancer   r   len	itertoolsislicechainr   r
   r   r!   superr(   r	   Pnt_swrappedr.   r/   r0   )
r&   argskwargsr,   r2   xyzpnt	__class__s
            r'   r(   zVertex.__init__h   sp   Hd+JJsD!JJsAJJsAJJsA 7		&++-8P7QRSS$q'=1aDGX.Gr(TQT]%;;1a=&&yq)'DaHGAq! ~ .vaA?FFHI 	 	 oodll+!$#%%'3557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                    t         j                  t        i}t        |      } ||   t	        j                  |            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   s<     f
 s^
*z*6==+=>>r*   c                    t        d      )z&extrude - invalid operation for Vertexz&Vertices can't be created by extrusionNotImplementedError)rP   rQ   	directions      r'   extrudezVertex.extrude   s     ""JKKr*   c                   t        |t              rt        |      }nt        |t              rt        |j                        }n{t        |t
              r-|j                  | j                               rt        | g      S dS t        |t              r.|j                  | j                         |      rt        | g      S dS t        |t              r"| j                  |      |k  rt        | g      S dS |j                  | ||      S )a  Single-object intersection for Vertex.

        For a vertex (0D), intersection means the vertex lies on/in the other shape.

        Args:
            other: Shape or geometry object to intersect with
            tolerance: tolerance for intersection detection
            include_touched: if True, include boundary contacts
                (only relevant when Solids are involved)
        N)r:   r   r!   r   positionr   	intersectcenterr   r   containsdistance_to
_intersect)r&   other	toleranceinclude_toucheds       r'   r`   zVertex._intersect   s    " eV$5MEx(5>>*Et$(-(F9dV$PDPu% &+^^DKKM9%M	4&!SW eV$(,(8(8(?9(L9dV$VRVV iAAr*   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r:   r!   r.   r/   r0   r   tuple	TypeErrorr&   ra   
new_vertex	new_others       r'   __add__zVertex.__add__   s    , eV$ 0$&&5772BDFFUWWDTUJ  /uI$dffy{{&:DFFY[[<PJ  b r*   c                    t        d      )zintersect operator +zVertices can't be intersectedrV   )r&   rB   rC   s      r'   __and__zVertex.__and__   s    !"ABBr*   c                Z    t        | j                  | j                  | j                  f      S )zInitialize to beginning)iterr.   r/   r0   r%   s    r'   __iter__zVertex.__iter__   s     TVVTVVTVV,--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(r4   ))r.   r/   r0   r%   s    r'   __repr__zVertex.__repr__   s+     466("TVVHA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      )aR  Subtract

        Subtract 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)re   rh   s       r'   __sub__zVertex.__sub__  s    " eV$ 0$&&5772BDFFUWWDTUJ  /uI$dffy{{&:DFFY[[<PJ  \ r*   c                    t        |       S )z!The center of a vertex is itself!)r   r%   s    r'   r]   zVertex.center!  s    d|r*   c                    t        d      )zsplit - not implementedzVertices cannot be split.rV   )r&   toolkeeps      r'   splitzVertex.split%  s    !"=>>r*   zUse 'tuple(Vertex)' instead.c                    t        j                  | j                        }|j                         |j	                         |j                         fS )z&Return vertex as three tuple of floats)r	   r@   rA   r.   r/   r0   )r&   
geom_points     r'   to_tuplezVertex.to_tuple)  s5     __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.verticesB  s    $!!r*   )r,   r   )r.   floatr/   r   r0   r   )r2   zIterable[float])returnint)r   r   )rQ   r   r   r   )rQ   r   rX   r   r   r!   )gư>F)ra   z(Shape | Vector | Location | Axis | Planerb   r   rc   boolr   zShapeList | None)ra   z,Vertex | Vector | tuple[float, float, float]r   r!   )r   str)ra   zVertex | Vector | tupler   r!   )r   r   )rx   r   ry   r   )r   ztuple[float, float, float])r   r   r   r!   )r   r!   )r   zShapeList[Vertex])__name__
__module____qualname____doc__orderr   r(   propertyrJ   rL   classmethodrT   rY   r`   rk   rm   rp   rs   ru   r]   r   TOPrz   r   r}   r   r   r   __classcell__)rH   s   @r'   r!   r!   M   sZ   J E , , 4 4 - - 4 4;D     
? 
? L L   %	#B7#B #B 	#B
 
#BN!A!	!FC.7< 6:XX ? ./@ 0@
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)r:   r   rA   r7   r   rN   rO   MoreCurrentIsSamer!   r   Next)edge1edge2topods_edge1topods_edge2vert_expvertex1	explorer2vertex2s           r'   topo_explore_common_vertexr   G  s     'uk:5L&uk:5L|3)) |R-=-=>H
--/""$ $L"2B2BC	nn'')G ~~g&fmmG455NN nn 	 --/ r*   )r   Edge | TopoDS_Edger   r   r   zVertex | None)0r   
__future__r   r<   typingr   r   collections.abcr   typing_extensionsr   r   
OCP.TopAbsTopAbsrN   OCP.BRepr	   OCP.BRepBuilderAPIr
   
OCP.TopExpr   
OCP.TopoDSr   r   r   r   OCP.gpr   build123d.geometryr   r   r   r   r   r   build123d.build_enumsr   
shape_corer   r   r   r   r   one_dr   r   r!   r   r$   r*   r'   <module>r      sw   3j #  * $ .   8 & G G  P P & K K!w"U=! w"t&8r*   