
    j\                         d dl Z d dlZ	 d dlZ G d d      Z G d d      Z G d d      Zd Zd	 Z	d
 Z
y# e$ r dZY 1w xY w)    Nc                   L    e Zd ZdZd Zed        Zed        Zed        Zd Z	y)ContactDatazK
    Data structure for holding information about a collision contact.
    c                     t        |      | _        |d   |j                  |d   |j                  i| _        |j
                  | _        |j                  | _        |j                  | _
        y)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      >/DATA/.local/lib/python3.12/site-packages/trimesh/collision.py__init__zContactData.__init__   sR     Z
Ah

E!HgjjA
~~kk//    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        zz$r   N)
__name__
__module____qualname____doc__r   propertyr   r   r   r"    r   r   r   r      sM    0" 	 	 	 	 	 	 r   r   c                   2    e Zd ZdZd Zed        Zd Zd Zy)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                  | _        y)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__]   so     Z
Ah		58VYY?
!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   s*    -& 	 	 ""r   r+   c                   j    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y)CollisionManagerz(
    A mesh-mesh collision manager.
    c                     t         t        d      i | _        t        j                  d       | _        t        j                         | _        | j                  j                          y)z;
        Initialize a mesh-mesh collision manager.
        Nz7No FCL Available! Please install the python-fcl libraryc                       y Nr)   r)   r   r   <lambda>z+CollisionManager.__init__.<locals>.<lambda>   s    dr   )	fcl
ValueError_objscollectionsdefaultdict_namesDynamicAABBTreeCollisionManager_managersetupr   s    r   r   zCollisionManager.__init__   sP     ;VWW
 "--l;;;=r   Nc                 v   |t        j                  d      }t        j                  |t         j                        }|j                  dk7  rt        d      | j                  |      }t        j                  |ddddf   |dddf         }t        j                  ||      }|| j                  v r(| j                  j                  | j                  |          ||d| j                  |<   || j                  t        |      <   | j                  j                  |       | 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MM)2::>	??f$788   & MM)BQBF+Yrr1u-=>a( 4::MM**4::d+;<#$d3

4 $BtH$$Q'r   c                 l   || j                   v r| j                  j                  | j                   |   d          | j                  j                  | j                   |   d          t	        | j                   j                  |      d         }| j                  j                  |       y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::MM**4::d+;E+BCMM  D!1%!89-f56GKKOOG$v%?@AAr   c                     || j                   v r`| j                   |   d   }|j                  |ddddf          |j                  |dddf          | j                  j	                  |       y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-.MM  #v%?@AAr   c                 b   |t        j                  d      }| j                  |      }t        j                  |ddddf   |dddf         }t        j
                  ||      }t        j                         }|s|r+t        j                  t        j                  dd            }| j                  j                  ||t        j                         |j                  j                  }	t               }
g }|s|r|j                  j                  D ]  }|j                  }||k(  r|j                   }| j#                  |      }|df}||j                   k(  rt%        t'        |            }|r|
j)                  |       |sn|j+                  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   & MM)BQBF+Yrr1u-=>a( !!#;%%,,fUYZE 	a(D(DE**  E; <<00ZZ: B))"-|,#!(5/2E%))$/ ''E7(CD 1 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 ]{  }| j                  |j                        | j                  |j                        f}|r#|j                  t        t        |                   |sa|j!                  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  s   0 !!#;%%,,fUYZE 	eS%A%AB**E; <<00++GJJ79K9KGJJ9WX%))%u*>? ''E7(CD 1 K,l::,,,<''Mr   c                    t        j                         }|s|r+t        j                  t        j                  dd            }| j                  j	                  |j                  |t         j
                         |j                  j                  }t               }g }|s|r|j                  j                  D ]  }d}	| j                  |j                        |j                  |j                        f}
|
d   8| j                  |j                        |j                  |j                        f}
d}	|r|j                  |
       |s|	rt        t        |
            }
