
    l0jX:                     X    d Z ddlZddlmZmZmZ ddlmZ ddl	m
Z
 	 ddZdd
ZddZdS )zJ
remesh.py
-------------

Deal with re- triangulation of existing meshes.
    N   )graphgroupingutil)tol)faces_to_edgesFc                    |)t          j        t          |          t                    }n-t          j        t          |          t                    }d||<   ||         }t          j        t          |          d          }t          j        |          \  }}	| ||                  	                    d          }
|	
                    d          t          |           z   }t          j        |dddf         |dddf         |dddf         |dddf         |dddf         |dddf         |dddf         |dddf         |dddf         |dddf         |dddf         |dddf         g          
                    d          }t          j        ||          |f          }t          j        | |
f          }|si }|                                D ]W\  }}||         t          j        fd	ddgddgddgfD                       }||         }t          j        ||f          ||<   X|||fS |rt          j        |          d         }t          |          t          |          z
  }t          j        ||t          |          d
z  z             
                    d          }t!          t#          ||                    }|||fS ||fS )a  
    Subdivide a mesh into smaller triangles.

    Note that if `face_index` is passed, only those
    faces will be subdivided and their neighbors won't
    be modified making the mesh no longer "watertight."

    Parameters
    ------------
    vertices : (n, 3) float
      Vertices in space
    faces : (m, 3) int
      Indexes of vertices which make up triangular faces
    face_index : faces to subdivide.
      if None: all faces of mesh will be subdivided
      if (n,) int array of indices: only specified faces
    vertex_attributes : dict
      Contains (n, d) attribute data
    return_index : bool
      If True, return index of original face for new faces

    Returns
    ----------
    new_vertices : (q, 3) float
      Vertices in space
    new_faces : (p, 3) int
      Remeshed faces
    index_dict : dict
      Only returned if `return_index`, {index of
      original face : index of new faces}.
    NdtypeTr   axis   r      c                 V    g | ]%}d d |d d f                              d          &S )Nr   r   )mean).0g	attr_triss     Q/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/trimesh/remesh.py
<listcomp>zsubdivide.<locals>.<listcomp>_   s=    SSSQ111a7#((a(00SSS       )r   r   )nponeslenboolzerossortr   r   unique_rowsr   reshapecolumn_stackvstackitemsnonzeroarangedictzip)verticesfaces
face_indexvertex_attributesreturn_index	face_maskfaces_subsetedgesuniqueinversemidmid_idxf	new_facesnew_verticesnew_attributeskeyvaluesattr_midr&   startstack
index_dictr   s                          @r   	subdivider@      s   D GCJJd333		HSZZt444	 $	* #L GN<00q999E*511OFG
5=
!
&
&A
&
.
.Coog&&X6G 	AAAAqDMAAAqDMAAAqDMAAAAqDMAAAqDMAAAqDMAAAAqDMAAAqDMAAAqDM	
	 	 gg ( 	5),a011I9h_--L$,2244 	@ 	@KC|,IySSSSAq6Aq6Aq6:RSSS H  'H"$)VX,>"?"?N3Y66 3*Y''* E

S\\)	%Q!!344<<WEE#gu--..
Y
22""r   
   c                    g }g }g }t          j        |t           j        d          }t          j        | t           j        d          }	t          j        t          |                    }
t          |dz             D ]m}t          j        |	|ddg df         ddf         d          dz                      d          d	z  }||k    	                    d          }| }t          j        ||                                         d
          \  }}|                    |	|                    |                    |                    d                     |rM|                    |
