From 35c7ca2324f9d8eb22d70f0d329827c555357f9d Mon Sep 17 00:00:00 2001 From: Damien Seguin Date: Thu, 30 Jun 2022 16:52:25 +0100 Subject: [PATCH] feat: change hitTestPlane signature to use plane - optimise ray --- ray.js | 25 ++++++++++++++----------- test/ray.js | 12 ++++++------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/ray.js b/ray.js index 34f4a19..b068ee2 100644 --- a/ray.js +++ b/ray.js @@ -42,32 +42,35 @@ export function create() { } /** - * Determines if a ray intersect a plane + * Determines if a ray intersect a plane and set intersection point * https://www.cs.princeton.edu/courses/archive/fall00/cs426/lectures/raycast/sld017.htm * @param {ray} ray - * @param {import("pex-math").vec3} point - * @param {import("pex-math").vec3} normal + * @param {plane} plane * @param {import("pex-math").vec3} out * @returns {number} */ -export function hitTestPlane(ray, point, normal, out = vec3.create()) { - const origin = vec3.set(TEMP_0, ray[0]); - const direction = vec3.set(TEMP_1, ray[1]); +export function hitTestPlane( + [origin, direction], + [point, normal], + out = vec3.create() +) { + vec3.set(TEMP_0, origin); + vec3.set(TEMP_1, direction); - const dotDirectionNormal = vec3.dot(direction, normal); + const dotDirectionNormal = vec3.dot(TEMP_1, normal); if (dotDirectionNormal === 0) return Intersections.SamePlane; - point = vec3.set(TEMP_2, point); + vec3.set(TEMP_2, point); - const t = vec3.dot(vec3.sub(point, origin), normal) / dotDirectionNormal; + const t = vec3.dot(vec3.sub(TEMP_2, TEMP_0), normal) / dotDirectionNormal; if (t < 0) return Intersections.Parallel; - vec3.set(out, vec3.add(origin, vec3.scale(direction, t))); + vec3.set(out, vec3.add(TEMP_0, vec3.scale(TEMP_1, t))); return Intersections.Intersect; } /** - * Determines if a ray intersect a triangle + * Determines if a ray intersect a triangle and set intersection point * http://geomalgorithms.com/a06-_intersect-2.html#intersect3D_RayTriangle() * @param {ray} ray * @param {triangle} triangle diff --git a/test/ray.js b/test/ray.js index 1efafa9..b7afa6b 100644 --- a/test/ray.js +++ b/test/ray.js @@ -20,26 +20,26 @@ describe("ray", () => { describe("hitTestPlane()", () => { it("should return 1 if the ray is intersecting", () => { deepEqual( - ray.hitTestPlane(Y_UP_RAY, ...Y_UP_PLANE), + ray.hitTestPlane(Y_UP_RAY, Y_UP_PLANE), ray.Intersections.Intersect ); deepEqual( - ray.hitTestPlane(Y_DOWN_RAY, ...Y_UP_PLANE), + ray.hitTestPlane(Y_DOWN_RAY, Y_UP_PLANE), ray.Intersections.Intersect ); deepEqual( - ray.hitTestPlane(XYZ_RAY, ...Y_UP_PLANE), + ray.hitTestPlane(XYZ_RAY, Y_UP_PLANE), ray.Intersections.Intersect ); }); it("should set the plane intersection point", () => { const out = []; - ray.hitTestPlane(Y_UP_RAY, ...Y_UP_PLANE, out); + ray.hitTestPlane(Y_UP_RAY, Y_UP_PLANE, out); deepEqual(out, ORIGIN); }); it("should return -1 if the ray is not intersecting (ray on the plane)", () => { deepEqual( - ray.hitTestPlane(Z_UP_RAY, ...Y_UP_PLANE), + ray.hitTestPlane(Z_UP_RAY, Y_UP_PLANE), ray.Intersections.SamePlane ); }); @@ -50,7 +50,7 @@ describe("ray", () => { [0, 1, 0], [0, 1, 1], ], - ...Y_UP_PLANE + Y_UP_PLANE ), ray.Intersections.Parallel );