Skip to content

Commit

Permalink
feat: change enum case to pascal case
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnsgn committed May 25, 2022
1 parent f6ac708 commit 40a4c06
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 47 deletions.
14 changes: 7 additions & 7 deletions plane.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import { hitTestPlane } from "./ray.js";
* @readonly
* @enum {number}
*/
export const SIDE = Object.freeze({
ON_PLANE: 0,
SAME: -1,
OPPOSITE: 1,
export const Side = Object.freeze({
OnPlane: 0,
Same: -1,
Opposite: 1,
});

const TEMP_0 = vec3.create();
Expand All @@ -45,7 +45,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 SIDE.OPPOSITE;
if (dot < 0) return SIDE.SAME;
return SIDE.ON_PLANE;
if (dot > 0) return Side.Opposite;
if (dot < 0) return Side.Same;
return Side.OnPlane;
}
32 changes: 16 additions & 16 deletions ray.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { vec3 } from "pex-math";
* @readonly
* @enum {number}
*/
export const INTERSECTIONS = Object.freeze({
INTERSECT: 1,
NO_INTERSECT: 0,
SAME_PLANE: -1,
PARALLEL: -2,
TRIANGLE_DEGENERATE: -2,
export const Intersections = Object.freeze({
Intersect: 1,
NoIntersect: 0,
SamePlane: -1,
Parallel: -2,
TriangleDegenerate: -2,
});

const TEMP_0 = vec3.create();
Expand Down Expand Up @@ -57,15 +57,15 @@ export function hitTestPlane(ray, point, normal, out = vec3.create()) {
const direction = vec3.set(TEMP_1, ray[1]);

const dotDirectionNormal = vec3.dot(direction, normal);
if (dotDirectionNormal === 0) return INTERSECTIONS.SAME_PLANE;
if (dotDirectionNormal === 0) return Intersections.SamePlane;

point = vec3.set(TEMP_2, point);

const t = vec3.dot(vec3.sub(point, origin), normal) / dotDirectionNormal;
if (t < 0) return INTERSECTIONS.PARALLEL;
if (t < 0) return Intersections.Parallel;

vec3.set(out, vec3.add(origin, vec3.scale(direction, t)));
return INTERSECTIONS.INTERSECT;
return Intersections.Intersect;
}

/**
Expand All @@ -86,7 +86,7 @@ export function hitTestTriangle(
const v = vec3.sub(vec3.set(TEMP_1, p2), p0);
const n = vec3.cross(vec3.set(TEMP_2, u), v);

if (vec3.length(n) < EPSILON) return INTERSECTIONS.TRIANGLE_DEGENERATE;
if (vec3.length(n) < EPSILON) return Intersections.TriangleDegenerate;

// ray vectors
const w0 = vec3.sub(vec3.set(TEMP_3, origin), p0);
Expand All @@ -96,14 +96,14 @@ export function hitTestTriangle(
const b = vec3.dot(n, direction);

if (Math.abs(b) < EPSILON) {
if (a === 0) return INTERSECTIONS.SAME_PLANE;
return INTERSECTIONS.NO_INTERSECT;
if (a === 0) return Intersections.SamePlane;
return Intersections.NoIntersect;
}

// get intersect point of ray with triangle plane
const r = a / b;
// ray goes away from triangle
if (r < -EPSILON) return INTERSECTIONS.NO_INTERSECT;
if (r < -EPSILON) return Intersections.NoIntersect;

// for a segment, also test if (r > 1.0) => no intersect
// intersect point of ray and plane
Expand All @@ -125,17 +125,17 @@ export function hitTestTriangle(

// get and test parametric coords
const s = (uv * wv - vv * wu) / D;
if (s < -EPSILON || s > 1.0 + EPSILON) return INTERSECTIONS.NO_INTERSECT;
if (s < -EPSILON || s > 1.0 + EPSILON) return Intersections.NoIntersect;

const t = (uv * wu - uu * wv) / D;
if (t < -EPSILON || s + t > 1.0 + EPSILON) return INTERSECTIONS.NO_INTERSECT;
if (t < -EPSILON || s + t > 1.0 + EPSILON) return Intersections.NoIntersect;

vec3.set(out, u);
vec3.scale(out, s);
vec3.add(out, vec3.scale(vec3.set(TEMP_7, v), t));
vec3.add(out, p0);

return INTERSECTIONS.INTERSECT;
return Intersections.Intersect;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/plane.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,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]), plane.SIDE.SAME);
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]), plane.SIDE.OPPOSITE);
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), plane.SIDE.ON_PLANE);
deepEqual(plane.side(Y_UP_PLANE, ORIGIN), plane.Side.OnPlane);
});
});
});
40 changes: 19 additions & 21 deletions test/ray.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {

import { ray } from "../index.js";

const { INTERSECTIONS } = ray;

describe("ray", () => {
it("create() should create a new ray with origin and direction", () => {
deepEqual(ray.create(), [ORIGIN, [0, 0, 1]]);
Expand All @@ -23,15 +21,15 @@ describe("ray", () => {
it("should return 1 if the ray is intersecting", () => {
deepEqual(
ray.hitTestPlane(Y_UP_RAY, ...Y_UP_PLANE),
INTERSECTIONS.INTERSECT
ray.Intersections.Intersect
);
deepEqual(
ray.hitTestPlane(Y_DOWN_RAY, ...Y_UP_PLANE),
INTERSECTIONS.INTERSECT
ray.Intersections.Intersect
);
deepEqual(
ray.hitTestPlane(XYZ_RAY, ...Y_UP_PLANE),
INTERSECTIONS.INTERSECT
ray.Intersections.Intersect
);
});
it("should set the plane intersection point", () => {
Expand All @@ -42,7 +40,7 @@ describe("ray", () => {
it("should return -1 if the ray is not intersecting (ray on the plane)", () => {
deepEqual(
ray.hitTestPlane(Z_UP_RAY, ...Y_UP_PLANE),
INTERSECTIONS.SAME_PLANE
ray.Intersections.SamePlane
);
});
it("should return -2 if the ray is not intersecting (ray parallel to plane)", () => {
Expand All @@ -54,7 +52,7 @@ describe("ray", () => {
],
...Y_UP_PLANE
),
INTERSECTIONS.PARALLEL
ray.Intersections.Parallel
);
});
});
Expand All @@ -70,15 +68,15 @@ describe("ray", () => {
it("should intersect the triangle", () => {
deepEqual(
ray.hitTestTriangle(Y_UP_RAY, triangle),
INTERSECTIONS.INTERSECT
ray.Intersections.Intersect
);
deepEqual(
ray.hitTestTriangle(Y_DOWN_RAY, triangle),
INTERSECTIONS.INTERSECT
ray.Intersections.Intersect
);
deepEqual(
ray.hitTestTriangle(XYZ_RAY, triangle),
INTERSECTIONS.INTERSECT
ray.Intersections.Intersect
);
});
it("should set the triangle intersection point", () => {
Expand All @@ -93,7 +91,7 @@ describe("ray", () => {
[0, 0, 1],
[1, 0, -1],
]),
INTERSECTIONS.TRIANGLE_DEGENERATE
ray.Intersections.TriangleDegenerate
);
});
it("should not intersect if a ray is outside the triangle", () => {
Expand All @@ -105,7 +103,7 @@ describe("ray", () => {
],
triangle
),
INTERSECTIONS.NO_INTERSECT
ray.Intersections.NoIntersect
);
});
it("should not intersect if a ray is outside the triangle (ray parallel to triangle)", () => {
Expand All @@ -117,17 +115,17 @@ describe("ray", () => {
],
triangle
),
INTERSECTIONS.NO_INTERSECT
ray.Intersections.NoIntersect
);
});
it("should intersect if a ray is coplanar to the triangle", () => {
deepEqual(
ray.hitTestTriangle(X_UP_RAY, triangle),
INTERSECTIONS.SAME_PLANE
ray.Intersections.SamePlane
);
deepEqual(
ray.hitTestTriangle(Z_UP_RAY, triangle),
INTERSECTIONS.SAME_PLANE
ray.Intersections.SamePlane
);
});
});
Expand All @@ -138,10 +136,10 @@ describe("ray", () => {
[1, 1, 1],
];
it("should intersect the box", () => {
deepEqual(ray.hitTestAABB(Y_UP_RAY, box), INTERSECTIONS.INTERSECT);
deepEqual(ray.hitTestAABB(Y_DOWN_RAY, box), INTERSECTIONS.INTERSECT);
deepEqual(ray.hitTestAABB(XYZ_RAY, box), INTERSECTIONS.INTERSECT);
deepEqual(ray.hitTestAABB(Z_UP_RAY, box), INTERSECTIONS.INTERSECT);
deepEqual(ray.hitTestAABB(Y_UP_RAY, box), ray.Intersections.Intersect);
deepEqual(ray.hitTestAABB(Y_DOWN_RAY, box), ray.Intersections.Intersect);
deepEqual(ray.hitTestAABB(XYZ_RAY, box), ray.Intersections.Intersect);
deepEqual(ray.hitTestAABB(Z_UP_RAY, box), ray.Intersections.Intersect);
});
it("should not intersect if a ray is outside the box", () => {
deepEqual(
Expand All @@ -152,7 +150,7 @@ describe("ray", () => {
],
box
),
INTERSECTIONS.NO_INTERSECT
ray.Intersections.NoIntersect
);
});
it("should intersect if a ray is coplanar to one of the boxes sides", () => {
Expand All @@ -164,7 +162,7 @@ describe("ray", () => {
],
box
),
INTERSECTIONS.INTERSECT
ray.Intersections.Intersect
);
});
});
Expand Down

0 comments on commit 40a4c06

Please sign in to comment.