/**
 * @see A Survey of Efficient Representations for Independent Unit Vectors (Journal of Computer Graphics Techniques Vol. 3, No. 2, 2014)
 *
 * Uses hemi-octahedron for sphere approximation
 *
 * @param {number[]} output
 * @param {number} output_offset
 * @param {number} x range [-1,1]
 * @param {number} y range [-1,1]
 */
export function decode_hemioct_to_unit(output, output_offset, x, y) {
    // Rotate and scale the unit square back to the center diamond
    const t_x = (x + y) * 0.5;
    const t_y = (x - y) * 0.5;

    //
    const v_z = 1.0 - Math.abs(t_x) - Math.abs(t_y);

    // normalize
    const l2 = (t_x * t_x + t_y * t_y + v_z * v_z);
    const m = 1 / Math.sqrt(l2);

    output[output_offset] = t_x * m;
    output[output_offset + 1] = t_y * m;
    output[output_offset + 2] = v_z * m;
}
