feat:node-modules

This commit is contained in:
houjunxiang
2025-11-24 10:26:18 +08:00
parent 753766893b
commit 8a3e48d856
8825 changed files with 567399 additions and 1 deletions

View File

@@ -0,0 +1,311 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createDistance = void 0;
var _is = require("../../utils/is.js");
var _factory = require("../../utils/factory.js");
const name = 'distance';
const dependencies = ['typed', 'addScalar', 'subtractScalar', 'divideScalar', 'multiplyScalar', 'deepEqual', 'sqrt', 'abs'];
const createDistance = exports.createDistance = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
let {
typed,
addScalar,
subtractScalar,
multiplyScalar,
divideScalar,
deepEqual,
sqrt,
abs
} = _ref;
/**
* Calculates:
* The eucledian distance between two points in N-dimensional spaces.
* Distance between point and a line in 2 and 3 dimensional spaces.
* Pairwise distance between a set of 2D or 3D points
* NOTE:
* When substituting coefficients of a line(a, b and c), use ax + by + c = 0 instead of ax + by = c
* For parametric equation of a 3D line, x0, y0, z0, a, b, c are from: (xx0, yy0, zz0) = t(a, b, c)
*
* Syntax:
*
* math.distance([x1,y1], [x2,y2])
* math.distance({pointOneX, pointOneY}, {pointTwoX, pointTwoY})
* math.distance([x1,y1,z1], [x2,y2,z2])
* math.distance({pointOneX, pointOneY, pointOneZ}, {pointTwoX, pointTwoY, pointTwoZ})
* math.distance([x1,y1,z1,a1], [x2,y2,z2,a2])
* math.distance([[x1,y1], [x2,y2], [x3,y3]])
* math.distance([[x1,y1,z1], [x2,y2,z2], [x3,y3,z3]])
* math.distance([pointX,pointY], [a,b,c])
* math.distance([pointX,pointY], [lineOnePtX,lineOnePtY], [lineTwoPtX,lineTwoPtY])
* math.distance({pointX, pointY}, {lineOnePtX, lineOnePtY}, {lineTwoPtX, lineTwoPtY})
* math.distance([pointX,pointY,pointZ], [x0, y0, z0, a, b, c])
* math.distance({pointX, pointY, pointZ}, {x0, y0, z0, a, b, c})
*
* Examples:
* math.distance([0,0], [4,4]) // Returns 5.656854249492381
* math.distance(
* {pointOneX: 0, pointOneY: 0},
* {pointTwoX: 10, pointTwoY: 10}) // Returns 14.142135623730951
* math.distance([1, 0, 1], [4, -2, 2]) // Returns 3.7416573867739413
* math.distance(
* {pointOneX: 4, pointOneY: 5, pointOneZ: 8},
* {pointTwoX: 2, pointTwoY: 7, pointTwoZ: 9}) // Returns 3
* math.distance([1, 0, 1, 0], [0, -1, 0, -1]) // Returns 2
* math.distance([[1, 2], [1, 2], [1, 3]]) // Returns [0, 1, 1]
* math.distance([[1,2,4], [1,2,6], [8,1,3]]) // Returns [2, 7.14142842854285, 7.681145747868608]
* math.distance([10, 10], [8, 1, 3]) // Returns 11.535230316796387
* math.distance([0, 0], [3, 0], [0, 4]) // Returns 2.4
* math.distance(
* {pointX: 0, pointY: 0},
* {lineOnePtX: 3, lineOnePtY: 0},
* {lineTwoPtX: 0, lineTwoPtY: 4}) // Returns 2.4
* math.distance([2, 3, 1], [1, 1, 2, 5, 0, 1]) // Returns 2.3204774044612857
* math.distance(
* {pointX: 2, pointY: 3, pointZ: 1},
* {x0: 1, y0: 1, z0: 2, a: 5, b: 0, c: 1}) // Returns 2.3204774044612857
*
* @param {Array | Matrix | Object} x Co-ordinates of first point
* @param {Array | Matrix | Object} y Co-ordinates of second point
* @return {Number | BigNumber} Returns the distance from two/three points
*/
return typed(name, {
'Array, Array, Array': function (x, y, z) {
// Point to Line 2D (x=Point, y=LinePoint1, z=LinePoint2)
if (x.length === 2 && y.length === 2 && z.length === 2) {
if (!_2d(x)) {
throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument');
}
if (!_2d(y)) {
throw new TypeError('Array with 2 numbers or BigNumbers expected for second argument');
}
if (!_2d(z)) {
throw new TypeError('Array with 2 numbers or BigNumbers expected for third argument');
}
if (deepEqual(y, z)) {
throw new TypeError('LinePoint1 should not be same with LinePoint2');
}
const xCoeff = subtractScalar(z[1], y[1]);
const yCoeff = subtractScalar(y[0], z[0]);
const constant = subtractScalar(multiplyScalar(z[0], y[1]), multiplyScalar(y[0], z[1]));
return _distancePointLine2D(x[0], x[1], xCoeff, yCoeff, constant);
} else {
throw new TypeError('Invalid Arguments: Try again');
}
},
'Object, Object, Object': function (x, y, z) {
if (Object.keys(x).length === 2 && Object.keys(y).length === 2 && Object.keys(z).length === 2) {
if (!_2d(x)) {
throw new TypeError('Values of pointX and pointY should be numbers or BigNumbers');
}
if (!_2d(y)) {
throw new TypeError('Values of lineOnePtX and lineOnePtY should be numbers or BigNumbers');
}
if (!_2d(z)) {
throw new TypeError('Values of lineTwoPtX and lineTwoPtY should be numbers or BigNumbers');
}
if (deepEqual(_objectToArray(y), _objectToArray(z))) {
throw new TypeError('LinePoint1 should not be same with LinePoint2');
}
if ('pointX' in x && 'pointY' in x && 'lineOnePtX' in y && 'lineOnePtY' in y && 'lineTwoPtX' in z && 'lineTwoPtY' in z) {
const xCoeff = subtractScalar(z.lineTwoPtY, y.lineOnePtY);
const yCoeff = subtractScalar(y.lineOnePtX, z.lineTwoPtX);
const constant = subtractScalar(multiplyScalar(z.lineTwoPtX, y.lineOnePtY), multiplyScalar(y.lineOnePtX, z.lineTwoPtY));
return _distancePointLine2D(x.pointX, x.pointY, xCoeff, yCoeff, constant);
} else {
throw new TypeError('Key names do not match');
}
} else {
throw new TypeError('Invalid Arguments: Try again');
}
},
'Array, Array': function (x, y) {
// Point to Line 2D (x=[pointX, pointY], y=[x-coeff, y-coeff, const])
if (x.length === 2 && y.length === 3) {
if (!_2d(x)) {
throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument');
}
if (!_3d(y)) {
throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument');
}
return _distancePointLine2D(x[0], x[1], y[0], y[1], y[2]);
} else if (x.length === 3 && y.length === 6) {
// Point to Line 3D
if (!_3d(x)) {
throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument');
}
if (!_parametricLine(y)) {
throw new TypeError('Array with 6 numbers or BigNumbers expected for second argument');
}
return _distancePointLine3D(x[0], x[1], x[2], y[0], y[1], y[2], y[3], y[4], y[5]);
} else if (x.length === y.length && x.length > 0) {
// Point to Point N-dimensions
if (!_containsOnlyNumbers(x)) {
throw new TypeError('All values of an array should be numbers or BigNumbers');
}
if (!_containsOnlyNumbers(y)) {
throw new TypeError('All values of an array should be numbers or BigNumbers');
}
return _euclideanDistance(x, y);
} else {
throw new TypeError('Invalid Arguments: Try again');
}
},
'Object, Object': function (x, y) {
if (Object.keys(x).length === 2 && Object.keys(y).length === 3) {
if (!_2d(x)) {
throw new TypeError('Values of pointX and pointY should be numbers or BigNumbers');
}
if (!_3d(y)) {
throw new TypeError('Values of xCoeffLine, yCoeffLine and constant should be numbers or BigNumbers');
}
if ('pointX' in x && 'pointY' in x && 'xCoeffLine' in y && 'yCoeffLine' in y && 'constant' in y) {
return _distancePointLine2D(x.pointX, x.pointY, y.xCoeffLine, y.yCoeffLine, y.constant);
} else {
throw new TypeError('Key names do not match');
}
} else if (Object.keys(x).length === 3 && Object.keys(y).length === 6) {
// Point to Line 3D
if (!_3d(x)) {
throw new TypeError('Values of pointX, pointY and pointZ should be numbers or BigNumbers');
}
if (!_parametricLine(y)) {
throw new TypeError('Values of x0, y0, z0, a, b and c should be numbers or BigNumbers');
}
if ('pointX' in x && 'pointY' in x && 'x0' in y && 'y0' in y && 'z0' in y && 'a' in y && 'b' in y && 'c' in y) {
return _distancePointLine3D(x.pointX, x.pointY, x.pointZ, y.x0, y.y0, y.z0, y.a, y.b, y.c);
} else {
throw new TypeError('Key names do not match');
}
} else if (Object.keys(x).length === 2 && Object.keys(y).length === 2) {
// Point to Point 2D
if (!_2d(x)) {
throw new TypeError('Values of pointOneX and pointOneY should be numbers or BigNumbers');
}
if (!_2d(y)) {
throw new TypeError('Values of pointTwoX and pointTwoY should be numbers or BigNumbers');
}
if ('pointOneX' in x && 'pointOneY' in x && 'pointTwoX' in y && 'pointTwoY' in y) {
return _euclideanDistance([x.pointOneX, x.pointOneY], [y.pointTwoX, y.pointTwoY]);
} else {
throw new TypeError('Key names do not match');
}
} else if (Object.keys(x).length === 3 && Object.keys(y).length === 3) {
// Point to Point 3D
if (!_3d(x)) {
throw new TypeError('Values of pointOneX, pointOneY and pointOneZ should be numbers or BigNumbers');
}
if (!_3d(y)) {
throw new TypeError('Values of pointTwoX, pointTwoY and pointTwoZ should be numbers or BigNumbers');
}
if ('pointOneX' in x && 'pointOneY' in x && 'pointOneZ' in x && 'pointTwoX' in y && 'pointTwoY' in y && 'pointTwoZ' in y) {
return _euclideanDistance([x.pointOneX, x.pointOneY, x.pointOneZ], [y.pointTwoX, y.pointTwoY, y.pointTwoZ]);
} else {
throw new TypeError('Key names do not match');
}
} else {
throw new TypeError('Invalid Arguments: Try again');
}
},
Array: function (arr) {
if (!_pairwise(arr)) {
throw new TypeError('Incorrect array format entered for pairwise distance calculation');
}
return _distancePairwise(arr);
}
});
function _isNumber(a) {
// distance supports numbers and bignumbers
return typeof a === 'number' || (0, _is.isBigNumber)(a);
}
function _2d(a) {
// checks if the number of arguments are correct in count and are valid (should be numbers)
if (a.constructor !== Array) {
a = _objectToArray(a);
}
return _isNumber(a[0]) && _isNumber(a[1]);
}
function _3d(a) {
// checks if the number of arguments are correct in count and are valid (should be numbers)
if (a.constructor !== Array) {
a = _objectToArray(a);
}
return _isNumber(a[0]) && _isNumber(a[1]) && _isNumber(a[2]);
}
function _containsOnlyNumbers(a) {
// checks if the number of arguments are correct in count and are valid (should be numbers)
if (!Array.isArray(a)) {
a = _objectToArray(a);
}
return a.every(_isNumber);
}
function _parametricLine(a) {
if (a.constructor !== Array) {
a = _objectToArray(a);
}
return _isNumber(a[0]) && _isNumber(a[1]) && _isNumber(a[2]) && _isNumber(a[3]) && _isNumber(a[4]) && _isNumber(a[5]);
}
function _objectToArray(o) {
const keys = Object.keys(o);
const a = [];
for (let i = 0; i < keys.length; i++) {
a.push(o[keys[i]]);
}
return a;
}
function _pairwise(a) {
// checks for valid arguments passed to _distancePairwise(Array)
if (a[0].length === 2 && _isNumber(a[0][0]) && _isNumber(a[0][1])) {
if (a.some(aI => aI.length !== 2 || !_isNumber(aI[0]) || !_isNumber(aI[1]))) {
return false;
}
} else if (a[0].length === 3 && _isNumber(a[0][0]) && _isNumber(a[0][1]) && _isNumber(a[0][2])) {
if (a.some(aI => aI.length !== 3 || !_isNumber(aI[0]) || !_isNumber(aI[1]) || !_isNumber(aI[2]))) {
return false;
}
} else {
return false;
}
return true;
}
function _distancePointLine2D(x, y, a, b, c) {
const num = abs(addScalar(addScalar(multiplyScalar(a, x), multiplyScalar(b, y)), c));
const den = sqrt(addScalar(multiplyScalar(a, a), multiplyScalar(b, b)));
return divideScalar(num, den);
}
function _distancePointLine3D(x, y, z, x0, y0, z0, a, b, c) {
let num = [subtractScalar(multiplyScalar(subtractScalar(y0, y), c), multiplyScalar(subtractScalar(z0, z), b)), subtractScalar(multiplyScalar(subtractScalar(z0, z), a), multiplyScalar(subtractScalar(x0, x), c)), subtractScalar(multiplyScalar(subtractScalar(x0, x), b), multiplyScalar(subtractScalar(y0, y), a))];
num = sqrt(addScalar(addScalar(multiplyScalar(num[0], num[0]), multiplyScalar(num[1], num[1])), multiplyScalar(num[2], num[2])));
const den = sqrt(addScalar(addScalar(multiplyScalar(a, a), multiplyScalar(b, b)), multiplyScalar(c, c)));
return divideScalar(num, den);
}
function _euclideanDistance(x, y) {
const vectorSize = x.length;
let result = 0;
let diff = 0;
for (let i = 0; i < vectorSize; i++) {
diff = subtractScalar(x[i], y[i]);
result = addScalar(multiplyScalar(diff, diff), result);
}
return sqrt(result);
}
function _distancePairwise(a) {
const result = [];
let pointA = [];
let pointB = [];
for (let i = 0; i < a.length - 1; i++) {
for (let j = i + 1; j < a.length; j++) {
if (a[0].length === 2) {
pointA = [a[i][0], a[i][1]];
pointB = [a[j][0], a[j][1]];
} else if (a[0].length === 3) {
pointA = [a[i][0], a[i][1], a[i][2]];
pointB = [a[j][0], a[j][1], a[j][2]];
}
result.push(_euclideanDistance(pointA, pointB));
}
}
return result;
}
});

