-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathMath.SphereIntersections.test.js
167 lines (112 loc) · 4.24 KB
/
Math.SphereIntersections.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
import { Vector3, Triangle, Sphere, Plane } from 'three';
import { sphereIntersectTriangle } from '../src/math/MathUtilities.js';
import { setRandomVector } from './utils.js';
describe( 'Sphere Intersections', () => {
it( 'should intersect triangles with a vertex inside', () => {
const sphere = new Sphere();
sphere.radius = 1;
setRandomVector( sphere.center, 10 );
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( sphere.center );
setRandomVector( triangle[ fields[ i1 ] ], 1 + 0.0001 + Math.random() )
.add( sphere.center );
setRandomVector( triangle[ fields[ i2 ] ], 1 + 0.0001 + Math.random() )
.add( sphere.center );
expect( sphereIntersectTriangle( sphere, triangle ) ).toBe( true );
}
} );
it( 'should intersect triangles with two vertices inside', () => {
const sphere = new Sphere();
sphere.radius = 1;
setRandomVector( sphere.center, 10 );
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( sphere.center );
setRandomVector( triangle[ fields[ i1 ] ], Math.random() - 0.0001 )
.add( sphere.center );
setRandomVector( triangle[ fields[ i2 ] ], 1 + 0.0001 + Math.random() )
.add( sphere.center );
expect( sphereIntersectTriangle( sphere, triangle ) ).toBe( true );
}
} );
it( 'should intersect triangles with all vertices inside', () => {
const sphere = new Sphere();
sphere.radius = 1;
setRandomVector( sphere.center, 10 );
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( sphere.center );
setRandomVector( triangle[ fields[ i1 ] ], Math.random() - 0.0001 )
.add( sphere.center );
setRandomVector( triangle[ fields[ i2 ] ], Math.random() - 0.0001 )
.add( sphere.center );
expect( sphereIntersectTriangle( sphere, triangle ) ).toBe( true );
}
} );
it( 'should intersect triangles that only intersect the middle', () => {
const sphere = new Sphere();
sphere.radius = 1;
setRandomVector( sphere.center, 10 );
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 ] ], 1 + 0.0001 + Math.random() );
triangle[ fields[ i1 ] ]
.copy( triangle[ fields[ i0 ] ] )
.multiplyScalar( - 1 )
.add( sphere.center );
triangle[ fields[ i0 ] ]
.add( sphere.center );
setRandomVector( triangle[ fields[ i2 ] ], 1 + 0.0001 + Math.random() )
.add( sphere.center );
expect( sphereIntersectTriangle( sphere, triangle ) ).toBe( true );
}
} );
it( 'should not intersect triangles outside sphere', () => {
const sphere = new Sphere();
sphere.radius = 1;
setRandomVector( sphere.center, 10 );
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, sphere.center );
plane.constant += ( Math.sign( Math.random() - 0.5 ) || 1 ) * 1.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( sphere.center );
plane.projectPoint( vec, triangle[ fields[ i0 ] ] );
setRandomVector( vec, 10 * Math.random() )
.add( sphere.center );
plane.projectPoint( vec, triangle[ fields[ i1 ] ] );
setRandomVector( vec, 10 * Math.random() )
.add( sphere.center );
plane.projectPoint( vec, triangle[ fields[ i2 ] ] );
expect( sphereIntersectTriangle( sphere, triangle ) ).toBe( false );
}
} );
} );