Ë
    :vžjÅ1  ã                   ó|   — d Z ddlZddlmZmZmZmZ ddlmZ	 ddl
mZ ddlmZ  G d	„ d
«      Z	 	 	 dd„Zd„ Zdd„Zy)z7
A basic slow implementation of ray- triangle queries.
é    Né   )ÚcachingÚgroupingÚintersectionsÚutil)Ú	triangles)Útolé   ©Úcontains_pointsc                   ó:   — e Zd ZdZd„ Z	 	 d	d„Zd„ Zd„ Zd„ Zd„ Z	y)
ÚRayMeshIntersectorzs
    An object to query a mesh for ray intersections.
    Precomputes an r-tree for each triangle on the mesh.
    c                 ón   — || _         t        j                  | j                   j                  «      | _        y )N)Úmeshr   ÚCacheÚ__hash__Ú_cache)Úselfr   s     úi/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/trimesh/ray/ray_triangle.pyÚ__init__zRayMeshIntersector.__init__   s#   € ØˆŒ	Ü—m‘m D§I¡I×$6Ñ$6Ó7ˆ�ó    c                 óL  — t        | j                  j                  ||| j                  j                  || j                  j                  ¬«      \  }}}|rNt        |«      dk(  r|||fS t        j                  t        j                  ||f«      «      d   }	||	   ||	   ||	   fS ||fS )a¬  
        Find the intersections between the current mesh and an
        array of rays.

        Parameters
        ------------
        ray_origins :  (m, 3) float
          Ray origin points
        ray_directions : (m, 3) float
          Ray direction vectors
        multiple_hits :  bool
          Consider multiple hits of each ray or not
        return_locations : bool
          Return hit locations or not

        Returns
        -----------
        index_triangle : (h,) int
          Index of triangles hit
        index_ray : (h,) int
          Index of ray that hit triangle
        locations : (h, 3) float
          [optional] Position of intersection in space
        )r   Úray_originsÚray_directionsÚtreeÚmultiple_hitsÚtriangles_normalr   )
Úray_triangle_idr   r   Útriangles_treeÚface_normalsÚlenr   Úunique_rowsÚnpÚcolumn_stack)
r   r   r   Úreturn_locationsr   ÚkwargsÚ	index_triÚ	index_rayÚ	locationsÚuniques
             r   Úintersects_idz RayMeshIntersector.intersects_id   s¯   € ô@ -<Ø—i‘i×)Ñ)Ø#Ø)Ø—‘×)Ñ)Ø'Ø!ŸY™Y×3Ñ3ô-
Ñ)ˆ�I˜yñ Ü�9‹~ Ò"Ø  )¨YÐ6Ð6Ü×)Ñ)¬"¯/©/¸9ÀiÐ:PÓ*QÓRÐSTÑUˆFØ˜VÑ$ i°Ñ&7¸À6Ñ9JÐJÐJØ˜)Ð#Ð#r   c                 ó@   —  | j                   d||ddœ|¤Ž\  }}}|||fS )až  
        Return unique cartesian locations where rays hit the mesh.
        If you are counting the number of hits a ray had, this method
        should be used as if only the triangle index is used on- edge hits
        will be counted twice.

        Parameters
        ------------
        ray_origins : (m, 3) float
          Ray origin points
        ray_directions : (m, 3) float
          Ray direction vectors

        Returns
        ---------
        locations : (n) sequence of (m,3) float
          Intersection points
        index_ray : (n,) int
          Array of ray indexes
        index_tri: (n,) int
          Array of triangle (face) indexes
        T)r   r   r%   © )r+   )r   r   r   r&   r'   r(   r)   s          r   Úintersects_locationz&RayMeshIntersector.intersects_locationF   sD   € ð. -?¨D×,>Ñ,>ð -
