From 7f19c7e00c104e4c9af8474f9bff57b1639eee5b Mon Sep 17 00:00:00 2001 From: Mike Biddlecombe Date: Wed, 25 Jul 2018 18:34:07 -0700 Subject: [PATCH] Add an element-wise 'abs' method for vectors --- spec/gl-matrix/vec2-spec.js | 19 +++++++++++++++++++ spec/gl-matrix/vec3-spec.js | 19 +++++++++++++++++++ spec/gl-matrix/vec4-spec.js | 19 +++++++++++++++++++ src/vec2.js | 13 +++++++++++++ src/vec3.js | 14 ++++++++++++++ src/vec4.js | 15 +++++++++++++++ 6 files changed, 99 insertions(+) diff --git a/spec/gl-matrix/vec2-spec.js b/spec/gl-matrix/vec2-spec.js index 795d684b..14e8c123 100644 --- a/spec/gl-matrix/vec2-spec.js +++ b/spec/gl-matrix/vec2-spec.js @@ -242,6 +242,25 @@ describe("vec2", function() { }); }); + describe("abs", function() { + beforeEach(function() { vecA = [-1, -2]; }); + + describe("with a separate output vector", function() { + beforeEach(function() { result = vec2.abs(out, vecA); }); + + it("should place values into out", function() { expect(out).toBeEqualish([1, 2]); }); + it("should return out", function() { expect(result).toBe(out); }); + it("should not modify vecA", function() { expect(vecA).toBeEqualish([-1, -2]); }); + }); + + describe("when vecA is the output vector", function() { + beforeEach(function() { result = vec2.abs(vecA, vecA); }); + + it("should place values into vecA", function() { expect(vecA).toBeEqualish([1, 2]); }); + it("should return vecA", function() { expect(result).toBe(vecA); }); + }); + }); + describe("round", function() { beforeEach(function() { vecA = [Math.E, Math.PI]; }); diff --git a/spec/gl-matrix/vec3-spec.js b/spec/gl-matrix/vec3-spec.js index 47ad0236..323d5bfa 100644 --- a/spec/gl-matrix/vec3-spec.js +++ b/spec/gl-matrix/vec3-spec.js @@ -383,6 +383,25 @@ describe("vec3", function() { }); }); + describe("abs", function() { + beforeEach(function() { vecA = [-1, -2, -3]; }); + + describe("with a separate output vector", function() { + beforeEach(function() { result = vec3.abs(out, vecA); }); + + it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3]); }); + it("should return out", function() { expect(result).toBe(out); }); + it("should not modify vecA", function() { expect(vecA).toBeEqualish([-1, -2, -3]); }); + }); + + describe("when vecA is the output vector", function() { + beforeEach(function() { result = vec3.abs(vecA, vecA); }); + + it("should place values into vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); }); + it("should return vecA", function() { expect(result).toBe(vecA); }); + }); + }); + describe("round", function() { beforeEach(function() { vecA = [Math.E, Math.PI, Math.SQRT2]; }); diff --git a/spec/gl-matrix/vec4-spec.js b/spec/gl-matrix/vec4-spec.js index 901fb6df..eb4dfc81 100644 --- a/spec/gl-matrix/vec4-spec.js +++ b/spec/gl-matrix/vec4-spec.js @@ -243,6 +243,25 @@ describe("vec4", function() { }); }); + describe("abs", function() { + beforeEach(function() { vecA = [-1, -2, -3, -4]; }); + + describe("with a separate output vector", function() { + beforeEach(function() { result = vec4.abs(out, vecA); }); + + it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3, 4]); }); + it("should return out", function() { expect(result).toBe(out); }); + it("should not modify vecA", function() { expect(vecA).toBeEqualish([-1, -2, -3, -4]); }); + }); + + describe("when vecA is the output vector", function() { + beforeEach(function() { result = vec4.abs(vecA, vecA); }); + + it("should place values into vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); }); + it("should return vecA", function() { expect(result).toBe(vecA); }); + }); + }); + describe("round", function() { beforeEach(function() { vecA = [Math.E, Math.PI, Math.SQRT2, Math.SQRT1_2]; }); diff --git a/src/vec2.js b/src/vec2.js index b0d3204e..b7f63e86 100644 --- a/src/vec2.js +++ b/src/vec2.js @@ -183,6 +183,19 @@ export function max(out, a, b) { return out; } +/** + * Math.abs the components of a vec2 + * + * @param {vec2} out the receiving vector + * @param {vec2} a vector to abs + * @returns {vec2} out + */ +export function abs(out, a) { + out[0] = Math.abs(a[0]); + out[1] = Math.abs(a[1]); + return out; +} + /** * Math.round the components of a vec2 * diff --git a/src/vec3.js b/src/vec3.js index 271f5277..ada4cdd5 100644 --- a/src/vec3.js +++ b/src/vec3.js @@ -211,6 +211,20 @@ export function max(out, a, b) { return out; } +/** + * Math.abs the components of a vec3 + * + * @param {vec3} out the receiving vector + * @param {vec3} a vector to abs + * @returns {vec3} out + */ +export function abs(out, a) { + out[0] = Math.abs(a[0]); + out[1] = Math.abs(a[1]); + out[2] = Math.abs(a[2]); + return out; +} + /** * Math.round the components of a vec3 * diff --git a/src/vec4.js b/src/vec4.js index b502d0dd..3e9127e3 100644 --- a/src/vec4.js +++ b/src/vec4.js @@ -213,6 +213,21 @@ export function max(out, a, b) { return out; } +/** + * Math.abs the components of a vec4 + * + * @param {vec4} out the receiving vector + * @param {vec4} a vector to abs + * @returns {vec4} out + */ +export function abs(out, a) { + out[0] = Math.abs(a[0]); + out[1] = Math.abs(a[1]); + out[2] = Math.abs(a[2]); + out[3] = Math.abs(a[3]); + return out; +} + /** * Math.round the components of a vec4 *