dhart.raytracer.Intersect

dhart.raytracer.Intersect(bvh: EmbreeBVH, origin: Tuple[float, float, float] | List[Tuple[float, float, float]], direction: Tuple[float, float, float] | List[Tuple[float, float, float]], max_distance: float = -1.0) array | Tuple[float, int]

Cast one or more rays to get the distance to their point of intersection and the ID of the mesh they intersected.

In situations where multiple rays are shot, rays will be cast in parallel.

Note

Accepts the following configurations:
  1. Single origin, single direction

  2. Multiple origins, multiple directions

  3. Single origin, multiple directions

  4. Multiple origins, single direction

Parameters:
  • bvh – A valid Embree BVH

  • origin – a single tuple of x,y,z coordinates, or a list of x,y,z tuples

  • direction – A single direction or list of directions

  • max_distance – the maximum distance tto still be considered a hit for the ray

Returns:

If a single ray, then a tuple of float

for distance and an int for meshid. If multiple rays are casted, an array of RayResult structs is returned in a RayResult List. In all cases, a distance/meshid of -1 indicates a miss.

Return type:

Union[RayResultList, Tuple[float, int]]

Examples

Casting a single ray

>>> import numpy as np
>>> from numpy.lib import recfunctions as rfn
>>> from dhart.geometry import LoadOBJ, CommonRotations, ConstructPlane
>>> from dhart.raytracer import EmbreeBVH, Intersect
>>>
>>> loaded_obj = ConstructPlane()
>>> loaded_obj.Rotate(CommonRotations.Yup_to_Zup)
>>> bvh = EmbreeBVH(loaded_obj)
>>>
>>> result = Intersect(bvh, (0,0,1), (0,0,-1))
>>> print(np.around(result,5))
[1. 0.]
>>> #Casting rays with an equal number of directions and origins
>>>
>>> loaded_obj = ConstructPlane()
>>> loaded_obj.Rotate(CommonRotations.Yup_to_Zup)
>>> bvh = EmbreeBVH(loaded_obj)
>>>
>>> hit_point = Intersect(bvh, [(0,0,1)] * 10, [(0,0,-1)] * 10)
>>> # Convert to numpy unstructured and round
>>> print(np.around(rfn.structured_to_unstructured(hit_point.array),5))
[[1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]
 [1. 0.]]
>>> #Casting multiple rays with one direction and multiple origins
>>>
>>> loaded_obj = ConstructPlane()
>>> loaded_obj.Rotate(CommonRotations.Yup_to_Zup)
>>> bvh = EmbreeBVH(loaded_obj)
>>>
>>> origins = [(0,0,x) for x in range(0,5)]
>>> hit_point = Intersect(bvh, origins, (0,0,-1))
>>> print(np.around(rfn.structured_to_unstructured(hit_point.array),5))
[[-1. -1.]
 [ 1.  0.]
 [ 2.  0.]
 [ 3.  0.]
 [ 4.  0.]]