Ë
    :vžj#-  ã                   ó¬   — d Z ddlZddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZmZ d	d
lmZ ej$                  ZdZdZ G d„ d«      Z G d„ d«      Zy)z[
Ray queries using the embreex package with the
API wrapped to match our native raytracer.
é    N)Úrtcore_scene)ÚTriangleMeshé   )ÚcachingÚintersectionsÚutil)Úlog_time)Ú	ArrayLikeÚIntegeré   ©Úcontains_pointsg�íµ ÷Æ°>g:Œ0âŽyE>c                   óÈ   — e Zd Zddefd„Zed„ «       Zej                  d„ «       Z		 dde
de
defd„Ze	 	 	 dde
de
ded	ed
ef
d„«       Zed„ «       Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zy)ÚRayMeshIntersectorÚscale_to_boxc                 ó~   — || _         || _        t        j                  | j                   j                  ¬«      | _        y)a7  
        Do ray- mesh queries.

        Parameters
        -------------
        geometry : Trimesh object
          Mesh to do ray tests on
        scale_to_box : bool
          If true, will scale mesh to approximate
          unit cube to avoid problems with extreme
          large or small meshes.
        ©Úid_functionN)ÚmeshÚ_scale_to_boxr   ÚCacheÚ__hash__Ú_cache)ÚselfÚgeometryr   s      úi/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/trimesh/ray/ray_pyembree.pyÚ__init__zRayMeshIntersector.__init__   s-   € ð ˆŒ	Ø)ˆÔÜ—m‘m°·	±	×0BÑ0BÔCˆ�ó    c                 óX   — | j                   rd| j                  j                  z  }|S d}|S )z/
        Scaling factor for precision.
        g      Y@g      ð?)r   r   Úscale)r   r    s     r   Ú_scalezRayMeshIntersector._scale+   s4   € ð
 ×Òð ˜DŸI™IŸO™OÑ+ˆEð ˆð ˆEØˆr   c                 ó‚   — t        | j                  j                  | j                  j                  | j                  ¬«      S )z8
        A cached version of the embreex scene.
        )ÚverticesÚfacesr    )Ú_EmbreeWrapr   r#   r$   r!   )r   s    r   Ú_scenezRayMeshIntersector._scene8   s.   € ô
 Ø—Y‘Y×'Ñ'¨t¯y©y¯©ÀdÇkÁkô
ð 	
r   Úray_originsÚray_directionsÚmultiple_hitsc                 ó>   — | j                  |||d¬«      \  }}}|||fS )aØ  
        Return the location of where a ray hits a surface.

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

        Returns
        ---------
        locations : (m) sequence of (p, 3) float
          Intersection points
        index_ray : (m,) int
          Indexes of ray
        index_tri : (m,) int
          Indexes of mesh.faces
        T)r'   r(   r)   Úreturn_locations)Úintersects_id)r   r'   r(   r)   Ú	index_triÚ	index_rayÚ	locationss          r   Úintersects_locationz&RayMeshIntersector.intersects_locationA   s;   € ð2 -1×,>Ñ,>Ø#Ø)Ø'Ø!ð	 -?ó -
Ñ)ˆ�I˜yð ˜) YÐ.Ð.r   Úmax_hitsr+   c                 óˆ  — t        j                  |t         j                  ¬«      }t        j                  |t         j                  ¬«      }|j                  |j                  k7  rt	        d«      ‚t        j                  |«      }t        j                  dt         j                  ¬«      g}t        j                  dt         j                  ¬«      g}t        j                  dt         j                  ¬«      g}|s|rft        t        | j                  j                  t        z  «      }	||	z  }
| j                  j                  dd…ddd…f   }| j                  j                  }t        j                   t#        |«      dt         j                  ¬«      }t        j$                  t#        |«      «      }t'        |«      D �]@  }| j(                  j+                  ||   ||   «      }|dk7  }|j-                  «       s �n||   }||   }|s&|s$|j/                  |«       |j/                  |«        nÔ||   |k(  }|||<   ||    }||    }||   }t1        j2                  |   |   ||   ||   ¬«      \  }}||   ||   }}|j/                  |«       |j/                  |«       |j/                  |«       |s nM|
|   z   ||<   ||   	z  |
|<   |
|xx   dz  cc<   ||xx   |
|   z  cc<   t        j4                  ||g«      }�ŒC t        j4                  |«      }t        j4                  |«      }|r||t        j4                  |«      fS ||fS )	aC  
        Find the triangles hit by a list of rays, including
        optionally multiple hits along a single ray.


        Parameters
        ----------
        ray_origins : (n, 3) float
          Origins of rays
        ray_directions : (n, 3) float
          Direction (vector) of rays
        multiple_hits : bool
          If True will return every hit along the ray
          If False will only return first hit
        max_hits : int
          Maximum number of hits per ray
        return_locations : bool
          Should we return hit locations or not

        Returns
        ---------
        index_tri : (m,) int
          Indexes of mesh.faces
        index_ray : (m,) int
          Indexes of ray
        locations : (m) sequence of (p, 3) float
          Intersection points, only returned if return_locations
        ©Údtypeú%Ray origin and direction don't match!r   )r   é   Néÿÿÿÿ)Úplane_originsÚplane_normalsÚline_originsÚline_directionsg       @)ÚnpÚarrayÚfloat64ÚshapeÚ
