Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
135 lines (121 loc) · 2.61 KB
/
Copy pathutils.js
File metadata and controls
135 lines (121 loc) · 2.61 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/** @module utils */
/** @constant {number} */
export const EPSILON = 0.000001;
/** @constant {number} */
export const HALF_PI = Math.PI * 0.5;
/** @constant {import("./types.js").Vec3} */
export const Y_UP = /** @type {import("./types.js").Vec3} */ (
Object.freeze([0, 1, 0])
);
/**
* Linear interpolation between two numbers.
*
* @param {number} a
* @param {number} b
* @param {number} t
* @returns {number}
*/
export function lerp(a, b, t) {
return a + (b - a) * t;
}
/**
* Clamps a number between two numbers.
*
* @param {number} n
* @param {number} min
* @param {number} max
* @returns {number}
*/
export function clamp(n, min, max) {
return Math.max(min, Math.min(n, max));
}
/**
* Smooth Hermite interpolation between 0 and 1
*
* @param {number} n
* @param {number} min
* @param {number} max
* @returns {number}
*/
export function smoothstep(n, min, max) {
n = clamp((n - min) / (max - min), 0, 1);
return n * n * (3 - 2 * n);
}
/**
* Maps a number from one range to another.
*
* @param {number} n
* @param {number} inStart
* @param {number} inEnd
* @param {number} outStart
* @param {number} outEnd
* @returns {number}
*/
export function remap(n, inStart, inEnd, outStart, outEnd) {
return outStart + ((outEnd - outStart) * (n - inStart)) / (inEnd - inStart);
}
/**
* @ignore
* @deprecated Use "remap()"
*/
export function map(...args) {
return remap(...args);
}
/** @private */
export function _truncate(n, precision) {
const scale = 10 ** precision;
return Math.trunc(Number((n * scale).toPrecision(15))) / scale;
}
/**
* Transforms degrees into radians.
*
* @param {import("./types.js").Degrees} degrees
* @returns {import("./types.js").Radians}
*/
export function toRadians(degrees) {
return (degrees * Math.PI) / 180;
}
/**
* Transforms radians into degrees.
*
* @param {import("./types.js").Radians} radians
* @returns {import("./types.js").Degrees}
*/
export function toDegrees(radians) {
return (radians * 180) / Math.PI;
}
/**
* Check if a number is a power of two. Limited to 32-bit integers.
*
* @param {number} a
* @returns {boolean}
*/
export function isPowerOfTwo(a) {
return (a | 0) === a && a > 0 && (a & (a - 1)) === 0;
}
/**
* Returns the next highest power of two.
*
* @param {number} n
* @returns {number}
*/
export function nextPowerOfTwo(n) {
if (n === 0) return 1;
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return n + 1;
}
/**
* Returns the previous power of two.
*
* @param {number} n
* @returns {number}
*/
export function prevPowerOfTwo(n) {
if (n <= 1) return 0;
return nextPowerOfTwo(n) / 2;
}