
    j!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	 dd
lmZ d ZddefdZddZddZddefdZddZy# e$ rZdd	lmZ  ee      ZY dZ[=dZ[ww xY w# e$ rZdd	lmZ  ee      ZY dZ[VdZ[ww xY w)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yt        j                  | j                        }d}| j                  j                  t        j                        j                         }t        j                  |      D ]  }|j                  |      }t        t        |j                                     }t        j                  ||      D ]  }t        j                  |      }||   }t!        |      }	t#        t        j$                  |	d      d      }
t'        |
      dk(  rY|	|
d      }|d   d   |d   d   k(  ss|dz  }||d      ddd   ||d   <     |dkD  r|| _        t)        j*                  d|t'        | j                        d	z         y)
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               ;/DATA/.local/lib/python3.12/site-packages/trimesh/repair.pyfix_windingr4   !   se    !!  !4!45IGJJOOBJJ',,.E --i8
z* T!'')_% a/I
 +I#D"4(E Q!7qIG7|q gaj)I|A)A,q/11&+IaL&9$B$&?il#% 0 9: {
II#Wc$**o.AB    	multibodyc                 @   |s,| j                   r| j                  dk  r| j                          yt        | j                        }t        j                  t        | j                        t              }| j                  }| j                  }| j                  j                  d      }|D ]s  }t        |      dk  rt        j                   ||   j                  d            \  }}	|r|	sAt        j                  ||   ||   d      j                  }
|
dk  sod||<   u |j!                         rrd	| j"                  v r(| j$                  j'                         }||xx   d
z  cc<   nd}t        j(                  | j                  |         | j                  |<   ||| _        yyy)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KKM "$"5"56F 88C

O40D ..C  E##G,J 
 z?Q"00J1G1O1OPW1XY( X **
OU:%6T

& 	
 C<#D' , xxzT[[('',,.GDMT!MG99TZZ%56

4 'D  r5   c                 4    t        |        t        | |       y)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                    t        j                  | j                        }t        |j	                               j                         D cg c]  \  }}|dk7  s| }}}t        j                  |      }|_|j                  dk7  rPt        j                  |      }|j                  dk(  s|j                  dk(  sg d}|| j                  j                  |<   |S c c}}w )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
    r   r   )r;   )r   )   r   r   rW   )r   r   r   dictdegreeitemsr   arraysizeshapevisualface_colors)r(   color	adjacencykvbrokens         r3   broken_facesre      s    "   !4!45I !1!1!34::<G<DAqQa<FGXXfFV[[A-t#u{{d':$E*/'M Hs   
CCuse_fanc                 \   t        | j                        dk  ry| j                  ryt        | j                  d      }t        |      dk  ry| j
                  |   }t        j                  t        j                  |            }t        ||      }t        |      dk(  ryt        |      }t        |      }t        |      }t        j                  ||      j                  d      j                  d	      }	t        j                   ||	         ||	<   | 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   )rf   r   )r   r   r   )r&   r   r?   r
   edges_sortedr0   r   cycle_basisr   r   r   r   r   isinrE   rG   rI   extend_faces)
r(   rf   boundary_groupsboundaryholes	new_faces	new_edgeshashable_newhashable_oldneeds_reverses
             r3   
fill_holesrt      s     4:: !!2!2!DO
?a zz/*H NN2++H56E "%9I
9~ y)I !+L *L GGL,7??HLLRSLTM  "yy=)ABIm 	i r5   c                 
   |(t        j                  t        | j                              }t	        | |      d   D cg c]E  }t        |j
                        dkD  r+|j
                  d   |j
                  d   k(  r|j
                  G }}| j                  }| j                  }| j                  }| j                  }|rt        j                  |D 	cg c]  }	||	   j                  d       c}	      }
t        |      }t        j                  ||
f      }t        |      D 	cg c]N  \  }}	t        j                  t        j                  t        |	      dz
  t               ||z   z  |	dd |	dd f      P }}}	nV|D 	cg c]K  }	t        j                  t        j                  t        |	      dz
  t               |	d   z  |	dd	 |	d
d f      M }}	t#        t%        t        |            |      D ]  \  }}|ddddf   j'                         }|j)                  d       |j+                  |d      }t        |      dk(  rSt        j,                  |      }|||      }t/        j0                  ||dd          \  }}|j3                         s|d   }t        j4                  ||      }|j                         dk  st        j6                  |      ||<    t        j                  |      }|r|
fS |S c c}w c c}	w c c}	}w c c}	w )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`)
    Nentitiesr   r   r   r   r   r8   r   
   g|=)r)r   aranger&   r   r   pointsverticesr>   
edges_faceedges_sorted_treer[   meanvstack	enumeratecolumn_stackonesintzipranger   r%   query_ball_pointconcatenater   rR   rG   dotrI   )r(   r   insert_verticeser{   r|   rR   r}   	tree_edgep	centroidscountifantquery
edge_indexoriginalcheckvalidsigns                        r3   stitchr     s   , }		#djj/* tU+J77Aqxx=1!!< 	
7   }}HG J&&IHHG1hqk..A.6GH	H99h	23
 "&)
)1 OORWWSVaZs;uqyI1Sb6STUVUWSXYZ) 	 
 
 OORWWSVaZs;adBAaGQqQSWUV 	 
 E#c(OS)1 crc12gJOO	A
 **1*6u:?^^E*
 :j12 !((!BQ%9uyy{a vvh&99;?YYq\CF; *> ))C.CI~JQ  H

s   A
K0K5 AK:;AL )F)N)NF)__doc__numpyr    r   r   	constantsr   geometryr   r   r	   groupingr
   r   networkxr   BaseExceptionE
exceptionsr   path.exchange.miscr   r4   rC   rS   rU   re   rt   r    r5   r3   <module>r      s       7 ' /(16CrB(4 B(J-6:=d =@dK   -	!	B  (,$Q'M(s.   A A5 A2A--A25B:BB