
    Ti!0                     <   d 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mZ 	 ddlZn## e$ rZdd	lmZ  ee          ZY dZ[ndZ[ww xY w	 dd
lmZ n## e$ rZdd	lmZ  ee          ZY dZ[ndZ[ww xY wd ZddefdZddZddZddefdZddZdS )zL
repair.py
-------------

Fill holes and fix winding and normals of meshes.
    N   )graph	triangles)log)faces_to_edgestriangulate_quads)connected_components)
group_rowshashable_rows)ExceptionWrapper)faces_to_pathc                 |   | j         rdS t          j        | j                  }d}| j                            t          j                                                  }t          j	        |          D ]}|
                    |          }t          t          |                                                    }t          j        ||          D ]}t          j        |          }||         }t!          |          }	t#          t          j        |	d          d          }
t'          |
          dk    rf|	|
d                  }|d         d         |d         d         k    r%|dz  }||d                  ddd         ||d         <   |dk    r|| _        t)          j        d|t'          | j                  d	z             dS )
z
    Traverse and change mesh faces in-place to make sure
    winding is correct with edges on adjacent faces in
    opposite directions.

    Parameters
    -------------
    mesh : Trimesh
      Source geometry to alter in-place.
    Nr   r   axis   require_countzflipped %d/%d edges   )is_winding_consistentnxfrom_edgelistface_adjacencyfacesviewnpndarraycopyr	   subgraphnextiternodes	bfs_edgesravelr   r
   sortlenr   debug)mesh	graph_allflippedr   
componentsgstart	face_pairpairedgesoverlap	edge_pairs               H/DATA/AppData/hermes/venv/lib/python3.11/site-packages/trimesh/repair.pyfix_windingr4   !   s    !  !455IGJOOBJ'',,..E -i88 @ @
z** T!''))__%% a// 	@ 	@I
 ++I#D"4((E Q!7!7!7qIIIG7||q  gaj)I|A)A,q/111&+IaL&9$$B$&?il#%	@( {{
I#Wc$*oo.ABBBBB    F	multibodyc                 (   |s(| j         r| j        dk     r|                                  dS t          | j                  }t          j        t          | j                  t                    }| j
        }| j        }| j                            d          }|D ]~}t          |          dk     rt          j         ||                             d                    \  }}	|r|	sKt          j        ||         ||         d          j        }
|
dk     rd||<   |                                rgd	| j        v r*| j                                        }||xx         d
z  cc<   nd}t          j        | j        |                   | j        |<   ||| _        dS dS dS )z
    Check to see if a mesh has normals pointing "out."

    Parameters
    -------------
    mesh : trimesh.Trimesh
      Mesh to fix in-place.
    multibody : bool
      If True will try to fix normals on every body
    g        Ndtype)r         )r   r   T)crossesskip_inertiaface_normalsg      )is_watertightvolumeinvertr	   r   r   zerosr&   r   boolr   triangles_crossr0   reshaper   mass_propertiesany_cacher>   r   fliplr)r(   r6   groupsfliptricross
face_edges
face_indexis_tightis_woundr@   normalss               r3   fix_inversionrS   Z   s      	$+"3"3KKMMM "$"566F 8C
OO4000D .C E##G,,J  $ $
 z??Q"0J1G1O1OPW1X1XYY(  	X 	 *
OU:%6T
 
 

 	
 C<<#D xxzz (T[((',,..GDMMMT!MMMMG9TZ%566
4 'D( ( r5   c                 F    t          |            t          | |           dS )a  
    Fix the winding and direction of a mesh face and
    face normals in-place.

    Really only meaningful on watertight meshes but will orient all
    faces and winding in a uniform way for non-watertight face
    patches as well.

    Parameters
    -------------
    mesh : trimesh.Trimesh
      Mesh to fix normals on
    multibody : bool
      if True try to correct normals direction
      on every body rather than just one

    Notes
    --------------
    mesh.faces : will flip columns on inverted faces
    )r6   N)r4   rS   )r(   r6   s     r3   fix_normalsrU      s+    , $),,,,,,r5   c                 n   t          j        | j                  }d t          |                                                                          D             }t          j        |          }|H|j        dk    r=t          j        |          }|j	        dk    s|j	        dk    sg d}|| j
        j        |<   |S )al  
    Return the index of faces in the mesh which break the
    watertight status of the mesh.

    Parameters
    --------------
    mesh : trimesh.Trimesh
      Mesh to check broken faces on
    color: (4,) uint8 or None
      Will set broken faces to this color if not None

    Returns
    ---------------
    broken : (n, ) int
      Indexes of mesh.faces
    c                 $    g | ]\  }}|d k    |S r    ).0kvs      r3   
