Plane.js 6.2 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.clone(), 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.clone(), 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 83
	var normal = one3.clone().normalize();
	var a = new THREE.Plane().setFromNormalAndCoplanarPoint( normal, zero3 );
84
	
85
	ok( a.normal.equals( normal ), "Passed!" );
86 87 88 89
	ok( a.constant == 0, "Passed!" );
});

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

98
test( "negate/distanceToPoint", function() {
B
Ben Houston 已提交
99 100 101 102 103 104
	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!" );

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

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
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!" );
});

131 132 133
test( "isInterestionLine/intersectLine", function() {
	var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );

B
Ben Houston 已提交
134 135 136
	var l1 = new THREE.Line3( new THREE.Vector3( -10, 0, 0 ), new THREE.Vector3( 10, 0, 0 ) );
	ok( a.isIntersectionLine( l1 ), "Passed!" );
	ok( a.intersectLine( l1 ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
137 138 139

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

B
Ben Houston 已提交
140 141
	ok( a.isIntersectionLine( l1 ), "Passed!" );
	ok( a.intersectLine( l1 ).equals( new THREE.Vector3( 3, 0, 0 ) ), "Passed!" );
142 143 144 145


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

B
Ben Houston 已提交
146 147
	ok( ! a.isIntersectionLine( l1 ), "Passed!" );
	ok( a.intersectLine( l1 ) === undefined, "Passed!" );
148 149 150
	
	a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 11 );

B
Ben Houston 已提交
151 152
	ok( ! a.isIntersectionLine( l1 ), "Passed!" );
	ok( a.intersectLine( l1 ) === undefined, "Passed!" );
153 154 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 194 195
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!" );

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