114 lines
3.2 KiB
JavaScript
114 lines
3.2 KiB
JavaScript
/*
|
|
* Planck.js
|
|
* The MIT License
|
|
* Copyright (c) 2021 Erin Catto, Ali Shakiba
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
var _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
|
|
var _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
|
|
|
|
var common = require('../util/common');
|
|
var native = Math;
|
|
var math = module.exports = Object.create(native);
|
|
|
|
math.EPSILON = 1e-9; // TODO
|
|
|
|
/**
|
|
* This function is used to ensure that a floating point number is not a NaN or
|
|
* infinity.
|
|
*/
|
|
math.isFinite = function(x) {
|
|
return (typeof x === 'number') && isFinite(x) && !isNaN(x);
|
|
}
|
|
|
|
math.assert = function(x) {
|
|
if (!_ASSERT) return;
|
|
if (!math.isFinite(x)) {
|
|
_DEBUG && common.debug(x);
|
|
throw new Error('Invalid Number!');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* TODO: This is a approximate yet fast inverse square-root.
|
|
*/
|
|
math.invSqrt = function(x) {
|
|
// TODO
|
|
return 1 / native.sqrt(x);
|
|
}
|
|
|
|
/**
|
|
* Next Largest Power of 2 Given a binary integer value x, the next largest
|
|
* power of 2 can be computed by a SWAR algorithm that recursively "folds" the
|
|
* upper bits into the lower bits. This process yields a bit vector with the
|
|
* same most significant 1 as x, but all 1's below it. Adding 1 to that value
|
|
* yields the next largest power of 2. For a 32-bit value:
|
|
*/
|
|
math.nextPowerOfTwo = function(x) {
|
|
// TODO
|
|
x |= (x >> 1);
|
|
x |= (x >> 2);
|
|
x |= (x >> 4);
|
|
x |= (x >> 8);
|
|
x |= (x >> 16);
|
|
return x + 1;
|
|
}
|
|
|
|
math.isPowerOfTwo = function(x) {
|
|
return x > 0 && (x & (x - 1)) == 0;
|
|
}
|
|
|
|
math.mod = function(num, min, max) {
|
|
if (typeof min === 'undefined') {
|
|
max = 1, min = 0;
|
|
} else if (typeof max === 'undefined') {
|
|
max = min, min = 0;
|
|
}
|
|
if (max > min) {
|
|
num = (num - min) % (max - min);
|
|
return num + (num < 0 ? max : min);
|
|
} else {
|
|
num = (num - max) % (min - max);
|
|
return num + (num <= 0 ? min : max);
|
|
}
|
|
};
|
|
|
|
math.clamp = function(num, min, max) {
|
|
if (num < min) {
|
|
return min;
|
|
} else if (num > max) {
|
|
return max;
|
|
} else {
|
|
return num;
|
|
}
|
|
};
|
|
|
|
math.random = function(min, max) {
|
|
if (typeof min === 'undefined') {
|
|
max = 1;
|
|
min = 0;
|
|
} else if (typeof max === 'undefined') {
|
|
max = min;
|
|
min = 0;
|
|
}
|
|
return min == max ? min : native.random() * (max - min) + min;
|
|
};
|