ValueErrorr   ÚunitizeÚzerosÚint64ÚmaxÚ_ray_offset_floorr   r    Ú_ray_offset_factorÚ	trianglesÚface_normalsÚfullÚlenÚarangeÚranger&   ÚrunÚanyÚappendr   Úplanes_linesÚconcatenate)r   r'   r(   r)   r1   r+   Úresult_triangleÚresult_ray_idxÚresult_locationsÚbase_offsetÚray_offsetsr8   r9   Úlast_hitÚliveÚ_depthÚqueryÚhitÚhit_raysÚhit_trisÚdupeÚok_raysÚok_trisÚ	dupe_raysÚnew_originsÚvalidr-   r.   s                               r   r,   z RayMeshIntersector.intersects_idc   s  € ôN —h‘h˜{´"·*±*Ô=ˆÜŸ™ .¼¿
¹
ÔCˆØ×Ñ × 4Ñ 4Ò4ÜÐDÓEÐEÜŸ™ nÓ5ˆô Ÿ8™8 A¬R¯X©XÔ6Ð7ˆÜŸ(™( 1¬B¯H©HÔ5Ð6ˆÜŸH™H V´2·:±:Ô>Ð?ÐáÑ,äÔ/°·±·±ÔCUÑ1UÓVˆKØ(¨;Ñ6ˆKð !ŸI™I×/Ñ/²°1²a°Ñ8ˆMØ ŸI™I×2Ñ2ˆMô —7‘7œ3˜{Ó+¨R´r·x±xÔ@ˆô �y‰yœ˜[Ó)Ó*ˆô
 ˜H—oˆFð —K‘K—O‘O K°Ñ$5°~ÀdÑ7KÓLˆEØ˜2‘+ˆCØ—7‘7”9Úð ˜C‘yˆHØ˜S‘zˆHñ !Ñ)9Ø×&Ñ& xÔ0Ø×%Ñ% hÔ/Ùð ˜HÑ%¨Ñ1ˆDà!)ˆH�XÑà ˜u‘oˆGØ ˜u‘oˆGØ  ™ˆIô
 "/×!;Ñ!;Ø+¨GÑ4Ø+¨GÑ4Ø(¨Ñ1Ø .¨wÑ 7ô	"ÑˆK˜ð  ' u™~¨w°u©~�WˆGà×#Ñ# KÔ0Ø×"Ñ" 7Ô+Ø×!Ñ! 'Ô*á Ùð $/°¸WÑ1EÑ#EˆK˜Ñ Ø#1°'Ñ#:¸[Ñ#HˆK˜Ñ à˜	Ó" cÑ)Ó"Ø˜	Ó" k°)Ñ&<Ñ<Ó"ô —>‘> 7¨IÐ"6Ó7ŠDðo &ôr —N‘N ?Ó3ˆ	Ü—N‘N >Ó2ˆ	ÙØ˜i¬¯©Ð8HÓ)IÐIÐIØ˜)Ð#Ð#r   c                 ó  — t        j                  |t        ¬«      }t        j                  |t        ¬«      }|j                  |j                  k7  rt	        d«      ‚t        j                  |«      }| j                  j                  ||«      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
        r3   r5   )	r<   r=   Ú_embree_dtyper?   r@   r   rA   r&   rM   )r   r'   r(   s      r   Úintersects_firstz#RayMeshIntersector.intersects_firstç   sg   € ô& —h‘h˜{´-Ô@ˆÜŸ™ .¼ÔFˆØ×Ñ × 4Ñ 4Ò4ÜÐDÓEÐEÜŸ™ nÓ5ˆà�{‰{�‰˜{¨NÓ;Ð;r   c                 ó6   — | j                  ||¬«      }|dk7  }|S )aO  
        Check if a list of rays hits the surface.


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

        Returns
        ----------
        hit : (n,) bool
          Did each ray hit the surface
        )r'   r(   r7   )rf   )r   r'   r(   Úfirstr[   s        r   Úintersects_anyz!RayMeshIntersector.intersects_any  s.   € ð$ ×%Ñ%Ø#°Nð &ó 
ˆð �r‰kˆØˆ
r   c                 ó   — t        | |«      S )ab  
        Check if a mesh contains a list of points, using ray tests.

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

        Parameters
        ---------
        points: (n, 3) 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   c                 ó^   — | j                   j                  «       }|j                  dd «       |S )Nr   )Ú__dict__ÚcopyÚpop©r   Ústates     r   Ú__getstate__zRayMeshIntersector.__getstate__+  s'   € Ø—‘×"Ñ"Ó$ˆà�	‰	�(˜DÔ!Øˆr   c                 ó˜   — | j                   j                  |«       t        j                  | j                  j
                  ¬«      | _        y )Nr   )rm   Úupdater   r   r   r   r   rp   s     r   Ú__setstate__zRayMeshIntersector.__setstate__1  s.   € Ø�‰×Ñ˜UÔ#ä—m‘m°·	±	×0BÑ0BÔCˆ�r   c                 ó"   — | j                  «       S )N)Ú__copy__©r   Úargss     r   Ú__deepcopy__zRayMeshIntersector.__deepcopy__6  s   € Ø�}‰}‹Ðr   c                 óD   — t        | j                  | j                  ¬«      S )N)r   r   )r   r   r   rx   s     r   rw   zRayMeshIntersector.__copy__9  s   € Ü!¨4¯9©9À4×CUÑCUÔVÐVr   N)T)Téd   F)Ú__name__Ú
