
    j1                     |    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     E/DATA/.local/lib/python3.12/site-packages/trimesh/ray/ray_triangle.py__init__zRayMeshIntersector.__init__   s#    	mmDII$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    @ -<ii))#)))'!YY33-
)Iy 9~" )Y66))"//9i:P*QRSTUFV$i&769J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,>,> -
#)!-
 	-
)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   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<!#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 -$^/>@.-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 irzz:I--2::>K]]>DN |((3 5TNF $N3v&L$V,O (1a0M%2%:%:;N%O"{ 122(8 $00##!'	OHe 1$EIIK HHRrxx(HHRrxx(HHRrzz*
 	
  55E"HK ..	y	 %%1%-q388|0L/Q/QWX/Q/YC u%c*I uc"I}H I..F  	)BCHG'"I'"I H H)X-- 9~)X-- HHx~~i7PQ7P!a**,-7PQREUYu-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-.$aS3t9_% ) 88Jbhh/%rxx1P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]]>DN ]]6"F 66.!((a(0D(**40JxxyGtq!Q*GHPPQXYHxx)D/J/$!Q*1-/JKSSH 3''+G
j!AW%(99Xg=NNAgJ %Aa+o AqD'//'
"C
AqD'//'
"C S K/DS K/Dd|,44b!^=Q=QRS=T5UVH99hlll2HLLaL4HIJL
 BHH23kAALG HJs    I 
I
)NNT)gh㈵>)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   