Skip to content

Commit

Permalink
Add an element-wise 'abs' method for vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Biddlecombe authored and Mike Biddlecombe committed Mar 6, 2022
1 parent 0bca31a commit 7f19c7e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
19 changes: 19 additions & 0 deletions spec/gl-matrix/vec2-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]; });

Expand Down
19 changes: 19 additions & 0 deletions spec/gl-matrix/vec3-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]; });

Expand Down
19 changes: 19 additions & 0 deletions spec/gl-matrix/vec4-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]; });

Expand Down
13 changes: 13 additions & 0 deletions src/vec2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
14 changes: 14 additions & 0 deletions src/vec3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
15 changes: 15 additions & 0 deletions src/vec4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 7f19c7e

Please sign in to comment.