
    l0j*                         d dl Z	 d dlmZmZ d dlmZ n# e$ r Y nw xY w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Zd Zd ZdS )    N)
coo_matrixeye)spsolve   )	triangles)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                 T/home/wildlama/miniconda3/envs/lam/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_humphreyr3   ]   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   r-   )r   r   nur   r!   r   indexr   s           r'   filter_taubinr8      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   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_laplacianrM      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..9FSL   	8!(5/EEEhOC{{$XugsCHH%53;77H DMKr)   c                 "  
 | j         }||D ]}|g||<   	| j                            t          j                  t          j        |          }t          j        d t          |          D                       }|rt          j        d |D                       }nNt          j        d          

fdt          |          D             }t          j        d |D                       }t          |||fft                    gdz            }	|	S )	a  
    Calculate a sparse matrix for laplacian operations.
    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
    Returns
    ----------
    laplacian : scipy.sparse.coo.coo_matrix
      Laplacian operator
    Nc                 :    g | ]\  }}|gt          |          z  S  len).0ins      r'   
<listcomp>z)laplacian_calculation.<locals>.<listcomp>  s(    HHH41a1#A,HHHr)   c                 T    g | ]%}d t          |          z  gt          |          z  &S )r,   rQ   )rS   rU   s     r'   rV   z)laplacian_calculation.<locals>.<listcomp>  s/    JJJ1c!ff~A6JJJr)   r:   c                     g | ]T\  }}d t          j        dt          j        t          j        |         |         z
  dz                                z  US )r,   gư>r5   )r   r@   sqrtr   )rS   rT   rU   r?   r   s      r'   rV   z)laplacian_calculation.<locals>.<listcomp>#  sm     
 
 
 1 jrwrvx{Xa[/HQ.NPT'U'UVVWWX
 
 
r)   c                 <    g | ]}||                                 z  S rP   )sum)rS   rT   s     r'   rV   z)laplacian_calculation.<locals>.<listcomp>)  s$    :::qq15577{:::r)   r5   )r   )
vertex_neighborsr   r   r   r   concatenate	enumerater?   r   rR   )r   equal_weightpinned_vertices	neighborsrT   colrowdatanormsmatrixr?   r   s             @@r'   r   r      s?     %I "  	 	A3IaLL }!!"*--H .
#
#C
.HH9Y3G3GHHH
I
IC <~JJ	JJJKK wqzz
 
 
 
 
 "),,
 
 
 ~::E:::;; Sz*3x==/A2EFFFFMr)   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   rR   r   r   )r   r   r   rh   vert_normalss        r'   r=   r=   1  sO     }HJE $L  Hu5599,GGL<   r)   c                 \    | ||z  z   }t          ||         d          d         }|||z
  z  S )a  
    Get de 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   rF   vrE   	vertices2v2s          r'   rC   rC   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   scipy.sparser   r   scipy.sparse.linalgr   ImportError r   geometryr   r
   utilr   r(   r3   r8   rM   r   r=   rC   rP   r)   r'   <module>ru      s^      	,,,,,,,,+++++++ 	 	 	D	       " " " " " " & & & & & &      
 
#K K K K\+ + + +\) ) ) )Z OSB B B BJ2 2 2 2j! ! !4    s    