Ø#Ø)Ø!ñ-
ð ñ	-
Ñ)ˆ�I˜yð ˜) YÐ.Ð.r   c                 ó¦   —  | j                   d||dddœ|¤Ž\  }}t        j                  t        |«      t        j                  ¬«      dz  }|||<   |S )an  
        Find the index of the first triangle a ray hits.


        Parameters
        ----------
        ray_origins : (n, 3) float
          Origins of rays
        ray_directions : (n, 3) float
          Direction (vector) of rays

        Returns
        ----------
        triangle_index : (n,) int
          Index of triangle ray hit, or -1 if not hit
        F)r   r   r%   r   ©Údtypeéÿÿÿÿr-   )r+   r#   Úonesr!   Úint64)r   r   r   r&   r'   r(   Úresults          r   Úintersects_firstz#RayMeshIntersector.intersects_firste   se   € ð$ "4 ×!3Ñ!3ð "
Ø#Ø)Ø"Øñ	"
ð
 ñ"
Ñˆ�Iô —‘œ˜[Ó)´·±Ô:¸RÑ?ˆØ%ˆˆyÑàˆr   c                 óÈ   — | j                  ||«      \  }}t        j                  t        |«      t        ¬«      }t        j
                  |«      }t        |«      dkD  rd||<   |S )ad  
        Find out if each ray hit any triangle on the mesh.

        Parameters
        ------------
        ray_origins : (m, 3) float
          Ray origin points
        ray_directions : (m, 3) float
          Ray direction vectors

        Returns
        ---------
        hit : (m,) bool
          Whether any ray hit any triangle on the mesh
        r0   r   T)r+   r#   Úzerosr!   Úboolr*   )r   r   r   r&   Ú
_index_trir(   Úhit_anyÚhit_idxs           r   Úintersects_anyz!RayMeshIntersector.intersects_any…   sY   € ð  !%× 2Ñ 2°;ÀÓ OÑˆ
�IÜ—(‘(œ3˜{Ó+´4Ô8ˆÜ—)‘)˜IÓ&ˆÜˆw‹<˜!ÒØ#ˆG�GÑØˆr   c                 ó   — t        | |«      S )as  
        Check if a mesh contains a list of points, using ray tests.

        If the point is on the surface of the mesh the behavior
        is undefined.

        Parameters
        ------------
        points : (n, 3) float
          Points in space

        Returns
        ---------
        contains : (n,) bool
          Whether point is inside mesh or not
        r   )r   Úpointss     r   r   z"RayMeshIntersector.contains_pointsœ   s   € ô$ ˜t VÓ,Ð,r   N)FT)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r+   r.   r6   r=   r   r-   r   r   r   r      s/   „ ñò
8ð Øó-$ò^/ò>ò@ó.-r   r   c                 ó   — t        j                  | t         j                  ¬«      } t        j                  |t         j                  ¬«      }t        j                  |t         j                  ¬«      }|€t        j                  | «      }t        |||¬«      \  }}| |   }||   }	||   }
|dd…ddd…f   }|€3t        j                  |«      \  }}|j                  «       st        d«      ‚||   }t        j                  |||	|
¬«      \  }}t        |«      dk(  s|j                  «       snt        j                  g t         j                  ¬«      t        j                  g t         j                  ¬«      t        j                  g t         j                  ¬«      fS t        j                  ||   |«      }t        j                   |t"        j$                   kD  j                  d¬«      |dt"        j$                  z   k  j                  d¬«      «      }||   |   }||   |   }||   }|||   z
  }t'        j(                  |||   «      }|d	kD  }||   }||   }||   }||   }|r|||fS t        |«      dk(  r|||fS t        j                  t+        j,                  |«      D �cg c]  }|||   j/                  «          ‘Œ c}«      }||   ||   ||   fS c c}w )
a‚  
    Find the intersections between a group of triangles and rays

    Parameters
    -------------
    triangles : (n, 3, 3) float
      Triangles in space
    ray_origins : (m, 3) float
      Ray origin points
    ray_directions : (m, 3) float
      Ray direction vectors
    triangles_normal : (n, 3) float
      Normal vector of triangles, optional
    tree : rtree.Index
      Rtree object holding triangle bounds

    Returns
    -----------
    index_triangle : (h,) int
      Index of triangles hit
    index_ray : (h,) int
      Index of ray that hit triangle
    locations : (h, 3) float
      Position of intersection in space
    r0   N)r   r   r   r   zInvalid triangles!)Úplane_originsÚplane_normalsÚline_originsÚline_directionsr
   ©Úaxisg�íµ ÷Æ°¾)r#   Ú
