/**
 * Calculates the incenter of a triangle.
 *
 * @see https://en.wikipedia.org/wiki/Incenter
 * @param {number[]|Float32Array|Float64Array|ArrayLike<number>} output
 * @param {number} output_offset
 * @param {number} x0
 * @param {number} y0
 * @param {number} z0
 * @param {number} x1
 * @param {number} y1
 * @param {number} z1
 * @param {number} x2
 * @param {number} y2
 * @param {number} z2
 * @returns {boolean} true when the result was calculated, false for degenerate triangle
 */
export function triangle3_incenter(
    output, output_offset,
    x0, y0, z0,
    x1, y1, z1,
    x2, y2, z2
) {
    // Calculate edge lengths opposite to each vertex

    // 'a' is length of edge v1-v2 (opposite v0)
    const dxA = x2 - x1;
    const dyA = y2 - y1;
    const dzA = z2 - z1;

    const a = Math.sqrt(dxA * dxA + dyA * dyA + dzA * dzA);

    // 'b' is length of edge v2-v0 (opposite v1)
    const dxB = x0 - x2;
    const dyB = y0 - y2;
    const dzB = z0 - z2;

    const b = Math.sqrt(dxB * dxB + dyB * dyB + dzB * dzB);

    // 'c' is length of edge v0-v1 (opposite v2)
    const dxC = x1 - x0;
    const dyC = y1 - y0;
    const dzC = z1 - z0;

    const c = Math.sqrt(dxC * dxC + dyC * dyC + dzC * dzC);

    const perimeter = a + b + c;

    if (perimeter <= 0) {
        // 0 perimeter triangle
        return false;
    }

    output[output_offset] = (a * x0 + b * x1 + c * x2) / perimeter;
    output[output_offset + 1] = (a * y0 + b * y1 + c * y2) / perimeter;
    output[output_offset + 2] = (a * z0 + b * z1 + c * z2) / perimeter;

    return true;

}