Plane.js 6.6 KB
Newer Older
1 2 3 4 5 6
/**
 * @author bhouston / http://exocortex.com
 */

module( "Plane" );

7 8 9 10 11 12 13
var comparePlane = function ( a, b, threshold ) {
	threshold = threshold || 0.0001;
	return ( a.normal.distanceTo( b.normal ) < threshold &&
	Math.abs( a.constant - b.constant ) < threshold );
};


14 15
test( "constructor", function() {
	var a = new THREE.Plane();
16
	ok( a.normal.x == 1, "Passed!" );
17 18 19 20
	ok( a.normal.y == 0, "Passed!" );
	ok( a.normal.z == 0, "Passed!" );
	ok( a.constant == 0, "Passed!" );

21
	a = new THREE.Plane( one3, 0 );
22 23 24 25 26
	ok( a.normal.x == 1, "Passed!" );
	ok( a.normal.y == 1, "Passed!" );
	ok( a.normal.z == 1, "Passed!" );
	ok( a.constant == 0, "Passed!" );

27
	a = new THREE.Plane( one3, 1 );
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
	ok( a.normal.x == 1, "Passed!" );
	ok( a.normal.y == 1, "Passed!" );
	ok( a.normal.z == 1, "Passed!" );
	ok( a.constant == 1, "Passed!" );
});

test( "copy", function() {
	var a = new THREE.Plane( new THREE.Vector3( x, y, z ), w );
	var b = new THREE.Plane().copy( a );
	ok( b.normal.x == x, "Passed!" );
	ok( b.normal.y == y, "Passed!" );
	ok( b.normal.z == z, "Passed!" );
	ok( b.constant == w, "Passed!" );

	// ensure that it is a true copy
	a.normal.x = 0;
	a.normal.y = -1;
	a.normal.z = -2;
	a.constant = -3;
	ok( b.normal.x == x, "Passed!" );
	ok( b.normal.y == y, "Passed!" );
	ok( b.normal.z == z, "Passed!" );
	ok( b.constant == w, "Passed!" );
});

test( "set", function() {
	var a = new THREE.Plane();
55
	ok( a.normal.x == 1, "Passed!" );
56 57 58 59 60 61 62 63 64 65 66 67 68
	ok( a.normal.y == 0, "Passed!" );
	ok( a.normal.z == 0, "Passed!" );
	ok( a.constant == 0, "Passed!" );

	var b = a.clone().set( new THREE.Vector3( x, y, z ), w );
	ok( b.normal.x == x, "Passed!" );
	ok( b.normal.y == y, "Passed!" );
	ok( b.normal.z == z, "Passed!" );
	ok( b.constant == w, "Passed!" );
});

test( "setComponents", function() {
	var a = new THREE.Plane();
69
	ok( a.normal.x == 1, "Passed!" );
70 71 72 73 74 75 76 77 78 79 80 81
	ok( a.normal.y == 0, "Passed!" );
	ok( a.normal.z == 0, "Passed!" );
	ok( a.constant == 0, "Passed!" );

	var b = a.clone().setComponents( x, y, z , w );
	ok( b.normal.x == x, "Passed!" );
	ok( b.normal.y == y, "Passed!" );
	ok( b.normal.z == z, "Passed!" );
	ok( b.constant == w, "Passed!" );
});

test( "setFromNormalAndCoplanarPoint", function() {
82
	var a = new THREE.Plane().setFromNormalAndCoplanarPoint( one3, zero3 );
83
	
84
	ok( a.normal.equals( one3.clone().normalize() ), "Passed!" );
85 86 87 88
	ok( a.constant == 0, "Passed!" );
});

test( "normalize", function() {
89
	var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), 2 );
90 91 92
	
	a.normalize();
	ok( a.normal.length() == 1, "Passed!" );
93
	ok( a.normal.equals( new THREE.Vector3( 1, 0, 0 ) ), "Passed!" );
94 95
	ok( a.constant == 1, "Passed!" );
});
96

