瀏覽代碼

Use clamp for clampScalar implementation

Tom Whittock 11 年之前
父節點
當前提交
8375a6f421
共有 4 個文件被更改,包括 122 次插入117 次删除
  1. 39 40
      src/math/Vector2.js
  2. 43 50
      src/math/Vector3.js
  3. 14 27
      src/math/Vector4.js
  4. 26 0
      test/unit/math/Vector2.js

+ 39 - 40
src/math/Vector2.js

@@ -226,46 +226,45 @@ THREE.Vector2.prototype = {
 		return this;
 		return this;
 	},
 	},
 
 
-    clampScalar: function ( minVal, maxVal ) {
-
-        if ( this.x < minVal ) {
-            this.x = minVal;
-        } else if ( this.x > maxVal ) {
-            this.x = maxVal;
-        }
-
-        if ( this.y < minVal ) {
-            this.y = minVal;
-        } else if ( this.y > maxVal ) {
-            this.y = maxVal;
-        }
-
-        return this;
-    },
-
-    floor: function() {
-        this.x = Math.floor(this.x);
-        this.y = Math.floor(this.y);
-        return this;
-    },
-
-    ceil: function() {
-        this.x = Math.ceil(this.x);
-        this.y = Math.ceil(this.y);
-        return this;
-    },
-
-    round: function() {
-        this.x = Math.round(this.x);
-        this.y = Math.round(this.y);
-        return this;
-    },
-
-    roundToZero: function() {
-        this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
-        this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
-        return this;
-    },
+	clampScalar: ( function () {
+		var min, max;
+
+		return function ( minVal, maxVal ) {
+			if ( !min || !max ) {
+				min = new THREE.Vector2();
+				max = new THREE.Vector2();
+			}
+
+			min.set(minVal, minVal);
+			max.set(maxVal, maxVal);
+			return this.clamp(min, max);
+
+		};
+	} )(),
+
+	floor: function() {
+		this.x = Math.floor(this.x);
+		this.y = Math.floor(this.y);
+		return this;
+	},
+
+	ceil: function() {
+		this.x = Math.ceil(this.x);
+		this.y = Math.ceil(this.y);
+		return this;
+	},
+
+	round: function() {
+		this.x = Math.round(this.x);
+		this.y = Math.round(this.y);
+		return this;
+	},
+
+	roundToZero: function() {
+		this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
+		this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
+		return this;
+	},
 
 
 	negate: function() {
 	negate: function() {
 
 

+ 43 - 50
src/math/Vector3.js

@@ -404,56 +404,49 @@ THREE.Vector3.prototype = {
 
 
 	},
 	},
 
 
-    clampScalar: function ( minVal, maxVal ) {
-
-        if ( this.x < minVal ) {
-            this.x = minVal;
-        } else if ( this.x > maxVal ) {
-            this.x = maxVal;
-        }
-
-        if ( this.y < minVal ) {
-            this.y = minVal;
-        } else if ( this.y > maxVal ) {
-            this.y = maxVal;
-        }
-
-        if ( this.z < minVal ) {
-            this.z = minVal;
-        } else if ( this.z > maxVal ) {
-            this.z = maxVal;
-        }
-
-        return this;
-    },
-
-    floor: function() {
-        this.x = Math.floor(this.x);
-        this.y = Math.floor(this.y);
-        this.z = Math.floor(this.z);
-        return this;
-    },
-
-    ceil: function() {
-        this.x = Math.ceil(this.x);
-        this.y = Math.ceil(this.y);
-        this.z = Math.ceil(this.z);
-        return this;
-    },
-
-    round: function() {
-        this.x = Math.round(this.x);
-        this.y = Math.round(this.y);
-        this.z = Math.round(this.z);
-        return this;
-    },
-
-    roundToZero: function() {
-        this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
-        this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
-        this.z = (this.z < 0) ? Math.ceil(this.z) : Math.floor(this.z);
-        return this;
-    },
+	clampScalar: ( function () {
+		var min, max;
+
+		return function ( minVal, maxVal ) {
+			if ( !min || !max ) {
+				min = new THREE.Vector3();
+				max = new THREE.Vector3();
+			}
+
+			min.set(minVal, minVal, minVal);
+			max.set(maxVal, maxVal, maxVal);
+			return this.clamp(min, max);
+
+		};
+	} )(),
+
+	floor: function() {
+		this.x = Math.floor(this.x);
+		this.y = Math.floor(this.y);
+		this.z = Math.floor(this.z);
+		return this;
+	},
+
+	ceil: function() {
+		this.x = Math.ceil(this.x);
+		this.y = Math.ceil(this.y);
+		this.z = Math.ceil(this.z);
+		return this;
+	},
+
+	round: function() {
+		this.x = Math.round(this.x);
+		this.y = Math.round(this.y);
+		this.z = Math.round(this.z);
+		return this;
+	},
+
+	roundToZero: function() {
+		this.x = (this.x < 0) ? Math.ceil(this.x) : Math.floor(this.x);
+		this.y = (this.y < 0) ? Math.ceil(this.y) : Math.floor(this.y);
+		this.z = (this.z < 0) ? Math.ceil(this.z) : Math.floor(this.z);
+		return this;
+	},
 
 
 	negate: function () {
 	negate: function () {
 
 

+ 14 - 27
src/math/Vector4.js

@@ -483,34 +483,21 @@ THREE.Vector4.prototype = {
 
 
 	},
 	},
 
 
-    clampScalar: function ( minVal, maxVal ) {
-
-        if ( this.x < minVal ) {
-            this.x = minVal;
-        } else if ( this.x > maxVal ) {
-            this.x = maxVal;
-        }
-
-        if ( this.y < minVal ) {
-            this.y = minVal;
-        } else if ( this.y > maxVal ) {
-            this.y = maxVal;
-        }
-
-        if ( this.z < minVal ) {
-            this.z = minVal;
-        } else if ( this.z > maxVal ) {
-            this.z = maxVal;
-        }
-
-        if ( this.w < minVal ) {
-            this.w = minVal;
-        } else if ( this.w > maxVal ) {
-            this.w = maxVal;
-        }
+	clampScalar: ( function () {
+		var min, max;
 
 
-        return this;
-    },
+		return function ( minVal, maxVal ) {
+			if ( !min || !max ) {
+				min = new THREE.Vector4();
+				max = new THREE.Vector4();
+			}
+
+			min.set(minVal, minVal, minVal, minVal);
+			max.set(maxVal, maxVal, maxVal, maxVal);
+			return this.clamp(min, max);
+
+		};
+	} )(),
 
 
     floor: function() {
     floor: function() {
         this.x = Math.floor(this.x);
         this.x = Math.floor(this.x);

+ 26 - 0
test/unit/math/Vector2.js

@@ -124,6 +124,32 @@ test( "min/max/clamp", function() {
 	c.clamp( b, a );
 	c.clamp( b, a );
 	ok( c.x == -x, "Passed!" );
 	ok( c.x == -x, "Passed!" );
 	ok( c.y == y, "Passed!" );
 	ok( c.y == y, "Passed!" );
+
+	c.set(-2*x, 2*x);
+	c.clampScalar( -x, x );
+	equal( c.x, -x, "scalar clamp x" );
+	equal( c.y,  x, "scalar clamp y" );
+});
+
+test( "rounding", function() {
+	deepEqual( new THREE.Vector2( -0.1, 0.1 ).floor(), new THREE.Vector2( -1, 0 ), "floor .1" );
+	deepEqual( new THREE.Vector2( -0.5, 0.5 ).floor(), new THREE.Vector2( -1, 0 ), "floor .5" );
+	deepEqual( new THREE.Vector2( -0.9, 0.9 ).floor(), new THREE.Vector2( -1, 0 ), "floor .9" );
+
+	deepEqual( new THREE.Vector2( -0.1, 0.1 ).ceil(), new THREE.Vector2( 0, 1 ), "ceil .1" );
+	deepEqual( new THREE.Vector2( -0.5, 0.5 ).ceil(), new THREE.Vector2( 0, 1 ), "ceil .5" );
+	deepEqual( new THREE.Vector2( -0.9, 0.9 ).ceil(), new THREE.Vector2( 0, 1 ), "ceil .9" );
+
+	deepEqual( new THREE.Vector2( -0.1, 0.1 ).round(), new THREE.Vector2( 0, 0 ), "round .1" );
+	deepEqual( new THREE.Vector2( -0.5, 0.5 ).round(), new THREE.Vector2( 0, 1 ), "round .5" );
+	deepEqual( new THREE.Vector2( -0.9, 0.9 ).round(), new THREE.Vector2( -1, 1 ), "round .9" );
+
+	deepEqual( new THREE.Vector2( -0.1, 0.1 ).roundToZero(), new THREE.Vector2( 0, 0 ), "roundToZero .1" );
+	deepEqual( new THREE.Vector2( -0.5, 0.5 ).roundToZero(), new THREE.Vector2( 0, 0 ), "roundToZero .5" );
+	deepEqual( new THREE.Vector2( -0.9, 0.9 ).roundToZero(), new THREE.Vector2( 0, 0 ), "roundToZero .9" );
+	deepEqual( new THREE.Vector2( -1.1, 1.1 ).roundToZero(), new THREE.Vector2( -1, 1 ), "roundToZero 1.1" );
+	deepEqual( new THREE.Vector2( -1.5, 1.5 ).roundToZero(), new THREE.Vector2( -1, 1 ), "roundToZero 1.5" );
+	deepEqual( new THREE.Vector2( -1.9, 1.9 ).roundToZero(), new THREE.Vector2( -1, 1 ), "roundToZero 1.9" );
 });
 });
 
 
 test( "negate", function() {
 test( "negate", function() {