
    l0j,              	       t   d Z ddlmZmZ ddlZddlmZmZ ddl	m
Z
 ddl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Y dZ[ndZ[ww xY w	 ddlmZ n# e$ r eZY nw xY we G d d                      Z eddd          Zedfdeeef         deeedf         deddfdZd Zd ZddZdS )aP  
convex.py

Deal with creating and checking convex objects in 2, 3 and N dimensions.

Convex is defined as:
1) "Convex, meaning "curving out" or "extending outward" (compare to concave)
2) having an outline or surface curved like the exterior of a circle or sphere.
3) (of a polygon) having only interior angles measuring less than 180
    )	dataclassfieldsN   )	trianglesutil)tol)
Geometry3D)NDArrayUnion)
ConvexHull)ExceptionWrapper)
QhullErrorc                       e Zd ZU dZdZeed<   	 dZeed<   	 dZeed<   	 dZ	eed<   	 dZ
eed<   	 dZeed<   	 dZeed	<   	 dZeed
<   	 dZeed<   	 dZeed<   	 dZeed<   	 dZeed<   	 dZeed<   	 dZeed<   	 dZeed<   	 dZeed<   	 defdZdS )QhullOptionsa  
    A helper class for constructing correct Qhull option strings.
    More details available at: http://www.qhull.org/html/qh-quick.htm#options

    Currently only includes the boolean flag options, which is most of them.

    Parameters
    -----------
    Qa
      Allow input with fewer or more points than coordinates
    Qc
      Keep coplanar points with nearest facet
    Qi
      Keep interior points with nearest facet.
    QJ
      Joggled input to avoid precision problems
    Qt
      Triangulated output.
    Qu
      Compute upper hull for furthest-site Delaunay triangulation
    Qw
      Allow warnings about Qhull options
    Qbb
      Scale last coordinate to [0,m] for Delaunay
    Qs
      Search all points for the initial simplex
    Qv
      Test vertex neighbors for convexity
    Qx
      Exact pre-merges (allows coplanar facets)
    Qz
      Add a point-at-infinity for Delaunay triangulations
    QbB
      Scale input to fit the unit cube
    QR0
      Random rotation (n=seed, n=0 time, n=-1 time/no rotate)
    Qg
      only build good facets (needs 'QGn', 'QVn', or 'Pdk')
    Pp
      Do not print statistics about precision problems and remove
      some of the warnings including the narrow hull warning.
    FQaQcQiQJQtQuQwQbbQsQvQxQzQbBQR0QgPpreturnc                 `     d                      fdt                     D                       S )z
        Construct the `qhull_options` string used by `scipy.spatial`
        objects and functions.

        Returns
        ----------
        qhull_options
          Can be passed to `scipy.spatial.[ConvexHull,Delaunay,Voronoi]`
         c              3   P   K   | ] }t          |j                  |j        V  !d S )N)getattrname).0fselfs     Q/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/trimesh/convex.py	<genexpr>z'QhullOptions.__str__.<locals>.<genexpr>   s6      NN1af8M8MNNNNNNN    )joinr   )r)   s   `r*   __str__zQhullOptions.__str__   s2     xxNNNNtNNNNNNr,   N)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    strr.    r,   r*   r   r   "   s|        ) )V BAB2B4B5B BGB. C7B5B/B6B?C,CC BABB&
O 
O 
O 
O 
O 
O 
Or,   r   T)r   r    r   objqhull_optionsrepairr!   ztrimesh.Trimeshc                 "   ddl m} |d}nYt          |t                    rt	          |          }n4t          |t                    r|}nt          t          |                    t          | d          r%| j        	                    t          j                  }nDt          j        | t          j                  }t          j        |d          st!          d          	 t#          ||          }nB# t$          $ r5 t          j                            d	d
           t#          |d          }Y nw xY wt          j        |j                  }t          j        t/          |j                  t          j                  }t          j        t/          |                    ||<   ||j                                                 }	|j        |                                         }
|s ||
|	d
d          S t;          j        |
|	                   }t          j        |d
          \  }}|	|         }	||         }t;          j         |          }|
|	         !                    d          }t          j"        ||d          }||z
  }t          j#        ||          dk     }t          j$        |	|                   |	|<   ||xx         dz  cc<   ||||d} ||
