
    j/                         d dl ZddlmZm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ZddZddZ	 ddZ	 	 ddededee   fdZd Zd Z y# e
$ rZddlmZ  ee      ZeeecZZ	ZY dZ[ldZ[ww xY w)    N   )	ArrayLikeOptional)
coo_matrixeye)spsolve)ExceptionWrapper)graph	triangles)Trimesh)index_sparsemass_properties)unitizec                 N   |t        |       }|r| j                  }| j                  j                         j	                  t
        j                        }| j                  j                         j	                  t
        j                        }|r,|j                  d   }	t        |	      |t        |	      |z
  z  z   }
t        |      D ]X  }|s|j                  |      |z
  }|||z  z  }nt        
|      }|s1t        j                  ||   d      d   }||z  dz  z  }Z || _        | S )a  
    Smooth a mesh in-place using laplacian smoothing.
    Articles
    1 - "Improved Laplacian Smoothing of Noisy Surface Meshes"
       J. Vollmer, R. Mencl, and H. Muller
    2 - "Implicit Fairing of Irregular Meshes using Diffusion
       and Curvature Flow". M. Desbrun,  M. Meyer,
       P. Schroder, A.H.B. Caltech
    Parameters
    ------------
    mesh : trimesh.Trimesh
    Mesh to be smoothed in place
    lamb : float
    Diffusion speed constant
    If   0.0, no diffusion
    If > 0.0, diffusion occurs
    implicit_time_integration: boolean
    if False: explicit time integration
        -lamb <= 1.0 - Stability Limit (Article 1)
    if True: implicit time integration
        -lamb no limit (Article 2)
    iterations : int
    Number of passes to run filter
    laplacian_operator : None or scipy.sparse.coo.coo_matrix
    Sparse matrix laplacian operator
    Will be autogenerated if None
    r   Tskip_inertiavolumegUUUUUU?)laplacian_calculationr   verticescopyviewnpndarrayfacesshaper   rangedotr   r   r   )meshlamb
iterationsimplicit_time_integrationvolume_constraintlaplacian_operatorvol_inir   r   dlapAA_indexr   vol_news                 >/DATA/.local/lib/python3.12/site-packages/trimesh/smoothing.pyfilter_laplacianr+      s$   J !248 ++ }}!!#((4HJJOO""2::.E !!''*YT-?!?@@ 
#($((2X=Cs
"H r8,H //dSG 7*	::H# $( DMK    c                    |t        |       }| j                  j                         j                  t        j
                        }|j                         }t        |      D ]T  }|j                         }|j                  |      }|||z  d|z
  |z  z   z
  }	|||	z  d|z
  |j                  |	      z  z   z  }V || _        | S )a  
    Smooth a mesh in-place using laplacian smoothing
    and Humphrey filtering.
    Articles
    "Improved Laplacian Smoothing of Noisy Surface Meshes"
    J. Vollmer, R. Mencl, and H. Muller
    Parameters
    ------------
    mesh : trimesh.Trimesh
      Mesh to be smoothed in place
    alpha : float
      Controls shrinkage, range is 0.0 - 1.0
      If 0.0, not considered
      If 1.0, no smoothing
    beta : float
      Controls how aggressive smoothing is
      If 0.0, no smoothing
      If 1.0, full aggressiveness
    iterations : int
      Number of passes to run filter
    laplacian_operator : None or scipy.sparse.coo.coo_matrix
      Sparse matrix laplacian operator
      Will be autogenerated if None
          ?r   r   r   r   r   r   r   r   )
r   alphabetar!   r$   r   originalr(   vert_qvert_bs
             r*   filter_humphreyr5   c   s    4 !248 }}!!#((4H}}H 
#%))(3UX-u0FFGD6MS4Z3E3I3I&3Q$QQQ	 $ DMKr,   c                    |t        |       }| j                  j                         j                  t        j
                        }t        |      D ]/  }|j                  |      |z
  }|dz  dk(  r	|||z  z  }(|||z  z  }1 || _        | S )a  
    Smooth a mesh in-place using laplacian smoothing
    and taubin filtering.
    Articles
    "Improved Laplacian Smoothing of Noisy Surface Meshes"
    J. Vollmer, R. Mencl, and H. Muller
    Parameters
    ------------
    mesh : trimesh.Trimesh
      Mesh to be smoothed in place.
    lamb : float
      Controls shrinkage, range is 0.0 - 1.0
    nu : float
      Controls dilation, range is 0.0 - 1.0
      Nu shall be between 0.0 < 1.0/lambda - 1.0/nu < 0.1
    iterations : int
      Number of passes to run the filter
    laplacian_operator : None or scipy.sparse.coo.coo_matrix
      Sparse matrix laplacian operator
      Will be autogenerated if None
       r   r/   )r   r    nur!   r$   r   indexr   s           r*   filter_taubinr:      s    . !248 }}!!#((4H z" $$X.919>s
"HS H # DMKr,   c                    |t        |       }|r| j                  }| j                  j                         j	                  t
        j                        }| j                  j                         j	                  t
        j                        }dt        j                  | j                        dz  z  }t        |      D ]  }	t        |       }
|j                  |      }||z
  }t        j                  |
|z  j                  t        j                  d                  }dt        j                  d|      z  }t        j                  d|z  t        j                   d||z  t        j"                  |      z              }|j                  |      }||||z
  z  z  }|st%        ||   d      d	   }|	d
