Browse Source

Box3: Remove closures.

Mugen87 6 years ago
parent
commit
50612de861
1 changed files with 177 additions and 182 deletions
  1. 177 182
      src/math/Box3.js

+ 177 - 182
src/math/Box3.js

@@ -5,6 +5,15 @@ import { Vector3 } from './Vector3.js';
  * @author WestLangley / http://github.com/WestLangley
  */
 
+var points;
+var vector;
+
+var v0, v1, v2;
+var f0, f1, f2;
+var center;
+var extents;
+var triangleNormal;
+
 function Box3( min, max ) {
 
 	this.min = ( min !== undefined ) ? min : new Vector3( + Infinity, + Infinity, + Infinity );
@@ -105,22 +114,18 @@ Object.assign( Box3.prototype, {
 
 	},
 
-	setFromCenterAndSize: function () {
-
-		var v1 = new Vector3();
+	setFromCenterAndSize: function ( center, size ) {
 
-		return function setFromCenterAndSize( center, size ) {
+		if ( vector === undefined ) vector = new Vector3();
 
-			var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
+		var halfSize = vector.copy( size ).multiplyScalar( 0.5 );
 
-			this.min.copy( center ).sub( halfSize );
-			this.max.copy( center ).add( halfSize );
+		this.min.copy( center ).sub( halfSize );
+		this.max.copy( center ).add( halfSize );
 
-			return this;
-
-		};
+		return this;
 
-	}(),
+	},
 
 	setFromObject: function ( object ) {
 
@@ -215,47 +220,45 @@ Object.assign( Box3.prototype, {
 
 	},
 
-	expandByObject: function () {
-
-		// Computes the world-axis-aligned bounding box of an object (including its children),
-		// accounting for both the object's, and children's, world transforms
+	expandByObject: function ( object ) {
 
-		var scope, i, l;
+		if ( vector === undefined ) vector = new Vector3();
 
-		var v1 = new Vector3();
+		var i, l;
 
-		function traverse( node ) {
+		// Computes the world-axis-aligned bounding box of an object (including its children),
+		// accounting for both the object's, and children's, world transforms
 
-			var geometry = node.geometry;
+		object.updateWorldMatrix( false, false );
 
-			if ( geometry !== undefined ) {
+		var geometry = object.geometry;
 
-				if ( geometry.isGeometry ) {
+		if ( geometry !== undefined ) {
 
-					var vertices = geometry.vertices;
+			if ( geometry.isGeometry ) {
 
-					for ( i = 0, l = vertices.length; i < l; i ++ ) {
+				var vertices = geometry.vertices;
 
-						v1.copy( vertices[ i ] );
-						v1.applyMatrix4( node.matrixWorld );
+				for ( i = 0, l = vertices.length; i < l; i ++ ) {
 
-						scope.expandByPoint( v1 );
+					vector.copy( vertices[ i ] );
+					vector.applyMatrix4( object.matrixWorld );
 
-					}
+					this.expandByPoint( vector );
 
-				} else if ( geometry.isBufferGeometry ) {
+				}
 
-					var attribute = geometry.attributes.position;
+			} else if ( geometry.isBufferGeometry ) {
 
-					if ( attribute !== undefined ) {
+				var attribute = geometry.attributes.position;
 
-						for ( i = 0, l = attribute.count; i < l; i ++ ) {
+				if ( attribute !== undefined ) {
 
-							v1.fromBufferAttribute( attribute, i ).applyMatrix4( node.matrixWorld );
+					for ( i = 0, l = attribute.count; i < l; i ++ ) {
 
-							scope.expandByPoint( v1 );
+						vector.fromBufferAttribute( attribute, i ).applyMatrix4( object.matrixWorld );
 
-						}
+						this.expandByPoint( vector );
 
 					}
 
@@ -265,19 +268,19 @@ Object.assign( Box3.prototype, {
 
 		}
 
-		return function expandByObject( object ) {
+		//
 
-			scope = this;
+		var children = object.children;
 
-			object.updateMatrixWorld( true );
+		for ( i = 0, l = children.length; i < l; i ++ ) {
 
-			object.traverse( traverse );
+			this.expandByObject( children[ i ] );
 
-			return this;
+		}
 
-		};
+		return this;
 
-	}(),
+	},
 
 	containsPoint: function ( point ) {
 
@@ -324,21 +327,17 @@ Object.assign( Box3.prototype, {
 
 	},
 
-	intersectsSphere: ( function () {
-
-		var closestPoint = new Vector3();
+	intersectsSphere: function ( sphere ) {
 
-		return function intersectsSphere( sphere ) {
+		if ( vector === undefined ) vector = new Vector3();
 
-			// Find the point on the AABB closest to the sphere center.
-			this.clampPoint( sphere.center, closestPoint );
+		// Find the point on the AABB closest to the sphere center.
+		this.clampPoint( sphere.center, vector );
 
-			// If that point is inside the sphere, the AABB and sphere intersect.
-			return closestPoint.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
+		// If that point is inside the sphere, the AABB and sphere intersect.
+		return vector.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
 
-		};
-
-	} )(),
+	},
 
 	intersectsPlane: function ( plane ) {
 
@@ -387,106 +386,78 @@ Object.assign( Box3.prototype, {
 
 	},
 
-	intersectsTriangle: ( function () {
-
-		// triangle centered vertices
-		var v0 = new Vector3();
-		var v1 = new Vector3();
-		var v2 = new Vector3();
-
-		// triangle edge vectors
-		var f0 = new Vector3();
-		var f1 = new Vector3();
-		var f2 = new Vector3();
-
-		var testAxis = new Vector3();
-
-		var center = new Vector3();
-		var extents = new Vector3();
-
-		var triangleNormal = new Vector3();
-
-		function satForAxes( axes ) {
+	intersectsTriangle: function ( triangle ) {
 
-			var i, j;
+		if ( v0 === undefined ) {
 
-			for ( i = 0, j = axes.length - 3; i <= j; i += 3 ) {
+			// triangle centered vertices
 
-				testAxis.fromArray( axes, i );
-				// project the aabb onto the seperating axis
-				var r = extents.x * Math.abs( testAxis.x ) + extents.y * Math.abs( testAxis.y ) + extents.z * Math.abs( testAxis.z );
-				// project all 3 vertices of the triangle onto the seperating axis
-				var p0 = v0.dot( testAxis );
-				var p1 = v1.dot( testAxis );
-				var p2 = v2.dot( testAxis );
-				// actual test, basically see if either of the most extreme of the triangle points intersects r
-				if ( Math.max( - Math.max( p0, p1, p2 ), Math.min( p0, p1, p2 ) ) > r ) {
+			v0 = new Vector3();
+			v1 = new Vector3();
+			v2 = new Vector3();
 
-					// points of the projected triangle are outside the projected half-length of the aabb
-					// the axis is seperating and we can exit
-					return false;
+			// triangle edge vectors
 
-				}
-
-			}
+			f0 = new Vector3();
+			f1 = new Vector3();
+			f2 = new Vector3();
 
-			return true;
+			center = new Vector3();
+			extents = new Vector3();
+			triangleNormal = new Vector3();
 
 		}
 
-		return function intersectsTriangle( triangle ) {
-
-			if ( this.isEmpty() ) {
+		if ( this.isEmpty() ) {
 
-				return false;
+			return false;
 
-			}
+		}
 
-			// compute box center and extents
-			this.getCenter( center );
-			extents.subVectors( this.max, center );
-
-			// translate triangle to aabb origin
-			v0.subVectors( triangle.a, center );
-			v1.subVectors( triangle.b, center );
-			v2.subVectors( triangle.c, center );
-
-			// compute edge vectors for triangle
-			f0.subVectors( v1, v0 );
-			f1.subVectors( v2, v1 );
-			f2.subVectors( v0, v2 );
-
-			// test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb
-			// make an axis testing of each of the 3 sides of the aabb against each of the 3 sides of the triangle = 9 axis of separation
-			// axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned)
-			var axes = [
-				0, - f0.z, f0.y, 0, - f1.z, f1.y, 0, - f2.z, f2.y,
-				f0.z, 0, - f0.x, f1.z, 0, - f1.x, f2.z, 0, - f2.x,
-				- f0.y, f0.x, 0, - f1.y, f1.x, 0, - f2.y, f2.x, 0
-			];
-			if ( ! satForAxes( axes ) ) {
+		// compute box center and extents
+		this.getCenter( center );
+		extents.subVectors( this.max, center );
+
+		// translate triangle to aabb origin
+		v0.subVectors( triangle.a, center );
+		v1.subVectors( triangle.b, center );
+		v2.subVectors( triangle.c, center );
+
+		// compute edge vectors for triangle
+		f0.subVectors( v1, v0 );
+		f1.subVectors( v2, v1 );
+		f2.subVectors( v0, v2 );
+
+		// test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb
+		// make an axis testing of each of the 3 sides of the aabb against each of the 3 sides of the triangle = 9 axis of separation
+		// axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned)
+		var axes = [
+			0, - f0.z, f0.y, 0, - f1.z, f1.y, 0, - f2.z, f2.y,
+			f0.z, 0, - f0.x, f1.z, 0, - f1.x, f2.z, 0, - f2.x,
+			- f0.y, f0.x, 0, - f1.y, f1.x, 0, - f2.y, f2.x, 0
+		];
+		if ( ! satForAxes( axes, v0, v1, v2, extents ) ) {
 
-				return false;
+			return false;
 
-			}
+		}
 
-			// test 3 face normals from the aabb
-			axes = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ];
-			if ( ! satForAxes( axes ) ) {
+		// test 3 face normals from the aabb
+		axes = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ];
+		if ( ! satForAxes( axes, v0, v1, v2, extents ) ) {
 
-				return false;
+			return false;
 
-			}
+		}
 
-			// finally testing the face normal of the triangle
-			// use already existing triangle edge vectors here
-			triangleNormal.crossVectors( f0, f1 );
-			axes = [ triangleNormal.x, triangleNormal.y, triangleNormal.z ];
-			return satForAxes( axes );
+		// finally testing the face normal of the triangle
+		// use already existing triangle edge vectors here
+		triangleNormal.crossVectors( f0, f1 );
+		axes = [ triangleNormal.x, triangleNormal.y, triangleNormal.z ];
 
-		};
+		return satForAxes( axes, v0, v1, v2, extents );
 
-	} )(),
+	},
 
 	clampPoint: function ( point, target ) {
 
@@ -501,41 +472,34 @@ Object.assign( Box3.prototype, {
 
 	},
 
-	distanceToPoint: function () {
-
-		var v1 = new Vector3();
-
-		return function distanceToPoint( point ) {
-
-			var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
-			return clampedPoint.sub( point ).length();
+	distanceToPoint: function ( point ) {
 
-		};
+		if ( vector === undefined ) vector = new Vector3();
 
-	}(),
+		var clampedPoint = vector.copy( point ).clamp( this.min, this.max );
 
-	getBoundingSphere: function () {
+		return clampedPoint.sub( point ).length();
 
-		var v1 = new Vector3();
+	},
 
-		return function getBoundingSphere( target ) {
+	getBoundingSphere: function ( target ) {
 
-			if ( target === undefined ) {
+		if ( vector === undefined ) vector = new Vector3();
 
-				console.error( 'THREE.Box3: .getBoundingSphere() target is now required' );
-				//target = new Sphere(); // removed to avoid cyclic dependency
+		if ( target === undefined ) {
 
-			}
+			console.error( 'THREE.Box3: .getBoundingSphere() target is now required' );
+			//target = new Sphere(); // removed to avoid cyclic dependency
 
-			this.getCenter( target.center );
+		}
 
-			target.radius = this.getSize( v1 ).length() * 0.5;
+		this.getCenter( target.center );
 
-			return target;
+		target.radius = this.getSize( vector ).length() * 0.5;
 
-		};
+		return target;
 
-	}(),
+	},
 
 	intersect: function ( box ) {
 
@@ -558,41 +522,41 @@ Object.assign( Box3.prototype, {
 
 	},
 
-	applyMatrix4: function () {
-
-		var points = [
-			new Vector3(),
-			new Vector3(),
-			new Vector3(),
-			new Vector3(),
-			new Vector3(),
-			new Vector3(),
-			new Vector3(),
-			new Vector3()
-		];
+	applyMatrix4: function ( matrix ) {
 
-		return function applyMatrix4( matrix ) {
+		if ( points === undefined ) {
+
+			points = [
+				new Vector3(),
+				new Vector3(),
+				new Vector3(),
+				new Vector3(),
+				new Vector3(),
+				new Vector3(),
+				new Vector3(),
+				new Vector3()
+			];
 
-			// transform of empty box is an empty box.
-			if ( this.isEmpty() ) return this;
+		}
 
-			// NOTE: I am using a binary pattern to specify all 2^3 combinations below
-			points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
-			points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
-			points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
-			points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
-			points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
-			points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
-			points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
-			points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
+		// transform of empty box is an empty box.
+		if ( this.isEmpty() ) return this;
 
-			this.setFromPoints( points );
+		// NOTE: I am using a binary pattern to specify all 2^3 combinations below
+		points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
+		points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
+		points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
+		points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
+		points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
+		points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
+		points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
+		points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
 
-			return this;
+		this.setFromPoints( points );
 
-		};
+		return this;
 
-	}(),
+	},
 
 	translate: function ( offset ) {
 
@@ -611,5 +575,36 @@ Object.assign( Box3.prototype, {
 
 } );
 
+var testAxis;
+
+function satForAxes( axes, v0, v1, v2, extents ) {
+
+	if ( testAxis === undefined ) testAxis = new Vector3();
+
+	var i, j;
+
+	for ( i = 0, j = axes.length - 3; i <= j; i += 3 ) {
+
+		testAxis.fromArray( axes, i );
+		// project the aabb onto the seperating axis
+		var r = extents.x * Math.abs( testAxis.x ) + extents.y * Math.abs( testAxis.y ) + extents.z * Math.abs( testAxis.z );
+		// project all 3 vertices of the triangle onto the seperating axis
+		var p0 = v0.dot( testAxis );
+		var p1 = v1.dot( testAxis );
+		var p2 = v2.dot( testAxis );
+		// actual test, basically see if either of the most extreme of the triangle points intersects r
+		if ( Math.max( - Math.max( p0, p1, p2 ), Math.min( p0, p1, p2 ) ) > r ) {
+
+			// points of the projected triangle are outside the projected half-length of the aabb
+			// the axis is seperating and we can exit
+			return false;
+
+		}
+
+	}
+
+	return true;
+
+}
 
 export { Box3 };