import { SurfacePoint3 } from "../SurfacePoint3.js";
import { computeTriangleRayIntersection } from "./computeTriangleRayIntersection.js";


const sp3 = new SurfacePoint3();

/**
 * NOTE: adapted from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteIntrRay3Triangle3.h
 * @source https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm (Möller and Trumbore, « Fast, Minimum Storage Ray-Triangle Intersection », Journal of Graphics Tools, vol. 2,‎ 1997, p. 21–28)
 * @param {Vector3} result
 * @param {Vector3} rayOrigin
 * @param {Vector3} rayDirection
 * @param {Vector3} a
 * @param {Vector3} b
 * @param {Vector3} c
 * @returns {boolean}
 */
export function rayTriangleIntersection(
    result,
    rayOrigin, rayDirection,
    a, b, c
) {

    const hit_found = computeTriangleRayIntersection(
        sp3,
        rayOrigin.x, rayOrigin.y, rayOrigin.z,
        rayDirection.x, rayDirection.y, rayDirection.z,
        a.x, a.y, a.z,
        b.x, b.y, b.z,
        c.x, c.y, c.z
    );

    result.copy(sp3.position);

    return hit_found;
}

