提交 3595365f 编写于 作者: B Ben Houston

add Plane.intersectLine with unit tests.

上级 eb25ee76
......@@ -112,6 +112,40 @@ THREE.Plane.prototype = {
},
intersectLine: function ( startPoint, endPoint, optionalTarget ) {
var result = optionalTarget || new THREE.Vector3();
var direction = THREE.Plane.__v1.sub( endPoint, startPoint );
var denominator = this.normal.dot( direction );
if ( denominator == 0 ) {
// line is coplanar, return origin
if( this.distanceToPoint( startPoint ) == 0 ) {
return result.copy( startPoint );
}
// Unsure if this is the correct method to handle this case.
return undefined;
}
var t = - ( startPoint.dot( this.normal ) + this.constant ) / denominator;
if( t < 0 || t > 1 ) {
return undefined;
}
return result.copy( direction ).multiplyScalar( t ).addSelf( startPoint );
},
coplanarPoint: function ( optionalTarget ) {
var result = optionalTarget || new THREE.Vector3();
......
......@@ -115,6 +115,31 @@ test( "distanceToSphere", function() {
ok( a.distanceToSphere( b ) === -1, "Passed!" );
});
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!" );
});
test( "projectPoint", function() {
var a = new THREE.Plane( new THREE.Vector3( 1, 0, 0 ), 0 );
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册