-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathMath.OBB.test.js
162 lines (107 loc) · 3.88 KB
/
Math.OBB.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
import { Vector3, Triangle, Plane } from 'three';
import { OrientedBox } from '../src/math/OrientedBox.js';
import { setRandomVector, getRandomOrientation } from './utils.js';
describe( 'OBB Intersections', () => {
let box, center;
beforeEach( () => {
box = new OrientedBox();
box.min.set( - 1, - 1, - 1 );
box.max.set( 1, 1, 1 );
getRandomOrientation( box.matrix, 10 );
box.needsUpdate = true;
center = new Vector3();
center.setFromMatrixPosition( box.matrix );
} );
it( 'should intersect triangles with a vertex inside', () => {
const triangle = new Triangle();
for ( let i = 0; i < 100; i ++ ) {
const fields = [ 'a', 'b', 'c' ];
const i0 = i % 3;
const i1 = ( i + 1 ) % 3;
const i2 = ( i + 2 ) % 3;
setRandomVector( triangle[ fields[ i0 ] ], Math.random() - 0.0001 )
.add( center );
setRandomVector( triangle[ fields[ i1 ] ], 3 + 0.0001 + Math.random() )
.add( center );
setRandomVector( triangle[ fields[ i2 ] ], 3 + 0.0001 + Math.random() )
.add( center );
expect( box.intersectsTriangle( triangle ) ).toBe( true );
}
} );
it( 'should intersect triangles with two vertices inside', () => {
const triangle = new Triangle();
for ( let i = 0; i < 100; i ++ ) {
const fields = [ 'a', 'b', 'c' ];
const i0 = i % 3;
const i1 = ( i + 1 ) % 3;
const i2 = ( i + 2 ) % 3;
setRandomVector( triangle[ fields[ i0 ] ], Math.random() - 0.0001 )
.add( center );
setRandomVector( triangle[ fields[ i1 ] ], Math.random() - 0.0001 )
.add( center );
setRandomVector( triangle[ fields[ i2 ] ], 3 + 0.0001 + Math.random() )
.add( center );
expect( box.intersectsTriangle( triangle ) ).toBe( true );
}
} );
it( 'should intersect triangles with all vertices inside', () => {
const triangle = new Triangle();
for ( let i = 0; i < 100; i ++ ) {
const fields = [ 'a', 'b', 'c' ];
const i0 = i % 3;
const i1 = ( i + 1 ) % 3;
const i2 = ( i + 2 ) % 3;
setRandomVector( triangle[ fields[ i0 ] ], Math.random() - 0.0001 )
.add( center );
setRandomVector( triangle[ fields[ i1 ] ], Math.random() - 0.0001 )
.add( center );
setRandomVector( triangle[ fields[ i2 ] ], Math.random() - 0.0001 )
.add( center );
expect( box.intersectsTriangle( triangle ) ).toBe( true );
}
} );
it( 'should intersect triangles that cut across', () => {
const triangle = new Triangle();
for ( let i = 0; i < 100; i ++ ) {
const fields = [ 'a', 'b', 'c' ];
const i0 = i % 3;
const i1 = ( i + 1 ) % 3;
const i2 = ( i + 2 ) % 3;
setRandomVector( triangle[ fields[ i0 ] ], 3 + 0.0001 + Math.random() );
triangle[ fields[ i1 ] ]
.copy( triangle[ fields[ i0 ] ] )
.multiplyScalar( - 1 )
.add( center );
triangle[ fields[ i0 ] ]
.add( center );
setRandomVector( triangle[ fields[ i2 ] ], 3 + 0.0001 + Math.random() )
.add( center );
expect( box.intersectsTriangle( triangle ) ).toBe( true );
}
} );
it( 'should not intersect triangles outside sphere', () => {
const plane = new Plane();
const vec = new Vector3();
const triangle = new Triangle();
for ( let i = 0; i < 100; i ++ ) {
// project the triangle out onto a plane
setRandomVector( plane.normal, 1 );
plane.setFromNormalAndCoplanarPoint( plane.normal, center );
plane.constant += ( Math.sign( Math.random() - 0.5 ) || 1 ) * 5.001;
const fields = [ 'a', 'b', 'c' ];
const i0 = i % 3;
const i1 = ( i + 1 ) % 3;
const i2 = ( i + 2 ) % 3;
setRandomVector( vec, 10 * Math.random() )
.add( center );
plane.projectPoint( vec, triangle[ fields[ i0 ] ] );
setRandomVector( vec, 10 * Math.random() )
.add( center );
plane.projectPoint( vec, triangle[ fields[ i1 ] ] );
setRandomVector( vec, 10 * Math.random() )
.add( center );
plane.projectPoint( vec, triangle[ fields[ i2 ] ] );
expect( box.intersectsTriangle( triangle ) ).toBe( false );
}
} );
} );