distance.js 14 KB

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