import { v3_dot } from "../../vec3/v3_dot.js";

/**
 * 2,0,or -2; 2: above, -2 : below, 0 : intersects plane
 * @param {number} plane_normal_x
 * @param {number} plane_normal_y
 * @param {number} plane_normal_z
 * @param {number} plane_constant
 * @param {number} _x0
 * @param {number} _y0
 * @param {number} _z0
 * @param {number} _x1
 * @param {number} _y1
 * @param {number} _z1
 * @returns {number}
 */
export function aabb3_compute_plane_side(
    plane_normal_x,
    plane_normal_y,
    plane_normal_z,
    plane_constant,
    _x0,
    _y0,
    _z0,
    _x1,
    _y1,
    _z1
) {

    let x0,
        y0,
        z0,
        x1,
        y1,
        z1;

    // construct nearest and farthest corners along the plane normal
    if (plane_normal_x > 0) {
        x0 = _x0;
        x1 = _x1;
    } else {
        x0 = _x1;
        x1 = _x0;
    }

    if (plane_normal_y > 0) {
        y0 = _y0;
        y1 = _y1;
    } else {
        y0 = _y1;
        y1 = _y0;
    }

    if (plane_normal_z > 0) {
        z0 = _z0;
        z1 = _z1;
    } else {
        z0 = _z1;
        z1 = _z0;
    }

    /*
      planar tests are inlined for performance and to save on a few operation,
      if you write it out fully it would be vec4 dot product and comparison to 0,
      since we don't have 4th component for AABB corners,
      we can use vec3 and just move planar offset to the right-hand side of the comparison operation
     */

    const neg_plane_constant = -plane_constant;

    // check the farthest corner
    if (v3_dot(x1, y1, z1, plane_normal_x, plane_normal_y, plane_normal_z) < neg_plane_constant) {
        // if furthest corner is below plane, the nearest one will be as well, no need to continue
        return -2;
    }

    // check the nearest corner
    if (v3_dot(plane_normal_x, plane_normal_y, plane_normal_z, x0, y0, z0) >= neg_plane_constant) {
        // above the plane, both corners are above the plane
        return 2;
    }

    return 0;
}
