Skip to content

Commit

Permalink
Add Matrix2/3.multiplyByScale and a Matrix4.multiplyByScale test
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x committed Jun 9, 2015
1 parent 6236d65 commit 8b8c5b8
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Source/Core/Matrix2.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,41 @@ define([
return result;
};

/**
* Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
*
* @param {Matrix2} matrix The matrix on the left-hand side.
* @param {Cartesian2} scale The non-uniform scale on the right-hand side.
* @param {Matrix2} result The object onto which to store the result.
* @returns {Matrix2} The modified result parameter.
*
* @see Matrix2.fromScale
* @see Matrix2.multiplyByUniformScale
*
* @example
* // Instead of Cesium.Matrix2.multiply(m, Cesium.Matrix2.fromScale(scale), m);
* Cesium.Matrix2.multiplyByScale(m, scale, m);
*/
Matrix2.multiplyByScale = function(matrix, scale, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(matrix)) {
throw new DeveloperError('matrix is required');
}
if (!defined(scale)) {
throw new DeveloperError('scale is required');
}
if (!defined(result)) {
throw new DeveloperError('result is required');
}
//>>includeEnd('debug');

result[0] = matrix[0] * scale.x;
result[1] = matrix[1] * scale.x;
result[2] = matrix[2] * scale.y;
result[3] = matrix[3] * scale.y;
return result;
};

/**
* Creates a negated copy of the provided matrix.
*
Expand Down
40 changes: 40 additions & 0 deletions Source/Core/Matrix3.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,46 @@ define([
return result;
};

/**
* Computes the product of a matrix times a (non-uniform) scale, as if the scale were a scale matrix.
*
* @param {Matrix3} matrix The matrix on the left-hand side.
* @param {Cartesian3} scale The non-uniform scale on the right-hand side.
* @param {Matrix3} result The object onto which to store the result.
* @returns {Matrix3} The modified result parameter.
*
* @see Matrix3.fromScale
* @see Matrix3.multiplyByUniformScale
*
* @example
* // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
* Cesium.Matrix3.multiplyByScale(m, scale, m);
*/
Matrix3.multiplyByScale = function(matrix, scale, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(matrix)) {
throw new DeveloperError('matrix is required');
}
if (!defined(scale)) {
throw new DeveloperError('scale is required');
}
if (!defined(result)) {
throw new DeveloperError('result is required');
}
//>>includeEnd('debug');

result[0] = matrix[0] * scale.x;
result[1] = matrix[1] * scale.x;
result[2] = matrix[2] * scale.x;
result[3] = matrix[3] * scale.y;
result[4] = matrix[4] * scale.y;
result[5] = matrix[5] * scale.y;
result[6] = matrix[6] * scale.z;
result[7] = matrix[7] * scale.z;
result[8] = matrix[8] * scale.z;
return result;
};

