UNPKG

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