"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVoxelIndex = exports.setVoxelAt = exports.forEachVoxel = exports.cloneVoxelData = exports.validateVoxelData = exports.createFilledVoxelData = exports.clampSizeAxis = void 0;
const clampSizeAxis = (value) => {
    if (!Number.isFinite(value))
        return 1;
    const rounded = Math.floor(Math.max(1, Math.min(64, value)));
    return rounded;
};
exports.clampSizeAxis = clampSizeAxis;
const createFilledVoxelData = (size, materialIndex = 0) => {
    const [sx, sy, sz] = size;
    const total = sx * sy * sz;
    return new Array(total).fill(materialIndex);
};
exports.createFilledVoxelData = createFilledVoxelData;
const validateVoxelData = (voxels, size) => {
    const expected = size[0] * size[1] * size[2];
    if (voxels.length !== expected)
        return false;
    return voxels.every(value => Number.isInteger(value) && value >= -1);
};
exports.validateVoxelData = validateVoxelData;
const cloneVoxelData = (voxels) => voxels.slice();
exports.cloneVoxelData = cloneVoxelData;
const forEachVoxel = (voxel, callback) => {
    const [sx, sy, sz] = voxel.size;
    const { voxels } = voxel;
    for (let z = 0; z < sz; z += 1) {
        for (let y = 0; y < sy; y += 1) {
            for (let x = 0; x < sx; x += 1) {
                const index = x + y * sx + z * sx * sy;
                callback({ x, y, z, index }, voxels[index] ?? -1);
            }
        }
    }
};
exports.forEachVoxel = forEachVoxel;
const setVoxelAt = (voxels, size, coord, value) => {
    const [sx, sy, sz] = size;
    if (coord.x < 0 ||
        coord.x >= sx ||
        coord.y < 0 ||
        coord.y >= sy ||
        coord.z < 0 ||
        coord.z >= sz) {
        return;
    }
    const index = coord.x + coord.y * sx + coord.z * sx * sy;
    voxels[index] = value;
};
exports.setVoxelAt = setVoxelAt;
const getVoxelIndex = (size, coord) => {
    return coord.x + coord.y * size[0] + coord.z * size[0] * size[1];
};
exports.getVoxelIndex = getVoxelIndex;
//# sourceMappingURL=voxels.js.map