Skip to content

Commit

Permalink
Merge remote-tracking branch 'AlexXsWx/master' into masterlocal
Browse files Browse the repository at this point in the history
  • Loading branch information
stefnotch committed Jul 13, 2018
2 parents 30194cd + 83b3152 commit ac270ee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/gl-matrix/quat-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,16 @@ describe("quat", function() {
});
});

describe("random", function() {
beforeEach(function() { result = quat.random(out); });

it("should result in a normalized quaternion", function() {
let copy = quat.clone(out);
expect(quat.normalize(out, out)).toBeEqualish(copy);
});
it("should return out", function() { expect(result).toBe(out); });
});

describe("invert", function() {
describe("with a separate output quaternion", function() {
beforeEach(function() { result = quat.invert(out, quatA); });
Expand Down
23 changes: 23 additions & 0 deletions src/gl-matrix/quat.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,29 @@ export function slerp(out, a, b, t) {
return out;
}

/**
* Generates a random quaternion
*
* @param {quat} out the receiving quaternion
* @returns {quat} out
*/
export function random(out) {
// Implementation of http://planning.cs.uiuc.edu/node198.html
// TODO: Calling random 3 times is probably not the fastest solution
let u1 = glMatrix.RANDOM();
let u2 = glMatrix.RANDOM();
let u3 = glMatrix.RANDOM();

let sqrt1MinusU1 = Math.sqrt(1 - u1);
let sqrtU1 = Math.sqrt(u1);

out[0] = sqrt1MinusU1 * Math.sin(2.0 * Math.PI * u2);
out[1] = sqrt1MinusU1 * Math.cos(2.0 * Math.PI * u2);
out[2] = sqrtU1 * Math.sin(2.0 * Math.PI * u3);
out[3] = sqrtU1 * Math.cos(2.0 * Math.PI * u3);
return out;
}

/**
* Calculates the inverse of a quat
*
Expand Down

0 comments on commit ac270ee

Please sign in to comment.