
    Ti1                         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dS )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	d
S )RayMeshIntersectorzs
    An object to query a mesh for ray intersections.
    Precomputes an r-tree for each triangle on the mesh.
    c                 Z    || _         t          j        | j         j                  | _        d S )N)meshr   Cache__hash___cache)selfr   s     R/DATA/AppData/hermes/venv/lib/python3.11/site-packages/trimesh/ray/ray_triangle.py__init__zRayMeshIntersector.__init__   s#    	mDI$677    FTc                 6   t          | j        j        ||| j        j        || j        j                  \  }}}|r]t          |          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)#))'!Y3-
 -
 -
)Iy  	K9~~"" )Y66)"/9i:P*Q*QRRSTUFV$i&769JJJ)##r   c                 6     | 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   sK    . -?D,> -
#)!-
 -
 	-
 -
)Iy )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   dtyper-   )r+   r#   onesr!   int64)r   r   r   r&   r'   r(   results          r   intersects_firstz#RayMeshIntersector.intersects_firste   sq    $ "4!3 "
#)"	"
 "

 "
 "
I [)):::R?%yr   c                     |                      ||          \  }}t          j        t          |          t                    }t          j        |          }t          |          dk    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   si      !% 2 2; O O
I(3{++4888)I&&w<<!#GG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    $ tV,,,r   N)FT)
__name__
__module____qualname____doc__r   r+   r.   r6   r=   r   r-   r   r   r   r      s         
8 8 8 -$ -$ -$ -$^/ / />  @  .- - - - -r   r   Tc                    t          j        | t           j                  } t          j        |t           j                  }t          j        |t           j                  }|t          j        |           }t          |||          \  }}| |         }||         }	||         }
|dddddf         }|;t          j        |          \  }}|                                st          d          n||         }t          j
        |||	|
          \  }}t          |          dk    s|                                s_t          j        g t           j                  t          j        g t           j                  t          j        g t           j                  fS t          j        ||         |          }t          j        |t"          j         k                        d          |dt"          j        z   k                         d                    }||         |         }||         |         }||         }|||         z
  }t'          j        |||                   d	k    }||         }||         }||         }|         |r|||fS t          |          dk    r|||fS t          j        fd
t+          j        |          D                       }||         ||         ||         fS )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ưc                 P    g | ]"}||                                                   #S r-   )argmin).0gdistances     r   
<listcomp>z#ray_triangle_id.<locals>.<listcomp>*  s.    QQQ!a**,,-QQQr   )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)r   r   r   r   r   r   ray_candidatesray_idtriangle_candidatesrG   rH   rE   rF   triangle_oklocationvalidbarycentrichitr'   r(   vectorforwardfirstrO   s                          @r   r   r      s   B irz:::I-2:>>>K]>DDDN |(33 5T  NF $N3v&L$V,O (1aaa0M%2%:;N%O%O"{   	31222	3 )8 $0##!'	  OHe 1$$EIIKK$ HRrx(((HRrx(((HRrz***
 	
  5E"H K .	y	 %%1%--q38|0L/Q/QWX/Q/Y/Y C u%c*I uc"I}H I..F 	)BCCHG'"I'"I H H .)X-- 9~~)X-- HQQQQx~i7P7PQQQRREUYu-x>>r   c                    t          | ||j                  }g }g }t          |          D ]b\  }}t          |                    |                    }|                    |           |                    |gt          |          z             ct          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_boundsrm   	enumeratelistintersectionextendr!   r#   r[   r4   )	r   r   r   boundingindex
candidatesirm   cands	            r   rU   rU   /  s    0 t{  H EJx(( & &	6D%%f--..$aS3t99_%%%%8Jbh///%rx1P1P1PPPr   h㈵>c                     t          j         t           j                   t          j        t           j                  t          j        |          }t          j                                      d          }|                    d          j        |         }t          j         fdt          |          D                                           d          }t          j        fdt          |          D                                           d          }|dk                        d	          }t          j	        |          }	||         ||         z
  ||         z  |	|<   ||	|	|k     <   |	d
d
df                             d          }
|	d
d
df                             d          }|
z   z   }|z   z   }t          j
        ||f                              d	dj        d         f          }t          j        |                    d          |                    d          f          }|t          j        g d          |z  z  }|S )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   c                 2    g | ]\  }}|         |         S r-   r-   )rM   rv   ar   s      r   rP   zray_bounds.<locals>.<listcomp>o  s&    GGGtq!Q*GGGr   )r2   r
   c                 2    g | ]\  }}|         |         S r-   r-   )rM   rv   r{   r   s      r   rP   zray_bounds.<locals>.<listcomp>p  s'    JJJ$!Q*1-JJJr   g        r2   Nr   r   )r2   r2   r2   r
   r
   r
   )r#   rQ   rR   absargmaxreshapeTr[   ro   
zeros_liker$   shapehstackminmax)r   r   rm   buffer_distrJ   
axis_boundaxis_oriaxis_dirnonzerott_at_bon_aon_bon_planeray_boundings   ``              r   rn   rn   T  s3   $ -2:>>>K]>DDDN ]6""F 6.!!((a(00D((*40JxGGGGyGGGHHPPQXYYHxJJJJ)D//JJJKKSS H 3''++G
j!!AW%(99Xg=NNAgJ %Aa+o AAAqD'//'
"
"C
AAAqD'//'
"
"C S K/DS K/Dd|,,44b!^=QRS=T5UVVH9hlll22HLLaL4H4HIJJL
 BH22233kAALr   )NNT)rx   )rC   numpyr#    r   r   r   r   r   rS   	constantsr	   ray_utilr   r   r   rU   rn   r-   r   r   <module>r      s         5 5 5 5 5 5 5 5 5 5 5 5 ) ) ) ) ) )       % % % % % %a- a- a- a- a- a- a- a-P 	{? {? {? {?|"Q "Q "QJ> > > > > >r   