import { triangle_compute_signed_volume } from "../triangle/triangle_compute_signed_volume.js";


/**
 * Uses counter-clock-wise winding rule
 * Very similar to "orient3d" method
 * @see https://en.wikipedia.org/wiki/Tetrahedron
 * @param {ArrayLike<number>|number[]|Float32Array} points
 * @param {number} a
 * @param {number} b
 * @param {number} c
 * @param {number} d
 * @returns {number}
 */
export function tetrahedron_compute_signed_volume(points, a, b, c, d) {

    const a3 = a * 3;
    const b3 = b * 3;
    const c3 = c * 3;
    const d3 = d * 3;

    const ax = points[a3];
    const ay = points[a3 + 1];
    const az = points[a3 + 2];

    const bx = points[b3];
    const by = points[b3 + 1];
    const bz = points[b3 + 2];

    const cx = points[c3];
    const cy = points[c3 + 1];
    const cz = points[c3 + 2];

    const dx = points[d3];
    const dy = points[d3 + 1];
    const dz = points[d3 + 2];

    const adx = ax - dx;
    const ady = ay - dy;
    const adz = az - dz;

    const bdx = bx - dx;
    const bdy = by - dy;
    const bdz = bz - dz;

    const cdx = cx - dx;
    const cdy = cy - dy;
    const cdz = cz - dz;

    return triangle_compute_signed_volume(
        adx, ady, adz,
        bdx, bdy, bdz,
        cdx, cdy, cdz
    );
}
