Skip to content

Commit

Permalink
Added quat.random
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexXsWx committed Jul 29, 2017
1 parent a8540ce commit 83b3152
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/gl-matrix-min.js

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions dist/gl-matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -5570,6 +5570,7 @@ exports.rotateY = rotateY;
exports.rotateZ = rotateZ;
exports.calculateW = calculateW;
exports.slerp = slerp;
exports.random = random;
exports.invert = invert;
exports.conjugate = conjugate;
exports.fromMat3 = fromMat3;
Expand Down Expand Up @@ -5875,6 +5876,29 @@ function slerp(out, a, b, t) {
return out;
}

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

var sqrt1MinusU1 = Math.sqrt(1 - u1);
var 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
10 changes: 10 additions & 0 deletions spec/gl-matrix/quat-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,16 @@ describe("quat", function() {

// TODO: slerp, calcuateW, rotateX, rotateY, rotateZ

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 @@ -254,6 +254,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 83b3152

Please sign in to comment.