|	||d
d          }|%                    d           ||j&        stO          |d          S |S )aM  
    Get a new Trimesh object representing the convex hull of the
    current mesh attempting to return a watertight mesh with correct
    normals.

    Arguments
    --------
    obj
      Mesh or `(n, 3)` points.
    qhull_options
      Options to pass to qhull.

    Returns
    --------
    convex
      Mesh of convex hull.
    r   )TrimeshNverticesdtype)   z'Object must be Trimesh or (n,3) points!r8   z1Failed to compute convex hull: retrying with `QJ`T)exc_infor   F)r<   facesprocessvalidate)check_valid)crossesaxisr   )weightsrI   g        g      )triangles_crosstriangles_center
area_facescentroid)r<   rC   face_normalsinitial_cacherD   rE   )	multibody)(baser;   
isinstancer   r5   	TypeErrortypehasattrr<   viewnpndarrayasarrayfloat64r   is_shape
ValueErrorr   r   logdebugsortzeroslenpointsint64arange	simplicescopyr   crossunitizeareameanaveragediagonal_dotfliplrfix_normalsis_winding_consistentconvex_hull)r7   r8   r9   r;   	qhull_strrc   hullvidmaskrC   r<   rG   normalsvalidtriangles_arearL   rN   test_vector	backwardsrP   convexs                        r*   rq   rq      sG   .  			M<	0	0 -&&			M3	'	' -!		]++,,,sJ H""2:.. Crz222}VW-- 	HFGGG6&	::: 6 6 6JUYZZZ&5556 '$-
 
 C8C$$BH555D	#c((##DI %%''E{3$$&&H UwteTTTT ohuo..G \'t<<<NGU %LEenG ^G444N+++33 z*NKKKH"X-K!';77#=I yy!122E)I$ #,$	 M W#  F '''
 V%A66666Ms   C1 1<D0/D0c                     | j         | j        dddf                  }| j        | j        dddf                  }| j        dddf         }| j        |         |z
  }t          j        ||          }|S )aF  
    Test if a mesh is convex by projecting the vertices of
    a triangle onto the normal of its adjacent face.

    Parameters
    ----------
    mesh : Trimesh
      Input geometry

    Returns
    ----------
    projection : (len(mesh.face_adjacency),) float
      Distance of projection of adjacent vertex onto plane
    Nr   r   )rO   face_adjacencyr<   face_adjacency_edgesface_adjacency_unsharedr   rm   )meshrv   origins	vid_othervector_otherdotss         r*   adjacency_projectionsr   #  s~       3AAAqD 9:GmD5aaad;<G ,QQQT2I=+g5L \733DKr,   c                 N   | j         r| j        dk    rdS | j        t          j        k    }|| j                                     d          }|                                sdS t          j        | j	        z  }t          | j        |                                         |k               }|S )z
    Check if a mesh is convex.

    Parameters
    -----------
    mesh : Trimesh
      Input geometry

    Returns
    -----------
    convex : bool
      Was passed mesh convex or not
    r   FrH   )is_watertight
body_countrM   r   zeror}   allanyplanarscaler3   face_adjacency_projectionsmax)r   nonzeroadj_ok	thresholdr{   s        r*   	is_convexr   A  s       A!5!5u o(GT()--1-55F ::<< u 
TZ'I
 $1&9==??)KLLFMr,   QbB Ppc                    t          | d          r| j        j        S t          j        | t          j                  }t          |j                  dk    rt          d          t          ||          }|j
        |j                 }|S )aK  
    Try to extract a convex set of points from multiple input formats.

    Details on qhull options:
      http://www.qhull.org/html/qh-quick.htm#options

    Parameters
    ---------
    obj: Trimesh object
         (n,d) points
         (m,) Trimesh objects

    Returns
    --------
    points: (o,d) convex set of points
    rq   r=      zpoints must be (n, dimension)!rA   )rV   rq   r<   rX   
asanyarrayr[   rb   shaper]   r   rc   )r7   r8   initialrs   rc   s        r*   hull_pointsr   k  s    " sM"" (''mCrz222G
7=Q9:::g];;;D['FMr,   )r   ) r2   dataclassesr   r   numpyrX    r   r   	constantsr   parentr	   typedr
   r   scipy.spatialr   ImportErrorE
exceptionsr   r   BaseExceptionr   QHULL_DEFAULTr5   r3   rq   r   r   r   r6   r,   r*   <module>r      s,  	 	 * ) ) ) ) ) ) )                         ! ! ! ! ! ! ! !%((((((( % % %,,,,,,!!!$$JJJJJJ%
(((((((   JJJ yO yO yO yO yO yO yO yOx $4888
 5B~ ~	z7"	#~sD01~ ~ 	~ ~ ~ ~B  <' ' 'T     s&   3 AAAA A('A(