Browse Source

Merge remote-tracking branch 'bhouston/vector-index-accessors' into dev

Mr.doob 12 years ago
parent
commit
f7c93b82cc

+ 1 - 1
src/math/Quaternion.js

@@ -201,7 +201,7 @@ THREE.Quaternion.prototype = {
 
 
 	length: function () {
 	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 );
 
 
 	},
 	},
 
 

+ 35 - 4
src/math/Vector2.js

@@ -41,6 +41,30 @@ THREE.Vector2.prototype = {
 
 
 	},
 	},
 
 
+
+    setComponent: function ( index, value ) {
+
+        switch( index ) {
+
+            case 0: this.x = value; break;
+            case 1: this.y = value; break;
+            default: throw new Error( "index is out of range: " + index );
+
+        }
+
+    },
+
+    getComponent: function ( index ) {
+
+        switch( index ) {
+
+            case 0: return this.x;
+            case 1: return this.y;
+            default: throw new Error( "index is out of range: " + index );
+
+    	}
+    },
+
 	copy: function ( v ) {
 	copy: function ( v ) {
 
 
 		this.x = v.x;
 		this.x = v.x;
@@ -106,7 +130,7 @@ THREE.Vector2.prototype = {
 
 
 	divideScalar: function ( s ) {
 	divideScalar: function ( s ) {
 
 
-		if ( s ) {
+		if ( s !== 0 ) {
 
 
 			this.x /= s;
 			this.x /= s;
 			this.y /= s;
 			this.y /= s;
@@ -205,7 +229,7 @@ THREE.Vector2.prototype = {
 
 
 	length: function () {
 	length: function () {
 
 
-		return Math.sqrt( this.lengthSq() );
+		return Math.sqrt( this.x * this.x + this.y * this.y );
 
 
 	},
 	},
 
 
@@ -230,7 +254,14 @@ THREE.Vector2.prototype = {
 
 
 	setLength: function ( l ) {
 	setLength: function ( l ) {
 
 
-		return this.normalize().multiplyScalar( l );
+		var oldLength = this.length();
+		
+		if ( oldLength !== 0 && l !== oldLength  ) {
+
+			this.multiplyScalar( l / oldLength );
+		}
+
+		return this;
 
 
 	},
 	},
 
 
@@ -255,4 +286,4 @@ THREE.Vector2.prototype = {
 
 
 	}
 	}
 
 
-};
+};

+ 37 - 4
src/math/Vector3.js

@@ -54,6 +54,32 @@ THREE.Vector3.prototype = {
 
 
 	},
 	},
 
 
+    setComponent: function ( index, value ) {
+
+        switch( index ) {
+
+            case 0: this.x = value; break;
+            case 1: this.y = value; break;
+            case 2: this.z = value; break;
+            default: throw new Error( "index is out of range: " + index );
+
+        }
+
+    },
+
+    getComponent: function ( index ) {
+
+        switch( index ) {
+
+            case 0: return this.x;
+            case 1: return this.y;
+            case 2: return this.z;
+            default: throw new Error( "index is out of range: " + index );
+
+        }
+
+    },
+
 	copy: function ( v ) {
 	copy: function ( v ) {
 
 
 		this.x = v.x;
 		this.x = v.x;
@@ -156,7 +182,7 @@ THREE.Vector3.prototype = {
 
 
 	divideScalar: function ( s ) {
 	divideScalar: function ( s ) {
 
 
-		if ( s ) {
+		if ( s !== 0 ) {
 
 
 			this.x /= s;
 			this.x /= s;
 			this.y /= s;
 			this.y /= s;
@@ -280,7 +306,7 @@ THREE.Vector3.prototype = {
 
 
 	length: function () {
 	length: function () {
 
 
-		return Math.sqrt( this.lengthSq() );
+		return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
 
 
 	},
 	},
 
 
@@ -298,7 +324,14 @@ THREE.Vector3.prototype = {
 
 
 	setLength: function ( l ) {
 	setLength: function ( l ) {
 
 
-		return this.normalize().multiplyScalar( l );
+		var oldLength = this.length();
+		
+		if ( oldLength !== 0 && l !== oldLength  ) {
+
+			this.multiplyScalar( l / oldLength );
+		}
+
+		return this;
 
 
 	},
 	},
 
 
@@ -571,4 +604,4 @@ THREE.Vector3.prototype = {
 
 
 	}
 	}
 
 
-};
+};

+ 40 - 5
src/math/Vector4.js

@@ -62,6 +62,34 @@ THREE.Vector4.prototype = {
 
 
 	},
 	},
 
 
+    setComponent: function ( index, value ) {
+
+        switch( index ) {
+
+            case 0: this.x = value; break;
+            case 1: this.y = value; break;
+            case 2: this.z = value; break;
+            case 3: this.w = value; break;
+            default: throw new Error( "index is out of range: " + index );
+
+        }
+
+    },
+
+    getComponent: function ( index ) {
+
+        switch( index ) {
+
+            case 0: return this.x;
+            case 1: return this.y;
+            case 2: return this.z;
+            case 3: return this.w;
+            default: throw new Error( "index is out of range: " + index );
+
+        }
+
+    },
+
 	copy: function ( v ) {
 	copy: function ( v ) {
 
 
 		this.x = v.x;
 		this.x = v.x;
@@ -141,7 +169,7 @@ THREE.Vector4.prototype = {
 
 
 	divideScalar: function ( s ) {
 	divideScalar: function ( s ) {
 
 
-		if ( s ) {
+		if ( s !== 0 ) {
 
 
 			this.x /= s;
 			this.x /= s;
 			this.y /= s;
 			this.y /= s;
@@ -283,13 +311,13 @@ THREE.Vector4.prototype = {
 
 
 	lengthSq: function () {
 	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 () {
 	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 +335,15 @@ THREE.Vector4.prototype = {
 
 
 	setLength: function ( l ) {
 	setLength: function ( l ) {
 
 
-		return this.normalize().multiplyScalar( l );
+		var oldLength = this.length();
+		
+		if ( oldLength !== 0 && l !== oldLength  ) {
 
 
+			this.multiplyScalar( l / oldLength );
+		}
+
+		return this;
+		
 	},
 	},
 
 
 	lerpSelf: function ( v, alpha ) {
 	lerpSelf: function ( v, alpha ) {
@@ -486,4 +521,4 @@ THREE.Vector4.prototype = {
 
 
 	}
 	}
 
 
-};
+};

+ 1 - 1
test/benchmark/benchmarking_float32array_accesspatterns.html → test/benchmark/benchmarking_float32array.html

@@ -30,7 +30,7 @@
 
 
   <!-- add class-based unit tests below -->
   <!-- add class-based unit tests below -->
 
 
-  <script src="core/Float32ArrayAccessPatterns.js"></script>
+  <script src="core/Float32Array.js"></script>
   
   
 </body>
 </body>
 </html>
 </html>

+ 20 - 0
test/benchmark/benchmarking_vector3components.html

@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>ThreeJS Benchmark Tests - Using Files in /src</title>
+</head>
+<body>
+
+  During this Benchmarking test the browser will be unresponsive.<br/><br/>
+
+  Benchmark output is written to the JavaScript console.  To access the JavaScript console presss Ctrl-Shift-J.
+
+  <script src="benchmark-1.0.0.js"></script>
+
+  <!-- add class-based unit tests below -->
+
+  <script src="core/Vector3Components.js"></script>
+  
+</body>
+</html>

+ 20 - 0
test/benchmark/benchmarking_vector3length.html

@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>ThreeJS Benchmark Tests - Using Files in /src</title>
+</head>
+<body>
+
+  During this Benchmarking test the browser will be unresponsive.<br/><br/>
+
+  Benchmark output is written to the JavaScript console.  To access the JavaScript console presss Ctrl-Shift-J.
+
+  <script src="benchmark-1.0.0.js"></script>
+
+  <!-- add class-based unit tests below -->
+
+  <script src="core/Vector3Length.js"></script>
+  
+</body>
+</html>

+ 20 - 0
test/benchmark/benchmarking_vector3storage.html

@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>ThreeJS Benchmark Tests - Using Files in /src</title>
+</head>
+<body>
+
+  During this Benchmarking test the browser will be unresponsive.<br/><br/>
+
+  Benchmark output is written to the JavaScript console.  To access the JavaScript console presss Ctrl-Shift-J.
+
+  <script src="benchmark-1.0.0.js"></script>
+
+  <!-- add class-based unit tests below -->
+
+  <script src="core/Vector3Storage.js"></script>
+  
+</body>
+</html>

+ 108 - 0
test/benchmark/core/Float32Array.js

@@ -0,0 +1,108 @@
+
+var input = new Float32Array( 10000 * 3 );
+var output = new Float32Array( 10000 * 3 );
+
+for( var j = 0, jl = input.length; j < jl; j ++ ) {
+	input[j] = j;
+}
+
+var inputVectors = [];
+var outputVectors = [];
+
+for( var j = 0, jl = input.length/3; j < jl; j ++ ) {
+	inputVectors.push( new THREE.Vector3( j*3, j*3+1, j*3+2 ) );
+	outputVectors.push( new THREE.Vector3() );
+}
+
+var suite = new Benchmark.Suite;
+
+suite.add('Float32Array-Float32Array', function() {
+	var value3 = new Float32Array( 3 );
+	for (var i = 0, il = input.length / 3; i < il; i += 3) {
+	  value3[0] = input[i + 0];
+	  value3[1] = input[i + 1];
+	  value3[2] = input[i + 2];
+	  value3[0] *= 1.01;
+	  value3[1] *= 1.03;
+	  value3[2] *= 0.98;
+	  output[i + 0] = value3[0];
+	  output[i + 1] = value3[1];
+	  output[i + 2] = value3[2];
+	}
+});
+
+suite.add('Float32Array-Array', function() {
+	var value2 = [0,0,0];
+	for (var i = 0, il = input.length / 3; i < il; i += 3) {
+	  value2[0] = input[i + 0];
+	  value2[1] = input[i + 1];
+	  value2[2] = input[i + 2];
+	  value2[0] *= 1.01;
+	  value2[1] *= 1.03;
+	  value2[2] *= 0.98;
+	  output[i + 0] = value2[0];
+	  output[i + 1] = value2[1];
+	  output[i + 2] = value2[2];
+	}
+});
+
+suite.add('Float32Array-Literal', function() {
+	var x, y, z;
+	for (var i = 0, il = input.length / 3; i < il; i += 3) {
+	  x = input[i + 0];
+	  y = input[i + 1];
+	  z = input[i + 2];
+	  x *= 1.01;
+	  y *= 1.03;
+	  z *= 0.98;
+	  output[i + 0] = x;
+	  output[i + 1] = y;
+	  output[i + 2] = z;
+	}
+});
+
+suite.add('Float32Array-Vector3', function() {
+	var value = new THREE.Vector3();
+	for (var i = 0, il = input.length / 3; i < il; i += 3) {
+	  value.x = input[i + 0];
+	  value.y = input[i + 1];
+	  value.z = input[i + 2];
+	  value.x *= 1.01;
+	  value.y *= 1.03;
+	  value.z *= 0.98;
+	  output[i + 0] = value.x;
+	  output[i + 1] = value.y;
+	  output[i + 2] = value.z;
+	}
+});
+
+suite.add('Vector3Array-Direct', function() {
+	for (var i = 0, il = inputVectors.length; i < il; i ++ ) {
+		outputVectors[i].copy( inputVectors[i] );
+		outputVectors[i].x *= 1.01;
+		outputVectors[i].y *= 1.03;
+		outputVectors[i].z *= 0.98;
+	}
+});
+
+suite.add('Vector3Array-Vector3', function() {
+	var value = new THREE.Vector3();
+	for (var i = 0, il = inputVectors.length; i < il; i ++ ) {
+	  value.copy( inputVectors[i] );
+	  value.x *= 1.01;
+	  value.y *= 1.03;
+	  value.z *= 0.98;
+	  outputVectors[i].copy( value );
+	}
+});
+
+suite.on('cycle', function(event, bench) {
+  console.log(String(event.target));
+});
+
+suite.on('complete', function() {
+  console.log('Fastest is ' + this.filter('fastest').pluck('name'));
+  console.log( "Done" );
+});
+
+suite.run(true);

+ 0 - 155
test/benchmark/core/Float32ArrayAccessPatterns.js

@@ -1,155 +0,0 @@
-
-var array = new Float32Array( 10000 * 3 );
-
-for( var j = 0, jl = array.length; j < jl; j ++ ) {
-	array[j] = j;
-}
-
-var vectorArray = [];
-
-for( var j = 0, jl = array.length/3; j < jl; j ++ ) {
-	vectorArray.push( new THREE.Vector3( j*3, j*3+1, j*3+2 ) );
-}
-
-var Float32ArrayCopyTest = function( array ) {
-	var x, y, z;
-	for (var i = 0, il = array.length / 3; i < il; i += 3) {
-	  x = array[i + 0];
-	  y = array[i + 1];
-	  z = array[i + 2];
-	  x *= 1.01;
-	  y *= 1.03;
-	  z *= 0.98;
-	  array[i + 0] = x;
-	  array[i + 1] = y;
-	  array[i + 2] = z;
-	}
-};
-
-var Float32ArrayDirectTest = function( array ) {
-	for (var i = 0, il = array.length / 3; i < il; i += 3) {
-	  array[i + 0] *= 1.01;
-	  array[i + 1] *= 1.03;
-	  array[i + 2] *= 0.98;
-	}
-};
-
-
-var Float32ArrayVector3CopyTest = function( array ) {
-	var value = new THREE.Vector3();
-	for (var i = 0, il = array.length / 3; i < il; i += 3) {
-	  value.x = array[i + 0];
-	  value.y = array[i + 1];
-	  value.z = array[i + 2];
-	  value.x *= 1.01;
-	  value.y *= 1.03;
-	  value.z *= 0.98;
-	  array[i + 0] = value.x;
-	  array[i + 1] = value.y;
-	  array[i + 2] = value.z;
-	}
-};
-
-var Float32ArrayArrayCopyTest = function( array ) {
-	var value2 = [0,0,0];
-	for (var i = 0, il = array.length / 3; i < il; i += 3) {
-	  value2[0] = array[i + 0];
-	  value2[1] = array[i + 1];
-	  value2[2] = array[i + 2];
-	  value2[0] *= 1.01;
-	  value2[1] *= 1.03;
-	  value2[2] *= 0.98;
-	  array[i + 0] = value2[0];
-	  array[i + 1] = value2[1];
-	  array[i + 2] = value2[2];
-	}
-};
-
-var Float32ArrayFloat32ArrayCopyTest = function( array ) {
-	var value3 = new Float32Array( 3 );
-	for (var i = 0, il = array.length / 3; i < il; i += 3) {
-	  value3[0] = array[i + 0];
-	  value3[1] = array[i + 1];
-	  value3[2] = array[i + 2];
-	  value3[0] *= 1.01;
-	  value3[1] *= 1.03;
-	  value3[2] *= 0.98;
-	  array[i + 0] = value3[0];
-	  array[i + 1] = value3[1];
-	  array[i + 2] = value3[2];
-	}
-};
-
-
-var Vector3ArrayVector3CopyTest = function( array ) {
-	var value = new THREE.Vector3();
-	for (var i = 0, il = vectorArray.length; i < il; i ++ ) {
-	  value.copy( vectorArray[i] );
-	  value.x *= 1.01;
-	  value.y *= 1.03;
-	  value.z *= 0.98;
-	  vectorArray[i].copy( value );
-	}
-};
-
-var Vector3ArrayVector3RefTest = function( array ) {
-	for (var i = 0, il = vectorArray.length; i < il; i ++ ) {
-	  var value = vectorArray[i];
-	  value.x *= 1.01;
-	  value.y *= 1.03;
-	  value.z *= 0.98;
-	}
-};
-
-var Vector3ArrayVector3DirectTest = function( array ) {
-	for (var i = 0, il = vectorArray.length; i < il; i ++ ) {
-	  vectorArray[i].x *= 1.01;
-	  vectorArray[i].y *= 1.03;
-	  vectorArray[i].z *= 0.98;
-	}
-};
-
-var suite = new Benchmark.Suite;
-
-suite.add('Float32ArrayFloat32ArrayCopyTest', function() {
-  Float32ArrayFloat32ArrayCopyTest( array );
-});
-
-suite.add('Float32DirectArray', function() {
-  Float32ArrayDirectTest( array );
-});
-
-suite.add('Float32ArrayArrayCopyTest', function() {
-  Float32ArrayArrayCopyTest( array );
-});
-
-suite.add('Float32CopyArray', function() {
-  Float32ArrayCopyTest( array );
-});
-
-suite.add('Float32ArrayVector3CopyTest', function() {
-  Float32ArrayVector3CopyTest( array );
-});
-
-suite.add('Vector3ArrayVector3Ref', function() {
-  Vector3ArrayVector3RefTest( array );
-});
-
-suite.add('Vector3ArrayVector3Direct', function() {
-  Vector3ArrayVector3DirectTest( array );
-});
-
-suite.add('Vector3ArrayVector3Copy', function() {
-  Vector3ArrayVector3CopyTest( array );
-});
-
-suite.on('cycle', function(event, bench) {
-  console.log(String(event.target));
-});
-
-suite.on('complete', function() {
-  console.log('Fastest is ' + this.filter('fastest').pluck('name'));
-  console.log( "Done" );
-});
-
-suite.run(true);

+ 149 - 0
test/benchmark/core/Vector3Components.js

@@ -0,0 +1,149 @@
+THREE = {};
+    
+THREE.Vector3 = function ( x, y, z ) {
+
+    this.x = x || 0;
+    this.y = y || 0;
+    this.z = z || 0;
+
+};
+
+THREE.Vector3.prototype = {
+
+    constructor: THREE.Vector3,
+ 
+    setComponent: function ( index, value ) {
+
+        this[ THREE.Vector3.__indexToName[ index ] ] = value;
+        
+    },
+
+    getComponent: function ( index ) {
+
+        return this[ THREE.Vector3.__indexToName[ index ] ];
+
+    }, 
+
+
+
+    setComponent2: function ( index, value ) {
+
+        switch( index ) {
+
+            case 0: this.x = value; break;
+            case 1: this.y = value; break;
+            case 2: this.z = value; break;
+            default: throw new Error( "index is out of range: " + index );
+
+        }
+
+    },
+
+    getComponent2: function ( index ) {
+
+        switch( index ) {
+
+            case 0: return this.x;
+            case 1: return this.y;
+            case 2: return this.z;
+            default: throw new Error( "index is out of range: " + index );
+
+        }
+
+    },
+
+
+    getComponent3: function ( index ) {
+
+        if ( index === 0 ) return this.x;
+        if ( index === 1 ) return this.y;
+        if ( index === 2 ) return this.z;
+
+        throw new Error( "index is out of range: " + index );
+
+    },
+
+    getComponent4: function ( index ) {
+
+        if ( index === 0 ) return this.x;
+        else if ( index === 1 ) return this.y;
+        else if ( index === 2 ) return this.z;
+        else throw new Error( "index is out of range: " + index );
+
+    }
+};
+
+
+THREE.Vector3.__indexToName = {
+  0: 'x',
+  1: 'y',
+  2: 'z'
+};
+
+var a = [];
+
+for ( var i = 0; i < 100000; i ++ ) {
+    
+    a[ i ] = new THREE.Vector3( i * 0.01, i * 2, i * -1.3 );
+    
+}
+
+
+var suite = new Benchmark.Suite;
+
+suite.add('IndexToName', function() {
+
+    var result = 0;
+
+    for ( var i = 0; i < 100000; i ++ ) {
+        result += a[i].getComponent( i % 3 );
+    }
+
+});
+
+suite.add('SwitchStatement', function() {
+
+    var result = 0;
+
+    for ( var i = 0; i < 100000; i ++ ) {
+
+        result += a[i].getComponent2( i % 3 );
+
+    }
+
+});
+
+suite.add('IfAndReturnSeries', function() {
+
+    var result = 0;
+
+    for ( var i = 0; i < 100000; i ++ ) {
+
+        result += a[i].getComponent3( i % 3 );
+
+    }
+
+});
+
+suite.add('IfReturnElseSeries', function() {
+
+    var result = 0;
+
+    for ( var i = 0; i < 100000; i ++ ) {
+
+        result += a[i].getComponent4( i % 3 );
+
+    }
+
+});
+
+suite.on('cycle', function(event, bench) {
+  console.log(String(event.target));
+});
+
+suite.on('complete', function() {
+  console.log('Fastest is ' + this.filter('fastest').pluck('name'));
+  console.log( "Done" );
+});
+
+suite.run(true);

+ 88 - 0
test/benchmark/core/Vector3Length.js

@@ -0,0 +1,88 @@
+THREE = {};
+    
+THREE.Vector3 = function ( x, y, z ) {
+
+    this.x = x || 0;
+    this.y = y || 0;
+    this.z = z || 0;
+
+};
+
+THREE.Vector3.prototype = {
+
+    constructor: THREE.Vector3,
+    
+    lengthSq: function () {
+
+            return this.x * this.x + this.y * this.y + this.z * this.z;
+
+    },
+
+    length: function () {
+
+            return Math.sqrt( this.lengthSq() );
+
+    },
+
+    length2: function () {
+
+            return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
+
+    }
+    
+};
+
+var a = [];
+
+for ( var i = 0; i < 100000; i ++ ) {
+    
+    a[ i ] = new THREE.Vector3( i * 0.01, i * 2, i * -1.3 );
+    
+}
+
+
+var suite = new Benchmark.Suite;
+
+suite.add('NoCallTest', function() {
+
+    var result = 0;
+
+    for ( var i = 0; i < 100000; i ++ ) {
+        var v = a[i];
+        result += Math.sqrt( v.x * v.x + v.y * v.y + v.z * v.z );
+    }
+
+});
+
+suite.add('InlineCallTest', function() {
+
+    var result = 0;
+
+    for ( var i = 0; i < 100000; i ++ ) {
+
+        result += a[ i ].length2();
+
+    }
+
+});
+
+suite.add('FunctionCallTest', function() {
+    var result = 0;
+
+    for ( var i = 0; i < 100000; i ++ ) {
+
+        result += a[ i ].length();
+
+    }
+});
+
+suite.on('cycle', function(event, bench) {
+  console.log(String(event.target));
+});
+
+suite.on('complete', function() {
+  console.log('Fastest is ' + this.filter('fastest').pluck('name'));
+  console.log( "Done" );
+});
+
+suite.run(true);

+ 120 - 0
test/benchmark/core/Vector3Storage.js

@@ -0,0 +1,120 @@
+THREE = {};
+    
+THREE.Vector3 = function ( x, y, z ) {
+
+    this.x = x || 0;
+    this.y = y || 0;
+    this.z = z || 0;
+
+};
+
+THREE.Vector3.prototype = {
+
+    constructor: THREE.Vector3,
+    
+    length: function () {
+
+            return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
+
+    }
+    
+};
+
+THREE.Vector3X = function ( x, y, z ) {
+
+    var elements = this.elements = new Float32Array( 3 );
+    elements[0] = x || 0;
+    elements[1] = y || 1;
+    elements[2] = z || 2;
+
+};
+
+THREE.Vector3X.prototype = {
+
+    constructor: THREE.Vector3X,
+    
+    length: function () {
+
+            return Math.sqrt( this.elements[0] * this.elements[0] + this.elements[1] * this.elements[1] + this.elements[2] * this.elements[2] );
+
+    }
+    
+};
+
+
+THREE.Vector3Y = function ( x, y, z ) {
+
+    this.elements = [ x || 0, y || 1, z || 2 ];
+
+};
+
+THREE.Vector3Y.prototype = {
+
+    constructor: THREE.Vector3Y,
+    
+    length: function () {
+
+            return Math.sqrt( this.elements[0] * this.elements[0] + this.elements[1] * this.elements[1] + this.elements[2] * this.elements[2] );
+
+    }
+    
+};
+
+
+var suite = new Benchmark.Suite;
+
+suite.add('Vector3-Set', function() {
+
+    var array = [];
+    for ( var i = 0; i < 100000; i ++ ) {
+        var v = new THREE.Vector3( i, i, i );
+        array.push( v );
+    }
+
+    var result = 0;
+    for ( var i = 0; i < 100000; i ++ ) {
+        var v = array[i];
+        result += v.length();
+    }
+});
+
+suite.add('Vector3-Float32Array', function() {
+
+    var array = [];
+    for ( var i = 0; i < 100000; i ++ ) {
+        var v = new THREE.Vector3X( i, i, i );
+        array.push( v );
+    }
+
+    var result = 0;
+    for ( var i = 0; i < 100000; i ++ ) {
+        var v = array[i];
+        result += v.length();
+    }
+});
+
+suite.add('Vector3-Array', function() {
+
+    var array = [];
+    for ( var i = 0; i < 100000; i ++ ) {
+        var v = new THREE.Vector3Y( i, i, i );
+        array.push( v );
+    }
+
+    var result = 0;
+    for ( var i = 0; i < 100000; i ++ ) {
+        var v = array[i];
+        result += v.length();
+    }
+});
+
+suite.on('cycle', function(event, bench) {
+  console.log(String(event.target));
+});
+
+suite.on('complete', function() {
+  console.log('Fastest is ' + this.filter('fastest').pluck('name'));
+  console.log( "Done" );
+});
+
+suite.run(true);

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

@@ -48,6 +48,17 @@ test( "setX,setY", function() {
 	ok( a.y == y, "Passed!" );
 	ok( a.y == y, "Passed!" );
 });
 });
 
 
+test( "setComponent,getComponent", function() {
+	var a = new THREE.Vector2();
+	ok( a.x == 0, "Passed!" );
+	ok( a.y == 0, "Passed!" );
+
+	a.setComponent( 0, 1 );
+	a.setComponent( 1, 2 );
+	ok( a.getComponent( 0 ) == 1, "Passed!" );
+	ok( a.getComponent( 1 ) == 2, "Passed!" );
+});
+
 test( "add", function() {
 test( "add", function() {
 	var a = new THREE.Vector2( x, y );
 	var a = new THREE.Vector2( x, y );
 	var b = new THREE.Vector2( -x, -y );
 	var b = new THREE.Vector2( -x, -y );
@@ -184,6 +195,11 @@ test( "setLength", function() {
 	ok( a.length() == x, "Passed!" );
 	ok( a.length() == x, "Passed!" );
 	a.setLength( y );
 	a.setLength( y );
 	ok( a.length() == y, "Passed!" );
 	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() {
 test( "lerpSelf/clone", function() {

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

@@ -59,6 +59,20 @@ test( "setX,setY,setZ", function() {
 	ok( a.z == z, "Passed!" );
 	ok( a.z == z, "Passed!" );
 });
 });
 
 
+test( "setComponent,getComponent", function() {
+	var a = new THREE.Vector3();
+	ok( a.x == 0, "Passed!" );
+	ok( a.y == 0, "Passed!" );
+	ok( a.z == 0, "Passed!" );
+
+	a.setComponent( 0, 1 );
+	a.setComponent( 1, 2 );
+	a.setComponent( 2, 3 );
+	ok( a.getComponent( 0 ) == 1, "Passed!" );
+	ok( a.getComponent( 1 ) == 2, "Passed!" );
+	ok( a.getComponent( 2 ) == 3, "Passed!" );
+});
+
 test( "add", function() {
 test( "add", function() {
 	var a = new THREE.Vector3( x, y, z );
 	var a = new THREE.Vector3( x, y, z );
 	var b = new THREE.Vector3( -x, -y, -z );
 	var b = new THREE.Vector3( -x, -y, -z );
@@ -217,6 +231,12 @@ test( "setLength", function() {
 	ok( a.length() == x, "Passed!" );
 	ok( a.length() == x, "Passed!" );
 	a.setLength( y );
 	a.setLength( y );
 	ok( a.length() == y, "Passed!" );
 	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() {
 test( "lerpSelf/clone", function() {

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

@@ -69,6 +69,23 @@ test( "setX,setY,setZ,setW", function() {
 	ok( a.w == w, "Passed!" );
 	ok( a.w == w, "Passed!" );
 });
 });
 
 
+test( "setComponent,getComponent", function() {
+	var a = new THREE.Vector4();
+	ok( a.x == 0, "Passed!" );
+	ok( a.y == 0, "Passed!" );
+	ok( a.z == 0, "Passed!" );
+	ok( a.w == 1, "Passed!" );
+
+	a.setComponent( 0, 1 );
+	a.setComponent( 1, 2 );
+	a.setComponent( 2, 3 );
+	a.setComponent( 3, 4 );
+	ok( a.getComponent( 0 ) == 1, "Passed!" );
+	ok( a.getComponent( 1 ) == 2, "Passed!" );
+	ok( a.getComponent( 2 ) == 3, "Passed!" );
+	ok( a.getComponent( 3 ) == 4, "Passed!" );
+});
+
 test( "add", function() {
 test( "add", function() {
 	var a = new THREE.Vector4( x, y, z, w );
 	var a = new THREE.Vector4( x, y, z, w );
 	var b = new THREE.Vector4( -x, -y, -z, -w );
 	var b = new THREE.Vector4( -x, -y, -z, -w );
@@ -254,6 +271,11 @@ test( "setLength", function() {
 	ok( a.length() == x, "Passed!" );
 	ok( a.length() == x, "Passed!" );
 	a.setLength( y );
 	a.setLength( y );
 	ok( a.length() == y, "Passed!" );
 	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() {
 test( "lerpSelf/clone", function() {