|j!                  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  sp   8 !!#;%%,,fUYZE 	m44eS=Y=YZ**E; <<00&&wzz2!//

; 8#**7::6%33GJJ?E #G%))%0 %huo 6 ''E7(CD% 1( K,l::,,,<''Mr   c                 n   |t        j                  d      }| j                  |      }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                  j                  ||t        j                         |j                  j                  }	d\  }
}|s|r|j                  j                  }||k(  r|j                  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   & MM)BQBF+Yrr1u-=>a(   !4!4D!QR$$##*.t ""$	E 	q%)D)DE<<,,  
d+BTz\\__%%b)D<(EU\\__$huo.u||4D;T4''T>!T>!Or   c                    t        j                  t        j                  d            }|r=t        j                  t        j                  dd      t        j                               }||| j                  vrt        | d      | j                  |   d   }| j                  j                  |       | j                  j                  |       | j                  j                  ||t         j                         | j                  j                  |       | j                  j                          n*| j                  j                  |t         j                         |j                  j                  }d\  }}|s|rt| j                  |j                  j                        | j                  |j                  j                         f}t        ||j                        }t#        t%        |            }|r|r|||fS |r||fS |r||fS |S )a  
        Get the minimum distance between objects in the manager.

        If name is provided, computes the minimum distance between the
        specified object and any other object in the manager.
        If name is None, computes the minimum distance between any pair
        of objects in the manager.

        Parameters
        -------------
        name : str or None
          If provided, the identifier for the object already in the manager
          to compute distances from. If None, computes distances between
          all pairs of objects.
        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 objects
        names : (2,) str
          The names of the closest objects
        data : DistanceData
          Extra data about the distance query
        Tr   r   r^   rJ   r   )r<   r+   r   r   r>   r=   rC   rT   rW   r3   r   rV   r1   r/   rv   rt   ru   rw   r   )	r   r!   r{   r|   r   rJ   r3   r   r   s	            r   min_distance_internalz&CollisionManager.min_distance_internal  s   <   !4!4D!QR$$##*.+/ ""$E 4::% D6)C!DEE**T"5)CMM**3/MM  % MM""3s/J/JK MM((-MM  " MM""5#*E*EF<<,, t;""5<<??3""5<<??3E  u||4D&-(EKUD((U?"T>!Or   c                 P   t        j                  t        j                  d            }|r=t        j                  t        j                  dd      t        j                               }| j                  j                  |j                  |t         j                         |j                  j                  }d\  }}|s|rd}| j                  |j                  j                        |j                  |j                  j                        f}|d   Ld}| j                  |j                  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_otherd  so   6   !4!4D!QR$$##*.+/ ""$E 	}55uc>Y>YZ<<,, t;G""5<<??3++ELLOO<E Qx&&u||7!//@
 5\Fx/05DKUD((U?"T>!Or   c                 N    |j                   rt        |      }|S 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-     >> &C 
 d#C
r   c                 2    | 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     {{2d8$$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* EJKZ4lBJ DIHTN`CJ*%r   r7   c                    t        j                         }|j                  t        | j                        t        | j
                               |j                  | 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     sV     ,,.CNNS_C<NNOOO$--4::O>LLNJr   c                 .   t        j                  dt        j                  t        | j                        dft         j
                        z  | j                  fd      }t        j                  | j                  t        |      |j                               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     sd     
	
RWWc$**oq):	:DJJGa
B ::dmmSWbjjl;;r   c                     t               }i }| j                  j                  D ]8  }| j                  |   \  }}|j                  || j                  |   |      ||<   : ||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     sm       GG**kk$'8**ENN84 + 
 +
 Gr   )r?   numpyrL   r<   BaseExceptionr   r+   r7   r   r   r   r)   r   r   <module>r      s`     
I  I XC" C"Lo% o%d*<(c  
Cs   6 A A 