|                    t          j        |
|         d          j                                        }
|	                                s n0||k    rt%          d          t'          |	||                   \  }	}ot)          j        ||          \  }}|r;t          j        |          }t          |          t          |          k    sJ |||fS ||fS )a  
    Subdivide a mesh until every edge is shorter than a
    specified length.

    Will return a triangle soup, not a nicely structured mesh.

    Parameters
    ------------
    vertices : (n, 3) float
      Vertices in space
    faces : (m, 3) int
      Indices of vertices which make up triangles
    max_edge : float
      Maximum length of any edge in the result
    max_iter : int
      The maximum number of times to run subdivision
    return_index : bool
      If True, return index of original face for new faces

    Returns
    ------------
    vertices : (j, 3) float
      Vertices in space
    faces : (q, 3) int
      Indices of vertices
    index : (q, 3) int
      Only returned if `return_index`, index of
      original face for each new face.
    T)r   copyr   N)r   r   r   r   r   r   r   g      ?return_inverser   )r   r   zmax_iter exceeded!)r   arrayint64float64r'   r   rangediffsumanyr   unique_bincountflattenappendr"   tileTravel
ValueErrorr@   r   append_facesconcatenate)r*   r+   max_edgemax_iterr.   	done_face	done_vertdone_idxcurrent_facescurrent_verticescurrent_indexiedge_lengthtoo_longface_okr2   r3   final_verticesfinal_facesfinal_indexs                       r   subdivide_to_sizere   u   s_   > IIH HU"(>>>Mx
FFF Ic%jj))M 8a<   #
 #
 G$]111lll?%CRaR%GHqQQQUVV
#1#++  (*//Q/77) #2'"**,,T
 
 

 	)&122211222 	OOOM'2333GM($;VDDFLLNNM ||~~ 	E ==1222 -6mH5-
 -
)	==
 #'"3Iy"I"INK 8nX..;3{#3#33333{K77;&&r   c                    	 ddl m n# t          $ r	 ddl m Y nw xY w|d}fdt	          |          D ]} | |          \  } }t
          j        s	 t          j        |           	                                sJ t          j        |          	                                sJ t          j        | |                   	                                sJ |ddddf         |ddddf         k    	                                sJ | |fS )a  
    Subdivide a mesh by dividing each triangle into four triangles
    and approximating their smoothed surface (loop subdivision).
    This function is an array-based implementation of loop subdivision,
    which avoids slow for loop and enables faster calculation.

    Overall process:
    1. Calculate odd vertices.
      Assign a new odd vertex on each edge and
      calculate the value for the boundary case and the interior case.
      The value is calculated as follows.
          v2
        / f0 \        0
      v0--e--v1      /   \
        \f1 /     v0--e--v1
          v3
      - interior case : 3:1 ratio of mean(v0,v1) and mean(v2,v3)
      - boundary case : mean(v0,v1)
    2. Calculate even vertices.
      The new even vertices are calculated with the existing
      vertices and their adjacent vertices.
        1---2
       / \/ \      0---1
      0---v---3     / \/ \
       \ /\/    b0---v---b1
        k...4
      - interior case : (1-kB):B ratio of v and k adjacencies
      - boundary case : 3:1 ratio of v and mean(b0,b1)
    3. Compose new faces with new vertices.

    Parameters
    ------------
    vertices : (n, 3) float
      Vertices in space
    faces : (m, 3) int
      Indices of vertices which make up triangles

    Returns
    ------------
    vertices : (j, 3) float
      Vertices in space
    faces : (q, 3) int
      Indices of vertices
    iterations : int
          Number of iterations to run subdivision
    r   )zip_longest)izip_longestNr   c                    t          |d          \  }}|                    d           t          j        |          \  }}t	          j        t          j        |d          d          }t          j        |d          }t          |          dz  t          |          z   t          |          k    st          j        ||                   }t          |          dk    rt          d          g }	g }
