123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /**
- * @author bhouston / http://exocortex.com
- */
- THREE.Plane = function ( normal, constant ) {
- if ( normal === undefined && constant === undefined ) {
- this.normal = new THREE.Vector3();
- this.constant = 0;
- } else {
- this.normal = normal.clone();
- this.constant = constant || 0;
- }
- };
- THREE.Plane.prototype = {
- constructor: THREE.Plane,
- set: function ( normal, constant ) {
- this.normal.copy( normal );
- this.constant = constant;
- return this;
- },
- setComponents: function ( x, y, z, w ) {
- this.normal.set( x, y, z );
- this.constant = w;
- return this;
- },
- setFromNormalAndCoplanarPoint: function ( normal, point ) {
- this.normal.copy( normal );
- this.constant = - point.dot( normal );
- return this;
- },
- setFromCoplanarPoints: function ( a, b, c ) {
- var normal = THREE.Plane.__v1.sub( b, a ).cross(
- THREE.Plane.__v2.sub( c, a ) );
- // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
- this.setFromNormalAndCoplanarPoint( normal, a );
- return this;
- },
- copy: function ( plane ) {
- this.normal.copy( plane.normal );
- this.constant = plane.constant;
- return this;
- },
- flip: function () {
- this.normal.negate();
- return this;
- },
- normalize: function () {
- // Note: will lead to a divide by zero if the plane is invalid.
- var inverseNormalLength = 1.0 / this.normal.length();
- this.normal.multiplyScalar( inverseNormalLength );
- this.constant *= inverseNormalLength;
- return this;
- },
- distanceToPoint: function ( point ) {
- return this.normal.dot( point ) + this.constant;
- },
- distanceToSphere: function ( sphere ) {
- return this.distanceToPoint( sphere.center ) - sphere.radius;
- },
- projectPoint: function ( point ) {
- return this.orthoPoint( point ).subSelf( point ).negate();
- },
- orthoPoint: function ( point ) {
- var perpendicularMagnitude = this.distanceToPoint( point );
- return new THREE.Vector3().copy( this.normal ).multiplyScalar( perpendicularMagnitude );
- },
- intersectsLine: function ( startPoint, endPoint ) {
- // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
- var startSign = this.distanceToPoint( startPoint );
- var endSign = this.distanceToPoint( endPoint );
- return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
- },
- coplanarPoint: function () {
- return new THREE.Vector3().copy( this.normal ).multiplyScalar( - this.constant );
- },
- translate: function ( offset ) {
- this.constant = - offset.dot( this.normal );
- return this;
- },
- equals: function ( plane ) {
- return plane.normal.equals( this.normal ) && ( plane.constant == this.constant );
- },
- clone: function () {
- return new THREE.Plane().copy( this );
- }
- };
- THREE.Plane.__v1 = new THREE.Vector3();
- THREE.Plane.__v2 = new THREE.Vector3();
|