intersect.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createIntersect = void 0;
  6. var _factory = require("../../utils/factory.js");
  7. var name = 'intersect';
  8. var dependencies = ['typed', 'config', 'abs', 'add', 'addScalar', 'matrix', 'multiply', 'multiplyScalar', 'divideScalar', 'subtract', 'smaller', 'equalScalar', 'flatten', 'isZero', 'isNumeric'];
  9. var createIntersect = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  10. var typed = _ref.typed,
  11. config = _ref.config,
  12. abs = _ref.abs,
  13. add = _ref.add,
  14. addScalar = _ref.addScalar,
  15. matrix = _ref.matrix,
  16. multiply = _ref.multiply,
  17. multiplyScalar = _ref.multiplyScalar,
  18. divideScalar = _ref.divideScalar,
  19. subtract = _ref.subtract,
  20. smaller = _ref.smaller,
  21. equalScalar = _ref.equalScalar,
  22. flatten = _ref.flatten,
  23. isZero = _ref.isZero,
  24. isNumeric = _ref.isNumeric;
  25. /**
  26. * Calculates the point of intersection of two lines in two or three dimensions
  27. * and of a line and a plane in three dimensions. The inputs are in the form of
  28. * arrays or 1 dimensional matrices. The line intersection functions return null
  29. * if the lines do not meet.
  30. *
  31. * Note: Fill the plane coefficients as `x + y + z = c` and not as `x + y + z + c = 0`.
  32. *
  33. * Syntax:
  34. *
  35. * math.intersect(endPoint1Line1, endPoint2Line1, endPoint1Line2, endPoint2Line2)
  36. * math.intersect(endPoint1, endPoint2, planeCoefficients)
  37. *
  38. * Examples:
  39. *
  40. * math.intersect([0, 0], [10, 10], [10, 0], [0, 10]) // Returns [5, 5]
  41. * math.intersect([0, 0, 0], [10, 10, 0], [10, 0, 0], [0, 10, 0]) // Returns [5, 5, 0]
  42. * math.intersect([1, 0, 1], [4, -2, 2], [1, 1, 1, 6]) // Returns [7, -4, 3]
  43. *
  44. * @param {Array | Matrix} w Co-ordinates of first end-point of first line
  45. * @param {Array | Matrix} x Co-ordinates of second end-point of first line
  46. * @param {Array | Matrix} y Co-ordinates of first end-point of second line
  47. * OR Co-efficients of the plane's equation
  48. * @param {Array | Matrix} z Co-ordinates of second end-point of second line
  49. * OR undefined if the calculation is for line and plane
  50. * @return {Array} Returns the point of intersection of lines/lines-planes
  51. */
  52. return typed('intersect', {
  53. 'Array, Array, Array': _AAA,
  54. 'Array, Array, Array, Array': _AAAA,
  55. 'Matrix, Matrix, Matrix': function MatrixMatrixMatrix(x, y, plane) {
  56. var arr = _AAA(x.valueOf(), y.valueOf(), plane.valueOf());
  57. return arr === null ? null : matrix(arr);
  58. },
  59. 'Matrix, Matrix, Matrix, Matrix': function MatrixMatrixMatrixMatrix(w, x, y, z) {
  60. // TODO: output matrix type should match input matrix type
  61. var arr = _AAAA(w.valueOf(), x.valueOf(), y.valueOf(), z.valueOf());
  62. return arr === null ? null : matrix(arr);
  63. }
  64. });
  65. function _AAA(x, y, plane) {
  66. x = _coerceArr(x);
  67. y = _coerceArr(y);
  68. plane = _coerceArr(plane);
  69. if (!_3d(x)) {
  70. throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument');
  71. }
  72. if (!_3d(y)) {
  73. throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument');
  74. }
  75. if (!_4d(plane)) {
  76. throw new TypeError('Array with 4 numbers expected as third argument');
  77. }
  78. return _intersectLinePlane(x[0], x[1], x[2], y[0], y[1], y[2], plane[0], plane[1], plane[2], plane[3]);
  79. }
  80. function _AAAA(w, x, y, z) {
  81. w = _coerceArr(w);
  82. x = _coerceArr(x);
  83. y = _coerceArr(y);
  84. z = _coerceArr(z);
  85. if (w.length === 2) {
  86. if (!_2d(w)) {
  87. throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument');
  88. }
  89. if (!_2d(x)) {
  90. throw new TypeError('Array with 2 numbers or BigNumbers expected for second argument');
  91. }
  92. if (!_2d(y)) {
  93. throw new TypeError('Array with 2 numbers or BigNumbers expected for third argument');
  94. }
  95. if (!_2d(z)) {
  96. throw new TypeError('Array with 2 numbers or BigNumbers expected for fourth argument');
  97. }
  98. return _intersect2d(w, x, y, z);
  99. } else if (w.length === 3) {
  100. if (!_3d(w)) {
  101. throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument');
  102. }
  103. if (!_3d(x)) {
  104. throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument');
  105. }
  106. if (!_3d(y)) {
  107. throw new TypeError('Array with 3 numbers or BigNumbers expected for third argument');
  108. }
  109. if (!_3d(z)) {
  110. throw new TypeError('Array with 3 numbers or BigNumbers expected for fourth argument');
  111. }
  112. 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]);
  113. } else {
  114. throw new TypeError('Arrays with two or thee dimensional points expected');
  115. }
  116. }
  117. /** Coerce row and column 2-dim arrays to 1-dim array */
  118. function _coerceArr(arr) {
  119. // row matrix
  120. if (arr.length === 1) return arr[0];
  121. // column matrix
  122. if (arr.length > 1 && Array.isArray(arr[0])) {
  123. if (arr.every(function (el) {
  124. return Array.isArray(el) && el.length === 1;
  125. })) return flatten(arr);
  126. }
  127. return arr;
  128. }
  129. function _2d(x) {
  130. return x.length === 2 && isNumeric(x[0]) && isNumeric(x[1]);
  131. }
  132. function _3d(x) {
  133. return x.length === 3 && isNumeric(x[0]) && isNumeric(x[1]) && isNumeric(x[2]);
  134. }
  135. function _4d(x) {
  136. return x.length === 4 && isNumeric(x[0]) && isNumeric(x[1]) && isNumeric(x[2]) && isNumeric(x[3]);
  137. }
  138. function _intersect2d(p1a, p1b, p2a, p2b) {
  139. var o1 = p1a;
  140. var o2 = p2a;
  141. var d1 = subtract(o1, p1b);
  142. var d2 = subtract(o2, p2b);
  143. var det = subtract(multiplyScalar(d1[0], d2[1]), multiplyScalar(d2[0], d1[1]));
  144. if (isZero(det)) return null;
  145. if (smaller(abs(det), config.epsilon)) {
  146. return null;
  147. }
  148. var d20o11 = multiplyScalar(d2[0], o1[1]);
  149. var d21o10 = multiplyScalar(d2[1], o1[0]);
  150. var d20o21 = multiplyScalar(d2[0], o2[1]);
  151. var d21o20 = multiplyScalar(d2[1], o2[0]);
  152. var t = divideScalar(addScalar(subtract(subtract(d20o11, d21o10), d20o21), d21o20), det);
  153. return add(multiply(d1, t), o1);
  154. }
  155. function _intersect3dHelper(a, b, c, d, e, f, g, h, i, j, k, l) {
  156. // (a - b)*(c - d) + (e - f)*(g - h) + (i - j)*(k - l)
  157. var add1 = multiplyScalar(subtract(a, b), subtract(c, d));
  158. var add2 = multiplyScalar(subtract(e, f), subtract(g, h));
  159. var add3 = multiplyScalar(subtract(i, j), subtract(k, l));
  160. return addScalar(addScalar(add1, add2), add3);
  161. }
  162. function _intersect3d(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) {
  163. var d1343 = _intersect3dHelper(x1, x3, x4, x3, y1, y3, y4, y3, z1, z3, z4, z3);
  164. var d4321 = _intersect3dHelper(x4, x3, x2, x1, y4, y3, y2, y1, z4, z3, z2, z1);
  165. var d1321 = _intersect3dHelper(x1, x3, x2, x1, y1, y3, y2, y1, z1, z3, z2, z1);
  166. var d4343 = _intersect3dHelper(x4, x3, x4, x3, y4, y3, y4, y3, z4, z3, z4, z3);
  167. var d2121 = _intersect3dHelper(x2, x1, x2, x1, y2, y1, y2, y1, z2, z1, z2, z1);
  168. var numerator = subtract(multiplyScalar(d1343, d4321), multiplyScalar(d1321, d4343));
  169. var denominator = subtract(multiplyScalar(d2121, d4343), multiplyScalar(d4321, d4321));
  170. if (isZero(denominator)) return null;
  171. var ta = divideScalar(numerator, denominator);
  172. var tb = divideScalar(addScalar(d1343, multiplyScalar(ta, d4321)), d4343);
  173. var pax = addScalar(x1, multiplyScalar(ta, subtract(x2, x1)));
  174. var pay = addScalar(y1, multiplyScalar(ta, subtract(y2, y1)));
  175. var paz = addScalar(z1, multiplyScalar(ta, subtract(z2, z1)));
  176. var pbx = addScalar(x3, multiplyScalar(tb, subtract(x4, x3)));
  177. var pby = addScalar(y3, multiplyScalar(tb, subtract(y4, y3)));
  178. var pbz = addScalar(z3, multiplyScalar(tb, subtract(z4, z3)));
  179. if (equalScalar(pax, pbx) && equalScalar(pay, pby) && equalScalar(paz, pbz)) {
  180. return [pax, pay, paz];
  181. } else {
  182. return null;
  183. }
  184. }
  185. function _intersectLinePlane(x1, y1, z1, x2, y2, z2, x, y, z, c) {
  186. var x1x = multiplyScalar(x1, x);
  187. var x2x = multiplyScalar(x2, x);
  188. var y1y = multiplyScalar(y1, y);
  189. var y2y = multiplyScalar(y2, y);
  190. var z1z = multiplyScalar(z1, z);
  191. var z2z = multiplyScalar(z2, z);
  192. var numerator = subtract(subtract(subtract(c, x1x), y1y), z1z);
  193. var denominator = subtract(subtract(subtract(addScalar(addScalar(x2x, y2y), z2z), x1x), y1y), z1z);
  194. var t = divideScalar(numerator, denominator);
  195. var px = addScalar(x1, multiplyScalar(t, subtract(x2, x1)));
  196. var py = addScalar(y1, multiplyScalar(t, subtract(y2, y1)));
  197. var pz = addScalar(z1, multiplyScalar(t, subtract(z2, z1)));
  198. return [px, py, pz];
  199. // TODO: Add cases when line is parallel to the plane:
  200. // (a) no intersection,
  201. // (b) line contained in plane
  202. }
  203. });
  204. exports.createIntersect = createIntersect;