Browse Source

proposed Vector3.minSelf, maxSelf - issue #2715

Ben Houston 12 years ago
parent
commit
acde547ebb
2 changed files with 24 additions and 20 deletions
  1. 5 20
      src/core/Box3.js
  2. 19 0
      src/core/Vector3.js

+ 5 - 20
src/core/Box3.js

@@ -52,25 +52,15 @@
 	};
 
 	THREE.Box3.prototype.extendByPoint = function ( point ) {
-		// TODO: replace by THREE.Vector3.min/max methods when they exist
-		this.min.x = Math.min( this.min.x, point.x );
-		this.min.y = Math.min( this.min.y, point.y );
-		this.min.z = Math.min( this.min.z, point.z );
-		this.max.x = Math.max( this.max.x, point.x );
-		this.max.y = Math.max( this.max.y, point.y );
-		this.max.z = Math.max( this.max.z, point.z );
+		this.min.minSelf( point );
+		this.max.maxSelf( point );
 		return this;
 	};
 
 	THREE.Box3.prototype.extendByBox = function ( box ) {
 		// Assumption: for speed purposes, we assume that the box is not empty, e.g. box.min < box.max
-		// TODO: replace by THREE.Vector3.min/max methods when they exist
-		this.min.x = Math.min( this.min.x, box.min.x );
-		this.min.y = Math.min( this.min.y, box.min.y );
-		this.min.z = Math.min( this.min.z, box.min.z );
-		this.max.x = Math.max( this.max.x, box.max.x );
-		this.max.y = Math.max( this.max.y, box.max.y );
-		this.max.z = Math.max( this.max.z, box.max.z );
+		this.min.minSelf( box.min );
+		this.max.maxSelf( box.max );
 		return this;
 	};
 
@@ -135,12 +125,7 @@
 	};
 
 	THREE.Box3.prototype.clampPoint = function ( point ) {
-		// TODO: replace by THREE.Vector3.min/max methods when they exist
-		return new THREE.Vector3(
-			Math.min( Math.max( this.min.x, point.x ), this.max.x ),
-			Math.min( Math.max( this.min.y, point.y ), this.max.y ),
-			Math.min( Math.max( this.min.z, point.z ), this.max.z )
-			);
+		return point.maxSelf( this.min ).minSelf( this.max );
 	};
 
 	THREE.Box3.prototype.distanceToPoint = function ( point ) {

+ 19 - 0
src/core/Vector3.js

@@ -174,6 +174,25 @@ THREE.Vector3.prototype = {
 
 	},
 
+	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 );
+
+		return this;
+
+	},
+
+	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 );
+
+		return this;
+
+	},
 
 	negate: function() {