View File

@@ -0,0 +1,206 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createIntersect = void 0;
var _factory = require("../../utils/factory.js");
const name = 'intersect';
const dependencies = ['typed', 'config', 'abs', 'add', 'addScalar', 'matrix', 'multiply', 'multiplyScalar', 'divideScalar', 'subtract', 'smaller', 'equalScalar', 'flatten', 'isZero', 'isNumeric'];
const createIntersect = exports.createIntersect = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
let {
typed,
config,
abs,
add,
addScalar,
matrix,
multiply,
multiplyScalar,
divideScalar,
subtract,
smaller,
equalScalar,
flatten,
isZero,
isNumeric
} = _ref;
/**
* Calculates the point of intersection of two lines in two or three dimensions
* and of a line and a plane in three dimensions. The inputs are in the form of
* arrays or 1 dimensional matrices. The line intersection functions return null
* if the lines do not meet.
*
* Note: Fill the plane coefficients as `x + y + z = c` and not as `x + y + z + c = 0`.
*
* Syntax:
*
* math.intersect(endPoint1Line1, endPoint2Line1, endPoint1Line2, endPoint2Line2)
* math.intersect(endPoint1, endPoint2, planeCoefficients)
*
* Examples:
*
* math.intersect([0, 0], [10, 10], [10, 0], [0, 10]) // Returns [5, 5]
* math.intersect([0, 0, 0], [10, 10, 0], [10, 0, 0], [0, 10, 0]) // Returns [5, 5, 0]
* math.intersect([1, 0, 1], [4, -2, 2], [1, 1, 1, 6]) // Returns [7, -4, 3]
*
* @param {Array | Matrix} w Co-ordinates of first end-point of first line
* @param {Array | Matrix} x Co-ordinates of second end-point of first line
* @param {Array | Matrix} y Co-ordinates of first end-point of second line
* OR Co-efficients of the plane's equation
* @param {Array | Matrix} z Co-ordinates of second end-point of second line
* OR undefined if the calculation is for line and plane
* @return {Array} Returns the point of intersection of lines/lines-planes
*/
return typed('intersect', {
'Array, Array, Array': _AAA,
'Array, Array, Array, Array': _AAAA,
'Matrix, Matrix, Matrix': function (x, y, plane) {
const arr = _AAA(x.valueOf(), y.valueOf(), plane.valueOf());
return arr === null ? null : matrix(arr);
},
'Matrix, Matrix, Matrix, Matrix': function (w, x, y, z) {
// TODO: output matrix type should match input matrix type
const arr = _AAAA(w.valueOf(), x.valueOf(), y.valueOf(), z.valueOf());
return arr === null ? null : matrix(arr);
}
});
function _AAA(x, y, plane) {
x = _coerceArr(x);
y = _coerceArr(y);
plane = _coerceArr(plane);
if (!_3d(x)) {
throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument');
}
if (!_3d(y)) {
throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument');
}
if (!_4d(plane)) {
throw new TypeError('Array with 4 numbers expected as third argument');
}
return _intersectLinePlane(x[0], x[1], x[2], y[0], y[1], y[2], plane[0], plane[1], plane[2], plane[3]);
}
function _AAAA(w, x, y, z) {
w = _coerceArr(w);
x = _coerceArr(x);
y = _coerceArr(y);
z = _coerceArr(z);
if (w.length === 2) {
if (!_2d(w)) {
throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument');
}
if (!_2d(x)) {
throw new TypeError('Array with 2 numbers or BigNumbers expected for second argument');
}
if (!_2d(y)) {
throw new TypeError('Array with 2 numbers or BigNumbers expected for third argument');
}
if (!_2d(z)) {
throw new TypeError('Array with 2 numbers or BigNumbers expected for fourth argument');
}
return _intersect2d(w, x, y, z);
} else if (w.length === 3) {
if (!_3d(w)) {
throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument');
}
if (!_3d(x)) {
throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument');
}
if (!_3d(y)) {
throw new TypeError('Array with 3 numbers or BigNumbers expected for third argument');
}
if (!_3d(z)) {
throw new TypeError('Array with 3 numbers or BigNumbers expected for fourth argument');
}
return _intersect3d(w[0], w[1], w[2], x[0], x[1], x[2], y[0], y[1], y[2], z[0], z[1], z[2]);
} else {
throw new TypeError('Arrays with two or thee dimensional points expected');
}
}
/** Coerce row and column 2-dim arrays to 1-dim array */
function _coerceArr(arr) {
// row matrix
if (arr.length === 1) return arr[0];
// column matrix
if (arr.length > 1 && Array.isArray(arr[0])) {
if (arr.every(el => Array.isArray(el) && el.length === 1)) return flatten(arr);
}
return arr;
}
function _2d(x) {
return x.length === 2 && isNumeric(x[0]) && isNumeric(x[1]);
}
function _3d(x) {
return x.length === 3 && isNumeric(x[0]) && isNumeric(x[1]) && isNumeric(x[2]);
}
function _4d(x) {
return x.length === 4 && isNumeric(x[0]) && isNumeric(x[1]) && isNumeric(x[2]) && isNumeric(x[3]);
}
function _intersect2d(p1a, p1b, p2a, p2b) {
const o1 = p1a;
const o2 = p2a;
const d1 = subtract(o1, p1b);
const d2 = subtract(o2, p2b);
const det = subtract(multiplyScalar(d1[0], d2[1]), multiplyScalar(d2[0], d1[1]));
if (isZero(det)) return null;
if (smaller(abs(det), config.relTol)) {
return null;
}
const d20o11 = multiplyScalar(d2[0], o1[1]);
const d21o10 = multiplyScalar(d2[1], o1[0]);
const d20o21 = multiplyScalar(d2[0], o2[1]);
const d21o20 = multiplyScalar(d2[1], o2[0]);
const t = divideScalar(addScalar(subtract(subtract(d20o11, d21o10), d20o21), d21o20), det);
return add(multiply(d1, t), o1);
}
function _intersect3dHelper(a, b, c, d, e, f, g, h, i, j, k, l) {
// (a - b)*(c - d) + (e - f)*(g - h) + (i - j)*(k - l)
const add1 = multiplyScalar(subtract(a, b), subtract(c, d));
const add2 = multiplyScalar(subtract(e, f), subtract(g, h));
const add3 = multiplyScalar(subtract(i, j), subtract(k, l));
return addScalar(addScalar(add1, add2), add3);
}
function _intersect3d(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) {
const d1343 = _intersect3dHelper(x1, x3, x4, x3, y1, y3, y4, y3, z1, z3, z4, z3);
const d4321 = _intersect3dHelper(x4, x3, x2, x1, y4, y3, y2, y1, z4, z3, z2, z1);
const d1321 = _intersect3dHelper(x1, x3, x2, x1, y1, y3, y2, y1, z1, z3, z2, z1);
const d4343 = _intersect3dHelper(x4, x3, x4, x3, y4, y3, y4, y3, z4, z3, z4, z3);
const d2121 = _intersect3dHelper(x2, x1, x2, x1, y2, y1, y2, y1, z2, z1, z2, z1);
const numerator = subtract(multiplyScalar(d1343, d4321), multiplyScalar(d1321, d4343));
const denominator = subtract(multiplyScalar(d2121, d4343), multiplyScalar(d4321, d4321));
if (isZero(denominator)) return null;
const ta = divideScalar(numerator, denominator);
const tb = divideScalar(addScalar(d1343, multiplyScalar(ta, d4321)), d4343);
const pax = addScalar(x1, multiplyScalar(ta, subtract(x2, x1)));
const pay = addScalar(y1, multiplyScalar(ta, subtract(y2, y1)));
const paz = addScalar(z1, multiplyScalar(ta, subtract(z2, z1)));
const pbx = addScalar(x3, multiplyScalar(tb, subtract(x4, x3)));
const pby = addScalar(y3, multiplyScalar(tb, subtract(y4, y3)));
const pbz = addScalar(z3, multiplyScalar(tb, subtract(z4, z3)));
if (equalScalar(pax, pbx) && equalScalar(pay, pby) && equalScalar(paz, pbz)) {
return [pax, pay, paz];
} else {
return null;
}
}
function _intersectLinePlane(x1, y1, z1, x2, y2, z2, x, y, z, c) {
const x1x = multiplyScalar(x1, x);
const x2x = multiplyScalar(x2, x);
const y1y = multiplyScalar(y1, y);
const y2y = multiplyScalar(y2, y);
const z1z = multiplyScalar(z1, z);
const z2z = multiplyScalar(z2, z);
const numerator = subtract(subtract(subtract(c, x1x), y1y), z1z);
const denominator = subtract(subtract(subtract(addScalar(addScalar(x2x, y2y), z2z), x1x), y1y), z1z);
const t = divideScalar(numerator, denominator);
const px = addScalar(x1, multiplyScalar(t, subtract(x2, x1)));
const py = addScalar(y1, multiplyScalar(t, subtract(y2, y1)));
const pz = addScalar(z1, multiplyScalar(t, subtract(z2, z1)));
return [px, py, pz];
// TODO: Add cases when line is parallel to the plane:
// (a) no intersection,
// (b) line contained in plane
}
});