97
test( "negate/distanceToPoint", function() {
B
Ben Houston 已提交
98 99 100 101 102 103
	var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), -2 );
	
	a.normalize();
	ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === 3, "Passed!" );
	ok( a.distanceToPoint( new THREE.Vector3( 1, 0, 0 ) ) === 0, "Passed!" );

104
	a.negate();
B
Ben Houston 已提交
105 106 107 108
	ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === -3, "Passed!" );
	ok( a.distanceToPoint( new THREE.Vector3( 1, 0, 0 ) ) === 0, "Passed!" );
});

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
test( "distanceToPoint", function() {
	var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), -2 );
	
	a.normalize();
	ok( a.distanceToPoint( a.projectPoint( zero3.clone() ) ) === 0, "Passed!" );
	ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === 3, "Passed!" );
});

test( "distanceToSphere", function() {
	var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );

	var b = new THREE.Sphere( new THREE.Vector3( 2, 0, 0 ), 1 );
	
	ok( a.distanceToSphere( b ) === 1, "Passed!" );

	a.set( new THREE.Vector3( 1, 0, 0 ), 2 );
	ok( a.distanceToSphere( b ) === 3, "Passed!" );
	a.set( new THREE.Vector3( 1, 0, 0 ), -2 );
	ok( a.distanceToSphere( b ) === -1, "Passed!" );
});

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
test( "isInterestionLine/intersectLine", function() {
	var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );

	ok( a.isIntersectionLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
	ok( a.intersectLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );

	a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), -3 );

	ok( a.isIntersectionLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
	ok( a.intersectLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ).equals( new THREE.Vector3( 3, 0, 0 ) ), "Passed!" );


	a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), -11 );

	ok( ! a.isIntersectionLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
	ok( a.intersectLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ) === undefined, "Passed!" );
	
	a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 11 );

	ok( ! a.isIntersectionLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
	ok( a.intersectLine( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) ) === undefined, "Passed!" );

});


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
test( "projectPoint", function() {
	var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );

	ok( a.projectPoint( new THREE.Vector3( 10, 0, 0 ) ).equals( zero3 ), "Passed!" );
	ok( a.projectPoint( new THREE.Vector3( -10, 0, 0 ) ).equals( zero3 ), "Passed!" );

	a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
	ok( a.projectPoint( new THREE.Vector3( 0, 0, 0 ) ).equals( new THREE.Vector3( 0, 1, 0 ) ), "Passed!" );
	ok( a.projectPoint( new THREE.Vector3( 0, 1, 0 ) ).equals( new THREE.Vector3( 0, 1, 0 ) ), "Passed!" );
	
});

test( "orthoPoint", function() {
	var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );

	ok( a.orthoPoint( new THREE.Vector3( 10, 0, 0 ) ).equals( new THREE.Vector3( 10, 0, 0 ) ), "Passed!" );
	ok( a.orthoPoint( new THREE.Vector3( -10, 0, 0 ) ).equals( new THREE.Vector3( -10, 0, 0 ) ), "Passed!" );
});

test( "coplanarPoint", function() {
	var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
	ok( a.distanceToPoint( a.coplanarPoint() ) === 0, "Passed!" );

	a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
	ok( a.distanceToPoint( a.coplanarPoint() ) === 0, "Passed!" );
});

test( "transform/translate", function() {

	var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );

	var m = new THREE.Matrix4();
	m.makeRotationZ( Math.PI * 0.5 );

	ok( comparePlane( a.clone().transform( m ), new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), 0 ) ), "Passed!" );

	a = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), -1 );
	ok( comparePlane( a.clone().transform( m ), new THREE.Plane( new THREE.Vector3( -1, 0, 0 ), -1 ) ), "Passed!" );

194
	m.makeTranslation( new THREE.Vector3( 1, 1, 1 ) );
195 196
	ok( comparePlane( a.clone().transform( m ), a.clone().translate( new THREE.Vector3( 1, 1, 1 ) ) ), "Passed!" );
});