__module__Ú__qualname__Úboolr   Úpropertyr!   r   Úcache_decoratorr&   r
   r0   r	   r   r,   rf   ri   r   rr   ru   rz   rw   © r   r   r   r      sð   „ ñD¨tó Dð" ñ
ó ð
ð ×Ññ
ó ð
ð #ñ	 /àð /ð "ð /ð ó	 /ðD ð
 #ØØ!&ñA$àðA$ð "ðA$ð ð	A$ð
 ðA$ð òA$ó ðA$ðF ñ<ó ð<ò4ò0-ò"òDò
óWr   r   c                   ó   — e Zd ZdZd„ Zd„ Zy)r%   z¥
    A light wrapper for Embreex scene objects which
    allows queries to be scaled to help with precision
    issues, as well as selecting the correct dtypes.
    c                 óÐ  — t        j                  |t         j                  ¬«      }|j                  d¬«      | _        t        |«      | _        || j                  z
  | j                  z  }t        j                  «       | _	        t        | j                  |j                  t        «      |j                  t         j                  «      j                  t         j                  «      ¬«       y )Nr3   r   )Úaxis)Úscener#   Úindices)r<   r=   r>   ÚminÚoriginÚfloatr    r   ÚEmbreeScener‡   r   Úastypere   ÚviewÚndarrayÚint32)r   r#   r$   r    Úscaleds        r   r   z_EmbreeWrap.__init__D  s”   € Ü—‘˜(¬"¯*©*Ô5ˆØ—j‘j a�jÓ(ˆŒÜ˜5“\ˆŒ
Ø˜4Ÿ;™;Ñ&¨$¯*©*Ñ4ˆä!×-Ñ-Ó/ˆŒ
äØ—*‘*Ø—]‘]¤=Ó1Ø—J‘JœrŸz™zÓ*×1Ñ1´"·(±(Ó;ö	
r   c                 ó  — t        j                  |t         j                  ¬«      | j                  z
  | j                  z  } | j
                  j                  |j                  t        «      |j                  t        «      fi |¤ŽS )Nr3   )	r<   r=   r>   rŠ   r    r‡   rM   r�   re   )r   ÚoriginsÚnormalsÚkwargsr‘   s        r   rM   z_EmbreeWrap.runR  s^   € Ü—(‘(˜7¬"¯*©*Ô5¸¿¹ÑCÀtÇzÁzÑQˆàˆt�z‰z�~‰~Ø�M‰Mœ-Ó(¨'¯.©.¼Ó*Gñ
ØKQñ
ð 	
r   N)r}   r~   r   Ú__doc__r   rM   rƒ   r   r   r%   r%   =  s   „ ñò
ó
r   r%   )r–   Únumpyr<   Úembreexr   Úembreex.mesh_constructionr   Ú r   r   r   Ú	constantsr	   Útypedr
   r   Úray_utilr   Úfloat32re   rF   rE   r   r%   rƒ   r   r   Ú<module>rŸ      sX   ðñó
 õ !Ý 2ç +Ñ +Ý  ß &Ý %ð —
‘
€ð Ð ØÐ ÷aWñ aW÷H	
ò 
r   