<listcomp>z broken_faces.<locals>.<listcomp>   s!    GGGDAqQar5   Nr   )r;   rX   )   r   r   r^   )r   r   r   dictdegreeitemsr   arraysizeshapevisualface_colors)r(   color	adjacencybrokens       r3   broken_facesrj      s    "  !455IGGD!1!1!3!344::<<GGGFXfFV[A--t##u{d':':$$$E*/'Mr5   use_fanc                    t          | j                  dk     rdS | j        rdS t          | j        d          }t          |          dk     rdS | j        |         }t          j        t          j        |                    }t          ||          }t          |          dk    rdS t          |          }t          |          }t          |          }t          j        ||                              d                              d	          }	t          j        ||	                   ||	<   |                     |           | j        S )
a  
    Fill boundary holes in-place using fans, which may result
    in bad answers if the holes are non convex!

    Face colors and attributes will be padded with default values
    so shapes match.

    Parameters
    ----------
    mesh : trimesh.Trimesh
        Mesh will be repaired in-place.
    use_fan
      If passed, holes larger than quads will be triangulated
      using fans which are only valid for non-convex holes.
    r   FTr   r   )rk   r   )r   r   r   )r&   r   r?   r
   edges_sortedr0   r   cycle_basisr   r   r   r   r   isinrE   rG   rI   extend_faces)
r(   rk   boundary_groupsboundaryholes	new_faces	new_edgeshashable_newhashable_oldneeds_reverses
             r3   
fill_holesry      sG     4:u t !!2!DDDO
?au z/*H N2+H5566E "%999I
9~~u y))I !++L **L GL,77??HHLLRSLTTM  "y=)ABBIm 	i   r5   c                 Z   |&t          j        t          | j                            }d t	          | |          d         D             }| j        | j        }| j        }| j        }|rat          j	        fd|D                       }t                    t          j
        |f          fdt          |          D             }nd |D             }t          t          t          |                    |          D ]\  }	}
|
ddddf                                         }|                    d	           |                    |d
          }t          |          dk    rgt          j        |          }|||                  }t%          j        |
dd                            \  }}|                                s|d         }t          j        ||          }|                                dk     rt          j        |
          ||	<   t          j
        |          }|r||fS |S )aC  
    Create a fan stitch over the boundary of the specified
    faces. If the boundary is non-convex a triangle fan
    is going to be extremely wonky.

    Parameters
    -----------
    mesh : trimesh.Trimesh
      Mesh to create fan stitch on.
    faces : (n,) int
      Face indexes to stitch with triangle fans.
    insert_vertices : bool
      Allow stitching to insert new vertices?

    Returns
    ----------
    fan : (m, 3) int
      New triangles referencing mesh.vertices.
    vertices : (p, 3) float
      Inserted vertices (only returned `if insert_vertices`)
    Nc                     g | ]=}t          |j                  d k    |j        d         |j        d         k    6|j        >S )r   r   r   )r&   points)rZ   es     r3   r]   zstitch.<locals>.<listcomp>3  sM       qx==1!!<!< 	
!<!<!<r5   entitiesc                 H    g | ]}|                              d           S )r   r   )mean)rZ   pverticess     r3   r]   zstitch.<locals>.<listcomp>C  s.    GGG1hqk..A.66GGGr5   c           	          g | ][\  }}t          j        t          j        t          |          d z
  t                    |z   z  |dd         |d d         f          \S )r   r8   Nr   r   column_stackonesr&   int)rZ   ir   counts      r3   r]   zstitch.<locals>.<listcomp>J  su     
 
 
1 ORWSVVaZs;;;uqyI1SbS6STUVUWUWSXYZZ
 
 
r5   c           	          g | ][}t          j        t          j        t          |          d z
  t                    |d         z  |dd         |dd         f          \S )r   r8   r   r   r   r   r   )rZ   r   s     r3   r]   zstitch.<locals>.<listcomp>Q  sl     
 
 
 ORWSVVaZs;;;adBAadGQqQStWUVV
 
 
r5   
   r   r   g|=)rr   r   )r   aranger&   r   r   r   r>   
edges_faceedges_sorted_treerb   vstack	enumeratezipranger   r%   query_ball_pointconcatenater   rR   rG   dotr   rI   )r(   r   insert_verticesr|   rR   r   	tree_edge	centroidsfanr   tr}   query
edge_indexoriginalcheckvalidsignr   r   s                     @@r3   stitchr     sM   , }	#dj//**
 tU++J7  F }HG J&I 
HGGGGGGGHH	H9h	233
 
 
 
!&))
 
 

 

 
 
 E#c((OOS)) " "1 crc122gJOO	A
 **1*66u::??^E**
 :j12 !(!BQB%99uyy{{ 	a vh&&99;;??Yq\\CF
)C..C I~Jr5   )F)N)NF)__doc__numpyr    r   r   	constantsr   geometryr   r   r	   groupingr
   r   networkxr   BaseExceptionE
exceptionsr   path.exchange.miscr   r4   rC   rS   rU   rj   ry   r   rY   r5   r3   <module>r      s                      7 7 7 7 7 7 7 7 ' ' ' ' ' ' / / / / / / / /    -,,,,,		!		BBBBBB(1111111 ( ( (,,,,,,$$Q''MMMMMM(6C 6C 6CrB( B(4 B( B( B( B(J- - - -6   := =d = = = =@d d d d d ds,   1 AAAA A<!A77A<