asanyarrayÚfloat64Útriangles_modÚbounds_treeÚray_triangle_candidatesÚnormalsÚallÚ
ValueErrorr   Úplanes_linesr!   ÚanyÚarrayr4   Úpoints_to_barycentricÚlogical_andr	   Úzeror   Údiagonal_dotr   ÚgroupÚargmin)r   r   r   r   r   r   Úray_candidatesÚray_idÚtriangle_candidatesrG   rH   rE   rF   Útriangle_okÚlocationÚvalidÚbarycentricÚhitr'   r(   ÚvectorÚdistanceÚforwardÚgÚfirsts                            r   r   r   ±   sÍ  € ôB —‘˜i¬r¯z©zÔ:€IÜ—-‘- ´2·:±:Ô>€KÜ—]‘] >¼¿¹ÔD€Nð €|Ü×(Ñ(¨Ó3ˆô 5Ø°ÀTôÑ€N�Fð $ NÑ3Ðà˜vÑ&€LØ$ VÑ,€Oð (ª¨1ªa¨Ñ0€MØÐÜ%2×%:Ñ%:Ð;NÓ%OÑ"ˆ�{Ø�‰Ô ÜÐ1Ó2Ð2à(¨Ñ8ˆô $×0Ñ0Ø#Ø#Ø!Ø'ô	�O€Hˆeô ÐÓ 1Ò$¨E¯I©I¬Kô �H‰H�RœrŸx™xÔ(Ü�H‰H�RœrŸx™xÔ(Ü�H‰H�RœrŸz™zÔ*ð
ð 	
ô  ×5Ñ5Ø˜EÑ" Hó€Kô �.‰.Ø	œŸ™�yÑ	 ×%Ñ%¨1Ð%Ó-°¸qÄ3Ç8Á8¹|Ñ0L×/QÑ/QÐWXÐ/QÓ/Yó€Cð ˜uÑ% cÑ*€Ið �u‘˜cÑ"€Ià˜‰}€Hð ˜ IÑ.Ñ.€FÜ× Ñ  ¨¸	Ñ)BÓC€HØ˜Ñ€Gà˜'Ñ"€IØ˜'Ñ"€IØ˜Ñ €HØ˜Ñ €HáØ˜) XÐ-Ð-ô ˆ9ƒ~˜ÒØ˜) XÐ-Ð-ô �H‰H´x·~±~ÀiÔ7PÓQÑ7P°!�a˜ ™×*Ñ*Ó,Ó-Ð7PÑQÓR€Eà�UÑ˜Y uÑ-¨x¸©Ð>Ð>ùò Rs   ÊKc                 ó†  — t        | ||j                  ¬«      }g }g }t        |«      D ]N  \  }}t        |j	                  |«      «      }|j                  |«       |j                  |gt        |«      z  «       ŒP t        j                  |t        j                  ¬«      t        j                  |t        j                  ¬«      fS )a1  
    Do broad- phase search for triangles that the rays
    may intersect.

    Does this by creating a bounding box for the ray as it
    passes through the volume occupied by the tree

    Parameters
    ------------
    ray_origins : (m, 3) float
      Ray origin points.
    ray_directions : (m, 3) float
      Ray direction vectors
    tree : rtree object
      Ccontains AABB of each triangle

    Returns
    ----------
    ray_candidates : (n,) int
      Triangle indexes
    ray_id : (n,) int
      Corresponding ray index for a triangle candidate
    )r   r   Úboundsr0   )
