
    l0j6X                         d dl Z d dlZ	 d dlZn# e$ r dZY nw xY w G d d          Z G d d          Z G d d          Zd Zd	 Z	d
 Z
dS )    Nc                   `    e Zd ZdZd Zed             Zed             Zed             Zd Z	dS )ContactDatazK
    Data structure for holding information about a collision contact.
    c                     t          |          | _        |d         |j        |d         |j        i| _        |j        | _        |j        | _        |j	        | _
        dS )z
        Initialize a ContactData.

        Parameters
        ----------
        names : list of str
          The names of the two objects in order.
        contact : fcl.Contact
          The contact in question.
        r      N)setnamesb1b2_indsnormal_normalpos_pointpenetration_depth_depth)selfr   contacts      T/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/trimesh/collision.py__init__zContactData.__init__   sM     ZZ
Ah
E!HgjA
~k/    c                     | j         S )z
        The 3D intersection normal for this contact.

        Returns
        -------
        normal : (3,) float
          The intersection normal.
        )r   r   s    r   r   zContactData.normal"   s     |r   c                     | j         S )z
        The 3D point of intersection for this contact.

        Returns
        -------
        point : (3,) float
          The intersection point.
        )r   r   s    r   pointzContactData.point.        {r   c                     | j         S )z
        The penetration depth of the 3D point of intersection for this contact.

        Returns
        -------
        depth : float
          The penetration depth.
        )r   r   s    r   depthzContactData.depth:   r   r   c                     | j         |         S )a)  
        Returns the index of the face in contact for the mesh with
        the given name.

        Parameters
        ----------
        name : str
          The name of the target object.

        Returns
        -------
        index : int
          The index of the face in collision
        r   r   names     r   indexzContactData.indexF        z$r   N)
__name__
__module____qualname____doc__r   propertyr   r   r   r"    r   r   r   r      s         0 0 0" 	 	 X	 	 	 X	 	 	 X	         r   r   c                   :    e Zd ZdZd Zed             Zd Zd ZdS )DistanceDatazH
    Data structure for holding information about a distance query.
    c                     t          |          | _        |d         |j        |d         |j        i| _        |d         |j        d         |d         |j        d         i| _        |j        | _        dS )z
        Initialize a DistanceData.

        Parameters
        ----------
        names : list of str
          The names of the two objects in order.
        contact : fcl.DistanceResult
          The distance query result.
        r   r   N)	r   r   r	   r
   r   nearest_points_pointsmin_distance	_distance)r   r   results      r   r   zDistanceData.__init__]   sh     ZZ
Ah	58VY?
!Hf+A.!Hf+A.
  ,r   c                     | j         S )z
        Returns the distance between the two objects.

        Returns
        -------
        distance : float
          The euclidean distance between the objects.
        )r0   r   s    r   distancezDistanceData.distancep   s     ~r   c                     | j         |         S )a'  
        Returns the index of the closest face for the mesh with
        the given name.

        Parameters
        ----------
        name : str
          The name of the target object.

        Returns
        -------
        index : int
          The index of the face in collisoin.
        r   r    s     r   r"   zDistanceData.index|   r#   r   c                     | j         |         S )a  
        The 3D point of closest distance on the mesh with the given name.

        Parameters
        ----------
        name : str
          The name of the target object.

        Returns
        -------
        point : (3,) float
          The closest point.
        )r.   r    s     r   r   zDistanceData.point   s     |D!!r   N)	r$   r%   r&   r'   r   r(   r3   r"   r   r)   r   r   r+   r+   X   sf         - - -& 	 	 X	     "" " " " "r   r+   c                   l    e Zd ZdZd ZddZd Zd Z	 ddZdd	Z	dd
Z
	 ddZddZddZd Zd ZdS )CollisionManagerz(
    A mesh-mesh collision manager.
    c                     t           t          d          i | _        t          j        d           | _        t          j                    | _        | j                                         dS )z;
        Initialize a mesh-mesh collision manager.
        Nz7No FCL Available! Please install the python-fcl libraryc                      d S Nr)   r)   r   r   <lambda>z+CollisionManager.__init__.<locals>.<lambda>   s    d r   )	fcl
ValueError_objscollectionsdefaultdict_namesDynamicAABBTreeCollisionManager_managersetupr   s    r   r   zCollisionManager.__init__   s_     ;VWWW
 "-ll;;;==r   Nc                 Z   |t          j        d          }t          j        |t           j                  }|j        dk    rt          d          |                     |          }t          j        |ddddf         |dddf                   }t          j	        ||          }|| j
        v r%| j                            | j
        |                    ||d| j
        |<   || j        t          |          <   | j                            |           | j                                         |S )a  
        Add an object to the collision manager.

        If an object with the given name is already in the manager,
        replace it.

        Parameters
        ----------
        name : str
          An identifier for the object
        mesh : Trimesh object
          The geometry of the collision object
        transform : (4,4) float
          Homogeneous transform matrix for the object
        N   dtype)rF   rF   ztransform must be (4,4)!   )objgeom)npeye
asanyarrayfloat32shaper=   _get_fcl_objr<   	TransformCollisionObjectr>   rC   unregisterObjectrA   idregisterObjectupdate)r   r!   mesh	transformrK   tos          r   
add_objectzCollisionManager.add_object   s"   $ q		IM)2:>>>	?f$$7888   && M)BQBF+Yrr1u-=>>a(( 4:M**4:d+;<<<#$d33
4 $BtHH$$Q'''r   c                 v   || j         v r| j                            | j         |         d                    | j                            | j         |         d                    t	          | j                             |          d                   }| j                            |           dS t          | d          )z
        Delete an object from the collision manager.

        Parameters
        ----------
        name : str
          The identifier for the object
        rJ   rK    not in collision manager!N)r>   rC   rT   rW   rU   poprA   r=   )r   r!   geom_ids      r   remove_objectzCollisionManager.remove_object   s     4:M**4:d+;E+BCCCM  D!1%!8999--f566GKOOG$$$$$@@@AAAr   c                    || j         v ro| j         |         d         }|                    |ddddf                    |                    |dddf                    | j                            |           dS t          | d          )aD  
        Set the transform for one of the manager's objects.
        This replaces the prior transform.

        Parameters
        ----------
        name : str
          An identifier for the object already in the manager
        transform : (4,4) float
          A new homogeneous transform matrix for the object
        rJ   NrI   r^   )r>   setRotationsetTranslationrC   rW   r=   )r   r!   rY   r[   s       r   set_transformzCollisionManager.set_transform   s     4:
4 'AMM)BQBF+,,,Yrr1u-...M  #####@@@AAAr   Fc                 P   |t          j        d          }|                     |          }t          j        |ddddf         |dddf                   }t          j        ||          }t          j                    }|s|r)t          j        t          j        dd                    }| j        	                    ||t          j
                   |j        j        }	t                      }
g }|s|r|j        j        D ]}|j        }||k    r|j        }|                     |          }|df}||j        k    rt%          t'          |                    }|r|
                    |           |r#|                    t-          ||                     |r|r|	|
|fS |r|	|
fS |r|	|fS |	S )	aI  
        Check a single object for collisions against all objects in the
        manager.

        Parameters
        ----------
        mesh : Trimesh object
          The geometry of the collision object
        transform : (4,4) float
          Homogeneous transform matrix
        return_names : bool
          If true, a set is returned containing the names
          of all objects in collision with the object
        return_data :  bool
          If true, a list of ContactData is returned as well

        Returns
        ------------
        is_collision : bool
          True if a collision occurs and False otherwise
        names : set of str
          [OPTIONAL] The set of names of objects that collided with the
          provided one
        contacts : list of ContactData
          [OPTIONAL] All contacts detected
        NrF   rI   順 Tnum_max_contactsenable_contactrequest
__external)rL   rM   rQ   r<   rR   rS   CollisionDataCollisionRequestrC   collidedefaultCollisionCallbackr1   is_collisionr   contactso1o2_extract_nametuplereversedaddappendr   )r   rX   rY   return_namesreturn_datarK   rZ   r[   cdatar1   objs_in_collisioncontact_datar   cgr!   r   s                   r   in_collision_singlez$CollisionManager.in_collision_single  s   : q		I   && M)BQBF+Yrr1u-=>>a(( !## 	; 	%,fUYZZZ  E 	a(DEEE*  EE 	E; 	E <0 E EZ:: B))"--|,##!(5//22E 0%))$/// E ''E7(C(CDDD 	K 	,l:: 	,,, 	<''Mr   c                 l   t          j                    }|s|r)t          j        t          j        dd                    }| j                            |t           j                   |j        j        }t                      }g }|s|r|j        j	        D ]}| 
                    |j                  | 
                    |j                  f}|r/|                    t          t          |                               |r#|                    t#          ||                     |r|r|||fS |r||fS |r||fS |S )a  
        Check if any pair of objects in the manager collide with one another.

        Parameters
        ----------
        return_names : bool
          If true, a set is returned containing the names
          of all pairs of objects in collision.
        return_data :  bool
          If true, a list of ContactData is returned as well

        Returns
        -------
        is_collision : bool
          True if a collision occurred between any pair of objects
          and False otherwise
        names : set of 2-tup
          The set of pairwise collisions. Each tuple
          contains two names in alphabetical order indicating
          that the two corresponding objects are in collision.
        contacts : list of ContactData
          All contacts detected
        rg   Trh   rk   )r<   rn   ro   rC   rp   rq   r1   rr   r   rs   rv   rt   ru   ry   rw   sortedrz   r   )	r   r{   r|   r}   r1   r~   r   r   r   s	            r   in_collision_internalz&CollisionManager.in_collision_internalP  so   0 !## 	; 	%,fUYZZZ  E 	eS%ABBB*EE 	E; 	E <0 E E++GJ779K9KGJ9W9WX @%))%u*>*>??? E ''E7(C(CDDD 	K 	,l:: 	,,, 	<''Mr   c                     t          j                    }|s|r)t          j        t          j        dd                    }| j                            |j        |t           j                   |j        j        }t                      }g }|s|r|j        j	        D ]}d}	| 
                    |j                  |
                    |j                  f}
|
d         6| 
                    |j                  |
                    |j                  f}
d}	|r|                    |
           |rA|	rt          t          |
                    }
|                    t#          |
|                     |r|r|||fS |r||fS |r||fS |S )a  
        Check if any object from this manager collides with any object
        from another manager.

        Parameters
        -------------------
        other_manager : CollisionManager
          Another collision manager object
        return_names : bool
          If true, a set is returned containing the names
          of all pairs of objects in collision.
        return_data : bool
          If true, a list of ContactData is returned as well

        Returns
        -------------
        is_collision : bool
          True if a collision occurred between any pair of objects
          and False otherwise
        names : set of 2-tup
          The set of pairwise collisions. Each tuple
          contains two names (first from this manager,
          second from the other_manager) indicating
          that the two corresponding objects are in collision.
        contacts : list of ContactData
          All contacts detected
        rg   Trh   rk   Fr   )r<   rn   ro   rC   rp   rq   r1   rr   r   rs   rv   rt   ru   ry   rw   rx   rz   r   )r   other_managerr{   r|   r}   r1   r~   r   r   reverser   s              r   in_collision_otherz#CollisionManager.in_collision_other  s   8 !## 	; 	%,fUYZZZ  E 	m4eS=YZZZ*EE 	E; 	E <0 E E&&wz22!//
;; 8#**7:66%33GJ??E #G 1%))%000 E 7 %huoo 6 6 ''E7(C(CDDD 	K 	,l:: 	,,, 	<''Mr   c                 2   |t          j        d          }|                     |          }t          j        |ddddf         |dddf                   }t          j        ||          }t          j        t          j        d                    }|r:t          j        t          j        dd          t          j                              }| j	        
                    ||t          j                   |j        j        }	d\  }
}|s|rx|j        j        }||k    r|j        j        }|                     |          }
|
df}||j        j        k    rt#          t%          |                    }t          ||j                  }|r|r|	|
|fS |r|	|
fS |r|	|fS |	S )	a  
        Get the minimum distance between a single object and any
        object in the manager.

        Parameters
        ---------------
        mesh : Trimesh object
          The geometry of the collision object
        transform : (4,4) float
          Homogeneous transform matrix for the object
        return_names : bool
          If true, return name of the closest object
        return_data : bool
          If true, a DistanceData object is returned as well

        Returns
        -------------
        distance : float
          Min distance between mesh and any object in the manager
        name : str
          The name of the object in the manager that was closest
        data : DistanceData
          Extra data about the distance query
        NrF   rI   Tenable_signed_distanceenable_nearest_pointsr   NNrm   )rL   rM   rQ   r<   rR   rS   r+   DistanceRequestDistanceResultrC   r3   defaultDistanceCallbackr1   r/   rt   ru   rv   rw   rx   )r   rX   rY   return_namer|   rK   rZ   r[   ddatar3   r!   datar   r   s                 r   min_distance_singlez$CollisionManager.min_distance_single  s   6 q		I   && M)BQBF+Yrr1u-=>>a((  !4D!Q!Q!QRR 	$#*.t   "$$	 E 	q%)DEEE<,  
d 
	5+ 
	5BTzz\_%%b))D<(EU\_$$huoo..u|44D 	; 	T4'' 	T>! 	T>!Or   c                 H   t          j        t          j        d                    }|r:t          j        t          j        dd          t          j                              }| j                            |t           j                   |j        j        }d\  }}|s|ro| 	                    |j        j
                  | 	                    |j        j                  f}t          ||j                  }t          t          |                    }|r|r|||fS |r||fS |r||fS |S )aW  
        Get the minimum distance between any pair of objects in the manager.

        Parameters
        -------------
        return_names : bool
          If true, a 2-tuple is returned containing the names
          of the closest objects.
        return_data : bool
          If true, a DistanceData object is returned as well

        Returns
        -----------
        distance : float
          Min distance between any two managed objects
        names : (2,) str
          The names of the closest objects
        data : DistanceData
          Extra data about the distance query
        Tr   r   r   )r<   r+   r   r   rC   r3   r   r1   r/   rv   rt   ru   rw   r   )r   r{   r|   r   r3   r   r   s          r   min_distance_internalz&CollisionManager.min_distance_internal  s@   *  !4D!Q!Q!QRR 	$#*.+/   "$$ E 	uc&ABBB<, t 	); 	)""5<?33""5<?33E  u|44D&--((E 	K 	UD(( 	U?" 	T>!Or   c                 
   t          j        t          j        d                    }|r:t          j        t          j        dd          t          j                              }| j                            |j        |t           j                   |j        j        }d\  }}|s|rd}| 	                    |j        j
                  |	                    |j        j                  f}|d         @d}| 	                    |j        j                  |	                    |j        j
                  f}t          |          }	|rt          t          |	                    }	t          |	|j                  }|r|r|||fS |r||fS |r||fS |S )aI  
        Get the minimum distance between any pair of objects,
        one in each manager.

        Parameters
        ----------
        other_manager : CollisionManager
          Another collision manager object
        return_names : bool
          If true, a 2-tuple is returned containing
          the names of the closest objects.
        return_data : bool
          If true, a DistanceData object is returned as well

        Returns
        -----------
        distance : float
          The min distance between a pair of objects,
          one from each manager.
        names : 2-tup of str
          A 2-tuple containing two names (first from this manager,
          second from the other_manager) indicating
          the two closest objects.
        data : DistanceData
          Extra data about the distance query
        Tr   r   r   Fr   )r<   r+   r   r   rC   r3   r   r1   r/   rv   rt   ru   rw   rx   )
r   r   r{   r|   r   r3   r   r   r   dnamess
             r   min_distance_otherz#CollisionManager.min_distance_otherI  s   6  !4D!Q!Q!QRR 	$#*.+/   "$$ E 	}5uc>YZZZ<, t 	6; 	6G""5<?33++ELO<<E Qx&&u|77!//@@
 5\\F 1x//0055D 	K 	UD(( 	U?" 	T>!Or   c                 R    |j         rt          |          }nt          |          }|S )a  
        Get a BVH or Convex for a mesh.

        Parameters
        -------------
        mesh : Trimesh
          Mesh to create BVH/Convex for

        Returns
        --------------
        obj : fcl.BVHModel or fcl.Convex
          BVH/Convex object of source mesh
        )	is_convexmesh_to_convexmesh_to_BVH)r   rX   rJ   s      r   rQ   zCollisionManager._get_fcl_obj  s/     > 	$ &&CCd##C
r   c                 6    | j         t          |                   S )aG  
        Retrieve the name of an object from the manager by its
        CollisionObject, or return None if not found.

        Parameters
        -----------
        geom : CollisionObject or BVHModel
          Input model

        Returns
        ------------
        names : hashable
          Name of input geometry
        )rA   rU   )r   rK   s     r   rv   zCollisionManager._extract_name  s     {2d88$$r   r:   )NFF)FF)r$   r%   r&   r'   r   r\   ra   re   r   r   r   r   r   r   rQ   rv   r)   r   r   r7   r7      s           ( ( ( (TB B B&B B B* EJK K K KZ4 4 4 4lB B B BJ DIH H H HT3 3 3 3jC C C CJ  *% % % % %r   r7   c                    t          j                    }|                    t          | j                  t          | j                             |                    | j        | j                   |                                 |S )z
    Create a BVHModel object from a Trimesh object

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

    Returns
    ------------
    bvh : fcl.BVHModel
      BVH of input geometry
    )	num_tris_num_vertices_)verts	triangles)r<   BVHModel
beginModellenfacesverticesaddSubModelendModel)rX   bvhs     r   r   r     sc     ,..CNNS__C<N<NNOOOOO$-4:O>>>LLNNNJr   c                    t          j        dt          j        t          | j                  dft           j                  z  | j        fd          }t          j        | j        t          |          |	                                          S )z
    Create a Convex object from a Trimesh object

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

    Returns
    ------------
    convex : fcl.Convex
      Convex of input geometry
    rI   r   rG   )axis)
rL   concatenateonesr   r   int64r<   Convexr   flatten)rX   fss     r   r   r     so     
	
RWc$*ooq):::	:DJGa
 
 
B :dmSWWbjjll;;;r   c                     t                      }i }| j        j        D ]8}| j        |         \  }}|                    || j        |         |          ||<   9||fS )ad  
    Create collision objects from a trimesh.Scene object.

    Parameters
    ------------
    scene : trimesh.Scene
      Scene to create collision objects for

    Returns
    ------------
    manager : CollisionManager
      CollisionManager for objects in scene
    objects: {node name: CollisionObject}
      Collision objects for nodes in scene
    )r!   rX   rY   )r7   graphnodes_geometryr\   geometry)scenemanagerobjectsnodeTr   s         r   scene_to_collisionr     ss        GG* 
 
k$'8**EN84 + 
 
 Gr   )r?   numpyrL   r<   BaseExceptionr   r+   r7   r   r   r   r)   r   r   <module>r      s          JJJJ   
CCCI  I  I  I  I  I  I  I XC" C" C" C" C" C" C" C"LT% T% T% T% T% T% T% T%n  *< < <(    s    