import { v3_array_matrix4_rotate } from "../../vec3/v3_array_matrix4_rotate.js";
import { v3_matrix4_multiply } from "../../vec3/v3_matrix4_multiply.js";

/**
 *
 * @param {number[]|ArrayLike<number>|Float32Array} output 6 component vector, [origin_x, origin_y, origin_z, direction_x, direction_y, direction_z]
 * @param {number} output_offset
 * @param {number[]|ArrayLike<number>|Float32Array} input 6 component vector, [origin_x, origin_y, origin_z, direction_x, direction_y, direction_z]
 * @param {number} input_offset
 * @param {number[]|ArrayLike<number>|Float32Array} m4 4x4 matrix
 * @returns {boolean} false if matrix transformation is impossible, such as when scale of the matrix is 0
 */
export function ray3_array_apply_matrix4(output, output_offset, input, input_offset, m4) {

    const origin_x = input[input_offset + 0];
    const origin_y = input[input_offset + 1];
    const origin_z = input[input_offset + 2];

    // transform ray to local space (inlined for speed)
    const det = m4[3] * origin_x + m4[7] * origin_y + m4[11] * origin_z + m4[15];

    if (det === 0) {
        return false;
    }

    v3_matrix4_multiply(output, output_offset, input, input_offset, m4);
    v3_array_matrix4_rotate(output, output_offset + 3, input, input_offset + 3, m4);

    return true;
}
