Browse Source

expand Math.min/max per @alteredq recommendations, add clampSelf per @gero3

Ben Houston 12 years ago
parent
commit
2be1ad5bab
5 changed files with 167 additions and 49 deletions
  1. 1 18
      src/core/Box2.js
  2. 1 25
      src/core/Box3.js
  3. 47 0
      src/core/Vector2.js
  4. 46 6
      src/core/Vector3.js
  5. 72 0
      src/core/Vector4.js

+ 1 - 18
src/core/Box2.js

@@ -177,24 +177,7 @@ THREE.Box2.prototype = {
 
 
 	clampPoint: function ( point ) {
 	clampPoint: function ( point ) {
 
 
-		var p = new THREE.Vector2().copy( point );
-
-		// this requires less if's then a point.minSelf( this.max ).maxSelf( this.min )
-		if( p.x < this.min.x ) {
-			p.x = this.min.x;
-		}
-		else if( p.x > this.max.x ) {
-			p.x = this.max.x;
-		}
-
-		if( p.y < this.min.y ) {
-			p.y = this.min.y;
-		}
-		else if( p.y > this.max.y ) {
-			p.y = this.max.y;
-		}
-
-		return p;
+		return new THREE.Vector2().copy( point ).clampSelf( this.min, this.max );
 	},
 	},
 
 
 	distanceToPoint: function ( point ) {
 	distanceToPoint: function ( point ) {

+ 1 - 25
src/core/Box3.js

@@ -190,31 +190,7 @@ THREE.Box3.prototype = {
 
 
 	clampPoint: function ( point ) {
 	clampPoint: function ( point ) {
 
 
-		var p = new THREE.Vector3().copy( point );
-
-		// this requires less if's then a point.minSelf( this.max ).maxSelf( this.min )
-		if( p.x < this.min.x ) {
-			p.x = this.min.x;
-		}
-		else if( p.x > this.max.x ) {
-			p.x = this.max.x;
-		}
-
-		if( p.y < this.min.y ) {
-			p.y = this.min.y;
-		}
-		else if( p.y > this.max.y ) {
-			p.y = this.max.y;
-		}
-
-		if( p.z < this.min.z ) {
-			p.z = this.min.z;
-		}
-		else if( p.z > this.max.z ) {
-			p.z = this.max.z;
-		}
-
-		return p;
+		return new THREE.Vector3().copy( point ).clampSelf( this.min, this.max );
 	},
 	},
 
 
 	distanceToPoint: function ( point ) {
 	distanceToPoint: function ( point ) {

+ 47 - 0
src/core/Vector2.js

@@ -96,6 +96,53 @@ THREE.Vector2.prototype = {
 
 
 	},
 	},
 
 
+	minSelf: function ( v ) {
+
+		if( this.x > min.x ) {
+			this.x = min.x;
+		}
+		if( this.y > min.y ) {
+			this.y = min.y;
+		}
+
+		return this;
+
+	},
+
+	maxSelf: function ( v ) {
+
+		if( this.x < max.x ) {
+			this.x = max.x;
+		}
+		if( this.y < max.y ) {
+			this.y = max.y;
+		}
+
+		return this;
+
+	},
+
+	clampSelf: function ( min, max ) {
+
+		// This function assumes min < max, if this assumption isn't true it will not operate correctly
+
+		if( this.x < min.x ) {
+			this.x = min.x;
+		}
+		else if( this.x > max.x ) {
+			this.x = max.x;
+		}
+
+		if( this.y < min.y ) {
+			this.y = min.y;
+		}
+		else if( this.y > max.y ) {
+			this.y = max.y;
+		}
+
+		return this;
+	},
+
 	negate: function() {
 	negate: function() {
 
 
 		return this.multiplyScalar( - 1 );
 		return this.multiplyScalar( - 1 );

+ 46 - 6
src/core/Vector3.js

@@ -176,9 +176,15 @@ THREE.Vector3.prototype = {
 
 
 	minSelf: function ( v ) {
 	minSelf: function ( v ) {
 
 
-		this.x = Math.min( this.x, v.x );
-		this.y = Math.min( this.y, v.y );
-		this.z = Math.min( this.z, v.z );
+		if( this.x > min.x ) {
+			this.x = min.x;
+		}
+		if( this.y > min.y ) {
+			this.y = min.y;
+		}
+		if( this.z > min.z ) {
+			this.z = min.z;
+		}
 
 
 		return this;
 		return this;
 
 
@@ -186,14 +192,48 @@ THREE.Vector3.prototype = {
 
 
 	maxSelf: function ( v ) {
 	maxSelf: function ( v ) {
 
 
-		this.x = Math.max( this.x, v.x );
-		this.y = Math.max( this.y, v.y );
-		this.z = Math.max( this.z, v.z );
+		if( this.x < max.x ) {
+			this.x = max.x;
+		}
+		if( this.y < max.y ) {
+			this.y = max.y;
+		}
+		if( this.z < max.z ) {
+			this.z = max.z;
+		}
 
 
 		return this;
 		return this;
 
 
 	},
 	},
 
 
+	clampSelf: function ( min, max ) {
+
+		// This function assumes min < max, if this assumption isn't true it will not operate correctly
+
+		if( this.x < min.x ) {
+			this.x = min.x;
+		}
+		else if( this.x > max.x ) {
+			this.x = max.x;
+		}
+
+		if( this.y < min.y ) {
+			this.y = min.y;
+		}
+		else if( this.y > max.y ) {
+			this.y = max.y;
+		}
+
+		if( this.z < min.z ) {
+			this.z = min.z;
+		}
+		else if( this.z > max.z ) {
+			this.z = max.z;
+		}
+
+		return this;
+	},
+
 	negate: function() {
 	negate: function() {
 
 
 		return this.multiplyScalar( - 1 );
 		return this.multiplyScalar( - 1 );

+ 72 - 0
src/core/Vector4.js

@@ -118,6 +118,78 @@ THREE.Vector4.prototype = {
 
 
 	},
 	},
 
 
+	minSelf: function ( v ) {
+
+		if( this.x > min.x ) {
+			this.x = min.x;
+		}
+		if( this.y > min.y ) {
+			this.y = min.y;
+		}
+		if( this.z > min.z ) {
+			this.z = min.z;
+		}
+		if( this.w > min.w ) {
+			this.w = min.w;
+		}
+
+		return this;
+
+	},
+
+	maxSelf: function ( v ) {
+
+		if( this.x < max.x ) {
+			this.x = max.x;
+		}
+		if( this.y < max.y ) {
+			this.y = max.y;
+		}
+		if( this.z < max.z ) {
+			this.z = max.z;
+		}
+		if( this.w < max.w ) {
+			this.w = max.w;
+		}
+
+		return this;
+
+	},
+
+	clampSelf: function ( min, max ) {
+
+		// This function assumes min < max, if this assumption isn't true it will not operate correctly
+
+		if( this.x < min.x ) {
+			this.x = min.x;
+		}
+		else if( this.x > max.x ) {
+			this.x = max.x;
+		}
+
+		if( this.y < min.y ) {
+			this.y = min.y;
+		}
+		else if( this.y > max.y ) {
+			this.y = max.y;
+		}
+
+		if( this.z < min.z ) {
+			this.z = min.z;
+		}
+		else if( this.z > max.z ) {
+			this.z = max.z;
+		}
+
+		if( this.w < min.w ) {
+			this.w = min.w;
+		}
+		else if( this.w > max.w ) {
+			this.w = max.w;
+		}
+
+		return this;
+	},
 
 
 	negate: function() {
 	negate: function() {