-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
580 additions
and
434 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 ); | ||
|
||
} | ||
|
||
} ); | ||
|
||
} ); |
Oops, something went wrong.