All files / packages/tools/src/utilities/planar isPlaneIntersectingAABB.ts

7.69% Statements 1/13
0% Branches 0/4
0% Functions 0/1
7.69% Lines 1/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                              1x                                                                                  
import { vec3 } from 'gl-matrix';
 
/**
 * Checks if a plane intersects with an Axis-Aligned Bounding Box (AABB).
 *
 * @param origin - The origin point of the plane.
 * @param normal - The normal vector of the plane.
 * @param minX - The minimum x-coordinate of the AABB.
 * @param minY - The minimum y-coordinate of the AABB.
 * @param minZ - The minimum z-coordinate of the AABB.
 * @param maxX - The maximum x-coordinate of the AABB.
 * @param maxY - The maximum y-coordinate of the AABB.
 * @param maxZ - The maximum z-coordinate of the AABB.
 * @returns A boolean indicating whether the plane intersects with the AABB.
 */
export const isPlaneIntersectingAABB = (
  origin,
  normal,
  minX,
  minY,
  minZ,
  maxX,
  maxY,
  maxZ
) => {
  const vertices = [
    vec3.fromValues(minX, minY, minZ),
    vec3.fromValues(maxX, minY, minZ),
    vec3.fromValues(minX, maxY, minZ),
    vec3.fromValues(maxX, maxY, minZ),
    vec3.fromValues(minX, minY, maxZ),
    vec3.fromValues(maxX, minY, maxZ),
    vec3.fromValues(minX, maxY, maxZ),
    vec3.fromValues(maxX, maxY, maxZ),
  ];
 
  const normalVec = vec3.fromValues(normal[0], normal[1], normal[2]);
  const originVec = vec3.fromValues(origin[0], origin[1], origin[2]);
 
  // Compute the distance from the plane to the origin using vec3.dot
  const planeDistance = -vec3.dot(normalVec, originVec);
 
  // Check if all vertices are on the same side of the plane
  let initialSign = null;
  for (const vertex of vertices) {
    // Calculate distance using vec3.dot to simplify the equation
    const distance = vec3.dot(normalVec, vertex) + planeDistance;
    if (initialSign === null) {
      initialSign = Math.sign(distance);
    } else if (Math.sign(distance) !== initialSign) {
      return true; // Found a vertex on the other side, so it intersects
    }
  }
 
  return false;
};