d}|D ]}t          j
        ||                             d	          d
          \  }} (| |         |                    d                    \  }}||z  }|t          |          z  }|	                    |           |
                    |           t	          j        |	          t	          j        |
          fS t	          j        t          |          t                    }d||<   ||         }| }t	          j        t          |                                        t"                    }|d d df         ||d d df         <   |d d df         ||d d df         <   ||         }|||                  }| ||                                      d          }|||                  }| |         d d df         }| |         d d df         }|||                  }|||                  }||d d d d d f         |d d d d d f         k                        d	                    }||d d d d d f         |d d d d d f         k                        d	                    }| |         }| |         }d|z  d|z  z   |dz  z   |dz  z   ||<   t          j        ||         t          |                     }t	          j        t-           )|dd	i                    j        }t	          j        | g dg          }|dz                       t                                        d          } t	          j        |           }!ddt	          j        dt          j        z  | z            z  dz   dz  z
  d| z  z  }"|"d d d f         ||                             d          z  d| d d d f         |"d d d f         z  z
  | z  z   }!|                                rt	          j        t          |           t                    }#d|#t	          j        ||         |                    <   ||#         }$d	|$|#||#                   <   ||$                             d          dz  d| |#         z  z   |!|#<   |                    d          t          |           z   }%t	          j        |d d df         |%d d df         |%d d df         |%d d df         |d d df         |%d d df         |%d d df         |%d d df         |d d df         |%d d df         |%d d df         |%d d df         g                              d          }&t	          j        |!|f          }'|'|&fS )NT)r.   r   r   r   )require_countz*Some edges are shared by more than 2 facesr   r   rD   r   )r*   r+   r
   g      ?g       @)r1   	max_index	fillvalue)        rm   rm   g      D@g       @r   @   g      ?)r   r    r   r!   r   
group_rowsr   r   connected_componentsrS   rM   r"   rO   r$   r   r   astypeintr   rL   	neighborsrF   listrQ   rK   
zeros_likecospir2   r#   )*r*   r+   r1   
edges_facer2   r3   
edge_inter
edge_boundfaces_group	seq_verts	seq_facescountr6   	cur_verts	cur_facesedge_bound_maskedge_inter_mask	edge_pairopposite_face1opposite_face2oddee_v0e_v1e_f0e_f1e_v2_idxe_v3_idxe_v2e_v3rs   	vertices_kevenbetavrt_bound_maskboundary_neighborsodd_idxr7   r8   
_subdividerg   s*                                           r   r   z"subdivide_loop.<locals>._subdivide  s   *5tDDDz


".u55 WX0aHHHqQQQ
(a@@@
:"S__4E

BB
  4Z
5KLLK;1$$ !MNNN II E  , ,
 #+":!H$$R((# # #
 (2z%f-W__W5M5M( ( ($	9 U"	Y'  +++  ++++ 9Y''9)=)=== (3u::T:::&*
#)&1** HSZZ((//44	&0A&6	*QQQT"#&0A&6	*QQQT"##F+#If$56 uV}%***22&)*{111a4 {111a4 ^O45^O45$qqq!!!Tz*a4
m;@@DDDE$qqq!!!Tz*a4
m;@@DDDE!!  %t|edl:TCZG$QT*TO O%-3x==QQQ	 HT++y"GB"G"GHHIIK	 Ix9::	]""4((,,!,44 }X&& bfQY]333a7A=="q&IDMIi044Q7771QQQW:QQQW--9: 	    	Xc(mm4@@@NIMN29U6]O3C%DEEF "+>!:MOy/H IIJ ,-11q199C? 889   //'**S]]:Oaaad111aaad111aaad111
 
 ''

 	$ y$--Y&&r   )
	itertoolsrg   BaseExceptionrh   rI   r   strictr   isfiniteall)r*   r+   
iterations_r   rg   s       @@r   subdivide_loopr      sv   ^:))))))) : : :99999999: 
Q' Q' Q' Q' Q' Q'f : 6 6$*Xu55%%
z 4 4{8$$((*****{5!!%%'''''{8E?++//11111 aaaeaaa!e,1133333U?s    )NNF)rA   F)N)__doc__numpyr    r   r   r   	constantsr   geometryr   r@   re   r    r   r   <module>r      s         # # # # # # # # # #       $ $ $ $ $ $ LQc# c# c# c#LX' X' X' X'vX X X X X Xr   