-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathMath.TriangleIntersections.test.js
370 lines (253 loc) · 8.75 KB
/
Math.TriangleIntersections.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import { Triangle, Line3 } from 'three';
import { ExtendedTriangle } from '../src/math/ExtendedTriangle.js';
describe( 'Triangle Intersections', () => {
const t1 = new ExtendedTriangle();
const t2 = new Triangle();
it( 'should return false if they are at different angles but not intersecting', () => {
t1.a.set( - 1, - 1, 0 );
t1.b.set( 1, - 1, 0 );
t1.c.set( 0, 1, 0 );
t1.needsUpdate = true;
t2.a.set( - 1, 0, 0 );
t2.b.set( 0, 0, 1 );
t2.c.set( - 2, 0, 1 );
t2.needsUpdate = true;
expect( t1.intersectsTriangle( t2 ) ).toBe( false );
} );
it( 'should return true if just one vertex is in the middle of a triangle', () => {
t1.a.set( - 1, 0, 0 );
t1.b.set( 1, 0, 0 );
t1.c.set( 0, 1, 0 );
t1.needsUpdate = true;
t2.a.set( 0, 0.5, 0 );
t2.b.set( - 1, 0.5, 1 );
t2.c.set( 1, 0.5, 1 );
expect( t1.intersectsTriangle( t2 ) ).toBe( true );
} );
it( 'should return true if just one vertex is overlapping', () => {
t1.a.set( - 1, 0, 0 );
t1.b.set( 1, 0, 0 );
t1.c.set( 0, 1, 0 );
t1.needsUpdate = true;
t2.a.set( - 1, 0, 0 );
t2.b.set( - 3, 0, 0 );
t2.c.set( - 2, 0, 1 );
expect( t1.intersectsTriangle( t2 ) ).toBe( true );
} );
it( 'should return true if the triangles are intersecting at an angle', () => {
t1.a.set( - 1, 0, 0 );
t1.b.set( 1, 0, 0 );
t1.c.set( 0, 1, 0 );
t1.needsUpdate = true;
t2.a.set( 0, 0.5, 0.5 );
t2.b.set( 0.5, 0.5, - 0.5 );
t2.c.set( - 0.5, 0.5, - 0.5 );
expect( t1.intersectsTriangle( t2 ) ).toBe( true );
} );
// coplanar cases
it( 'should return false if the triangles are on the same plane but separated', () => {
t1.a.set( - 1, 0, 0 );
t1.b.set( 1, 0, 0 );
t1.c.set( 0, 1, 0 );
t1.needsUpdate = true;
t2.a.set( - 3, 0, 0 );
t2.b.set( - 1.001, 0, 0 );
t2.c.set( - 2, 1, 0 );
expect( t1.intersectsTriangle( t2 ) ).toBe( false );
} );
it( 'should return true if the triangles are the same', () => {
t1.a.set( - 1, 0, 0 );
t1.b.set( 1, 0, 0 );
t1.c.set( 0, 1, 0 );
t1.needsUpdate = true;
t2.a.set( - 1, 0, 0 );
t2.b.set( 1, 0, 0 );
t2.c.set( 0, 1, 0 );
expect( t1.intersectsTriangle( t2 ) ).toBe( true );
} );
it( 'should return true if the triangles are on the same plane and overlapping', () => {
t1.a.set( - 1, 0, 0 );
t1.b.set( 1, 0, 0 );
t1.c.set( 0, 1, 0 );
t1.needsUpdate = true;
t2.a.set( - 2, 0, 0 );
t2.b.set( 0, 0, 0 );
t2.c.set( - 1, 1, 0 );
expect( t1.intersectsTriangle( t2 ) ).toBe( true );
} );
it( 'should return true if one triangle is completely inside the other', () => {
t1.a.set( - 1, 0, 0 );
t1.b.set( 1, 0, 0 );
t1.c.set( 0, 1, 0 );
t1.needsUpdate = true;
t2.a.set( - 0.5, 0.25, 0 );
t2.b.set( 0.5, 0.25, 0 );
t2.c.set( 0, 0.75, 0 );
expect( t1.intersectsTriangle( t2 ) ).toBe( true );
} );
it( 'should return false if they the same but offset on one axis', () => {
t1.a.set( - 1, 0, 0 );
t1.b.set( 1, 0, 0 );
t1.c.set( 0, 1, 0 );
t1.needsUpdate = true;
t2.a.set( - 1, 0, 0.01 );
t2.b.set( 1, 0, 0.01 );
t2.c.set( 0, 1, 0.01 );
expect( t1.intersectsTriangle( t2 ) ).toBe( false );
} );
it( 'should return false when triangles are disconnected with aligned vertices on y.', () => {
t1.a.set( - 0.5, 0.5, 1.5 );
t1.b.set( - 0.5, 0.5, 2.5 );
t1.c.set( 0.5, 0.5, 1.5 );
t1.needsUpdate = true;
t2.a.set( 0.5, 0.5, 0.5 );
t2.b.set( 0.5, - 0.5, 0.5 );
t2.c.set( 0.5, 0.5, - 0.5 );
t2.needsUpdate = true;
expect( t1.intersectsTriangle( t2 ) ).toBe( false );
} );
} );
describe( 'Triangle Intersection line', () => {
const t1 = new ExtendedTriangle();
const t2 = new ExtendedTriangle();
const target = new Line3();
const expected = new Line3();
const expectVerticesToBeClose = ( a, b ) => {
expect( a.x ).toBeCloseTo( b.x );
expect( a.y ).toBeCloseTo( b.y );
expect( a.z ).toBeCloseTo( b.z );
};
const expectLinesToBeClose = ( a, b ) => {
// Try both line orientations
try {
expectVerticesToBeClose( a.start, b.start );
expectVerticesToBeClose( a.end, b.end );
} catch {
expectVerticesToBeClose( a.end, b.start );
expectVerticesToBeClose( a.start, b.end );
}
};
it( 'should intersect on point', () => {
t1.a.set( 0, 0, 0 );
t1.b.set( 0, 0, 2 );
t1.c.set( 2, 0, 0 );
t1.needsUpdate = true;
t2.a.set( 1, - 1, 0 );
t2.b.set( 1, 1, 0 );
t2.c.set( 1, 0, - 1 );
t2.needsUpdate = true;
expected.start.set( 1, 0, 0 );
expected.end.set( 1, 0, 0 );
expect( t1.intersectsTriangle( t2, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
} );
it( 'should intersect', () => {
t1.a.set( 0, 0, 0 );
t1.b.set( 0, 0, 5 );
t1.c.set( 5, 0, 0 );
t1.needsUpdate = true;
t2.a.set( 1, - 1, 1 );
t2.b.set( 1, - 1, - 1 );
t2.c.set( 1, 1, 1 );
t2.needsUpdate = true;
expected.start.set( 1, 0, 0 );
expected.end.set( 1, 0, 1 );
expect( t1.intersectsTriangle( t2, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
} );
it( 'should intersect on common side', () => {
t1.a.set( 0, 0, 0 );
t1.b.set( 3, 0, 0 );
t1.c.set( 0, 1, 2 );
t1.needsUpdate = true;
t2.a.set( 1, 0, 0 );
t2.b.set( 2, 0, 0 );
t2.c.set( 0, 1, - 2 );
t2.needsUpdate = true;
expected.start.set( 1, 0, 0 );
expected.end.set( 2, 0, 0 );
expect( t1.intersectsTriangle( t2, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
} );
// coplanar cases
it( 'should support triangles that intersect along a coplanar edge.', () => {
t1.b.set( - 1, 0, 0 );
t1.c.set( 2, 0, 0 );
t1.a.set( 2, 0, 2 );
t1.needsUpdate = true;
t2.a.set( 1, 0, 0 );
t2.b.set( - 2, - 2, 0 );
t2.c.set( - 2, 2, 0 );
t2.needsUpdate = true;
expected.start.set( - 1, 0, 0 );
expected.end.set( 1, 0, 0 );
expect( t1.intersectsTriangle( t2, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
} );
it( 'should be coplanar and line is zero', () => {
t1.a.set( 0, 0, 0 );
t1.b.set( 3, 0, 0 );
t1.c.set( 0, 0, 2 );
t1.needsUpdate = true;
t2.a.set( 1, 0, 0 );
t2.b.set( 2, 0, 0 );
t2.c.set( 0, 0, - 2 );
t2.needsUpdate = true;
expected.start.set( 0, 0, 0 );
expected.end.set( 0, 0, 0 );
expect( t1.intersectsTriangle( t2, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
} );
// this test fails due to floating point precision issues. If the triangles are scaled up
// it reports an intersection as expected.
it.skip( 'triangles almost coplanar should intersect on point', () => {
t1.a.set( 0.0720, 0.2096, 0.3220 );
t1.b.set( 0.0751, 0.2148, 0.3234 );
t1.c.set( 0.0693, 0.2129, 0.3209 );
t1.needsUpdate = true;
t2.a.set( 0.0677, 0.2170, 0.3196 );
t2.b.set( 0.0607, 0.2135, 0.3165 );
t2.c.set( 0.0693, 0.2129, 0.3209 );
t2.needsUpdate = true;
expected.start.set( 0.0693, 0.2129, 0.3209 );
expected.end.set( 0.0693, 0.2129, 0.3209 );
expect( t1.intersectsTriangle( t2, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
expect( t2.intersectsTriangle( t1, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
} );
// this test fails due to a bug in the intersection function. It only intersects
// on one triangle intersection order.
it( 'triangles should return a correct intersection (issue #538)', () => {
t1.a.set( - 5.781455993652344, - 7.291503906249993, - 30 );
t1.b.set( - 5.781455993652344, - 7.291503906250007, 30 );
t1.c.set( 0, - 7.291503906249993, - 30 );
t1.needsUpdate = true;
t2.a.set( - 5.781455993652344, - 7.29150390625, - 4.098872661590576 );
t2.b.set( - 6.386039733886719, - 11.163619995117188, - 4.485982418060303 );
t2.c.set( 13.468360900878906, - 6.142303466796875, - 4.028029918670654 );
t2.needsUpdate = true;
expected.start.set( - 2.4950, - 7.2915, - 4.1065 );
expected.end.set( - 5.7815, - 7.2915, - 4.0989 );
expect( t1.intersectsTriangle( t2, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
expect( t2.intersectsTriangle( t1, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
} );
it( 'triangles should return a correct intersection (issue #543)', () => {
t1.a.set( - 15.430519104003906, 29.432968139648445, - 25 );
t1.b.set( - 15.430519104003906, 29.43296813964843, 25 );
t1.c.set( 0, 29.432968139648445, - 25 );
t1.needsUpdate = true;
t2.a.set( - 4.854911804199219, 36.03794860839844, 0.0777292251586914 );
t2.b.set( - 15.430519104003906, 29.432968139648438, - 1.905876636505127 );
t2.c.set( - 16.118995666503906, 26.96272277832031, - 2.8487582206726074 );
t2.needsUpdate = true;
expected.start.set( - 15.4305, 29.433, - 1.9059 );
expected.end.set( - 13.053, 29.4323, - 2.0522 );
expect( t1.intersectsTriangle( t2, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
expect( t2.intersectsTriangle( t1, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
} );
} );