2
0
Эх сурвалжийг харах

reworked Vector classes, no change in API, reordered methods, divide-by-zero prevention, removed use of .set() in methods for better performance

Eberhard Gräther 14 жил өмнө
parent
commit
c3436327ad
3 өөрчлөгдсөн 219 нэмэгдсэн , 248 устгасан
  1. 56 47
      src/core/Vector2.js
  2. 87 145
      src/core/Vector3.js
  3. 76 56
      src/core/Vector4.js

+ 56 - 47
src/core/Vector2.js

@@ -1,6 +1,7 @@
 /**
  * @author mr.doob / http://mrdoob.com/
  * @author philogb / http://blog.thejit.org/
+ * @author egraether / http://egraether.com/
  */
 
 THREE.Vector2 = function ( x, y ) {
@@ -27,38 +28,42 @@ THREE.Vector2.prototype = {
 
 	copy : function ( v ) {
 
-		this.set(
-
-			v.x,
-			v.y
-
-		);
+		this.x = v.x;
+		this.y = v.y;
 
 		return this;
 
 	},
 
-	addSelf : function ( v ) {
+	clone : function () {
 
-		this.set(
+		return new THREE.Vector2( this.x, this.y );
 
-			this.x + v.x,
-			this.y + v.y
+	},
 
-		);
+
+	add : function ( v1, v2 ) {
+
+		this.x = v1.x + v2.x;
+		this.y = v1.y + v2.y;
 
 		return this;
 
 	},
 
-	add : function ( v1, v2 ) {
+	addSelf : function ( v ) {
 
-		this.set(
+		this.x += v.x;
+		this.y += v.y;
 
-			v1.x + v2.x,
-			v1.y + v2.y
+		return this;
+
+	},
 
-		);
+	sub : function ( v1, v2 ) {
+
+		this.x = v1.x - v2.x;
+		this.y = v1.y - v2.y;
 
 		return this;
 
@@ -66,61 +71,55 @@ THREE.Vector2.prototype = {
 
 	subSelf : function ( v ) {
 
-		this.set(
-
-			this.x - v.x,
-			this.y - v.y
-
-		);
+		this.x -= v.x;
+		this.y -= v.y;
 
 		return this;
 
 	},
 
-	sub : function ( v1, v2 ) {
-
-		this.set(
-
-			v1.x - v2.x,
-			v1.y - v2.y
+	multiplyScalar : function ( s ) {
 
-		);
+		this.x *= s;
+		this.y *= s;
 
 		return this;
 
 	},
 
-	multiplyScalar : function ( s ) {
+	divideScalar : function ( s ) {
+
+		if ( s ) {
+
+			this.x /= s;
+			this.y /= s;
 
-		this.set(
+		} else {
 
-			this.x * s,
-			this.y * s
+			this.set( 0, 0 );
 
-		);
+		}
 
 		return this;
 
 	},
 
+
 	negate : function() {
 
-		this.set(
+		return this.multiplyScalar( -1 );
 
-			- this.x,
-			- this.y
+	},
 
-		);
+	dot : function ( v ) {
 
-		return this;
+		return this.x * v.x + this.y * v.y;
 
 	},
 
-	unit : function () {
-
-		this.multiplyScalar( 1 / this.length() );
+	lengthSq : function () {
 
-		return this;
+		return this.x * this.x + this.y * this.y;
 
 	},
 
@@ -130,15 +129,25 @@ THREE.Vector2.prototype = {
 
 	},
 
-	lengthSq : function () {
+	normalize : function () {
 
-		return this.x * this.x + this.y * this.y;
+		return this.divideScalar( this.length() );
 
 	},
 
-	clone : function () {
+	setLength : function ( l ) {
 
-		return new THREE.Vector2( this.x, this.y );
+		return this.normalize().multiplyScalar( l );
+
+	},
+
+	// deprecated: same as normalize
+	unit : function () {
+
+		return this.normalize();
+
+		// this.multiplyScalar( 1 / this.length() );
+		// return this;
 
 	}
 

+ 87 - 145
src/core/Vector3.js

@@ -3,6 +3,7 @@
  * @author kile / http://kile.stravaganza.org/
  * @author philogb / http://blog.thejit.org/
  * @author mikael emtinger / http://gomo.se/
+ * @author egraether / http://egraether.com/
  */
 
 THREE.Vector3 = function ( x, y, z ) {
@@ -32,73 +33,56 @@ THREE.Vector3.prototype = {
 
 	copy : function ( v ) {
 
-		this.set(
-
-			v.x,
-			v.y,
-			v.z
-
-		);
+		this.x = v.x;
+		this.y = v.y;
+		this.z = v.z;
 
 		return this;
 
 	},
 
+	clone : function () {
+
+		return new THREE.Vector3( this.x, this.y, this.z );
 
-	add : function ( a, b ) {
+	},
 
-		this.set(
 
-			a.x + b.x,
-			a.y + b.y,
-			a.z + b.z
+	add : function ( v1, v2 ) {
 
-		);
+		this.x = v1.x + v2.x;
+		this.y = v1.y + v2.y;
+		this.z = v1.z + v2.z;
 
 		return this;
 
 	},
 
-
 	addSelf : function ( v ) {
 
-		this.set(
-
-			this.x + v.x,
-			this.y + v.y,
-			this.z + v.z
-
-		);
+		this.x += v.x;
+		this.y += v.y;
+		this.z += v.z;
 
 		return this;
 
 	},
 
-
 	addScalar : function ( s ) {
 
-		this.set(
-
-			this.x + s,
-			this.y + s,
-			this.z + s
-
-		);
+		this.x += s;
+		this.y += s;
+		this.z += s;
 
 		return this;
 
 	},
 
+	sub : function ( v1, v2 ) {
 
-	sub : function ( a, b ) {
-
-		this.set(
-
-			a.x - b.x,
-			a.y - b.y,
-			a.z - b.z
-
-		);
+		this.x = v1.x - v2.x;
+		this.y = v1.y - v2.y;
+		this.z = v1.z - v2.z;
 
 		return this;
 
@@ -106,180 +90,151 @@ THREE.Vector3.prototype = {
 
 	subSelf : function ( v ) {
 
-		this.set(
-
-			this.x - v.x,
-			this.y - v.y,
-			this.z - v.z
-
-		);
+		this.x -= v.x;
+		this.y -= v.y;
+		this.z -= v.z;
 
 		return this;
 
 	},
 
+	multiply : function ( a, b ) {
 
-	cross : function ( a, b ) {
-
-		this.set(
-
-			a.y * b.z - a.z * b.y,
-			a.z * b.x - a.x * b.z,
-			a.x * b.y - a.y * b.x
-
-		);
+		this.x = a.x * b.x;
+		this.y = a.y * b.y;
+		this.z = a.z * b.z;
 
 		return this;
 
 	},
 
-	crossSelf : function ( v ) {
-
-		var tx = this.x, ty = this.y, tz = this.z;
-
-		this.set(
-
-			ty * v.z - tz * v.y,
-			tz * v.x - tx * v.z,
-			tx * v.y - ty * v.x
+	multiplySelf : function ( v ) {
 
-		);
+		this.x *= v.x;
+		this.y *= v.y;
+		this.z *= v.y;
 
 		return this;
 
 	},
 
-	multiply : function ( a, b ) {
-
-		this.set(
-
-			a.x * b.x,
-			a.y * b.y,
-			a.z * b.z
+	multiplyScalar : function ( s ) {
 
-		);
+		this.x *= s;
+		this.y *= s;
+		this.z *= s;
 
 		return this;
 
 	},
 
-	multiplySelf : function ( v ) {
-
-		this.set(
+	divideSelf : function ( v ) {
 
-			this.x * v.x,
-			this.y * v.y,
-			this.z * v.z
+		return this.divide( this, v );
 
-		);
+	},
 
-		return this;
+	divideScalar : function ( s ) {
 
-	},
+		if ( s ) {
 
-	multiplyScalar : function ( s ) {
+			this.x /= s;
+			this.y /= s;
+			this.z /= s;
 
-		this.set(
+		} else {
 
-			this.x * s,
-			this.y * s,
-			this.z * s
+			this.set( 0, 0, 0 );
 
-		);
+		}
 
 		return this;
 
 	},
 
-	divideSelf : function ( v ) {
-
-		this.set(
 
-			this.x / v.x,
-			this.y / v.y,
-			this.z / v.z
+	negate : function() {
 
-		);
-
-		return this;
+		return this.multiplyScalar( -1 );
 
 	},
 
-	divideScalar : function ( s ) {
+	dot : function ( v ) {
 
-		this.set(
+		return this.x * v.x + this.y * v.y + this.z * v.z;
 
-			this.x / s,
-			this.y / s,
-			this.z / s
+	},
 
-		);
+	lengthSq : function () {
 
-		return this;
+		return this.x * this.x + this.y * this.y + this.z * this.z;
 
 	},
 
-	negate : function () {
+	length : function () {
 
-		this.set(
+		return Math.sqrt( this.lengthSq() );
 
-			- this.x,
-			- this.y,
-			- this.z
+	},
 
-		);
+	lengthManhattan : function () {
 
-		return this;
+		// correct version
+		// return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
+
+		return this.x + this.y + this.z;
 
 	},
 
-	dot : function ( v ) {
+	normalize : function () {
 
-		return this.x * v.x + this.y * v.y + this.z * v.z;
+		return this.divideScalar( this.length() );
 
 	},
 
-	distanceTo : function ( v ) {
+	setLength : function ( l ) {
 
-		return Math.sqrt( this.distanceToSquared( v ) );
+		return this.normalize().multiplyScalar( l );
 
 	},
 
-	distanceToSquared : function ( v ) {
 
-		var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
-		return dx * dx + dy * dy + dz * dz;
+	cross : function ( a, b ) {
 
-	},
+		this.x = a.y * b.z - a.z * b.y;
+		this.y = a.z * b.x - a.x * b.z;
+		this.z = a.x * b.y - a.y * b.x;
 
-	length : function () {
-
-		return Math.sqrt( this.lengthSq() );
+		return this;
 
 	},
 
-	lengthSq : function () {
-
-		return this.x * this.x + this.y * this.y + this.z * this.z;
+	crossSelf : function ( v ) {
 
-	},
+		return this.set(
 
-	lengthManhattan : function () {
+			this.y * v.z - this.z * v.y,
+			this.z * v.x - this.x * v.z,
+			this.x * v.y - this.y * v.x
 
-		return this.x + this.y + this.z;
+		);
 
 	},
 
-	normalize : function () {
 
-		var l = this.length();
+	distanceTo : function ( v ) {
 
-		l > 0 ? this.multiplyScalar( 1 / l ) : this.set( 0, 0, 0 );
+		return Math.sqrt( this.distanceToSquared( v ) );
 
-		return this;
+	},
+
+	distanceToSquared : function ( v ) {
+
+		return new THREE.Vector3().sub( this, v ).lengthSq();
 
 	},
 
+
 	setPositionFromMatrix : function ( m ) {
 
 		this.x = m.n14;
@@ -308,22 +263,9 @@ THREE.Vector3.prototype = {
 
 	},
 
-	setLength : function ( l ) {
-
-		return this.normalize().multiplyScalar( l );
-
-	},
-
 	isZero : function () {
 
-		var almostZero = 0.0001;
-		return ( Math.abs( this.x ) < almostZero ) && ( Math.abs( this.y ) < almostZero ) && ( Math.abs( this.z ) < almostZero );
-
-	},
-
-	clone : function () {
-
-		return new THREE.Vector3( this.x, this.y, this.z );
+		return ( this.lengthSq() < 0.0001 /* almostZero */ );
 
 	}
 

+ 76 - 56
src/core/Vector4.js

@@ -2,6 +2,7 @@
  * @author supereggbert / http://www.paulbrunt.co.uk/
  * @author philogb / http://blog.thejit.org/
  * @author mikael emtinger / http://gomo.se/
+ * @author egraether / http://egraether.com/
  */
 
 THREE.Vector4 = function ( x, y, z, w ) {
@@ -32,7 +33,7 @@ THREE.Vector4.prototype = {
 
 	copy : function ( v ) {
 
-		this.set(
+		return this.set(
 
 			v.x,
 			v.y,
@@ -41,20 +42,21 @@ THREE.Vector4.prototype = {
 
 		);
 
-		return this;
-
 	},
 
-	add : function ( v1, v2 ) {
+	clone : function () {
 
-		this.set(
+		return new THREE.Vector4( this.x, this.y, this.z, this.w );
 
-			v1.x + v2.x,
-			v1.y + v2.y,
-			v1.z + v2.z,
-			v1.w + v2.w
+	},
 
-		);
+
+	add : function ( v1, v2 ) {
+
+		this.x = v1.x + v2.x;
+		this.y = v1.y + v2.y;
+		this.z = v1.z + v2.z;
+		this.w = v1.w + v2.w;
 
 		return this;
 
@@ -62,96 +64,114 @@ THREE.Vector4.prototype = {
 
 	addSelf : function ( v ) {
 
-		this.set(
+		this.x += v.x;
+		this.y += v.y;
+		this.z += v.z;
+		this.w += v.w;
 
-			this.x + v.x,
-			this.y + v.y,
-			this.z + v.z,
-			this.w + v.w
+		return this;
 
-		);
+	},
+
+	sub : function ( v1, v2 ) {
+
+		this.x = v1.x - v2.x;
+		this.y = v1.y - v2.y;
+		this.z = v1.z - v2.z;
+		this.w = v1.w - v2.w;
 
 		return this;
 
 	},
 
-	sub : function ( v1, v2 ) {
+	subSelf : function ( v ) {
+
+		this.x -= v.x;
+		this.y -= v.y;
+		this.z -= v.z;
+		this.w -= v.w;
 
-		this.set(
+		return this;
 
-			v1.x - v2.x,
-			v1.y - v2.y,
-			v1.z - v2.z,
-			v1.w - v2.w
+	},
 
-		);
+	multiplyScalar : function ( s ) {
+
+		this.x *= s;
+		this.y *= s;
+		this.z *= s;
+		this.w *= s;
 
 		return this;
 
 	},
 
-	subSelf : function ( v ) {
+	divideScalar : function ( s ) {
 
-		this.set(
+		if ( s ) {
 
-			this.x - v.x,
-			this.y - v.y,
-			this.z - v.z,
-			this.w - v.w
+			this.x /= s;
+			this.y /= s;
+			this.z /= s;
+			this.w /= s;
 
-		);
+		} else {
+
+			this.set( 0, 0, 0, 1 );
+
+		}
 
 		return this;
 
 	},
 
-	multiplyScalar : function ( s ) {
 
-		this.set(
+	negate : function() {
 
-			this.x * s,
-			this.y * s,
-			this.z * s,
-			this.w * s
+		return this.multiplyScalar( -1 );
 
-		);
+	},
 
-		return this;
+	dot : function ( v ) {
+
+		return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
 
 	},
 
-	divideScalar : function ( s ) {
+	lengthSq : function () {
 
-		this.set(
+		return this.dot( this );
 
-			this.x / s,
-			this.y / s,
-			this.z / s,
-			this.w / s
+	},
 
-		);
+	length : function () {
 
-		return this;
+		return Math.sqrt( this.lengthSq() );
 
 	},
 
-	lerpSelf : function ( v, alpha ) {
+	normalize : function () {
+
+		return this.divideScalar( this.length() );
 
-		this.set(
+	},
 
-			this.x + (v.x - this.x) * alpha,
-			this.y + (v.y - this.y) * alpha,
-			this.z + (v.z - this.z) * alpha,
-			this.w + (v.w - this.w) * alpha
+	setLength : function ( l ) {
 
-		);
+		return this.normalize().multiplyScalar( l );
 
 	},
 
-	clone : function () {
 
-		return new THREE.Vector4( this.x, this.y, this.z, this.w );
+	lerpSelf : function ( v, alpha ) {
+
+		this.x += (v.x - this.x) * alpha;
+		this.y += (v.y - this.y) * alpha;
+		this.z += (v.z - this.z) * alpha;
+		this.w += (v.w - this.w) * alpha;
 
-	}
+		return this;
+
+	},
 
 };