distance.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createDistance = void 0;
  6. var _is = require("../../utils/is.js");
  7. var _factory = require("../../utils/factory.js");
  8. var name = 'distance';
  9. var dependencies = ['typed', 'addScalar', 'subtract', 'divideScalar', 'multiplyScalar', 'unaryMinus', 'sqrt', 'abs'];
  10. var createDistance = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  11. var typed = _ref.typed,
  12. addScalar = _ref.addScalar,
  13. subtract = _ref.subtract,
  14. multiplyScalar = _ref.multiplyScalar,
  15. divideScalar = _ref.divideScalar,
  16. unaryMinus = _ref.unaryMinus,
  17. sqrt = _ref.sqrt,
  18. abs = _ref.abs;
  19. /**
  20. * Calculates:
  21. * The eucledian distance between two points in N-dimensional spaces.
  22. * Distance between point and a line in 2 and 3 dimensional spaces.
  23. * Pairwise distance between a set of 2D or 3D points
  24. * NOTE:
  25. * When substituting coefficients of a line(a, b and c), use ax + by + c = 0 instead of ax + by = c
  26. * For parametric equation of a 3D line, x0, y0, z0, a, b, c are from: (x−x0, y−y0, z−z0) = t(a, b, c)
  27. *
  28. * Syntax:
  29. * math.distance([x1, y1], [x2, y2])
  30. *- math.distance({pointOneX: 4, pointOneY: 5}, {pointTwoX: 2, pointTwoY: 7})
  31. * math.distance([x1, y1, z1], [x2, y2, z2])
  32. * math.distance({pointOneX: 4, pointOneY: 5, pointOneZ: 8}, {pointTwoX: 2, pointTwoY: 7, pointTwoZ: 9})
  33. * math.distance([x1, y1, ... , N1], [x2, y2, ... , N2])
  34. * math.distance([[A], [B], [C]...])
  35. * math.distance([x1, y1], [LinePtX1, LinePtY1], [LinePtX2, LinePtY2])
  36. * math.distance({pointX: 1, pointY: 4}, {lineOnePtX: 6, lineOnePtY: 3}, {lineTwoPtX: 2, lineTwoPtY: 8})
  37. * math.distance([x1, y1, z1], [LinePtX1, LinePtY1, LinePtZ1], [LinePtX2, LinePtY2, LinePtZ2])
  38. * math.distance({pointX: 1, pointY: 4, pointZ: 7}, {lineOnePtX: 6, lineOnePtY: 3, lineOnePtZ: 4}, {lineTwoPtX: 2, lineTwoPtY: 8, lineTwoPtZ: 5})
  39. * math.distance([x1, y1], [xCoeffLine, yCoeffLine, constant])
  40. * math.distance({pointX: 10, pointY: 10}, {xCoeffLine: 8, yCoeffLine: 1, constant: 3})
  41. * math.distance([x1, y1, z1], [x0, y0, z0, a-tCoeff, b-tCoeff, c-tCoeff]) point and parametric equation of 3D line
  42. * math.distance([x, y, z], [x0, y0, z0, a, b, c])
  43. * math.distance({pointX: 2, pointY: 5, pointZ: 9}, {x0: 4, y0: 6, z0: 3, a: 4, b: 2, c: 0})
  44. *
  45. * Examples:
  46. * math.distance([0,0], [4,4]) // Returns 5.656854249492381
  47. * math.distance(
  48. * {pointOneX: 0, pointOneY: 0},
  49. * {pointTwoX: 10, pointTwoY: 10}) // Returns 14.142135623730951
  50. * math.distance([1, 0, 1], [4, -2, 2]) // Returns 3.7416573867739413
  51. * math.distance(
  52. * {pointOneX: 4, pointOneY: 5, pointOneZ: 8},
  53. * {pointTwoX: 2, pointTwoY: 7, pointTwoZ: 9}) // Returns 3
  54. * math.distance([1, 0, 1, 0], [0, -1, 0, -1]) // Returns 2
  55. * math.distance([[1, 2], [1, 2], [1, 3]]) // Returns [0, 1, 1]
  56. * math.distance([[1,2,4], [1,2,6], [8,1,3]]) // Returns [2, 7.14142842854285, 7.681145747868608]
  57. * math.distance([10, 10], [8, 1, 3]) // Returns 11.535230316796387
  58. * math.distance([10, 10], [2, 3], [-8, 0]) // Returns 8.759953130362847
  59. * math.distance(
  60. * {pointX: 1, pointY: 4},
  61. * {lineOnePtX: 6, lineOnePtY: 3},
  62. * {lineTwoPtX: 2, lineTwoPtY: 8}) // Returns 2.720549372624744
  63. * math.distance([2, 3, 1], [1, 1, 2, 5, 0, 1]) // Returns 2.3204774044612857
  64. * math.distance(
  65. * {pointX: 2, pointY: 3, pointZ: 1},
  66. * {x0: 1, y0: 1, z0: 2, a: 5, b: 0, c: 1} // Returns 2.3204774044612857
  67. *
  68. * @param {Array | Matrix | Object} x Co-ordinates of first point
  69. * @param {Array | Matrix | Object} y Co-ordinates of second point
  70. * @return {Number | BigNumber} Returns the distance from two/three points
  71. */
  72. return typed(name, {
  73. 'Array, Array, Array': function ArrayArrayArray(x, y, z) {
  74. // Point to Line 2D (x=Point, y=LinePoint1, z=LinePoint2)
  75. if (x.length === 2 && y.length === 2 && z.length === 2) {
  76. if (!_2d(x)) {
  77. throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument');
  78. }
  79. if (!_2d(y)) {
  80. throw new TypeError('Array with 2 numbers or BigNumbers expected for second argument');
  81. }
  82. if (!_2d(z)) {
  83. throw new TypeError('Array with 2 numbers or BigNumbers expected for third argument');
  84. }
  85. var m = divideScalar(subtract(z[1], z[0]), subtract(y[1], y[0]));
  86. var xCoeff = multiplyScalar(multiplyScalar(m, m), y[0]);
  87. var yCoeff = unaryMinus(multiplyScalar(m, y[0]));
  88. var constant = x[1];
  89. return _distancePointLine2D(x[0], x[1], xCoeff, yCoeff, constant);
  90. } else {
  91. throw new TypeError('Invalid Arguments: Try again');
  92. }
  93. },
  94. 'Object, Object, Object': function ObjectObjectObject(x, y, z) {
  95. if (Object.keys(x).length === 2 && Object.keys(y).length === 2 && Object.keys(z).length === 2) {
  96. if (!_2d(x)) {
  97. throw new TypeError('Values of pointX and pointY should be numbers or BigNumbers');
  98. }
  99. if (!_2d(y)) {
  100. throw new TypeError('Values of lineOnePtX and lineOnePtY should be numbers or BigNumbers');
  101. }
  102. if (!_2d(z)) {
  103. throw new TypeError('Values of lineTwoPtX and lineTwoPtY should be numbers or BigNumbers');
  104. }
  105. if ('pointX' in x && 'pointY' in x && 'lineOnePtX' in y && 'lineOnePtY' in y && 'lineTwoPtX' in z && 'lineTwoPtY' in z) {
  106. var m = divideScalar(subtract(z.lineTwoPtY, z.lineTwoPtX), subtract(y.lineOnePtY, y.lineOnePtX));
  107. var xCoeff = multiplyScalar(multiplyScalar(m, m), y.lineOnePtX);
  108. var yCoeff = unaryMinus(multiplyScalar(m, y.lineOnePtX));
  109. var constant = x.pointX;
  110. return _distancePointLine2D(x.pointX, x.pointY, xCoeff, yCoeff, constant);
  111. } else {
  112. throw new TypeError('Key names do not match');
  113. }
  114. } else {
  115. throw new TypeError('Invalid Arguments: Try again');
  116. }
  117. },
  118. 'Array, Array': function ArrayArray(x, y) {
  119. // Point to Line 2D (x=[pointX, pointY], y=[x-coeff, y-coeff, const])
  120. if (x.length === 2 && y.length === 3) {
  121. if (!_2d(x)) {
  122. throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument');
  123. }
  124. if (!_3d(y)) {
  125. throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument');
  126. }
  127. return _distancePointLine2D(x[0], x[1], y[0], y[1], y[2]);
  128. } else if (x.length === 3 && y.length === 6) {
  129. // Point to Line 3D
  130. if (!_3d(x)) {
  131. throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument');
  132. }
  133. if (!_parametricLine(y)) {
  134. throw new TypeError('Array with 6 numbers or BigNumbers expected for second argument');
  135. }
  136. return _distancePointLine3D(x[0], x[1], x[2], y[0], y[1], y[2], y[3], y[4], y[5]);
  137. } else if (x.length === y.length && x.length > 0) {
  138. // Point to Point N-dimensions
  139. if (!_containsOnlyNumbers(x)) {
  140. throw new TypeError('All values of an array should be numbers or BigNumbers');
  141. }
  142. if (!_containsOnlyNumbers(y)) {
  143. throw new TypeError('All values of an array should be numbers or BigNumbers');
  144. }
  145. return _euclideanDistance(x, y);
  146. } else {
  147. throw new TypeError('Invalid Arguments: Try again');
  148. }
  149. },
  150. 'Object, Object': function ObjectObject(x, y) {
  151. if (Object.keys(x).length === 2 && Object.keys(y).length === 3) {
  152. if (!_2d(x)) {
  153. throw new TypeError('Values of pointX and pointY should be numbers or BigNumbers');
  154. }
  155. if (!_3d(y)) {
  156. throw new TypeError('Values of xCoeffLine, yCoeffLine and constant should be numbers or BigNumbers');
  157. }
  158. if ('pointX' in x && 'pointY' in x && 'xCoeffLine' in y && 'yCoeffLine' in y && 'constant' in y) {
  159. return _distancePointLine2D(x.pointX, x.pointY, y.xCoeffLine, y.yCoeffLine, y.constant);
  160. } else {
  161. throw new TypeError('Key names do not match');
  162. }
  163. } else if (Object.keys(x).length === 3 && Object.keys(y).length === 6) {
  164. // Point to Line 3D
  165. if (!_3d(x)) {
  166. throw new TypeError('Values of pointX, pointY and pointZ should be numbers or BigNumbers');
  167. }
  168. if (!_parametricLine(y)) {
  169. throw new TypeError('Values of x0, y0, z0, a, b and c should be numbers or BigNumbers');
  170. }
  171. 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) {
  172. return _distancePointLine3D(x.pointX, x.pointY, x.pointZ, y.x0, y.y0, y.z0, y.a, y.b, y.c);
  173. } else {
  174. throw new TypeError('Key names do not match');
  175. }
  176. } else if (Object.keys(x).length === 2 && Object.keys(y).length === 2) {
  177. // Point to Point 2D
  178. if (!_2d(x)) {
  179. throw new TypeError('Values of pointOneX and pointOneY should be numbers or BigNumbers');
  180. }
  181. if (!_2d(y)) {
  182. throw new TypeError('Values of pointTwoX and pointTwoY should be numbers or BigNumbers');
  183. }
  184. if ('pointOneX' in x && 'pointOneY' in x && 'pointTwoX' in y && 'pointTwoY' in y) {
  185. return _euclideanDistance([x.pointOneX, x.pointOneY], [y.pointTwoX, y.pointTwoY]);
  186. } else {
  187. throw new TypeError('Key names do not match');
  188. }
  189. } else if (Object.keys(x).length === 3 && Object.keys(y).length === 3) {
  190. // Point to Point 3D
  191. if (!_3d(x)) {
  192. throw new TypeError('Values of pointOneX, pointOneY and pointOneZ should be numbers or BigNumbers');
  193. }
  194. if (!_3d(y)) {
  195. throw new TypeError('Values of pointTwoX, pointTwoY and pointTwoZ should be numbers or BigNumbers');
  196. }
  197. if ('pointOneX' in x && 'pointOneY' in x && 'pointOneZ' in x && 'pointTwoX' in y && 'pointTwoY' in y && 'pointTwoZ' in y) {
  198. return _euclideanDistance([x.pointOneX, x.pointOneY, x.pointOneZ], [y.pointTwoX, y.pointTwoY, y.pointTwoZ]);
  199. } else {
  200. throw new TypeError('Key names do not match');
  201. }
  202. } else {
  203. throw new TypeError('Invalid Arguments: Try again');
  204. }
  205. },
  206. Array: function Array(arr) {
  207. if (!_pairwise(arr)) {
  208. throw new TypeError('Incorrect array format entered for pairwise distance calculation');
  209. }
  210. return _distancePairwise(arr);
  211. }
  212. });
  213. function _isNumber(a) {
  214. // distance supports numbers and bignumbers
  215. return typeof a === 'number' || (0, _is.isBigNumber)(a);
  216. }
  217. function _2d(a) {
  218. // checks if the number of arguments are correct in count and are valid (should be numbers)
  219. if (a.constructor !== Array) {
  220. a = _objectToArray(a);
  221. }
  222. return _isNumber(a[0]) && _isNumber(a[1]);
  223. }
  224. function _3d(a) {
  225. // checks if the number of arguments are correct in count and are valid (should be numbers)
  226. if (a.constructor !== Array) {
  227. a = _objectToArray(a);
  228. }
  229. return _isNumber(a[0]) && _isNumber(a[1]) && _isNumber(a[2]);
  230. }
  231. function _containsOnlyNumbers(a) {
  232. // checks if the number of arguments are correct in count and are valid (should be numbers)
  233. if (!Array.isArray(a)) {
  234. a = _objectToArray(a);
  235. }
  236. return a.every(_isNumber);
  237. }
  238. function _parametricLine(a) {
  239. if (a.constructor !== Array) {
  240. a = _objectToArray(a);
  241. }
  242. return _isNumber(a[0]) && _isNumber(a[1]) && _isNumber(a[2]) && _isNumber(a[3]) && _isNumber(a[4]) && _isNumber(a[5]);
  243. }
  244. function _objectToArray(o) {
  245. var keys = Object.keys(o);
  246. var a = [];
  247. for (var i = 0; i < keys.length; i++) {
  248. a.push(o[keys[i]]);
  249. }
  250. return a;
  251. }
  252. function _pairwise(a) {
  253. // checks for valid arguments passed to _distancePairwise(Array)
  254. if (a[0].length === 2 && _isNumber(a[0][0]) && _isNumber(a[0][1])) {
  255. if (a.some(function (aI) {
  256. return aI.length !== 2 || !_isNumber(aI[0]) || !_isNumber(aI[1]);
  257. })) {
  258. return false;
  259. }
  260. } else if (a[0].length === 3 && _isNumber(a[0][0]) && _isNumber(a[0][1]) && _isNumber(a[0][2])) {
  261. if (a.some(function (aI) {
  262. return aI.length !== 3 || !_isNumber(aI[0]) || !_isNumber(aI[1]) || !_isNumber(aI[2]);
  263. })) {
  264. return false;
  265. }
  266. } else {
  267. return false;
  268. }
  269. return true;
  270. }
  271. function _distancePointLine2D(x, y, a, b, c) {
  272. var num = abs(addScalar(addScalar(multiplyScalar(a, x), multiplyScalar(b, y)), c));
  273. var den = sqrt(addScalar(multiplyScalar(a, a), multiplyScalar(b, b)));
  274. return divideScalar(num, den);
  275. }
  276. function _distancePointLine3D(x, y, z, x0, y0, z0, a, b, c) {
  277. var num = [subtract(multiplyScalar(subtract(y0, y), c), multiplyScalar(subtract(z0, z), b)), subtract(multiplyScalar(subtract(z0, z), a), multiplyScalar(subtract(x0, x), c)), subtract(multiplyScalar(subtract(x0, x), b), multiplyScalar(subtract(y0, y), a))];
  278. num = sqrt(addScalar(addScalar(multiplyScalar(num[0], num[0]), multiplyScalar(num[1], num[1])), multiplyScalar(num[2], num[2])));
  279. var den = sqrt(addScalar(addScalar(multiplyScalar(a, a), multiplyScalar(b, b)), multiplyScalar(c, c)));
  280. return divideScalar(num, den);
  281. }
  282. function _euclideanDistance(x, y) {
  283. var vectorSize = x.length;
  284. var result = 0;
  285. var diff = 0;
  286. for (var i = 0; i < vectorSize; i++) {
  287. diff = subtract(x[i], y[i]);
  288. result = addScalar(multiplyScalar(diff, diff), result);
  289. }
  290. return sqrt(result);
  291. }
  292. function _distancePairwise(a) {
  293. var result = [];
  294. var pointA = [];
  295. var pointB = [];
  296. for (var i = 0; i < a.length - 1; i++) {
  297. for (var j = i + 1; j < a.length; j++) {
  298. if (a[0].length === 2) {
  299. pointA = [a[i][0], a[i][1]];
  300. pointB = [a[j][0], a[j][1]];
  301. } else if (a[0].length === 3) {
  302. pointA = [a[i][0], a[i][1], a[i][2]];
  303. pointB = [a[j][0], a[j][1], a[j][2]];
  304. }
  305. result.push(_euclideanDistance(pointA, pointB));
  306. }
  307. }
  308. return result;
  309. }
  310. });
  311. exports.createDistance = createDistance;