Skip to content

Commit

Permalink
feat: change hitTestPlane signature to use plane
Browse files Browse the repository at this point in the history
- optimise ray
  • Loading branch information
dmnsgn committed Jun 30, 2022
1 parent 04edc03 commit 35c7ca2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
25 changes: 14 additions & 11 deletions ray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions test/ray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});
Expand All @@ -50,7 +50,7 @@ describe("ray", () => {
[0, 1, 0],
[0, 1, 1],
],
...Y_UP_PLANE
Y_UP_PLANE
),
ray.Intersections.Parallel
);
Expand Down

0 comments on commit 35c7ca2

Please sign in to comment.