import { v3_length_sqr } from "./v3_length_sqr.js";

/**
 *
 * @param {number[]|Float32Array} output
 * @param {number} output_offset
 * @param {number[]|Float32Array} input
 * @param {number} input_offset
 */
export function v3_array_normalize(
    output, output_offset,
    input, input_offset,
) {


    // extract vector components
    const x = input[input_offset];
    const y = input[input_offset + 1];
    const z = input[input_offset + 2];

    // compute vector inverse magnitude
    const length_sqr = v3_length_sqr(x, y, z);

    // avoid division by 0
    const n = length_sqr !== 0 ? 1.0 / Math.sqrt(length_sqr) : 1;

    // write back
    output[output_offset] = x * n;
    output[output_offset + 1] = y * n;
    output[output_offset + 2] = z * n;

}