提交 c3436327 编写于 作者: E Eberhard Gräther

reworked Vector classes, no change in API, reordered methods, divide-by-zero...

reworked Vector classes, no change in API, reordered methods, divide-by-zero prevention, removed use of .set() in methods for better performance
上级 95bb462e
/** /**
* @author mr.doob / http://mrdoob.com/ * @author mr.doob / http://mrdoob.com/
* @author philogb / http://blog.thejit.org/ * @author philogb / http://blog.thejit.org/
* @author egraether / http://egraether.com/
*/ */
THREE.Vector2 = function ( x, y ) { THREE.Vector2 = function ( x, y ) {
...@@ -27,38 +28,42 @@ THREE.Vector2.prototype = { ...@@ -27,38 +28,42 @@ THREE.Vector2.prototype = {
copy : function ( v ) { copy : function ( v ) {
this.set( this.x = v.x;
this.y = v.y;
v.x,
v.y
);
return this; 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; return this;
}, },
add : function ( v1, v2 ) { addSelf : function ( v ) {
this.set( this.x += v.x;
this.y += v.y;
v1.x + v2.x, return this;
v1.y + v2.y
},
); sub : function ( v1, v2 ) {
this.x = v1.x - v2.x;
this.y = v1.y - v2.y;
return this; return this;
...@@ -66,61 +71,55 @@ THREE.Vector2.prototype = { ...@@ -66,61 +71,55 @@ THREE.Vector2.prototype = {
subSelf : function ( v ) { subSelf : function ( v ) {
this.set( this.x -= v.x;
this.y -= v.y;
this.x - v.x,
this.y - v.y
);
return this; return this;
}, },
sub : function ( v1, v2 ) { multiplyScalar : function ( s ) {
this.set(
v1.x - v2.x,
v1.y - v2.y
); this.x *= s;
this.y *= s;
return this; return this;
}, },
multiplyScalar : function ( s ) { divideScalar : function ( s ) {
if ( s ) {
this.x /= s;
this.y /= s;
this.set( } else {
this.x * s, this.set( 0, 0 );
this.y * s
); }
return this; return this;
}, },
negate : function() { 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 () { lengthSq : function () {
this.multiplyScalar( 1 / this.length() );
return this; return this.x * this.x + this.y * this.y;
}, },
...@@ -130,15 +129,25 @@ THREE.Vector2.prototype = { ...@@ -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;
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
* @author kile / http://kile.stravaganza.org/ * @author kile / http://kile.stravaganza.org/
* @author philogb / http://blog.thejit.org/ * @author philogb / http://blog.thejit.org/
* @author mikael emtinger / http://gomo.se/ * @author mikael emtinger / http://gomo.se/
* @author egraether / http://egraether.com/
*/ */
THREE.Vector3 = function ( x, y, z ) { THREE.Vector3 = function ( x, y, z ) {
...@@ -32,73 +33,56 @@ THREE.Vector3.prototype = { ...@@ -32,73 +33,56 @@ THREE.Vector3.prototype = {
copy : function ( v ) { copy : function ( v ) {
this.set( this.x = v.x;
this.y = v.y;
v.x, this.z = v.z;
v.y,
v.z
);
return this; return this;
}, },
clone : function () {
return new THREE.Vector3( this.x, this.y, this.z );
add : function ( a, b ) { },
this.set(
a.x + b.x, add : function ( v1, v2 ) {
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; return this;
}, },
addSelf : function ( v ) { addSelf : function ( v ) {
this.set( this.x += v.x;
this.y += v.y;
this.x + v.x, this.z += v.z;
this.y + v.y,
this.z + v.z
);
return this; return this;
}, },
addScalar : function ( s ) { addScalar : function ( s ) {
this.set( this.x += s;
this.y += s;
this.x + s, this.z += s;
this.y + s,
this.z + s
);
return this; return this;
}, },
sub : function ( v1, v2 ) {
sub : function ( a, b ) { this.x = v1.x - v2.x;
this.y = v1.y - v2.y;
this.set( this.z = v1.z - v2.z;
a.x - b.x,
a.y - b.y,
a.z - b.z
);
return this; return this;
...@@ -106,180 +90,151 @@ THREE.Vector3.prototype = { ...@@ -106,180 +90,151 @@ THREE.Vector3.prototype = {
subSelf : function ( v ) { subSelf : function ( v ) {
this.set( this.x -= v.x;
this.y -= v.y;
this.x - v.x, this.z -= v.z;
this.y - v.y,
this.z - v.z
);
return this; return this;
}, },
multiply : function ( a, b ) {
cross : function ( a, b ) { this.x = a.x * b.x;
this.y = a.y * b.y;
this.set( this.z = a.z * b.z;
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
);
return this; return this;
}, },
crossSelf : function ( v ) { multiplySelf : 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
); this.x *= v.x;
this.y *= v.y;
this.z *= v.y;
return this; return this;
}, },
multiply : function ( a, b ) { multiplyScalar : function ( s ) {
this.set(
a.x * b.x,
a.y * b.y,
a.z * b.z
); this.x *= s;
this.y *= s;
this.z *= s;
return this; return this;
}, },
multiplySelf : function ( v ) { divideSelf : function ( v ) {
this.set(
this.x * v.x, return this.divide( this, v );
this.y * v.y,
this.z * v.z
); },
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.set( 0, 0, 0 );
this.y * s,
this.z * s
); }
return this; return this;
}, },
divideSelf : function ( v ) {
this.set(
this.x / v.x, negate : function() {
this.y / v.y,
this.z / v.z
); return this.multiplyScalar( -1 );
return this;
}, },
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; cross : function ( a, b ) {
return dx * dx + dy * dy + dz * dz;
}, 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 this;
return Math.sqrt( this.lengthSq() );
}, },
lengthSq : function () { crossSelf : function ( v ) {
return this.x * this.x + this.y * this.y + this.z * this.z;
}, 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 ) { setPositionFromMatrix : function ( m ) {
this.x = m.n14; this.x = m.n14;
...@@ -308,22 +263,9 @@ THREE.Vector3.prototype = { ...@@ -308,22 +263,9 @@ THREE.Vector3.prototype = {
}, },
setLength : function ( l ) {
return this.normalize().multiplyScalar( l );
},
isZero : function () { isZero : function () {
var almostZero = 0.0001; return ( this.lengthSq() < 0.0001 /* almostZero */ );
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 );
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
* @author supereggbert / http://www.paulbrunt.co.uk/ * @author supereggbert / http://www.paulbrunt.co.uk/
* @author philogb / http://blog.thejit.org/ * @author philogb / http://blog.thejit.org/
* @author mikael emtinger / http://gomo.se/ * @author mikael emtinger / http://gomo.se/
* @author egraether / http://egraether.com/
*/ */
THREE.Vector4 = function ( x, y, z, w ) { THREE.Vector4 = function ( x, y, z, w ) {
...@@ -32,7 +33,7 @@ THREE.Vector4.prototype = { ...@@ -32,7 +33,7 @@ THREE.Vector4.prototype = {
copy : function ( v ) { copy : function ( v ) {
this.set( return this.set(
v.x, v.x,
v.y, v.y,
...@@ -41,20 +42,21 @@ THREE.Vector4.prototype = { ...@@ -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; return this;
...@@ -62,96 +64,114 @@ THREE.Vector4.prototype = { ...@@ -62,96 +64,114 @@ THREE.Vector4.prototype = {
addSelf : function ( v ) { 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, return this;
this.y + v.y,
this.z + v.z,
this.w + v.w
); },
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; 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; return this;
}, },
subSelf : function ( v ) { divideScalar : function ( s ) {
this.set( if ( s ) {
this.x - v.x, this.x /= s;
this.y - v.y, this.y /= s;
this.z - v.z, this.z /= s;
this.w - v.w this.w /= s;
); } else {
this.set( 0, 0, 0, 1 );
}
return this; return this;
}, },
multiplyScalar : function ( s ) {
this.set( negate : function() {
this.x * s, return this.multiplyScalar( -1 );
this.y * s,
this.z * s,
this.w * s
); },
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, setLength : function ( l ) {
this.y + (v.y - this.y) * alpha,
this.z + (v.z - this.z) * alpha,
this.w + (v.w - this.w) * alpha
); 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;
},
}; };
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册