import { v3_distance_above_plane } from "../../vec3/v3_distance_above_plane.js";
import { EPSILON } from "../../../math/EPSILON.js";

/**
 * Checks if point lies in a volume bound by a number of planes, such a camera frustum
 * @param {number} x
 * @param {number} y
 * @param {number} z
 * @param {number} plane_mask
 * @param {number[]} planes
 * @param {number} plane_count
 * @return {boolean}
 * @private
 */
export function is_point_within_planes(
    x, y, z,
    plane_mask,
    planes,
    plane_count
) {

    for (let plane_index = 0; plane_index < plane_count; plane_index++) {
        const plane_bit_mask = 1 << plane_index;

        if ((plane_mask & plane_bit_mask) === 0) {
            // non-intersecting plane, ignore
            continue;
        }

        const plane_address = plane_index * 4;

        const nx = planes[plane_address];
        const ny = planes[plane_address + 1];
        const nz = planes[plane_address + 2];
        const c = planes[plane_address + 3];

        const distance_to_plane = v3_distance_above_plane(x, y, z, nx, ny, nz, c);

        if (distance_to_plane < -EPSILON) {
            // point is outside of one of the planes
            return false;
        }
    }

    return true;
}
