Skip to content

Commit

Permalink
Reorganize arbitraries
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Apr 26, 2024
1 parent 68926aa commit aa39e31
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions test/arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ import type { Vector4 } from "../src/vector.js";

const RADIAN = Math.PI / 180;

export function arbitrary3dRotationMatrix(): fc.Arbitrary<Matrix3x3> {
return arbitraryQuaternion().map(([x, y, z, w]) => {
// based on https://github.com/rawify/Quaternion.js/blob/c3834673b502e64e1866dbbf13568c0be93e52cc/quaternion.js#L791
const wx = w * x;
const wy = w * y;
const wz = w * z;
const xx = x * x;
const xy = x * y;
const xz = x * z;
const yy = y * y;
const yz = y * z;
const zz = z * z;

return [
[1 - 2 * (yy + zz), 2 * (xy - wz), 2 * (xz + wy)],
[2 * (xy + wz), 1 - 2 * (xx + zz), 2 * (yz - wx)],
[2 * (xz - wy), 2 * (yz + wx), 1 - 2 * (xx + yy)],
];
});
}

export function arbitraryLatLon(): fc.Arbitrary<[number, number]> {
return fc.tuple(
fc.double({
Expand All @@ -19,7 +40,7 @@ export function arbitraryLatLon(): fc.Arbitrary<[number, number]> {
);
}

export function arbitraryQuaternion(): fc.Arbitrary<Vector4> {
function arbitraryQuaternion(): fc.Arbitrary<Vector4> {
// based on https://github.com/mrdoob/three.js/blob/a2e9ee8204b67f9dca79f48cf620a34a05aa8126/src/math/Quaternion.js#L592
// Ken Shoemake
// Uniform random rotations
Expand All @@ -43,24 +64,3 @@ export function arbitraryQuaternion(): fc.Arbitrary<Vector4> {
return [x, y, z, w];
});
}

export function arbitrary3dRotationMatrix(): fc.Arbitrary<Matrix3x3> {
return arbitraryQuaternion().map(([x, y, z, w]) => {
// based on https://github.com/rawify/Quaternion.js/blob/c3834673b502e64e1866dbbf13568c0be93e52cc/quaternion.js#L791
const wx = w * x;
const wy = w * y;
const wz = w * z;
const xx = x * x;
const xy = x * y;
const xz = x * z;
const yy = y * y;
const yz = y * z;
const zz = z * z;

return [
[1 - 2 * (yy + zz), 2 * (xy - wz), 2 * (xz + wy)],
[2 * (xy + wz), 1 - 2 * (xx + zz), 2 * (yz - wx)],
[2 * (xz - wy), 2 * (yz + wx), 1 - 2 * (xx + yy)],
];
});
}

0 comments on commit aa39e31

Please sign in to comment.