Ú
ray_boundsrj   Ú	enumerateÚlistÚintersectionÚextendr!   r#   rU   r4   )	r   r   r   ÚboundingÚindexÚ
candidatesÚirj   Úcands	            r   rO   rO   /  sœ   € ô0 Ø°ÀtÇ{Á{ô€Hð €EØ€JÜ˜xÖ(‰	ˆˆ6Ü�D×%Ñ% fÓ-Ó.ˆØ×Ñ˜$ÔØ�‰�a�Sœ3˜t›9‘_Õ%ð )ô �8‰8�J¤b§h¡hÔ/´·±¸%ÄrÇxÁxÔ1PÐPÐPr   c                 ó˜  — t        j                  | t         j                  ¬«      } t        j                  |t         j                  ¬«      }t        j                  |«      }t        j                  |«      j	                  d¬«      }|j                  d«      j                  |   }t        j                  t        |«      D ��cg c]  \  }}| |   |   ‘Œ c}}«      j                  d«      }t        j                  t        |«      D ��cg c]  \  }}||   |   ‘Œ c}}«      j                  d«      }	|	dk7  j                  d«      }
t        j                  |«      }||
   ||
   z
  |	|
   z  ||
<   ||||k  <   |dd…d	f   j                  d«      }|dd…df   j                  d«      }||z  | z   }||z  | z   }t        j                  ||f«      j                  dd
|j                  d   f«      }t        j                  |j                  d¬«      |j                  d¬«      f«      }|t        j                  g d¢«      |z  z  }|S c c}}w c c}}w )a&  
    Given a set of rays and a bounding box for the volume of interest
    where the rays will be passing through, find the bounding boxes
    of the rays as they pass through the volume.

    Parameters
    ------------
    ray_origins:      (m,3) float, ray origin points
    ray_directions:   (m,3) float, ray direction vectors
    bounds:           (2,3) bounding box (min, max)
    buffer_dist:      float, distance to pad zero width bounding boxes

    Returns
    ---------
    ray_bounding: (n) set of AABB of rays passing through volume
    r0   r
   rI   )r   r2   )r2   r
   g        r2   Nr   r   )r2   r2   r2   r
   r
   r
   )r#   rK   rL   ÚabsÚargmaxÚreshapeÚTrU   rl   Ú
zeros_liker$   ÚshapeÚhstackÚminÚmax)r   r   rj   Úbuffer_distrJ   Ú
axis_boundrs   ÚaÚaxis_oriÚaxis_dirÚnonzeroÚtÚt_aÚt_bÚon_aÚon_bÚon_planeÚray_boundings                     r   rk   rk   T  s  € ô$ —-‘- ´2·:±:Ô>€KÜ—]‘] >¼¿¹ÔD€Nô �]‰]˜6Ó"€Fô �6‰6�.Ó!×(Ñ(¨aÐ(Ó0€DØ—‘ Ó(×*Ñ*¨4Ñ0€JÜ�x‰x´yÀ´ÔG±©t¨q°!˜ Q™¨Ó*°ÒGÓH×PÑPÐQXÓY€HÜ�x‰x¼)ÀD¼/ÔJ¹/±$°!°Q˜¨Ñ*¨1Ó-¸/ÒJÓK×SÑSØó€Hð ˜3‰×'Ñ'¨Ó+€GÜ
�‰�jÓ!€AØ˜WÑ%¨°Ñ(9Ñ9¸XÀgÑ=NÑN€A€g�Jð %€A€aˆ+�oÑð ŠAˆqˆD‰'�/‰/˜'Ó
"€CØ
ŠAˆqˆD‰'�/‰/˜'Ó
"€Cð ˜SÑ  KÑ/€DØ˜SÑ  KÑ/€Dä�‰  d˜|Ó,×4Ñ4°b¸!¸^×=QÑ=QÐRSÑ=TÐ5UÓV€Hä—9‘9˜hŸl™l°˜lÓ2°H·L±LÀa°LÓ4HÐIÓJ€Lð
 ”B—H‘HÒ2Ó3°kÑAÑA€LàÐùóG HùÛJs   Ã I 
ÄI
)NNT)gñhãˆµøä>)rC   Únumpyr#   Ú r   r   r   r   r   rM   Ú	constantsr	   Úray_utilr   r   r   rO   rk   r-   r   r   Ú<module>r�      sI   ðñó ç 5Ó 5Ý )Ý Ý %÷a-ñ a-ðP Ø	Øó{?ò|"QôJ>r   