
    l0j:                        d 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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ZddZddZ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)
group_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copyconnected_components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               Q/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/trimesh/repair.pyfix_windingr2       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    Fc                    | j         sdS |r@t          j        | j                  }t	          |          dk    r!| j        dk     r|                                  dS t          j        t	          | j	                  t                    }| j        }| j        }|D ]6}t          j        ||         ||         d          d         }|dk     rd||<   7|                                rgd| j        v r*| j                                        }||xx         d	z  cc<   nd}t          j        | j	        |                   | j	        |<   ||| _        dS dS dS | j        dk     r|                                  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
    Nr   g        dtypeT)crossesskip_inertiavolumeface_normalsg      )is_watertightr   r   r   r$   r9   invertr   zerosr   boolr   triangles_crossmass_propertiesany_cacher:   r   fliplr)	r&   	multibodygroupsfliptricrossr   r9   normalss	            r1   fix_inversionrJ   Y   s      $+D,?@@v;;!{S  FxDJt444n$ 	# 	#E.E
E%Lt  F
 ||"U88:: 
	,,,+0022%!yD)9::DJt"$+!!!
	, 
	, #" 
s		 
	r3   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
    )rD   N)r2   rJ   )r&   rD   s     r1   fix_normalsrL      s+    , $),,,,,,r3   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      r1   
<listcomp>z broken_faces.<locals>.<listcomp>   s!    GGGDAqQar3   Nr   )   rO   )   r   r   rV   )r   r   r   dictdegreeitemsr   arraysizeshapevisualface_colors)r&   color	adjacencybrokens       r1   broken_facesrb      s    "  !455IGGD!1!1!3!344::<<GGGFXfFV[A--t##u{d':':$$$E*/'Mr3   c                 l   d }t          | j                  dk     rdS | j        rdS t          | j        d          }t          |          dk     rdS | j        |         }d |D             }t          j        t          j	        ||f                    }g }g }t          j
        |          D ]} ||          \  }	}
t          |	          d	k    r%t          j        |	          }	|	|	d	k     xx         t          |          t          | j                  z   t          |
          z   z  cc<   |                    |
           |                    |	           t          j        |          }t          j        |          }t          |          d	k    rdS t          |          D ]L\  }}|d
d         }| j         |j        | d                  }|d	         |d         k    }|s|d
d
d         ||<   Mt          |          d	k    rt          j        | j        |f          }n| j        }d| j        j        v r| j        j        d         }nd
}t'          j        ||                   \  }}|                                sdS | j        j        r| j        j        dk    r| j        j        }nd
}t          j        | j        d         ||         f          | _        || _        | j                                         |t          j        ||f          | _        |xt          j        |          }t          |          dk    rQt          j        |d         t          j        |          df          }t          j        ||f          }|| j        _        tA          j!        dt          j        |                     | j        S )ab  
    Fill single- triangle holes on triangular meshes by adding
    new triangles to fill the holes. New triangles will have
    proper winding and normals, and if face colors exist the color
    of the last face will be assigned to the new triangles.

    Parameters
    ------------
    mesh : trimesh.Trimesh
      Mesh will be repaired in- place
    c                     t          j        |           } t          |           dk    r| gg fS t          |           dk    r| g d         }| g d         }||gg fS g g fS )a  
        Given a loop of vertex indices  representing a hole
        turn it into triangular faces.
        If unable to do so, return None

        Parameters
        -----------
        hole : (n,) int
          Ordered loop of vertex indices

        Returns
        ---------
        faces : (n, 3) int
          New faces
        vertices : (m, 3) float
          New vertices
        r   rU   )r   r   r   )r   r   r   )r   
asanyarrayr$   )holeface_Aface_Bs      r1   hole_to_facesz!fill_holes.<locals>.hole_to_faces   sp    $ }T""t99>>62:t99>>)))_F)))_FF#R''2vr3   r   FTr   r   c                     g | ]}d |iS )indexrP   )rQ   is     r1   rT   zfill_holes.<locals>.<listcomp>  s    ;;;agq\;;;r3   )rf   r   Nr   rk   r   r:   facer   z Filled in mesh with %i triangles)"r$   r   r;   r   edges_sortedr.   r   r   r   column_stackcycle_basisrZ   verticesextend	enumerateget_edge_datavstackrB   cacher   rI   rA   r]   definedkindr^   _dataverifyr:   r\   tilesumr   r%   )r&   ri   boundary_groupsboundary_edgesindex_as_dictr*   	new_faces
new_vertexrf   r   vertex
face_indexrm   	edge_testedge_boundaryreversednew_verticescached_normalsnew_normalsvalidr_   color_shape
new_colorss                          r1   
fill_holesr      s     : 4:u t !!2!DDDO
 ?auZ0N;;?;;;M 	.-)HIIJJAIJq!!     &4000vu::?? eaiC
OOc$-.@.@@3v;;NN&!!!##I*%%J
9~~ u%i00 / /
D !H	
?1?I#>w#GH
 Q<=#33 	/$(2JIj! :!y$-!<==} ****>: #*<	+BCCK99;; u { t{/699' DJw/51ABCCDJ DM 	K !I~{&CDD  huoo{q  rRVE]]A,>??JE:#677J&0DK#I0"&--@@@r3   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)rQ   es     r1   rT   zstitch.<locals>.<listcomp>~  sM       qx==1!!<!< 	
!<!<!<r3   entitiesc                 H    g | ]}|                              d           S )r   r   )mean)rQ   prq   s     r1   rT   zstitch.<locals>.<listcomp>  s.    GGG1hqk..A.66GGGr3   c           	          g | ][\  }}t          j        t          j        t          |          d z
  t                    |z   z  |dd         |d d         f          \S )r   r5   Nr   r   ro   onesr$   int)rQ   rl   r   counts      r1   rT   zstitch.<locals>.<listcomp>  su     
 
 
1 ORWSVVaZs;;;uqyI1SbS6STUVUWUWSXYZZ
 
 
r3   c           	          g | ][}t          j        t          j        t          |          d z
  t                    |d         z  |dd         |dd         f          \S )r   r5   r   r   r   r   r   )rQ   r   s     r1   rT   zstitch.<locals>.<listcomp>  sl     
 
 
 ORWSVVaZs;;;adBAadGQqQStWUVV
 
 
r3   
   r   r   g|=)rr   r   )r   aranger$   r   r
   rq   r:   
edges_faceedges_sorted_treerZ   ru   rs   zipranger   r#   query_ball_pointconcatenater   rI   rA   dotr   rC   )r&   r   insert_verticesr   rI   r   	tree_edge	centroidsfanrl   tr   query
edge_indexoriginalcheckr   signr   rq   s                     @@r1   stitchr   b  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~Jr3   )F)N)NF)__doc__numpyr    r   r   	constantsr   geometryr   groupingr   networkxr   BaseExceptionE
exceptionsr	   path.exchange.miscr
   r2   rJ   rL   rb   r   r   rP   r3   r1   <module>r      s                      $ $ $ $ $ $                -,,,,,		!		BBBBBB(1111111 ( ( (,,,,,,$$Q''MMMMMM(6C 6C 6Cr3 3 3 3l- - - -6   :X X Xvd d d d d ds,   ' AAAA A2A--A2