
    l0j                         d Z ddlZddlmZmZ ddlmZmZm	Z	m
Z
mZmZ ddlmZ 	 	 	 dded	ee         fd
Zdede	e         fdZ	 ddedee         de	e         fdZddedee
         fdZdede	e         fdZdS )zG
sample.py
------------

Randomly sample surface and volume of meshes.
    N   )transformationsutil)	ArrayLikeIntegerNDArrayNumberOptionalfloat64)uv_to_interpolated_colorFcountface_weightc                 X   || j         }t          j        |          }|t          j        j        }n$t          j                            |          j        } ||          |d         z  }t          j        ||          }| j        | j        dddf                  }	| j        | j        ddddf                                                  }
|
t          j	        |	d          
                    d          z  }
|	|         }	|
|         }
|rt          | j        d          r| j        j        | j        dddf                  }| j        j        | j        ddddf                                                  }t          j	        |d          
                    d          }||z  }||         }||         } |t          |
          d	df          }|                    d
          
                    d          dk    }||xx         dz  cc<   t          j        |          }|
|z                      d
          }||	z   }|rlt          | j        d          r@||z                      d
          }||z   }| j        j        j        }t'          ||          }n| j        j        |         }|||fS ||fS )a  
    Sample the surface of a mesh, returning the specified
    number of points

    For individual triangle sampling uses this method:
    http://mathworld.wolfram.com/TrianglePointPicking.html

    Parameters
    -----------
    mesh : trimesh.Trimesh
      Geometry to sample the surface of
    count : int
      Number of points to return
    face_weight : None or len(mesh.faces) float
      Weight faces by a factor other than face area.
      If None will be the same as face_weight=mesh.area
    sample_color : bool
      Option to calculate the color of the sampled points.
      Default is False.
    seed : None or int
      If passed as an integer will provide deterministic results
      otherwise pulls the seed from operating system entropy.

    Returns
    ---------
    samples : (count, 3) float
      Points in space on the surface of mesh
    face_index : (count,) int
      Indices of faces for each sampled point
    colors : (count, 4) float
      Colors of each sampled point
      Returns only when the sample_color is True
    Nr   r   )r      )r   r      uv)r   r   r   r   )axisg      ?)
area_facesnpcumsumrandomdefault_rngsearchsortedverticesfacescopytilereshapehasattrvisualr   lensumabsmaterialimager   face_colors)meshr   r   sample_colorseed
weight_cumr   	face_pick
face_indextri_originstri_vectors
uv_origins
uv_vectorsuv_origins_tilerandom_lengthsrandom_testsample_vectorsamplessample_uv_vector
uv_samplestexturecolorss                         Q/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/trimesh/sample.pysample_surfacer<      s   R  o ;''J |!&&t,,3 u
2.IY77J -
111a4 01K-
111abb5 127799K27;//77
CCCK j)Kj)K ,T22 ,[^DJqqq!t$45
[^DJqqq!""u$56;;==
'*f55==jIIo%

+

+
 VS--q!455N !$$!$,,44R883>K;3&VN++N !>166A6>>M k)G 	+4;%% 	9 *^ ;@@a@HH)J6Jk*0G-j'BBFF[,Z8F
F**J    returnc                     t           j                            |df          | j        z  | j        d         z   }|                     |          }||         d|         }|S )aX  
    Use rejection sampling to produce points randomly
    distributed in the volume of a mesh.


    Parameters
    -----------
    mesh : trimesh.Trimesh
      Geometry to sample
    count : int
      Number of points to return

    Returns
    ---------
    samples : (n, 3) float
      Points in the volume of the mesh where n <= count
    r   r   N)r   r   extentsboundscontains)r(   r   points	containedr6   s        r;   volume_meshrE   |   sX    $ iqz**T\9T[^KFf%%IY'GNr=   	transformc                     t           j                            |df          dz
  }|| z  }|t          j        ||          }|S )a  
    Return random samples inside a rectangular volume,
    useful for sampling inside oriented bounding boxes.

    Parameters
    -----------
    extents :   (3,) float
      Side lengths of rectangular solid
    count : int
      Number of points to return
    transform : (4, 4) float
      Homogeneous transformation matrix

    Returns
    ---------
    samples : (count, 3) float
      Points in requested volume
    r   g      ?)r   r   r   transform_points)r@   r   rF   r6   s       r;   volume_rectangularrI      sI    * iqz**S0GwG!27IFFNr=   radiusc                 r   ddl m} |t          j        | j        d|z  z            }t          | |dz  |          \  }} |||          \  }}t          |          |k    r|d|         ||         d|         fS t          j        	                    dt          |           d| d           |||         fS )	a,  
    Sample the surface of a mesh, returning samples which are
    VERY approximately evenly spaced. This is accomplished by
    sampling and then rejecting pairs that are too close together.

    Note that since it is using rejection sampling it may return
    fewer points than requested (i.e. n < count). If this is the
    case a log.warning will be emitted.

    Parameters
    -----------
    mesh : trimesh.Trimesh
      Geometry to sample the surface of
    count : int
      Number of points to return
    radius : None or float
      Removes samples below this radius
    seed : None or int
      Provides deterministic values

    Returns
    ---------
    samples : (n, 3) float
      Points in space on the surface of mesh
    face_index : (n,) int
      Indices of faces for each sampled point
    r   )remove_closeNr   )r*   z	only got /z	 samples!)
rC   rL   r   sqrtarear<   r"   r   logwarning)r(   r   rJ   r*   rL   rC   indexmasks           r;   sample_surface_evenrT      s    8 %$$$$$ ~a%i011 #4>>>MFE  <//LFD 6{{efuf~uT{6E6222 	H?V??u???@@@5;r=   c                     t           j                            d| f          \  }}t           j        dz  |z  }t          j        d|z  dz
            }t	          j        t          j        ||f                    }|S )aU  
    Correctly pick random points on the surface of a unit sphere

    Uses this method:
    http://mathworld.wolfram.com/SpherePointPicking.html

    Parameters
    -----------
    count : int
      Number of points to return

    Returns
    ----------
    points : (count, 3) float
      Random points on the surface of a unit sphere
    r   r   )r   r   piarccosr   spherical_to_vectorcolumn_stack)r   uvthetaphirC   s         r;   sample_surface_spherer^      sj    $ 9QJ''DAqEAIME
)QUaK
 
 C%boucl&C&CDDFMr=   )NFN)N)NN)__doc__numpyr    r   r   typedr   r   r   r	   r
   r   r!   r   r<   rE   rI   rT   intr^    r=   r;   <module>re      s        # # # # # # # # I I I I I I I I I I I I I I I I , , , , , , (,	j jj )$j j j jZW )9    2 ?C (0(;W   8/ /W /hv6F / / / /d )9      r=   