Skip to content

Instantly share code, notes, and snippets.

@marmakoide
Created November 15, 2022 09:22
Show Gist options
  • Save marmakoide/6890d920a5f6931c0f4c7548f8b6ea51 to your computer and use it in GitHub Desktop.
Save marmakoide/6890d920a5f6931c0f4c7548f8b6ea51 to your computer and use it in GitHub Desktop.
Check if an axis aligned bounding box intersects the segment [AB]. Box extents are half the size of the box
def aabox_segment_3d_intersecting(box_center, box_extents, A, B):
M = (A + B) / 2 - box_center
AB = B - A
if numpy.any(numpy.fabs(M) > box_extents + .5 * numpy.fabs(AB)):
return False
U = AB / numpy.sqrt(numpy.sum(AB ** 2))
abs_U = numpy.fabs(U)
abs_W = numpy.fabs(numpy.cross(U, M))
if abs_W[0] > box_extents[1] * abs_U[2] + box_extents[2] * abs_U[1]:
return False
if abs_W[1] > box_extents[2] * abs_U[0] + box_extents[0] * abs_U[2]:
return False
if abs_W[2] > box_extents[0] * abs_U[1] + box_extents[1] * abs_U[0]:
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment