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

/**
 * Compute AABB for a group of triangles based on index-buffer
 * @param {number[]|Float32Array} output
 * @param {number} output_offset
 * @param {number[]|ArrayLike<number>} indices
 * @param {number[]|ArrayLike<number>} positions
 * @param {number} index_offset
 * @param {number} index_count
 * @param {number} dimension_count
 */
export function compute_triangle_group_aabb3(
    output,
    output_offset,
    indices,
    positions,
    index_offset,
    index_count,
    dimension_count
) {

    let i = 0;
    for (; i < dimension_count; i++) {
        // min bound
        output[output_offset + i] = Infinity;
        // max bound
        output[output_offset + i + dimension_count] = -Infinity;
    }

    for (i = 0; i < index_count; i++) {
        for (let j = 0; j < 3; j++) {
            const index = indices[index_offset + i * 3 + j];

            const i3 = index * dimension_count;

            for (let k = 0; k < dimension_count; k++) {
                const v = positions[i3 + k];

                output[output_offset + k] = min2(output[output_offset + k], v);
                output[output_offset + k + dimension_count] = max2(output[output_offset + k + dimension_count], v);
            }
        }
    }
}
