diff --git a/package-lock.json b/package-lock.json index b6e589ba..b68f355b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3603,6 +3603,12 @@ "node": ">=8" } }, + "node_modules/hisabati": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/hisabati/-/hisabati-0.2.0.tgz", + "integrity": "sha512-KBS/seXYjE7xdIb70nVTFnDHWQxzwzsALY5uPlfw1aR4T1Jk0XBK/8gK8yFCj+PU0AJV5MJAGo/p3sj4RaTHtQ==", + "license": "MIT" + }, "node_modules/human-id": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/human-id/-/human-id-4.2.0.tgz", @@ -5263,7 +5269,12 @@ "dependencies": { "@wimaengine/command": "0.3.0", "@wimaengine/ecs": "0.3.0", + "@wimaengine/hierarchy": "0.3.0", "@wimaengine/logger": "0.3.0", + "@wimaengine/math": "0.3.0", + "@wimaengine/reflect": "0.3.0", + "@wimaengine/relationship": "0.3.0", + "@wimaengine/transform": "0.3.0", "@wimaengine/type": "0.3.0" } }, @@ -5485,7 +5496,8 @@ "@wimaengine/core": "0.3.0", "@wimaengine/ecs": "0.3.0", "@wimaengine/reflect": "0.3.0", - "@wimaengine/type": "0.3.0" + "@wimaengine/type": "0.3.0", + "hisabati": "^0.2.0" } }, "packages/misc": { diff --git a/packages/geometry/src/AABB/boundingbox.js b/packages/geometry/src/AABB/boundingbox.js index a10e48ae..fbe8fd80 100644 --- a/packages/geometry/src/AABB/boundingbox.js +++ b/packages/geometry/src/AABB/boundingbox.js @@ -154,6 +154,6 @@ export class BoundingBox2D { /** * @typedef BoundingBox2DSerial * @property {number} type - * @property {import('@wimaengine/math').Vector2Serial} max - * @property {import('@wimaengine/math').Vector2Serial} min + * @property {import('@wimaengine/math').Vector2Like} max + * @property {import('@wimaengine/math').Vector2Like} min */ diff --git a/packages/geometry/src/AABB/boundingcircle.js b/packages/geometry/src/AABB/boundingcircle.js index cd6298fd..c9fe248b 100644 --- a/packages/geometry/src/AABB/boundingcircle.js +++ b/packages/geometry/src/AABB/boundingcircle.js @@ -110,5 +110,5 @@ export class BoundingCircle { * @typedef BoundingCircleSerial * @property {number} type * @property {number} r - * @property {import('@wimaengine/math').Vector2Serial} pos + * @property {import('@wimaengine/math').Vector2Like} pos */ diff --git a/packages/gravity/src/resources/gravity.js b/packages/gravity/src/resources/gravity.js index c39905c7..09565d5d 100644 --- a/packages/gravity/src/resources/gravity.js +++ b/packages/gravity/src/resources/gravity.js @@ -10,7 +10,7 @@ export class Gravity2D extends Vector2 { } /** - * @param {import('@wimaengine/math').Vector2Serial} value + * @param {import('@wimaengine/math').Vector2Like} value * @param {Gravity2D} [out] */ static deserialize(value, out = new Gravity2D()) { @@ -28,7 +28,7 @@ export class Gravity3D extends Vector3 { } /** - * @param {import('@wimaengine/math').Vector3Serial} value + * @param {import('@wimaengine/math').Vector3Like} value * @param {Gravity3D} [out] */ static deserialize(value, out = new Gravity3D()) { diff --git a/packages/math/package.json b/packages/math/package.json index 0f3a1de4..6521c30a 100644 --- a/packages/math/package.json +++ b/packages/math/package.json @@ -26,7 +26,6 @@ ], "scripts": { "build": "vite build --config ../../.config/vite.config.js", - "test": "vitest run --dir tests --passWithNoTests", "types": "tsc -p ./tsc.type.json" }, "dependencies": { @@ -34,7 +33,8 @@ "@wimaengine/core": "0.3.0", "@wimaengine/ecs": "0.3.0", "@wimaengine/reflect": "0.3.0", - "@wimaengine/type": "0.3.0" + "@wimaengine/type": "0.3.0", + "hisabati": "^0.2.0" }, "types": "./dist/index.d.ts" } diff --git a/packages/math/src/core/affines/affine2.js b/packages/math/src/core/affines/affine2.js deleted file mode 100644 index 74a16a8d..00000000 --- a/packages/math/src/core/affines/affine2.js +++ /dev/null @@ -1,547 +0,0 @@ -import { Rotary } from '../angles' -import { Vector2 } from '../vectors' - -/** - * A class that is used to transform positions through rotation, scaling and translation. - * - * | a | c | e | - * |---|---|---| - * | b | d | f | - */ -export class Affine2 { - - /** - * @type {number} - */ - a - - /** - * @type {number} - */ - b - - /** - * @type {number} - */ - c - - /** - * @type {number} - */ - d - - /** - * @type {number} - */ - x - - /** - * @type {number} - */ - y - - /** - * @param {number} e11 - * @param {number} e12 - * @param {number} e13 - * @param {number} e21 - * @param {number} e22 - * @param {number} e23 - */ - constructor(e11 = 1, e12 = 0, e13 = 0, e21 = 0, e22 = 1, e23 = 0) { - Affine2.set(e11, e12, e13, e21, e22, e23, this) - } - - /** - * @param {number} e11 - * @param {number} e12 - * @param {number} e13 - * @param {number} e21 - * @param {number} e22 - * @param {number} e23 - */ - set(e11, e12, e13, e21, e22, e23) { - Affine2.set(e11, e12, e13, e21, e22, e23, this) - - return this - } - - /** - * Copies a affine into this affine. - * - * @param {Affine2} affine - * @returns {this} - */ - copy(affine) { - Affine2.copy(affine, this) - - return this - } - - /** - * Creates a new affine,fills its values with this ones and returns the former. - * - * @returns {Affine2} - */ - clone() { - return new Affine2().copy(this) - } - - /** - * @param {Vector2} translation - * @param {Rotary} orientation - * @param {Vector2} scale - * - * @returns {this} - */ - compose(translation, orientation, scale) { - Affine2.compose(translation, orientation, scale, this) - - return this - } - - /** - * @returns {[Vector2,Rotary,Vector2]} - */ - decompose() { - return Affine2.decompose(this) - } - - /** - * Translates a affine by a given amount. - * - * @param {Vector2} translation - * @returns {this} - */ - translate(translation) { - Affine2.translate(this, translation, this) - - return this - } - - /** - * Rotates the affine by the given angle. - * - * @param {Rotary} angle - * @returns {this} - */ - rotate(angle) { - Affine2.rotate(this, angle, this) - - return this - } - - /** - * Scales a affine by a given amount. - * - * @param {Vector2} scale - * @returns {this} - */ - scale(scale) { - Affine2.scale(this, scale, this) - - return this - } - - /** - * @param {Vector2} target - * @returns {this} - */ - lookAt(target) { - const eye = new Vector2(this.x, this.y) - - Affine2.lookAt(eye, target, this) - - return this - } - - /** - * Transforms the given vector. - * - * @param { Vector2} vector - */ - transform(vector) { - return Affine2.transform(this, vector, vector) - } - - /** - * Inverts the affine. - * - * @returns {this} - */ - invert() { - Affine2.invert(this, this) - - return this - } - - /** - * Multiplies with another affine, - * A * B = C, where A is this affine. - * - * @param {Affine2} affine - * @returns {this} - */ - multiply(affine) { - Affine2.multiply(this, affine, this) - - return this - } - - /** - * Multiplies with another affine, - * A * B = C, where A is this affine. - * - * @param {Affine2} affine - * @returns {this} - */ - divide(affine) { - Affine2.divide(this, affine, this) - - return this - } - - /** - * Deeply checks if a affine is equal to another. - * - * @param {Affine2} affine - * @returns {boolean} - */ - equals(affine) { - return Affine2.equal(this, affine) - } - - /** - * @param {number} e11 - * @param {number} e12 - * @param {number} e13 - * @param {number} e21 - * @param {number} e22 - * @param {number} e23 - * @param {Affine2} out - */ - static set(e11, e12, e13, e21, e22, e23, out = new Affine2()) { - out.a = e11 - out.b = e21 - out.c = e12 - out.d = e22 - out.x = e13 - out.y = e23 - - return out - } - - /** - * @param {Affine2} affine - * @param {Affine2} [out] - */ - static copy(affine, out = new Affine2()) { - out.a = affine.a - out.b = affine.b - out.c = affine.c - out.d = affine.d - out.x = affine.x - out.y = affine.y - - return out - } - - /** - * @param {Affine2} value - * @returns {Affine2Serial} - */ - static serialize(value) { - return [value.a, value.b, value.c, value.d, value.x, value.y] - } - - /** - * @param {Affine2Serial} value - * @param {Affine2} [out] - */ - static deserialize(value, out = new Affine2()) { - - out.a = value[0] - out.b = value[1] - out.c = value[2] - out.d = value[3] - out.x = value[4] - out.y = value[5] - - return out - } - - /** - * @param {unknown} value - * @returns {value is Affine2Serial} - */ - static validateSerial(value) { - return Array.isArray(value) && - value.length === 6 && - typeof value[0] === 'number' && - typeof value[1] === 'number' && - typeof value[2] === 'number' && - typeof value[3] === 'number' && - typeof value[4] === 'number' && - typeof value[5] === 'number' - } - - /** - * @param {Affine2} out - * @returns {Affine2} - */ - static identity(out = new Affine2()) { - this.set(1, 0, 0, 0, 1, 0, out) - - return out - } - - /** - * @param {Affine2} out - * @returns {Affine2} - */ - static zero(out = new Affine2()) { - this.set(0, 0, 0, 0, 0, 0, out) - - return out - } - - /** - * @param {Vector2} translation - * @param {Rotary} orientation - * @param {Vector2} scale - * @param {Affine2} affine - * @returns {Affine2} - */ - static compose(translation, orientation, scale, affine = new Affine2()) { - const { cos, sin } = orientation - - affine.a = cos * scale.x - affine.b = sin * scale.x - affine.c = -sin * scale.y - affine.d = cos * scale.y - affine.x = translation.x - affine.y = translation.y - - return affine - } - - /** - * @param {Affine2} affine - * @param {Vector2} position - * @param {Rotary} orientation - * @param {Vector2} scale - * @returns {[Vector2,Rotary,Vector2]} - */ - static decompose( - affine, - position = new Vector2(), - orientation = new Rotary(), - scale = new Vector2() - ) { - const scaleX = new Vector2(affine.a, affine.b).magnitude() - const scaleY = new Vector2(affine.c, affine.d).magnitude() - - position.x = affine.x - position.y = affine.y - - orientation.cos = affine.a / scaleX - orientation.sin = affine.b / scaleX - - scale.x = scaleX - scale.y = scaleY - - return [position, orientation, scale] - } - - /** - * @param {Affine2} affine1 - * @param {Affine2} affine2 - * @param {Affine2} [out] - */ - static multiply(affine1, affine2, out = new Affine2()) { - const { a: a1, b: b1, c: c1, d: d1, x: x1, y: y1 } = affine1 - const { a: a2, b: b2, c: c2, d: d2, x: x2, y: y2 } = affine2 - - out.a = a2 * a1 + b2 * c1 - out.b = a2 * b1 + b2 * d1 - out.c = c2 * a1 + d2 * c1 - out.d = c2 * b1 + d2 * d1 - out.x = x2 * a1 + y2 * c1 + x1 - out.y = x2 * b1 + y2 * d1 + y1 - - return out - } - - /** - * @param {Affine2} affine1 - * @param {Affine2} affine2 - * @param {Affine2} [out] - */ - static divide(affine1, affine2, out = new Affine2()) { - const multiplier = this.invert(affine2) - - this.multiply(affine1, multiplier, out) - - return out - } - - /** - * @param {Affine2} affine - * @param {Affine2} [out] - */ - static invert(affine, out = new Affine2()) { - const { a, b, c, d, x, y } = affine - const det = a * d - b * c - const invDet = 1 / det - - if (det === 0) return this.zero(out) - - out.a = d * invDet - out.b = -b * invDet - out.c = -c * invDet - out.d = a * invDet - out.x = (c * y - d * x) * det - out.y = -(a * y - b * x) * det - - return out - } - - /** - * @param {Affine2} affine - * @param {Vector2} translation - * @param {Affine2} [out] - */ - static translate(affine, translation, out = new Affine2()) { - out.a = affine.a - out.b = affine.b - out.c = affine.c - out.d = affine.d - out.x = affine.x + translation.x - out.y = affine.y + translation.y - - return out - } - - /** - * @param {Affine2} affine - * @param {Rotary} rotary - * @param {Affine2} out - */ - static rotate(affine, rotary, out = new Affine2()) { - const { a, b, c, d, x, y } = affine - const { cos, sin } = rotary - - out.a = a * cos - b * sin - out.b = a * sin + b * cos - out.c = c * cos - d * sin - out.d = c * sin + d * cos - out.x = x * cos - y * sin - out.y = x * sin + y * cos - - return out - } - - /** - * @param {Affine2} affine - * @param {Vector2} scale - * @param {Affine2} [out] - */ - static scale(affine, scale, out = new Affine2()) { - out.a = affine.a * scale.x - out.b = affine.b * scale.x - out.c = affine.c * scale.y - out.d = affine.d * scale.y - out.x = affine.x - out.y = affine.y - - return out - } - - /** - * @param {Vector2} eye - * @param {Vector2} target - * @param {Affine2} out - */ - static lookAt(eye, target, out = new Affine2()) { - const y = Vector2.subtract(target, eye) - - if (Vector2.magnitudeSquared(y) === 0) { - y.x = 1 - } - - Vector2.normalize(y, y) - - out.a = y.x - out.b = y.y - out.c = -y.y - out.d = y.x - out.x = eye.y - out.y = eye.y - - return out - } - - /** - * @param {Affine2} affine - * @param {Vector2} v - * @param {Vector2} [out] - */ - static transform(affine, v, out = new Vector2()) { - const { a, b, c, d, x, y } = affine - const { x: vx, y: vy } = v - - out.x = a * vx + c * vy + x - out.y = b * vx + d * vy + y - - return out - } - - /** - * @param {Affine2} affine1 - * @param {Affine2} affine2 - * @returns {boolean} - */ - static equal(affine1, affine2) { - return ( - (affine1.a === affine2.a) && - (affine1.b === affine2.b) && - (affine1.c === affine2.c) && - (affine1.d === affine2.d) && - (affine1.x === affine2.x) && - (affine1.y === affine2.y) - ) - } - - /** - * Allows iteration of components. - * - * @yields {number} - */ - * [Symbol.iterator]() { - yield this.a - yield this.b - yield this.c - yield this.d - yield this.x - yield this.y - } - - /** - * @readonly - * @type {Affine2} - */ - static Identity = Affine2.identity() - - /** - * @readonly - * @type {Affine2} - */ - static Zero = Affine2.zero() -} - -/** - * Serialized form of `Affine2`. - * - * @typedef Affine2Serial - * @type {[number, number, number, number, number, number]} - */ diff --git a/packages/math/src/core/affines/affine3.js b/packages/math/src/core/affines/affine3.js deleted file mode 100644 index 4ab4b7d1..00000000 --- a/packages/math/src/core/affines/affine3.js +++ /dev/null @@ -1,895 +0,0 @@ -import { Quaternion } from '../angles' -import { invert } from '../functions' -import { Matrix3, Matrix4 } from '../matrices' -import { Vector3 } from '../vectors' - -/** - * Represents a 3x4 affine. - * Can be used to represent 3 dimensional rotation, scale,skew and translation. - * - * - * | a | d | g | j | - * |---|---|---|---| - * | b | e | h | k | - * | c | f | i | l | - */ -export class Affine3 { - - /** - * @type {number} - */ - a - - /** - * @type {number} - */ - b - - /** - * @type {number} - */ - c - - /** - * @type {number} - */ - d - - /** - * @type {number} - */ - e - - /** - * @type {number} - */ - f - - /** - * @type {number} - */ - g - - /** - * @type {number} - */ - h - - /** - * @type {number} - */ - i - - /** - * @type {number} - */ - x - - /** - * @type {number} - */ - y - - /** - * @type {number} - */ - z - constructor( - e11 = 1, - e12 = 0, - e13 = 0, - e14 = 0, - e21 = 0, - e22 = 1, - e23 = 0, - e24 = 0, - e31 = 0, - e32 = 0, - e33 = 1, - e34 = 0 - ) { - Affine3.set(e11, e12, e13, e14, e21, e22, e23, e24, e31, e32, e33, e34, this) - } - - /** - * @param {number} e11 - * @param {number} e12 - * @param {number} e13 - * @param {number} e14 - * @param {number} e21 - * @param {number} e22 - * @param {number} e23 - * @param {number} e24 - * @param {number} e31 - * @param {number} e32 - * @param {number} e33 - * @param {number} e34 - */ - set(e11, e12, e13, e14, e21, e22, e23, e24, e31, e32, e33, e34) { - Affine3.set(e11, e12, e13, e14, e21, e22, e23, e24, e31, e32, e33, e34, this) - - return this - } - - /** - * Copies a affine into this affine. - * - * @param {Affine3} affine - * @returns {this} - */ - copy(affine) { - Affine3.copy(affine, this) - - return this - } - - /** - * Creates a new affine,fills its values with this ones and returns the former. - * - * @returns {Affine3} - */ - clone() { - return new Affine3().copy(this) - } - - /** - * @param {Vector3} translation - * @param {Quaternion} orientation - * @param {Vector3} scale - * - * @returns {this} - */ - compose(translation, orientation, scale) { - Affine3.compose(translation, orientation, scale, this) - - return this - } - - /** - * @returns {[Vector3,Quaternion,Vector3]} - */ - decompose() { - return Affine3.decompose(this) - } - - /** - * Translates a affine by a given amount. - * - * @param {Vector3} translation - * @returns {this} - */ - translate(translation) { - Affine3.translate(this, translation, this) - - return this - } - - /** - * Rotates the affine by the given angle. - * - * @param {Quaternion} angle - * @returns {this} - */ - rotate(angle) { - Affine3.rotate(this, angle, this) - - return this - } - - /** - * Scales a affine by a given amount. - * - * @param {Vector3} scale - * @returns {this} - */ - scale(scale) { - Affine3.scale(this, scale, this) - - return this - } - - /** - * @param {Vector3} target - * @param {Vector3} up - * @returns {this} - */ - lookAt(target, up) { - const eye = new Vector3(this.x, this.y, this.z) - - Affine3.lookAt(eye, target, up, this) - - return this - } - - /** - * Transforms the given vector. - * - * @param {Vector3} vector - */ - transform(vector) { - return Affine3.transform(this, vector, vector) - } - - /** - * Inverts the affine. - * - * @returns {this} - */ - invert() { - Affine3.invert(this, this) - - return this - } - - /** - * Multiplies with another affine, - * A * B = C, where A is this affine. - * - * @param {Affine3} affine - * @returns {this} - */ - multiply(affine) { - Affine3.multiply(this, affine, this) - - return this - } - - /** - * Multiplies with another affine, - * A * B = C, where A is this affine. - * - * @param {Affine3} affine - * @returns {this} - */ - divide(affine) { - Affine3.divide(this, affine, this) - - return this - } - - /** - * Deeply checks if a affine is equal to another. - * - * @param {Affine3} affine - * @returns {boolean} - */ - equals(affine) { - return Affine3.equal(this, affine) - } - - /** - * @param {number} e11 - * @param {number} e12 - * @param {number} e13 - * @param {number} e14 - * @param {number} e21 - * @param {number} e22 - * @param {number} e23 - * @param {number} e24 - * @param {number} e31 - * @param {number} e32 - * @param {number} e33 - * @param {number} e34 - * @param {Affine3} out - */ - static set(e11, e12, e13, e14, e21, e22, e23, e24, e31, e32, e33, e34, out = new Affine3()) { - out.a = e11 - out.b = e21 - out.c = e31 - out.d = e12 - out.e = e22 - out.f = e32 - out.g = e13 - out.h = e23 - out.i = e33 - out.x = e14 - out.y = e24 - out.z = e34 - - return out - } - - /** - * @param {Affine3} affine - * @param {Affine3} [out] - */ - static copy(affine, out = new Affine3()) { - out.a = affine.a - out.b = affine.b - out.c = affine.c - out.d = affine.d - out.e = affine.e - out.f = affine.f - out.g = affine.g - out.h = affine.h - out.i = affine.i - out.x = affine.x - out.y = affine.y - out.z = affine.z - - return out - } - - /** - * @param {Affine3} value - * @returns {Affine3Serial} - */ - static serialize(value) { - return [ - value.a, - value.b, - value.c, - value.d, - value.e, - value.f, - value.g, - value.h, - value.i, - value.x, - value.y, - value.z - ] - } - - /** - * @param {Affine3Serial} value - * @param {Affine3} [out] - */ - static deserialize(value, out = new Affine3()) { - out.a = value[0] - out.b = value[1] - out.c = value[2] - out.d = value[3] - out.e = value[4] - out.f = value[5] - out.g = value[6] - out.h = value[7] - out.i = value[8] - out.x = value[9] - out.y = value[10] - out.z = value[11] - - return out - } - - /** - * @param {unknown} value - * @returns {value is Affine3Serial} - */ - static validateSerial(value) { - return Array.isArray(value) && - value.length === 12 && - typeof value[0] === 'number' && - typeof value[1] === 'number' && - typeof value[2] === 'number' && - typeof value[3] === 'number' && - typeof value[4] === 'number' && - typeof value[5] === 'number' && - typeof value[6] === 'number' && - typeof value[7] === 'number' && - typeof value[8] === 'number' && - typeof value[9] === 'number' && - typeof value[10] === 'number' && - typeof value[11] === 'number' - } - - /** - * @param {Affine3} out - * @returns {Affine3} - */ - static identity(out = new Affine3()) { - out.a = 1 - out.b = 0 - out.c = 0 - out.d = 0 - out.e = 1 - out.f = 0 - out.g = 0 - out.h = 0 - out.i = 1 - out.x = 0 - out.y = 0 - out.z = 0 - - return out - } - - /** - * @param {Affine3} out - * @returns {Affine3} - */ - static zero(out = new Affine3()) { - out.a = 0 - out.b = 0 - out.c = 0 - out.d = 0 - out.e = 0 - out.f = 0 - out.g = 0 - out.h = 0 - out.i = 0 - out.x = 0 - out.y = 0 - out.z = 0 - - return out - } - - /** - * @param {Vector3} position - * @param {Quaternion} orientation - * @param {Vector3} scale - * @param {Affine3} out - */ - static compose(position, orientation, scale, out = new Affine3()) { - const { x, y, z, w } = orientation - const x2 = x + x, - y2 = y + y, - z2 = z + z - const xx = x * x2, - xy = x * y2, - xz = x * z2 - const yy = y * y2, - yz = y * z2, - zz = z * z2 - const wx = w * x2, - wy = w * y2, - wz = w * z2 - - const sx = scale.x, - sy = scale.y, - sz = scale.z - - out.a = (1 - (yy + zz)) * sx - out.b = (xy + wz) * sx - out.c = (xz - wy) * sx - - out.d = (xy - wz) * sy - out.e = (1 - (xx + zz)) * sy - out.f = (yz + wx) * sy - - out.g = (xz + wy) * sz - out.h = (yz - wx) * sz - out.i = (1 - (xx + yy)) * sz - - out.x = position.x - out.y = position.y - out.z = position.z - - return out - } - - /** - * @param {Affine3} affine - * @returns {[Vector3,Quaternion,Vector3]} - */ - static decompose(affine) { - const position = new Vector3() - const orientation = new Quaternion() - const scale = new Vector3() - const rotMatrix = new Matrix3( - affine.a, - affine.d, - affine.g, - affine.b, - affine.e, - affine.h, - affine.c, - affine.f, - affine.i - ) - - const det = Matrix3.determinant(rotMatrix) - const sx = new Vector3().set(affine.a, affine.b, affine.c) - .magnitude() * (det < 0 ? -1 : 1) - const sy = new Vector3().set(affine.d, affine.e, affine.f) - .magnitude() - const sz = new Vector3().set(affine.g, affine.h, affine.i) - .magnitude() - const invSX = 1 / sx - const invSY = 1 / sy - const invSZ = 1 / sz - - rotMatrix.a *= invSX - rotMatrix.b *= invSX - rotMatrix.c *= invSX - rotMatrix.d *= invSY - rotMatrix.e *= invSY - rotMatrix.f *= invSY - rotMatrix.g *= invSZ - rotMatrix.h *= invSZ - rotMatrix.i *= invSZ - - position.x = affine.x - position.y = affine.y - position.z = affine.z - - scale.x = sx - scale.y = sy - scale.z = sz - - Quaternion.fromRotationMatrix(rotMatrix, orientation) - - return [position, orientation, scale] - } - - /** - * @param {Affine3} affine1 - * @param {Affine3} affine2 - * @param {Affine3} out - */ - static multiply(affine1, affine2, out = new Affine3()) { - const - a11 = affine1.a, - a21 = affine1.b, - a31 = affine1.c, - a12 = affine1.d, - a22 = affine1.e, - a32 = affine1.f, - a13 = affine1.g, - a23 = affine1.h, - a33 = affine1.i, - a14 = affine1.x, - a24 = affine1.y, - a34 = affine1.z - - const - b11 = affine2.a, - b21 = affine2.b, - b31 = affine2.c, - b12 = affine2.d, - b22 = affine2.e, - b32 = affine2.f, - b13 = affine2.g, - b23 = affine2.h, - b33 = affine2.i, - b14 = affine2.x, - b24 = affine2.y, - b34 = affine2.z - - out.a = a11 * b11 + a12 * b21 + a13 * b31 - out.b = a21 * b11 + a22 * b21 + a23 * b31 - out.c = a31 * b11 + a32 * b21 + a33 * b31 - - out.d = a11 * b12 + a12 * b22 + a13 * b32 - out.e = a21 * b12 + a22 * b22 + a23 * b32 - out.f = a31 * b12 + a32 * b22 + a33 * b32 - - out.g = a11 * b13 + a12 * b23 + a13 * b33 - out.h = a21 * b13 + a22 * b23 + a23 * b33 - out.i = a31 * b13 + a32 * b23 + a33 * b33 - - out.x = a11 * b14 + a12 * b24 + a13 * b34 + a14 - out.y = a21 * b14 + a22 * b24 + a23 * b34 + a24 - out.z = a31 * b14 + a32 * b24 + a33 * b34 + a34 - - return out - } - - /** - * @param {Affine3} affine1 - * @param {Affine3} affine2 - * @param {Affine3} [out] - */ - static divide(affine1, affine2, out = new Affine3()) { - const multiplier = this.invert(affine2) - - this.multiply(affine1, multiplier, out) - - return out - } - - /** - * @param {Affine3} affine - * @param {Affine3} [out] - */ - static invert(affine, out = new Affine3()) { - const { a, b, c, d, e, f, g, h, i, x, y, z } = affine - const t11 = i * e - h * f, - t12 = h * c - i * b, - t13 = f * b - e * c, - - det = a * t11 + d * t12 + g * t13 - - if (det === 0) return this.zero(out) - - const detInv = invert(det) - - out.a = t11 * detInv - out.b = t12 * detInv - out.c = t13 * detInv - out.d = (g * f - i * d) * detInv - out.e = (i * a - g * c) * detInv - out.f = (d * c - f * a) * detInv - out.g = (h * d - g * e) * detInv - out.h = (g * b - h * a) * detInv - out.i = (e * a - d * b) * detInv - - out.x = -(out.a * x + out.d * y + out.g * z) - out.y = -(out.b * x + out.e * y + out.h * z) - out.z = -(out.c * x + out.f * y + out.i * z) - - return out - } - - /** - * @param {Affine3} affine - * @param {Vector3} translation - * @param {Affine3} [out] - */ - static translate(affine, translation, out = new Affine3()) { - out.a = affine.a - out.b = affine.b - out.c = affine.c - out.d = affine.d - out.e = affine.e - out.f = affine.f - out.g = affine.g - out.h = affine.h - out.i = affine.i - out.x = affine.x + translation.x - out.y = affine.y + translation.y - out.z = affine.z + translation.z - - return out - } - - /** - * @param {Affine3} affine - * @param {Quaternion} rotation - * @param {Affine3} [out] - */ - static rotate(affine, rotation, out = new Affine3()) { - const { x, y, z } = affine - const { x: qx, y: qy, z: qz, w: qw } = rotation - const q00 = qw * qw, - q01 = qw * qx, - q02 = qw * qy, - q03 = qw * qz, - q11 = qx * qx, - q12 = qx * qy, - q13 = qx * qz, - q22 = qy * qy, - q23 = qy * qz, - q33 = qz * qz - const tx = 2 * (qy * z - qz * y) - const ty = 2 * (qz * x - qx * z) - const tz = 2 * (qx * y - qy * x) - const matrixA = new Matrix3( - affine.a, - affine.d, - affine.f, - affine.b, - affine.e, - affine.h, - affine.c, - affine.f, - affine.i - ) - - const matrixB = new Matrix3( - 2 * (q00 + q11) - 1, - 2 * (q12 - q03), - 2 * (q13 + q02), - 2 * (q12 + q03), - 2 * (q00 + q22) - 1, - 2 * (q23 + q01), - 2 * (q13 + q02), - 2 * (q12 + q03), - 2 * (q00 + q33) - 1 - ) - - Matrix3.multiply(matrixA, matrixB, matrixA) - - out.a = matrixA.a - out.b = matrixA.b - out.c = matrixA.c - out.d = matrixA.d - out.e = matrixA.e - out.f = matrixA.f - out.g = matrixA.g - out.h = matrixA.h - out.i = matrixA.i - out.x = x + qw * tx + qy * tz - qz * ty - out.y = y + qw * ty + qz * tx - qx * tz - out.z = z + qw * tz + qx * ty - qy * tx - - return out - } - - /** - * @param {Affine3} affine - * @param {Vector3} scale - * @param {Affine3} [out] - */ - static scale(affine, scale, out = new Affine3()) { - out.a = affine.a * scale.x - out.b = affine.b * scale.x - out.c = affine.c * scale.x - out.d = affine.d * scale.y - out.e = affine.e * scale.y - out.f = affine.f * scale.y - out.g = affine.g * scale.z - out.h = affine.h * scale.z - out.i = affine.i * scale.z - - return out - } - - /** - * @param {Vector3} eye - * @param {Vector3} target - * @param {Vector3} up - * @param {Affine3} out - */ - static lookAt(eye, target, up, out = new Affine3()) { - const eyex = eye.x, - eyey = eye.y, - eyez = eye.z, - upx = up.x, - upy = up.y, - upz = up.z - - let zx = eyex - target.x, - zy = eyey - target.y, - zz = eyez - target.z - - let len = zx * zx + zy * zy + zz * zz - - if (len > 0) { - len = invert(Math.sqrt(len)) - zx *= len - zy *= len - zz *= len - } else { - zz = 1 - } - - let xx = upy * zz - upz * zy, - xy = upz * zx - upx * zz, - xz = upx * zy - upy * zx - - len = xx * xx + xy * xy + xz * xz - - if (len > 0) { - len = invert(Math.sqrt(len)) - xx *= len - xy *= len - xz *= len - } else { - if (Math.abs(upz) === 1) { - zx += 0.0001 - } else { - zz += 0.0001 - } - - len = zx * zx + zy * zy + zz * zz - len = invert(Math.sqrt(len)) - zx *= len - zy *= len - zz *= len - xx = upy * zz - upz * zy - xy = upz * zx - upx * zz - xz = upx * zy - upy * zx - } - - out.a = xx - out.b = xy - out.c = xz - out.d = zy * xz - zz * xy - out.e = zz * xx - zx * xz - out.f = zx * xy - zy * xx - out.g = zx - out.h = zy - out.i = zz - out.x = eyex - out.y = eyey - out.z = eyez - - return out - } - - /** - * @param {Affine3} affine - * @param {Vector3} vector - * @param {Vector3} out - */ - static transform(affine, vector, out = new Vector3()) { - const { a, b, c, d, e, f, g, h, i, x, y, z } = affine - const { x: vx, y: vy, z: vz } = vector - - out.x = a * vx + d * vy + g * vz + x - out.y = b * vx + e * vy + h * vz + y - out.z = c * vx + f * vy + i * vz + z - - return out - } - - /** - * @param {Affine3} affine - * @param {Matrix4} out - * @returns {Matrix4} - */ - static toMatrix4(affine, out = new Matrix4()) { - return Matrix4.set( - affine.a, - affine.d, - affine.g, - affine.x, - affine.b, - affine.e, - affine.h, - affine.y, - affine.c, - affine.f, - affine.i, - affine.z, - 0, - 0, - 0, - 1, - out - ) - } - - /** - * @param {Affine3} affine1 - * @param {Affine3} Affine3 - * @returns {boolean} - */ - static equal(affine1, Affine3) { - return ( - (affine1.a === Affine3.a) && - (affine1.b === Affine3.b) && - (affine1.c === Affine3.c) && - (affine1.d === Affine3.d) && - (affine1.e === Affine3.e) && - (affine1.f === Affine3.f) && - (affine1.g === Affine3.g) && - (affine1.h === Affine3.h) && - (affine1.i === Affine3.i) && - (affine1.x === Affine3.x) && - (affine1.y === Affine3.y) && - (affine1.z === Affine3.z) - ) - } - - /** - * Allows iteration of components. - * - * @yields {number} - */ - * [Symbol.iterator]() { - yield this.a - yield this.b - yield this.c - yield this.d - yield this.e - yield this.f - yield this.g - yield this.h - yield this.i - yield this.x - yield this.y - yield this.z - } - - /** - * @readonly - * @type {Affine3} - */ - static Identity = Affine3.identity() - - /** - * @readonly - * @type {Affine3} - */ - static Zero = Affine3.zero() -} - -/** - * Serialized form of `Affine3`. - * - * @typedef Affine3Serial - * @type {[number, number, number, number, number, number, number, number, number, number, number, number]} - */ diff --git a/packages/math/src/core/affines/index.js b/packages/math/src/core/affines/index.js deleted file mode 100644 index cdd86138..00000000 --- a/packages/math/src/core/affines/index.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from './affine2' -export * from './affine3' diff --git a/packages/math/src/core/angles/angle.js b/packages/math/src/core/angles/angle.js deleted file mode 100644 index 300613b5..00000000 --- a/packages/math/src/core/angles/angle.js +++ /dev/null @@ -1,72 +0,0 @@ -import { lerp } from '../functions' - -/** - * Wrapper class since JavaScript doesn't support references to numbers explicitly. - * Keeps record of the orientation of an entity. - */ -export class Angle { - - /** - * Orientation in radians. - * - * @type {number} - */ - value = 0 - - /** - * @param {number} [rad=0] - Angle in radians. - */ - constructor(rad = 0) { - this.value = rad - } - - /** - * @param {Angle} a - * @param {Angle} b - * @param {number} t - * @param {Angle} out - */ - static lerp(a, b, t, out) { - out.value = lerp(a.value, b.value, t) - } - - /** - * Copies the orientation of another angle. - * - * @param {Angle} angle - */ - static copy(angle) { - this.value = angle.value - } - - /** - * @param {Angle} value - */ - static serialize(value) { - return value.value - } - - /** - * @param {AngleSerial} value - * @param {Angle} [out] - */ - static deserialize(value, out = new Angle()) { - out.value = value - - return out - } - - /** - * @param {unknown} value - * @returns {value is AngleSerial} - */ - static validateSerial(value) { - return typeof value === 'number' - } -} - -/** - * Serialized form of `Angle`. - * - * @typedef {number} AngleSerial - */ diff --git a/packages/math/src/core/angles/index.js b/packages/math/src/core/angles/index.js deleted file mode 100644 index f1cb58be..00000000 --- a/packages/math/src/core/angles/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export * from './angle' -export * from './rotary' -export * from './quaternion' diff --git a/packages/math/src/core/angles/quaternion.js b/packages/math/src/core/angles/quaternion.js deleted file mode 100644 index d0de1b25..00000000 --- a/packages/math/src/core/angles/quaternion.js +++ /dev/null @@ -1,638 +0,0 @@ -import { clamp, fuzzyEqual } from '../functions' -import { Matrix3 } from '../matrices' -import { Vector3 } from '../vectors' - -export class Quaternion { - constructor(x = 0, y = 0, z = 0, w = 1) { - this.x = x - this.y = y - this.z = z - this.w = w - } - - /** - * @param {number} x - * @param {number} y - * @param {number} z - * @param {number} w - */ - set(x, y, z, w) { - Quaternion.set(x, y, z, w, this) - - return this - } - - /** - * @param {Quaternion} quaternion - */ - copy(quaternion) { - Quaternion.copy(quaternion, this) - - return this - } - - clone() { - return new Quaternion().copy(this) - } - - /** - * @returns {this} - */ - reverse() { - Quaternion.reverse(this, this) - - return this - } - - /** - * @param {Quaternion} v - */ - dot(v) { - return Quaternion.dot(this, v) - } - - magnitudeSquared() { - return Quaternion.magnitudeSquared(this) - } - - magnitude() { - return Math.sqrt(Quaternion.magnitude(this)) - } - - /** - * @returns {this} - */ - normalize() { - Quaternion.normalize(this, this) - - return this - } - - /** - * @param {Quaternion} q - * @returns {this} - */ - multiply(q) { - Quaternion.multiply(this, q, this) - - return this - } - - /** - * @param {number} angle - */ - rotateX(angle) { - Quaternion.rotateX(angle, this) - - return this - } - - /** - * @param {number} angle - */ - rotateY(angle) { - Quaternion.rotateY(angle, this) - - return this - } - - /** - * @param {number} angle - */ - rotateZ(angle) { - Quaternion.rotateZ(angle, this) - - return this - } - - /** - * @param {Quaternion} quaternion - * @returns {boolean} - */ - equals(quaternion) { - return Quaternion.equal(this, quaternion) - } - - /** - * @param {number} x - * @param {number} y - * @param {number} z - * @param {number} w - * @param {Quaternion} out - */ - static set(x, y, z, w, out = new Quaternion()) { - out.x = x - out.y = y - out.z = z - out.w = w - - return out - } - - /** - * @param {Quaternion} from - * @param {Quaternion} to - */ - static copy(from, to = new Quaternion()) { - to.x = from.x - to.y = from.y - to.z = from.z - to.w = from.w - - return to - } - - /** - * @param {Quaternion} value - */ - static serialize(value) { - return { - x: value.x, - y: value.y, - z: value.z, - w: value.w - } - } - - /** - * @param {QuaternionSerial} value - * @param {Quaternion} [out] - */ - static deserialize(value, out = new Quaternion()) { - out.x = value.x - out.y = value.y - out.z = value.z - out.w = value.w - - return out - } - - /** - * @param {unknown} value - * @returns {value is QuaternionSerial} - */ - static validateSerial(value) { - if (!value || typeof value !== 'object') { - return false - } - - if (!('x' in value) || !('y' in value) || !('z' in value) || !('w' in value)) { - return false - } - - return typeof value.x === 'number' && - typeof value.y === 'number' && - typeof value.z === 'number' && - typeof value.w === 'number' - } - - /** - * @param {Quaternion} out - */ - static identity(out = new Quaternion()) { - out.x = 0 - out.y = 0 - out.z = 0 - out.w = 1 - - return out - } - - /** - * @param {Quaternion} out - */ - static zero(out = new Quaternion()) { - out.x = 0 - out.y = 0 - out.z = 0 - out.w = 0 - - return out - } - - /** - * @param {Quaternion} q - * @returns {number} - */ - static magnitudeSquared(q) { - return q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w - } - - /** - * @param {Quaternion} q - * @returns {number} - */ - static magnitude(q) { - return Math.sqrt(this.magnitudeSquared(q)) - } - - /** - * @param {Quaternion} q - * @param {Quaternion} out - * @returns {Quaternion} - */ - static normalize(q, out = new Quaternion()) { - const l = this.magnitude(q) - - if (l === 0) { - this.identity(out) - } else { - const inv = 1 / l - - out.x = q.x * inv - out.y = q.y * inv - out.z = q.z * inv - out.w = q.w * inv - } - - return out - } - - /** - * @param {number} angle - * @param {Quaternion} out - */ - static rotateX(angle, out = new Quaternion()) { - const halfAngle = angle * 0.5 - - const ax = out.x - const ay = out.y - const az = out.z - const aw = out.w - - const bx = Math.sin(halfAngle) - const bw = Math.cos(halfAngle) - - return this.set( - ax * bw + aw * bx, - ay * bw + az * bx, - az * bw - ay * bx, - aw * bw - ax * bx, - out - ) - } - - /** - * @param {number} angle - * @param {Quaternion} out - */ - static rotateY(angle, out = new Quaternion()) { - const halfAngle = angle * 0.5 - - const ax = out.x - const ay = out.y - const az = out.z - const aw = out.w - - const by = Math.sin(halfAngle) - const bw = Math.cos(halfAngle) - - return this.set( - ax * bw - az * by, - ay * bw + aw * by, - az * bw + ax * by, - aw * bw - ay * by, - out - ) - } - - /** - * @param {number} angle - * @param {Quaternion} out - */ - static rotateZ(angle, out = new Quaternion()) { - const halfAngle = angle * 0.5 - - const ax = out.x - const ay = out.y - const az = out.z - const aw = out.w - - const bz = Math.sin(halfAngle) - const bw = Math.cos(halfAngle) - - return this.set( - ax * bw + ay * bz, - ay * bw - ax * bz, - az * bw + aw * bz, - aw * bw - az * bz, - out - ) - } - - /** - * @param {Quaternion} a - * @param {Quaternion} b - * @param {Quaternion} [out] - * @returns {Quaternion} - */ - static multiply(a, b, out = new Quaternion()) { - const qax = a.x, - qay = a.y, - qaz = a.z, - qaw = a.w - const qbx = b.x, - qby = b.y, - qbz = b.z, - qbw = b.w - - out.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby - out.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz - out.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx - out.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz - - return out - } - - /** - * @param {Quaternion} q - * @param {number} s - * @param {Quaternion} [out] - * @returns {Quaternion} - */ - static multiplyScalar(q, s, out = new Quaternion()) { - out.x = q.x * s - out.y = q.y * s - out.z = q.z * s - out.w = q.w * s - - return out - } - - /** - * @param {Quaternion} a - * @param {Quaternion} out - */ - static reverse(a, out = new Quaternion()) { - out.x = -a.x - out.y = -a.y - out.z = -a.z - out.w = a.w - - return out - } - - /** - * @param {Quaternion} q1 - * @param {Quaternion} q2 - * @returns {number} - */ - static dot(q1, q2) { - return q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w - } - - /** - * @param {Quaternion} a - * @param {Quaternion} b - */ - static angleBetween(a, b) { - return 2 * Math.acos(Math.abs(clamp(a.dot(b), -1, 1))) - } - - /** - * @param {number} x - * @param {number} y - * @param {number} z - * @param {Quaternion} out - */ - static fromEuler(x, y, z, out = new Quaternion()) { - const c1 = Math.cos(x / 2) - const c2 = Math.cos(y / 2) - const c3 = Math.cos(z / 2) - - const s1 = Math.sin(x / 2) - const s2 = Math.sin(y / 2) - const s3 = Math.sin(z / 2) - - out.x = s1 * c2 * c3 + c1 * s2 * s3 - out.y = c1 * s2 * c3 - s1 * c2 * s3 - out.z = c1 * c2 * s3 + s1 * s2 * c3 - out.w = c1 * c2 * c3 - s1 * s2 * s3 - - return out - } - - /** - * @param {Matrix3} matrix - * @param {Quaternion} out - * @returns {Quaternion} - */ - static fromRotationMatrix(matrix, out = new Quaternion()) { - const - m11 = matrix.a, - m12 = matrix.d, - m13 = matrix.g, - m21 = matrix.b, - m22 = matrix.e, - m23 = matrix.h, - m31 = matrix.c, - m32 = matrix.f, - m33 = matrix.i, - trace = matrix.trace() - - if (trace > 0) { - const s = 0.5 / Math.sqrt(trace + 1.0) - - out.w = 0.25 / s - out.x = (m32 - m23) * s - out.y = (m13 - m31) * s - out.z = (m21 - m12) * s - } else if (m11 > m22 && m11 > m33) { - const s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33) - - out.w = (m32 - m23) / s - out.x = 0.25 * s - out.y = (m12 + m21) / s - out.z = (m13 + m31) / s - } else if (m22 > m33) { - const s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33) - - out.w = (m13 - m31) / s - out.x = (m12 + m21) / s - out.y = 0.25 * s - out.z = (m23 + m32) / s - } else { - const s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22) - - out.w = (m21 - m12) / s - out.x = (m13 + m31) / s - out.y = (m23 + m32) / s - out.z = 0.25 * s - } - - return out - } - - /** - * @param {Vector3} axis - * @param {number} angle - * @param {Quaternion} out - * @returns {Quaternion} - */ - static fromAxisAngle(axis, angle, out = new Quaternion()) { - const halfAngle = angle * 0.5, - s = Math.sin(halfAngle) - - out.x = axis.x * s - out.y = axis.y * s - out.z = axis.z * s - out.w = Math.cos(halfAngle) - - return out - } - - /** - * @param {Quaternion} orientation - * @param {Vector3} vector - * @returns {Vector3} - */ - static transformVector3(orientation, vector) { - const vx = vector.x, - vy = vector.y, - vz = vector.z - const qx = orientation.x, - qy = orientation.y, - qz = orientation.z, - qw = orientation.w - - const tx = 2 * (qy * vz - qz * vy) - const ty = 2 * (qz * vx - qx * vz) - const tz = 2 * (qx * vy - qy * vx) - - vector.x = vx + qw * tx + qy * tz - qz * ty - vector.y = vy + qw * ty + qz * tx - qx * tz - vector.z = vz + qw * tz + qx * ty - qy * tx - - return vector - } - - /** - * @param {Quaternion} a - * @param {Quaternion} b - * @param {number} t - * @param {Quaternion} [out] - */ - static slerp(a, b, t, out = new Quaternion()) { - if (t === 0) return out.copy(a) - if (t === 1) return out.copy(b) - - const { x, y, z, w } = a - - let cosHalfTheta = w * b.w + x * b.x + y * b.y + z * b.z - - if (cosHalfTheta < 0) { - out.w = -b.w - out.x = -b.x - out.y = -b.y - out.z = -b.z - - cosHalfTheta = -cosHalfTheta - } else { - out.copy(b) - } - - if (cosHalfTheta >= 1.0) { - out.w = w - out.x = x - out.y = y - out.z = z - - return out - } - - const sqrSinHalfTheta = 1.0 - cosHalfTheta * cosHalfTheta - - if (sqrSinHalfTheta <= Number.EPSILON) { - const s = 1 - t - - out.w = s * w + t * out.w - out.x = s * x + t * out.x - out.y = s * y + t * out.y - out.z = s * z + t * out.z - out.normalize() - - return out - } - - const sinHalfTheta = Math.sqrt(sqrSinHalfTheta) - const halfTheta = Math.atan2(sinHalfTheta, cosHalfTheta) - const ratioA = Math.sin((1 - t) * halfTheta) / sinHalfTheta, - ratioB = Math.sin(t * halfTheta) / sinHalfTheta - - out.w = (w * ratioA + out.w * ratioB) - out.x = (x * ratioA + out.x * ratioB) - out.y = (y * ratioA + out.y * ratioB) - out.z = (z * ratioA + out.z * ratioB) - - return this - } - - /** - * @param {Quaternion} a - * @param {Quaternion} b - * @returns {boolean} - */ - static equal(a, b) { - return (a.x === b.x) && (a.y === b.y) && (a.z === b.z) && (a.w === b.w) - } - - /** - * @param {Quaternion} a - * @param {Quaternion} b - * @param {number} [tolerance] - * @returns {boolean} - */ - static fuzzyEqual(a, b, tolerance) { - return ( - fuzzyEqual(a.x, b.x, tolerance) && - fuzzyEqual(a.y, b.y, tolerance) && - fuzzyEqual(a.z, b.z, tolerance) && - fuzzyEqual(a.w, b.w, tolerance) - ) - } - - static random(out = new Quaternion()) { - const theta1 = 2 * Math.PI * Math.random() - const theta2 = 2 * Math.PI * Math.random() - - const x0 = Math.random() - const r1 = Math.sqrt(1 - x0) - const r2 = Math.sqrt(x0) - - return out.set( - r1 * Math.sin(theta1), - r1 * Math.cos(theta1), - r2 * Math.sin(theta2), - r2 * Math.cos(theta2) - ) - } - - * [Symbol.iterator]() { - yield this.x - yield this.y - yield this.z - yield this.w - } - - /** - * @readonly - * @type {Quaternion} - */ - static Identity = Quaternion.identity() - - /** - * @readonly - * @type {Quaternion} - */ - static Zero = Quaternion.zero() -} - -/** - * Serialized form of `Quaternion`. - * - * @typedef QuaternionSerial - * @property {number} x - * @property {number} y - * @property {number} z - * @property {number} w - */ diff --git a/packages/math/src/core/angles/rotary.js b/packages/math/src/core/angles/rotary.js deleted file mode 100644 index 0c7967c4..00000000 --- a/packages/math/src/core/angles/rotary.js +++ /dev/null @@ -1,423 +0,0 @@ -import { TAU } from '../constants' -import { clamp, fuzzyEqual } from '../functions' -import { Matrix2 } from '../matrices' - -export class Rotary { - - /** - * @type {number} - */ - cos - - /** - * @type {number} - */ - sin - - constructor(cos = 1, sin = 0) { - this.cos = cos - this.sin = sin - } - - /** - * @param {number} cos - * @param {number} sin - */ - set(cos, sin) { - Rotary.set(cos, sin, this) - - return this - } - - /** - * @param { Rotary} v - * @returns {this} - */ - copy(v) { - Rotary.copy(v, this) - - return this - } - - /** - * @returns {Rotary} - */ - clone() { - return Rotary.copy(this) - } - - /** - * @returns {number} - */ - magnitudeSquared() { - return Rotary.magnitudeSquared(this) - } - - /** - * @returns {number} - */ - magnitude() { - return Rotary.magnitude(this) - } - - /** - * @returns {this} - */ - normalize() { - Rotary.normalize(this, this) - - return this - } - - /** - * @param {number} angle - * @returns {this} - */ - rotate(angle) { - Rotary.rotate(angle, this) - - return this - } - - /** - * @param {Rotary} rotary - * @returns {this} - */ - multiply(rotary) { - Rotary.multiply(rotary, this, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - multiplyScalar(scalar) { - Rotary.multiplyScalar(this, scalar, this) - - return this - } - - /** - * @param {Rotary} rotary - * @returns {number} - */ - dot(rotary) { - return Rotary.dot(this, rotary) - } - - /** - * @returns {this} - */ - reverse() { - Rotary.reverse(this, this) - - return this - } - - /** - * @param {Rotary} rotary - * @returns {boolean} - */ - equals(rotary) { - return Rotary.equal(rotary, this) - } - - /** - * @param {number} cos - * @param {number} sin - * @param {Rotary} out - * @returns {Rotary} - */ - static set(cos, sin, out = new Rotary()) { - out.cos = cos - out.sin = sin - - return out - } - - /** - * @param {Rotary} rotary - * @param {Rotary} out - * @returns {Rotary} - */ - static copy(rotary, out = new Rotary()) { - out.cos = rotary.cos - out.sin = rotary.sin - - return out - } - - /** - * @param {Rotary} value - */ - static serialize(value) { - return { - cos: value.cos, - sin: value.sin - } - } - - /** - * @param {RotarySerial} value - * @param {Rotary} [out] - */ - static deserialize(value, out = new Rotary()) { - out.cos = value.cos - out.sin = value.sin - - return out - } - - /** - * @param {unknown} value - * @returns {value is RotarySerial} - */ - static validateSerial(value) { - if (!value || typeof value !== 'object') { - return false - } - - if (!('cos' in value) || !('sin' in value)) { - return false - } - - return typeof value.cos === 'number' && typeof value.sin === 'number' - } - - /** - * @param {Rotary} out - * @returns {Rotary} - */ - static identity(out = new Rotary()) { - out.cos = 1 - out.sin = 0 - - return out - } - - /** - * @param {Rotary} out - * @returns {Rotary} - */ - static zero(out = new Rotary()) { - out.cos = 0 - out.sin = 0 - - return out - } - - /** - * @param {Rotary} q - * @returns {number} - */ - static magnitudeSquared(q) { - return q.cos * q.cos + q.sin * q.sin - } - - /** - * @param {Rotary} q - * @returns {number} - */ - static magnitude(q) { - return Math.sqrt(this.magnitudeSquared(q)) - } - - /** - * @param {Rotary} q - * @param {Rotary} out - * @returns {Rotary} - */ - static normalize(q, out = new Rotary()) { - const l = this.magnitude(q) - - if (l === 0) { - this.identity(out) - } else { - const inv = 1 / l - - out.cos = q.cos * inv - out.sin = q.sin * inv - } - - return out - } - - /** - * @param {number} angle - * @param {Rotary} out - */ - static rotate(angle, out = new Rotary()) { - const { cos: cosA, sin: sinA } = out - const cosB = Math.cos(angle) - const sinB = Math.sin(angle) - - out.cos = cosA * cosB - sinA * sinB - out.sin = sinA * cosB + cosA * sinB - - return out - } - - /** - * @param {Rotary} rotation1 - * @param {Rotary} rotation2 - * @param {Rotary} out - */ - static multiply(rotation1, rotation2, out = new Rotary()) { - const { cos: cosA, sin: sinA } = rotation1 - const { cos: cosB, sin: sinB } = rotation2 - - out.cos = cosA * cosB - sinA * sinB - out.sin = sinA * cosB + cosA * sinB - - return out - } - - /** - * @param {Rotary} rotation - * @param {number} scalar - * @param {Rotary} out - */ - static multiplyScalar(rotation, scalar, out = new Rotary()) { - out.cos = rotation.cos * scalar - out.sin = rotation.sin * scalar - - return out - } - - /** - * @param {Rotary} rotation - * @param {Rotary} out - */ - static reverse(rotation, out = new Rotary()) { - out.cos = -rotation.cos - out.sin = -rotation.sin - - return out - } - - /** - * @param {Rotary} a - * @param {Rotary} b - * @returns {number} - */ - static dot(a, b) { - return a.cos * b.cos + a.sin * b.sin - } - - /** - * @param {Rotary} a - * @param {Rotary} b - */ - static angleBetween(a, b) { - return 2 * Math.acos(Math.abs(clamp(Rotary.dot(a, b), -1, 1))) - } - - /** - * @param {number} angle - * @param {Rotary} out - * @returns {Rotary} - */ - static fromAngle(angle, out = new Rotary()) { - out.cos = Math.cos(angle) - out.sin = Math.sin(angle) - - return out - } - - /** - * @param {Matrix2} matrix - * @param {Rotary} out - */ - static fromRotationMatrix(matrix, out = new Rotary()) { - out.cos = matrix.a - out.sin = matrix.b - - return out - } - - /** - * @param {Rotary} out - */ - static toAngle(out) { - const angle = Math.acos(out.cos) - - if (out.sin >= 0) { - return angle - } - - return TAU - angle - } - - /** - * @param {Rotary} a - * @param {Rotary} b - * @param {number} t - * @param {Rotary} out - */ - static slerp(a, b, t, out = new Rotary()) { - const x = (a.cos + b.cos) * t - const y = (a.sin + b.sin) * t - const length = Math.sqrt(x * x + y * y) - - out.cos = x / length - out.sin = y / length - - return out - } - - /** - * @param {Rotary} rot1 - * @param {Rotary} rot2 - */ - static equal(rot1, rot2) { - return ( - rot1.cos === rot2.cos && - rot1.sin === rot2.sin - ) - } - - /** - * @param {Rotary} rot1 - * @param {Rotary} rot2 - * @param {number} [tolerance] - */ - static fuzzyEqual(rot1, rot2, tolerance) { - return ( - fuzzyEqual(rot1.cos, rot2.cos, tolerance) && - fuzzyEqual(rot1.sin, rot2.sin, tolerance) - ) - } - - static random() { - const angle = Math.random() * TAU - - return new Rotary(Math.cos(angle), Math.sin(angle)) - } - - * [Symbol.iterator]() { - yield this.cos - yield this.sin - } - - /** - * @readonly - * @type {Rotary} - */ - static Identity = Rotary.identity() - - /** - * @readonly - * @type {Rotary} - */ - static Zero = Rotary.zero() -} - -/** - * Serialized form of `Rotary`. - * - * @typedef RotarySerial - * @property {number} cos - * @property {number} sin - */ diff --git a/packages/math/src/core/basis/basis2.js b/packages/math/src/core/basis/basis2.js deleted file mode 100644 index 3a861b6e..00000000 --- a/packages/math/src/core/basis/basis2.js +++ /dev/null @@ -1,69 +0,0 @@ -import { Vector2 } from '../vectors' - -export class Basis2 { - - /** - * @type {Vector2} - */ - x - - /** - * @type {Vector2} - */ - y - - /** - * @param {Vector2} x - * @param {Vector2} y - */ - constructor(x = new Vector2(1, 0), y = new Vector2(0, 1)) { - this.x = x - this.y = y - } - - /** - * @param {Basis2} value - */ - static serialize(value) { - return { - x: Vector2.serialize(value.x), - y: Vector2.serialize(value.y) - } - } - - /** - * @param {Basis2Serial} value - * @param {Basis2} [out] - */ - static deserialize(value, out = new Basis2()) { - out.x = Vector2.deserialize(value.x, out.x) - out.y = Vector2.deserialize(value.y, out.y) - - return out - } - - /** - * @param {unknown} value - * @returns {value is Basis2Serial} - */ - static validateSerial(value) { - if (!value || typeof value !== 'object') { - return false - } - - if (!('x' in value) || !('y' in value)) { - return false - } - - return Vector2.validateSerial(value.x) && - Vector2.validateSerial(value.y) - } -} - -/** - * Serialized form of `Basis2`. - * - * @typedef Basis2Serial - * @property {any} x - * @property {any} y - */ diff --git a/packages/math/src/core/basis/basis3.js b/packages/math/src/core/basis/basis3.js deleted file mode 100644 index 0bf6f0a7..00000000 --- a/packages/math/src/core/basis/basis3.js +++ /dev/null @@ -1,78 +0,0 @@ -import { Vector3 } from '../vectors' - -export class Basis3 { - - /** - * @type {Vector3} - */ - x - - /** - * @type {Vector3} - */ - y - - /** - * @type {Vector3} - */ - z - constructor( - x = new Vector3(1, 0, 0), - y = new Vector3(0, 1, 0), - z = new Vector3(0, 0, 1) - ) { - this.x = x - this.y = y - this.z = z - } - - /** - * @param {Basis3} value - */ - static serialize(value) { - return { - x: Vector3.serialize(value.x), - y: Vector3.serialize(value.y), - z: Vector3.serialize(value.z) - } - } - - /** - * @param {Basis3Serial} value - * @param {Basis3} [out] - */ - static deserialize(value, out = new Basis3()) { - out.x = Vector3.deserialize(value.x, out.x) - out.y = Vector3.deserialize(value.y, out.y) - out.z = Vector3.deserialize(value.z, out.z) - - return out - } - - /** - * @param {unknown} value - * @returns {value is Basis3Serial} - */ - static validateSerial(value) { - if (!value || typeof value !== 'object') { - return false - } - - if (!('x' in value) || !('y' in value) || !('z' in value)) { - return false - } - - return Vector3.validateSerial(value.x) && - Vector3.validateSerial(value.y) && - Vector3.validateSerial(value.z) - } -} - -/** - * Serialized form of `Basis3`. - * - * @typedef Basis3Serial - * @property {any} x - * @property {any} y - * @property {any} z - */ diff --git a/packages/math/src/core/basis/index.js b/packages/math/src/core/basis/index.js deleted file mode 100644 index 7bebb937..00000000 --- a/packages/math/src/core/basis/index.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from './basis2' -export * from './basis3' diff --git a/packages/math/src/core/constants.js b/packages/math/src/core/constants.js deleted file mode 100644 index 2a0901a5..00000000 --- a/packages/math/src/core/constants.js +++ /dev/null @@ -1,10 +0,0 @@ -export const TAU = Math.PI * 2 -export const { PI } = Math -export const HALF_PI = Math.PI / 2 -export const QUARTER_PI = Math.PI / 4 -export const DEG2RAD = Math.PI / 180 -export const RAD2DEG = 180 / Math.PI -export const epilson = 0.0000001 -export const SQRT2 = Math.sqrt(2) -export const INV_SQRT2 = 1 / Math.sqrt(2) -export const PHI = (1 + Math.sqrt(5)) / 2 diff --git a/packages/math/src/core/functions/clamp.js b/packages/math/src/core/functions/clamp.js deleted file mode 100644 index fe9f9ba9..00000000 --- a/packages/math/src/core/functions/clamp.js +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Clamps a value between two numbers. - * - * @param {number} value - The number to clamp. - * @param {number} min - The minimal bound of the clamped number. - * @param {number} max - The maximum bound of the clamped number. - * @returns {number} - */ -export function clamp(value, min, max) { - if (value < min) return min - if (value > max) return max - - return value -} - -/** - * @param {number} value - * @param {number} step - * @returns {number} - */ -export function snap(value, step) { - return Math.round(value / step) * step -} - -/** - * @param {number} value - * @param {number} step - * @returns {number} - */ -export function snapDown(value, step) { - return Math.floor(value / step) * step -} - -/** - * @param {number} value - * @param {number} step - * @returns {number} - */ -export function snapUp(value, step) { - return Math.ceil(value / step) * step -} diff --git a/packages/math/src/core/functions/easing.js b/packages/math/src/core/functions/easing.js deleted file mode 100644 index 9fbd9752..00000000 --- a/packages/math/src/core/functions/easing.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * @param {number} value - * @returns {number} - */ -export function smoothStep(value) { - return value * value * (3 - 2 * value) -}; - -/** - * @param {number} value - */ -export function smootherStep(value) { - return value * value * value * (value * (value * 6 - 15) + 10) -} diff --git a/packages/math/src/core/functions/index.js b/packages/math/src/core/functions/index.js deleted file mode 100644 index 8ddafb09..00000000 --- a/packages/math/src/core/functions/index.js +++ /dev/null @@ -1,6 +0,0 @@ -export * from './clamp' -export * from './easing' -export * from './indexmap' -export * from './interpolation' -export * from './mathematical' -export * from './noise' diff --git a/packages/math/src/core/functions/indexmap.js b/packages/math/src/core/functions/indexmap.js deleted file mode 100644 index d75a6edb..00000000 --- a/packages/math/src/core/functions/indexmap.js +++ /dev/null @@ -1,26 +0,0 @@ -import { Vector2, Vector3 } from '../vectors' - -/** - * @param {number} value - * @param {number} width - * @returns {Vector2} - */ -export function mapToIndex2D(value, width) { - return new Vector2(value % width, Math.floor(value / width)) -} - -/** - * @param {number} index - * @param {number} width - * @param {number} height - * @returns {Vector3} - */ -export function mapToIndex3D(index, width, height) { - const depthMax = width * height - const rem = index % depthMax - const z = Math.floor(index / depthMax) - const y = Math.floor(rem / width) - const x = rem % width - - return new Vector3(x, y, z) -} diff --git a/packages/math/src/core/functions/interpolation.js b/packages/math/src/core/functions/interpolation.js deleted file mode 100644 index a2a27631..00000000 --- a/packages/math/src/core/functions/interpolation.js +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Interpolates between two numbers by a constant t. - * - * @param {number} from - The minimal bound of the interpolation. - * @param {number} to - The maximum bound of the interpolation. - * @param {number} t - A number between 0 and 1 to interpolate - * by.Any other number greater than 1 or less than 0 will - * extapolate beyond b or a respectively. - * @returns {number} - */ -export function lerp(from, to, t) { - return from + t * (to - from) -} - -/** - * Gets the interpolation constant between two numbers give a value - * between them. - * - * @param {number} from - The minimal bound of the interpolation. - * @param {number} to - The maximum bound of the interpolation. - * @param {number} value - The value between the bounds. - * @returns {number} A number between 0 and 1 if value respects - * the bounds. - */ -export function inverseLerp(from, to, value) { - return (value - from) / (to - from) -} - -/** - * Maps a value from one range to another. - * - * @param {number} v - * @param {number} x1 - * @param {number} y1 - * @param {number} x2 - * @param {number} y2 - * @returns {number} - */ -export function remap(v, x1, y1, x2, y2) { - return x2 + v * (y2 - x2) / (y1 - x1) -} - -export const Interpolation = { - - /** - * @param {number} p0 - * @param {number} p1 - * @param {number} t - * - * @returns {number} - */ - Linear(p0, p1, t) { - return (p1 - p0) * t + p0 - }, - - /** - * @param {number} p0 - * @param {number} p1 - * @param {number} p2 - * @param {number} p3 - * @param {number} t - * - * @returns {number} - */ - CatmullRom(p0, p1, p2, p3, t) { - const v0 = (p2 - p0) * 0.5 - const v1 = (p3 - p1) * 0.5 - const t2 = t * t - const t3 = t * t2 - - return (2 * p1 - 2 * p2 + v0 + v1) * t3 + (-3 * p1 + 3 * p2 - 2 * v0 - v1) * t2 + v0 * t + p1 - }, - - /** - * @param {number} p0 - * @param {number} p1 - * @param {number} t - */ - cosine(p0, p1, t) { - const c = (1 - Math.cos(t * 3.1415927)) * 0.5 - - return (1 - c) * p0 + c * p1 - } -} diff --git a/packages/math/src/core/functions/mathematical.js b/packages/math/src/core/functions/mathematical.js deleted file mode 100644 index 6bf24654..00000000 --- a/packages/math/src/core/functions/mathematical.js +++ /dev/null @@ -1,126 +0,0 @@ -import { DEG2RAD, RAD2DEG, epilson } from '../constants' - -/** - * Returns the square of a number. - * - * @param {number} x - The number to square. - * @returns {number} - */ -export function sq(x) { - return x * x -} - -/** - * Returns the power of a number by a given exponent. - * - * @param {number} x - The number to power. - * @param {number} [e=2] - The number to power by. - * @returns {number} - */ -export function exp(x, e = 2) { - return x ** e -} - -/** - * Returns the square root pf a number. - * - * @param {number} x - The number to root. - * @returns {number} - */ -export function sqrt(x) { - return Math.sqrt(x) -} - -/** - * @param {number} value - */ -export function invert(value) { - return 1 / value -} - -/** - * Rounds a given value to a given precision. - * - * @param {number} number - The number to round. - * @param {number} [precision=4] - How many decimal places there should be. - * @returns {number} - */ -export function round(number, precision = 4) { - const x = 10 ** precision - - return Math.round(number * x) / x -} - -/** - * Returns a unique number given from a pair of numbers. - * @param {number} a - * @param {number} b - * @returns {number} - */ -export function naturalizePair(a, b) { - if (a > b) return (a + b) * (a + b + 1) / 2 + a - - return (a + b) * (a + b + 1) / 2 + b -} - -/** - * Converts a degree to a radian. - * - * @param {number} deg - Number to convert. - * @returns {number} - */ -export function degToRad(deg) { - return deg * DEG2RAD -} - -/** - * Converts a radian to a degree. - * - * @param {number} rad - Number to convert. - * @returns {number} - */ -export function radToDeg(rad) { - return rad * RAD2DEG -} - -/** - * @param {number} value - * @param {number} min - * @param {number} max - */ -export function wrap(value, min, max) { - const range = max - min - - return (min + ((((value - min) % range) + range) % range)) -} - -/** - * @param {number} a - * @param {number} b - * @returns {number} - */ -export function cantorPair(a, b) { - return 0.5 * (a + b) * (a + b + 1) + b - -} - -/** - * @param {number} a - * @param {number} b - * @returns {number} - */ -export function cantorPairSigned(a, b) { - const x = (a >= 0.0 ? 2.0 * a : (-2.0 * a) - 1.0) - const y = (b >= 0.0 ? 2.0 * b : (-2.0 * b) - 1.0) - - return cantorPair(x, y) -} - -/** - * @param {number} a - * @param {number} b - * @param {number} tolerance - */ -export function fuzzyEqual(a, b, tolerance = epilson) { - return Math.abs(a - b) <= tolerance -} diff --git a/packages/math/src/core/functions/noise.js b/packages/math/src/core/functions/noise.js deleted file mode 100644 index 7adad8f6..00000000 --- a/packages/math/src/core/functions/noise.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Creates a random number between the parameters. - * - * @param {number} [min=0] - The minimal bound of the random number. - * @param {number} [max=1] - The maximum bound of the random number. - * @returns {number} - */ -export function rand(min = 0, max = 1) { - return Math.random() * (max - min) + min -} diff --git a/packages/math/src/core/index.js b/packages/math/src/core/index.js deleted file mode 100644 index c36a82d4..00000000 --- a/packages/math/src/core/index.js +++ /dev/null @@ -1,7 +0,0 @@ -export * from './affines' -export * from './angles' -export * from './basis' -export * from './functions' -export * from './matrices' -export * from './vectors' -export * from './constants' diff --git a/packages/math/src/core/matrices/index.js b/packages/math/src/core/matrices/index.js deleted file mode 100644 index b68d665b..00000000 --- a/packages/math/src/core/matrices/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export * from './matrix2' -export * from './matrix3' -export * from './matrix4' diff --git a/packages/math/src/core/matrices/matrix2.js b/packages/math/src/core/matrices/matrix2.js deleted file mode 100644 index 6e296875..00000000 --- a/packages/math/src/core/matrices/matrix2.js +++ /dev/null @@ -1,494 +0,0 @@ -import { invert } from '../functions' - -/** - * Represents a 2x2 matrix. - * - * | a | c | - * |---|---| - * | b | d | - */ -export class Matrix2 { - - /** - * @type {number} - */ - a - - /** - * @type {number} - */ - b - - /** - * @type {number} - */ - c - - /** - * @type {number} - */ - d - - /** - * @param {number} e11 - * @param {number} e12 - * @param {number} e21 - * @param {number} e22 - */ - constructor(e11 = 1, e12 = 0, e21 = 0, e22 = 1) { - this.a = e11 - this.b = e21 - this.c = e12 - this.d = e22 - } - - /** - * @param {number} e11 - * @param {number} e12 - * @param {number} e21 - * @param {number} e22 - * @returns {this} - */ - set(e11, e12, e21, e22) { - Matrix2.set(e11, e12, e21, e22, this) - - return this - } - - /** - * @param {Matrix2} other - */ - copy(other) { - Matrix2.copy(other, this) - - return this - } - - /** - * @returns {Matrix2} - */ - clone() { - return Matrix2.copy(this) - } - - /** - * @returns {this} - */ - transpose() { - Matrix2.transpose(this, this) - - return this - } - - /** - * @returns {number} - */ - determinant() { - return Matrix2.determinant(this) - } - - /** - * @returns {number} - */ - trace() { - return Matrix2.trace(this) - } - - /** - * @param {Matrix2} matrix - * @returns {this} - */ - add(matrix) { - Matrix2.add(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - addScalar(scalar) { - Matrix2.addScalar(this, scalar, this) - - return this - } - - /** - * @param {Matrix2} matrix - * @returns {this} - */ - subtract(matrix) { - Matrix2.subtract(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - subtractScalar(scalar) { - Matrix2.subtractScalar(this, scalar, this) - - return this - } - - /** - * @param {Matrix2} matrix - * @returns {this} - */ - multiply(matrix) { - Matrix2.multiply(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - multiplyScalar(scalar) { - Matrix2.multiplyScalar(this, scalar, this) - - return this - } - - /** - * @param {Matrix2} matrix - * @returns {this} - */ - divide(matrix) { - Matrix2.divide(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - divideScalar(scalar) { - Matrix2.divideScalar(this, scalar, this) - - return this - } - - /** - * @returns {this} - */ - invert() { - Matrix2.invert(this, this) - - return this - } - - /** - * @param {Matrix2} matrix - * @returns {boolean} - */ - equals(matrix) { - return Matrix2.equal(this, matrix) - } - - /** - * @param {number} e11 - * @param {number} e12 - * @param {number} e21 - * @param {number} e22 - * @param {Matrix2} out - */ - static set(e11, e12, e21, e22, out = new Matrix2()) { - out.a = e11 - out.b = e21 - out.c = e12 - out.d = e22 - - return out - } - - /** - * @param {Matrix2} matrix - * @param {Matrix2} out - * @returns {Matrix2} - */ - static copy(matrix, out = new Matrix2()) { - out.a = matrix.a - out.b = matrix.b - out.c = matrix.c - out.d = matrix.d - - return out - } - - /** - * @param {Matrix2} value - * @returns {Matrix2Serial} - */ - static serialize(value) { - return [value.a, value.b, value.c, value.d] - } - - /** - * @param {Matrix2Serial} value - * @param {Matrix2} [out] - */ - static deserialize(value, out = new Matrix2()) { - out.a = value[0] - out.b = value[1] - out.c = value[2] - out.d = value[3] - - return out - } - - /** - * @param {unknown} value - * @returns {value is Matrix2Serial} - */ - static validateSerial(value) { - return Array.isArray(value) && - value.length === 4 && - typeof value[0] === 'number' && - typeof value[1] === 'number' && - typeof value[2] === 'number' && - typeof value[3] === 'number' - } - - /** - * @param {Matrix2} matrix - * @param {Matrix2} out - * @returns {Matrix2} - */ - static transpose(matrix, out = new Matrix2()) { - const { b } = matrix - - out.a = matrix.a - out.b = matrix.c - out.c = b - out.d = matrix.d - - return out - } - - /** - * @param {Matrix2} out - * @returns {Matrix2} - */ - static identity(out = new Matrix2()) { - this.set(1, 0, 0, 1, out) - - return out - } - - /** - * @param {Matrix2} out - * @returns {Matrix2} - */ - static zero(out = new Matrix2()) { - this.set(0, 0, 0, 0, out) - - return out - } - - /** - * @param {Matrix2} matrix - * @returns {number} - */ - static determinant(matrix) { - return matrix.a * matrix.d - matrix.c * matrix.b - } - - /** - * @param {Matrix2} matrix - * @returns {number} - */ - static trace(matrix) { - return matrix.a + matrix.d - } - - /** - * @param {Matrix2} matrix1 - * @param {Matrix2} matrix2 - * @param {Matrix2} out - * @returns {Matrix2} - */ - static add(matrix1, matrix2, out = new Matrix2()) { - out.a = matrix1.a + matrix2.a - out.b = matrix1.b + matrix2.b - out.c = matrix1.c + matrix2.c - out.d = matrix1.d + matrix2.d - - return out - } - - /** - * @param {Matrix2} matrix - * @param {number} scalar - * @param {Matrix2} out - * @returns {Matrix2} - */ - static addScalar(matrix, scalar, out = new Matrix2()) { - out.a = matrix.a + scalar - out.b = matrix.b + scalar - out.c = matrix.c + scalar - out.d = matrix.d + scalar - - return out - } - - /** - * @param {Matrix2} matrix1 - * @param {Matrix2} matrix2 - * @param {Matrix2} out - * @returns {Matrix2} - */ - static subtract(matrix1, matrix2, out = new Matrix2()) { - out.a = matrix1.a - matrix2.a - out.b = matrix1.b - matrix2.b - out.c = matrix1.c - matrix2.c - out.d = matrix1.d - matrix2.d - - return out - } - - /** - * @param {Matrix2} matrix - * @param {number} scalar - * @param {Matrix2} out - * @returns {Matrix2} - */ - static subtractScalar(matrix, scalar, out = new Matrix2()) { - out.a = matrix.a - scalar - out.b = matrix.b - scalar - out.c = matrix.c - scalar - out.d = matrix.d - scalar - - return out - } - - /** - * @param {Matrix2} matrix1 - * @param {Matrix2} matrix2 - * @param {Matrix2} out - * @returns {Matrix2} - */ - static multiply(matrix1, matrix2, out = new Matrix2()) { - const { a: aa, b: ab, c: ac, d: ad } = matrix1 - const { a: ba, b: bb, c: bc, d: bd } = matrix2 - - out.a = aa * ba + ac * bb - out.b = ab * ba + ad * bb - out.c = aa * bc + ac * bd - out.d = ab * bc + ad * bd - - return out - } - - /** - * @param {Matrix2} matrix - * @param {number} scalar - * @param {Matrix2} out - * @returns {Matrix2} - */ - static multiplyScalar(matrix, scalar, out = new Matrix2()) { - out.a = matrix.a * scalar - out.b = matrix.b * scalar - out.c = matrix.c * scalar - out.d = matrix.d * scalar - - return out - } - - /** - * @param {Matrix2} matrix1 - * @param {Matrix2} matrix2 - * @param {Matrix2} out - * @returns {Matrix2} - */ - static divide(matrix1, matrix2, out = new Matrix2()) { - const multiplier = this.invert(matrix2) - - this.multiply(matrix1, multiplier, out) - - return out - } - - /** - * @param {Matrix2} matrix - * @param {number} scalar - * @param {Matrix2} out - * @returns {Matrix2} - */ - static divideScalar(matrix, scalar, out = new Matrix2()) { - this.multiplyScalar(matrix, invert(scalar), out) - - return out - } - - /** - * @param {Matrix2} matrix - * @param {Matrix2} out - * @returns {Matrix2} - */ - static invert(matrix, out = new Matrix2()) { - const { a, b, c, d } = matrix - const det = this.determinant(matrix) - - if (det === 0) { - return this.zero(out) - } - - const detInv = invert(det) - - out.a = detInv * d - out.b = detInv * -b - out.c = detInv * -c - out.d = detInv * a - - return out - } - - /** - * @param {Matrix2} matrix1 - * @param {Matrix2} matrix2 - * @returns {boolean} - */ - static equal(matrix1, matrix2) { - return ( - matrix1.a === matrix2.a || - matrix1.b === matrix2.b || - matrix1.c === matrix2.c || - matrix1.d === matrix2.d - ) - } - - /** - * Allows iteration of components. - * - * @yields {number} - */ - * [Symbol.iterator]() { - yield this.a - yield this.b - yield this.c - yield this.d - } - - /** - * @type {Matrix2} - */ - static Identity = Matrix2.identity() - - /** - * @type {Matrix2} - */ - static Zero = Matrix2.zero() -} - -/** - * Serialized form of `Matrix2`. - * - * @typedef Matrix2Serial - * @type {[number, number, number, number]} - */ diff --git a/packages/math/src/core/matrices/matrix3.js b/packages/math/src/core/matrices/matrix3.js deleted file mode 100644 index 45d2078f..00000000 --- a/packages/math/src/core/matrices/matrix3.js +++ /dev/null @@ -1,671 +0,0 @@ -import { invert } from '../functions' -import { Vector3 } from '../vectors' - -/** - * Represents a 3x3 square matrix. - * Can br used to represent 3 dimensional rotation, scale and skew. - * - * Column major. - * - * | a | d | g | - * |---|---|---| - * | b | e | h | - * | c | f | i | - */ -export class Matrix3 { - - /** - * @type {number} - */ - a - - /** - * @type {number} - */ - b - - /** - * @type {number} - */ - c - - /** - * @type {number} - */ - d - - /** - * @type {number} - */ - e - - /** - * @type {number} - */ - f - - /** - * @type {number} - */ - g - - /** - * @type {number} - */ - h - - /** - * @type {number} - */ - i - constructor( - e11 = 1, - e12 = 0, - e13 = 0, - e21 = 0, - e22 = 1, - e23 = 0, - e31 = 0, - e32 = 0, - e33 = 1 - ) { - this.a = e11 - this.b = e21 - this.c = e31 - this.d = e12 - this.e = e22 - this.f = e32 - this.g = e13 - this.h = e23 - this.i = e33 - } - - /** - * @param {number} e11 - * @param {number} e12 - * @param {number} e13 - * @param {number} e21 - * @param {number} e22 - * @param {number} e23 - * @param {number} e31 - * @param {number} e32 - * @param {number} e33 - * @returns {this} - */ - set(e11, e12, e13, e21, e22, e23, e31, e32, e33) { - Matrix3.set(e11, e12, e13, e21, e22, e23, e31, e32, e33, this) - - return this - } - - /** - * @param {Matrix3} other - */ - copy(other) { - Matrix3.copy(other, this) - - return this - } - - /** - * @returns {Matrix3} - */ - clone() { - return Matrix3.copy(this) - } - - /** - * @returns {this} - */ - transpose() { - Matrix3.transpose(this, this) - - return this - } - - /** - * @returns {number} - */ - determinant() { - return Matrix3.determinant(this) - } - - /** - * @returns {number} - */ - trace() { - return Matrix3.trace(this) - } - - /** - * @param {Matrix3} matrix - * @returns {this} - */ - add(matrix) { - Matrix3.add(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - addScalar(scalar) { - Matrix3.addScalar(this, scalar, this) - - return this - } - - /** - * @param {Matrix3} matrix - * @returns {this} - */ - subtract(matrix) { - Matrix3.subtract(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - subtractScalar(scalar) { - Matrix3.subtractScalar(this, scalar, this) - - return this - } - - /** - * @param {Matrix3} matrix - * @returns {this} - */ - multiply(matrix) { - Matrix3.multiply(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - multiplyScalar(scalar) { - Matrix3.multiplyScalar(this, scalar, this) - - return this - } - - /** - * @param {Matrix3} matrix - * @returns {this} - */ - divide(matrix) { - Matrix3.divide(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - divideScalar(scalar) { - Matrix3.divideScalar(this, scalar, this) - - return this - } - - /** - * @returns {this} - */ - invert() { - Matrix3.invert(this, this) - - return this - } - - /** - * @param {Matrix3} matrix - * @returns {boolean} - */ - equals(matrix) { - return Matrix3.equal(this, matrix) - } - - /** - * @param {number} e11 - * @param {number} e12 - * @param {number} e13 - * @param {number} e21 - * @param {number} e22 - * @param {number} e23 - * @param {number} e31 - * @param {number} e32 - * @param {number} e33 - * @param {Matrix3} out - */ - static set( - e11, - e12, - e13, - e21, - e22, - e23, - e31, - e32, - e33, - out = new Matrix3() - ) { - out.a = e11 - out.b = e21 - out.c = e31 - out.d = e12 - out.e = e22 - out.f = e32 - out.g = e13 - out.h = e23 - out.i = e33 - - return out - } - - /** - * @param {Matrix3} matrix - * @param {Matrix3} [out=new Matrix3()] - */ - static copy(matrix, out = new Matrix3()) { - out.a = matrix.a - out.b = matrix.b - out.c = matrix.c - out.d = matrix.d - out.e = matrix.e - out.f = matrix.f - out.g = matrix.g - out.h = matrix.h - out.i = matrix.i - - return out - } - - /** - * @param {Matrix3} value - * @returns {Matrix3Serial} - */ - static serialize(value) { - return [value.a, value.b, value.c, value.d, value.e, value.f, value.g, value.h, value.i] - } - - /** - * @param {Matrix3Serial} value - * @param {Matrix3} [out] - */ - static deserialize(value, out = new Matrix3()) { - const serial = /** @type {Matrix3Serial} */ (value) - - out.a = serial[0] - out.b = serial[1] - out.c = serial[2] - out.d = serial[3] - out.e = serial[4] - out.f = serial[5] - out.g = serial[6] - out.h = serial[7] - out.i = serial[8] - - return out - } - - /** - * @param {unknown} value - * @returns {value is Matrix3Serial} - */ - static validateSerial(value) { - return Array.isArray(value) && - value.length === 9 && - typeof value[0] === 'number' && - typeof value[1] === 'number' && - typeof value[2] === 'number' && - typeof value[3] === 'number' && - typeof value[4] === 'number' && - typeof value[5] === 'number' && - typeof value[6] === 'number' && - typeof value[7] === 'number' && - typeof value[8] === 'number' - } - - /** - * @param {Matrix3} matrix - * @param {Matrix3} [out=new Matrix3()] - */ - static transpose(matrix, out = new Matrix3()) { - const { a, b, c, d, e, f, g, h, i } = matrix - - out.a = a - out.b = d - out.c = g - out.d = b - out.e = e - out.f = h - out.g = c - out.h = f - out.i = i - - return out - } - - /** - * @param {Matrix3} [out= new Matrix3()] - * @returns - */ - static identity(out = new Matrix3()) { - Matrix3.set(1, 0, 0, 0, 1, 0, 0, 0, 1, out) - - return out - } - - /** - * @param {Matrix3} out - * @returns {Matrix3} - */ - static zero(out = new Matrix3()) { - Matrix3.set(0, 0, 0, 0, 0, 0, 0, 0, 0, out) - - return out - } - - /** - * @param {Matrix3} matrix - */ - static determinant(matrix) { - return matrix.a * matrix.e * matrix.i - matrix.a * matrix.f * matrix.h - matrix.b * matrix.d * matrix.i + matrix.b * matrix.f * matrix.g + matrix.c * matrix.d * matrix.h - matrix.c * matrix.e * matrix.g - } - - /** - * @param {Matrix3} matrix - * @returns {number} - */ - static trace(matrix) { - return matrix.a + matrix.e + matrix.i - } - - /** - * @param {Matrix3} matrix1 - * @param {Matrix3} matrix2 - * @param {Matrix3} out - * @returns {Matrix3} - */ - static add(matrix1, matrix2, out = new Matrix3()) { - out.a = matrix1.a + matrix2.a - out.b = matrix1.b + matrix2.b - out.c = matrix1.c + matrix2.c - out.d = matrix1.d + matrix2.d - out.e = matrix1.e + matrix2.e - out.f = matrix1.f + matrix2.f - out.g = matrix1.g + matrix2.g - out.h = matrix1.h + matrix2.h - out.i = matrix1.i + matrix2.i - - return out - } - - /** - * @param {Matrix3} matrix - * @param {number} scalar - * @param {Matrix3} out - * @returns {Matrix3} - */ - static addScalar(matrix, scalar, out = new Matrix3()) { - out.a = matrix.a + scalar - out.b = matrix.b + scalar - out.c = matrix.c + scalar - out.d = matrix.d + scalar - out.e = matrix.e + scalar - out.f = matrix.f + scalar - out.g = matrix.g + scalar - out.h = matrix.h + scalar - out.i = matrix.i + scalar - - return out - } - - /** - * @param {Matrix3} matrix1 - * @param {Matrix3} matrix2 - * @param {Matrix3} out - * @returns {Matrix3} - */ - static subtract(matrix1, matrix2, out = new Matrix3()) { - out.a = matrix1.a - matrix2.a - out.b = matrix1.b - matrix2.b - out.c = matrix1.c - matrix2.c - out.d = matrix1.d - matrix2.d - out.e = matrix1.e - matrix2.e - out.f = matrix1.f - matrix2.f - out.g = matrix1.g - matrix2.g - out.h = matrix1.h - matrix2.h - out.i = matrix1.i - matrix2.i - - return out - } - - /** - * @param {Matrix3} matrix - * @param {number} scalar - * @param {Matrix3} out - * @returns {Matrix3} - */ - static subtractScalar(matrix, scalar, out = new Matrix3()) { - out.a = matrix.a - scalar - out.b = matrix.b - scalar - out.c = matrix.c - scalar - out.d = matrix.d - scalar - out.e = matrix.e - scalar - out.f = matrix.f - scalar - out.g = matrix.g - scalar - out.h = matrix.h - scalar - out.i = matrix.i - scalar - - return out - } - - /** - * @param {Matrix3} matrix1 - * @param {Matrix3} matrix2 - * @param {Matrix3} out - */ - static multiply(matrix1, matrix2, out = new Matrix3()) { - const a11 = matrix1.a, - a12 = matrix1.d, - a13 = matrix1.g - const a21 = matrix1.b, - a22 = matrix1.e, - a23 = matrix1.h - const a31 = matrix1.c, - a32 = matrix1.f, - a33 = matrix1.i - - const b11 = matrix2.a, - b12 = matrix2.d, - b13 = matrix2.g - const b21 = matrix2.b, - b22 = matrix2.e, - b23 = matrix2.h - const b31 = matrix2.c, - b32 = matrix2.f, - b33 = matrix2.i - - out.a = a11 * b11 + a12 * b21 + a13 * b31 - out.d = a11 * b12 + a12 * b22 + a13 * b32 - out.g = a11 * b13 + a12 * b23 + a13 * b33 - - out.b = a21 * b11 + a22 * b21 + a23 * b31 - out.e = a21 * b12 + a22 * b22 + a23 * b32 - out.h = a21 * b13 + a22 * b23 + a23 * b33 - - out.c = a31 * b11 + a32 * b21 + a33 * b31 - out.f = a31 * b12 + a32 * b22 + a33 * b32 - out.i = a31 * b13 + a32 * b23 + a33 * b33 - - return out - } - - /** - * @param {Matrix3} matrix - * @param {number} scalar - * @param {Matrix3} [out=new Matrix3()] - */ - static multiplyScalar(matrix, scalar, out = new Matrix3()) { - out.a = matrix.a * scalar - out.b = matrix.b * scalar - out.c = matrix.c * scalar - out.d = matrix.d * scalar - out.e = matrix.e * scalar - out.f = matrix.f * scalar - out.g = matrix.g * scalar - out.h = matrix.h * scalar - out.i = matrix.i * scalar - - return out - } - - /** - * @param {Matrix3} matrix1 - * @param {Matrix3} matrix2 - * @param {Matrix3} out - */ - static divide(matrix1, matrix2, out = new Matrix3()) { - const multiplier = this.invert(matrix2) - - this.multiply(matrix1, multiplier, out) - - return out - } - - /** - * @param {Matrix3} matrix - * @param {number} scalar - * @param {Matrix3} [out=new Matrix3()] - */ - static divideScalar(matrix, scalar, out = new Matrix3()) { - this.multiplyScalar(matrix, invert(scalar), out) - - return out - } - - /** - * @param {Matrix3} matrix - * @param {Matrix3} [out=new Matrix3()] - */ - static invert(matrix, out = new Matrix3()) { - const - { a, b, c, d, e, f, g, h, i } = matrix, - - t11 = i * e - h * f, - t12 = h * c - i * b, - t13 = f * b - e * c, - - det = a * t11 + d * t12 + g * t13 - - if (det === 0) return Matrix3.zero(out) - - const detInv = invert(det) - - out.a = t11 * detInv - out.b = t12 * detInv - out.c = t13 * detInv - out.d = (g * f - i * d) * detInv - out.e = (i * a - g * c) * detInv - out.f = (d * c - f * a) * detInv - out.g = (h * d - g * e) * detInv - out.h = (g * b - h * a) * detInv - out.i = (e * a - d * b) * detInv - - return out - } - - /** - * @param {Matrix3} matrix1 - * @param {Matrix3} matrix2 - */ - static equal(matrix1, matrix2) { - return ( - matrix1.a === matrix2.a && - matrix1.b === matrix2.b && - matrix1.c === matrix2.c && - matrix1.d === matrix2.d && - matrix1.e === matrix2.e && - matrix1.f === matrix2.f && - matrix1.g === matrix2.g && - matrix1.h === matrix2.h && - matrix1.i === matrix2.i - ) - } - - /** - * @param {Vector3} euler - * @param {Matrix3} [out=new Matrix3()] - */ - static fromEuler(euler, out = new Matrix3()) { - const { x } = euler, - { y } = euler, - { z } = euler - const a = Math.cos(x), - b = Math.sin(x) - const c = Math.cos(y), - d = Math.sin(y) - const e = Math.cos(z), - f = Math.sin(z) - const ae = a * e, - af = a * f, - be = b * e, - bf = b * f - - out.a = c * e - out.d = -c * f - out.g = d - - out.b = af + be * d - out.e = ae - bf * d - out.h = -b * c - - out.c = bf - ae * d - out.f = be + af * d - out.i = a * c - - return out - } - - /** - * Allows iteration of components. - * - * @yields {number} - */ - * [Symbol.iterator]() { - yield this.a - yield this.b - yield this.c - yield this.d - yield this.e - yield this.f - yield this.g - yield this.h - yield this.i - } - - /** - * @type {Matrix3} - */ - static Identity = Matrix3.identity() - - /** - * @type {Matrix3} - */ - static Zero = Matrix3.zero() -} - -/** - * @typedef Matrix3Serial - * @type {[number, number, number, number, number, number, number, number, number]} - */ diff --git a/packages/math/src/core/matrices/matrix4.js b/packages/math/src/core/matrices/matrix4.js deleted file mode 100644 index 51aedc7e..00000000 --- a/packages/math/src/core/matrices/matrix4.js +++ /dev/null @@ -1,985 +0,0 @@ -import { invert } from '../functions' - -/** - * Represents a 4x4 square matrix. - * Can be used to represent 3 dimensional rotation, scale and skew. - * - * Column major. - * - * | a | e | i | m | - * |---|---|---|---| - * | b | f | j | n | - * | c | g | k | o | - * | d | h | l | p | - */ -export class Matrix4 { - - /** - * @type {number} - */ - a = 1 - - /** - * @type {number} - */ - b = 0 - - /** - * @type {number} - */ - c = 0 - - /** - * @type {number} - */ - d = 0 - - /** - * @type {number} - */ - e = 0 - - /** - * @type {number} - */ - f = 1 - - /** - * @type {number} - */ - g = 0 - - /** - * @type {number} - */ - h = 0 - - /** - * @type {number} - */ - i = 0 - - /** - * @type {number} - */ - j = 0 - - /** - * @type {number} - */ - k = 1 - - /** - * @type {number} - */ - l = 0 - - /** - * @type {number} - */ - m = 0 - - /** - * @type {number} - */ - n = 0 - - /** - * @type {number} - */ - o = 0 - - /** - * @type {number} - */ - p = 1 - - /** - * @param {number} n11 - * @param {number} n12 - * @param {number} n13 - * @param {number} n14 - * @param {number} n21 - * @param {number} n22 - * @param {number} n23 - * @param {number} n24 - * @param {number} n31 - * @param {number} n32 - * @param {number} n33 - * @param {number} n34 - * @param {number} n41 - * @param {number} n42 - * @param {number} n43 - * @param {number} n44 - */ - constructor( - n11 = 1, - n12 = 0, - n13 = 0, - n14 = 0, - n21 = 0, - n22 = 1, - n23 = 0, - n24 = 0, - n31 = 0, - n32 = 0, - n33 = 1, - n34 = 0, - n41 = 0, - n42 = 0, - n43 = 0, - n44 = 1 - ) { - Matrix4.set( - n11, - n12, - n13, - n14, - n21, - n22, - n23, - n24, - n31, - n32, - n33, - n34, - n41, - n42, - n43, - n44, - this - ) - } - - /** - * @param {number} e11 - * @param {number} e12 - * @param {number} e13 - * @param {number} e14 - * @param {number} e21 - * @param {number} e22 - * @param {number} e23 - * @param {number} e24 - * @param {number} e31 - * @param {number} e32 - * @param {number} e33 - * @param {number} e34 - * @param {number} e41 - * @param {number} e42 - * @param {number} e43 - * @param {number} e44 - * @returns {this} - */ - set(e11, e12, e13, e14, e21, e22, e23, e24, e31, e32, e33, e34, e41, e42, e43, e44) { - Matrix4.set(e11, e12, e13, e14, e21, e22, e23, e24, e31, e32, e33, e34, e41, e42, e43, e44, this) - - return this - } - - /** - * @param {Matrix4} other - */ - copy(other) { - Matrix4.copy(other, this) - - return this - } - - /** - * @returns {Matrix4} - */ - clone() { - return Matrix4.copy(this) - } - - /** - * @returns {this} - */ - transpose() { - Matrix4.transpose(this, this) - - return this - } - - /** - * @returns {number} - */ - determinant() { - return Matrix4.determinant(this) - } - - /** - * @returns {number} - */ - trace() { - return Matrix4.trace(this) - } - - /** - * @param {Matrix4} matrix - * @returns {this} - */ - add(matrix) { - Matrix4.add(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - addScalar(scalar) { - Matrix4.addScalar(this, scalar, this) - - return this - } - - /** - * @param {Matrix4} matrix - * @returns {this} - */ - subtract(matrix) { - Matrix4.subtract(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - subtractScalar(scalar) { - Matrix4.subtractScalar(this, scalar, this) - - return this - } - - /** - * @param {Matrix4} matrix - * @returns {this} - */ - multiply(matrix) { - Matrix4.multiply(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - multiplyScalar(scalar) { - Matrix4.multiplyScalar(this, scalar, this) - - return this - } - - /** - * @param {Matrix4} matrix - * @returns {this} - */ - divide(matrix) { - Matrix4.divide(this, matrix, this) - - return this - } - - /** - * @param {number} scalar - * @returns {this} - */ - divideScalar(scalar) { - Matrix4.divideScalar(this, scalar, this) - - return this - } - - /** - * @returns {this} - */ - invert() { - Matrix4.invert(this, this) - - return this - } - - /** - * @param {Matrix4} matrix - * @returns {boolean} - */ - equals(matrix) { - return Matrix4.equal(this, matrix) - } - - /** - * @param {number} n11 - * @param {number} n12 - * @param {number} n13 - * @param {number} n14 - * @param {number} n21 - * @param {number} n22 - * @param {number} n23 - * @param {number} n24 - * @param {number} n31 - * @param {number} n32 - * @param {number} n33 - * @param {number} n34 - * @param {number} n41 - * @param {number} n42 - * @param {number} n43 - * @param {number} n44 - * @param {Matrix4} out - * @returns {Matrix4} - */ - static set( - n11 = 1, - n12 = 0, - n13 = 0, - n14 = 0, - n21 = 0, - n22 = 1, - n23 = 0, - n24 = 0, - n31 = 0, - n32 = 0, - n33 = 1, - n34 = 0, - n41 = 0, - n42 = 0, - n43 = 0, - n44 = 1, - out = new Matrix4() - ) { - out.a = n11 - out.b = n21 - out.c = n31 - out.d = n41 - out.e = n12 - out.f = n22 - out.g = n32 - out.h = n42 - out.i = n13 - out.j = n23 - out.k = n33 - out.l = n43 - out.m = n14 - out.n = n24 - out.o = n34 - out.p = n44 - - return out - } - - /** - * @param {Matrix4} matrix - * @param {Matrix4} out - * @returns {Matrix4} - */ - static copy(matrix, out = new Matrix4()) { - out.a = matrix.a - out.b = matrix.b - out.c = matrix.c - out.d = matrix.d - out.e = matrix.e - out.f = matrix.f - out.g = matrix.g - out.h = matrix.h - out.i = matrix.i - out.j = matrix.j - out.k = matrix.k - out.l = matrix.l - out.m = matrix.m - out.n = matrix.n - out.o = matrix.o - out.p = matrix.p - - return out - } - - /** - * @param {Matrix4} value - * @returns {Matrix4Serial} - */ - static serialize(value) { - return [ - value.a, - value.b, - value.c, - value.d, - value.e, - value.f, - value.g, - value.h, - value.i, - value.j, - value.k, - value.l, - value.m, - value.n, - value.o, - value.p - ] - } - - /** - * @param {Matrix4Serial} value - * @param {Matrix4} [out] - */ - static deserialize(value, out = new Matrix4()) { - out.a = value[0] - out.b = value[1] - out.c = value[2] - out.d = value[3] - out.e = value[4] - out.f = value[5] - out.g = value[6] - out.h = value[7] - out.i = value[8] - out.j = value[9] - out.k = value[10] - out.l = value[11] - out.m = value[12] - out.n = value[13] - out.o = value[14] - out.p = value[15] - - return out - } - - /** - * @param {unknown} value - * @returns {value is Matrix4Serial} - */ - static validateSerial(value) { - return Array.isArray(value) && - value.length === 16 && - typeof value[0] === 'number' && - typeof value[1] === 'number' && - typeof value[2] === 'number' && - typeof value[3] === 'number' && - typeof value[4] === 'number' && - typeof value[5] === 'number' && - typeof value[6] === 'number' && - typeof value[7] === 'number' && - typeof value[8] === 'number' && - typeof value[9] === 'number' && - typeof value[10] === 'number' && - typeof value[11] === 'number' && - typeof value[12] === 'number' && - typeof value[13] === 'number' && - typeof value[14] === 'number' && - typeof value[15] === 'number' - } - - /** - * @param {Matrix4} matrix - * @param {Matrix4} out - * @returns {Matrix4} - */ - static transpose(matrix, out = new Matrix4()) { - let tmp - - out.a = matrix.a - out.f = matrix.f - out.k = matrix.k - out.p = matrix.p - - tmp = matrix.b - out.b = matrix.e - out.e = tmp - - tmp = matrix.c - out.c = matrix.i - out.i = tmp - - tmp = matrix.g - out.g = matrix.j - out.j = tmp - - tmp = matrix.d - out.d = matrix.m - out.m = tmp - - tmp = matrix.h - out.h = matrix.n - out.n = tmp - - tmp = matrix.l - out.l = matrix.o - out.o = tmp - - return out - } - - /** - * @param {Matrix4} out - * @returns {Matrix4} - */ - static identity(out = new Matrix4()) { - Matrix4.set( - // eslint-disable function-call - 1, - 0, - 0, - 0, - 0, - 1, - 0, - 0, - 0, - 0, - 1, - 0, - 0, - 0, - 0, - 1, - out - ) - - return out - } - - /** - * @param {Matrix4} out - * @returns {Matrix4} - */ - static zero(out = new Matrix4()) { - Matrix4.set( - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - out - ) - - return out - } - - /** - * @param {Matrix4} matrix - * @returns {number} - */ - static determinant(matrix) { - const n11 = matrix.a, - n12 = matrix.e, - n13 = matrix.i, - n14 = matrix.m, - n21 = matrix.b, - n22 = matrix.f, - n23 = matrix.j, - n24 = matrix.n, - n31 = matrix.c, - n32 = matrix.g, - n33 = matrix.k, - n34 = matrix.o, - n41 = matrix.d, - n42 = matrix.h, - n43 = matrix.l, - n44 = matrix.p - - return ( - n41 * ( - +n14 * n23 * n32 - - n13 * n24 * n32 - - n14 * n22 * n33 + - n12 * n24 * n33 + - n13 * n22 * n34 - - n12 * n23 * n34 - ) + - n42 * ( - +n11 * n23 * n34 - - n11 * n24 * n33 + - n14 * n21 * n33 - - n13 * n21 * n34 + - n13 * n24 * n31 - - n14 * n23 * n31 - ) + - n43 * ( - +n11 * n24 * n32 - - n11 * n22 * n34 - - n14 * n21 * n32 + - n12 * n21 * n34 + - n14 * n22 * n31 - - n12 * n24 * n31 - ) + - n44 * ( - -n13 * n22 * n31 - - n11 * n23 * n32 + - n11 * n22 * n33 + - n13 * n21 * n32 - - n12 * n21 * n33 + - n12 * n23 * n31 - ) - ) - - } - - /** - * @param {Matrix4} matrix - * @returns {number} - */ - static trace(matrix) { - return matrix.a + matrix.f + matrix.k + matrix.p - } - - /** - * @param {Matrix4} matrix1 - * @param {Matrix4} matrix2 - * @param {Matrix4} out - * @returns {Matrix4} - */ - static add(matrix1, matrix2, out = new Matrix4()) { - out.a = matrix1.a + matrix2.a - out.b = matrix1.b + matrix2.b - out.c = matrix1.c + matrix2.c - out.d = matrix1.d + matrix2.d - out.e = matrix1.e + matrix2.e - out.f = matrix1.f + matrix2.f - out.g = matrix1.g + matrix2.g - out.h = matrix1.h + matrix2.h - out.i = matrix1.i + matrix2.i - out.j = matrix1.j + matrix2.j - out.k = matrix1.k + matrix2.k - out.l = matrix1.l + matrix2.l - out.m = matrix1.m + matrix2.m - out.n = matrix1.n + matrix2.n - out.o = matrix1.o + matrix2.o - out.p = matrix1.p + matrix2.p - - return out - } - - /** - * @param {Matrix4} matrix - * @param {number} scalar - * @param {Matrix4} out - * @returns {Matrix4} - */ - static addScalar(matrix, scalar, out = new Matrix4()) { - out.a = matrix.a + scalar - out.b = matrix.b + scalar - out.c = matrix.c + scalar - out.d = matrix.d + scalar - out.e = matrix.e + scalar - out.f = matrix.f + scalar - out.g = matrix.g + scalar - out.h = matrix.h + scalar - out.i = matrix.i + scalar - out.j = matrix.j + scalar - out.k = matrix.k + scalar - out.l = matrix.l + scalar - out.m = matrix.m + scalar - out.n = matrix.n + scalar - out.o = matrix.o + scalar - out.p = matrix.p + scalar - - return out - } - - /** - * @param {Matrix4} matrix1 - * @param {Matrix4} matrix2 - * @param {Matrix4} out - * @returns {Matrix4} - */ - static subtract(matrix1, matrix2, out = new Matrix4()) { - out.a = matrix1.a - matrix2.a - out.b = matrix1.b - matrix2.b - out.c = matrix1.c - matrix2.c - out.d = matrix1.d - matrix2.d - out.e = matrix1.e - matrix2.e - out.f = matrix1.f - matrix2.f - out.g = matrix1.g - matrix2.g - out.h = matrix1.h - matrix2.h - out.i = matrix1.i - matrix2.i - out.j = matrix1.j - matrix2.j - out.k = matrix1.k - matrix2.k - out.l = matrix1.l - matrix2.l - out.m = matrix1.m - matrix2.m - out.n = matrix1.n - matrix2.n - out.o = matrix1.o - matrix2.o - out.p = matrix1.p - matrix2.p - - return out - } - - /** - * @param {Matrix4} matrix - * @param {number} scalar - * @param {Matrix4} out - * @returns {Matrix4} - */ - static subtractScalar(matrix, scalar, out = new Matrix4()) { - out.a = matrix.a - scalar - out.b = matrix.b - scalar - out.c = matrix.c - scalar - out.d = matrix.d - scalar - out.e = matrix.e - scalar - out.f = matrix.f - scalar - out.g = matrix.g - scalar - out.h = matrix.h - scalar - out.i = matrix.i - scalar - out.j = matrix.j - scalar - out.k = matrix.k - scalar - out.l = matrix.l - scalar - out.m = matrix.m - scalar - out.n = matrix.n - scalar - out.o = matrix.o - scalar - out.p = matrix.p - scalar - - return out - } - - /** - * @param {Matrix4} matrix1 - * @param {Matrix4} matrix2 - * @param {Matrix4} out - * @returns {Matrix4} - */ - static multiply(matrix1, matrix2, out = new Matrix4()) { - const - a11 = matrix1.a, - a12 = matrix1.e, - a13 = matrix1.i, - a14 = matrix1.m, - a21 = matrix1.b, - a22 = matrix1.f, - a23 = matrix1.j, - a24 = matrix1.n, - a31 = matrix1.c, - a32 = matrix1.g, - a33 = matrix1.k, - a34 = matrix1.o, - a41 = matrix1.d, - a42 = matrix1.h, - a43 = matrix1.l, - a44 = matrix1.p - - const - b11 = matrix2.a, - b12 = matrix2.e, - b13 = matrix2.i, - b14 = matrix2.m, - b21 = matrix2.b, - b22 = matrix2.f, - b23 = matrix2.j, - b24 = matrix2.n, - b31 = matrix2.c, - b32 = matrix2.g, - b33 = matrix2.k, - b34 = matrix2.o, - b41 = matrix2.d, - b42 = matrix2.h, - b43 = matrix2.l, - b44 = matrix2.p - - out.a = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41 - out.e = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42 - out.i = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43 - out.m = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44 - - out.b = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41 - out.f = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42 - out.j = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43 - out.n = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44 - - out.c = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41 - out.g = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42 - out.k = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43 - out.o = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44 - - out.d = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41 - out.h = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42 - out.l = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43 - out.p = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44 - - return out - } - - /** - * @param {Matrix4} matrix - * @param {number} scalar - * @param {Matrix4} out - * @returns {Matrix4} - */ - static multiplyScalar(matrix, scalar, out = new Matrix4()) { - out.a = matrix.a * scalar - out.b = matrix.b * scalar - out.c = matrix.c * scalar - out.d = matrix.d * scalar - out.e = matrix.e * scalar - out.f = matrix.f * scalar - out.g = matrix.g * scalar - out.h = matrix.h * scalar - out.i = matrix.i * scalar - out.j = matrix.j * scalar - out.k = matrix.k * scalar - out.l = matrix.l * scalar - out.m = matrix.m * scalar - out.n = matrix.n * scalar - out.o = matrix.o * scalar - out.p = matrix.p * scalar - - return out - } - - /** - * @param {Matrix4} matrix1 - * @param {Matrix4} matrix2 - * @param {Matrix4} out - */ - static divide(matrix1, matrix2, out = new Matrix4()) { - const multiplier = this.invert(matrix2) - - this.multiply(matrix1, multiplier, out) - - return out - } - - /** - * @param {Matrix4} matrix - * @param {number} scalar - * @param {Matrix4} [out=new Matrix4()] - */ - static divideScalar(matrix, scalar, out = new Matrix4()) { - this.multiplyScalar(matrix, invert(scalar), out) - - return out - } - - /** - * @param {Matrix4} matrix - * @param {Matrix4} out - * @returns {Matrix4} - */ - static invert(matrix, out = new Matrix4()) { - const n11 = matrix.a, - n21 = matrix.b, - n31 = matrix.c, - n41 = matrix.d, - n12 = matrix.e, - n22 = matrix.f, - n32 = matrix.g, - n42 = matrix.h, - n13 = matrix.i, - n23 = matrix.j, - n33 = matrix.k, - n43 = matrix.l, - n14 = matrix.m, - n24 = matrix.n, - n34 = matrix.o, - n44 = matrix.p, - - t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44, - t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44, - t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44, - t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34 - - const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14 - - if (det === 0) return Matrix4.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, out) - - const detInv = 1 / det - - out.a = t11 * detInv - out.b = (n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44) * detInv - out.c = (n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44) * detInv - out.d = (n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43) * detInv - - out.e = t12 * detInv - out.f = (n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44) * detInv - out.g = (n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44) * detInv - out.h = (n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43) * detInv - - out.i = t13 * detInv - out.j = (n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44) * detInv - out.k = (n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44) * detInv - out.l = (n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43) * detInv - - out.m = t14 * detInv - out.n = (n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34) * detInv - out.o = (n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34) * detInv - out.p = (n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33) * detInv - - return out - } - - /** - * @param {Matrix4} matrix1 - * @param {Matrix4} matrix2 - * @returns {boolean} - */ - static equal(matrix1, matrix2) { - return ( - matrix1.a === matrix2.a && - matrix1.b === matrix2.b && - matrix1.c === matrix2.c && - matrix1.d === matrix2.d && - matrix1.e === matrix2.e && - matrix1.f === matrix2.f && - matrix1.g === matrix2.g && - matrix1.h === matrix2.h && - matrix1.i === matrix2.i && - matrix1.j === matrix2.j && - matrix1.k === matrix2.k && - matrix1.l === matrix2.l && - matrix1.m === matrix2.m && - matrix1.n === matrix2.n && - matrix1.o === matrix2.o && - matrix1.p === matrix2.p - ) - } - - /** - * Allows iteration of components. - * - * @yields {number} - */ - * [Symbol.iterator]() { - yield this.a - yield this.b - yield this.c - yield this.d - yield this.e - yield this.f - yield this.g - yield this.h - yield this.i - yield this.j - yield this.k - yield this.l - yield this.m - yield this.n - yield this.o - yield this.p - } - - /** - * @type {Matrix4} - */ - static Identity = Matrix4.identity() - - /** - * @type {Matrix4} - */ - static Zero = Matrix4.zero() -} - -/** - * @typedef Matrix4Serial - * @type {[number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number]} - */ diff --git a/packages/math/src/core/vectors/boolean/bvector2.js b/packages/math/src/core/vectors/boolean/bvector2.js deleted file mode 100644 index ecca34f5..00000000 --- a/packages/math/src/core/vectors/boolean/bvector2.js +++ /dev/null @@ -1,17 +0,0 @@ -export class BVector2 { - - /** - * @type {boolean} - */ - x - - /** - * @type {boolean} - */ - y - - constructor(x = false, y = false) { - this.x = x - this.y = y - } -} diff --git a/packages/math/src/core/vectors/boolean/bvector3.js b/packages/math/src/core/vectors/boolean/bvector3.js deleted file mode 100644 index 77f2f14f..00000000 --- a/packages/math/src/core/vectors/boolean/bvector3.js +++ /dev/null @@ -1,23 +0,0 @@ -export class BVector3 { - - /** - * @type {boolean} - */ - x - - /** - * @type {boolean} - */ - y - - /** - * @type {boolean} - */ - z - - constructor(x = false, y = false, z = false) { - this.x = x - this.y = y - this.z = z - } -} diff --git a/packages/math/src/core/vectors/boolean/bvector4.js b/packages/math/src/core/vectors/boolean/bvector4.js deleted file mode 100644 index aeaac633..00000000 --- a/packages/math/src/core/vectors/boolean/bvector4.js +++ /dev/null @@ -1,28 +0,0 @@ -export class BVector4 { - - /** - * @type {boolean} - */ - x - - /** - * @type {boolean} - */ - y - - /** - * @type {boolean} - */ - z - - /** - * @type {boolean} - */ - w - constructor(x = false, y = false, z = false, w = false) { - this.x = x - this.y = y - this.z = z - this.w = w - } -} diff --git a/packages/math/src/core/vectors/boolean/index.js b/packages/math/src/core/vectors/boolean/index.js deleted file mode 100644 index d4139243..00000000 --- a/packages/math/src/core/vectors/boolean/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export * from './bvector2' -export * from './bvector3' -export * from './bvector4' diff --git a/packages/math/src/core/vectors/float/index.js b/packages/math/src/core/vectors/float/index.js deleted file mode 100644 index b224b58a..00000000 --- a/packages/math/src/core/vectors/float/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export * from './vector2' -export * from './vector3' -export * from './vector4' diff --git a/packages/math/src/core/vectors/float/vector2.js b/packages/math/src/core/vectors/float/vector2.js deleted file mode 100644 index b93471cd..00000000 --- a/packages/math/src/core/vectors/float/vector2.js +++ /dev/null @@ -1,724 +0,0 @@ -import { TAU } from '../..' -import { invert, lerp } from '../../functions' - -/** - * This is a 2D vector class. - * - */ -export class Vector2 { - - /** - * @param {number} [x] - The x coordinate of the vector. - * @param {number} [y] - The y coordinate of the vector. - */ - constructor(x = 0, y = 0) { - this.x = x - this.y = y - } - - /** - * Sets values of this vector. - * - * @param {number} x - * @param {number} y - */ - set(x, y) { - Vector2.set(x, y, this) - - return this - } - - /** - * Copies values of another vector to this vector. - * - * @param { Vector2} v - * @returns {this} - */ - copy(v) { - Vector2.copy(v, this) - - return this - } - - /** - * Returns a copy of this. - * @returns {Vector2} - */ - clone() { - return Vector2.copy(this) - } - - /** - * @param {number} scalar - * @returns {this} - */ - splat(scalar) { - Vector2.splat(scalar, this) - - return this - } - - /** - * Returns the length squared of this vector. - * - * @returns {number} - */ - magnitudeSquared() { - return Vector2.magnitudeSquared(this) - } - - /** - *Returns length of this vector. - * - * @returns {number} - */ - magnitude() { - return Vector2.magnitude(this) - } - - /** - * Returns the distance between this vector and another vector. - * @param {Vector2} v - The other vector. - * @returns {number} - */ - distanceTo(v) { - return Vector2.distanceTo(this, v) - } - - /** - * Returns the squared distance between this vector and another vector. - * - * @param {Vector2} v - The other vector. - * @returns {number} - */ - distanceToSquared(v) { - return Vector2.distanceToSquared(this, v) - } - - /** - * Makes this a unit vector. - * @returns {this} - */ - normalize() { - Vector2.normalize(this, this) - - return this - } - - /** - * Sets this vector to have the given length. - * - * @param {number} length - */ - setMagnitude(length) { - Vector2.setMagnitude(this, length, this) - - return this - } - - /** - * Adds a given vector into this. - * - * @param {Vector2} v - * @returns {this} - */ - add(v) { - Vector2.add(this, v, this) - - return this - } - - /** - * Adds a scalar value into this vector's component values. - * - * @param {number} n - */ - addScalar(n) { - Vector2.addScalar(this, n, this) - - return this - } - - /** - * Subtracts a given vector from this vector. - * - * @param { Vector2} v - * @returns {this} - */ - subtract(v) { - Vector2.subtract(this, v, this) - - return this - } - - /** - * Subtracts a scalar value from this vector's component values. - * - * @param {number} n - * @returns {this} - */ - subtractScalar(n) { - Vector2.subtractScalar(this, n, this) - - return this - } - - /** - * Multiplies this vector with a given vector. - * @param {Vector2} v - * @returns {this} - */ - multiply(v) { - Vector2.multiply(this, v, this) - - return this - } - - /** - * Multiplies a scalar value with this vector's component values. - * @param {number} s - * @returns {this} - */ - multiplyScalar(s) { - Vector2.multiplyScalar(this, s, this) - - return this - } - - /** - * Divides this vector with a scalar. - * - * @param {Vector2} v - * @returns {this} - */ - divide(v) { - Vector2.divide(this, v, this) - - return this - } - - /** - * @param {number} s - * @returns {this} - */ - divideScalar(s) { - Vector2.divideScalar(this, s, this) - - return this - } - - /** - * Calculates the dot product of this with another vector. - * - * @param {Vector2} v - * @returns {number} - */ - dot(v) { - return Vector2.dot(this, v) - } - - /** - * Calculates the cross product of this with another vector. - * - * @param {Vector2} v - * @returns {number} - */ - cross(v) { - return Vector2.cross(this, v) - } - - /** - * Negates the values of this vector. - * - * @returns {this} - */ - reverse() { - Vector2.reverse(this, this) - - return this - } - - /** - * @returns {this} - */ - invert() { - Vector2.invert(this, this) - - return this - } - - /** - * Checks to see if this vector is equal to another vector. - * - * @param { Vector2} v - * @returns {boolean} - */ - equals(v) { - return Vector2.equal(this, v) - } - - /** - * @param {number} x - * @param {number} y - * @param {Vector2} out - */ - static set(x, y, out = new Vector2()) { - out.x = x - out.y = y - - return out - } - - /** - * @param {Vector2} v - * @param {Vector2} [out=new Vector2()] - */ - static copy(v, out = new Vector2()) { - out.x = v.x - out.y = v.y - - return out - } - - /** - * @param {Vector2} value - */ - static serialize(value) { - return { - x: value.x, - y: value.y - } - } - - /** - * @param {Vector2Serial} value - * @param {Vector2} [out] - */ - static deserialize(value, out = new Vector2()) { - out.x = value.x - out.y = value.y - - return out - } - - /** - * @param {unknown} value - * @returns {value is Vector2Serial} - */ - static validateSerial(value) { - if (!value || typeof value !== 'object') { - return false - } - - if (!('x' in value) || !('y' in value)) { - return false - } - - return typeof value.x === 'number' && typeof value.y === 'number' - } - - /** - * @param {number} scalar - * @param {Vector2} out - * @returns {Vector2} - */ - static splat(scalar, out = new Vector2()) { - return Vector2.set(scalar, scalar, out) - } - - /** - * @param {Vector2} v - */ - static magnitudeSquared(v) { - return v.y ** 2 + v.x ** 2 - } - - /** - * @param {Vector2} v - */ - static magnitude(v) { - return Math.sqrt(Vector2.magnitudeSquared(v)) - } - - /** - * Calculates length squared of this vector to another vector. - * - * @param {Vector2} v1 - * @param {Vector2} v2 - */ - static distanceToSquared(v1, v2) { - const temp = new Vector2(v1.x - v2.x, v1.y - v2.y) - - return this.magnitudeSquared(temp) - } - - /** - * @param {Vector2} v1 - * @param {Vector2} v2 - */ - static distanceTo(v1, v2) { - const temp = new Vector2(v1.x - v2.x, v1.y - v2.y) - - return this.magnitude(temp) - } - - /** - * @param {Vector2} v - * @param {Vector2} [out=new Vector2()] - */ - static normalize(v, out = new Vector2()) { - const length = Vector2.magnitude(v) - - if (length === 0) return out - - out.x = v.x / length - out.y = v.y / length - - return out - } - - /** - * @param {Vector2} v - * @param {number} length - * @param {Vector2} [out] - */ - static setMagnitude(v, length, out = new Vector2()) { - this.normalize(v, out) - this.multiplyScalar(out, length, out) - - return out - } - - /** - * @param {Vector2} v - * @param {number} min - * @param {number} max - * @param {Vector2} out - */ - static clampMagnitude(v, min, max, out) { - if (this.equal(v, this.Zero)) return this.copy(v, out) - - const length = this.magnitude(v) - - if (length > max) return this.multiplyScalar(v, max / length, out) - if (length < min) return this.multiplyScalar(v, min / length, out) - - return this.copy(v, out) - } - - /** - * @param {Vector2} v1 - * @param {Vector2} v2 - * @param {Vector2} [out] - */ - static add(v1, v2, out = new Vector2()) { - out.x = v1.x + v2.x - out.y = v1.y + v2.y - - return out - } - - /** - * @param {Vector2} v1 - * @param {number} n - * @param {Vector2} [out] - */ - static addScalar(v1, n, out = new Vector2()) { - out.x = v1.x + n - out.y = v1.y + n - - return out - } - - /** - * @param {Vector2} from - * @param {Vector2} to - * @param {Vector2} [out] - */ - static subtract(from, to, out = new Vector2()) { - out.x = from.x - to.x - out.y = from.y - to.y - - return out - } - - /** - * @param {Vector2} v1 - * @param {number} n - * @param {Vector2} [out] - */ - static subtractScalar(v1, n, out = new Vector2()) { - out.x = v1.x - n - out.y = v1.y - n - - return out - } - - /** - * @param {Vector2} v1 - * @param {Vector2} v2 - * @param {Vector2} [out] - */ - static multiply(v1, v2, out = new Vector2()) { - out.x = v1.x * v2.x - out.y = v1.y * v2.y - - return out - } - - /** - * @param {Vector2} v1 - * @param {number} n - * @param {Vector2} [out] - */ - static multiplyScalar(v1, n, out = new Vector2()) { - out.x = v1.x * n - out.y = v1.y * n - - return out - } - - /** - * @param {Vector2} v1 - * @param {Vector2} v2 - * @param {Vector2} [out] - */ - static divide(v1, v2, out = new Vector2()) { - out.x = v1.x / v2.x - out.y = v1.y / v2.y - - return out - } - - /** - * @param {Vector2} v1 - * @param {number} n - * @param {Vector2} [out] - */ - static divideScalar(v1, n, out = new Vector2()) { - return Vector2.multiplyScalar(v1, 1 / n, out) - } - - /** - * @param {Vector2} v - * @param {Vector2} [out=new Vector2()] - */ - static reverse(v, out = new Vector2()) { - return Vector2.multiplyScalar(v, -1, out) - } - - /** - * @param {Vector2} v - * @param {Vector2} out - */ - static invert(v, out = new Vector2()) { - out.x = invert(v.x) - out.y = invert(v.y) - - return out - } - - /** - * @param {Vector2} v1 - * @param {Vector2} v2 - */ - static dot(v1, v2) { - return v1.x * v2.x + v1.y * v2.y - } - - /** - * @param {Vector2} v1 - * @param {Vector2} v2 - */ - static cross(v1, v2) { - return v1.x * v2.y - v1.y * v2.x - } - - /** - * @param {Vector2} v1 - * @param {number} n - * @param {Vector2} out - */ - static crossScalar(v1, n, out = new Vector2()) { - out.x = v1.y * -n - out.y = v1.x * n - - return out - } - - /** - * Returns a Vector2 that has been lerped between v1 and v2. - * @param { Vector2} from - The vector to lerp from. - * @param { Vector2} to - The vector to lerp from. - * @param {number} t - A value from 0 to 1 to scale the new Vector2 between v1 and v2. - * @param { Vector2} [out] - The vector to store results into. - * - * @returns { Vector2} - */ - static lerp(from, to, t, out = new Vector2()) { - out.x = lerp(from.x, to.x, t) - out.y = lerp(from.y, to.y, t) - - return out - } - - /** - * @param {Vector2} v - * @param {Vector2} normal - * @param {Vector2} [out] - */ - static reflect(v, normal, out = new Vector2()) { - const multiplier = this.dot(v, normal) * 2 - - out.x = v.x - normal.x * multiplier - out.y = v.y - normal.y * multiplier - - return out - } - - /** - * @param {Vector2} v1 - * @param {Vector2} v2 - */ - static equal(v1, v2) { - return v1.x === v2.x && v1.y === v2.y - } - - /** - * @param {Vector2} v - * @param {Vector2} out - */ - static normal(v, out = new Vector2()) { - return Vector2.set(-v.y, v.x, out) - } - - /** - * @param {Vector2} v - * @param {number} angle - * @param {Vector2} out - */ - static rotate(v, angle, out = new Vector2()) { - return Vector2.rotateFast(v, Math.cos(angle), Math.sin(angle), out) - } - - /** - * @param {Vector2} v - * @param {number} cos - * @param {number} sin - * @param {Vector2} out - */ - static rotateFast(v, cos, sin, out = new Vector2()) { - const { x } = v - - out.x = x * cos - v.y * sin - out.y = x * sin + v.y * cos - - return out - } - - /** - * Gets the angle (in radians) between two - * vectors in the shortest direction from v1 to v2 in the range of `0` to `Math.PI`. - * - * @param { Vector2} v1 - Start of the angle. - * @param { Vector2} v2 - End of the angle. - * @returns {number} - */ - static angleBetween(v1, v2) { - return Math.acos(this.dot(v1, v2) / (this.magnitude(v1) * this.magnitude(v2))) - } - - /** - * Returns a unit vector pointing in the - * given angle starting from the positive x axis. - * - * @param {number} angle - Angle in radians from 0 to `Math.PI * 2`. - * @param { Vector2} [out] - Vector2 to store results in. - * @returns { Vector2} - */ - static fromAngle(angle, out = new Vector2()) { - Vector2.set(Math.cos(angle), Math.sin(angle), out) - - return out - } - - /** - * Returns the angle in radians between the positive x-axis and the vector. - * - * @param { Vector2} v - * @returns {number} - */ - static toAngle(v) { - const a = Math.atan2(v.y, v.x) - - return a < 0 ? TAU + a : a - } - - /** - * Generates a new unit Vector2 in a random direction. - * - * @param { Vector2} [out] - * @returns {Vector2} - */ - static random(out) { - return Vector2.fromAngle(Math.random() * TAU, out) - } - - /** - * Allows for iteration of components. - * - * @yields {number} - */ - * [Symbol.iterator]() { - yield this.x - yield this.y - } - - /** - * A vector whose x and y values will remain 0. - * - * @readonly - * @type { Vector2 } - */ - static Zero = new Vector2() - - /** - * A unit vector pointing in the x-axis. - * - * @readonly - * @type { Vector2 } - */ - static X = new Vector2(1, 0) - - /** - * A unit vector pointing in the y-axis. - * - * @readonly - * @type { Vector2 } - */ - static Y = new Vector2(0, 1) - - /** - * A unit vector pointing in the negative x-axis. - * - * @readonly - * @type { Vector2 } - */ - static NegX = new Vector2(-1, 0) - - /** - * A unit vector pointing in the nega y-axis. - * - * @readonly - * @type { Vector2 } - */ - static NegY = new Vector2(0, -1) -} - -/** - * Serialized form of `Vector2`. - * - * @typedef Vector2Serial - * @property {number} x - * @property {number} y - */ diff --git a/packages/math/src/core/vectors/float/vector3.js b/packages/math/src/core/vectors/float/vector3.js deleted file mode 100644 index ca939325..00000000 --- a/packages/math/src/core/vectors/float/vector3.js +++ /dev/null @@ -1,692 +0,0 @@ -import { invert, lerp } from '../../functions' - -export class Vector3 { - constructor(x = 0, y = 0, z = 0) { - this.x = x - this.y = y - this.z = z - } - - /** - * Sets x,y and z values of this vector to the given parameter. - * - * @param {number} x - * @param {number} y - * @param {number} z - */ - set(x, y, z) { - Vector3.set(x, y, z, this) - - return this - } - - /** - * Copies values of another vector to this vector. - * - * @param {Vector3} v - * @returns {this} - */ - copy(v) { - Vector3.copy(v, this) - - return this - } - - /** - * Returns a copy of this. - * @returns {Vector3} - */ - clone() { - return Vector3.copy(this) - } - - /** - * @param {number} scalar - * @returns {this} - */ - splat(scalar) { - Vector3.splat(scalar, this) - - return this - } - - /** - * Returns the length squared of this vector. - * - * @returns {number} - */ - magnitudeSquared() { - return Vector3.magnitudeSquared(this) - } - - /** - *Returns length of this vector. - * - * @returns {number} - */ - magnitude() { - return Vector3.magnitude(this) - } - - /** - * Returns the distance between this vector and another vector. - * @param {Vector3} v - The other vector. - * @returns {number} - */ - distanceTo(v) { - return Vector3.distanceTo(this, v) - } - - /** - * Returns the squared distance between this vector and another vector. - * - * @param {Vector3} v - The other vector. - * @returns {number} - */ - distanceToSquared(v) { - return Vector3.distanceToSquared(this, v) - } - - /** - * Makes this a unit vector. - * @returns {this} - */ - normalize() { - Vector3.normalize(this, this) - - return this - } - - /** - * Sets this vector to have the given length. - * - * @param {number} length - */ - setMagnitude(length) { - Vector3.setMagnitude(this, length, this) - - return this - } - - /** - * Adds a given vector into this. - * - * @param {Vector3} v - * @returns {this} - */ - add(v) { - Vector3.add(this, v, this) - - return this - } - - /** - * Adds a scalar value into this vector's component values. - * - * @param {number} n - */ - addScalar(n) { - Vector3.addScalar(this, n, this) - - return this - } - - /** - * Subtracts a given vector from this vector. - * - * @param { Vector3} v - * @returns {this} - */ - subtract(v) { - Vector3.subtract(this, v, this) - - return this - } - - /** - * Subtracts a scalar value from this vector's component values. - * - * @param {number} n - * @returns {this} - */ - subtractScalar(n) { - Vector3.subtractScalar(this, n, this) - - return this - } - - /** - * Multiplies this vector with a given vector. - * @param {Vector3} v - * @returns {this} - */ - multiply(v) { - Vector3.multiply(this, v, this) - - return this - } - - /** - * Multiplies a scalar value with this vector's component values. - * @param {number} s - * @returns {this} - */ - multiplyScalar(s) { - Vector3.multiplyScalar(this, s, this) - - return this - } - - /** - * Divides this vector with a scalar. - * - * @param {Vector3} v - * @returns {this} - */ - divide(v) { - Vector3.divide(this, v, this) - - return this - } - - /** - * @param {number} s - * @returns {this} - */ - divideScalar(s) { - Vector3.divideScalar(this, s, this) - - return this - } - - /** - * Calculates the dot product of this with another vector. - * - * @param {Vector3} v - * @returns {number} - */ - dot(v) { - return Vector3.dot(this, v) - } - - /** - * Calculates the cross product of this with another vector. - * - * @param {Vector3} v - * @returns {Vector3} - */ - cross(v) { - Vector3.cross(this, v, this) - - return this - } - - /** - * Negates the values of this vector. - * - * @returns {this} - */ - reverse() { - Vector3.reverse(this, this) - - return this - } - - /** - * @returns {this} - */ - invert() { - Vector3.invert(this, this) - - return this - } - - /** - * Checks to see if this vector is equal to another vector. - * - * @param { Vector3} v - * @returns {boolean} - */ - equals(v) { - return Vector3.equal(this, v) - } - - /** - * @param {number} x - * @param {number} y - * @param {number} z - * @param {Vector3} out - */ - static set(x, y, z, out = new Vector3()) { - out.x = x - out.y = y - out.z = z - - return out - } - - /** - * @param {Vector3} v1 - * @param {Vector3} out - */ - static copy(v1, out = new Vector3()) { - out.x = v1.x - out.y = v1.y - out.z = v1.z - - return out - } - - /** - * @param {Vector3} value - */ - static serialize(value) { - return { - x: value.x, - y: value.y, - z: value.z - } - } - - /** - * @param {Vector3Serial} value - * @param {Vector3} [out] - */ - static deserialize(value, out = new Vector3()) { - out.x = value.x - out.y = value.y - out.z = value.z - - return out - } - - /** - * @param {unknown} value - * @returns {value is Vector3Serial} - */ - static validateSerial(value) { - if (!value || typeof value !== 'object') { - return false - } - - if (!('x' in value) || !('y' in value) || !('z' in value)) { - return false - } - - return typeof value.x === 'number' && - typeof value.y === 'number' && - typeof value.z === 'number' - } - - /** - * @param {number} scalar - * @param {Vector3} out - */ - static splat(scalar, out = new Vector3()) { - out.x = scalar - out.y = scalar - out.z = scalar - - return out - } - - /** - * @param {Vector3} v - */ - static magnitudeSquared(v) { - return v.x * v.x + v.y * v.y + v.z * v.z - } - - /** - * @param {Vector3} v - */ - static magnitude(v) { - return Math.sqrt(Vector3.magnitudeSquared(v)) - } - - /** - * @param {Vector3} v1 - * @param {Vector3} v2 - */ - static distanceToSquared(v1, v2) { - const dx = v1.x - v2.x, - dy = v1.y - v2.y, - dz = v1.z - v2.z - - return dx * dx + dy * dy + dz * dz - } - - /** - * @param {Vector3} v1 - * @param {Vector3} v2 - */ - static distanceTo(v1, v2) { - return Math.sqrt(Vector3.distanceToSquared(v1, v2)) - } - - /** - * @param {Vector3} v - * @param {Vector3} out - */ - static normalize(v, out = new Vector3()) { - const length = this.magnitude(v) || 1 - - this.divideScalar(v, length, out) - - return out - } - - /** - * @param {Vector3} v - * @param {number} length - * @param {Vector3} out - */ - static setMagnitude(v, length, out = new Vector3()) { - Vector3.normalize(v, out) - Vector3.multiplyScalar(out, length, out) - - return out - } - - /** - * @param {Vector3} v - * @param {number} min - * @param {number} max - * @param {Vector3} out - */ - static clampMagnitude(v, min, max, out = new Vector3()) { - const length = Vector3.magnitude(v) || 1 - - if (length < min) return Vector3.multiplyScalar(v, min / length, out) - if (length > max) return Vector3.multiplyScalar(v, max / length, out) - - return Vector3.copy(v, out) - } - - /** - * @param {Vector3} v1 - * @param {Vector3} v2 - * @param {Vector3} out - */ - static add(v1, v2, out = new Vector3()) { - out.x = v1.x + v2.x - out.y = v1.y + v2.y - out.z = v1.z + v2.z - - return out - } - - /** - * @param {Vector3} v1 - * @param {number} scalar - * @param {Vector3} out - */ - static addScalar(v1, scalar, out = new Vector3()) { - out.x = v1.x + scalar - out.y = v1.y + scalar - out.z = v1.z + scalar - - return out - } - - /** - * @param {Vector3} from - * @param {Vector3} to - * @param {Vector3} out - */ - static subtract(from, to, out = new Vector3()) { - out.x = from.x - to.x - out.y = from.y - to.y - out.z = from.z - to.z - - return out - } - - /** - * @param {Vector3} v1 - * @param {number} scalar - * @param {Vector3} out - */ - static subtractScalar(v1, scalar, out = new Vector3()) { - out.x = v1.x - scalar - out.y = v1.y - scalar - out.z = v1.z - scalar - - return out - } - - /** - * @param {Vector3} v1 - * @param {Vector3} v2 - * @param {Vector3} out - */ - static multiply(v1, v2, out = new Vector3()) { - out.x = v1.x * v2.x - out.y = v1.y * v2.y - out.z = v1.z * v2.z - - return out - } - - /** - * @param {Vector3} v1 - * @param {number} scalar - * @param {Vector3} out - */ - static multiplyScalar(v1, scalar, out = new Vector3()) { - out.x = v1.x * scalar - out.y = v1.y * scalar - out.z = v1.z * scalar - - return out - } - - /** - * @param {Vector3} v1 - * @param {Vector3} v2 - * @param {Vector3} out - */ - static divide(v1, v2, out = new Vector3()) { - out.x = v1.x / v2.x - out.y = v1.y / v2.y - out.z = v1.z / v2.z - - return out - } - - /** - * @param {Vector3} v1 - * @param {number} scalar - * @param {Vector3} out - */ - static divideScalar(v1, scalar, out = new Vector3()) { - return Vector3.multiplyScalar(v1, 1 / scalar, out) - } - - /** - * @param {Vector3} v - * @param {Vector3} out - */ - static reverse(v, out = new Vector3()) { - out.x = -v.x - out.y = -v.y - out.z = -v.z - - return out - } - - /** - * @param {Vector3} v - * @param {Vector3} out - */ - static invert(v, out = new Vector3()) { - out.x = invert(v.x) - out.y = invert(v.y) - out.z = invert(v.z) - - return out - } - - /** - * @param {Vector3} v1 - * @param {Vector3} v2 - */ - static dot(v1, v2) { - return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z - } - - /** - * @param {Vector3} a - * @param {Vector3} b - * @param {Vector3} [out] - */ - static cross(a, b, out = new Vector3()) { - const ax = a.x, - ay = a.y, - az = a.z - const bx = b.x, - by = b.y, - bz = b.z - - out.x = ay * bz - az * by - out.y = az * bx - ax * bz - out.z = ax * by - ay * bx - - return out - } - - /** - * @param {Vector3} from - * @param {Vector3} to - * @param {number} t - * @param {Vector3} out - */ - static lerp(from, to, t, out = new Vector3()) { - out.x = lerp(from.x, to.x, t) - out.y = lerp(from.y, to.y, t) - out.z = lerp(from.z, to.z, t) - - return out - } - - /** - * @param {Vector3} v - * @param {Vector3} normal - * @param {Vector3} out - */ - static reflect(v, normal, out = new Vector3()) { - const multiplier = v.dot(normal) * 2 - - out.x = v.x - normal.x * multiplier - out.y = v.y - normal.y * multiplier - out.z = v.z - normal.z * multiplier - - return out - } - - /** - * @param {Vector3} v1 - * @param {Vector3} v2 - */ - static angleBetween(v1, v2) { - return Math.acos(v1.dot(v1) / (v1.magnitude() * v2.magnitude())) - } - - /** - * @param {Vector3} v1 - * @param {Vector3} v2 - */ - static equal(v1, v2) { - return v1.x === v2.x && v1.y === v2.y && v1.z === v2.z - } - - /** - * @param {Vector3} out - * @returns - */ - static random(out) { - const theta = Math.random() * Math.PI * 2 - const u = Math.random() * 2 - 1 - const c = Math.sqrt(1 - u * u) - - out.x = c * Math.cos(theta) - out.y = u - out.z = c * Math.sin(theta) - - return out - } - - /** - * @yields {number} - */ - * [Symbol.iterator]() { - yield this.x - yield this.y - yield this.z - } - - /** - * A vector whose x and y values will remain 0. - * - * @readonly - * @type {Vector3} - */ - static Zero = new Vector3() - - /** - * Unit vector pointing in the x-axis. - * - * @readonly - * @type {Vector3} - */ - static X = new Vector3(1, 0, 0) - - /** - * Unit vector pointing in the y-axis. - * - * @readonly - * @type {Vector3} - */ - static Y = new Vector3(0, 1, 0) - - /** - * Unit vector pointing in the z-axis. - * - * @readonly - * @type {Vector3} - */ - static Z = new Vector3(0, 0, 1) - - /** - * Unit vector pointing in the negative x-axis. - * - * @readonly - * @type {Vector3} - */ - static NegX = new Vector3(-1, 0, 0) - - /** - * Unit vector pointing in the negative y-axis. - * - * @readonly - * @type {Vector3} - */ - static NegY = new Vector3(0, -1, 0) - - /** - * Unit vector pointing in the negative z-axis. - * - * @readonly - * @type {Vector3} - */ - static NegZ = new Vector3(0, 0, -1) -} - -/** - * Serialized form of `Vector3`. - * - * @typedef Vector3Serial - * @property {number} x - * @property {number} y - * @property {number} z - */ diff --git a/packages/math/src/core/vectors/float/vector4.js b/packages/math/src/core/vectors/float/vector4.js deleted file mode 100644 index 01b46e6a..00000000 --- a/packages/math/src/core/vectors/float/vector4.js +++ /dev/null @@ -1,718 +0,0 @@ -import { invert, lerp } from '../../functions' - -export class Vector4 { - - /** - * @type {number} - */ - x - - /** - * @type {number} - */ - y - - /** - * @type {number} - */ - z - - /** - * @type {number} - */ - w - - /** - * @param {number} x - * @param {number} y - * @param {number} z - * @param {number} w - */ - constructor(x = 0, y = 0, z = 0, w = 0) { - this.x = x - this.y = y - this.z = z - this.w = w - } - - /** - * Sets x,y and z values of this vector to the given parameter. - * - * @param {number} x - * @param {number} y - * @param {number} z - * @param {number} w - */ - set(x, y, z, w) { - Vector4.set(x, y, z, w, this) - - return this - } - - /** - * Copies values of another vector to this vector. - * - * @param {Vector4} v - * @returns {this} - */ - copy(v) { - Vector4.copy(v, this) - - return this - } - - /** - * Returns a copy of this. - * @returns {Vector4} - */ - clone() { - return Vector4.copy(this) - } - - /** - * @param {number} scalar - * @returns {this} - */ - splat(scalar) { - Vector4.splat(scalar, this) - - return this - } - - /** - * Returns the length squared of this vector. - * - * @returns {number} - */ - magnitudeSquared() { - return Vector4.magnitudeSquared(this) - } - - /** - *Returns length of this vector. - * - * @returns {number} - */ - magnitude() { - return Vector4.magnitude(this) - } - - /** - * Returns the distance between this vector and another vector. - * @param {Vector4} v - The other vector. - * @returns {number} - */ - distanceTo(v) { - return Vector4.distanceTo(this, v) - } - - /** - * Returns the squared distance between this vector and another vector. - * - * @param {Vector4} v - The other vector. - * @returns {number} - */ - distanceToSquared(v) { - return Vector4.distanceToSquared(this, v) - } - - /** - * Makes this a unit vector. - * @returns {this} - */ - normalize() { - Vector4.normalize(this, this) - - return this - } - - /** - * Sets this vector to have the given length. - * - * @param {number} length - */ - setMagnitude(length) { - Vector4.setMagnitude(this, length, this) - - return this - } - - /** - * Adds a given vector into this. - * - * @param {Vector4} v - * @returns {this} - */ - add(v) { - Vector4.add(this, v, this) - - return this - } - - /** - * Adds a scalar value into this vector's component values. - * - * @param {number} n - */ - addScalar(n) { - Vector4.addScalar(this, n, this) - - return this - } - - /** - * Subtracts a given vector from this vector. - * - * @param { Vector4} v - * @returns {this} - */ - subtract(v) { - Vector4.subtract(this, v, this) - - return this - } - - /** - * Subtracts a scalar value from this vector's component values. - * - * @param {number} n - * @returns {this} - */ - subtractScalar(n) { - Vector4.subtractScalar(this, n, this) - - return this - } - - /** - * Multiplies this vector with a given vector. - * @param {Vector4} v - * @returns {this} - */ - multiply(v) { - Vector4.multiply(this, v, this) - - return this - } - - /** - * Multiplies a scalar value with this vector's component values. - * @param {number} s - * @returns {this} - */ - multiplyScalar(s) { - Vector4.multiplyScalar(this, s, this) - - return this - } - - /** - * Divides this vector with a scalar. - * - * @param {Vector4} v - * @returns {this} - */ - divide(v) { - Vector4.divide(this, v, this) - - return this - } - - /** - * @param {number} s - * @returns {this} - */ - divideScalar(s) { - Vector4.divideScalar(this, s, this) - - return this - } - - /** - * Calculates the dot product of this with another vector. - * - * @param {Vector4} v - * @returns {number} - */ - dot(v) { - return Vector4.dot(this, v) - } - - /** - * Negates the values of this vector. - * - * @returns {this} - */ - reverse() { - Vector4.reverse(this, this) - - return this - } - - /** - * @returns {this} - */ - invert() { - Vector4.invert(this, this) - - return this - } - - /** - * Checks to see if this vector is equal to another vector. - * - * @param { Vector4} v - * @returns {boolean} - */ - equals(v) { - return Vector4.equal(this, v) - } - - /** - * @param {number} x - * @param {number} y - * @param {number} z - * @param {number} w - * @param {Vector4} out - */ - static set(x, y, z, w, out = new Vector4()) { - out.x = x - out.y = y - out.z = z - out.w = w - - return out - - } - - /** - * @param {Vector4} v - * @param {Vector4} out - */ - static copy(v, out = new Vector4()) { - out.x = v.x - out.y = v.y - out.z = v.z - out.w = v.w - - return out - } - - /** - * @param {Vector4} value - */ - static serialize(value) { - return { - x: value.x, - y: value.y, - z: value.z, - w: value.w - } - } - - /** - * @param {Vector4Serial} value - * @param {Vector4} [out] - */ - static deserialize(value, out = new Vector4()) { - out.x = value.x - out.y = value.y - out.z = value.z - out.w = value.w - - return out - } - - /** - * @param {unknown} value - * @returns {value is Vector4Serial} - */ - static validateSerial(value) { - if (!value || typeof value !== 'object') { - return false - } - - if (!('x' in value) || !('y' in value) || !('z' in value) || !('w' in value)) { - return false - } - - return typeof value.x === 'number' && - typeof value.y === 'number' && - typeof value.z === 'number' && - typeof value.w === 'number' - } - - /** - * @param {number} scalar - * @param {Vector4} out - */ - static splat(scalar, out = new Vector4()) { - out.x = scalar - out.y = scalar - out.z = scalar - out.w = scalar - - return out - } - - /** - * @param {Vector4} v - */ - static magnitudeSquared(v) { - return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w - } - - /** - * @param {Vector4} v - */ - static magnitude(v) { - return Math.sqrt(Vector4.magnitudeSquared(v)) - } - - /** - * @param {Vector4} v1 - * @param {Vector4} v2 - */ - static distanceToSquared(v1, v2) { - const dx = v1.x - v2.x, - dy = v1.y - v2.y, - dz = v1.z - v2.z, - dw = v1.w - v2.w - - return dx * dx + dy * dy + dz * dz + dw * dw - } - - /** - * @param {Vector4} v1 - * @param {Vector4} v2 - */ - static distanceTo(v1, v2) { - return Math.sqrt(Vector4.distanceToSquared(v1, v2)) - } - - /** - * @param {Vector4} v - * @param {Vector4} out - */ - static normalize(v, out = new Vector4()) { - const length = this.magnitude(v) || 1 - - return this.divideScalar(v, length, out) - } - - /** - * @param {Vector4} v - * @param {number} length - * @param {Vector4} out - */ - static setMagnitude(v, length, out = new Vector4()) { - this.normalize(v, out) - this.multiplyScalar(out, length, out) - - return out - } - - /** - * @param {Vector4} v - * @param {number} min - * @param {number} max - * @param {Vector4} out - */ - static clampMagnitude(v, min, max, out = new Vector4()) { - const length = this.magnitude(v) || 1 - - if (length < min) return this.multiplyScalar(v, min / length, out) - if (length > max) return this.multiplyScalar(v, max / length, out) - - return this.copy(v, out) - } - - /** - * @param {Vector4} a - * @param {Vector4} b - * @param {Vector4} out - */ - static add(a, b, out = new Vector4()) { - out.x = a.x + b.x - out.y = a.y + b.y - out.z = a.z + b.z - out.w = a.w + b.w - - return out - - } - - /** - * @param {Vector4} v - * @param {number} s - * @param {Vector4} out - */ - static addScalar(v, s, out = new Vector4()) { - - out.x = v.x + s - out.y = v.y + s - out.z = v.z + s - out.w = v.w + s - - return out - } - - /** - * @param {Vector4} a - * @param {Vector4} b - * @param {Vector4} out - */ - static subtract(a, b, out = new Vector4()) { - out.x = a.x - b.x - out.y = a.y - b.y - out.z = a.z - b.z - out.w = a.w - b.w - - return out - } - - /** - * @param {Vector4} v - * @param {number} s - * @param {Vector4} out - */ - static subtractScalar(v, s, out = new Vector4()) { - out.x = v.x - s - out.y = v.y - s - out.z = v.z - s - out.w = v.w - s - - return out - } - - /** - * @param {Vector4} a - * @param {Vector4} b - * @param {Vector4} out - */ - static multiply(a, b, out = new Vector4()) { - out.x = a.x * b.x - out.y = a.y * b.y - out.z = a.z * b.z - out.w = a.w * b.w - - return out - } - - /** - * @param {Vector4} v - * @param {number} s - * @param {Vector4} out - */ - static multiplyScalar(v, s, out = new Vector4()) { - out.x = v.x * s - out.y = v.y * s - out.z = v.z * s - out.w = v.w * s - - return out - } - - /** - * @param {Vector4} a - * @param {Vector4} b - * @param {Vector4} out - */ - static divide(a, b, out = new Vector4()) { - out.x = a.x / b.x - out.y = a.y / b.y - out.z = a.z / b.z - out.w = a.w / b.w - - return out - } - - /** - * @param {Vector4} v - * @param {number} scalar - * @param {Vector4} out - */ - static divideScalar(v, scalar, out = new Vector4()) { - return Vector4.multiplyScalar(v, 1 / scalar, out) - } - - /** - * @param {Vector4} v - * @param {Vector4} out - */ - static reverse(v, out = new Vector4()) { - out.x = -v.x - out.y = -v.y - out.z = -v.z - out.w = -v.w - - return out - } - - /** - * @param {Vector4} v - * @param {Vector4} out - */ - static invert(v, out = new Vector4()) { - out.x = invert(v.x) - out.y = invert(v.y) - out.z = invert(v.z) - out.w = invert(v.w) - - return out - } - - /** - * @param {Vector4} a - * @param {Vector4} b - * @returns - */ - static dot(a, b) { - return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w - } - - /** - * @param {Vector4} from - * @param {Vector4} to - * @param {number} t - * @param {Vector4} out - */ - static lerp(from, to, t, out = new Vector4()) { - out.x = lerp(from.x, to.x, t) - out.y = lerp(from.y, to.y, t) - out.z = lerp(from.z, to.z, t) - out.w = lerp(from.w, to.w, t) - - return out - } - - /** - * @param {Vector4} v - * @param {Vector4} normal - * @param {Vector4} out - */ - static reflect(v, normal, out = new Vector4()) { - const multiplier = v.dot(normal) * 2 - - out.x = v.x - normal.x * multiplier - out.y = v.y - normal.y * multiplier - out.z = v.z - normal.z * multiplier - out.w = v.w - normal.w * multiplier - - return out - } - - /** - * @param {Vector4} a - * @param {Vector4} b - */ - static equal(a, b) { - return ( - (a.x === b.x) && - (a.y === b.y) && - (a.z === b.z) && - (a.w === b.w) - ) - } - - static random(out = new Vector4()) { - out.x = Math.random() - out.y = Math.random() - out.z = Math.random() - out.w = Math.random() - - return out - } - - /** - * @yields {number} - */ - * [Symbol.iterator]() { - yield this.x - yield this.y - yield this.z - yield this.w - } - - /** - * A vector whose components values will remain 0. - * - * @readonly - * @type {Vector4} - */ - static Zero = new Vector4() - - /** - * Unit vector pointing in the x-axis. - * - * @readonly - * @type {Vector4} - */ - static X = new Vector4(1, 0, 0, 0) - - /** - * Unit vector pointing in the y-axis. - * - * @readonly - * @type {Vector4} - */ - static Y = new Vector4(0, 1, 0, 0) - - /** - * Unit vector pointing in the z-axis. - * - * @readonly - * @type {Vector4} - */ - static Z = new Vector4(0, 0, 1, 0) - - /** - * Unit vector pointing in the negative w-axis. - * - * @readonly - * @type {Vector4} - */ - static W = new Vector4(0, 0, 0, 1) - - /** - * Unit vector pointing in the negative x-axis. - * - * @readonly - * @type {Vector4} - */ - static NegX = new Vector4(-1, 0, 0, 0) - - /** - * Unit vector pointing in the negative y-axis. - * - * @readonly - * @type {Vector4} - */ - static NegY = new Vector4(0, -1, 0, 0) - - /** - * Unit vector pointing in the negative z-axis. - * - * @readonly - * @type {Vector4} - */ - static NegZ = new Vector4(0, 0, -1, 0) - - /** - * Unit vector pointing in the negative w-axis. - * - * @readonly - * @type {Vector4} - */ - static NegW = new Vector4(0, 0, 0, -1) -} - -/** - * Serialized form of `Vector4`. - * - * @typedef Vector4Serial - * @property {number} x - * @property {number} y - * @property {number} z - * @property {number} w - */ diff --git a/packages/math/src/core/vectors/index.js b/packages/math/src/core/vectors/index.js deleted file mode 100644 index 8cc93227..00000000 --- a/packages/math/src/core/vectors/index.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from './boolean' -export * from './float' diff --git a/packages/math/src/index.js b/packages/math/src/index.js index a0bbe11c..927cf3e6 100644 --- a/packages/math/src/index.js +++ b/packages/math/src/index.js @@ -1,3 +1,3 @@ -export * from './core' +export * from 'hisabati' export * from './systems' export * from './plugin' diff --git a/packages/math/src/systems/types.js b/packages/math/src/systems/types.js index 05714b99..98dc6bec 100644 --- a/packages/math/src/systems/types.js +++ b/packages/math/src/systems/types.js @@ -18,7 +18,7 @@ import { Vector2, Vector3, Vector4 -} from '../core' +} from 'hisabati' /** * @param {World} world diff --git a/packages/math/tests/affines/affine2.test.js b/packages/math/tests/affines/affine2.test.js deleted file mode 100644 index c73faddb..00000000 --- a/packages/math/tests/affines/affine2.test.js +++ /dev/null @@ -1,499 +0,0 @@ -import { test, describe } from "vitest"; -import { Affine2,Vector2,Rotary } from "../../src/core"; -import { deepStrictEqual, strictEqual } from "node:assert"; - -describe("Testing `Affine2`", () => { - test('`Affine2` constructor is correct', () => { - const affine = new Affine2( - 1, 2, 3, - 4, 5, 6 - ) - strictEqual(affine.a, 1) - strictEqual(affine.b, 4) - strictEqual(affine.c, 2) - strictEqual(affine.d, 5) - strictEqual(affine.x, 3) - strictEqual(affine.y, 6) - }) - - test('`Affine2` default constructor is identity', () => { - const affine = new Affine2() - const expected = new Affine2( - 1, 0, 0, - 0, 1, 0, - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine2.copy` actually copies", () => { - const source = new Affine2( - 1, 2, 5, - 3, 4, 6 - ) - const destination = Affine2.copy(source) - - deepStrictEqual(destination, source) - }) - - test("`Affine2.serialize` returns an array serial", () => { - const affine = new Affine2( - 1, 2, 5, - 3, 4, 6 - ) - - deepStrictEqual(Affine2.serialize(affine), [1, 3, 2, 4, 5, 6]) - strictEqual(Affine2.validateSerial([1, 3, 2, 4, 5, 6]), true) - }) - - test("`Affine2.deserialize` reads an array serial", () => { - const actual = Affine2.deserialize([1, 3, 2, 4, 5, 6]) - const expected = new Affine2( - 1, 2, 5, - 3, 4, 6 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Affine2.set` sets values corrrectly", () => { - const expected = new Affine2( - 1, 2, 5, - 3, 4, 6 - ) - const actual = Affine2.set( - 1, 2, 5, - 3, 4, 6 - ) - - deepStrictEqual(actual, expected) - }) - - test('`Affine2.identity` is identity', () => { - const actual = Affine2.identity() - const expected = new Affine2( - 1, 0, 0, - 0, 1, 0 - ) - deepStrictEqual(actual, expected) - }) - - test('`Affine2.zero` is zero', () => { - const actual = Affine2.zero() - const expected = new Affine2( - 0, 0, 0, - 0, 0, 0 - ) - deepStrictEqual(actual, expected) - }) - - test("`Affine2.lookAt()` from origin to positive x-axis", () => { - const eye = new Vector2() - const target = new Vector2(1) - const affine = Affine2.lookAt(eye, target) - - const [position, orientation, scale] = affine.decompose() - - deepStrictEqual(position, new Vector2()) - deepStrictEqual(orientation, new Rotary(1, 0)) - deepStrictEqual(scale, new Vector2(1, 1)) - }) - - test("`Affine2.lookAt()` from origin to positive y-axis", () => { - const eye = new Vector2() - const target = new Vector2(0, 1) - const affine = Affine2.lookAt(eye, target) - - const [position, orientation, scale] = affine.decompose() - - deepStrictEqual(position, new Vector2()) - deepStrictEqual(orientation, new Rotary(0, 1)) - deepStrictEqual(scale, new Vector2(1, 1)) - }) - - test("`Affine2.lookAt()` from origin to negative x-axis", () => { - const eye = new Vector2() - const target = new Vector2(-1, 0) - const affine = Affine2.lookAt(eye, target) - - const [position, orientation, scale] = affine.decompose() - - deepStrictEqual(position, new Vector2()) - deepStrictEqual(orientation, new Rotary(-1, 0)) - deepStrictEqual(scale, new Vector2(1, 1)) - }) - - test("`Affine2.lookAt()` from origin to negative y-axis", () => { - const eye = new Vector2() - const target = new Vector2(0, -1) - const affine = Affine2.lookAt(eye, target) - - const [position, orientation, scale] = affine.decompose() - - deepStrictEqual(position, new Vector2()) - deepStrictEqual(orientation, new Rotary(0, -1)) - deepStrictEqual(scale, new Vector2(1, 1)) - }) - - test("`Affine2.lookAt()` from known position to up.", () => { - const eye = new Vector2(10, 10) - const target = new Vector2(10, 11) - const affine = Affine2.lookAt(eye, target) - - const [position, orientation, scale] = affine.decompose() - - deepStrictEqual(position, new Vector2(10, 10)) - deepStrictEqual(orientation, new Rotary(0, 1)) - deepStrictEqual(scale, new Vector2(1, 1)) - }) - - test("`Affine2.lookAt()` eye and target on same position.", () => { - const eye = new Vector2() - const target = new Vector2() - const affine = Affine2.lookAt(eye, target) - - const [position, orientation, scale] = affine.decompose() - - deepStrictEqual(position, new Vector2()) - deepStrictEqual(orientation, new Rotary(1, 0)) - deepStrictEqual(scale, new Vector2(1, 1)) - }) - - test("`Affine2.compose()` operates correctly (normal scale).", () => { - const position = new Vector2(100, 200) - const orientation = new Rotary(1, 0) - const scale = new Vector2(1, 1) - const affine = Affine2.compose(position, orientation, scale) - const expected = new Affine2( - 1, -0, 100, - 0, 1, 200 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine2.compose()` operates correctly (uniform scale).", () => { - const position = new Vector2(100, 200) - const orientation = new Rotary(1, 0) - const scale = new Vector2(4, 4) - const affine = Affine2.compose(position, orientation, scale) - const expected = new Affine2( - 4, -0, 100, - 0, 4, 200 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine2.compose()` operates correctly (non-uniform scale).", () => { - const position = new Vector2(100, 200) - const orientation = new Rotary(1, 0) - const scale = new Vector2(3, 2) - const affine = Affine2.compose(position, orientation, scale) - const expected = new Affine2( - 3, -0, 100, - 0, 2, 200 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine2.compose()` operates correctly (rotated to negative x-axis).", () => { - const position = new Vector2(100, 200) - const orientation = new Rotary(-1, 0) - const scale = new Vector2(1, 1) - const affine = Affine2.compose(position, orientation, scale) - const expected = new Affine2( - -1, -0, 100, - 0, -1, 200 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine2.compose()` operates correctly (rotated to positive y-axis).", () => { - const position = new Vector2(100, 200) - const orientation = new Rotary(0, 1) - const scale = new Vector2(1, 1) - const affine = Affine2.compose(position, orientation, scale) - const expected = new Affine2( - 0, -1, 100, - 1, 0, 200 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine2.compose()` operates correctly (rotated to negative y-axis).", () => { - const position = new Vector2(100, 200) - const orientation = new Rotary(0, -1) - const scale = new Vector2(1, 1) - const affine = Affine2.compose(position, orientation, scale) - const expected = new Affine2( - 0, 1, 100, - -1, 0, 200 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine2.compose()` operates correctly (rotated and large uniform scale).", () => { - const position = new Vector2(100, 200) - const orientation = new Rotary(0, -1) - const scale = new Vector2(100, 100) - const affine = Affine2.compose(position, orientation, scale) - const expected = new Affine2( - 0, 100, 100, - -100, 0, 200 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine2.decompose()` operates correctly (case 1).", () => { - const affine = new Affine2( - 1, 0, 100, - 0, 1, 200 - ) - const [position, orientation, scale] = Affine2.decompose(affine) - - deepStrictEqual(position, new Vector2(100, 200)) - deepStrictEqual(orientation, new Rotary(1, 0)) - deepStrictEqual(scale, new Vector2(1, 1)) - }) - - test("`Affine2.decompose()` operates correctly (case 2).", () => { - const affine = new Affine2( - 5, 0, 100, - 0, 3, 200 - ) - const [position, orientation, scale] = Affine2.decompose(affine) - - deepStrictEqual(position, new Vector2(100, 200)) - deepStrictEqual(orientation, new Rotary(1, 0)) - deepStrictEqual(scale, new Vector2(5, 3)) - }) - - test("`Affine2.decompose()` operates correctly (case 3).", () => { - const affine = new Affine2( - 0, -5, 100, - 3, 0, 200 - ) - const [position, orientation, scale] = Affine2.decompose(affine) - - deepStrictEqual(position, new Vector2(100, 200)) - deepStrictEqual(orientation, new Rotary(0, 1)) - deepStrictEqual(scale, new Vector2(3, 5)) - }) - - test("`Affine2.translate()` operates correctly.", () => { - const affine = new Affine2() - const position = new Vector2(100, 200) - const actual = Affine2.translate(affine, position) - const expected = new Affine2( - 1, 0, 100, - 0, 1, 200 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Affine2.rotate()` operates correctly.", () => { - const affine = new Affine2() - const rotation = new Rotary(0, 1) - const actual = Affine2.rotate(affine, rotation) - const expected = new Affine2( - 0, -1, 0, - 1, 0, 0 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Affine2.scale()` operates correctly (case 1).", () => { - const affine = new Affine2() - const rotation = new Vector2(100, 100) - const actual = Affine2.scale(affine, rotation) - const expected = new Affine2( - 100, 0, 0, - 0, 100, 0 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Affine2.scale()` operates correctly (case 2).", () => { - const affine = new Affine2().rotate(new Rotary(0, 1)) - const rotation = new Vector2(100, 100) - const actual = Affine2.scale(affine, rotation) - const expected = new Affine2( - 0, -100, 0, - 100, 0, 0 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Affine2.multiply` operates correctly (case 1)", () => { - const operand1 = new Affine2( - 1, 0, 100, - 0, 1, 100 - ) - const operand2 = new Affine2( - 0, -1, 100, - 1, 0, 100 - ) - - const expected = new Affine2( - 0, -1, 200, - 1, 0, 200 - ) - const result = Affine2.multiply(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Affine2.multiply` operates correctly (case 2)", () => { - const operand1 = new Affine2( - 0, -1, 100, - 1, 0, 100 - ) - const operand2 = new Affine2( - 1, 0, 100, - 0, 1, 100 - ) - const expected = new Affine2( - 0, -1, 0, - 1, 0, 200 - ) - const result = Affine2.multiply(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Affine2.divide` divides correctly", () => { - const operand1 = new Affine2( - 4, 3, 100, - 3, 2, 100 - ) - const operand2 = new Affine2( - 4, 3, 100, - 3, 2, 100 - ) - - const expected = new Affine2( - 1, 0, 0, - 0, 1, 0 - ) - const result = Affine2.divide(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Affine2.invert` operates correctly (case 1)", () => { - const affine = new Affine2( - 0, -1, 100, - 1, 0, 100 - ) - - const expected = new Affine2( - 0, 1, -100, - -1, 0, 100 - ) - const result = Affine2.invert(affine) - - deepStrictEqual(result, expected) - }) - - test("`Affine2.invert` operates correctly (case 2)", () => { - const affine = new Affine2( - 1, 0, 100, - 0, 1, 100 - ) - - const expected = new Affine2( - 1, -0, -100, - -0, 1, -100 - ) - const result = Affine2.invert(affine) - - deepStrictEqual(result, expected) - }) - - test("`Affine2.transform` operates correctly (case 1)", () => { - const affine = new Affine2( - 1, 0, 100, - 0, 1, 100 - ) - const vector = new Vector2(0, 0) - const expected = new Vector2(100, 100) - const result = Affine2.transform(affine, vector) - - deepStrictEqual(result, expected) - }) - - test("`Affine2.transform` operates correctly (case 2)", () => { - const affine = new Affine2( - 0, -1, 100, - 1, 0, 100 - ) - const vector = new Vector2(100, 0) - const expected = new Vector2(100, 200) - const result = Affine2.transform(affine, vector) - - deepStrictEqual(result, expected) - }) - - test("`Affine2.transform` operates correctly (case 3)", () => { - const affine = new Affine2( - 2, 0, 100, - 0, 3, 100 - ) - const vector = new Vector2(100, 0) - const expected = new Vector2(300, 100) - const result = Affine2.transform(affine, vector) - - deepStrictEqual(result, expected) - }) - - test("`Affine2.transform` operates correctly (case 4)", () => { - const affine = new Affine2( - 0, -3, 100, - 2, 0, 100 - ) - const vector = new Vector2(100, 200) - const expected = new Vector2(-500, 300) - const result = Affine2.transform(affine, vector) - - deepStrictEqual(result, expected) - }) - - test("`Affine2.equal` tests equality", () => { - const affine1 = new Affine2( - 1, 2, 5, - 3, 4, 6 - ) - const affine2 = new Affine2( - 1, 2, 5, - 3, 4, 6 - ) - - strictEqual(Affine2.equal(affine1, affine2), true) - }) - - test("`Affine2.Identity` is identity", () => { - const expected = new Affine2( - 1, 0, 0, - 0, 1, 0 - ) - deepStrictEqual(Affine2.Identity, expected) - }) - - test("`Affine2.Zero` is zero", () => { - const expected = new Affine2( - 0, 0, 0, - 0, 0, 0 - ) - deepStrictEqual(Affine2.Zero, expected) - }) -}) diff --git a/packages/math/tests/affines/affine3.test.js b/packages/math/tests/affines/affine3.test.js deleted file mode 100644 index 28ed85cf..00000000 --- a/packages/math/tests/affines/affine3.test.js +++ /dev/null @@ -1,643 +0,0 @@ -import { test, describe } from "vitest"; -import { Affine3,Vector3,Quaternion,INV_SQRT2 } from "../../src/core"; -import { deepStrictEqual, strictEqual } from "node:assert"; - -describe("Testing `Affine3`", () => { - test('`Affine3` constructor is correct', () => { - const affine = new Affine3( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12 - ) - strictEqual(affine.a, 1) - strictEqual(affine.b, 5) - strictEqual(affine.c, 9) - strictEqual(affine.d, 2) - strictEqual(affine.e, 6) - strictEqual(affine.f, 10) - strictEqual(affine.g, 3) - strictEqual(affine.h, 7) - strictEqual(affine.i, 11) - strictEqual(affine.x, 4) - strictEqual(affine.y, 8) - strictEqual(affine.z, 12) - }) - - test('`Affine3` default constructor is identity', () => { - const affine = new Affine3() - const expected = new Affine3( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine3.copy` actually copies", () => { - const source = new Affine3( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12 - ) - const destination = Affine3.copy(source) - - deepStrictEqual(destination, source) - }) - - test("`Affine3.serialize` returns an array serial", () => { - const affine = new Affine3( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12 - ) - - deepStrictEqual(Affine3.serialize(affine), [1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12]) - strictEqual(Affine3.validateSerial([1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12]), true) - }) - - test("`Affine3.deserialize` reads an array serial", () => { - const actual = Affine3.deserialize([1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12]) - const expected = new Affine3( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Affine3.set` sets values corrrectly", () => { - const expected = new Affine3( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12 - ) - const actual = Affine3.set( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12 - ) - - deepStrictEqual(actual, expected) - }) - - test('`Affine3.identity` is identity', () => { - const actual = Affine3.identity() - const expected = new Affine3( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0 - ) - deepStrictEqual(actual, expected) - }) - - test('`Affine3.zero` is zero', () => { - const actual = Affine3.zero() - const expected = new Affine3( - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0 - ) - deepStrictEqual(actual, expected) - }) - - test("`Affine3.lookAt()` from origin to positive x-axis", () => { - const eye = new Vector3() - const target = new Vector3(1) - const affine = Affine3.lookAt(eye, target, Vector3.Y) - - const [position, orientation, _] = affine.decompose() - - deepStrictEqual(position, new Vector3()) - strictEqual( - Quaternion.fuzzyEqual( - orientation, - new Quaternion(0, -INV_SQRT2, 0, INV_SQRT2), - 0.0005 - ), - true - ) - }) - - test("`Affine3.lookAt()` from origin to positive y-axis", () => { - const eye = new Vector3() - const target = new Vector3(0, 1) - const affine = Affine3.lookAt(eye, target, Vector3.Y) - - const [position, orientation, _] = affine.decompose() - - deepStrictEqual(position, new Vector3()) - strictEqual( - Quaternion.fuzzyEqual( - orientation, - new Quaternion(INV_SQRT2, 0, 0, INV_SQRT2), - 0.0005 - ), - true - ) - }) - - test("`Affine3.lookAt()` from origin to negative x-axis", () => { - const eye = new Vector3() - const target = new Vector3(-1, 0) - const affine = Affine3.lookAt(eye, target, Vector3.Y) - - const [position, orientation, _] = affine.decompose() - - deepStrictEqual(position, new Vector3()) - strictEqual( - Quaternion.fuzzyEqual( - orientation, - new Quaternion(0, INV_SQRT2, 0, INV_SQRT2), - 0.0005 - ), - true - ) - }) - - test("`Affine3.lookAt()` from origin to negative y-axis", () => { - const eye = new Vector3() - const target = new Vector3(0, -1) - const affine = Affine3.lookAt(eye, target, Vector3.Y) - - const [position, orientation, _] = affine.decompose() - - deepStrictEqual(position, new Vector3()) - strictEqual( - Quaternion.fuzzyEqual( - orientation, - new Quaternion(-INV_SQRT2, 0, 0, INV_SQRT2), - 0.0005 - ), - true - ) - }) - - test("`Affine3.lookAt()` from known position to up.", () => { - const eye = new Vector3(10, 10) - const target = new Vector3(10, 11) - const affine = Affine3.lookAt(eye, target, Vector3.Y) - - const [position, orientation, _] = affine.decompose() - - deepStrictEqual(position, new Vector3(10, 10)) - strictEqual( - Quaternion.fuzzyEqual( - orientation, - new Quaternion(INV_SQRT2, 0, 0, INV_SQRT2), - 0.0005 - ), - true - ) - }) - - test("`Affine3.lookAt()` eye and target on same position.", () => { - const eye = new Vector3() - const target = new Vector3() - const affine = Affine3.lookAt(eye, target, Vector3.Y) - - const [position, orientation, _] = affine.decompose() - - deepStrictEqual(position, new Vector3()) - deepStrictEqual(orientation, new Quaternion(0, 0, 0, 1)) - }) - - test("`Affine3.compose()` operates correctly (normal scale).", () => { - const position = new Vector3(100, 200, 300) - const orientation = new Quaternion(0, 0, 0, 1) - const scale = new Vector3(1, 1, 1) - const affine = Affine3.compose(position, orientation, scale) - const expected = new Affine3( - 1, 0, 0, 100, - 0, 1, 0, 200, - 0, 0, 1, 300 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine3.compose()` operates correctly (uniform scale).", () => { - const position = new Vector3(100, 200, 300) - const orientation = new Quaternion(0, 0, 0, 1) - const scale = new Vector3(4, 4, 4) - const affine = Affine3.compose(position, orientation, scale) - const expected = new Affine3( - 4, 0, 0, 100, - 0, 4, 0, 200, - 0, 0, 4, 300 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine3.compose()` operates correctly (non-uniform scale).", () => { - const position = new Vector3(100, 200, 300) - const orientation = new Quaternion(0, 0, 0, 1) - const scale = new Vector3(3, 2, 5) - const affine = Affine3.compose(position, orientation, scale) - const expected = new Affine3( - 3, 0, 0, 100, - 0, 2, 0, 200, - 0, 0, 5, 300 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine3.compose()` operates correctly (rotated).", () => { - const position = new Vector3(100, 200, 300) - const orientation = new Quaternion(1, 0, 0, 0) - const scale = new Vector3(1, 1, 1) - const affine = Affine3.compose(position, orientation, scale) - const expected = new Affine3( - 1, 0, 0, 100, - 0, -1, 0, 200, - 0, 0, -1, 300 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine3.compose()` operates correctly (rotated 2).", () => { - const position = new Vector3(100, 200, 300) - const orientation = new Quaternion(0, 1, 0, 0) - const scale = new Vector3(1, 1, 1) - const affine = Affine3.compose(position, orientation, scale) - const expected = new Affine3( - -1, 0, 0, 100, - 0, 1, 0, 200, - 0, 0, -1, 300 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine3.compose()` operates correctly (rotated 3).", () => { - const position = new Vector3(100, 200, 300) - const orientation = new Quaternion(0, 0, 1, 0) - const scale = new Vector3(1, 1, 1) - const affine = Affine3.compose(position, orientation, scale) - const expected = new Affine3( - -1, 0, 0, 100, - 0, -1, 0, 200, - 0, 0, 1, 300 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine3.compose()` operates correctly (rotated and large uniform scale).", () => { - const position = new Vector3(100, 200, 300) - const orientation = new Quaternion(0, -1, 0, 0) - const scale = new Vector3(100, 100, 100) - const affine = Affine3.compose(position, orientation, scale) - const expected = new Affine3( - -100, -0, 0, 100, - 0, 100, -0, 200, - 0, 0, -100, 300 - ) - - deepStrictEqual(affine, expected) - }) - - test("`Affine3.decompose()` operates correctly (case 1).", () => { - const affine = new Affine3( - 1, 0, 0, 100, - 0, 1, 0, 200, - 0, 0, 1, 300 - ) - const [position, orientation, scale] = Affine3.decompose(affine) - - deepStrictEqual(position, new Vector3(100, 200, 300)) - deepStrictEqual(orientation, new Quaternion(0, 0, 0, 1)) - deepStrictEqual(scale, new Vector3(1, 1, 1)) - }) - - test("`Affine3.decompose()` operates correctly (case 2).", () => { - const affine = new Affine3( - 1, 0, 0, 100, - 0, -1, 0, 200, - 0, 0, -1, 300 - ) - const [position, orientation, scale] = Affine3.decompose(affine) - - deepStrictEqual(position, new Vector3(100, 200, 300)) - deepStrictEqual(orientation, new Quaternion(1, 0, 0, 0)) - deepStrictEqual(scale, new Vector3(1, 1, 1)) - }) - - test("`Affine3.decompose()` operates correctly (case 3).", () => { - const affine = new Affine3( - 100, 0, 0, 100, - 0, -4, 0, 200, - 0, 0, -18, 300 - ) - const [position, orientation, scale] = Affine3.decompose(affine) - - deepStrictEqual(position, new Vector3(100, 200, 300)) - deepStrictEqual(orientation, new Quaternion(1, 0, 0, 0)) - deepStrictEqual(scale, new Vector3(100, 4, 18)) - }) - - test("`Affine3.translate()` operates correctly.", () => { - const affine = new Affine3() - const position = new Vector3(100, 200, 300) - const actual = Affine3.translate(affine, position) - const expected = new Affine3( - 1, 0, 0, 100, - 0, 1, 0, 200, - 0, 0, 1, 300 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Affine3.rotate()` operates correctly (case 1).", () => { - const affine = new Affine3() - const rotation = new Quaternion(1, 0, 0, 0) - const actual = Affine3.rotate(affine, rotation) - const expected = new Affine3( - 1, 0, 0, 0, - 0, -1, 0, 0, - 0, 0, -1, 0 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Affine3.rotate()` operates correctly (case 2).", () => { - const affine = new Affine3( - 1, 0, 0, 0, - 0, -1, 0, 0, - 0, 0, -1, 0 - ) - const rotation = new Quaternion(1, 0, 0, 0) - const actual = Affine3.rotate(affine, rotation) - const expected = new Affine3( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Affine3.scale()` operates correctly (case 1).", () => { - const affine = new Affine3() - const scale = new Vector3(1, 1, 1) - const actual = Affine3.scale(affine, scale) - const expected = new Affine3( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Affine3.scale()` operates correctly (case 2).", () => { - const affine = new Affine3().rotate(new Quaternion(1, 0, 0, 0)) - const scale = new Vector3(100, 10, 35) - const actual = Affine3.scale(affine, scale) - const expected = new Affine3( - 100, 0, 0, 0, - 0, -10, 0, 0, - 0, 0, -35, 0 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Affine3.multiply` operates correctly (case 1)", () => { - const operand1 = new Affine3( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0 - ) - const operand2 = new Affine3( - 1, 0, 0, 100, - 0, 1, 0, 200, - 0, 0, 1, 300 - ) - - const expected = new Affine3( - 1, 0, 0, 100, - 0, 1, 0, 200, - 0, 0, 1, 300 - ) - const result = Affine3.multiply(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.multiply` operates correctly (case 2)", () => { - const operand1 = new Affine3( - 1, 0, 0, 100, - 0, -1, 0, 0, - 0, 0, -1, 0 - ) - const operand2 = new Affine3( - -1, 0, 0, 100, - 0, 1, 0, 200, - 0, 0, -1, 300 - ) - const expected = new Affine3( - -1, 0, 0, 200, - 0, -1, 0, -200, - 0, 0, 1, -300 - ) - const result = Affine3.multiply(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.multiply` operates correctly (case 3)", () => { - const operand1 = new Affine3( - 3, 0, 0, 100, - 0, -2, 0, 0, - 0, 0, -10, 0 - ) - const operand2 = new Affine3( - -1, 0, 0, 100, - 0, 1, 0, 200, - 0, 0, -1, 300 - ) - const expected = new Affine3( - -3, 0, 0, 400, - 0, -2, 0, -400, - 0, 0, 10, -3000 - ) - const result = Affine3.multiply(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.divide` divides correctly", () => { - const operand1 = new Affine3( - 5, 0, 0, 100, - 0, -2, 0, 0, - 0, 0, -10, 0 - ) - const operand2 = new Affine3( - 5, 0, 0, 100, - 0, -2, 0, 0, - 0, 0, -10, 0 - ) - - const expected = new Affine3( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0 - ) - const result = Affine3.divide(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.invert` operates correctly (case 1)", () => { - const affine = new Affine3( - 1, 0, 0, 100, - 0, 1, 0, 200, - 0, 0, 1, 300 - ) - - const expected = new Affine3( - 1, 0, 0, -100, - 0, 1, 0, -200, - 0, 0, 1, -300 - ) - const result = Affine3.invert(affine) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.invert` operates correctly (case 2)", () => { - const affine = new Affine3( - 1, 0, 0, 100, - 0, -1, 0, 200, - 0, 0, -1, -300 - ) - - const expected = new Affine3( - 1, 0, 0, -100, - 0, -1, 0, 200, - 0, 0, -1, -300 - ) - - const result = Affine3.invert(affine) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.transform` operates correctly (case 1)", () => { - const affine = new Affine3( - 1, 0, 0, 100, - 0, 1, 0, 100, - 0, 0, 1, 100 - ) - const vector = new Vector3(0, 0, 0) - const expected = new Vector3(100, 100, 100) - const result = Affine3.transform(affine, vector) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.transform` operates correctly (case 2)", () => { - const affine = new Affine3( - -1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, -1, 0, - ) - const vector = new Vector3(100, 200, 300) - const expected = new Vector3(-100, 200, -300) - const result = Affine3.transform(affine, vector) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.transform` operates correctly (case 3)", () => { - const affine = new Affine3( - 0, -1, 0, 0, - 1, 0, 0, 0, - 0, 0, -1, 0, - ) - const vector = new Vector3(100, 200, 300) - const expected = new Vector3(-200, 100, -300) - const result = Affine3.transform(affine, vector) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.transform` operates correctly (case 4)", () => { - const affine = new Affine3( - 2, 0, 0, 0, - 0, 3, 0, 0, - 0, 0, 5, 0, - ) - const vector = new Vector3(100, 200, 300) - const expected = new Vector3(200, 600, 1500) - const result = Affine3.transform(affine, vector) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.transform` operates correctly (case 5)", () => { - const affine = new Affine3( - 0, 0, -10, 0, - -4, 0, 0, 0, - 0, 5, 0, 0, - ) - const vector = new Vector3(100, 200, 300) - const expected = new Vector3(-3000, -400,1000) - const result = Affine3.transform(affine, vector) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.transform` operates correctly (case 6)", () => { - const affine = new Affine3( - 0, 0, -10, 3100, - -4, 0, 0, 500, - 0, 5, 0, -900, - ) - const vector = new Vector3(100, 200, 300) - const expected = new Vector3(100, 100,100) - const result = Affine3.transform(affine, vector) - - deepStrictEqual(result, expected) - }) - - test("`Affine3.equal` tests equality", () => { - const affine1 = new Affine3( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12 - ) - const affine2 = new Affine3( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12 - ) - - strictEqual(Affine3.equal(affine1, affine2), true) - }) - - test("`Affine3.Identity` is identity", () => { - const expected = new Affine3( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0 - ) - deepStrictEqual(Affine3.Identity, expected) - }) - - test("`Affine3.Zero` is zero", () => { - const expected = new Affine3( - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0 - ) - deepStrictEqual(Affine3.Zero, expected) - }) -}) diff --git a/packages/math/tests/angles/quaternion.test.js b/packages/math/tests/angles/quaternion.test.js deleted file mode 100644 index fcce3021..00000000 --- a/packages/math/tests/angles/quaternion.test.js +++ /dev/null @@ -1,139 +0,0 @@ -import { test, describe } from "vitest"; -import { Quaternion,PI } from "../../src/core"; -import { deepStrictEqual, strictEqual } from "node:assert"; - -describe("Testing `Quaternion`", () => { - test('`Quaternion` constructor works correctly', () => { - const quaternion = new Quaternion(1, 2, 3, 4) - - strictEqual(quaternion.x, 1) - strictEqual(quaternion.y, 2) - strictEqual(quaternion.z, 3) - strictEqual(quaternion.w, 4) - }) - - test('`Quaternion` default constructor is identity', () => { - const quaternion = new Quaternion() - const expected = new Quaternion(0, 0, 0, 1) - - deepStrictEqual(quaternion, expected) - }) - - test('`Quaternion.set` sets correctly', () => { - const quaternion = Quaternion.set(1, 2, 3, 4) - const expected = new Quaternion(1, 2, 3, 4) - - deepStrictEqual(quaternion, expected) - }) - - test('`Quaternion.copy` copies correctly', () => { - const quaternion = new Quaternion(1, 2, 3, 4) - const actual = Quaternion.copy(quaternion) - const expected = new Quaternion(1, 2, 3, 4) - - deepStrictEqual(actual, expected) - }) - - test('`Quaternion.identity` is identity', () => { - const quaternion = Quaternion.identity() - const expected = new Quaternion(0, 0, 0, 1) - - deepStrictEqual(quaternion, expected) - }) - - test('`Quaternion.zero` is zero', () => { - const quaternion = Quaternion.zero() - const expected = new Quaternion(0, 0, 0, 0) - - deepStrictEqual(quaternion, expected) - }) - - test("`Quaternion.magnitudeSquared` operates correctly", () => { - const operand = new Quaternion(3, 4, 0, 0) - - deepStrictEqual(Quaternion.magnitudeSquared(operand), 25) - }) - - test("`Quaternion.magnitude` operates correctly", () => { - const operand = new Quaternion(3, 4, 0, 0) - - deepStrictEqual(Quaternion.magnitude(operand), 5) - }) - - test("`Quaternion.normalize` returns a unit vector", () => { - const quaternion = new Quaternion(0, 0, 0, 1) - const normalized = Quaternion.normalize(quaternion) - - strictEqual(Quaternion.magnitude(normalized), 1) - }) - - test("`Quaternion.normalize` preserves direction", () => { - const quaternion = new Quaternion(0, 10, 0, 0) - const expected = new Quaternion(0, 1, 0, 0) - const actual = Quaternion.normalize(quaternion) - - deepStrictEqual(actual, expected) - }) - - test("`Quaternion.dot` operates correctly", () => { - const operand1 = new Quaternion(1, 2, 9, 8) - const operand2 = new Quaternion(3, 4, 7, 6) - - deepStrictEqual(Quaternion.dot(operand1, operand2), 122) - }) - - test('`Quaternion.reverse` operates correctly', () => { - const quaternion = new Quaternion(1, -2, 3, 4) - const actual = Quaternion.reverse(quaternion) - const expected = new Quaternion(-1, 2, -3, 4) - - deepStrictEqual(actual, expected) - }) - - test("`Quaternion.multiply` operates correctly", () => { - const quat1 = new Quaternion(0, 1, 0, 0) - const quat2 = new Quaternion(0, -1, 0, 0) - const expected = new Quaternion(0, 0, 0, 1) - const actual = Quaternion.multiply(quat1, quat2) - - deepStrictEqual(actual, expected) - }) - - test("`Quaternion.rotateX` operates correctly", () => { - const expected = new Quaternion(1, 0, 0, 0) - const actual = Quaternion.rotateX(PI) - - strictEqual(Quaternion.fuzzyEqual(actual, expected), true) - }) - - test("`Quaternion.rotateY` operates correctly", () => { - const expected = new Quaternion(0, 1, 0, 0) - const actual = Quaternion.rotateY(PI) - - strictEqual(Quaternion.fuzzyEqual(actual, expected), true) - }) - - test("`Quaternion.rotateZ` operates correctly", () => { - const expected = new Quaternion(0, 0, 1, 0) - const actual = Quaternion.rotateZ(PI) - - strictEqual(Quaternion.fuzzyEqual(actual, expected), true) - }) - - test("`Quaternion.equal` checks equality correctly", () => { - const vector1 = new Quaternion(2, 5, 8, 3) - const vector2 = new Quaternion(2, 5, 8, 3) - - strictEqual(Quaternion.equal(vector1, vector2), true) - }) - - test("`Quaternion.Identity` is identity", () => { - const expected = new Quaternion(0, 0, 0, 1) - deepStrictEqual(Quaternion.Identity, expected) - }) - - test("`Quaternion.Zero` is zero", () => { - const expected = new Quaternion(0, 0, 0, 0) - deepStrictEqual(Quaternion.Zero, expected) - }) -}) \ No newline at end of file diff --git a/packages/math/tests/angles/rotary.test.js b/packages/math/tests/angles/rotary.test.js deleted file mode 100644 index cfcb7aa8..00000000 --- a/packages/math/tests/angles/rotary.test.js +++ /dev/null @@ -1,182 +0,0 @@ -import { test, describe } from "vitest"; -import { Rotary,HALF_PI, PI } from "../../src/core"; -import { deepStrictEqual, strictEqual } from "node:assert"; - -describe("Testing `Rotary`", () => { - test("`Rotary` constructor works correctly", () => { - const rotary = new Rotary(1, 2) - - strictEqual(rotary.cos, 1) - strictEqual(rotary.sin, 2) - }) - - test("`Rotary` default constructor is identity", () => { - const rotary = new Rotary() - const expected = new Rotary(1, 0) - - deepStrictEqual(rotary, expected) - }) - - test("`Rotary.copy` copies correctly", () => { - const rotary = new Rotary(1, 5) - const expected = Rotary.copy(rotary) - - deepStrictEqual(rotary, expected) - }) - - test("`Rotary.set` sets correctly", () => { - const rotary = Rotary.set(1, 2) - const expected = new Rotary(1, 2) - - deepStrictEqual(rotary, expected) - }) - - test("`Rotary.identity` is actually an identity", () => { - const expected = new Rotary(1, 0) - const actual = Rotary.identity() - - deepStrictEqual(actual, expected) - }) - - test("`Rotary.zero` is actually an zero", () => { - const expected = new Rotary(0, 0) - const actual = Rotary.zero() - - - deepStrictEqual(actual, expected) - }) - - test("`Rotary.normalize` returns a unit rotary", () => { - const rotary = new Rotary(1, 0) - const normalized = Rotary.normalize(rotary) - - strictEqual(Rotary.magnitude(normalized), 1) - }) - - test("`Rotary.normalize` preserves direction", () => { - const rotary = new Rotary(10, 0) - const expected = new Rotary(1, 0) - const actual = Rotary.normalize(rotary) - - deepStrictEqual(actual, expected) - }) - - test("`Rotary.magnitudeSquared` operates correctly", () => { - const operand = new Rotary(3, 4) - - deepStrictEqual(Rotary.magnitudeSquared(operand), 25) - }) - - test("`Rotary.magnitude` operates correctly", () => { - const operand = new Rotary(3, 4) - - deepStrictEqual(Rotary.magnitude(operand), 5) - }) - - test("`Rotary.multiply` operates correctly (case 1)", () => { - const rotary1 = new Rotary(1, 0) - const rotary2 = new Rotary(1, 0) - const expected = new Rotary(1, 0) - const actual = Rotary.multiply(rotary1, rotary2) - - deepStrictEqual(actual, expected) - }) - - test("`Rotary.multiply` operates correctly (case 2)", () => { - const rotary1 = new Rotary(1, 0) - const rotary2 = new Rotary(0, 1) - const expected = new Rotary(0, 1) - const actual = Rotary.multiply(rotary1, rotary2) - - deepStrictEqual(actual, expected) - }) - - test("`Rotary.multiply` operates correctly (case 3)", () => { - const rotary1 = new Rotary(0, 1) - const rotary2 = new Rotary(0, 1) - const expected = new Rotary(-1, 0) - const actual = Rotary.multiply(rotary1, rotary2) - - deepStrictEqual(actual, expected) - }) - - test("`Rotary.multiply` operates correctly (case 4)", () => { - const rotary1 = new Rotary(0, 1) - const rotary2 = new Rotary(-1, 0) - const expected = new Rotary(-0, -1) - const actual = Rotary.multiply(rotary1, rotary2) - - deepStrictEqual(actual, expected) - }) - - test("`Rotary.multiplyScalar` operates correctly", () => { - const rotary1 = new Rotary(0, 1) - const rotary2 = new Rotary(-1, 0) - const expected = new Rotary(-0, -1) - const actual = Rotary.multiply(rotary1, rotary2) - - deepStrictEqual(actual, expected) - }) - - test("`Rotary.rotate` operates correctly (case 1)", () => { - const expected = new Rotary(0, 1) - const actual = Rotary.rotate(HALF_PI) - - strictEqual(Rotary.fuzzyEqual(expected,actual), true) - }) - - test("`Rotary.rotate` operates correctly (case 2)", () => { - const expected = new Rotary(0, -1) - const actual = Rotary.rotate(-HALF_PI) - - strictEqual(Rotary.fuzzyEqual(expected,actual), true) - }) - - test("`Rotary.rotate` operates correctly (case 3)", () => { - const expected = new Rotary(-1, 0) - const actual = Rotary.rotate(PI) - - strictEqual(Rotary.fuzzyEqual(expected,actual), true) - }) - - test("`Rotary.rotate` operates correctly (case 4)", () => { - const expected = new Rotary(-1,0) - const actual = Rotary.rotate(-PI) - - strictEqual(Rotary.fuzzyEqual(expected,actual), true) - }) - - test("`Rotary.reverse` operates correctly", () => { - const operand = new Rotary(4, 6) - const expected = new Rotary(-4, -6) - const actual = Rotary.reverse(operand) - - deepStrictEqual(actual, expected) - }) - - test("`Rotary.dot` operates correctly", () => { - const operand1 = new Rotary(1, 2) - const operand2 = new Rotary(3, 4) - - deepStrictEqual(Rotary.dot(operand1, operand2), 11) - }) - - test("`Rotary.equal` checks equality correctly", () => { - const vector1 = new Rotary(2, 5) - const vector2 = new Rotary(2, 5) - - strictEqual(Rotary.equal(vector1, vector2), true) - }) - - test("`Rotary.Identity` is actually an identity", () => { - const expected = new Rotary(1, 0) - - deepStrictEqual(Rotary.Identity, expected) - }) - - test("`Rotary.Zero` is actually an identity", () => { - const expected = new Rotary(0, 0) - - deepStrictEqual(Rotary.Zero, expected) - }) -}) \ No newline at end of file diff --git a/packages/math/tests/matrices/matrix2.test.js b/packages/math/tests/matrices/matrix2.test.js deleted file mode 100644 index 2f177a66..00000000 --- a/packages/math/tests/matrices/matrix2.test.js +++ /dev/null @@ -1,300 +0,0 @@ -import { test, describe } from "vitest"; -import { deepStrictEqual, strictEqual } from "node:assert"; -import { Matrix2 } from "../../src/core"; - -describe("Testing `Matrix2`", () => { - test('`Matrix2` default constructor is identity', () => { - const actual = new Matrix2() - const expected = new Matrix2( - 1, 0, - 0, 1 - ) - deepStrictEqual(actual, expected) - }) - - test("`Matrix2.copy` actually copies", () => { - const source = new Matrix2( - 1, 2, - 3, 4 - ) - const destination = Matrix2.copy(source) - - deepStrictEqual(destination, source) - }) - - test("`Matrix2.serialize` returns an array serial", () => { - const matrix = new Matrix2( - 1, 2, - 3, 4 - ) - - deepStrictEqual(Matrix2.serialize(matrix), [1, 3, 2, 4]) - strictEqual(Matrix2.validateSerial([1, 3, 2, 4]), true) - }) - - test("`Matrix2.deserialize` reads an array serial", () => { - const actual = Matrix2.deserialize([1, 3, 2, 4]) - const expected = new Matrix2( - 1, 2, - 3, 4 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Matrix2.set` sets values corrrectly", () => { - const expected = new Matrix2( - 1, 2, - 3, 4 - ) - const actual = Matrix2.set( - 1, 2, - 3, 4 - ) - - deepStrictEqual(actual, expected) - }) - - test('`Matrix2.identity` is identity', () => { - const actual = Matrix2.identity() - const expected = new Matrix2( - 1, 0, - 0, 1 - ) - deepStrictEqual(actual, expected) - }) - - test('`Matrix2.zero` is zero', () => { - const actual = Matrix2.zero() - const expected = new Matrix2( - 0, 0, - 0, 0 - ) - deepStrictEqual(actual, expected) - }) - - test("`Matrix2.transpose` actually transposes", () => { - const source = new Matrix2( - 1, 2, - 3, 4 - ) - const expected = new Matrix2( - 1, 3, - 2, 4 - ) - const destination = Matrix2.transpose(source) - - deepStrictEqual(destination, expected) - }) - - test("`Matrix2.determinant` gives determinant", () => { - const matrix = new Matrix2( - 3, 2, - 2, 3 - ) - - strictEqual(matrix.determinant(), 5) - }) - - test("`Matrix2.trace` gives trace", () => { - const matrix = new Matrix2( - 1, 2, - 3, 4 - ) - - strictEqual(matrix.trace(), 5) - }) - - test("`Matrix2.add` adds correctly", () => { - const operand1 = new Matrix2( - 1, 2, - 3, 4 - ) - const operand2 = new Matrix2( - 1, 2, - 3, 4 - ) - - const expected = new Matrix2( - 2, 4, - 6, 8 - ) - const result = Matrix2.add(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix2.addScalar` adds correctly", () => { - const operand1 = new Matrix2( - 1, 2, - 3, 4 - ) - - const expected = new Matrix2( - 5, 6, - 7, 8 - ) - const result = Matrix2.addScalar(operand1, 4) - - deepStrictEqual(result, expected) - }) - - test("`Matrix2.subtract` subtracts correctly", () => { - const operand1 = new Matrix2( - 1, 2, - 3, 4 - ) - const operand2 = new Matrix2( - 1, 2, - 3, 4 - ) - - const expected = new Matrix2( - 0, 0, - 0, 0 - ) - const result = Matrix2.subtract(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix2.subtractScalar` subtracts correctly", () => { - const operand1 = new Matrix2( - 1, 2, - 3, 4 - ) - - const expected = new Matrix2( - -3, -2, - -1, 0 - ) - const result = Matrix2.subtractScalar(operand1, 4) - - deepStrictEqual(result, expected) - }) - - test("`Matrix2.multiply` multiplies correctly", () => { - const operand1 = new Matrix2( - 2, 1, - 3, 4 - ) - const operand2 = new Matrix2( - 5, 6, - 7, 8 - ) - - const expected = new Matrix2( - 17, 20, - 43, 50 - ) - const result = Matrix2.multiply(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix2.multiplyScalar` multiplies correctly", () => { - const operand1 = new Matrix2( - 1, 2, - 3, 4 - ) - - const expected = new Matrix2( - 4, 8, - 12, 16 - ) - const result = Matrix2.multiplyScalar(operand1, 4) - - deepStrictEqual(result, expected) - }) - - test("`Matrix2.divide` divides correctly", () => { - const operand1 = new Matrix2( - 1, 2, - 3, 4 - ) - const operand2 = new Matrix2( - 1, 2, - 3, 4 - ) - - const expected = new Matrix2( - 1, 0, - 0, 1 - ) - const result = Matrix2.divide(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix2.divideScalar` divides correctly", () => { - const operand1 = new Matrix2( - 2, 4, - 6, 8 - ) - - const expected = new Matrix2( - 1, 2, - 3, 4 - ) - const result = Matrix2.divideScalar(operand1, 2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix2.invert` actually invert", () => { - const matrix = new Matrix2( - 4, 3, - 3, 2 - ) - const expected = new Matrix2( - -2, 3, - 3, -4 - ) - const destination = Matrix2.invert(matrix) - - deepStrictEqual(destination, expected) - }) - - test("`Matrix2.invert` with no solution returns zero", () => { - const matrix = new Matrix2( - 2, 4, - 2, 4 - ) - const expected = new Matrix2( - 0, 0, - 0, 0 - ) - const destination = Matrix2.invert(matrix) - - deepStrictEqual(destination, expected) - }) - - test("`Matrix2.equal` tests equality", () => { - const matrix1 = new Matrix2( - 1, 2, - 3, 4 - ) - const matrix2 = new Matrix2( - 1, 2, - 3, 4 - ) - - strictEqual(Matrix2.equal(matrix1, matrix2), true) - }) - - test("`Matrix2.Identity` is identity", () => { - const expected = new Matrix2( - 1, 0, - 0, 1 - ) - deepStrictEqual(Matrix2.Identity, expected) - }) - - test("`Matrix2.Zero` is zero", () => { - const expected = new Matrix2( - 0, 0, - 0, 0 - ) - deepStrictEqual(Matrix2.Zero, expected) - }) -}) diff --git a/packages/math/tests/matrices/matrix3.test.js b/packages/math/tests/matrices/matrix3.test.js deleted file mode 100644 index 4107cf14..00000000 --- a/packages/math/tests/matrices/matrix3.test.js +++ /dev/null @@ -1,340 +0,0 @@ -import { test, describe } from "vitest"; -import { deepStrictEqual, strictEqual } from "node:assert"; -import { Matrix3 } from "../../src/core"; - -describe("Testing `Matrix3`", () => { - test('`Matrix3` default constructor is identity', () => { - const actual = new Matrix3() - const expected = new Matrix3( - 1, 0, 0, - 0, 1, 0, - 0, 0, 1 - ) - deepStrictEqual(actual, expected) - }) - - test("`Matrix3.copy` actually copies", () => { - const source = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - const destination = Matrix3.copy(source) - - deepStrictEqual(destination, source) - }) - - test("`Matrix3.serialize` returns an array serial", () => { - const matrix = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - - deepStrictEqual(Matrix3.serialize(matrix), [1, 4, 7, 2, 5, 8, 3, 6, 9]) - strictEqual(Matrix3.validateSerial([1, 4, 7, 2, 5, 8, 3, 6, 9]), true) - }) - - test("`Matrix3.deserialize` reads an array serial", () => { - const actual = Matrix3.deserialize([1, 4, 7, 2, 5, 8, 3, 6, 9]) - const expected = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Matrix3.set` sets values corrrectly", () => { - const expected = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - const actual = Matrix3.set( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - - deepStrictEqual(actual, expected) - }) - - test('`Matrix3.identity` is identity', () => { - const actual = Matrix3.identity() - const expected = new Matrix3( - 1, 0, 0, - 0, 1, 0, - 0, 0, 1 - ) - deepStrictEqual(actual, expected) - }) - - test('`Matrix3.zero` is zero', () => { - const actual = Matrix3.zero() - const expected = new Matrix3( - 0, 0, 0, - 0, 0, 0, - 0, 0, 0 - ) - deepStrictEqual(actual, expected) - }) - - test("`Matrix3.transpose` actually transposes", () => { - const source = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - const expected = new Matrix3( - 1, 4, 7, - 2, 5, 8, - 3, 6, 9 - ) - const destination = Matrix3.transpose(source) - - deepStrictEqual(destination, expected) - }) - - test("`Matrix3.determinant` gives determinant", () => { - const matrix = new Matrix3( - 1, 3, 0, - 4, 1, 0, - 2, 0, 1 - ) - - strictEqual(matrix.determinant(), -11) - }) - - test("`Matrix3.trace` gives trace", () => { - const matrix = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - - strictEqual(matrix.trace(), 15) - }) - - test("`Matrix3.add` adds correctly", () => { - const operand1 = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - const operand2 = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - - const expected = new Matrix3( - 2, 4, 6, - 8, 10, 12, - 14, 16, 18 - ) - const result = Matrix3.add(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix3.addScalar` adds correctly", () => { - const operand1 = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - - const expected = new Matrix3( - 2, 3, 4, - 5, 6, 7, - 8, 9, 10 - ) - const result = Matrix3.addScalar(operand1, 1) - - deepStrictEqual(result, expected) - }) - - test("`Matrix3.subtract` subtracts correctly", () => { - const operand1 = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - const operand2 = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - - const expected = new Matrix3( - 0, 0, 0, - 0, 0, 0, - 0, 0, 0 - ) - const result = Matrix3.subtract(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix3.subtractScalar` subtracts correctly", () => { - const operand1 = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - - const expected = new Matrix3( - 0, 1, 2, - 3, 4, 5, - 6, 7, 8 - ) - const result = Matrix3.subtractScalar(operand1, 1) - - deepStrictEqual(result, expected) - }) - - test("`Matrix3.multiply` multiplies correctly", () => { - const operand1 = new Matrix3( - 1, 8, 3, - 9, 4, 5, - 6, 2, 7 - ) - const operand2 = new Matrix3( - 6, 7, 4, - 1, 3, 2, - 5, 9, 8 - ) - - const expected = new Matrix3( - 29, 58, 44, - 83, 120, 84, - 73, 111, 84 - ) - const result = Matrix3.multiply(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix3.multiplyScalar` multiplies correctly", () => { - const operand1 = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - - const expected = new Matrix3( - 2, 4, 6, - 8, 10, 12, - 14, 16, 18 - ) - const result = Matrix3.multiplyScalar(operand1, 2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix3.divide` divides correctly", () => { - const operand1 = new Matrix3( - 1, 2, 3, - 0, 1, 4, - 5, 6, 0 - ) - const operand2 = new Matrix3( - 1, 2, 3, - 0, 1, 4, - 5, 6, 0 - ) - - const expected = new Matrix3( - 1, 0, 0, - 0, 1, 0, - 0, 0, 1 - ) - const result = Matrix3.divide(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix3.divideScalar` divides correctly", () => { - const operand1 = new Matrix3( - 2, 4, 6, - 8, 10, 12, - 14, 16, 18 - ) - - const expected = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - const result = Matrix3.divideScalar(operand1, 2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix3.invert` actually invert", () => { - const matrix = new Matrix3( - 1, 2, 3, - 0, 1, 4, - 5, 6, 0 - ) - const expected = new Matrix3( - -24, 18, 5, - 20, -15, -4, - -5, 4, 1 - ) - const destination = Matrix3.invert(matrix) - - deepStrictEqual(destination, expected) - }) - - test("`Matrix3.invert` with no solution returns zero", () => { - const matrix = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - const expected = new Matrix3( - 0, 0, 0, - 0, 0, 0, - 0, 0, 0 - ) - const destination = Matrix3.invert(matrix) - - deepStrictEqual(destination, expected) - }) - - test("`Matrix3.equal` tests equality", () => { - const matrix1 = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - const matrix2 = new Matrix3( - 1, 2, 3, - 4, 5, 6, - 7, 8, 9 - ) - - strictEqual(Matrix3.equal(matrix1, matrix2), true) - }) - - test("`Matrix3.Identity` is identity", () => { - const expected = new Matrix3( - 1, 0, 0, - 0, 1, 0, - 0, 0, 1 - ) - deepStrictEqual(Matrix3.Identity, expected) - }) - - test("`Matrix3.Zero` is zero", () => { - const expected = new Matrix3( - 0, 0, 0, - 0, 0, 0, - 0, 0, 0 - ) - deepStrictEqual(Matrix3.Zero, expected) - }) -}) diff --git a/packages/math/tests/matrices/matrix4.test.js b/packages/math/tests/matrices/matrix4.test.js deleted file mode 100644 index 7881a230..00000000 --- a/packages/math/tests/matrices/matrix4.test.js +++ /dev/null @@ -1,380 +0,0 @@ -import { test, describe } from "vitest"; -import { deepStrictEqual, strictEqual } from "node:assert"; -import { Matrix4 } from "../../src/core"; - -describe("Testing `Matrix4`", () => { - test('`Matrix4` default constructor is identity', () => { - const actual = new Matrix4() - const expected = new Matrix4( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1 - ) - deepStrictEqual(actual, expected) - }) - - test("`Matrix4.copy` actually copies", () => { - const source = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - const destination = Matrix4.copy(source) - - deepStrictEqual(destination, source) - }) - - test("`Matrix4.serialize` returns an array serial", () => { - const matrix = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - deepStrictEqual(Matrix4.serialize(matrix), [1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16]) - strictEqual(Matrix4.validateSerial([1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16]), true) - }) - - test("`Matrix4.deserialize` reads an array serial", () => { - const actual = Matrix4.deserialize([1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16]) - const expected = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - deepStrictEqual(actual, expected) - }) - - test("`Matrix4.set` sets values corrrectly", () => { - const expected = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - const actual = Matrix4.set( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - deepStrictEqual(actual, expected) - }) - - test('`Matrix4.identity` is identity', () => { - const actual = Matrix4.identity() - const expected = new Matrix4( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1 - ) - deepStrictEqual(actual, expected) - }) - - test('`Matrix4.zero` is zero', () => { - const actual = Matrix4.zero() - const expected = new Matrix4( - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0 - ) - deepStrictEqual(actual, expected) - }) - - test("`Matrix4.transpose` actually transposes", () => { - const source = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - const expected = new Matrix4( - 1, 5, 9, 13, - 2, 6, 10, 14, - 3, 7, 11, 15, - 4, 8, 12, 16 - ) - const destination = Matrix4.transpose(source) - - deepStrictEqual(destination, expected) - }) - - test("`Matrix4.determinant` gives determinant", () => { - const matrix = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - strictEqual(matrix.determinant(), 0) - }) - - test("`Matrix4.trace` gives trace", () => { - const matrix = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - strictEqual(matrix.trace(), 34) - }) - - test("`Matrix4.add` adds correctly", () => { - const operand1 = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - const operand2 = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - const expected = new Matrix4( - 2, 4, 6, 8, - 10, 12, 14, 16, - 18, 20, 22, 24, - 26, 28, 30, 32 - ) - const result = Matrix4.add(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix4.addScalar` adds correctly", () => { - const operand1 = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - const expected = new Matrix4( - 2, 3, 4, 5, - 6, 7, 8, 9, - 10, 11, 12, 13, - 14, 15, 16, 17 - ) - const result = Matrix4.addScalar(operand1, 1) - - deepStrictEqual(result, expected) - }) - - test("`Matrix4.subtract` subtracts correctly", () => { - const operand1 = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - const operand2 = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - const expected = new Matrix4( - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0 - ) - const result = Matrix4.subtract(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix4.subtractScalar` subtracts correctly", () => { - const operand1 = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - const expected = new Matrix4( - 0, 1, 2, 3, - 4, 5, 6, 7, - 8, 9, 10, 11, - 12, 13, 14, 15, - ) - const result = Matrix4.subtractScalar(operand1, 1) - - deepStrictEqual(result, expected) - }) - - test("`Matrix4.multiply` multiplies correctly", () => { - const operand1 = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - const operand2 = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - const expected = new Matrix4( - 90, 100, 110, 120, - 202, 228, 254, 280, - 314, 356, 398, 440, - 426, 484, 542, 600 - ) - const result = Matrix4.multiply(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix4.multiplyScalar` multiplies correctly", () => { - const operand1 = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - const expected = new Matrix4( - 2, 4, 6, 8, - 10, 12, 14, 16, - 18, 20, 22, 24, - 26, 28, 30, 32 - ) - const result = Matrix4.multiplyScalar(operand1, 2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix4.divide` divides correctly", () => { - const operand1 = new Matrix4( - 1, 0, 0, 1, - 0, 2, 1, 2, - 2, 1, 0, 1, - 2, 0, 1, 4 - ) - const operand2 = new Matrix4( - 1, 0, 0, 1, - 0, 2, 1, 2, - 2, 1, 0, 1, - 2, 0, 1, 4 - ) - - const expected = new Matrix4( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1 - ) - const result = Matrix4.divide(operand1, operand2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix4.divideScalar` divides correctly", () => { - const operand1 = new Matrix4( - 2, 4, 6, 8, - 10, 12, 14, 16, - 18, 20, 22, 24, - 26, 28, 30, 32 - ) - - const expected = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - const result = Matrix4.divideScalar(operand1, 2) - - deepStrictEqual(result, expected) - }) - - test("`Matrix4.invert` actually invert", () => { - const matrix = new Matrix4( - 1, 0, 0, 1, - 0, 2, 1, 2, - 2, 1, 0, 1, - 2, 0, 1, 4 - ) - const expected = new Matrix4( - -2, -0.5, 1, 0.5, - 1, 0.5, 0, -0.5, - -8, -1, 2, 2, - 3, 0.5, -1, -0.5 - ) - const destination = Matrix4.invert(matrix) - - deepStrictEqual(destination, expected) - }) - - test("`Matrix4.invert` with no solution returns zero", () => { - const matrix = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - const expected = new Matrix4( - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0 - ) - const destination = Matrix4.invert(matrix) - - deepStrictEqual(destination, expected) - }) - - test("`Matrix4.equal` tests equality", () => { - const matrix1 = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - const matrix2 = new Matrix4( - 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16 - ) - - strictEqual(Matrix4.equal(matrix1, matrix2), true) - }) - - test("`Matrix4.Identity` is identity", () => { - const expected = new Matrix4( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1 - ) - deepStrictEqual(Matrix4.Identity, expected) - }) - - test("`Matrix4.Zero` is zero", () => { - const expected = new Matrix4( - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0 - ) - deepStrictEqual(Matrix4.Zero, expected) - }) -}) diff --git a/packages/math/tests/vectors/vector2.test.js b/packages/math/tests/vectors/vector2.test.js deleted file mode 100644 index faefdd58..00000000 --- a/packages/math/tests/vectors/vector2.test.js +++ /dev/null @@ -1,230 +0,0 @@ -import { test, describe } from "vitest"; -import { Vector2 } from "../.."; -import { deepStrictEqual, strictEqual } from "node:assert"; - -describe("Testing `Vector2`", () => { - test("`Vector2` default constructor is zero", () => { - const vector = new Vector2() - const expected = new Vector2(0, 0) - - deepStrictEqual(vector, expected) - }) - - test("`Vector2.copy` copies correctly", () => { - const vector = new Vector2(1, 5) - const expected = Vector2.copy(vector) - - deepStrictEqual(vector, expected) - }) - - test("`Vector2.set` sets correctly", () => { - const vector = Vector2.set(1, 2) - const expected = new Vector2(1, 2) - - deepStrictEqual(vector, expected) - }) - - test("`Vector2.splat` splats correctly", () => { - const vector = Vector2.splat(1) - const expected = new Vector2(1, 1) - - deepStrictEqual(vector, expected) - }) - - test("`Vector2.normalize` returns a unit vector", () => { - const vector = new Vector2(1, 0) - const normalized = Vector2.normalize(vector) - - strictEqual(Vector2.magnitude(normalized), 1) - }) - - test("`Vector2.normalize` preserves direction", () => { - const vector = new Vector2(10, 0) - const expected = new Vector2(1, 0) - const actual = Vector2.normalize(vector) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.setMagnitude` sets length correctly", () => { - const vector = new Vector2(4, 5) - const actual = Vector2.setMagnitude(vector,10) - - deepStrictEqual(Vector2.magnitude(actual), 10) - }) - - test("`Vector2.setMagnitude` preserves direction", () => { - const vector = new Vector2(1, 0) - const expected = new Vector2(10, 0) - const actual = Vector2.setMagnitude(vector,10) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.magnitudeSquared` operates correctly", () => { - const operand = new Vector2(3, 4) - - deepStrictEqual(Vector2.magnitudeSquared(operand), 25) - }) - - test("`Vector2.magnitude` operates correctly", () => { - const operand = new Vector2(3, 4) - - deepStrictEqual(Vector2.magnitude(operand), 5) - }) - - test("`Vector2.distanceToSquared` operates correctly", () => { - const from = new Vector2(5, 3) - const to = new Vector2(10, 15) - - deepStrictEqual(Vector2.distanceToSquared(from, to), 169) - }) - - test("`Vector2.distanceTo` operates correctly", () => { - const from = new Vector2(5, 3) - const to = new Vector2(10, 15) - - deepStrictEqual(Vector2.distanceTo(from, to), 13) - }) - - test("`Vector2.add` operates correctly", () => { - const operand1 = new Vector2(1, 1) - const operand2 = new Vector2(1, 1) - const expected = new Vector2(2, 2) - const actual = Vector2.add(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.addScalar` operates correctly", () => { - const operand1 = new Vector2(1, 1) - const expected = new Vector2(2, 2) - const actual = Vector2.addScalar(operand1, 1) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.subtract` operates correctly", () => { - const operand1 = new Vector2(1, 1) - const operand2 = new Vector2(1, 1) - const expected = new Vector2(0, 0) - const actual = Vector2.subtract(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.subtractScalar` operates correctly", () => { - const operand1 = new Vector2(3, 5) - const expected = new Vector2(2, 4) - const actual = Vector2.subtractScalar(operand1, 1) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.multiply` operates correctly", () => { - const operand1 = new Vector2(2, 4) - const operand2 = new Vector2(2, 4) - const expected = new Vector2(4, 16) - const actual = Vector2.multiply(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.multiplyScalar` operates correctly", () => { - const operand1 = new Vector2(3, 5) - const expected = new Vector2(9, 15) - const actual = Vector2.multiplyScalar(operand1, 3) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.divide` operates correctly", () => { - const operand1 = new Vector2(4, 6) - const operand2 = new Vector2(2, 3) - const expected = new Vector2(2, 2) - const actual = Vector2.divide(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.divideScalar` operates correctly", () => { - const operand1 = new Vector2(6, 15) - const expected = new Vector2(2, 5) - const actual = Vector2.divideScalar(operand1, 3) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.reverse` operates correctly", () => { - const operand = new Vector2(4, 6) - const expected = new Vector2(-4, -6) - const actual = Vector2.reverse(operand) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.invert` operates correctly", () => { - const vector = new Vector2(0.5, 0.5) - const expected = new Vector2(2, 2) - const actual = Vector2.invert(vector) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.dot` operates correctly", () => { - const operand1 = new Vector2(1, 2) - const operand2 = new Vector2(3, 4) - - deepStrictEqual(Vector2.dot(operand1,operand2), 11) - }) - - test("`Vector2.cross` operates correctly", () => { - const operand1 = new Vector2(1, 2) - const operand2 = new Vector2(3, 4) - - deepStrictEqual(Vector2.cross(operand1,operand2), -2) - }) - - test("`Vector2.lerp` operates correctly", () => { - const operand1 = new Vector2(10, 10) - const operand2 = new Vector2(20, 20) - const expected = new Vector2(15, 15) - const actual = Vector2.lerp(operand1, operand2,0.5) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.reflect` operates correctly (case 1)", () => { - const incident = new Vector2(0, -1) - const normal = new Vector2(0, 1) - const expected = new Vector2(0, 1) - const actual = Vector2.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.reflect` operates correctly (case 2)", () => { - const incident = new Vector2(10,0) - const normal = new Vector2(0, 1) - const expected = new Vector2(10, 0) - const actual = Vector2.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.reflect` operates correctly (case 3)", () => { - const incident = new Vector2(0, 10) - const normal = new Vector2(0, 1) - const expected = new Vector2(0, -10) - const actual = Vector2.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector2.equal` checks equality correctly", () => { - const vector1 = new Vector2(2, 5) - const vector2 = new Vector2(2, 5) - - strictEqual(Vector2.equal(vector1, vector2), true) - }) -}) \ No newline at end of file diff --git a/packages/math/tests/vectors/vector3.test.js b/packages/math/tests/vectors/vector3.test.js deleted file mode 100644 index 231bcd99..00000000 --- a/packages/math/tests/vectors/vector3.test.js +++ /dev/null @@ -1,249 +0,0 @@ -import { test, describe } from "vitest"; -import { Vector3 } from "../.."; -import { deepStrictEqual, strictEqual } from "node:assert"; - -describe("Testing `Vector3`", () => { - test("`Vector3` default constructor is zero", () => { - const vector = new Vector3() - const expected = new Vector3(0, 0, 0) - - deepStrictEqual(vector, expected) - }) - - test("`Vector3.copy` copies correctly", () => { - const vector = new Vector3(1, 2, 3) - const expected = Vector3.copy(vector) - - deepStrictEqual(vector, expected) - }) - - test("`Vector3.set` sets correctly", () => { - const vector = Vector3.set(1, 2, 3) - const expected = new Vector3(1, 2, 3) - - deepStrictEqual(vector, expected) - }) - - test("`Vector3.splat` splats correctly", () => { - const vector = Vector3.splat(1) - const expected = new Vector3(1, 1, 1) - - deepStrictEqual(vector, expected) - }) - - test("`Vector3.normalize` returns a unit vector", () => { - const vector = new Vector3(1, 0, 0) - const normalized = Vector3.normalize(vector) - - strictEqual(Vector3.magnitude(normalized), 1) - }) - - test("`Vector3.normalize` preserves direction", () => { - const vector = new Vector3(10, 0, 0) - const expected = new Vector3(1, 0, 0) - const actual = Vector3.normalize(vector) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.setMagnitude` sets length correctly", () => { - const vector = new Vector3(4, 5, 0) - const actual = Vector3.setMagnitude(vector, 10) - - deepStrictEqual(Vector3.magnitude(actual), 10) - }) - - test("`Vector3.setMagnitude` preserves direction", () => { - const vector = new Vector3(1, 0, 0) - const expected = new Vector3(10, 0, 0) - const actual = Vector3.setMagnitude(vector, 10) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.magnitudeSquared` operates correctly", () => { - const operand = new Vector3(3, 4, 0) - - deepStrictEqual(Vector3.magnitudeSquared(operand), 25) - }) - - test("`Vector3.magnitude` operates correctly", () => { - const operand = new Vector3(3, 4, 0) - - deepStrictEqual(Vector3.magnitude(operand), 5) - }) - - test("`Vector3.distanceToSquared` operates correctly", () => { - const from = new Vector3(5, 3, 0) - const to = new Vector3(10, 15, 0) - - deepStrictEqual(Vector3.distanceToSquared(from, to), 169) - }) - - test("`Vector3.distanceTo` operates correctly", () => { - const from = new Vector3(5, 3, 0) - const to = new Vector3(10, 15, 0) - - deepStrictEqual(Vector3.distanceTo(from, to), 13) - }) - - test("`Vector3.add` operates correctly", () => { - const operand1 = new Vector3(1, 2, 3) - const operand2 = new Vector3(1, 2, 3) - const expected = new Vector3(2, 4, 6) - const actual = Vector3.add(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.addScalar` operates correctly", () => { - const operand1 = new Vector3(1, 2, 3) - const expected = new Vector3(2, 3, 4) - const actual = Vector3.addScalar(operand1, 1) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.subtract` operates correctly", () => { - const operand1 = new Vector3(1, 2, 3) - const operand2 = new Vector3(1, 2, 3) - const expected = new Vector3(0, 0, 0) - const actual = Vector3.subtract(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.subtractScalar` operates correctly", () => { - const operand1 = new Vector3(3, 5, 7) - const expected = new Vector3(2, 4, 6) - const actual = Vector3.subtractScalar(operand1, 1) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.multiply` operates correctly", () => { - const operand1 = new Vector3(2, 4, 6) - const operand2 = new Vector3(2, 4, 8) - const expected = new Vector3(4, 16, 48) - const actual = Vector3.multiply(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.multiplyScalar` operates correctly", () => { - const operand1 = new Vector3(3, 5, 7) - const expected = new Vector3(9, 15, 21) - const actual = Vector3.multiplyScalar(operand1, 3) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.divide` operates correctly", () => { - const operand1 = new Vector3(4, 6, 18) - const operand2 = new Vector3(2, 3, 9) - const expected = new Vector3(2, 2, 2) - const actual = Vector3.divide(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.divideScalar` operates correctly", () => { - const operand1 = new Vector3(6, 15, 24) - const expected = new Vector3(2, 5, 8) - const actual = Vector3.divideScalar(operand1, 3) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.reverse` operates correctly", () => { - const operand = new Vector3(4, 6, -8) - const expected = new Vector3(-4, -6, 8) - const actual = Vector3.reverse(operand) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.invert` operates correctly", () => { - const vector = new Vector3(0.5, 0.5, 0.25) - const expected = new Vector3(2, 2, 4) - const actual = Vector3.invert(vector) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.dot` operates correctly", () => { - const operand1 = new Vector3(1, 2) - const operand2 = new Vector3(3, 4) - - deepStrictEqual(Vector3.dot(operand1, operand2), 11) - }) - - test("`Vector3.cross` operates correctly", () => { - const operand1 = new Vector3(1, 2) - const operand2 = new Vector3(3, 4) - const expected = new Vector3(0, 0, -2) - - deepStrictEqual(Vector3.cross(operand1, operand2), expected) - }) - - test("`Vector3.lerp` operates correctly", () => { - const operand1 = new Vector3(10, 10, 10) - const operand2 = new Vector3(20, 20, 20) - const expected = new Vector3(15, 15, 15) - const actual = Vector3.lerp(operand1, operand2, 0.5) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.reflect` operates correctly (case 1)", () => { - const incident = new Vector3(0, -1, 0) - const normal = new Vector3(0, 1, 0) - const expected = new Vector3(0, 1, 0) - const actual = Vector3.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.reflect` operates correctly (case 2)", () => { - const incident = new Vector3(10, 0, 0) - const normal = new Vector3(0, 1, 0) - const expected = new Vector3(10, 0, 0) - const actual = Vector3.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.reflect` operates correctly (case 3)", () => { - const incident = new Vector3(0, 10, 0) - const normal = new Vector3(0, 1, 0) - const expected = new Vector3(0, -10, 0) - const actual = Vector3.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.reflect` operates correctly (case 4)", () => { - const incident = new Vector3(0, 10, 0) - const normal = new Vector3(0, 0, 1) - const expected = new Vector3(0, 10, 0) - const actual = Vector3.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.reflect` operates correctly (case 5)", () => { - const incident = new Vector3(10, 0, 0) - const normal = new Vector3(0, 0, -1) - const expected = new Vector3(10, 0, 0) - const actual = Vector3.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector3.equal` checks equality correctly", () => { - const vector1 = new Vector3(2, 5, 8) - const vector2 = new Vector3(2, 5, 8) - - strictEqual(Vector3.equal(vector1, vector2), true) - }) -}) \ No newline at end of file diff --git a/packages/math/tests/vectors/vector4.test.js b/packages/math/tests/vectors/vector4.test.js deleted file mode 100644 index 0585ac9b..00000000 --- a/packages/math/tests/vectors/vector4.test.js +++ /dev/null @@ -1,241 +0,0 @@ -import { test, describe } from "vitest"; -import { Vector4 } from "../.."; -import { deepStrictEqual, strictEqual } from "node:assert"; - -describe("Testing `Vector4`", () => { - test("`Vector4` default constructor is zero", () => { - const vector = new Vector4() - const expected = new Vector4(0, 0, 0, 0) - - deepStrictEqual(vector, expected) - }) - - test("`Vector4.copy` copies correctly", () => { - const vector = new Vector4(1, 2, 3, 4) - const expected = Vector4.copy(vector) - - deepStrictEqual(vector, expected) - }) - - test("`Vector4.set` sets correctly", () => { - const vector = Vector4.set(1, 2, 3, 4) - const expected = new Vector4(1, 2, 3, 4) - - deepStrictEqual(vector, expected) - }) - - test("`Vector4.splat` splats correctly", () => { - const vector = Vector4.splat(1) - const expected = new Vector4(1, 1, 1, 1) - - deepStrictEqual(vector, expected) - }) - - test("`Vector4.normalize` returns a unit vector", () => { - const vector = new Vector4(0, 0, 0, 1) - const normalized = Vector4.normalize(vector) - - strictEqual(Vector4.magnitude(normalized), 1) - }) - - test("`Vector4.normalize` preserves direction", () => { - const vector = new Vector4(0, 10, 0, 0) - const expected = new Vector4(0, 1, 0, 0) - const actual = Vector4.normalize(vector) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.setMagnitude` sets length correctly", () => { - const vector = new Vector4(3, 6, 7, 34) - const actual = Vector4.setMagnitude(vector, 10) - - deepStrictEqual(Vector4.magnitude(actual), 10) - }) - - test("`Vector4.setMagnitude` preserves direction", () => { - const vector = new Vector4(0, 1, 0, 0) - const expected = new Vector4(0, 10, 0, 0) - const actual = Vector4.setMagnitude(vector, 10) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.magnitudeSquared` operates correctly", () => { - const operand = new Vector4(3, 4, 0, 0) - - deepStrictEqual(Vector4.magnitudeSquared(operand), 25) - }) - - test("`Vector4.magnitude` operates correctly", () => { - const operand = new Vector4(3, 4, 0, 0) - - deepStrictEqual(Vector4.magnitude(operand), 5) - }) - - test("`Vector4.distanceToSquared` operates correctly", () => { - const from = new Vector4(5, 3, 0, 0) - const to = new Vector4(10, 15, 0, 0) - - deepStrictEqual(Vector4.distanceToSquared(from, to), 169) - }) - - test("`Vector4.distanceTo` operates correctly", () => { - const from = new Vector4(5, 3, 0, 0) - const to = new Vector4(10, 15, 0, 0) - - deepStrictEqual(Vector4.distanceTo(from, to), 13) - }) - - test("`Vector4.add` operates correctly", () => { - const operand1 = new Vector4(1, 2, 3, 4) - const operand2 = new Vector4(1, 2, 3, 4) - const expected = new Vector4(2, 4, 6, 8) - const actual = Vector4.add(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.addScalar` operates correctly", () => { - const operand1 = new Vector4(1, 2, 3, 5) - const expected = new Vector4(2, 3, 4, 6) - const actual = Vector4.addScalar(operand1, 1) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.subtract` operates correctly", () => { - const operand1 = new Vector4(1, 2, 3, 4) - const operand2 = new Vector4(1, 2, 3, 4) - const expected = new Vector4(0, 0, 0) - const actual = Vector4.subtract(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.subtractScalar` operates correctly", () => { - const operand1 = new Vector4(3, 5, 7, 8) - const expected = new Vector4(2, 4, 6, 7) - const actual = Vector4.subtractScalar(operand1, 1) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.multiply` operates correctly", () => { - const operand1 = new Vector4(2, 4, 6, 5) - const operand2 = new Vector4(2, 4, 8, 7) - const expected = new Vector4(4, 16, 48, 35) - const actual = Vector4.multiply(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.multiplyScalar` operates correctly", () => { - const operand1 = new Vector4(3, 5, 7, 10) - const expected = new Vector4(9, 15, 21, 30) - const actual = Vector4.multiplyScalar(operand1, 3) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.divide` operates correctly", () => { - const operand1 = new Vector4(4, 6, 18, 40) - const operand2 = new Vector4(2, 3, 9, 20) - const expected = new Vector4(2, 2, 2, 2) - const actual = Vector4.divide(operand1, operand2) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.divideScalar` operates correctly", () => { - const operand1 = new Vector4(6, 15, 24, 30) - const expected = new Vector4(2, 5, 8, 10) - const actual = Vector4.divideScalar(operand1, 3) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.reverse` operates correctly", () => { - const operand = new Vector4(4, 6, -8, 5) - const expected = new Vector4(-4, -6, 8, -5) - const actual = Vector4.reverse(operand) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.invert` operates correctly", () => { - const vector = new Vector4(0.5, 0.5, 0.25, -0.25) - const expected = new Vector4(2, 2, 4, -4) - const actual = Vector4.invert(vector) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.dot` operates correctly", () => { - const operand1 = new Vector4(1, 2, 9, 8) - const operand2 = new Vector4(3, 4, 7, 6) - - deepStrictEqual(Vector4.dot(operand1, operand2), 122) - }) - - test("`Vector4.lerp` operates correctly", () => { - const operand1 = new Vector4(10, 10, 10, 50) - const operand2 = new Vector4(20, 20, 20, 30) - const expected = new Vector4(15, 15, 15, 40) - const actual = Vector4.lerp(operand1, operand2, 0.5) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.reflect` operates correctly (case 1)", () => { - const incident = new Vector4(0, -1, 0, 0) - const normal = new Vector4(0, 1, 0, 0) - const expected = new Vector4(0, 1, 0, 0) - const actual = Vector4.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.reflect` operates correctly (case 2)", () => { - const incident = new Vector4(10, 0, 0, 0) - const normal = new Vector4(0, 1, 0, 0) - const expected = new Vector4(10, 0, 0, 0) - const actual = Vector4.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.reflect` operates correctly (case 3)", () => { - const incident = new Vector4(0, 10, 0, 0) - const normal = new Vector4(0, 1, 0, 0) - const expected = new Vector4(0, -10, 0, 0) - const actual = Vector4.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.reflect` operates correctly (case 4)", () => { - const incident = new Vector4(0, 10, 0, 0) - const normal = new Vector4(0, 0, 1, 0) - const expected = new Vector4(0, 10, 0, 0) - const actual = Vector4.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.reflect` operates correctly (case 5)", () => { - const incident = new Vector4(10, 0, 0, 0) - const normal = new Vector4(0, 0, -1, 0) - const expected = new Vector4(10, 0, 0, 0) - const actual = Vector4.reflect(incident, normal) - - deepStrictEqual(actual, expected) - }) - - test("`Vector4.equal` checks equality correctly", () => { - const vector1 = new Vector4(2, 5, 8, 3) - const vector2 = new Vector4(2, 5, 8, 3) - - strictEqual(Vector4.equal(vector1, vector2), true) - }) -}) \ No newline at end of file diff --git a/packages/movable/src/components/2d/acceleration.js b/packages/movable/src/components/2d/acceleration.js index 7b3b86cd..c1cf57fe 100644 --- a/packages/movable/src/components/2d/acceleration.js +++ b/packages/movable/src/components/2d/acceleration.js @@ -25,7 +25,7 @@ export class Acceleration2D extends Vector2 { } /** - * @param {import('@wimaengine/math').Vector2Serial} value + * @param {import('@wimaengine/math').Vector2Like} value * @param {Acceleration2D} [out] */ static deserialize(value, out = new Acceleration2D()) { diff --git a/packages/movable/src/components/2d/rotation.js b/packages/movable/src/components/2d/rotation.js index 8518e2f9..7d91ce4e 100644 --- a/packages/movable/src/components/2d/rotation.js +++ b/packages/movable/src/components/2d/rotation.js @@ -27,7 +27,7 @@ export class Rotation2D extends Angle { } /** - * @param {import('@wimaengine/math').AngleSerial} value + * @param {import('@wimaengine/math').AngleLike} value * @param {Rotation2D} [out] */ static deserialize(value, out = new Rotation2D()) { diff --git a/packages/movable/src/components/2d/torque.js b/packages/movable/src/components/2d/torque.js index 804d17a2..5626a612 100644 --- a/packages/movable/src/components/2d/torque.js +++ b/packages/movable/src/components/2d/torque.js @@ -27,7 +27,7 @@ export class Torque2D extends Angle { } /** - * @param {import('@wimaengine/math').AngleSerial} value + * @param {import('@wimaengine/math').AngleLike} value * @param {Torque2D} [out] */ static deserialize(value, out = new Torque2D()) { diff --git a/packages/movable/src/components/2d/velocity.js b/packages/movable/src/components/2d/velocity.js index 3cfb1c6c..22b6fa04 100644 --- a/packages/movable/src/components/2d/velocity.js +++ b/packages/movable/src/components/2d/velocity.js @@ -25,7 +25,7 @@ export class Velocity2D extends Vector2 { } /** - * @param {import('@wimaengine/math').Vector2Serial} value + * @param {import('@wimaengine/math').Vector2Like} value * @param {Velocity2D} [out] */ static deserialize(value, out = new Velocity2D()) { diff --git a/packages/movable/src/components/3d/acceleration.js b/packages/movable/src/components/3d/acceleration.js index 5a9ac31e..2626d01d 100644 --- a/packages/movable/src/components/3d/acceleration.js +++ b/packages/movable/src/components/3d/acceleration.js @@ -25,7 +25,7 @@ export class Acceleration3D extends Vector3 { } /** - * @param {import('@wimaengine/math').Vector3Serial} value + * @param {import('@wimaengine/math').Vector3Like} value * @param {Acceleration3D} [out] */ static deserialize(value, out = new Acceleration3D()) { diff --git a/packages/movable/src/components/3d/rotation.js b/packages/movable/src/components/3d/rotation.js index ffa913b5..9d6d009a 100644 --- a/packages/movable/src/components/3d/rotation.js +++ b/packages/movable/src/components/3d/rotation.js @@ -25,7 +25,7 @@ export class Rotation3D extends Vector3 { } /** - * @param {import('@wimaengine/math').Vector3Serial} value + * @param {import('@wimaengine/math').Vector3Like} value * @param {Rotation3D} [out] */ static deserialize(value, out = new Rotation3D()) { diff --git a/packages/movable/src/components/3d/torque.js b/packages/movable/src/components/3d/torque.js index 3a1d7597..feea7bbe 100644 --- a/packages/movable/src/components/3d/torque.js +++ b/packages/movable/src/components/3d/torque.js @@ -25,7 +25,7 @@ export class Torque3D extends Vector3 { } /** - * @param {import('@wimaengine/math').Vector3Serial} value + * @param {import('@wimaengine/math').Vector3Like} value * @param {Torque3D} [out] */ static deserialize(value, out = new Torque3D()) { diff --git a/packages/movable/src/components/3d/velocity.js b/packages/movable/src/components/3d/velocity.js index d1bfe317..35306e35 100644 --- a/packages/movable/src/components/3d/velocity.js +++ b/packages/movable/src/components/3d/velocity.js @@ -25,7 +25,7 @@ export class Velocity3D extends Vector3 { } /** - * @param {import('@wimaengine/math').Vector3Serial} value + * @param {import('@wimaengine/math').Vector3Like} value * @param {Velocity3D} [out] */ static deserialize(value, out = new Velocity3D()) { diff --git a/packages/narrowphase/src/components/collider.js b/packages/narrowphase/src/components/collider.js index 5a638874..6bb81706 100644 --- a/packages/narrowphase/src/components/collider.js +++ b/packages/narrowphase/src/components/collider.js @@ -315,7 +315,7 @@ export class Collider2D { * @typedef Collider2DSerial * @property {number} type * @property {number} angle - * @property {import('@wimaengine/math').Vector2Serial[]} vertices + * @property {import('@wimaengine/math').Vector2Like[]} vertices * @property {import('../core').GeometrySerial} geometry */ diff --git a/packages/narrowphase/src/core/geometry.js b/packages/narrowphase/src/core/geometry.js index 93bfb58e..d1ae6224 100644 --- a/packages/narrowphase/src/core/geometry.js +++ b/packages/narrowphase/src/core/geometry.js @@ -133,7 +133,7 @@ export class Geometry { /** * @typedef GeometrySerial - * @property {import('@wimaengine/math').Vector2Serial[]} vertices + * @property {import('@wimaengine/math').Vector2Like[]} vertices */ /** diff --git a/packages/render-core/src/assets/image.js b/packages/render-core/src/assets/image.js index 3914b7ed..7f56517a 100644 --- a/packages/render-core/src/assets/image.js +++ b/packages/render-core/src/assets/image.js @@ -70,5 +70,5 @@ export class Image { /** * @typedef ImageSerial * @property {number[]} raw - * @property {import('@wimaengine/math').Vector2Serial} dimensions + * @property {import('@wimaengine/math').Vector2Like} dimensions */ diff --git a/packages/transform/src/components/2d/globaltransform.js b/packages/transform/src/components/2d/globaltransform.js index 06d2731f..e60a78df 100644 --- a/packages/transform/src/components/2d/globaltransform.js +++ b/packages/transform/src/components/2d/globaltransform.js @@ -25,7 +25,7 @@ export class GlobalTransform2D extends Affine2 { } /** - * @param {import('@wimaengine/math').Affine2Serial} value + * @param {import('@wimaengine/math').Affine2Like} value * @param {GlobalTransform2D} [out] */ static deserialize(value, out = new GlobalTransform2D()) { diff --git a/packages/transform/src/components/2d/orientation.js b/packages/transform/src/components/2d/orientation.js index b2e4d3bf..aa4a2222 100644 --- a/packages/transform/src/components/2d/orientation.js +++ b/packages/transform/src/components/2d/orientation.js @@ -25,7 +25,7 @@ export class Orientation2D extends Rotary { } /** - * @param {import('@wimaengine/math').RotarySerial} value + * @param {import('@wimaengine/math').RotaryLike} value * @param {Orientation2D} [out] */ static deserialize(value, out = new Orientation2D()) { diff --git a/packages/transform/src/components/2d/position.js b/packages/transform/src/components/2d/position.js index 42998e85..25aa1058 100644 --- a/packages/transform/src/components/2d/position.js +++ b/packages/transform/src/components/2d/position.js @@ -25,7 +25,7 @@ export class Position2D extends Vector2 { } /** - * @param {import('@wimaengine/math').Vector2Serial} value + * @param {import('@wimaengine/math').Vector2Like} value * @param {Position2D} [out] */ static deserialize(value, out = new Position2D()) { diff --git a/packages/transform/src/components/2d/scale.js b/packages/transform/src/components/2d/scale.js index dce0bc73..99813283 100644 --- a/packages/transform/src/components/2d/scale.js +++ b/packages/transform/src/components/2d/scale.js @@ -28,7 +28,7 @@ export class Scale2D extends Vector2 { } /** - * @param {import('@wimaengine/math').Vector2Serial} value + * @param {import('@wimaengine/math').Vector2Like} value * @param {Scale2D} [out] */ static deserialize(value, out = new Scale2D()) { diff --git a/packages/transform/src/components/3d/globaltransform.js b/packages/transform/src/components/3d/globaltransform.js index 94d50a8a..797440e1 100644 --- a/packages/transform/src/components/3d/globaltransform.js +++ b/packages/transform/src/components/3d/globaltransform.js @@ -25,7 +25,7 @@ export class GlobalTransform3D extends Affine3 { } /** - * @param {import('@wimaengine/math').Affine3Serial} value + * @param {import('@wimaengine/math').Affine3Like} value * @param {GlobalTransform3D} [out] */ static deserialize(value, out = new GlobalTransform3D()) { diff --git a/packages/transform/src/components/3d/orientation.js b/packages/transform/src/components/3d/orientation.js index 446457f9..492ebb92 100644 --- a/packages/transform/src/components/3d/orientation.js +++ b/packages/transform/src/components/3d/orientation.js @@ -25,7 +25,7 @@ export class Orientation3D extends Quaternion { } /** - * @param {import('@wimaengine/math').QuaternionSerial} value + * @param {import('@wimaengine/math').QuaternionLike} value * @param {Orientation3D} [out] */ static deserialize(value, out = new Orientation3D()) { diff --git a/packages/transform/src/components/3d/position.js b/packages/transform/src/components/3d/position.js index aaf4d991..bda6cedf 100644 --- a/packages/transform/src/components/3d/position.js +++ b/packages/transform/src/components/3d/position.js @@ -25,7 +25,7 @@ export class Position3D extends Vector3 { } /** - * @param {import('@wimaengine/math').Vector3Serial} value + * @param {import('@wimaengine/math').Vector3Like} value * @param {Position3D} [out] */ static deserialize(value, out = new Position3D()) { diff --git a/packages/transform/src/components/3d/scale.js b/packages/transform/src/components/3d/scale.js index 4457172e..fd8621cd 100644 --- a/packages/transform/src/components/3d/scale.js +++ b/packages/transform/src/components/3d/scale.js @@ -29,7 +29,7 @@ export class Scale3D extends Vector3 { } /** - * @param {import('@wimaengine/math').Vector3Serial} value + * @param {import('@wimaengine/math').Vector3Like} value * @param {Scale3D} [out] */ static deserialize(value, out = new Scale3D()) {