-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplane.js
54 lines (48 loc) · 1.09 KB
/
plane.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/** @module plane */
import { vec3 } from "pex-math";
/**
* Enum for different side values
* @readonly
* @enum {number}
*/
export const Side = Object.freeze({
OnPlane: 0,
Same: -1,
Opposite: 1,
});
const TEMP_0 = vec3.create();
/**
* Creates a new plane
* @returns {import("./types.js").plane}
*/
export function create() {
return [
[0, 0, 0],
[0, 1, 0],
];
}
/**
* Returns on which side a point is.
* @param {import("./types.js").plane} plane
* @param {import("./types.js").vec3} point
* @returns {number}
*/
export function side([planePoint, planeNormal], point) {
vec3.set(TEMP_0, planePoint);
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.OnPlane;
}
/**
* Prints a plane to a string.
* @param {import("./types.js").plane} a
* @param {number} [precision=4]
* @returns {string}
*/
export function toString(a, precision = 4) {
// prettier-ignore
return `[${vec3.toString(a[0], precision)}, ${vec3.toString(a[1], precision)}]`;
}