# toji/gl-matrix

mat2.multiply, mat3.multiplyVec2

1 parent 99a6ced commit f783ba2a0df52790d9b34e67edb123008a464c5d sinisterchipmunk committed May 3, 2012
Showing with 75 additions and 3 deletions.
1. +39 −0 gl-matrix.js
2. +17 −0 spec/javascripts/mat2_spec.js
3. +19 −3 spec/javascripts/mat3_spec.js
39 gl-matrix.js
 @@ -529,6 +529,23 @@ }; /** + * Transforms the vec2 according to the given 3x3 matrix. + * + * @param {mat3} matrix the 3x3 matrix to multiply against + * @param {vec2} vec the vector to multiply + * @param {vec2} [dest] an optional receiving vector. If not given, vec is used. + * + * @returns {vec2} The multiplication result + **/ + mat3.multiplyVec2 = function(matrix, vec, dest) { + if (!dest) dest = vec; + var x = vec[0], y = vec[1]; + dest[0] = x * matrix[0] + y * matrix[3] + matrix[6]; + dest[1] = x * matrix[1] + y * matrix[4] + matrix[7]; + return dest; + }; + + /** * Transforms the vec3 according to this rotation matrix. * * @param {mat3} matrix the 3x3 matrix to multiply against @@ -2643,6 +2660,28 @@ return dest; } + /** + * Performs a matrix multiplication + * + * @param {mat2} matA First operand + * @param {mat2} matB Second operand + * @param {mat2} [dest] mat2 receiving operation result. If not specified result is written to matA + * + * @returns {mat2} dest if specified, matA otherwise + */ + mat2.multiply = function (matA, matB, dest) { + if (!dest) { dest = matA; } + var a11 = matA[0], + a12 = matA[1], + a21 = matA[2], + a22 = matA[3]; + dest[0] = a11 * matB[0] + a12 * matB[2]; + dest[1] = a11 * matB[1] + a12 * matB[3]; + dest[2] = a21 * matB[0] + a22 * matB[2]; + dest[3] = a21 * matB[1] + a22 * matB[3]; + return dest; + } + /* * Exports */
17 spec/javascripts/mat2_spec.js
 @@ -85,4 +85,21 @@ describe("mat2", function() { it("should return a", function() { expect(result).toBe(a); }); }); }); + + describe("multiply", function() { + describe("with dest", function() { + beforeEach(function() { result = mat2.multiply(a, b, dest); }); + it("should set dest", function() { expect(dest).toBeEqualish([19, 22, 43, 50]); }); + it("should return dest", function() { expect(result).toBe(dest); }); + it("should not modify a", function() { expect(a).toBeEqualish([1, 2, 3, 4]); }); + it("should not modify b", function() { expect(b).toBeEqualish([5, 6, 7, 8]); }); + }); + + describe("without dest", function() { + beforeEach(function() { result = mat2.multiply(a, b); }); + it("should set a", function() { expect(a).toBeEqualish([19, 22, 43, 50]); }); + it("should not change b", function() { expect(b).toBeEqualish([5, 6, 7, 8]); }); + it("should return a", function() { expect(result).toBe(a); }); + }); + }); });
22 spec/javascripts/mat3_spec.js
 @@ -1,11 +1,29 @@ describe("mat3", function() { - var mat, vec, dest; + var mat, vec, dest, result; beforeEach(function() { + mat = mat3.create([-1, 0, 1, 0, -1, 0, 1, 0, -1]); vec = vec3.create([1, 2, 3]); dest = vec3.create(); }); + describe("multiplyVec2", function() { + beforeEach(function() { vec = [1, 2]; dest = vec2.create() }); + + describe("with dest", function() { + beforeEach(function() { result = mat3.multiplyVec2(mat, vec, dest); }); + it("should return dest", function() { expect(result).toBe(dest); }); + it("should not alter vec", function() { expect(vec).toBeEqualish([1, 2]); }); + it("should set dest", function() { expect(dest).toBeEqualish([0, -2]); }); + }); + + describe("without dest", function() { + beforeEach(function() { result = mat3.multiplyVec2(mat, vec); }); + it("should return vec", function() { expect(result).toBe(vec); }); + it("should set vec", function() { expect(vec).toBeEqualish([0, -2]); }); + }); + }); + describe("multiplyVec3", function() { describe("when set to identity", function() { beforeEach(function() { mat = mat3.identity(mat3.create()); }); @@ -16,8 +34,6 @@ describe("mat3", function() { }); describe("with an arbitrary mat3", function() { - beforeEach(function() { mat = mat3.create([-1, 0, 1, 0, -1, 0, 1, 0, -1]); }); - describe("given a dest vec3", function() { it("should not modify incoming vec3", function() { mat3.multiplyVec3(mat, vec, vec3.create());