MCPcopy Create free account
hub / github.com/apple/ml-pointersect / ray_aabb_intersection

Function ray_aabb_intersection

plib/utils.py:266–326  ·  view source on GitHub ↗

Check whether a ray intersect with an axis-aligned bounding box. Args: ray_origin: (3,) a point on the ray (with t = 0) ray_direction: (3,) ray direction bbox_min_bounds: (3,) top left corner bbox_max_bounds:

(
        ray_origin: torch.Tensor,
        ray_direction: torch.Tensor,
        bbox_min_bounds: torch.Tensor,
        bbox_max_bounds: torch.Tensor,
        bbox_scaling_ratio: float = 1.0,
        t_min: float = 0.,
        t_max: float = 1.0e10,
)

Source from the content-addressed store, hash-verified

264
265
266def ray_aabb_intersection(
267 ray_origin: torch.Tensor,
268 ray_direction: torch.Tensor,
269 bbox_min_bounds: torch.Tensor,
270 bbox_max_bounds: torch.Tensor,
271 bbox_scaling_ratio: float = 1.0,
272 t_min: float = 0.,
273 t_max: float = 1.0e10,
274) -> T.Dict[str, T.Any]:
275 """
276 Check whether a ray intersect with an axis-aligned bounding box.
277
278 Args:
279 ray_origin:
280 (3,) a point on the ray (with t = 0)
281 ray_direction:
282 (3,) ray direction
283 bbox_min_bounds:
284 (3,) top left corner
285 bbox_max_bounds:
286 (3,) bottom right corner. The bbox encloses bbox_min_bounds to bbox_max_bounds.
287 bbox_scaling_ratio:
288 a scalar where we will scale the bbox wrt to its center.
289 t_min:
290 min t to consider
291 t_max:
292 max t to consider
293
294 Returns:
295 is_intersected: True if intersect.
296 t0: first intersection t
297 t1: second intersection t
298 """
299
300 # scale bbox
301 bbox_center = 0.5 * (bbox_min_bounds + bbox_max_bounds)
302 bbox_min_bounds = bbox_center + (bbox_min_bounds - bbox_center) * bbox_scaling_ratio
303 bbox_max_bounds = bbox_center + (bbox_max_bounds - bbox_center) * bbox_scaling_ratio
304
305 inv_ray_direction = 1. / ray_direction # (3,)
306 _t_nears = (bbox_min_bounds - ray_origin) * inv_ray_direction # (3,)
307 _t_fars = (bbox_max_bounds - ray_origin) * inv_ray_direction # (3,)
308
309 t_nears = torch.where(_t_nears > _t_fars, _t_fars, _t_nears)
310 t_fars = torch.where(_t_nears > _t_fars, _t_nears, _t_fars)
311
312 t_nears[torch.isnan(t_nears)] = -torch.inf
313 t_fars[torch.isnan(t_fars)] = torch.inf
314
315 t_near = torch.max(t_nears) # scalar, use fmin to ignore nan, no need for max
316 t_far = torch.min(t_fars) # scalar, use fmax to ignore nan, no need for min
317
318 t_near = torch.max(t_near, torch.ones_like(t_near) * t_min)
319 t_far = torch.min(t_far, torch.ones_like(t_far) * t_max)
320
321 is_intersect = t_near <= t_far
322 return dict(
323 is_intersected=is_intersect,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected