1 | import { isBigNumber } from '../../utils/is';
|
2 | import { factory } from '../../utils/factory';
|
3 | var name = 'intersect';
|
4 | var dependencies = ['typed', 'config', 'abs', 'add', 'addScalar', 'matrix', 'multiply', 'multiplyScalar', 'divideScalar', 'subtract', 'smaller', 'equalScalar'];
|
5 | export var createIntersect =
|
6 |
|
7 | factory(name, dependencies, function (_ref) {
|
8 | var typed = _ref.typed,
|
9 | config = _ref.config,
|
10 | abs = _ref.abs,
|
11 | add = _ref.add,
|
12 | addScalar = _ref.addScalar,
|
13 | matrix = _ref.matrix,
|
14 | multiply = _ref.multiply,
|
15 | multiplyScalar = _ref.multiplyScalar,
|
16 | divideScalar = _ref.divideScalar,
|
17 | subtract = _ref.subtract,
|
18 | smaller = _ref.smaller,
|
19 | equalScalar = _ref.equalScalar;
|
20 |
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | var intersect = typed('intersect', {
|
49 | 'Array, Array, Array': function ArrayArrayArray(x, y, plane) {
|
50 | if (!_3d(x)) {
|
51 | throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument');
|
52 | }
|
53 |
|
54 | if (!_3d(y)) {
|
55 | throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument');
|
56 | }
|
57 |
|
58 | if (!_4d(plane)) {
|
59 | throw new TypeError('Array with 4 numbers expected as third argument');
|
60 | }
|
61 |
|
62 | return _intersectLinePlane(x[0], x[1], x[2], y[0], y[1], y[2], plane[0], plane[1], plane[2], plane[3]);
|
63 | },
|
64 | 'Array, Array, Array, Array': function ArrayArrayArrayArray(w, x, y, z) {
|
65 | if (w.length === 2) {
|
66 | if (!_2d(w)) {
|
67 | throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument');
|
68 | }
|
69 |
|
70 | if (!_2d(x)) {
|
71 | throw new TypeError('Array with 2 numbers or BigNumbers expected for second argument');
|
72 | }
|
73 |
|
74 | if (!_2d(y)) {
|
75 | throw new TypeError('Array with 2 numbers or BigNumbers expected for third argument');
|
76 | }
|
77 |
|
78 | if (!_2d(z)) {
|
79 | throw new TypeError('Array with 2 numbers or BigNumbers expected for fourth argument');
|
80 | }
|
81 |
|
82 | return _intersect2d(w, x, y, z);
|
83 | } else if (w.length === 3) {
|
84 | if (!_3d(w)) {
|
85 | throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument');
|
86 | }
|
87 |
|
88 | if (!_3d(x)) {
|
89 | throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument');
|
90 | }
|
91 |
|
92 | if (!_3d(y)) {
|
93 | throw new TypeError('Array with 3 numbers or BigNumbers expected for third argument');
|
94 | }
|
95 |
|
96 | if (!_3d(z)) {
|
97 | throw new TypeError('Array with 3 numbers or BigNumbers expected for fourth argument');
|
98 | }
|
99 |
|
100 | 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]);
|
101 | } else {
|
102 | throw new TypeError('Arrays with two or thee dimensional points expected');
|
103 | }
|
104 | },
|
105 | 'Matrix, Matrix, Matrix': function MatrixMatrixMatrix(x, y, plane) {
|
106 | return matrix(intersect(x.valueOf(), y.valueOf(), plane.valueOf()));
|
107 | },
|
108 | 'Matrix, Matrix, Matrix, Matrix': function MatrixMatrixMatrixMatrix(w, x, y, z) {
|
109 |
|
110 | return matrix(intersect(w.valueOf(), x.valueOf(), y.valueOf(), z.valueOf()));
|
111 | }
|
112 | });
|
113 |
|
114 | function _isNumeric(a) {
|
115 |
|
116 | return typeof a === 'number' || isBigNumber(a);
|
117 | }
|
118 |
|
119 | function _2d(x) {
|
120 | return x.length === 2 && _isNumeric(x[0]) && _isNumeric(x[1]);
|
121 | }
|
122 |
|
123 | function _3d(x) {
|
124 | return x.length === 3 && _isNumeric(x[0]) && _isNumeric(x[1]) && _isNumeric(x[2]);
|
125 | }
|
126 |
|
127 | function _4d(x) {
|
128 | return x.length === 4 && _isNumeric(x[0]) && _isNumeric(x[1]) && _isNumeric(x[2]) && _isNumeric(x[3]);
|
129 | }
|
130 |
|
131 | function _intersect2d(p1a, p1b, p2a, p2b) {
|
132 | var o1 = p1a;
|
133 | var o2 = p2a;
|
134 | var d1 = subtract(o1, p1b);
|
135 | var d2 = subtract(o2, p2b);
|
136 | var det = subtract(multiplyScalar(d1[0], d2[1]), multiplyScalar(d2[0], d1[1]));
|
137 |
|
138 | if (smaller(abs(det), config.epsilon)) {
|
139 | return null;
|
140 | }
|
141 |
|
142 | var d20o11 = multiplyScalar(d2[0], o1[1]);
|
143 | var d21o10 = multiplyScalar(d2[1], o1[0]);
|
144 | var d20o21 = multiplyScalar(d2[0], o2[1]);
|
145 | var d21o20 = multiplyScalar(d2[1], o2[0]);
|
146 | var t = divideScalar(addScalar(subtract(subtract(d20o11, d21o10), d20o21), d21o20), det);
|
147 | return add(multiply(d1, t), o1);
|
148 | }
|
149 |
|
150 | function _intersect3dHelper(a, b, c, d, e, f, g, h, i, j, k, l) {
|
151 |
|
152 | var add1 = multiplyScalar(subtract(a, b), subtract(c, d));
|
153 | var add2 = multiplyScalar(subtract(e, f), subtract(g, h));
|
154 | var add3 = multiplyScalar(subtract(i, j), subtract(k, l));
|
155 | return addScalar(addScalar(add1, add2), add3);
|
156 | }
|
157 |
|
158 | function _intersect3d(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) {
|
159 | var d1343 = _intersect3dHelper(x1, x3, x4, x3, y1, y3, y4, y3, z1, z3, z4, z3);
|
160 |
|
161 | var d4321 = _intersect3dHelper(x4, x3, x2, x1, y4, y3, y2, y1, z4, z3, z2, z1);
|
162 |
|
163 | var d1321 = _intersect3dHelper(x1, x3, x2, x1, y1, y3, y2, y1, z1, z3, z2, z1);
|
164 |
|
165 | var d4343 = _intersect3dHelper(x4, x3, x4, x3, y4, y3, y4, y3, z4, z3, z4, z3);
|
166 |
|
167 | var d2121 = _intersect3dHelper(x2, x1, x2, x1, y2, y1, y2, y1, z2, z1, z2, z1);
|
168 |
|
169 | var ta = divideScalar(subtract(multiplyScalar(d1343, d4321), multiplyScalar(d1321, d4343)), subtract(multiplyScalar(d2121, d4343), multiplyScalar(d4321, d4321)));
|
170 | var tb = divideScalar(addScalar(d1343, multiplyScalar(ta, d4321)), d4343);
|
171 | var pax = addScalar(x1, multiplyScalar(ta, subtract(x2, x1)));
|
172 | var pay = addScalar(y1, multiplyScalar(ta, subtract(y2, y1)));
|
173 | var paz = addScalar(z1, multiplyScalar(ta, subtract(z2, z1)));
|
174 | var pbx = addScalar(x3, multiplyScalar(tb, subtract(x4, x3)));
|
175 | var pby = addScalar(y3, multiplyScalar(tb, subtract(y4, y3)));
|
176 | var pbz = addScalar(z3, multiplyScalar(tb, subtract(z4, z3)));
|
177 |
|
178 | if (equalScalar(pax, pbx) && equalScalar(pay, pby) && equalScalar(paz, pbz)) {
|
179 | return [pax, pay, paz];
|
180 | } else {
|
181 | return null;
|
182 | }
|
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 t = divideScalar(subtract(subtract(subtract(c, x1x), y1y), z1z), subtract(subtract(subtract(addScalar(addScalar(x2x, y2y), z2z), x1x), y1y), z1z));
|
193 | var px = addScalar(x1, multiplyScalar(t, subtract(x2, x1)));
|
194 | var py = addScalar(y1, multiplyScalar(t, subtract(y2, y1)));
|
195 | var pz = addScalar(z1, multiplyScalar(t, subtract(z2, z1)));
|
196 | return [px, py, pz];
|
197 |
|
198 |
|
199 | }
|
200 |
|
201 | return intersect;
|
202 | }); |
\ | No newline at end of file |