import { max2 } from "../../../math/max2.js";

/**
 * Created a grid of points to fill given bounds, spacing is used as upper-bound,
 * actual spacing will be justified to provide even distanced along each dimension separately
 * @param {AABB3} aabb3
 * @param {number} spacing
 * @param {function(x:number,y:number,z:number)} callback
 * @returns {number} total number of points placed
 */
export function make_justified_point_grid(aabb3, spacing, callback) {

    const steps_x = max2(1, Math.ceil(aabb3.getExtentsX() / spacing));
    const steps_y = max2(1, Math.ceil(aabb3.getExtentsY() / spacing));
    const steps_z = max2(1, Math.ceil(aabb3.getExtentsZ() / spacing));

    const spacing_x = aabb3.getExtentsX() / max2(1, steps_x - 1);
    const spacing_y = aabb3.getExtentsY() / max2(1, steps_y - 1);
    const spacing_z = aabb3.getExtentsZ() / max2(1, steps_z - 1);

    for (let i = 0; i < steps_x; i++) {
        for (let j = 0; j < steps_y; j++) {
            for (let k = 0; k < steps_z; k++) {
                const x = i * spacing_x + aabb3.x0;
                const y = j * spacing_y + aabb3.y0;
                const z = k * spacing_z + aabb3.z0;

                callback(x, y, z);
            }
        }
    }

    return steps_x * steps_y * steps_z;
}
