Skip to content

Commit

Permalink
feat: add enum for plane side
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnsgn committed May 12, 2022
1 parent 7b5643f commit 43897e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 14 additions & 3 deletions plane.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import { hitTestPlane } from "./ray.js";
* @typedef {number[][]} plane A plane defined by a 3D point and a normal vector perpendicular to the plane’s surface.
*/

/**
* Enum for different side values
* @readonly
* @enum {number}
*/
export const SIDE = Object.freeze({
ON_PLANE: 0,
SAME: -1,
OPPOSITE: 1,
});

const TEMP_0 = vec3.create();

/**
Expand Down Expand Up @@ -45,7 +56,7 @@ export function side([planePoint, planeNormal], point) {
vec3.sub(TEMP_0, point);
vec3.normalize(TEMP_0);
const dot = vec3.dot(TEMP_0, planeNormal);
if (dot > 0) return 1;
if (dot < 0) return -1;
return 0;
if (dot > 0) return SIDE.OPPOSITE;
if (dot < 0) return SIDE.SAME;
return SIDE.ON_PLANE;
}
6 changes: 3 additions & 3 deletions test/plane.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ describe("plane", () => {

describe("side()", () => {
it("should return -1 if the point is on the side of the normal", () => {
deepEqual(plane.side(Y_UP_PLANE, [0, 1, 0]), -1);
deepEqual(plane.side(Y_UP_PLANE, [0, 1, 0]), plane.SIDE.SAME);
});
it("should return 1 if the point is on the opposite side of the normal", () => {
deepEqual(plane.side(Y_UP_PLANE, [0, -1, 0]), 1);
deepEqual(plane.side(Y_UP_PLANE, [0, -1, 0]), plane.SIDE.OPPOSITE);
});
it("should return 0 if the point is on the plane", () => {
deepEqual(plane.side(Y_UP_PLANE, ORIGIN), 0);
deepEqual(plane.side(Y_UP_PLANE, ORIGIN), plane.SIDE.ON_PLANE);
});
});
});

0 comments on commit 43897e4

Please sign in to comment.