k(  rt'        |||
||      }||
z  |z
  z  z  } || _        | S )a  
    Smooth a mesh in-place using laplacian smoothing using a
    mutable diffusion laplacian.

    Articles
      Barroqueiro, B., Andrade-Campos, A., Dias-de-Oliveira,
      J., and Valente, R. (January 21, 2021).
      "Bridging between topology optimization and additive
       manufacturing via Laplacian smoothing." ASME. J. Mech. Des.


    Parameters
    ------------
    mesh : trimesh.Trimesh
      Mesh to be smoothed in place
    lamb : float
      Diffusion speed constant
      If   0.0, no diffusion
      If > 0.0, diffusion occurs
    iterations : int
      Number of passes to run filter
    laplacian_operator : None or scipy.sparse.coo.coo_matrix
      Sparse matrix laplacian operator
      Will be autogenerated if None
    g{Gz?      ?)   r   r.   g-q=g?Tr   r   r   )r   r   r   r   r   r   r   r   max
area_facesr   get_vertices_normalsr   absonesmaximumminimummeanr   dilate_slope)r   r    r!   r#   r$   v_inir   r   epsr(   normalsqipi_qiadillamberr   volslopes                     r*   filter_mut_dif_laplacianrP      s   < !248  }}!!#((4HJJOO""2::.E
"&&)S0
1C 
#&t,##H-2vvw++BGGFO<=RZZt,,C$J

3tbggdm8S(TU !$$X.FcHn-- !(5/EhOC{$XugsCH%53;77H' $, DMKr,   r   equal_weightpinned_verticesc                    |rt        j                  | j                        }|d|j                  t	        j
                  |j                  |      <   t	        j                  |j                  |f      |_        t	        j                  |j                  |f      |_        t	        j                  |j                  t	        j                  t        |      t              f      |_        |j                  d|j                  d      z        }|S | j                  }||D ]  }|g||<   
 | j                  j!                  t        j"                        }t	        j                  |      }t	        j                  t%        |      D cg c]  \  }}|gt        |      z   c}}      }	t	        j                  d      }
t%        |      D cg c]Q  \  }}dt	        j&                  dt	        j(                  t	        j*                  ||   ||   z
  dz  |
                  z  S }}}t	        j                  |D cg c]  }||j                         z   c}      }t-        ||	|fft        |      gdz  	      }|S c c}}w c c}}w c c}w )
al  
    Calculate a sparse matrix for laplacian operations.

    Note that setting equal_weight to False significantly hampers performance.

    Parameters
    -------------
    mesh : trimesh.Trimesh
      Input geometry
    equal_weight : bool
      If True, all neighbors will be considered equally
      If False, all neighbors will be weighted by inverse distance
    pinned_vertices : None or list of ints
      If None, no vertices are pinned
      If list, vertices will be pinned, such that they will not be moved
    Returns
    ----------
    laplacian : scipy.sparse.coo.coo_matrix
      Laplacian operator
    F)dtyper.   r   )axisr=   gư>r7   )r   )r
   edges_to_cooedgesdatar   isinrowconcatenatecolrB   lenboolmultiplysumvertex_neighborsr   r   r   	enumeraterC   sqrtr   r   )r   rQ   rR   	laplacian	neighborsir   r\   nrZ   rB   normsrX   s                r*   r   r     s%   2 &&tzz2	&FKINN2779==/BCNNIMM?+KLIMNNIMM?+KLIM^^_)=T!JKIN &&sY]]]-B'BC	D = ))	 &$ !s	! % ==%%bjj1 nnY'nny7KL7Ktq!qcCFl7KLM wwqz
 "),
 -1 jjrwwrvvx{Xa[/HQ.NPT'UVWX, 	 
 ~~E:Eqq1557{E:; sCj1#h-19LM	# M
 ;s   I3
AI92I?c                     | j                   }| j                  }| j                  }t        t	        |      |      j                  |      }t        |      S )z
    Compute Vertex normals using equal weighting of neighbors faces.
    Parameters
      -------------
      mesh : trimesh.Trimesh
        Input geometry
      Returns
      ----------
      vertices_normals: array
        Vertices normals
    )r   r   face_normalsr   r]   r   r   )r   r   r   rj   vert_normalss        r*   r@   r@   Q  sK     }}HJJE $$L  Hu599,GL<  r,   c                 H    | ||z  z   }t        ||   d      d   }|||z
  z  S )a  
    Get the derivate of dilation scalar by the volume variation by finite differences
    Thus, Vertices += vertex_normals*dilate_slope*(Initial_Volume - Srinked_Volume)
    Parameters
      -------------
      mesh : trimesh.Trimesh
        Input geometry
      vertices: mesh.vertices
      faces: mesh.faces
      normals: array
        vertices normals
      Returns
      ----------
      dilate_slope: float
        derivative
    Tr   r   r   )r   r   rI   vrH   	vertices2v2s          r*   rF   rF   k  s7    & 7S=(I	5)	=h	GBBFr,   )r<   
   FTN)g?r<   rp   N)r<   r<   rp   N)r<   rp   TN)TN)!numpyr   typedr   r   scipy.sparser   r   scipy.sparse.linalgr   ImportErrorE
exceptionsr	   wrapper r
   r   baser   geometryr   r   utilr   r+   r5   r:   rP   r^   r   r@   rF    r,   r*   <module>r~      s     &9,+   " & 
 
#K\+\)Z OSCP +/K
KK i(K\!4G  9,q!G&C*	9s   A) )B.BB