/**
* Creates a negated copy of the provided matrix.
*
Expand Down
38 changes: 38 additions & 0 deletions Specs/Core/Matrix2Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,25 @@ defineSuite([
expect(left).toEqual(expected);
});

it('multiplyByScale works', function() {
var m = new Matrix2(2, 3, 6, 7);
var scale = new Cartesian2(2.0, 3.0);
var expected = Matrix2.multiply(m, Matrix2.fromScale(scale), new Matrix2());
var result = new Matrix2();
var returnedResult = Matrix2.multiplyByScale(m, scale, result);
expect(returnedResult).toBe(result);
expect(result).toEqual(expected);
});

it('multiplyByScale works with "this" result parameter', function() {
var m = new Matrix2(1, 2, 5, 6);
var scale = new Cartesian2(1.0, 2.0);
var expected = Matrix2.multiply(m, Matrix2.fromScale(scale), new Matrix2());
var returnedResult = Matrix2.multiplyByScale(m, scale, m);
expect(returnedResult).toBe(m);
expect(m).toEqual(expected);
});

it('multiplyByVector works', function() {
var left = new Matrix2(1, 2, 3, 4);
var right = new Cartesian2(5, 6);
Expand Down Expand Up @@ -656,6 +675,19 @@ defineSuite([
}).toThrowDeveloperError();
});

it('multiplyByScale throws with no matrix parameter', function() {
expect(function() {
Matrix2.multiplyByScale(undefined, new Cartesian2());
}).toThrowDeveloperError();
});

it('multiplyByScale throws with no scale parameter', function() {
var m = new Matrix2();
expect(function() {
Matrix2.multiplyByScale(m, undefined);
}).toThrowDeveloperError();
});

it('multiplyByVector throws with no matrix parameter', function() {
var cartesian = new Cartesian2();
expect(function() {
Expand Down Expand Up @@ -737,6 +769,12 @@ defineSuite([
}).toThrowDeveloperError();
});

it('multiplyByScale throws without result parameter', function() {
expect(function() {
Matrix2.multiplyByScale(new Matrix2(), new Cartesian2());
}).toThrowDeveloperError();
});

it('multiplyByVector throws without result parameter', function() {
expect(function() {
Matrix2.multiplyByVector(new Matrix2(), new Cartesian2());
Expand Down
38 changes: 38 additions & 0 deletions Specs/Core/Matrix3Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,25 @@ defineSuite([
expect(left).toEqual(expected);
});

it('multiplyByScale works', function() {
var m = new Matrix3(2, 3, 4, 6, 7, 8, 10, 11, 12);
var scale = new Cartesian3(2.0, 3.0, 4.0);
var expected = Matrix3.multiply(m, Matrix3.fromScale(scale), new Matrix3());
var result = new Matrix3();
var returnedResult = Matrix3.multiplyByScale(m, scale, result);
expect(returnedResult).toBe(result);
expect(result).toEqual(expected);
});

it('multiplyByScale works with "this" result parameter', function() {
var m = new Matrix3(1, 2, 3, 5, 6, 7, 9, 10, 11);
var scale = new Cartesian3(1.0, 2.0, 3.0);
var expected = Matrix3.multiply(m, Matrix3.fromScale(scale), new Matrix3());
var returnedResult = Matrix3.multiplyByScale(m, scale, m);
expect(returnedResult).toBe(m);
expect(m).toEqual(expected);
});

it('multiplyByVector works', function() {
var left = new Matrix3(1, 2, 3, 4, 5, 6, 7, 8, 9);
var right = new Cartesian3(10, 11, 12);
Expand Down Expand Up @@ -937,6 +956,19 @@ defineSuite([
}).toThrowDeveloperError();
});

it('multiplyByScale throws with no matrix parameter', function() {
expect(function() {
Matrix3.multiplyByScale(undefined, new Cartesian3());
}).toThrowDeveloperError();
});

it('multiplyByScale throws with no scale parameter', function() {
var m = new Matrix3();
expect(function() {
Matrix3.multiplyByScale(m, undefined);
}).toThrowDeveloperError();
});

it('multiplyByVector throws with no matrix parameter', function() {
var cartesian = new Cartesian3();
expect(function() {
Expand Down Expand Up @@ -1054,6 +1086,12 @@ defineSuite([
}).toThrowDeveloperError();
});

it('multiplyByScale throws without result parameter', function() {
expect(function() {
Matrix3.multiplyByScale(new Matrix3(), new Cartesian3());
}).toThrowDeveloperError();
});

it('multiplyByVector throws without result parameter', function() {
expect(function() {
Matrix3.multiplyByVector(new Matrix3(), new Cartesian3());
Expand Down
8 changes: 8 additions & 0 deletions Specs/Core/Matrix4Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,14 @@ defineSuite([
var returnedResult = Matrix4.multiplyByScale(m, scale, result);
expect(returnedResult).toBe(result);
expect(result).toEqual(expected);

m = new Matrix4(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0, 0, 0, 1);
scale = new Cartesian3(2.0, 3.0, 4.0);
expected = Matrix4.multiply(m, Matrix4.fromScale(scale), new Matrix4());
result = new Matrix4();
returnedResult = Matrix4.multiplyByScale(m, scale, result);
expect(returnedResult).toBe(result);
expect(result).toEqual(expected);
});

it('multiplyByScale works with "this" result parameter', function() {
Expand Down

0 comments on commit 8b8c5b8

Please sign in to comment.