UNPKG

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