
    Ti/                     0   d dl ZddlmZmZ 	 d dlmZmZ d dlm	Z	 n*# e
$ r"ZddlmZ  ee          ZeeecZZ	ZY dZ[ndZ[ww xY w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 dS )     N   )	ArrayLikeOptional)
coo_matrixeye)spsolve)ExceptionWrapper)graph	triangles)Trimesh)index_sparsemass_properties)unitize      ?
   FTc                 p   |t          |           }|r| j        }| j                                                            t
          j                  }| j                                                            t
          j                  }|r2|j        d         }	t          |	          |t          |	          |z
  z  z   }
t          |          D ]d}|s!|                    |          |z
  }|||z  z  }nt          |
|          }|r-t          j        ||         d          d         }|||z  dz  z  }e|| _        | 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
    Nr   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                 K/DATA/AppData/hermes/venv/lib/python3.11/site-packages/trimesh/smoothing.pyfilter_laplacianr-      sS   J !2488  + }!!##((44HJOO""2:..E ! A!'*YYT-?!?@@ 
## ; ;( 	-$((22X=Cs
"HH r8,,H  	;/dSSSG 7*	::H DMK    皙?c                    |t          |           }| j                                                            t          j                  }|                                }t          |          D ]`}|                                }|                    |          }|||z  d|z
  |z  z   z
  }	|||	z  d|z
  |                    |	          z  z   z  }a|| _        | 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
    N      ?r   r   r   r   r   r   r   r    )
r!   alphabetar#   r&   r   originalr*   vert_qvert_bs
             r,   filter_humphreyr8   c   s    4 !2488 }!!##((44H}}H 
## R R%))(33UX-u0FFGD6MS4Z3E3I3I&3Q3Q$QQQ DMKr.   c                 *   |t          |           }| j                                                            t          j                  }t          |          D ]4}|                    |          |z
  }|dz  dk    r	|||z  z  },|||z  z  }5|| _        | 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
    N   r   r2   )r!   r"   nur#   r&   r   indexr    s           r,   filter_taubinr=      s    . !2488 }!!##((44H z"" ! ! $$X..919>>s
"HHS HH DMKr.   c                    |t          |           }|r| j        }| j                                                            t
          j                  }| j                                                            t
          j                  }dt          j        | j	                  dz  z  }t          |          D ])}	t          |           }
|                    |          }||z
  }t          j        |
|z                      t          j        d                              }dt          j        d|          z  }t          j        d|z  t          j        d||z  t          j        |          z                      }|                    |          }||||z
  z  z  }|rDt%          ||         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
    Ng{Gz?r   )   r   r1   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_laplacianrR      s   < !2488   }!!##((44HJOO""2:..E
"&))S0
1C 
## 8 8&t,,##H--2vw++BGFOO<<==RZt,,,C$J
3tbgdmm8S(T(TUU !$$X..FcHn--  	8!(5/EEEhOC{{$XugsCHH%53;77H DMKr.   r!   equal_weightpinned_verticesc                   
 |rt          j        | j                  }|d|j        t	          j        |j        |          <   t	          j        |j        |f          |_        t	          j        |j        |f          |_        t	          j        |j        t	          j	        t          |          t                    f          |_        |                    d|                    d          z            }n| j        }||D ]}|g||<   	| j                            t          j                  t	          j        |          }t	          j        d t%          |          D                       }t	          j	        d          

fd	t%          |          D             }t	          j        d
 |D                       }	t'          |	||fft                    gdz            }|S )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
    NF)dtyper1   r   )axisc                 :    g | ]\  }}|gt          |          z  S  )len).0ins      r,   
<listcomp>z)laplacian_calculation.<locals>.<listcomp>=  s(    LLLtq!qcCFFlLLLr.   r?   c                     g | ]T\  }}d t          j        dt          j        t          j        |         |         z
  dz                                z  US )r1   gư>r:   )r   rE   sqrtr    )r[   r\   r]   rD   r   s      r,   r^   z)laplacian_calculation.<locals>.<listcomp>C  sm     
 
 
 1 jrwrvx{Xa[/HQ.NPT'U'UVVWWX
 
 
r.   c                 <    g | ]}||                                 z  S rY   )sum)r[   r\   s     r,   r^   z)laplacian_calculation.<locals>.<listcomp>I  s$    :::qq15577{:::r.   r:   )r   )r
   edges_to_cooedgesdatar   isinrowconcatenatecolrD   rZ   boolmultiplyrb   vertex_neighborsr   r   r   	enumerater   )r!   rS   rT   	laplacian	neighborsr\   ri   rg   normsre   rD   r   s             @@r,   r   r     s   2  0N&tz22	&FKIN279=/BBCNIM?+KLLIMNIM?+KLLIM^_)=)=T!J!J!JK IN &&sY]]]-B-B'BCC		 )	 &$ # # !s	! =%%bj11 nY''nLLy7K7KLLLMM wqzz
 
 
 
 
 "),,
 
 
 ~::E:::;; sCj1#h--19LMMM	r.   c                     | j         }| j        }| j        }t          t	          |          |                              |          }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   rZ   r    r   )r!   r   r   rr   vert_normalss        r,   rB   rB   Q  sO     }HJE $L  Hu5599,GGL<   r.   c                 \    | ||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   rK   vrJ   	vertices2v2s          r,   rH   rH   k  s=    & 7S=(I	5)	=	=	=h	GBBFr.   )r   r   FTN)r/   r   r   N)r   r   r   N)r   r   TN)TN)!numpyr   typedr   r   scipy.sparser   r   scipy.sparse.linalgr   ImportErrorE
exceptionsr	   wrapper r
   r   baser   geometryr   r   utilr   r-   r8   r=   rR   rj   r   rB   rH   rY   r.   r,   <module>r      s       & & & & & & & &9,,,,,,,,+++++++ 9 9 9,,,,,,q!!G&C******	9               " " " " " " & & & & & &      
 
#K K K K\+ + + +\) ) ) )Z OSC C C CP +/K K
KK i(K K K K\! ! !4    s    A?A