Skip to content

Commit 02a74bf

Browse files
author
Mike Biddlecombe
committed
Add an element-wise 'abs' method for vectors
1 parent 44ec388 commit 02a74bf

File tree

6 files changed

+99
-0
lines changed

6 files changed

+99
-0
lines changed

spec/gl-matrix/vec2-spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,25 @@ describe("vec2", function() {
242242
});
243243
});
244244

245+
describe("abs", function() {
246+
beforeEach(function() { vecA = [-1, -2]; });
247+
248+
describe("with a separate output vector", function() {
249+
beforeEach(function() { result = vec2.abs(out, vecA); });
250+
251+
it("should place values into out", function() { expect(out).toBeEqualish([1, 2]); });
252+
it("should return out", function() { expect(result).toBe(out); });
253+
it("should not modify vecA", function() { expect(vecA).toBeEqualish([-1, -2]); });
254+
});
255+
256+
describe("when vecA is the output vector", function() {
257+
beforeEach(function() { result = vec2.abs(vecA, vecA); });
258+
259+
it("should place values into vecA", function() { expect(vecA).toBeEqualish([1, 2]); });
260+
it("should return vecA", function() { expect(result).toBe(vecA); });
261+
});
262+
});
263+
245264
describe("round", function() {
246265
beforeEach(function() { vecA = [Math.E, Math.PI]; });
247266

spec/gl-matrix/vec3-spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,25 @@ describe("vec3", function() {
383383
});
384384
});
385385

386+
describe("abs", function() {
387+
beforeEach(function() { vecA = [-1, -2, -3]; });
388+
389+
describe("with a separate output vector", function() {
390+
beforeEach(function() { result = vec3.abs(out, vecA); });
391+
392+
it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3]); });
393+
it("should return out", function() { expect(result).toBe(out); });
394+
it("should not modify vecA", function() { expect(vecA).toBeEqualish([-1, -2, -3]); });
395+
});
396+
397+
describe("when vecA is the output vector", function() {
398+
beforeEach(function() { result = vec3.abs(vecA, vecA); });
399+
400+
it("should place values into vecA", function() { expect(vecA).toBeEqualish([1, 2, 3]); });
401+
it("should return vecA", function() { expect(result).toBe(vecA); });
402+
});
403+
});
404+
386405
describe("round", function() {
387406
beforeEach(function() { vecA = [Math.E, Math.PI, Math.SQRT2]; });
388407

spec/gl-matrix/vec4-spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,25 @@ describe("vec4", function() {
243243
});
244244
});
245245

246+
describe("abs", function() {
247+
beforeEach(function() { vecA = [-1, -2, -3, -4]; });
248+
249+
describe("with a separate output vector", function() {
250+
beforeEach(function() { result = vec4.abs(out, vecA); });
251+
252+
it("should place values into out", function() { expect(out).toBeEqualish([1, 2, 3, 4]); });
253+
it("should return out", function() { expect(result).toBe(out); });
254+
it("should not modify vecA", function() { expect(vecA).toBeEqualish([-1, -2, -3, -4]); });
255+
});
256+
257+
describe("when vecA is the output vector", function() {
258+
beforeEach(function() { result = vec4.abs(vecA, vecA); });
259+
260+
it("should place values into vecA", function() { expect(vecA).toBeEqualish([1, 2, 3, 4]); });
261+
it("should return vecA", function() { expect(result).toBe(vecA); });
262+
});
263+
});
264+
246265
describe("round", function() {
247266
beforeEach(function() { vecA = [Math.E, Math.PI, Math.SQRT2, Math.SQRT1_2]; });
248267

src/gl-matrix/vec2.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ export function max(out, a, b) {
183183
return out;
184184
}
185185

186+
/**
187+
* Math.abs the components of a vec2
188+
*
189+
* @param {vec2} out the receiving vector
190+
* @param {vec2} a vector to abs
191+
* @returns {vec2} out
192+
*/
193+
export function abs(out, a) {
194+
out[0] = Math.abs(a[0]);
195+
out[1] = Math.abs(a[1]);
196+
return out;
197+
}
198+
186199
/**
187200
* Math.round the components of a vec2
188201
*

src/gl-matrix/vec3.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,20 @@ export function max(out, a, b) {
211211
return out;
212212
}
213213

214+
/**
215+
* Math.abs the components of a vec3
216+
*
217+
* @param {vec3} out the receiving vector
218+
* @param {vec3} a vector to abs
219+
* @returns {vec3} out
220+
*/
221+
export function abs(out, a) {
222+
out[0] = Math.abs(a[0]);
223+
out[1] = Math.abs(a[1]);
224+
out[2] = Math.abs(a[2]);
225+
return out;
226+
}
227+
214228
/**
215229
* Math.round the components of a vec3
216230
*

src/gl-matrix/vec4.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,21 @@ export function max(out, a, b) {
213213
return out;
214214
}
215215

216+
/**
217+
* Math.abs the components of a vec4
218+
*
219+
* @param {vec4} out the receiving vector
220+
* @param {vec4} a vector to abs
221+
* @returns {vec4} out
222+
*/
223+
export function abs(out, a) {
224+
out[0] = Math.abs(a[0]);
225+
out[1] = Math.abs(a[1]);
226+
out[2] = Math.abs(a[2]);
227+
out[3] = Math.abs(a[3]);
228+
return out;
229+
}
230+
216231
/**
217232
* Math.round the components of a vec4
218233
*

0 commit comments

Comments
 (0)