Skip to content

Commit

Permalink
Use only duck typing instead of 'instanceof' for alfador classes. Buf…
Browse files Browse the repository at this point in the history
…fed up some unit tests.
  • Loading branch information
kbirk committed Jun 2, 2015
1 parent a082d81 commit 1ffc014
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 229 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alfador",
"version": "0.4.4",
"version": "0.4.5",
"homepage": "https://github.com/kbirk/alfador",
"description": "A fast 3D math library for JavaScript",
"author": "Kevin Birk <birk.kevin@gmail.com>",
Expand Down
193 changes: 104 additions & 89 deletions build/alfador.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/alfador.min.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@
}

function handleError( err ) {
console.log( err.toString() );
process.exit(1);
console.error( err.toString() );
setTimeout( function() {
// set delay for full mocha error message
process.exit( 1 );
});
}

gulp.task('clean', function () {
Expand All @@ -69,7 +72,7 @@
.on( 'finish', function () {
return gulp.src( [ './test/*.js' ] )
.pipe( mocha( { reporter: 'list' } )
.on( 'error', handleError) ) // print mocha error message
.on( 'error', handleError ) ) // print mocha error message
.pipe( istanbul.writeReports() ); // Creating the reports after tests runned
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alfador",
"version": "0.4.4",
"version": "0.4.5",
"description": "A fast 3D math library for JavaScript",
"author": "Kevin Birk <birk.kevin@gmail.com>",
"main": "./src/exports.js",
Expand Down
60 changes: 36 additions & 24 deletions src/Mat33.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
*/
function Mat33( that ) {
if ( that ) {
if ( that instanceof Mat33 ) {
// copy data by value
this.data = that.data.slice( 0 );
} else if ( that.data && that.data.length === 16 ) {
// copy Mat44 data by value, account for index differences
this.data = [
that.data[0], that.data[1], that.data[2],
that.data[4], that.data[5], that.data[6],
that.data[8], that.data[9], that.data[10] ];
if ( that.data instanceof Array ) {
if ( that.data.length === 9 ) {
// copy Mat33 data by value
this.data = that.data.slice( 0 );
} else if ( that.data.length === 16 ) {
// copy Mat44 data by value, account for index differences
this.data = [
that.data[0], that.data[1], that.data[2],
that.data[4], that.data[5], that.data[6],
that.data[8], that.data[9], that.data[10] ];
}
} else if ( that.length === 9 ) {
// copy array by value, use prototype to cast array buffers
this.data = Array.prototype.slice.call( that );
Expand All @@ -30,7 +32,6 @@
} else {
return Mat33.identity();
}
return this;
}

/**
Expand Down Expand Up @@ -84,21 +85,21 @@
* @returns {Mat33} The scale matrix.
*/
Mat33.scale = function( scale ) {
if ( scale instanceof Array ) {
if ( typeof scale === "number" ) {
return new Mat33([
scale, 0, 0,
0, scale, 0,
0, 0, scale ]);
} else if ( scale instanceof Array ) {
return new Mat33([
scale[0], 0, 0,
0, scale[1], 0,
0, 0, scale[2] ]);
} else if ( scale instanceof Vec3 ) {
return new Mat33([
scale.x, 0, 0,
0, scale.y, 0,
0, 0, scale.z ]);
}
return new Mat33([
scale, 0, 0,
0, scale, 0,
0, 0, scale ]);
scale.x, 0, 0,
0, scale.y, 0,
0, 0, scale.z ]);
};

/**
Expand Down Expand Up @@ -293,7 +294,8 @@
*/
Mat33.prototype.multVector = function( that ) {
// ensure 'that' is a Vec3
that = ( that instanceof Vec3 ) ? that : new Vec3( that );
// it is safe to only cast if Array since the .w of a Vec4 is not used
that = ( that instanceof Array ) ? new Vec3( that ) : that;
return new Vec3({
x: this.data[0] * that.x + this.data[3] * that.y + this.data[6] * that.z,
y: this.data[1] * that.x + this.data[4] * that.y + this.data[7] * that.z,
Expand Down Expand Up @@ -332,7 +334,11 @@
var mat = new Mat33(),
i;
// ensure 'that' is a Mat33
that = ( that instanceof Mat33 ) ? that : new Mat33( that );
// must check if Array or Mat33
if ( ( that.data && that.data.length === 16 ) ||
that instanceof Array ) {
that = new Mat33( that );
}
for ( i=0; i<3; i++ ) {
mat.data[i] = this.data[i] * that.data[0] + this.data[i+3] * that.data[1] + this.data[i+6] * that.data[2];
mat.data[i+3] = this.data[i] * that.data[3] + this.data[i+3] * that.data[4] + this.data[i+6] * that.data[5];
Expand All @@ -351,18 +357,24 @@
*/
Mat33.prototype.mult = function( that ) {
if ( typeof that === "number" ) {
// scalar
return this.multScalar( that );
} else if ( that instanceof Array ) {
// array
if ( that.length === 3 || that.length === 4 ) {
return this.multVector( that );
} else {
return this.multMatrix( that );
}
} else if ( that instanceof Vec3 || that instanceof Vec4 ) {
}
// vector
if ( that.x !== undefined &&
that.y !== undefined &&
that.z !== undefined ) {
return this.multVector( that );
} else {
return this.multMatrix( that );
}
// matrix
return this.multMatrix( that );
};

/**
Expand Down
78 changes: 47 additions & 31 deletions src/Mat44.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
*/
function Mat44( that ) {
if ( that ) {
if ( that instanceof Mat33 ) {
// copy data by value, account for index differences
this.data = [
that.data[0], that.data[1], that.data[2], 0,
that.data[3], that.data[4], that.data[5], 0,
that.data[6], that.data[7], that.data[8], 0,
0, 0, 0, 1 ];
} else if ( that instanceof Mat44 ) {
// copy data by value
this.data = that.data.slice( 0 );
if ( that.data instanceof Array ) {
if ( that.data.length === 9 ) {
// copy Mat33 data by value, account for index differences
this.data = [
that.data[0], that.data[1], that.data[2], 0,
that.data[3], that.data[4], that.data[5], 0,
that.data[6], that.data[7], that.data[8], 0,
0, 0, 0, 1 ];
} else if ( that.data.length === 16 ) {
// copy Mat44 data by value
this.data = that.data.slice( 0 );
}
} else if ( that.length === 16 ) {
// copy array by value, use prototype to cast array buffers
this.data = Array.prototype.slice.call( that );
Expand All @@ -32,7 +34,6 @@
} else {
return Mat44.identity();
}
return this;
}

/**
Expand Down Expand Up @@ -90,23 +91,23 @@
* @returns {Mat44} The scale matrix.
*/
Mat44.scale = function( scale ) {
if ( scale instanceof Array ) {
return new Mat44(
[ scale[0], 0, 0, 0,
0, scale[1], 0, 0,
0, 0, scale[2], 0,
if ( typeof scale === "number" ) {
return new Mat44([
scale, 0, 0, 0,
0, scale, 0, 0,
0, 0, scale, 0,
0, 0, 0, 1 ]);
} else if ( scale instanceof Vec3 ) {
} else if ( scale instanceof Array ) {
return new Mat44([
scale.x, 0, 0, 0,
0, scale.y, 0, 0,
0, 0, scale.z, 0,
scale[0], 0, 0, 0,
0, scale[1], 0, 0,
0, 0, scale[2], 0,
0, 0, 0, 1 ]);
}
return new Mat44([
scale, 0, 0, 0,
0, scale, 0, 0,
0, 0, scale, 0,
scale.x, 0, 0, 0,
0, scale.y, 0, 0,
0, 0, scale.z, 0,
0, 0, 0, 1 ]);
};

Expand Down Expand Up @@ -220,7 +221,8 @@
*/
Mat44.prototype.multVector3 = function( that ) {
// ensure 'that' is a Vec3
that = ( that instanceof Vec3 ) ? that : new Vec3( that );
// it is safe to only cast if Array since Vec4 has own method
that = ( that instanceof Array ) ? new Vec3( that ) : that;
return new Vec3({
x: this.data[0] * that.x +
this.data[4] * that.y +
Expand All @@ -245,7 +247,8 @@
*/
Mat44.prototype.multVector4 = function( that ) {
// ensure 'that' is a Vec4
that = ( that instanceof Vec4 ) ? that : new Vec4( that );
// it is safe to only cast if Array since Vec3 has own method
that = ( that instanceof Array ) ? new Vec4( that ) : that;
return new Vec4({
x: this.data[0] * that.x +
this.data[4] * that.y +
Expand Down Expand Up @@ -297,7 +300,11 @@
var mat = new Mat44(),
i;
// ensure 'that' is a Mat44
that = ( that instanceof Mat44 ) ? that : new Mat44( that );
// must check if Array or Mat44
if ( ( that.data && that.data.length === 9 ) ||
that instanceof Array ) {
that = new Mat44( that );
}
for ( i=0; i<4; i++ ) {
mat.data[i] = this.data[i] * that.data[0] +
this.data[i+4] * that.data[1] +
Expand Down Expand Up @@ -329,22 +336,31 @@
*/
Mat44.prototype.mult = function( that ) {
if ( typeof that === "number" ) {
// scalar
return this.multScalar( that );
} else if ( that instanceof Array ) {
// array
if ( that.length === 3 ) {
return this.multVector3( that );
} else if ( that.length === 4 ) {
return this.multVector4( that );
} else {
return this.multMatrix( that );
}
} else if ( that instanceof Vec3 ) {
}
// vector
if ( that.x !== undefined &&
that.y !== undefined &&
that.z !== undefined ) {
if ( that.w !== undefined ) {
// vec4
return this.multVector4( that );
}
//vec3
return this.multVector3( that );
} else if ( that instanceof Vec4 ) {
return this.multVector4( that );
} else {
return this.multMatrix( that );
}
// matrix
return this.multMatrix( that );
};

/**
Expand Down
29 changes: 7 additions & 22 deletions src/Quaternion.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@
* a new Quaternion object.
* @memberof Quaternion
*
* @param {Quaternion} that - The quaterion to concatenate.
* @param {Quaternion|Array} that - The quaterion to concatenate.
*
* @returns {Quaternion} The resulting concatenated quaternion.
*/
Quaternion.prototype.multQuaternion = function( that ) {
Quaternion.prototype.mult = function( that ) {
that = ( that instanceof Array ) ? new Quaternion( that ) : that;
var w = (that.w * this.w) - (that.x * this.x) - (that.y * this.y) - (that.z * this.z),
x = this.y*that.z - this.z*that.y + this.w*that.x + this.x*that.w,
y = this.z*that.x - this.x*that.z + this.w*that.y + this.y*that.w,
Expand All @@ -76,33 +77,17 @@
* matrix to the provided vector, returning a new Vec3 object.
* @memberof Quaternion
*
* @param {Vec3|Vec4} that - The vector to rotate.
* @param {Vec3|Vec4|Array} that - The vector to rotate.
*
* @returns {Vec3} The resulting rotated vector.
*/
Quaternion.prototype.multVector = function( that ) {
Quaternion.prototype.rotate = function( that ) {
that = ( that instanceof Array ) ? new Vec3( that ) : that;
var vq = new Quaternion( 0, that.x, that.y, that.z ),
r = this.multQuaternion( vq ).multQuaternion( this.inverse() );
r = this.mult( vq ).mult( this.inverse() );
return new Vec3( r.x, r.y, r.z );
};

/**
* Multiples the quaternion by either a vector or quaternion
* argument.
* @memberof Quaternion
*
* @param {Vec3|Vec4|Quaternion} that - The vector or quaternion argument.
*
* @returns {Vec3|Quaternion} The multiplied result.
*/
Quaternion.prototype.mult = function( that ) {
if ( that instanceof Quaternion ) {
return this.multQuaternion( that );
} else {
return this.multVector( that );
}
};

/**
* Returns the rotation matrix that the quaternion represents.
* @memberof Quaternion
Expand Down
24 changes: 13 additions & 11 deletions src/Transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
*/
function Transform( that ) {
that = that || {};
if ( that instanceof Transform ) {
// copy transform by value
if ( that._up &&
that._forward &&
that._left &&
that._origin &&
that._scale ) {
// copy Transform by value
this._up = that.up();
this._forward = that.forward();
this._left = that.left();
this._origin = that.origin();
this._scale = that.scale();
} else if ( that instanceof Mat44 || that instanceof Mat33 ) {
// extract transform components from Mat44
} else if ( that.data && that.data instanceof Array ) {
// Mat33 or Mat44, extract transform components from Mat44
that = that.decompose();
this._up = that.up;
this._forward = that.forward;
Expand Down Expand Up @@ -180,15 +184,13 @@
* @returns {Transform} The resulting transform.
*/
Transform.prototype.mult = function( that ) {
if ( that instanceof Transform ) {
return new Transform( this.matrix().mult( that.matrix() ) );
} else if ( that instanceof Mat33 ||
that instanceof Mat44 ||
that instanceof Array ) {
if ( that instanceof Array ||
that.data instanceof Array ) {
// matrix or array
return new Transform( this.matrix().mult( that ) );
} else {
return Transform.identity();
}
// transform
return new Transform( this.matrix().mult( that.matrix() ) );
};

/**
Expand Down

0 comments on commit 1ffc014

Please sign in to comment.