瀏覽代碼

inline Vector2/3/4/Quaternion.length()/lengthSq() and optimize Vector2/3/4.setLength() with unit test coverage.

Ben Houston 12 年之前
父節點
當前提交
2613df2088
共有 7 個文件被更改,包括 45 次插入8 次删除
  1. 1 1
      src/math/Quaternion.js
  2. 9 2
      src/math/Vector2.js
  3. 9 2
      src/math/Vector3.js
  4. 10 3
      src/math/Vector4.js
  5. 5 0
      test/unit/math/Vector2.js
  6. 6 0
      test/unit/math/Vector3.js
  7. 5 0
      test/unit/math/Vector4.js

+ 1 - 1
src/math/Quaternion.js

@@ -201,7 +201,7 @@ THREE.Quaternion.prototype = {
 
 	length: function () {
 
-		return Math.sqrt( this.lengthSq() );
+		return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
 
 	},
 

+ 9 - 2
src/math/Vector2.js

@@ -205,7 +205,7 @@ THREE.Vector2.prototype = {
 
 	length: function () {
 
-		return Math.sqrt( this.lengthSq() );
+		return Math.sqrt( this.x * this.x + this.y * this.y );
 
 	},
 
@@ -230,7 +230,14 @@ THREE.Vector2.prototype = {
 
 	setLength: function ( l ) {
 
-		return this.normalize().multiplyScalar( l );
+		var oldLength = this.length();
+		
+		if( oldLength ) {
+
+			this.multiplyScalar( l / oldLength );
+		}
+
+		return this;
 
 	},
 

+ 9 - 2
src/math/Vector3.js

@@ -280,7 +280,7 @@ THREE.Vector3.prototype = {
 
 	length: function () {
 
-		return Math.sqrt( this.lengthSq() );
+		return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
 
 	},
 
@@ -298,7 +298,14 @@ THREE.Vector3.prototype = {
 
 	setLength: function ( l ) {
 
-		return this.normalize().multiplyScalar( l );
+		var oldLength = this.length();
+		
+		if( oldLength ) {
+
+			this.multiplyScalar( l / oldLength );
+		}
+
+		return this;
 
 	},
 

+ 10 - 3
src/math/Vector4.js

@@ -283,13 +283,13 @@ THREE.Vector4.prototype = {
 
 	lengthSq: function () {
 
-		return this.dot( this );
+		return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
 
 	},
 
 	length: function () {
 
-		return Math.sqrt( this.lengthSq() );
+		return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
 
 	},
 
@@ -307,8 +307,15 @@ THREE.Vector4.prototype = {
 
 	setLength: function ( l ) {
 
-		return this.normalize().multiplyScalar( l );
+		var oldLength = this.length();
+		
+		if( oldLength ) {
 
+			this.multiplyScalar( l / oldLength );
+		}
+
+		return this;
+		
 	},
 
 	lerpSelf: function ( v, alpha ) {

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

@@ -184,6 +184,11 @@ test( "setLength", function() {
 	ok( a.length() == x, "Passed!" );
 	a.setLength( y );
 	ok( a.length() == y, "Passed!" );
+
+	a = new THREE.Vector2( 0, 0 );
+	ok( a.length() == 0, "Passed!" );
+	a.setLength( y );
+	ok( a.length() == 0, "Passed!" );
 });
 
 test( "lerpSelf/clone", function() {

+ 6 - 0
test/unit/math/Vector3.js

@@ -217,6 +217,12 @@ test( "setLength", function() {
 	ok( a.length() == x, "Passed!" );
 	a.setLength( y );
 	ok( a.length() == y, "Passed!" );
+
+	a = new THREE.Vector3( 0, 0, 0 );
+	ok( a.length() == 0, "Passed!" );
+	a.setLength( y );
+	ok( a.length() == 0, "Passed!" );
+
 });
 
 test( "lerpSelf/clone", function() {

+ 5 - 0
test/unit/math/Vector4.js

@@ -254,6 +254,11 @@ test( "setLength", function() {
 	ok( a.length() == x, "Passed!" );
 	a.setLength( y );
 	ok( a.length() == y, "Passed!" );
+
+	a = new THREE.Vector4( 0, 0, 0, 0 );
+	ok( a.length() == 0, "Passed!" );
+	a.setLength( y );
+	ok( a.length() == 0, "Passed!" );
 });
 
 test( "lerpSelf/clone", function() {