Sphere.js 2.6 KB
Newer Older
B
Ben Houston 已提交
1 2 3 4 5 6 7 8
/**
 * @author bhouston / http://exocortex.com
 */

module( "Sphere" );

test( "constructor", function() {
	var a = new THREE.Sphere();
9
	ok( a.center.equals( zero3 ), "Passed!" );
B
Ben Houston 已提交
10 11
	ok( a.radius == 0, "Passed!" );

12 13
	a = new THREE.Sphere( one3, 1 );
	ok( a.center.equals( one3 ), "Passed!" );
14
	ok( a.radius == 1, "Passed!" );
B
Ben Houston 已提交
15 16 17
});

test( "copy", function() {
18
	var a = new THREE.Sphere( one3, 1 );
B
Ben Houston 已提交
19 20
	var b = new THREE.Sphere().copy( a );

21
	ok( b.center.equals( one3 ), "Passed!" );
22
	ok( b.radius == 1, "Passed!" );
B
Ben Houston 已提交
23 24

	// ensure that it is a true copy
25
	a.center = zero3;
B
Ben Houston 已提交
26
	a.radius = 0;
27
	ok( b.center.equals( one3 ), "Passed!" );
28
	ok( b.radius == 1, "Passed!" );
B
Ben Houston 已提交
29 30 31
});

test( "set", function() {
32
	var a = new THREE.Sphere();
33
	ok( a.center.equals( zero3 ), "Passed!" );
B
Ben Houston 已提交
34 35
	ok( a.radius == 0, "Passed!" );

36
	a.set( one3, 1 );
37
	ok( a.center.equals( one3 ), "Passed!" );
38
	ok( a.radius == 1, "Passed!" );
B
Ben Houston 已提交
39
});
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

test( "empty", function() {
	var a = new THREE.Sphere();
	ok( a.empty(), "Passed!" );

	a.set( one3, 1 );
	ok( ! a.empty(), "Passed!" );
});

test( "containsPoint", function() {
	var a = new THREE.Sphere( one3, 1 );

	ok( ! a.containsPoint( zero3 ), "Passed!" );
	ok( a.containsPoint( one3 ), "Passed!" );
});

test( "distanceToPoint", function() {
	var a = new THREE.Sphere( one3, 1 );

	ok( ( a.distanceToPoint( zero3 ) - 0.7320 ) < 0.001, "Passed!" );
	ok( a.distanceToPoint( one3 ) === -1, "Passed!" );
});

63 64 65 66 67 68 69 70 71
test( "isIntersectionSphere", function() {
	var a = new THREE.Sphere( one3, 1 );
	var b = new THREE.Sphere( zero3, 1 );
	var c = new THREE.Sphere( zero3, 0.25 );

	ok( a.isIntersectionSphere( b ) , "Passed!" );
	ok( ! a.isIntersectionSphere( c ) , "Passed!" );
});

72 73 74 75 76 77 78
test( "clampPoint", function() {
	var a = new THREE.Sphere( one3, 1 );

	ok( a.clampPoint( new THREE.Vector3( 1, 1, 3 ) ).equals( new THREE.Vector3( 1, 1, 2 ) ), "Passed!" );
	ok( a.clampPoint( new THREE.Vector3( 1, 1, -3 ) ).equals( new THREE.Vector3( 1, 1, 0 ) ), "Passed!" );
});

79
test( "getBoundingBox", function() {
80 81
	var a = new THREE.Sphere( one3, 1 );

82
	ok( a.getBoundingBox().equals( new THREE.Box3( zero3, two3 ) ), "Passed!" );
83 84

	a.set( zero3, 0 )
85
	ok( a.getBoundingBox().equals( new THREE.Box3( zero3, zero3 ) ), "Passed!" );
86 87 88 89 90 91 92
});

test( "transform", function() {
	var a = new THREE.Sphere( one3, 1 );

	var m = new THREE.Matrix4();
	var t1 = new THREE.Vector3( 1, -2, 1 );
93
	m.makeTranslation( t1 );
94

95
	ok( a.clone().transform( m ).getBoundingBox().equals( a.getBoundingBox().transform( m ) ), "Passed!" );
96 97 98 99 100 101 102 103
});

test( "translate", function() {
	var a = new THREE.Sphere( one3, 1 );

	a.translate( one3.clone().negate() );
	ok( a.center.equals( zero3 ), "Passed!" );
});