@math.gl/core
Advanced tools
Comparing version 4.0.0 to 4.0.1
import { NumericArray } from '@math.gl/types'; | ||
import { ConfigurationOptions } from '../../lib/common'; | ||
import { ConfigurationOptions } from "../../lib/common.js"; | ||
/** Base class for vectors and matrices */ | ||
@@ -67,2 +67,1 @@ export declare abstract class MathArray extends Array<number> { | ||
} | ||
//# sourceMappingURL=math-array.d.ts.map |
@@ -1,269 +0,223 @@ | ||
function _extendableBuiltin(cls) { | ||
function ExtendableBuiltin() { | ||
var instance = Reflect.construct(cls, Array.from(arguments)); | ||
Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); | ||
return instance; | ||
} | ||
ExtendableBuiltin.prototype = Object.create(cls.prototype, { | ||
constructor: { | ||
value: cls, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (Object.setPrototypeOf) { | ||
Object.setPrototypeOf(ExtendableBuiltin, cls); | ||
} else { | ||
ExtendableBuiltin.__proto__ = cls; | ||
} | ||
return ExtendableBuiltin; | ||
} | ||
import { config, formatValue, equals, isArray } from "../../lib/common.js"; | ||
export class MathArray extends _extendableBuiltin(Array) { | ||
clone() { | ||
return new this.constructor().copy(this); | ||
} | ||
fromArray(array, offset = 0) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = array[i + offset]; | ||
/** Base class for vectors and matrices */ | ||
export class MathArray extends Array { | ||
// Common methods | ||
/** | ||
* Clone the current object | ||
* @returns a new copy of this object | ||
*/ | ||
clone() { | ||
// @ts-expect-error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. | ||
return new this.constructor().copy(this); // eslint-disable-line | ||
} | ||
return this.check(); | ||
} | ||
toArray(targetArray = [], offset = 0) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
targetArray[offset + i] = this[i]; | ||
fromArray(array, offset = 0) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = array[i + offset]; | ||
} | ||
return this.check(); | ||
} | ||
return targetArray; | ||
} | ||
toObject(targetObject) { | ||
return targetObject; | ||
} | ||
from(arrayOrObject) { | ||
return Array.isArray(arrayOrObject) ? this.copy(arrayOrObject) : this.fromObject(arrayOrObject); | ||
} | ||
to(arrayOrObject) { | ||
if (arrayOrObject === this) { | ||
return this; | ||
toArray(targetArray = [], offset = 0) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
targetArray[offset + i] = this[i]; | ||
} | ||
return targetArray; | ||
} | ||
return isArray(arrayOrObject) ? this.toArray(arrayOrObject) : this.toObject(arrayOrObject); | ||
} | ||
toTarget(target) { | ||
return target ? this.to(target) : this; | ||
} | ||
toFloat32Array() { | ||
return new Float32Array(this); | ||
} | ||
toString() { | ||
return this.formatString(config); | ||
} | ||
formatString(opts) { | ||
let string = ''; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
string += (i > 0 ? ', ' : '') + formatValue(this[i], opts); | ||
toObject(targetObject) { | ||
return targetObject; | ||
} | ||
return "".concat(opts.printTypes ? this.constructor.name : '', "[").concat(string, "]"); | ||
} | ||
equals(array) { | ||
if (!array || this.length !== array.length) { | ||
return false; | ||
from(arrayOrObject) { | ||
return Array.isArray(arrayOrObject) | ||
? this.copy(arrayOrObject) | ||
: // @ts-ignore | ||
this.fromObject(arrayOrObject); | ||
} | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
if (!equals(this[i], array[i])) { | ||
return false; | ||
} | ||
to(arrayOrObject) { | ||
// @ts-ignore | ||
if (arrayOrObject === this) { | ||
return this; | ||
} | ||
// @ts-expect-error TS2339: Property 'toObject' does not exist on type 'MathArray'. | ||
return isArray(arrayOrObject) ? this.toArray(arrayOrObject) : this.toObject(arrayOrObject); | ||
} | ||
return true; | ||
} | ||
exactEquals(array) { | ||
if (!array || this.length !== array.length) { | ||
return false; | ||
toTarget(target) { | ||
return target ? this.to(target) : this; | ||
} | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
if (this[i] !== array[i]) { | ||
return false; | ||
} | ||
/** @deprecated */ | ||
toFloat32Array() { | ||
return new Float32Array(this); | ||
} | ||
return true; | ||
} | ||
negate() { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = -this[i]; | ||
toString() { | ||
return this.formatString(config); | ||
} | ||
return this.check(); | ||
} | ||
lerp(a, b, t) { | ||
if (t === undefined) { | ||
return this.lerp(this, a, b); | ||
/** Formats string according to options */ | ||
formatString(opts) { | ||
let string = ''; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
string += (i > 0 ? ', ' : '') + formatValue(this[i], opts); | ||
} | ||
return `${opts.printTypes ? this.constructor.name : ''}[${string}]`; | ||
} | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
const ai = a[i]; | ||
const endValue = typeof b === 'number' ? b : b[i]; | ||
this[i] = ai + t * (endValue - ai); | ||
equals(array) { | ||
if (!array || this.length !== array.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
if (!equals(this[i], array[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return this.check(); | ||
} | ||
min(vector) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = Math.min(vector[i], this[i]); | ||
exactEquals(array) { | ||
if (!array || this.length !== array.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
if (this[i] !== array[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return this.check(); | ||
} | ||
max(vector) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = Math.max(vector[i], this[i]); | ||
// Modifiers | ||
/** Negates all values in this object */ | ||
negate() { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = -this[i]; | ||
} | ||
return this.check(); | ||
} | ||
return this.check(); | ||
} | ||
clamp(minVector, maxVector) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = Math.min(Math.max(this[i], minVector[i]), maxVector[i]); | ||
lerp(a, b, t) { | ||
if (t === undefined) { | ||
return this.lerp(this, a, b); | ||
} | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
const ai = a[i]; | ||
const endValue = typeof b === 'number' ? b : b[i]; | ||
this[i] = ai + t * (endValue - ai); | ||
} | ||
return this.check(); | ||
} | ||
return this.check(); | ||
} | ||
add(...vectors) { | ||
for (const vector of vectors) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] += vector[i]; | ||
} | ||
/** Minimal */ | ||
min(vector) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = Math.min(vector[i], this[i]); | ||
} | ||
return this.check(); | ||
} | ||
return this.check(); | ||
} | ||
subtract(...vectors) { | ||
for (const vector of vectors) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] -= vector[i]; | ||
} | ||
/** Maximal */ | ||
max(vector) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = Math.max(vector[i], this[i]); | ||
} | ||
return this.check(); | ||
} | ||
return this.check(); | ||
} | ||
scale(scale) { | ||
if (typeof scale === 'number') { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] *= scale; | ||
} | ||
} else { | ||
for (let i = 0; i < this.ELEMENTS && i < scale.length; ++i) { | ||
this[i] *= scale[i]; | ||
} | ||
clamp(minVector, maxVector) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = Math.min(Math.max(this[i], minVector[i]), maxVector[i]); | ||
} | ||
return this.check(); | ||
} | ||
return this.check(); | ||
} | ||
multiplyByScalar(scalar) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] *= scalar; | ||
add(...vectors) { | ||
for (const vector of vectors) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] += vector[i]; | ||
} | ||
} | ||
return this.check(); | ||
} | ||
return this.check(); | ||
} | ||
check() { | ||
if (config.debug && !this.validate()) { | ||
throw new Error("math.gl: ".concat(this.constructor.name, " some fields set to invalid numbers'")); | ||
subtract(...vectors) { | ||
for (const vector of vectors) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] -= vector[i]; | ||
} | ||
} | ||
return this.check(); | ||
} | ||
return this; | ||
} | ||
validate() { | ||
let valid = this.length === this.ELEMENTS; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
valid = valid && Number.isFinite(this[i]); | ||
scale(scale) { | ||
if (typeof scale === 'number') { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] *= scale; | ||
} | ||
} | ||
else { | ||
for (let i = 0; i < this.ELEMENTS && i < scale.length; ++i) { | ||
this[i] *= scale[i]; | ||
} | ||
} | ||
return this.check(); | ||
} | ||
return valid; | ||
} | ||
sub(a) { | ||
return this.subtract(a); | ||
} | ||
setScalar(a) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = a; | ||
/** | ||
* Multiplies all elements by `scale` | ||
* Note: `Matrix4.multiplyByScalar` only scales its 3x3 "minor" | ||
*/ | ||
multiplyByScalar(scalar) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] *= scalar; | ||
} | ||
return this.check(); | ||
} | ||
return this.check(); | ||
} | ||
addScalar(a) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] += a; | ||
// Debug checks | ||
/** Throws an error if array length is incorrect or contains illegal values */ | ||
check() { | ||
if (config.debug && !this.validate()) { | ||
throw new Error(`math.gl: ${this.constructor.name} some fields set to invalid numbers'`); | ||
} | ||
return this; | ||
} | ||
return this.check(); | ||
} | ||
subScalar(a) { | ||
return this.addScalar(-a); | ||
} | ||
multiplyScalar(scalar) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] *= scalar; | ||
/** Returns false if the array length is incorrect or contains illegal values */ | ||
validate() { | ||
let valid = this.length === this.ELEMENTS; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
valid = valid && Number.isFinite(this[i]); | ||
} | ||
return valid; | ||
} | ||
return this.check(); | ||
} | ||
divideScalar(a) { | ||
return this.multiplyByScalar(1 / a); | ||
} | ||
clampScalar(min, max) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = Math.min(Math.max(this[i], min), max); | ||
// three.js compatibility | ||
/** @deprecated */ | ||
sub(a) { | ||
return this.subtract(a); | ||
} | ||
return this.check(); | ||
} | ||
get elements() { | ||
return this; | ||
} | ||
/** @deprecated */ | ||
setScalar(a) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = a; | ||
} | ||
return this.check(); | ||
} | ||
/** @deprecated */ | ||
addScalar(a) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] += a; | ||
} | ||
return this.check(); | ||
} | ||
/** @deprecated */ | ||
subScalar(a) { | ||
return this.addScalar(-a); | ||
} | ||
/** @deprecated */ | ||
multiplyScalar(scalar) { | ||
// Multiplies all elements | ||
// `Matrix4.scale` only scales its 3x3 "minor" | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] *= scalar; | ||
} | ||
return this.check(); | ||
} | ||
/** @deprecated */ | ||
divideScalar(a) { | ||
return this.multiplyByScalar(1 / a); | ||
} | ||
/** @deprecated */ | ||
clampScalar(min, max) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] = Math.min(Math.max(this[i], min), max); | ||
} | ||
return this.check(); | ||
} | ||
/** @deprecated */ | ||
get elements() { | ||
return this; | ||
} | ||
} | ||
//# sourceMappingURL=math-array.js.map |
import { NumericArray } from '@math.gl/types'; | ||
import { MathArray } from './math-array'; | ||
import { MathArray } from "./math-array.js"; | ||
/** Base class for matrices */ | ||
@@ -14,2 +14,1 @@ export declare abstract class Matrix extends MathArray { | ||
} | ||
//# sourceMappingURL=matrix.d.ts.map |
import { MathArray } from "./math-array.js"; | ||
import { checkNumber } from "../../lib/validators.js"; | ||
import { config } from "../../lib/common.js"; | ||
/** Base class for matrices */ | ||
export class Matrix extends MathArray { | ||
toString() { | ||
let string = '['; | ||
if (config.printRowMajor) { | ||
string += 'row-major:'; | ||
for (let row = 0; row < this.RANK; ++row) { | ||
for (let col = 0; col < this.RANK; ++col) { | ||
string += " ".concat(this[col * this.RANK + row]); | ||
// fromObject(object) { | ||
// const array = object.elements; | ||
// return this.fromRowMajor(array); | ||
// } | ||
// toObject(object) { | ||
// const array = object.elements; | ||
// this.toRowMajor(array); | ||
// return object; | ||
// } | ||
// TODO better override formatString? | ||
toString() { | ||
let string = '['; | ||
if (config.printRowMajor) { | ||
string += 'row-major:'; | ||
for (let row = 0; row < this.RANK; ++row) { | ||
for (let col = 0; col < this.RANK; ++col) { | ||
string += ` ${this[col * this.RANK + row]}`; | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
string += 'column-major:'; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
string += " ".concat(this[i]); | ||
} | ||
else { | ||
string += 'column-major:'; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
string += ` ${this[i]}`; | ||
} | ||
} | ||
string += ']'; | ||
return string; | ||
} | ||
string += ']'; | ||
return string; | ||
} | ||
getElementIndex(row, col) { | ||
return col * this.RANK + row; | ||
} | ||
getElement(row, col) { | ||
return this[col * this.RANK + row]; | ||
} | ||
setElement(row, col, value) { | ||
this[col * this.RANK + row] = checkNumber(value); | ||
return this; | ||
} | ||
getColumn(columnIndex, result = new Array(this.RANK).fill(-0)) { | ||
const firstIndex = columnIndex * this.RANK; | ||
for (let i = 0; i < this.RANK; ++i) { | ||
result[i] = this[firstIndex + i]; | ||
getElementIndex(row, col) { | ||
return col * this.RANK + row; | ||
} | ||
return result; | ||
} | ||
setColumn(columnIndex, columnVector) { | ||
const firstIndex = columnIndex * this.RANK; | ||
for (let i = 0; i < this.RANK; ++i) { | ||
this[firstIndex + i] = columnVector[i]; | ||
// By default assumes row major indices | ||
getElement(row, col) { | ||
return this[col * this.RANK + row]; | ||
} | ||
return this; | ||
} | ||
// By default assumes row major indices | ||
setElement(row, col, value) { | ||
this[col * this.RANK + row] = checkNumber(value); | ||
return this; | ||
} | ||
getColumn(columnIndex, result = new Array(this.RANK).fill(-0)) { | ||
const firstIndex = columnIndex * this.RANK; | ||
for (let i = 0; i < this.RANK; ++i) { | ||
result[i] = this[firstIndex + i]; | ||
} | ||
return result; | ||
} | ||
setColumn(columnIndex, columnVector) { | ||
const firstIndex = columnIndex * this.RANK; | ||
for (let i = 0; i < this.RANK; ++i) { | ||
this[firstIndex + i] = columnVector[i]; | ||
} | ||
return this; | ||
} | ||
} | ||
//# sourceMappingURL=matrix.js.map |
import { NumericArray } from '@math.gl/types'; | ||
import { MathArray } from './math-array'; | ||
import { MathArray } from "./math-array.js"; | ||
/** Base class for vectors with at least 2 elements */ | ||
@@ -44,2 +44,1 @@ export declare abstract class Vector extends MathArray { | ||
} | ||
//# sourceMappingURL=vector.d.ts.map |
import { MathArray } from "./math-array.js"; | ||
import { checkNumber } from "../../lib/validators.js"; | ||
import { assert } from "../../lib/assert.js"; | ||
/** Base class for vectors with at least 2 elements */ | ||
export class Vector extends MathArray { | ||
get x() { | ||
return this[0]; | ||
} | ||
set x(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
get y() { | ||
return this[1]; | ||
} | ||
set y(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
len() { | ||
return Math.sqrt(this.lengthSquared()); | ||
} | ||
magnitude() { | ||
return this.len(); | ||
} | ||
lengthSquared() { | ||
let length = 0; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
length += this[i] * this[i]; | ||
// ACCESSORS | ||
get x() { | ||
return this[0]; | ||
} | ||
return length; | ||
} | ||
magnitudeSquared() { | ||
return this.lengthSquared(); | ||
} | ||
distance(mathArray) { | ||
return Math.sqrt(this.distanceSquared(mathArray)); | ||
} | ||
distanceSquared(mathArray) { | ||
let length = 0; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
const dist = this[i] - mathArray[i]; | ||
length += dist * dist; | ||
set x(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
return checkNumber(length); | ||
} | ||
dot(mathArray) { | ||
let product = 0; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
product += this[i] * mathArray[i]; | ||
get y() { | ||
return this[1]; | ||
} | ||
return checkNumber(product); | ||
} | ||
normalize() { | ||
const length = this.magnitude(); | ||
if (length !== 0) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] /= length; | ||
} | ||
set y(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
return this.check(); | ||
} | ||
multiply(...vectors) { | ||
for (const vector of vectors) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] *= vector[i]; | ||
} | ||
/** | ||
* Returns the length of the vector from the origin to the point described by this vector | ||
* | ||
* @note `length` is a reserved word for Arrays, so `v.length()` will return number of elements | ||
* Instead we provide `len` and `magnitude` | ||
*/ | ||
len() { | ||
return Math.sqrt(this.lengthSquared()); | ||
} | ||
return this.check(); | ||
} | ||
divide(...vectors) { | ||
for (const vector of vectors) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] /= vector[i]; | ||
} | ||
/** | ||
* Returns the length of the vector from the origin to the point described by this vector | ||
*/ | ||
magnitude() { | ||
return this.len(); | ||
} | ||
return this.check(); | ||
} | ||
lengthSq() { | ||
return this.lengthSquared(); | ||
} | ||
distanceTo(vector) { | ||
return this.distance(vector); | ||
} | ||
distanceToSquared(vector) { | ||
return this.distanceSquared(vector); | ||
} | ||
getComponent(i) { | ||
assert(i >= 0 && i < this.ELEMENTS, 'index is out of range'); | ||
return checkNumber(this[i]); | ||
} | ||
setComponent(i, value) { | ||
assert(i >= 0 && i < this.ELEMENTS, 'index is out of range'); | ||
this[i] = value; | ||
return this.check(); | ||
} | ||
addVectors(a, b) { | ||
return this.copy(a).add(b); | ||
} | ||
subVectors(a, b) { | ||
return this.copy(a).subtract(b); | ||
} | ||
multiplyVectors(a, b) { | ||
return this.copy(a).multiply(b); | ||
} | ||
addScaledVector(a, b) { | ||
return this.add(new this.constructor(a).multiplyScalar(b)); | ||
} | ||
/** | ||
* Returns the squared length of the vector from the origin to the point described by this vector | ||
*/ | ||
lengthSquared() { | ||
let length = 0; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
length += this[i] * this[i]; | ||
} | ||
return length; | ||
} | ||
/** | ||
* Returns the squared length of the vector from the origin to the point described by this vector | ||
*/ | ||
magnitudeSquared() { | ||
return this.lengthSquared(); | ||
} | ||
distance(mathArray) { | ||
return Math.sqrt(this.distanceSquared(mathArray)); | ||
} | ||
distanceSquared(mathArray) { | ||
let length = 0; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
const dist = this[i] - mathArray[i]; | ||
length += dist * dist; | ||
} | ||
return checkNumber(length); | ||
} | ||
dot(mathArray) { | ||
let product = 0; | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
product += this[i] * mathArray[i]; | ||
} | ||
return checkNumber(product); | ||
} | ||
// MODIFIERS | ||
normalize() { | ||
const length = this.magnitude(); | ||
if (length !== 0) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] /= length; | ||
} | ||
} | ||
return this.check(); | ||
} | ||
multiply(...vectors) { | ||
for (const vector of vectors) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] *= vector[i]; | ||
} | ||
} | ||
return this.check(); | ||
} | ||
divide(...vectors) { | ||
for (const vector of vectors) { | ||
for (let i = 0; i < this.ELEMENTS; ++i) { | ||
this[i] /= vector[i]; | ||
} | ||
} | ||
return this.check(); | ||
} | ||
// THREE.js compatibility | ||
lengthSq() { | ||
return this.lengthSquared(); | ||
} | ||
distanceTo(vector) { | ||
return this.distance(vector); | ||
} | ||
distanceToSquared(vector) { | ||
return this.distanceSquared(vector); | ||
} | ||
getComponent(i) { | ||
assert(i >= 0 && i < this.ELEMENTS, 'index is out of range'); | ||
return checkNumber(this[i]); | ||
} | ||
setComponent(i, value) { | ||
assert(i >= 0 && i < this.ELEMENTS, 'index is out of range'); | ||
this[i] = value; | ||
return this.check(); | ||
} | ||
addVectors(a, b) { | ||
return this.copy(a).add(b); | ||
} | ||
subVectors(a, b) { | ||
return this.copy(a).subtract(b); | ||
} | ||
multiplyVectors(a, b) { | ||
return this.copy(a).multiply(b); | ||
} | ||
addScaledVector(a, b) { | ||
// @ts-expect-error error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. | ||
return this.add(new this.constructor(a).multiplyScalar(b)); | ||
} | ||
} | ||
//# sourceMappingURL=vector.js.map |
@@ -1,3 +0,3 @@ | ||
import { MathArray } from './base/math-array'; | ||
import { Quaternion } from './quaternion'; | ||
import { MathArray } from "./base/math-array.js"; | ||
import { Quaternion } from "./quaternion.js"; | ||
import { NumericArray } from '@math.gl/types'; | ||
@@ -77,2 +77,1 @@ declare enum RotationOrder { | ||
export {}; | ||
//# sourceMappingURL=euler.d.ts.map |
@@ -0,1 +1,3 @@ | ||
// Copyright (c) 2017 Uber Technologies, Inc. | ||
// MIT License | ||
import { MathArray } from "./base/math-array.js"; | ||
@@ -5,554 +7,481 @@ import { Quaternion } from "./quaternion.js"; | ||
import { checkNumber } from "../lib/validators.js"; | ||
// Internal constants | ||
const ERR_UNKNOWN_ORDER = 'Unknown Euler angle order'; | ||
const ALMOST_ONE = 0.99999; | ||
var RotationOrder; | ||
(function (RotationOrder) { | ||
RotationOrder[RotationOrder["ZYX"] = 0] = "ZYX"; | ||
RotationOrder[RotationOrder["YXZ"] = 1] = "YXZ"; | ||
RotationOrder[RotationOrder["XZY"] = 2] = "XZY"; | ||
RotationOrder[RotationOrder["ZXY"] = 3] = "ZXY"; | ||
RotationOrder[RotationOrder["YZX"] = 4] = "YZX"; | ||
RotationOrder[RotationOrder["XYZ"] = 5] = "XYZ"; | ||
RotationOrder[RotationOrder["ZYX"] = 0] = "ZYX"; | ||
RotationOrder[RotationOrder["YXZ"] = 1] = "YXZ"; | ||
RotationOrder[RotationOrder["XZY"] = 2] = "XZY"; | ||
RotationOrder[RotationOrder["ZXY"] = 3] = "ZXY"; | ||
RotationOrder[RotationOrder["YZX"] = 4] = "YZX"; | ||
RotationOrder[RotationOrder["XYZ"] = 5] = "XYZ"; | ||
})(RotationOrder || (RotationOrder = {})); | ||
export class Euler extends MathArray { | ||
static get ZYX() { | ||
return RotationOrder.ZYX; | ||
} | ||
static get YXZ() { | ||
return RotationOrder.YXZ; | ||
} | ||
static get XZY() { | ||
return RotationOrder.XZY; | ||
} | ||
static get ZXY() { | ||
return RotationOrder.ZXY; | ||
} | ||
static get YZX() { | ||
return RotationOrder.YZX; | ||
} | ||
static get XYZ() { | ||
return RotationOrder.XYZ; | ||
} | ||
static get RollPitchYaw() { | ||
return RotationOrder.ZYX; | ||
} | ||
static get DefaultOrder() { | ||
return RotationOrder.ZYX; | ||
} | ||
static get RotationOrders() { | ||
return RotationOrder; | ||
} | ||
static rotationOrder(order) { | ||
return RotationOrder[order]; | ||
} | ||
get ELEMENTS() { | ||
return 4; | ||
} | ||
constructor(x = 0, y = 0, z = 0, order = Euler.DefaultOrder) { | ||
super(-0, -0, -0, -0); | ||
if (arguments.length > 0 && Array.isArray(arguments[0])) { | ||
this.fromVector3(...arguments); | ||
} else { | ||
this.set(x, y, z, order); | ||
// Constants | ||
static get ZYX() { | ||
return RotationOrder.ZYX; | ||
} | ||
} | ||
fromQuaternion(quaternion) { | ||
const [x, y, z, w] = quaternion; | ||
const ysqr = y * y; | ||
const t0 = -2 * (ysqr + z * z) + 1; | ||
const t1 = +2 * (x * y + w * z); | ||
let t2 = -2 * (x * z - w * y); | ||
const t3 = +2 * (y * z + w * x); | ||
const t4 = -2 * (x * x + ysqr) + 1; | ||
t2 = t2 > 1 ? 1 : t2; | ||
t2 = t2 < -1 ? -1 : t2; | ||
const roll = Math.atan2(t3, t4); | ||
const pitch = Math.asin(t2); | ||
const yaw = Math.atan2(t1, t0); | ||
return this.set(roll, pitch, yaw, Euler.RollPitchYaw); | ||
} | ||
fromObject(object) { | ||
throw new Error('not implemented'); | ||
} | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
this[3] = Number.isFinite(array[3]) || this.order; | ||
return this.check(); | ||
} | ||
set(x = 0, y = 0, z = 0, order) { | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
this[3] = Number.isFinite(order) ? order : this[3]; | ||
return this.check(); | ||
} | ||
validate() { | ||
return validateOrder(this[3]) && Number.isFinite(this[0]) && Number.isFinite(this[1]) && Number.isFinite(this[2]); | ||
} | ||
toArray(array = [], offset = 0) { | ||
array[offset] = this[0]; | ||
array[offset + 1] = this[1]; | ||
array[offset + 2] = this[2]; | ||
return array; | ||
} | ||
toArray4(array = [], offset = 0) { | ||
array[offset] = this[0]; | ||
array[offset + 1] = this[1]; | ||
array[offset + 2] = this[2]; | ||
array[offset + 3] = this[3]; | ||
return array; | ||
} | ||
toVector3(result = [-0, -0, -0]) { | ||
result[0] = this[0]; | ||
result[1] = this[1]; | ||
result[2] = this[2]; | ||
return result; | ||
} | ||
get x() { | ||
return this[0]; | ||
} | ||
set x(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
get y() { | ||
return this[1]; | ||
} | ||
set y(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
get z() { | ||
return this[2]; | ||
} | ||
set z(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
get alpha() { | ||
return this[0]; | ||
} | ||
set alpha(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
get beta() { | ||
return this[1]; | ||
} | ||
set beta(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
get gamma() { | ||
return this[2]; | ||
} | ||
set gamma(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
get phi() { | ||
return this[0]; | ||
} | ||
set phi(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
get theta() { | ||
return this[1]; | ||
} | ||
set theta(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
get psi() { | ||
return this[2]; | ||
} | ||
set psi(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
get roll() { | ||
return this[0]; | ||
} | ||
set roll(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
get pitch() { | ||
return this[1]; | ||
} | ||
set pitch(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
get yaw() { | ||
return this[2]; | ||
} | ||
set yaw(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
get order() { | ||
return this[3]; | ||
} | ||
set order(value) { | ||
this[3] = checkOrder(value); | ||
} | ||
fromVector3(v, order) { | ||
return this.set(v[0], v[1], v[2], Number.isFinite(order) ? order : this[3]); | ||
} | ||
fromArray(array, offset = 0) { | ||
this[0] = array[0 + offset]; | ||
this[1] = array[1 + offset]; | ||
this[2] = array[2 + offset]; | ||
if (array[3] !== undefined) { | ||
this[3] = array[3]; | ||
static get YXZ() { | ||
return RotationOrder.YXZ; | ||
} | ||
return this.check(); | ||
} | ||
fromRollPitchYaw(roll, pitch, yaw) { | ||
return this.set(roll, pitch, yaw, RotationOrder.ZYX); | ||
} | ||
fromRotationMatrix(m, order = Euler.DefaultOrder) { | ||
this._fromRotationMatrix(m, order); | ||
return this.check(); | ||
} | ||
getRotationMatrix(m) { | ||
return this._getRotationMatrix(m); | ||
} | ||
getQuaternion() { | ||
const q = new Quaternion(); | ||
switch (this[3]) { | ||
case RotationOrder.XYZ: | ||
return q.rotateX(this[0]).rotateY(this[1]).rotateZ(this[2]); | ||
case RotationOrder.YXZ: | ||
return q.rotateY(this[0]).rotateX(this[1]).rotateZ(this[2]); | ||
case RotationOrder.ZXY: | ||
return q.rotateZ(this[0]).rotateX(this[1]).rotateY(this[2]); | ||
case RotationOrder.ZYX: | ||
return q.rotateZ(this[0]).rotateY(this[1]).rotateX(this[2]); | ||
case RotationOrder.YZX: | ||
return q.rotateY(this[0]).rotateZ(this[1]).rotateX(this[2]); | ||
case RotationOrder.XZY: | ||
return q.rotateX(this[0]).rotateZ(this[1]).rotateY(this[2]); | ||
default: | ||
throw new Error(ERR_UNKNOWN_ORDER); | ||
static get XZY() { | ||
return RotationOrder.XZY; | ||
} | ||
} | ||
_fromRotationMatrix(m, order = Euler.DefaultOrder) { | ||
const m11 = m[0], | ||
m12 = m[4], | ||
m13 = m[8]; | ||
const m21 = m[1], | ||
m22 = m[5], | ||
m23 = m[9]; | ||
const m31 = m[2], | ||
m32 = m[6], | ||
m33 = m[10]; | ||
order = order || this[3]; | ||
switch (order) { | ||
case Euler.XYZ: | ||
this[1] = Math.asin(clamp(m13, -1, 1)); | ||
if (Math.abs(m13) < ALMOST_ONE) { | ||
this[0] = Math.atan2(-m23, m33); | ||
this[2] = Math.atan2(-m12, m11); | ||
} else { | ||
this[0] = Math.atan2(m32, m22); | ||
this[2] = 0; | ||
static get ZXY() { | ||
return RotationOrder.ZXY; | ||
} | ||
static get YZX() { | ||
return RotationOrder.YZX; | ||
} | ||
static get XYZ() { | ||
return RotationOrder.XYZ; | ||
} | ||
static get RollPitchYaw() { | ||
return RotationOrder.ZYX; | ||
} | ||
static get DefaultOrder() { | ||
return RotationOrder.ZYX; | ||
} | ||
static get RotationOrders() { | ||
return RotationOrder; | ||
} | ||
static rotationOrder(order) { | ||
return RotationOrder[order]; | ||
} | ||
get ELEMENTS() { | ||
return 4; | ||
} | ||
/** | ||
* @class | ||
* @param {Number | Number[]} x | ||
* @param {Number=} [y] | ||
* @param {Number=} [z] | ||
* @param {Number=} [order] | ||
*/ | ||
constructor(x = 0, y = 0, z = 0, order = Euler.DefaultOrder) { | ||
// PERF NOTE: initialize elements as double precision numbers | ||
super(-0, -0, -0, -0); | ||
// eslint-disable-next-line prefer-rest-params | ||
if (arguments.length > 0 && Array.isArray(arguments[0])) { | ||
// eslint-disable-next-line prefer-rest-params | ||
// @ts-expect-error | ||
this.fromVector3(...arguments); | ||
} | ||
break; | ||
case Euler.YXZ: | ||
this[0] = Math.asin(-clamp(m23, -1, 1)); | ||
if (Math.abs(m23) < ALMOST_ONE) { | ||
this[1] = Math.atan2(m13, m33); | ||
this[2] = Math.atan2(m21, m22); | ||
} else { | ||
this[1] = Math.atan2(-m31, m11); | ||
this[2] = 0; | ||
else { | ||
this.set(x, y, z, order); | ||
} | ||
break; | ||
case Euler.ZXY: | ||
this[0] = Math.asin(clamp(m32, -1, 1)); | ||
if (Math.abs(m32) < ALMOST_ONE) { | ||
this[1] = Math.atan2(-m31, m33); | ||
this[2] = Math.atan2(-m12, m22); | ||
} else { | ||
this[1] = 0; | ||
this[2] = Math.atan2(m21, m11); | ||
} | ||
fromQuaternion(quaternion) { | ||
const [x, y, z, w] = quaternion; | ||
const ysqr = y * y; | ||
const t0 = -2 * (ysqr + z * z) + 1; | ||
const t1 = +2 * (x * y + w * z); | ||
let t2 = -2 * (x * z - w * y); | ||
const t3 = +2 * (y * z + w * x); | ||
const t4 = -2 * (x * x + ysqr) + 1; | ||
t2 = t2 > 1 ? 1 : t2; | ||
t2 = t2 < -1 ? -1 : t2; | ||
const roll = Math.atan2(t3, t4); | ||
const pitch = Math.asin(t2); | ||
const yaw = Math.atan2(t1, t0); | ||
return this.set(roll, pitch, yaw, Euler.RollPitchYaw); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
fromObject(object) { | ||
throw new Error('not implemented'); | ||
// return this.set(object.x, object.y, object.z, object.order); | ||
} | ||
// fromQuaternion(q, order) { | ||
// this._fromRotationMat[-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0]; | ||
// return this.check(); | ||
// } | ||
// If copied array does contain fourth element, preserves currently set order | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
// @ts-expect-error | ||
this[3] = Number.isFinite(array[3]) || this.order; | ||
return this.check(); | ||
} | ||
// Sets the three angles, and optionally sets the rotation order | ||
// If order is not specified, preserves currently set order | ||
set(x = 0, y = 0, z = 0, order) { | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
this[3] = Number.isFinite(order) ? order : this[3]; | ||
return this.check(); | ||
} | ||
validate() { | ||
return (validateOrder(this[3]) && | ||
Number.isFinite(this[0]) && | ||
Number.isFinite(this[1]) && | ||
Number.isFinite(this[2])); | ||
} | ||
// Does not copy the orientation element | ||
toArray(array = [], offset = 0) { | ||
array[offset] = this[0]; | ||
array[offset + 1] = this[1]; | ||
array[offset + 2] = this[2]; | ||
return array; | ||
} | ||
// Copies the orientation element | ||
toArray4(array = [], offset = 0) { | ||
array[offset] = this[0]; | ||
array[offset + 1] = this[1]; | ||
array[offset + 2] = this[2]; | ||
array[offset + 3] = this[3]; | ||
return array; | ||
} | ||
toVector3(result = [-0, -0, -0]) { | ||
result[0] = this[0]; | ||
result[1] = this[1]; | ||
result[2] = this[2]; | ||
return result; | ||
} | ||
/* eslint-disable no-multi-spaces, brace-style, no-return-assign */ | ||
// x, y, z angle notation (note: only corresponds to axis in XYZ orientation) | ||
get x() { | ||
return this[0]; | ||
} | ||
set x(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
get y() { | ||
return this[1]; | ||
} | ||
set y(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
get z() { | ||
return this[2]; | ||
} | ||
set z(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
// alpha, beta, gamma angle notation | ||
get alpha() { | ||
return this[0]; | ||
} | ||
set alpha(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
get beta() { | ||
return this[1]; | ||
} | ||
set beta(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
get gamma() { | ||
return this[2]; | ||
} | ||
set gamma(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
// phi, theta, psi angle notation | ||
get phi() { | ||
return this[0]; | ||
} | ||
set phi(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
get theta() { | ||
return this[1]; | ||
} | ||
set theta(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
get psi() { | ||
return this[2]; | ||
} | ||
set psi(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
// roll, pitch, yaw angle notation | ||
get roll() { | ||
return this[0]; | ||
} | ||
set roll(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
get pitch() { | ||
return this[1]; | ||
} | ||
set pitch(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
get yaw() { | ||
return this[2]; | ||
} | ||
set yaw(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
// rotation order, in all three angle notations | ||
get order() { | ||
return this[3]; | ||
} | ||
set order(value) { | ||
this[3] = checkOrder(value); | ||
} | ||
// Constructors | ||
fromVector3(v, order) { | ||
return this.set(v[0], v[1], v[2], Number.isFinite(order) ? order : this[3]); | ||
} | ||
// TODO - with and without 4th element | ||
fromArray(array, offset = 0) { | ||
this[0] = array[0 + offset]; | ||
this[1] = array[1 + offset]; | ||
this[2] = array[2 + offset]; | ||
if (array[3] !== undefined) { | ||
this[3] = array[3]; | ||
} | ||
break; | ||
case Euler.ZYX: | ||
this[1] = Math.asin(-clamp(m31, -1, 1)); | ||
if (Math.abs(m31) < ALMOST_ONE) { | ||
this[0] = Math.atan2(m32, m33); | ||
this[2] = Math.atan2(m21, m11); | ||
} else { | ||
this[0] = 0; | ||
this[2] = Math.atan2(-m12, m22); | ||
return this.check(); | ||
} | ||
// Common ZYX rotation order | ||
fromRollPitchYaw(roll, pitch, yaw) { | ||
return this.set(roll, pitch, yaw, RotationOrder.ZYX); | ||
} | ||
fromRotationMatrix(m, order = Euler.DefaultOrder) { | ||
this._fromRotationMatrix(m, order); | ||
return this.check(); | ||
} | ||
// ACCESSORS | ||
getRotationMatrix(m) { | ||
return this._getRotationMatrix(m); | ||
} | ||
// TODO - move to Quaternion | ||
getQuaternion() { | ||
const q = new Quaternion(); | ||
switch (this[3]) { | ||
case RotationOrder.XYZ: | ||
return q.rotateX(this[0]).rotateY(this[1]).rotateZ(this[2]); | ||
case RotationOrder.YXZ: | ||
return q.rotateY(this[0]).rotateX(this[1]).rotateZ(this[2]); | ||
case RotationOrder.ZXY: | ||
return q.rotateZ(this[0]).rotateX(this[1]).rotateY(this[2]); | ||
case RotationOrder.ZYX: | ||
return q.rotateZ(this[0]).rotateY(this[1]).rotateX(this[2]); | ||
case RotationOrder.YZX: | ||
return q.rotateY(this[0]).rotateZ(this[1]).rotateX(this[2]); | ||
case RotationOrder.XZY: | ||
return q.rotateX(this[0]).rotateZ(this[1]).rotateY(this[2]); | ||
default: | ||
throw new Error(ERR_UNKNOWN_ORDER); | ||
} | ||
break; | ||
case Euler.YZX: | ||
this[2] = Math.asin(clamp(m21, -1, 1)); | ||
if (Math.abs(m21) < ALMOST_ONE) { | ||
this[0] = Math.atan2(-m23, m22); | ||
this[1] = Math.atan2(-m31, m11); | ||
} else { | ||
this[0] = 0; | ||
this[1] = Math.atan2(m13, m33); | ||
} | ||
// INTERNAL METHODS | ||
// Conversion from Euler to rotation matrix and from matrix to Euler | ||
// Adapted from three.js under MIT license | ||
// // WARNING: this discards revolution information -bhouston | ||
// reorder(newOrder) { | ||
// const q = new Quaternion().setFromEuler(this); | ||
// return this.setFromQuaternion(q, newOrder); | ||
/* eslint-disable complexity, max-statements, one-var */ | ||
_fromRotationMatrix(m, order = Euler.DefaultOrder) { | ||
// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) | ||
const m11 = m[0], m12 = m[4], m13 = m[8]; | ||
const m21 = m[1], m22 = m[5], m23 = m[9]; | ||
const m31 = m[2], m32 = m[6], m33 = m[10]; | ||
order = order || this[3]; | ||
switch (order) { | ||
case Euler.XYZ: | ||
this[1] = Math.asin(clamp(m13, -1, 1)); | ||
if (Math.abs(m13) < ALMOST_ONE) { | ||
this[0] = Math.atan2(-m23, m33); | ||
this[2] = Math.atan2(-m12, m11); | ||
} | ||
else { | ||
this[0] = Math.atan2(m32, m22); | ||
this[2] = 0; | ||
} | ||
break; | ||
case Euler.YXZ: | ||
this[0] = Math.asin(-clamp(m23, -1, 1)); | ||
if (Math.abs(m23) < ALMOST_ONE) { | ||
this[1] = Math.atan2(m13, m33); | ||
this[2] = Math.atan2(m21, m22); | ||
} | ||
else { | ||
this[1] = Math.atan2(-m31, m11); | ||
this[2] = 0; | ||
} | ||
break; | ||
case Euler.ZXY: | ||
this[0] = Math.asin(clamp(m32, -1, 1)); | ||
if (Math.abs(m32) < ALMOST_ONE) { | ||
this[1] = Math.atan2(-m31, m33); | ||
this[2] = Math.atan2(-m12, m22); | ||
} | ||
else { | ||
this[1] = 0; | ||
this[2] = Math.atan2(m21, m11); | ||
} | ||
break; | ||
case Euler.ZYX: | ||
this[1] = Math.asin(-clamp(m31, -1, 1)); | ||
if (Math.abs(m31) < ALMOST_ONE) { | ||
this[0] = Math.atan2(m32, m33); | ||
this[2] = Math.atan2(m21, m11); | ||
} | ||
else { | ||
this[0] = 0; | ||
this[2] = Math.atan2(-m12, m22); | ||
} | ||
break; | ||
case Euler.YZX: | ||
this[2] = Math.asin(clamp(m21, -1, 1)); | ||
if (Math.abs(m21) < ALMOST_ONE) { | ||
this[0] = Math.atan2(-m23, m22); | ||
this[1] = Math.atan2(-m31, m11); | ||
} | ||
else { | ||
this[0] = 0; | ||
this[1] = Math.atan2(m13, m33); | ||
} | ||
break; | ||
case Euler.XZY: | ||
this[2] = Math.asin(-clamp(m12, -1, 1)); | ||
if (Math.abs(m12) < ALMOST_ONE) { | ||
this[0] = Math.atan2(m32, m22); | ||
this[1] = Math.atan2(m13, m11); | ||
} | ||
else { | ||
this[0] = Math.atan2(-m23, m33); | ||
this[1] = 0; | ||
} | ||
break; | ||
default: | ||
throw new Error(ERR_UNKNOWN_ORDER); | ||
} | ||
break; | ||
case Euler.XZY: | ||
this[2] = Math.asin(-clamp(m12, -1, 1)); | ||
if (Math.abs(m12) < ALMOST_ONE) { | ||
this[0] = Math.atan2(m32, m22); | ||
this[1] = Math.atan2(m13, m11); | ||
} else { | ||
this[0] = Math.atan2(-m23, m33); | ||
this[1] = 0; | ||
} | ||
break; | ||
default: | ||
throw new Error(ERR_UNKNOWN_ORDER); | ||
this[3] = order; | ||
return this; | ||
} | ||
this[3] = order; | ||
return this; | ||
} | ||
_getRotationMatrix(result) { | ||
const te = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0]; | ||
const x = this.x, | ||
y = this.y, | ||
z = this.z; | ||
const a = Math.cos(x); | ||
const c = Math.cos(y); | ||
const e = Math.cos(z); | ||
const b = Math.sin(x); | ||
const d = Math.sin(y); | ||
const f = Math.sin(z); | ||
switch (this[3]) { | ||
case Euler.XYZ: | ||
{ | ||
const ae = a * e, | ||
af = a * f, | ||
be = b * e, | ||
bf = b * f; | ||
te[0] = c * e; | ||
te[4] = -c * f; | ||
te[8] = d; | ||
te[1] = af + be * d; | ||
te[5] = ae - bf * d; | ||
te[9] = -b * c; | ||
te[2] = bf - ae * d; | ||
te[6] = be + af * d; | ||
te[10] = a * c; | ||
break; | ||
_getRotationMatrix(result) { | ||
const te = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0]; | ||
const x = this.x, y = this.y, z = this.z; | ||
const a = Math.cos(x); | ||
const c = Math.cos(y); | ||
const e = Math.cos(z); | ||
const b = Math.sin(x); | ||
const d = Math.sin(y); | ||
const f = Math.sin(z); | ||
switch (this[3]) { | ||
case Euler.XYZ: { | ||
const ae = a * e, af = a * f, be = b * e, bf = b * f; | ||
te[0] = c * e; | ||
te[4] = -c * f; | ||
te[8] = d; | ||
te[1] = af + be * d; | ||
te[5] = ae - bf * d; | ||
te[9] = -b * c; | ||
te[2] = bf - ae * d; | ||
te[6] = be + af * d; | ||
te[10] = a * c; | ||
break; | ||
} | ||
case Euler.YXZ: { | ||
const ce = c * e, cf = c * f, de = d * e, df = d * f; | ||
te[0] = ce + df * b; | ||
te[4] = de * b - cf; | ||
te[8] = a * d; | ||
te[1] = a * f; | ||
te[5] = a * e; | ||
te[9] = -b; | ||
te[2] = cf * b - de; | ||
te[6] = df + ce * b; | ||
te[10] = a * c; | ||
break; | ||
} | ||
case Euler.ZXY: { | ||
const ce = c * e, cf = c * f, de = d * e, df = d * f; | ||
te[0] = ce - df * b; | ||
te[4] = -a * f; | ||
te[8] = de + cf * b; | ||
te[1] = cf + de * b; | ||
te[5] = a * e; | ||
te[9] = df - ce * b; | ||
te[2] = -a * d; | ||
te[6] = b; | ||
te[10] = a * c; | ||
break; | ||
} | ||
case Euler.ZYX: { | ||
const ae = a * e, af = a * f, be = b * e, bf = b * f; | ||
te[0] = c * e; | ||
te[4] = be * d - af; | ||
te[8] = ae * d + bf; | ||
te[1] = c * f; | ||
te[5] = bf * d + ae; | ||
te[9] = af * d - be; | ||
te[2] = -d; | ||
te[6] = b * c; | ||
te[10] = a * c; | ||
break; | ||
} | ||
case Euler.YZX: { | ||
const ac = a * c, ad = a * d, bc = b * c, bd = b * d; | ||
te[0] = c * e; | ||
te[4] = bd - ac * f; | ||
te[8] = bc * f + ad; | ||
te[1] = f; | ||
te[5] = a * e; | ||
te[9] = -b * e; | ||
te[2] = -d * e; | ||
te[6] = ad * f + bc; | ||
te[10] = ac - bd * f; | ||
break; | ||
} | ||
case Euler.XZY: { | ||
const ac = a * c, ad = a * d, bc = b * c, bd = b * d; | ||
te[0] = c * e; | ||
te[4] = -f; | ||
te[8] = d * e; | ||
te[1] = ac * f + bd; | ||
te[5] = a * e; | ||
te[9] = ad * f - bc; | ||
te[2] = bc * f - ad; | ||
te[6] = b * e; | ||
te[10] = bd * f + ac; | ||
break; | ||
} | ||
default: | ||
throw new Error(ERR_UNKNOWN_ORDER); | ||
} | ||
case Euler.YXZ: | ||
{ | ||
const ce = c * e, | ||
cf = c * f, | ||
de = d * e, | ||
df = d * f; | ||
te[0] = ce + df * b; | ||
te[4] = de * b - cf; | ||
te[8] = a * d; | ||
te[1] = a * f; | ||
te[5] = a * e; | ||
te[9] = -b; | ||
te[2] = cf * b - de; | ||
te[6] = df + ce * b; | ||
te[10] = a * c; | ||
break; | ||
} | ||
case Euler.ZXY: | ||
{ | ||
const ce = c * e, | ||
cf = c * f, | ||
de = d * e, | ||
df = d * f; | ||
te[0] = ce - df * b; | ||
te[4] = -a * f; | ||
te[8] = de + cf * b; | ||
te[1] = cf + de * b; | ||
te[5] = a * e; | ||
te[9] = df - ce * b; | ||
te[2] = -a * d; | ||
te[6] = b; | ||
te[10] = a * c; | ||
break; | ||
} | ||
case Euler.ZYX: | ||
{ | ||
const ae = a * e, | ||
af = a * f, | ||
be = b * e, | ||
bf = b * f; | ||
te[0] = c * e; | ||
te[4] = be * d - af; | ||
te[8] = ae * d + bf; | ||
te[1] = c * f; | ||
te[5] = bf * d + ae; | ||
te[9] = af * d - be; | ||
te[2] = -d; | ||
te[6] = b * c; | ||
te[10] = a * c; | ||
break; | ||
} | ||
case Euler.YZX: | ||
{ | ||
const ac = a * c, | ||
ad = a * d, | ||
bc = b * c, | ||
bd = b * d; | ||
te[0] = c * e; | ||
te[4] = bd - ac * f; | ||
te[8] = bc * f + ad; | ||
te[1] = f; | ||
te[5] = a * e; | ||
te[9] = -b * e; | ||
te[2] = -d * e; | ||
te[6] = ad * f + bc; | ||
te[10] = ac - bd * f; | ||
break; | ||
} | ||
case Euler.XZY: | ||
{ | ||
const ac = a * c, | ||
ad = a * d, | ||
bc = b * c, | ||
bd = b * d; | ||
te[0] = c * e; | ||
te[4] = -f; | ||
te[8] = d * e; | ||
te[1] = ac * f + bd; | ||
te[5] = a * e; | ||
te[9] = ad * f - bc; | ||
te[2] = bc * f - ad; | ||
te[6] = b * e; | ||
te[10] = bd * f + ac; | ||
break; | ||
} | ||
default: | ||
throw new Error(ERR_UNKNOWN_ORDER); | ||
// last column | ||
te[3] = 0; | ||
te[7] = 0; | ||
te[11] = 0; | ||
// bottom row | ||
te[12] = 0; | ||
te[13] = 0; | ||
te[14] = 0; | ||
te[15] = 1; | ||
return te; | ||
} | ||
te[3] = 0; | ||
te[7] = 0; | ||
te[11] = 0; | ||
te[12] = 0; | ||
te[13] = 0; | ||
te[14] = 0; | ||
te[15] = 1; | ||
return te; | ||
} | ||
toQuaternion() { | ||
const cy = Math.cos(this.yaw * 0.5); | ||
const sy = Math.sin(this.yaw * 0.5); | ||
const cr = Math.cos(this.roll * 0.5); | ||
const sr = Math.sin(this.roll * 0.5); | ||
const cp = Math.cos(this.pitch * 0.5); | ||
const sp = Math.sin(this.pitch * 0.5); | ||
const w = cy * cr * cp + sy * sr * sp; | ||
const x = cy * sr * cp - sy * cr * sp; | ||
const y = cy * cr * sp + sy * sr * cp; | ||
const z = sy * cr * cp - cy * sr * sp; | ||
return new Quaternion(x, y, z, w); | ||
} | ||
toQuaternion() { | ||
// Abbreviations for the various angular functions | ||
const cy = Math.cos(this.yaw * 0.5); | ||
const sy = Math.sin(this.yaw * 0.5); | ||
const cr = Math.cos(this.roll * 0.5); | ||
const sr = Math.sin(this.roll * 0.5); | ||
const cp = Math.cos(this.pitch * 0.5); | ||
const sp = Math.sin(this.pitch * 0.5); | ||
const w = cy * cr * cp + sy * sr * sp; | ||
const x = cy * sr * cp - sy * cr * sp; | ||
const y = cy * cr * sp + sy * sr * cp; | ||
const z = sy * cr * cp - cy * sr * sp; | ||
return new Quaternion(x, y, z, w); | ||
} | ||
} | ||
// HELPER FUNCTIONS | ||
function validateOrder(value) { | ||
return value >= 0 && value < 6; | ||
return value >= 0 && value < 6; | ||
} | ||
function checkOrder(value) { | ||
if (value < 0 && value >= 6) { | ||
throw new Error(ERR_UNKNOWN_ORDER); | ||
} | ||
return value; | ||
if (value < 0 && value >= 6) { | ||
throw new Error(ERR_UNKNOWN_ORDER); | ||
} | ||
return value; | ||
} | ||
//# sourceMappingURL=euler.js.map |
import { NumericArray } from '@math.gl/types'; | ||
import { Matrix } from './base/matrix'; | ||
import { Matrix } from "./base/matrix.js"; | ||
declare enum INDICES { | ||
@@ -51,3 +51,3 @@ COL0ROW0 = 0, | ||
multiplyRight(a: NumericArray): this; | ||
rotate(radians: number): NumericArray; | ||
rotate(radians: number): this; | ||
scale(factor: NumericArray | number): this; | ||
@@ -64,2 +64,1 @@ translate(vec: NumericArray): this; | ||
export {}; | ||
//# sourceMappingURL=matrix3.d.ts.map |
@@ -0,1 +1,3 @@ | ||
// Copyright (c) 2017 Uber Technologies, Inc. | ||
// MIT License | ||
import { Matrix } from "./base/matrix.js"; | ||
@@ -8,202 +10,195 @@ import { checkVector } from "../lib/validators.js"; | ||
var INDICES; | ||
(function (INDICES) { | ||
INDICES[INDICES["COL0ROW0"] = 0] = "COL0ROW0"; | ||
INDICES[INDICES["COL0ROW1"] = 1] = "COL0ROW1"; | ||
INDICES[INDICES["COL0ROW2"] = 2] = "COL0ROW2"; | ||
INDICES[INDICES["COL1ROW0"] = 3] = "COL1ROW0"; | ||
INDICES[INDICES["COL1ROW1"] = 4] = "COL1ROW1"; | ||
INDICES[INDICES["COL1ROW2"] = 5] = "COL1ROW2"; | ||
INDICES[INDICES["COL2ROW0"] = 6] = "COL2ROW0"; | ||
INDICES[INDICES["COL2ROW1"] = 7] = "COL2ROW1"; | ||
INDICES[INDICES["COL2ROW2"] = 8] = "COL2ROW2"; | ||
INDICES[INDICES["COL0ROW0"] = 0] = "COL0ROW0"; | ||
INDICES[INDICES["COL0ROW1"] = 1] = "COL0ROW1"; | ||
INDICES[INDICES["COL0ROW2"] = 2] = "COL0ROW2"; | ||
INDICES[INDICES["COL1ROW0"] = 3] = "COL1ROW0"; | ||
INDICES[INDICES["COL1ROW1"] = 4] = "COL1ROW1"; | ||
INDICES[INDICES["COL1ROW2"] = 5] = "COL1ROW2"; | ||
INDICES[INDICES["COL2ROW0"] = 6] = "COL2ROW0"; | ||
INDICES[INDICES["COL2ROW1"] = 7] = "COL2ROW1"; | ||
INDICES[INDICES["COL2ROW2"] = 8] = "COL2ROW2"; | ||
})(INDICES || (INDICES = {})); | ||
const IDENTITY_MATRIX = Object.freeze([1, 0, 0, 0, 1, 0, 0, 0, 1]); | ||
export class Matrix3 extends Matrix { | ||
static get IDENTITY() { | ||
return getIdentityMatrix(); | ||
} | ||
static get ZERO() { | ||
return getZeroMatrix(); | ||
} | ||
get ELEMENTS() { | ||
return 9; | ||
} | ||
get RANK() { | ||
return 3; | ||
} | ||
get INDICES() { | ||
return INDICES; | ||
} | ||
constructor(array, ...args) { | ||
super(-0, -0, -0, -0, -0, -0, -0, -0, -0); | ||
if (arguments.length === 1 && Array.isArray(array)) { | ||
this.copy(array); | ||
} else if (args.length > 0) { | ||
this.copy([array, ...args]); | ||
} else { | ||
this.identity(); | ||
static get IDENTITY() { | ||
return getIdentityMatrix(); | ||
} | ||
} | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
this[3] = array[3]; | ||
this[4] = array[4]; | ||
this[5] = array[5]; | ||
this[6] = array[6]; | ||
this[7] = array[7]; | ||
this[8] = array[8]; | ||
return this.check(); | ||
} | ||
identity() { | ||
return this.copy(IDENTITY_MATRIX); | ||
} | ||
fromObject(object) { | ||
return this.check(); | ||
} | ||
fromQuaternion(q) { | ||
mat3_fromQuat(this, q); | ||
return this.check(); | ||
} | ||
set(m00, m10, m20, m01, m11, m21, m02, m12, m22) { | ||
this[0] = m00; | ||
this[1] = m10; | ||
this[2] = m20; | ||
this[3] = m01; | ||
this[4] = m11; | ||
this[5] = m21; | ||
this[6] = m02; | ||
this[7] = m12; | ||
this[8] = m22; | ||
return this.check(); | ||
} | ||
setRowMajor(m00, m01, m02, m10, m11, m12, m20, m21, m22) { | ||
this[0] = m00; | ||
this[1] = m10; | ||
this[2] = m20; | ||
this[3] = m01; | ||
this[4] = m11; | ||
this[5] = m21; | ||
this[6] = m02; | ||
this[7] = m12; | ||
this[8] = m22; | ||
return this.check(); | ||
} | ||
determinant() { | ||
return mat3_determinant(this); | ||
} | ||
transpose() { | ||
mat3_transpose(this, this); | ||
return this.check(); | ||
} | ||
invert() { | ||
mat3_invert(this, this); | ||
return this.check(); | ||
} | ||
multiplyLeft(a) { | ||
mat3_multiply(this, a, this); | ||
return this.check(); | ||
} | ||
multiplyRight(a) { | ||
mat3_multiply(this, this, a); | ||
return this.check(); | ||
} | ||
rotate(radians) { | ||
mat3_rotate(this, this, radians); | ||
return this.check(); | ||
} | ||
scale(factor) { | ||
if (Array.isArray(factor)) { | ||
mat3_scale(this, this, factor); | ||
} else { | ||
mat3_scale(this, this, [factor, factor]); | ||
static get ZERO() { | ||
return getZeroMatrix(); | ||
} | ||
return this.check(); | ||
} | ||
translate(vec) { | ||
mat3_translate(this, this, vec); | ||
return this.check(); | ||
} | ||
transform(vector, result) { | ||
let out; | ||
switch (vector.length) { | ||
case 2: | ||
out = vec2_transformMat3(result || [-0, -0], vector, this); | ||
break; | ||
case 3: | ||
out = vec3_transformMat3(result || [-0, -0, -0], vector, this); | ||
break; | ||
case 4: | ||
out = vec4_transformMat3(result || [-0, -0, -0, -0], vector, this); | ||
break; | ||
default: | ||
throw new Error('Illegal vector'); | ||
get ELEMENTS() { | ||
return 9; | ||
} | ||
checkVector(out, vector.length); | ||
return out; | ||
} | ||
transformVector(vector, result) { | ||
return this.transform(vector, result); | ||
} | ||
transformVector2(vector, result) { | ||
return this.transform(vector, result); | ||
} | ||
transformVector3(vector, result) { | ||
return this.transform(vector, result); | ||
} | ||
get RANK() { | ||
return 3; | ||
} | ||
get INDICES() { | ||
return INDICES; | ||
} | ||
constructor(array, ...args) { | ||
// PERF NOTE: initialize elements as double precision numbers | ||
super(-0, -0, -0, -0, -0, -0, -0, -0, -0); | ||
if (arguments.length === 1 && Array.isArray(array)) { | ||
this.copy(array); | ||
} | ||
else if (args.length > 0) { | ||
this.copy([array, ...args]); | ||
} | ||
else { | ||
this.identity(); | ||
} | ||
} | ||
copy(array) { | ||
// Element wise copy for performance | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
this[3] = array[3]; | ||
this[4] = array[4]; | ||
this[5] = array[5]; | ||
this[6] = array[6]; | ||
this[7] = array[7]; | ||
this[8] = array[8]; | ||
return this.check(); | ||
} | ||
// Constructors | ||
identity() { | ||
return this.copy(IDENTITY_MATRIX); | ||
} | ||
/** | ||
* | ||
* @param object | ||
* @returns self | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
fromObject(object) { | ||
return this.check(); | ||
} | ||
/** Calculates a 3x3 matrix from the given quaternion | ||
* q quat Quaternion to create matrix from | ||
*/ | ||
fromQuaternion(q) { | ||
mat3_fromQuat(this, q); | ||
return this.check(); | ||
} | ||
/** | ||
* accepts column major order, stores in column major order | ||
*/ | ||
// eslint-disable-next-line max-params | ||
set(m00, m10, m20, m01, m11, m21, m02, m12, m22) { | ||
this[0] = m00; | ||
this[1] = m10; | ||
this[2] = m20; | ||
this[3] = m01; | ||
this[4] = m11; | ||
this[5] = m21; | ||
this[6] = m02; | ||
this[7] = m12; | ||
this[8] = m22; | ||
return this.check(); | ||
} | ||
/** | ||
* accepts row major order, stores as column major | ||
*/ | ||
// eslint-disable-next-line max-params | ||
setRowMajor(m00, m01, m02, m10, m11, m12, m20, m21, m22) { | ||
this[0] = m00; | ||
this[1] = m10; | ||
this[2] = m20; | ||
this[3] = m01; | ||
this[4] = m11; | ||
this[5] = m21; | ||
this[6] = m02; | ||
this[7] = m12; | ||
this[8] = m22; | ||
return this.check(); | ||
} | ||
// Accessors | ||
determinant() { | ||
return mat3_determinant(this); | ||
} | ||
// Modifiers | ||
transpose() { | ||
mat3_transpose(this, this); | ||
return this.check(); | ||
} | ||
/** Invert a matrix. Note that this can fail if the matrix is not invertible */ | ||
invert() { | ||
mat3_invert(this, this); | ||
return this.check(); | ||
} | ||
// Operations | ||
multiplyLeft(a) { | ||
mat3_multiply(this, a, this); | ||
return this.check(); | ||
} | ||
multiplyRight(a) { | ||
mat3_multiply(this, this, a); | ||
return this.check(); | ||
} | ||
rotate(radians) { | ||
mat3_rotate(this, this, radians); | ||
return this.check(); | ||
} | ||
scale(factor) { | ||
if (Array.isArray(factor)) { | ||
mat3_scale(this, this, factor); | ||
} | ||
else { | ||
mat3_scale(this, this, [factor, factor]); | ||
} | ||
return this.check(); | ||
} | ||
translate(vec) { | ||
mat3_translate(this, this, vec); | ||
return this.check(); | ||
} | ||
// Transforms | ||
transform(vector, result) { | ||
let out; | ||
switch (vector.length) { | ||
case 2: | ||
out = vec2_transformMat3(result || [-0, -0], vector, this); | ||
break; | ||
case 3: | ||
out = vec3_transformMat3(result || [-0, -0, -0], vector, this); | ||
break; | ||
case 4: | ||
out = vec4_transformMat3(result || [-0, -0, -0, -0], vector, this); | ||
break; | ||
default: | ||
throw new Error('Illegal vector'); | ||
} | ||
checkVector(out, vector.length); | ||
return out; | ||
} | ||
/** @deprecated */ | ||
transformVector(vector, result) { | ||
return this.transform(vector, result); | ||
} | ||
/** @deprecated */ | ||
transformVector2(vector, result) { | ||
return this.transform(vector, result); | ||
} | ||
/** @deprecated */ | ||
transformVector3(vector, result) { | ||
return this.transform(vector, result); | ||
} | ||
} | ||
let ZERO_MATRIX3; | ||
let IDENTITY_MATRIX3 = null; | ||
function getZeroMatrix() { | ||
if (!ZERO_MATRIX3) { | ||
ZERO_MATRIX3 = new Matrix3([0, 0, 0, 0, 0, 0, 0, 0, 0]); | ||
Object.freeze(ZERO_MATRIX3); | ||
} | ||
return ZERO_MATRIX3; | ||
if (!ZERO_MATRIX3) { | ||
ZERO_MATRIX3 = new Matrix3([0, 0, 0, 0, 0, 0, 0, 0, 0]); | ||
Object.freeze(ZERO_MATRIX3); | ||
} | ||
return ZERO_MATRIX3; | ||
} | ||
function getIdentityMatrix() { | ||
if (!IDENTITY_MATRIX3) { | ||
IDENTITY_MATRIX3 = new Matrix3(); | ||
Object.freeze(IDENTITY_MATRIX3); | ||
} | ||
return IDENTITY_MATRIX3; | ||
if (!IDENTITY_MATRIX3) { | ||
IDENTITY_MATRIX3 = new Matrix3(); | ||
Object.freeze(IDENTITY_MATRIX3); | ||
} | ||
return IDENTITY_MATRIX3; | ||
} | ||
//# sourceMappingURL=matrix3.js.map |
import { NumericArray } from '@math.gl/types'; | ||
import { Matrix } from './base/matrix'; | ||
import { Matrix } from "./base/matrix.js"; | ||
declare enum INDICES { | ||
@@ -226,2 +226,1 @@ COL0ROW0 = 0, | ||
export {}; | ||
//# sourceMappingURL=matrix4.d.ts.map |
@@ -0,4 +1,8 @@ | ||
// Copyright (c) 2017 Uber Technologies, Inc. | ||
// MIT License | ||
import { Matrix } from "./base/matrix.js"; | ||
import { checkVector } from "../lib/validators.js"; | ||
/* eslint-disable camelcase */ | ||
import { vec2_transformMat4AsVector, vec3_transformMat4AsVector } from "../lib/gl-matrix-extras.js"; | ||
// @ts-ignore gl-matrix types... | ||
import { fromQuat as mat4_fromQuat, frustum as mat4_frustum, lookAt as mat4_lookAt, ortho as mat4_ortho, perspective as mat4_perspective, determinant as mat4_determinant, transpose as mat4_transpose, invert as mat4_invert, multiply as mat4_multiply, rotateX as mat4_rotateX, rotateY as mat4_rotateY, rotateZ as mat4_rotateZ, rotate as mat4_rotate, scale as mat4_scale, translate as mat4_translate } from "../gl-matrix/mat4.js"; | ||
@@ -9,23 +13,21 @@ import { transformMat4 as vec2_transformMat4 } from "../gl-matrix/vec2.js"; | ||
var INDICES; | ||
(function (INDICES) { | ||
INDICES[INDICES["COL0ROW0"] = 0] = "COL0ROW0"; | ||
INDICES[INDICES["COL0ROW1"] = 1] = "COL0ROW1"; | ||
INDICES[INDICES["COL0ROW2"] = 2] = "COL0ROW2"; | ||
INDICES[INDICES["COL0ROW3"] = 3] = "COL0ROW3"; | ||
INDICES[INDICES["COL1ROW0"] = 4] = "COL1ROW0"; | ||
INDICES[INDICES["COL1ROW1"] = 5] = "COL1ROW1"; | ||
INDICES[INDICES["COL1ROW2"] = 6] = "COL1ROW2"; | ||
INDICES[INDICES["COL1ROW3"] = 7] = "COL1ROW3"; | ||
INDICES[INDICES["COL2ROW0"] = 8] = "COL2ROW0"; | ||
INDICES[INDICES["COL2ROW1"] = 9] = "COL2ROW1"; | ||
INDICES[INDICES["COL2ROW2"] = 10] = "COL2ROW2"; | ||
INDICES[INDICES["COL2ROW3"] = 11] = "COL2ROW3"; | ||
INDICES[INDICES["COL3ROW0"] = 12] = "COL3ROW0"; | ||
INDICES[INDICES["COL3ROW1"] = 13] = "COL3ROW1"; | ||
INDICES[INDICES["COL3ROW2"] = 14] = "COL3ROW2"; | ||
INDICES[INDICES["COL3ROW3"] = 15] = "COL3ROW3"; | ||
INDICES[INDICES["COL0ROW0"] = 0] = "COL0ROW0"; | ||
INDICES[INDICES["COL0ROW1"] = 1] = "COL0ROW1"; | ||
INDICES[INDICES["COL0ROW2"] = 2] = "COL0ROW2"; | ||
INDICES[INDICES["COL0ROW3"] = 3] = "COL0ROW3"; | ||
INDICES[INDICES["COL1ROW0"] = 4] = "COL1ROW0"; | ||
INDICES[INDICES["COL1ROW1"] = 5] = "COL1ROW1"; | ||
INDICES[INDICES["COL1ROW2"] = 6] = "COL1ROW2"; | ||
INDICES[INDICES["COL1ROW3"] = 7] = "COL1ROW3"; | ||
INDICES[INDICES["COL2ROW0"] = 8] = "COL2ROW0"; | ||
INDICES[INDICES["COL2ROW1"] = 9] = "COL2ROW1"; | ||
INDICES[INDICES["COL2ROW2"] = 10] = "COL2ROW2"; | ||
INDICES[INDICES["COL2ROW3"] = 11] = "COL2ROW3"; | ||
INDICES[INDICES["COL3ROW0"] = 12] = "COL3ROW0"; | ||
INDICES[INDICES["COL3ROW1"] = 13] = "COL3ROW1"; | ||
INDICES[INDICES["COL3ROW2"] = 14] = "COL3ROW2"; | ||
INDICES[INDICES["COL3ROW3"] = 15] = "COL3ROW3"; | ||
})(INDICES || (INDICES = {})); | ||
const DEFAULT_FOVY = 45 * Math.PI / 180; | ||
const DEFAULT_FOVY = (45 * Math.PI) / 180; | ||
const DEFAULT_ASPECT = 1; | ||
@@ -35,446 +37,508 @@ const DEFAULT_NEAR = 0.1; | ||
const IDENTITY_MATRIX = Object.freeze([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]); | ||
/** 4x4 matrix */ | ||
export class Matrix4 extends Matrix { | ||
static get IDENTITY() { | ||
return getIdentityMatrix(); | ||
} | ||
static get ZERO() { | ||
return getZeroMatrix(); | ||
} | ||
get ELEMENTS() { | ||
return 16; | ||
} | ||
get RANK() { | ||
return 4; | ||
} | ||
get INDICES() { | ||
return INDICES; | ||
} | ||
constructor(array) { | ||
super(-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0); | ||
if (arguments.length === 1 && Array.isArray(array)) { | ||
this.copy(array); | ||
} else { | ||
this.identity(); | ||
static get IDENTITY() { | ||
return getIdentityMatrix(); | ||
} | ||
} | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
this[3] = array[3]; | ||
this[4] = array[4]; | ||
this[5] = array[5]; | ||
this[6] = array[6]; | ||
this[7] = array[7]; | ||
this[8] = array[8]; | ||
this[9] = array[9]; | ||
this[10] = array[10]; | ||
this[11] = array[11]; | ||
this[12] = array[12]; | ||
this[13] = array[13]; | ||
this[14] = array[14]; | ||
this[15] = array[15]; | ||
return this.check(); | ||
} | ||
set(m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33) { | ||
this[0] = m00; | ||
this[1] = m10; | ||
this[2] = m20; | ||
this[3] = m30; | ||
this[4] = m01; | ||
this[5] = m11; | ||
this[6] = m21; | ||
this[7] = m31; | ||
this[8] = m02; | ||
this[9] = m12; | ||
this[10] = m22; | ||
this[11] = m32; | ||
this[12] = m03; | ||
this[13] = m13; | ||
this[14] = m23; | ||
this[15] = m33; | ||
return this.check(); | ||
} | ||
setRowMajor(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { | ||
this[0] = m00; | ||
this[1] = m10; | ||
this[2] = m20; | ||
this[3] = m30; | ||
this[4] = m01; | ||
this[5] = m11; | ||
this[6] = m21; | ||
this[7] = m31; | ||
this[8] = m02; | ||
this[9] = m12; | ||
this[10] = m22; | ||
this[11] = m32; | ||
this[12] = m03; | ||
this[13] = m13; | ||
this[14] = m23; | ||
this[15] = m33; | ||
return this.check(); | ||
} | ||
toRowMajor(result) { | ||
result[0] = this[0]; | ||
result[1] = this[4]; | ||
result[2] = this[8]; | ||
result[3] = this[12]; | ||
result[4] = this[1]; | ||
result[5] = this[5]; | ||
result[6] = this[9]; | ||
result[7] = this[13]; | ||
result[8] = this[2]; | ||
result[9] = this[6]; | ||
result[10] = this[10]; | ||
result[11] = this[14]; | ||
result[12] = this[3]; | ||
result[13] = this[7]; | ||
result[14] = this[11]; | ||
result[15] = this[15]; | ||
return result; | ||
} | ||
identity() { | ||
return this.copy(IDENTITY_MATRIX); | ||
} | ||
fromObject(object) { | ||
return this.check(); | ||
} | ||
fromQuaternion(quaternion) { | ||
mat4_fromQuat(this, quaternion); | ||
return this.check(); | ||
} | ||
frustum(view) { | ||
const { | ||
left, | ||
right, | ||
bottom, | ||
top, | ||
near = DEFAULT_NEAR, | ||
far = DEFAULT_FAR | ||
} = view; | ||
if (far === Infinity) { | ||
computeInfinitePerspectiveOffCenter(this, left, right, bottom, top, near); | ||
} else { | ||
mat4_frustum(this, left, right, bottom, top, near, far); | ||
static get ZERO() { | ||
return getZeroMatrix(); | ||
} | ||
return this.check(); | ||
} | ||
lookAt(view) { | ||
const { | ||
eye, | ||
center = [0, 0, 0], | ||
up = [0, 1, 0] | ||
} = view; | ||
mat4_lookAt(this, eye, center, up); | ||
return this.check(); | ||
} | ||
ortho(view) { | ||
const { | ||
left, | ||
right, | ||
bottom, | ||
top, | ||
near = DEFAULT_NEAR, | ||
far = DEFAULT_FAR | ||
} = view; | ||
mat4_ortho(this, left, right, bottom, top, near, far); | ||
return this.check(); | ||
} | ||
orthographic(view) { | ||
const { | ||
fovy = DEFAULT_FOVY, | ||
aspect = DEFAULT_ASPECT, | ||
focalDistance = 1, | ||
near = DEFAULT_NEAR, | ||
far = DEFAULT_FAR | ||
} = view; | ||
checkRadians(fovy); | ||
const halfY = fovy / 2; | ||
const top = focalDistance * Math.tan(halfY); | ||
const right = top * aspect; | ||
return this.ortho({ | ||
left: -right, | ||
right, | ||
bottom: -top, | ||
top, | ||
near, | ||
far | ||
}); | ||
} | ||
perspective(view) { | ||
const { | ||
fovy = 45 * Math.PI / 180, | ||
aspect = 1, | ||
near = 0.1, | ||
far = 500 | ||
} = view; | ||
checkRadians(fovy); | ||
mat4_perspective(this, fovy, aspect, near, far); | ||
return this.check(); | ||
} | ||
determinant() { | ||
return mat4_determinant(this); | ||
} | ||
getScale(result = [-0, -0, -0]) { | ||
result[0] = Math.sqrt(this[0] * this[0] + this[1] * this[1] + this[2] * this[2]); | ||
result[1] = Math.sqrt(this[4] * this[4] + this[5] * this[5] + this[6] * this[6]); | ||
result[2] = Math.sqrt(this[8] * this[8] + this[9] * this[9] + this[10] * this[10]); | ||
return result; | ||
} | ||
getTranslation(result = [-0, -0, -0]) { | ||
result[0] = this[12]; | ||
result[1] = this[13]; | ||
result[2] = this[14]; | ||
return result; | ||
} | ||
getRotation(result, scaleResult) { | ||
result = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0]; | ||
scaleResult = scaleResult || [-0, -0, -0]; | ||
const scale = this.getScale(scaleResult); | ||
const inverseScale0 = 1 / scale[0]; | ||
const inverseScale1 = 1 / scale[1]; | ||
const inverseScale2 = 1 / scale[2]; | ||
result[0] = this[0] * inverseScale0; | ||
result[1] = this[1] * inverseScale1; | ||
result[2] = this[2] * inverseScale2; | ||
result[3] = 0; | ||
result[4] = this[4] * inverseScale0; | ||
result[5] = this[5] * inverseScale1; | ||
result[6] = this[6] * inverseScale2; | ||
result[7] = 0; | ||
result[8] = this[8] * inverseScale0; | ||
result[9] = this[9] * inverseScale1; | ||
result[10] = this[10] * inverseScale2; | ||
result[11] = 0; | ||
result[12] = 0; | ||
result[13] = 0; | ||
result[14] = 0; | ||
result[15] = 1; | ||
return result; | ||
} | ||
getRotationMatrix3(result, scaleResult) { | ||
result = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0]; | ||
scaleResult = scaleResult || [-0, -0, -0]; | ||
const scale = this.getScale(scaleResult); | ||
const inverseScale0 = 1 / scale[0]; | ||
const inverseScale1 = 1 / scale[1]; | ||
const inverseScale2 = 1 / scale[2]; | ||
result[0] = this[0] * inverseScale0; | ||
result[1] = this[1] * inverseScale1; | ||
result[2] = this[2] * inverseScale2; | ||
result[3] = this[4] * inverseScale0; | ||
result[4] = this[5] * inverseScale1; | ||
result[5] = this[6] * inverseScale2; | ||
result[6] = this[8] * inverseScale0; | ||
result[7] = this[9] * inverseScale1; | ||
result[8] = this[10] * inverseScale2; | ||
return result; | ||
} | ||
transpose() { | ||
mat4_transpose(this, this); | ||
return this.check(); | ||
} | ||
invert() { | ||
mat4_invert(this, this); | ||
return this.check(); | ||
} | ||
multiplyLeft(a) { | ||
mat4_multiply(this, a, this); | ||
return this.check(); | ||
} | ||
multiplyRight(a) { | ||
mat4_multiply(this, this, a); | ||
return this.check(); | ||
} | ||
rotateX(radians) { | ||
mat4_rotateX(this, this, radians); | ||
return this.check(); | ||
} | ||
rotateY(radians) { | ||
mat4_rotateY(this, this, radians); | ||
return this.check(); | ||
} | ||
rotateZ(radians) { | ||
mat4_rotateZ(this, this, radians); | ||
return this.check(); | ||
} | ||
rotateXYZ(angleXYZ) { | ||
return this.rotateX(angleXYZ[0]).rotateY(angleXYZ[1]).rotateZ(angleXYZ[2]); | ||
} | ||
rotateAxis(radians, axis) { | ||
mat4_rotate(this, this, radians, axis); | ||
return this.check(); | ||
} | ||
scale(factor) { | ||
mat4_scale(this, this, Array.isArray(factor) ? factor : [factor, factor, factor]); | ||
return this.check(); | ||
} | ||
translate(vector) { | ||
mat4_translate(this, this, vector); | ||
return this.check(); | ||
} | ||
transform(vector, result) { | ||
if (vector.length === 4) { | ||
result = vec4_transformMat4(result || [-0, -0, -0, -0], vector, this); | ||
checkVector(result, 4); | ||
return result; | ||
get ELEMENTS() { | ||
return 16; | ||
} | ||
return this.transformAsPoint(vector, result); | ||
} | ||
transformAsPoint(vector, result) { | ||
const { | ||
length | ||
} = vector; | ||
let out; | ||
switch (length) { | ||
case 2: | ||
out = vec2_transformMat4(result || [-0, -0], vector, this); | ||
break; | ||
case 3: | ||
out = vec3_transformMat4(result || [-0, -0, -0], vector, this); | ||
break; | ||
default: | ||
throw new Error('Illegal vector'); | ||
get RANK() { | ||
return 4; | ||
} | ||
checkVector(out, vector.length); | ||
return out; | ||
} | ||
transformAsVector(vector, result) { | ||
let out; | ||
switch (vector.length) { | ||
case 2: | ||
out = vec2_transformMat4AsVector(result || [-0, -0], vector, this); | ||
break; | ||
case 3: | ||
out = vec3_transformMat4AsVector(result || [-0, -0, -0], vector, this); | ||
break; | ||
default: | ||
throw new Error('Illegal vector'); | ||
get INDICES() { | ||
return INDICES; | ||
} | ||
checkVector(out, vector.length); | ||
return out; | ||
} | ||
transformPoint(vector, result) { | ||
return this.transformAsPoint(vector, result); | ||
} | ||
transformVector(vector, result) { | ||
return this.transformAsPoint(vector, result); | ||
} | ||
transformDirection(vector, result) { | ||
return this.transformAsVector(vector, result); | ||
} | ||
makeRotationX(radians) { | ||
return this.identity().rotateX(radians); | ||
} | ||
makeTranslation(x, y, z) { | ||
return this.identity().translate([x, y, z]); | ||
} | ||
constructor(array) { | ||
// PERF NOTE: initialize elements as double precision numbers | ||
super(-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0); | ||
if (arguments.length === 1 && Array.isArray(array)) { | ||
this.copy(array); | ||
} | ||
else { | ||
this.identity(); | ||
} | ||
} | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
this[3] = array[3]; | ||
this[4] = array[4]; | ||
this[5] = array[5]; | ||
this[6] = array[6]; | ||
this[7] = array[7]; | ||
this[8] = array[8]; | ||
this[9] = array[9]; | ||
this[10] = array[10]; | ||
this[11] = array[11]; | ||
this[12] = array[12]; | ||
this[13] = array[13]; | ||
this[14] = array[14]; | ||
this[15] = array[15]; | ||
return this.check(); | ||
} | ||
// eslint-disable-next-line max-params | ||
set(m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33) { | ||
this[0] = m00; | ||
this[1] = m10; | ||
this[2] = m20; | ||
this[3] = m30; | ||
this[4] = m01; | ||
this[5] = m11; | ||
this[6] = m21; | ||
this[7] = m31; | ||
this[8] = m02; | ||
this[9] = m12; | ||
this[10] = m22; | ||
this[11] = m32; | ||
this[12] = m03; | ||
this[13] = m13; | ||
this[14] = m23; | ||
this[15] = m33; | ||
return this.check(); | ||
} | ||
// accepts row major order, stores as column major | ||
// eslint-disable-next-line max-params | ||
setRowMajor(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { | ||
this[0] = m00; | ||
this[1] = m10; | ||
this[2] = m20; | ||
this[3] = m30; | ||
this[4] = m01; | ||
this[5] = m11; | ||
this[6] = m21; | ||
this[7] = m31; | ||
this[8] = m02; | ||
this[9] = m12; | ||
this[10] = m22; | ||
this[11] = m32; | ||
this[12] = m03; | ||
this[13] = m13; | ||
this[14] = m23; | ||
this[15] = m33; | ||
return this.check(); | ||
} | ||
toRowMajor(result) { | ||
result[0] = this[0]; | ||
result[1] = this[4]; | ||
result[2] = this[8]; | ||
result[3] = this[12]; | ||
result[4] = this[1]; | ||
result[5] = this[5]; | ||
result[6] = this[9]; | ||
result[7] = this[13]; | ||
result[8] = this[2]; | ||
result[9] = this[6]; | ||
result[10] = this[10]; | ||
result[11] = this[14]; | ||
result[12] = this[3]; | ||
result[13] = this[7]; | ||
result[14] = this[11]; | ||
result[15] = this[15]; | ||
return result; | ||
} | ||
// Constructors | ||
/** Set to identity matrix */ | ||
identity() { | ||
return this.copy(IDENTITY_MATRIX); | ||
} | ||
/** | ||
* | ||
* @param object | ||
* @returns self | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
fromObject(object) { | ||
return this.check(); | ||
} | ||
/** | ||
* Calculates a 4x4 matrix from the given quaternion | ||
* @param quaternion Quaternion to create matrix from | ||
* @returns self | ||
*/ | ||
fromQuaternion(quaternion) { | ||
mat4_fromQuat(this, quaternion); | ||
return this.check(); | ||
} | ||
/** | ||
* Generates a frustum matrix with the given bounds | ||
* @param view.left - Left bound of the frustum | ||
* @param view.right - Right bound of the frustum | ||
* @param view.bottom - Bottom bound of the frustum | ||
* @param view.top - Top bound of the frustum | ||
* @param view.near - Near bound of the frustum | ||
* @param view.far - Far bound of the frustum. Can be set to Infinity. | ||
* @returns self | ||
*/ | ||
frustum(view) { | ||
const { left, right, bottom, top, near = DEFAULT_NEAR, far = DEFAULT_FAR } = view; | ||
if (far === Infinity) { | ||
computeInfinitePerspectiveOffCenter(this, left, right, bottom, top, near); | ||
} | ||
else { | ||
mat4_frustum(this, left, right, bottom, top, near, far); | ||
} | ||
return this.check(); | ||
} | ||
/** | ||
* Generates a look-at matrix with the given eye position, focal point, | ||
* and up axis | ||
* @param view.eye - (vector) Position of the viewer | ||
* @param view.center - (vector) Point the viewer is looking at | ||
* @param view.up - (vector) Up axis | ||
* @returns self | ||
*/ | ||
lookAt(view) { | ||
const { eye, center = [0, 0, 0], up = [0, 1, 0] } = view; | ||
mat4_lookAt(this, eye, center, up); | ||
return this.check(); | ||
} | ||
/** | ||
* Generates a orthogonal projection matrix with the given bounds | ||
* from "traditional" view space parameters | ||
* @param view.left - Left bound of the frustum | ||
* @param view.right number Right bound of the frustum | ||
* @param view.bottom - Bottom bound of the frustum | ||
* @param view.top number Top bound of the frustum | ||
* @param view.near - Near bound of the frustum | ||
* @param view.far number Far bound of the frustum | ||
* @returns self | ||
*/ | ||
ortho(view) { | ||
const { left, right, bottom, top, near = DEFAULT_NEAR, far = DEFAULT_FAR } = view; | ||
mat4_ortho(this, left, right, bottom, top, near, far); | ||
return this.check(); | ||
} | ||
/** | ||
* Generates an orthogonal projection matrix with the same parameters | ||
* as a perspective matrix (plus focalDistance) | ||
* @param view.fovy Vertical field of view in radians | ||
* @param view.aspect Aspect ratio. Typically viewport width / viewport height | ||
* @param view.focalDistance Distance in the view frustum used for extent calculations | ||
* @param view.near Near bound of the frustum | ||
* @param view.far Far bound of the frustum | ||
* @returns self | ||
*/ | ||
orthographic(view) { | ||
const { fovy = DEFAULT_FOVY, aspect = DEFAULT_ASPECT, focalDistance = 1, near = DEFAULT_NEAR, far = DEFAULT_FAR } = view; | ||
checkRadians(fovy); | ||
const halfY = fovy / 2; | ||
const top = focalDistance * Math.tan(halfY); // focus_plane is the distance from the camera | ||
const right = top * aspect; | ||
return this.ortho({ | ||
left: -right, | ||
right, | ||
bottom: -top, | ||
top, | ||
near, | ||
far | ||
}); | ||
} | ||
/** | ||
* Generates a perspective projection matrix with the given bounds | ||
* @param view.fovy Vertical field of view in radians | ||
* @param view.aspect Aspect ratio. typically viewport width/height | ||
* @param view.near Near bound of the frustum | ||
* @param view.far Far bound of the frustum | ||
* @returns self | ||
*/ | ||
perspective(view) { | ||
const { fovy = (45 * Math.PI) / 180, aspect = 1, near = 0.1, far = 500 } = view; | ||
checkRadians(fovy); | ||
mat4_perspective(this, fovy, aspect, near, far); | ||
return this.check(); | ||
} | ||
// Accessors | ||
determinant() { | ||
return mat4_determinant(this); | ||
} | ||
/** | ||
* Extracts the non-uniform scale assuming the matrix is an affine transformation. | ||
* The scales are the "lengths" of the column vectors in the upper-left 3x3 matrix. | ||
* @param result | ||
* @returns self | ||
*/ | ||
getScale(result = [-0, -0, -0]) { | ||
// explicit is faster than hypot... | ||
result[0] = Math.sqrt(this[0] * this[0] + this[1] * this[1] + this[2] * this[2]); | ||
result[1] = Math.sqrt(this[4] * this[4] + this[5] * this[5] + this[6] * this[6]); | ||
result[2] = Math.sqrt(this[8] * this[8] + this[9] * this[9] + this[10] * this[10]); | ||
// result[0] = Math.hypot(this[0], this[1], this[2]); | ||
// result[1] = Math.hypot(this[4], this[5], this[6]); | ||
// result[2] = Math.hypot(this[8], this[9], this[10]); | ||
return result; | ||
} | ||
/** | ||
* Gets the translation portion, assuming the matrix is a affine transformation matrix. | ||
* @param result | ||
* @returns self | ||
*/ | ||
getTranslation(result = [-0, -0, -0]) { | ||
result[0] = this[12]; | ||
result[1] = this[13]; | ||
result[2] = this[14]; | ||
return result; | ||
} | ||
/** | ||
* Gets upper left 3x3 pure rotation matrix (non-scaling), assume affine transformation matrix | ||
* @param result | ||
* @param scaleResult | ||
* @returns self | ||
*/ | ||
getRotation(result, scaleResult) { | ||
result = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0]; | ||
scaleResult = scaleResult || [-0, -0, -0]; | ||
const scale = this.getScale(scaleResult); | ||
const inverseScale0 = 1 / scale[0]; | ||
const inverseScale1 = 1 / scale[1]; | ||
const inverseScale2 = 1 / scale[2]; | ||
result[0] = this[0] * inverseScale0; | ||
result[1] = this[1] * inverseScale1; | ||
result[2] = this[2] * inverseScale2; | ||
result[3] = 0; | ||
result[4] = this[4] * inverseScale0; | ||
result[5] = this[5] * inverseScale1; | ||
result[6] = this[6] * inverseScale2; | ||
result[7] = 0; | ||
result[8] = this[8] * inverseScale0; | ||
result[9] = this[9] * inverseScale1; | ||
result[10] = this[10] * inverseScale2; | ||
result[11] = 0; | ||
result[12] = 0; | ||
result[13] = 0; | ||
result[14] = 0; | ||
result[15] = 1; | ||
return result; | ||
} | ||
/** | ||
* | ||
* @param result | ||
* @param scaleResult | ||
* @returns self | ||
*/ | ||
getRotationMatrix3(result, scaleResult) { | ||
result = result || [-0, -0, -0, -0, -0, -0, -0, -0, -0]; | ||
scaleResult = scaleResult || [-0, -0, -0]; | ||
const scale = this.getScale(scaleResult); | ||
const inverseScale0 = 1 / scale[0]; | ||
const inverseScale1 = 1 / scale[1]; | ||
const inverseScale2 = 1 / scale[2]; | ||
result[0] = this[0] * inverseScale0; | ||
result[1] = this[1] * inverseScale1; | ||
result[2] = this[2] * inverseScale2; | ||
result[3] = this[4] * inverseScale0; | ||
result[4] = this[5] * inverseScale1; | ||
result[5] = this[6] * inverseScale2; | ||
result[6] = this[8] * inverseScale0; | ||
result[7] = this[9] * inverseScale1; | ||
result[8] = this[10] * inverseScale2; | ||
return result; | ||
} | ||
// Modifiers | ||
transpose() { | ||
mat4_transpose(this, this); | ||
return this.check(); | ||
} | ||
invert() { | ||
mat4_invert(this, this); | ||
return this.check(); | ||
} | ||
// Operations | ||
multiplyLeft(a) { | ||
mat4_multiply(this, a, this); | ||
return this.check(); | ||
} | ||
multiplyRight(a) { | ||
mat4_multiply(this, this, a); | ||
return this.check(); | ||
} | ||
// Rotates a matrix by the given angle around the X axis | ||
rotateX(radians) { | ||
mat4_rotateX(this, this, radians); | ||
// mat4_rotate(this, this, radians, [1, 0, 0]); | ||
return this.check(); | ||
} | ||
// Rotates a matrix by the given angle around the Y axis. | ||
rotateY(radians) { | ||
mat4_rotateY(this, this, radians); | ||
// mat4_rotate(this, this, radians, [0, 1, 0]); | ||
return this.check(); | ||
} | ||
/** | ||
* Rotates a matrix by the given angle around the Z axis. | ||
* @param radians | ||
* @returns self | ||
*/ | ||
rotateZ(radians) { | ||
mat4_rotateZ(this, this, radians); | ||
// mat4_rotate(this, this, radians, [0, 0, 1]); | ||
return this.check(); | ||
} | ||
/** | ||
* | ||
* @param param0 | ||
* @returns self | ||
*/ | ||
rotateXYZ(angleXYZ) { | ||
return this.rotateX(angleXYZ[0]).rotateY(angleXYZ[1]).rotateZ(angleXYZ[2]); | ||
} | ||
/** | ||
* | ||
* @param radians | ||
* @param axis | ||
* @returns self | ||
*/ | ||
rotateAxis(radians, axis) { | ||
mat4_rotate(this, this, radians, axis); | ||
return this.check(); | ||
} | ||
/** | ||
* | ||
* @param factor | ||
* @returns self | ||
*/ | ||
scale(factor) { | ||
mat4_scale(this, this, Array.isArray(factor) ? factor : [factor, factor, factor]); | ||
return this.check(); | ||
} | ||
/** | ||
* | ||
* @param vec | ||
* @returns self | ||
*/ | ||
translate(vector) { | ||
mat4_translate(this, this, vector); | ||
return this.check(); | ||
} | ||
// Transforms | ||
/** | ||
* Transforms any 2, 3 or 4 element vector. 2 and 3 elements are treated as points | ||
* @param vector | ||
* @param result | ||
* @returns self | ||
*/ | ||
transform(vector, result) { | ||
if (vector.length === 4) { | ||
result = vec4_transformMat4(result || [-0, -0, -0, -0], vector, this); | ||
checkVector(result, 4); | ||
return result; | ||
} | ||
return this.transformAsPoint(vector, result); | ||
} | ||
/** | ||
* Transforms any 2 or 3 element array as point (w implicitly 1) | ||
* @param vector | ||
* @param result | ||
* @returns self | ||
*/ | ||
transformAsPoint(vector, result) { | ||
const { length } = vector; | ||
let out; | ||
switch (length) { | ||
case 2: | ||
out = vec2_transformMat4(result || [-0, -0], vector, this); | ||
break; | ||
case 3: | ||
out = vec3_transformMat4(result || [-0, -0, -0], vector, this); | ||
break; | ||
default: | ||
throw new Error('Illegal vector'); | ||
} | ||
checkVector(out, vector.length); | ||
return out; | ||
} | ||
/** | ||
* Transforms any 2 or 3 element array as vector (w implicitly 0) | ||
* @param vector | ||
* @param result | ||
* @returns self | ||
*/ | ||
transformAsVector(vector, result) { | ||
let out; | ||
switch (vector.length) { | ||
case 2: | ||
out = vec2_transformMat4AsVector(result || [-0, -0], vector, this); | ||
break; | ||
case 3: | ||
out = vec3_transformMat4AsVector(result || [-0, -0, -0], vector, this); | ||
break; | ||
default: | ||
throw new Error('Illegal vector'); | ||
} | ||
checkVector(out, vector.length); | ||
return out; | ||
} | ||
/** @deprecated */ | ||
transformPoint(vector, result) { | ||
return this.transformAsPoint(vector, result); | ||
} | ||
/** @deprecated */ | ||
transformVector(vector, result) { | ||
return this.transformAsPoint(vector, result); | ||
} | ||
/** @deprecated */ | ||
transformDirection(vector, result) { | ||
return this.transformAsVector(vector, result); | ||
} | ||
// three.js math API compatibility | ||
makeRotationX(radians) { | ||
return this.identity().rotateX(radians); | ||
} | ||
makeTranslation(x, y, z) { | ||
return this.identity().translate([x, y, z]); | ||
} | ||
} | ||
// TODO initializing static members directly is an option, but make sure no tree-shaking issues | ||
let ZERO; | ||
let IDENTITY; | ||
function getZeroMatrix() { | ||
if (!ZERO) { | ||
ZERO = new Matrix4([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); | ||
Object.freeze(ZERO); | ||
} | ||
return ZERO; | ||
if (!ZERO) { | ||
ZERO = new Matrix4([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); | ||
Object.freeze(ZERO); | ||
} | ||
return ZERO; | ||
} | ||
function getIdentityMatrix() { | ||
if (!IDENTITY) { | ||
IDENTITY = new Matrix4(); | ||
Object.freeze(IDENTITY); | ||
} | ||
return IDENTITY; | ||
if (!IDENTITY) { | ||
IDENTITY = new Matrix4(); | ||
Object.freeze(IDENTITY); | ||
} | ||
return IDENTITY; | ||
} | ||
// HELPER FUNCTIONS | ||
function checkRadians(possiblyDegrees) { | ||
if (possiblyDegrees > Math.PI * 2) { | ||
throw Error('expected radians'); | ||
} | ||
if (possiblyDegrees > Math.PI * 2) { | ||
throw Error('expected radians'); | ||
} | ||
} | ||
// eslint-disable-next-line max-params | ||
function computeInfinitePerspectiveOffCenter(result, left, right, bottom, top, near) { | ||
const column0Row0 = 2 * near / (right - left); | ||
const column1Row1 = 2 * near / (top - bottom); | ||
const column2Row0 = (right + left) / (right - left); | ||
const column2Row1 = (top + bottom) / (top - bottom); | ||
const column2Row2 = -1; | ||
const column2Row3 = -1; | ||
const column3Row2 = -2 * near; | ||
result[0] = column0Row0; | ||
result[1] = 0; | ||
result[2] = 0; | ||
result[3] = 0; | ||
result[4] = 0; | ||
result[5] = column1Row1; | ||
result[6] = 0; | ||
result[7] = 0; | ||
result[8] = column2Row0; | ||
result[9] = column2Row1; | ||
result[10] = column2Row2; | ||
result[11] = column2Row3; | ||
result[12] = 0; | ||
result[13] = 0; | ||
result[14] = column3Row2; | ||
result[15] = 0; | ||
return result; | ||
const column0Row0 = (2 * near) / (right - left); | ||
const column1Row1 = (2 * near) / (top - bottom); | ||
const column2Row0 = (right + left) / (right - left); | ||
const column2Row1 = (top + bottom) / (top - bottom); | ||
const column2Row2 = -1; | ||
const column2Row3 = -1; | ||
const column3Row2 = -2 * near; | ||
result[0] = column0Row0; | ||
result[1] = 0; | ||
result[2] = 0; | ||
result[3] = 0; | ||
result[4] = 0; | ||
result[5] = column1Row1; | ||
result[6] = 0; | ||
result[7] = 0; | ||
result[8] = column2Row0; | ||
result[9] = column2Row1; | ||
result[10] = column2Row2; | ||
result[11] = column2Row3; | ||
result[12] = 0; | ||
result[13] = 0; | ||
result[14] = column3Row2; | ||
result[15] = 0; | ||
return result; | ||
} | ||
//# sourceMappingURL=matrix4.js.map |
@@ -1,6 +0,6 @@ | ||
import { Matrix4 } from './matrix4'; | ||
import { Vector3 } from './vector3'; | ||
import { Euler } from './euler'; | ||
import { Matrix4 } from "./matrix4.js"; | ||
import { Vector3 } from "./vector3.js"; | ||
import { Euler } from "./euler.js"; | ||
import { NumericArray } from '@math.gl/types'; | ||
declare type PoseOptions = { | ||
type PoseOptions = { | ||
position?: Readonly<NumericArray>; | ||
@@ -40,2 +40,1 @@ orientation?: Readonly<NumericArray>; | ||
export {}; | ||
//# sourceMappingURL=pose.d.ts.map |
@@ -1,2 +0,3 @@ | ||
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
// Copyright (c) 2017 Uber Technologies, Inc. | ||
// MIT License | ||
import { Matrix4 } from "./matrix4.js"; | ||
@@ -6,120 +7,104 @@ import { Vector3 } from "./vector3.js"; | ||
export class Pose { | ||
constructor({ | ||
x = 0, | ||
y = 0, | ||
z = 0, | ||
roll = 0, | ||
pitch = 0, | ||
yaw = 0, | ||
position, | ||
orientation | ||
} = {}) { | ||
_defineProperty(this, "position", void 0); | ||
_defineProperty(this, "orientation", void 0); | ||
if (Array.isArray(position) && position.length === 3) { | ||
this.position = new Vector3(position); | ||
} else { | ||
this.position = new Vector3(x, y, z); | ||
constructor({ x = 0, y = 0, z = 0, roll = 0, pitch = 0, yaw = 0, position, orientation } = {}) { | ||
if (Array.isArray(position) && position.length === 3) { | ||
this.position = new Vector3(position); | ||
} | ||
else { | ||
this.position = new Vector3(x, y, z); | ||
} | ||
if (Array.isArray(orientation) && orientation.length === 4) { | ||
// @ts-expect-error | ||
this.orientation = new Euler(orientation, orientation[3]); | ||
} | ||
else { | ||
this.orientation = new Euler(roll, pitch, yaw, Euler.RollPitchYaw); | ||
} | ||
} | ||
if (Array.isArray(orientation) && orientation.length === 4) { | ||
this.orientation = new Euler(orientation, orientation[3]); | ||
} else { | ||
this.orientation = new Euler(roll, pitch, yaw, Euler.RollPitchYaw); | ||
get x() { | ||
return this.position.x; | ||
} | ||
} | ||
get x() { | ||
return this.position.x; | ||
} | ||
set x(value) { | ||
this.position.x = value; | ||
} | ||
get y() { | ||
return this.position.y; | ||
} | ||
set y(value) { | ||
this.position.y = value; | ||
} | ||
get z() { | ||
return this.position.z; | ||
} | ||
set z(value) { | ||
this.position.z = value; | ||
} | ||
get roll() { | ||
return this.orientation.roll; | ||
} | ||
set roll(value) { | ||
this.orientation.roll = value; | ||
} | ||
get pitch() { | ||
return this.orientation.pitch; | ||
} | ||
set pitch(value) { | ||
this.orientation.pitch = value; | ||
} | ||
get yaw() { | ||
return this.orientation.yaw; | ||
} | ||
set yaw(value) { | ||
this.orientation.yaw = value; | ||
} | ||
getPosition() { | ||
return this.position; | ||
} | ||
getOrientation() { | ||
return this.orientation; | ||
} | ||
equals(pose) { | ||
if (!pose) { | ||
return false; | ||
set x(value) { | ||
this.position.x = value; | ||
} | ||
return this.position.equals(pose.position) && this.orientation.equals(pose.orientation); | ||
} | ||
exactEquals(pose) { | ||
if (!pose) { | ||
return false; | ||
get y() { | ||
return this.position.y; | ||
} | ||
return this.position.exactEquals(pose.position) && this.orientation.exactEquals(pose.orientation); | ||
} | ||
getTransformationMatrix() { | ||
const sr = Math.sin(this.roll); | ||
const sp = Math.sin(this.pitch); | ||
const sw = Math.sin(this.yaw); | ||
const cr = Math.cos(this.roll); | ||
const cp = Math.cos(this.pitch); | ||
const cw = Math.cos(this.yaw); | ||
return new Matrix4().setRowMajor(cw * cp, -sw * cr + cw * sp * sr, sw * sr + cw * sp * cr, this.x, sw * cp, cw * cr + sw * sp * sr, -cw * sr + sw * sp * cr, this.y, -sp, cp * sr, cp * cr, this.z, 0, 0, 0, 1); | ||
} | ||
getTransformationMatrixFromPose(pose) { | ||
return new Matrix4().multiplyRight(this.getTransformationMatrix()).multiplyRight(pose.getTransformationMatrix().invert()); | ||
} | ||
getTransformationMatrixToPose(pose) { | ||
return new Matrix4().multiplyRight(pose.getTransformationMatrix()).multiplyRight(this.getTransformationMatrix().invert()); | ||
} | ||
set y(value) { | ||
this.position.y = value; | ||
} | ||
get z() { | ||
return this.position.z; | ||
} | ||
set z(value) { | ||
this.position.z = value; | ||
} | ||
get roll() { | ||
return this.orientation.roll; | ||
} | ||
set roll(value) { | ||
this.orientation.roll = value; | ||
} | ||
get pitch() { | ||
return this.orientation.pitch; | ||
} | ||
set pitch(value) { | ||
this.orientation.pitch = value; | ||
} | ||
get yaw() { | ||
return this.orientation.yaw; | ||
} | ||
set yaw(value) { | ||
this.orientation.yaw = value; | ||
} | ||
getPosition() { | ||
return this.position; | ||
} | ||
getOrientation() { | ||
return this.orientation; | ||
} | ||
equals(pose) { | ||
if (!pose) { | ||
return false; | ||
} | ||
return this.position.equals(pose.position) && this.orientation.equals(pose.orientation); | ||
} | ||
exactEquals(pose) { | ||
if (!pose) { | ||
return false; | ||
} | ||
return (this.position.exactEquals(pose.position) && this.orientation.exactEquals(pose.orientation)); | ||
} | ||
getTransformationMatrix() { | ||
// setup pre computations for the sin/cos of the angles | ||
const sr = Math.sin(this.roll); | ||
const sp = Math.sin(this.pitch); | ||
const sw = Math.sin(this.yaw); | ||
const cr = Math.cos(this.roll); | ||
const cp = Math.cos(this.pitch); | ||
const cw = Math.cos(this.yaw); | ||
// Create matrix | ||
return new Matrix4().setRowMajor(cw * cp, // 0,0 | ||
-sw * cr + cw * sp * sr, // 0,1 | ||
sw * sr + cw * sp * cr, // 0,2 | ||
this.x, // 0,3 | ||
sw * cp, // 1,0 | ||
cw * cr + sw * sp * sr, // 1,1 | ||
-cw * sr + sw * sp * cr, // 1,2 | ||
this.y, // 1,3 | ||
-sp, // 2,0 | ||
cp * sr, // 2,1 | ||
cp * cr, // 2,2 | ||
this.z, // 2,3 | ||
0, 0, 0, 1); | ||
} | ||
getTransformationMatrixFromPose(pose) { | ||
return new Matrix4() | ||
.multiplyRight(this.getTransformationMatrix()) | ||
.multiplyRight(pose.getTransformationMatrix().invert()); | ||
} | ||
getTransformationMatrixToPose(pose) { | ||
return new Matrix4() | ||
.multiplyRight(pose.getTransformationMatrix()) | ||
.multiplyRight(this.getTransformationMatrix().invert()); | ||
} | ||
} | ||
//# sourceMappingURL=pose.js.map |
import { NumericArray } from '@math.gl/types'; | ||
import { MathArray } from './base/math-array'; | ||
import { MathArray } from "./base/math-array.js"; | ||
export declare class Quaternion extends MathArray { | ||
@@ -63,2 +63,1 @@ constructor(x?: number | Readonly<NumericArray>, y?: number, z?: number, w?: number); | ||
} | ||
//# sourceMappingURL=quaternion.d.ts.map |
import { MathArray } from "./base/math-array.js"; | ||
import { checkNumber, checkVector } from "../lib/validators.js"; | ||
import { Vector4 } from "./vector4.js"; | ||
import { fromMat3 as quat_fromMat3, setAxisAngle as quat_setAxisAngle, identity as quat_identity, length as quat_length, squaredLength as quat_squaredLength, dot as quat_dot, rotationTo as quat_rotationTo, add as quat_add, calculateW as quat_calculateW, conjugate as quat_conjugate, invert as quat_invert, lerp as quat_lerp, multiply as quat_multiply, rotateX as quat_rotateX, rotateY as quat_rotateY, rotateZ as quat_rotateZ, scale as quat_scale, slerp as quat_slerp } from "../gl-matrix/quat.js"; | ||
// @ts-ignore gl-matrix types... | ||
import { fromMat3 as quat_fromMat3, setAxisAngle as quat_setAxisAngle, identity as quat_identity, length as quat_length, squaredLength as quat_squaredLength, dot as quat_dot, | ||
// getAxisAngle as quat_getAxisAngle, | ||
rotationTo as quat_rotationTo, add as quat_add, calculateW as quat_calculateW, conjugate as quat_conjugate, invert as quat_invert, lerp as quat_lerp, multiply as quat_multiply, rotateX as quat_rotateX, rotateY as quat_rotateY, rotateZ as quat_rotateZ, scale as quat_scale, slerp as quat_slerp } from "../gl-matrix/quat.js"; | ||
// @ts-ignore gl-matrix types... | ||
import { transformQuat as vec4_transformQuat } from "../gl-matrix/vec4.js"; | ||
const IDENTITY_QUATERNION = [0, 0, 0, 1]; | ||
export class Quaternion extends MathArray { | ||
constructor(x = 0, y = 0, z = 0, w = 1) { | ||
super(-0, -0, -0, -0); | ||
if (Array.isArray(x) && arguments.length === 1) { | ||
this.copy(x); | ||
} else { | ||
this.set(x, y, z, w); | ||
constructor(x = 0, y = 0, z = 0, w = 1) { | ||
// PERF NOTE: initialize elements as double precision numbers | ||
super(-0, -0, -0, -0); | ||
// eslint-disable-next-line prefer-rest-params | ||
if (Array.isArray(x) && arguments.length === 1) { | ||
this.copy(x); | ||
} | ||
else { | ||
this.set(x, y, z, w); | ||
} | ||
} | ||
} | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
this[3] = array[3]; | ||
return this.check(); | ||
} | ||
set(x, y, z, w) { | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
this[3] = w; | ||
return this.check(); | ||
} | ||
fromObject(object) { | ||
this[0] = object.x; | ||
this[1] = object.y; | ||
this[2] = object.z; | ||
this[3] = object.w; | ||
return this.check(); | ||
} | ||
fromMatrix3(m) { | ||
quat_fromMat3(this, m); | ||
return this.check(); | ||
} | ||
fromAxisRotation(axis, rad) { | ||
quat_setAxisAngle(this, axis, rad); | ||
return this.check(); | ||
} | ||
identity() { | ||
quat_identity(this); | ||
return this.check(); | ||
} | ||
setAxisAngle(axis, rad) { | ||
return this.fromAxisRotation(axis, rad); | ||
} | ||
get ELEMENTS() { | ||
return 4; | ||
} | ||
get x() { | ||
return this[0]; | ||
} | ||
set x(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
get y() { | ||
return this[1]; | ||
} | ||
set y(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
get z() { | ||
return this[2]; | ||
} | ||
set z(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
get w() { | ||
return this[3]; | ||
} | ||
set w(value) { | ||
this[3] = checkNumber(value); | ||
} | ||
len() { | ||
return quat_length(this); | ||
} | ||
lengthSquared() { | ||
return quat_squaredLength(this); | ||
} | ||
dot(a) { | ||
return quat_dot(this, a); | ||
} | ||
rotationTo(vectorA, vectorB) { | ||
quat_rotationTo(this, vectorA, vectorB); | ||
return this.check(); | ||
} | ||
add(a) { | ||
quat_add(this, this, a); | ||
return this.check(); | ||
} | ||
calculateW() { | ||
quat_calculateW(this, this); | ||
return this.check(); | ||
} | ||
conjugate() { | ||
quat_conjugate(this, this); | ||
return this.check(); | ||
} | ||
invert() { | ||
quat_invert(this, this); | ||
return this.check(); | ||
} | ||
lerp(a, b, t) { | ||
if (t === undefined) { | ||
return this.lerp(this, a, b); | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
this[3] = array[3]; | ||
return this.check(); | ||
} | ||
quat_lerp(this, a, b, t); | ||
return this.check(); | ||
} | ||
multiplyRight(a) { | ||
quat_multiply(this, this, a); | ||
return this.check(); | ||
} | ||
multiplyLeft(a) { | ||
quat_multiply(this, a, this); | ||
return this.check(); | ||
} | ||
normalize() { | ||
const length = this.len(); | ||
const l = length > 0 ? 1 / length : 0; | ||
this[0] = this[0] * l; | ||
this[1] = this[1] * l; | ||
this[2] = this[2] * l; | ||
this[3] = this[3] * l; | ||
if (length === 0) { | ||
this[3] = 1; | ||
set(x, y, z, w) { | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
this[3] = w; | ||
return this.check(); | ||
} | ||
return this.check(); | ||
} | ||
rotateX(rad) { | ||
quat_rotateX(this, this, rad); | ||
return this.check(); | ||
} | ||
rotateY(rad) { | ||
quat_rotateY(this, this, rad); | ||
return this.check(); | ||
} | ||
rotateZ(rad) { | ||
quat_rotateZ(this, this, rad); | ||
return this.check(); | ||
} | ||
scale(b) { | ||
quat_scale(this, this, b); | ||
return this.check(); | ||
} | ||
slerp(arg0, arg1, arg2) { | ||
let start; | ||
let target; | ||
let ratio; | ||
switch (arguments.length) { | ||
case 1: | ||
({ | ||
start = IDENTITY_QUATERNION, | ||
target, | ||
ratio | ||
} = arg0); | ||
break; | ||
case 2: | ||
start = this; | ||
target = arg0; | ||
ratio = arg1; | ||
break; | ||
default: | ||
start = arg0; | ||
target = arg1; | ||
ratio = arg2; | ||
fromObject(object) { | ||
this[0] = object.x; | ||
this[1] = object.y; | ||
this[2] = object.z; | ||
this[3] = object.w; | ||
return this.check(); | ||
} | ||
quat_slerp(this, start, target, ratio); | ||
return this.check(); | ||
} | ||
transformVector4(vector, result = new Vector4()) { | ||
vec4_transformQuat(result, vector, this); | ||
return checkVector(result, 4); | ||
} | ||
lengthSq() { | ||
return this.lengthSquared(); | ||
} | ||
setFromAxisAngle(axis, rad) { | ||
return this.setAxisAngle(axis, rad); | ||
} | ||
premultiply(a) { | ||
return this.multiplyLeft(a); | ||
} | ||
multiply(a) { | ||
return this.multiplyRight(a); | ||
} | ||
/** | ||
* Creates a quaternion from the given 3x3 rotation matrix. | ||
* NOTE: The resultant quaternion is not normalized, so you should | ||
* be sure to renormalize the quaternion yourself where necessary. | ||
* @param m | ||
* @returns | ||
*/ | ||
fromMatrix3(m) { | ||
quat_fromMat3(this, m); | ||
return this.check(); | ||
} | ||
fromAxisRotation(axis, rad) { | ||
quat_setAxisAngle(this, axis, rad); | ||
return this.check(); | ||
} | ||
/** Set a quat to the identity quaternion */ | ||
identity() { | ||
quat_identity(this); | ||
return this.check(); | ||
} | ||
// Set the components of a quat to the given values | ||
// set(i, j, k, l) { | ||
// quat_set(this, i, j, k, l); | ||
// return this.check(); | ||
// } | ||
// Sets a quat from the given angle and rotation axis, then returns it. | ||
setAxisAngle(axis, rad) { | ||
return this.fromAxisRotation(axis, rad); | ||
} | ||
// Getters/setters | ||
get ELEMENTS() { | ||
return 4; | ||
} | ||
get x() { | ||
return this[0]; | ||
} | ||
set x(value) { | ||
this[0] = checkNumber(value); | ||
} | ||
get y() { | ||
return this[1]; | ||
} | ||
set y(value) { | ||
this[1] = checkNumber(value); | ||
} | ||
get z() { | ||
return this[2]; | ||
} | ||
set z(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
get w() { | ||
return this[3]; | ||
} | ||
set w(value) { | ||
this[3] = checkNumber(value); | ||
} | ||
// Calculates the length of a quat | ||
len() { | ||
return quat_length(this); | ||
} | ||
// Calculates the squared length of a quat | ||
lengthSquared() { | ||
return quat_squaredLength(this); | ||
} | ||
// Calculates the dot product of two quat's | ||
// @return {Number} | ||
dot(a) { | ||
return quat_dot(this, a); | ||
} | ||
// Gets the rotation axis and angle for a given quaternion. | ||
// If a quaternion is created with setAxisAngle, this method will | ||
// return the same values as providied in the original parameter | ||
// list OR functionally equivalent values. | ||
// Example: The quaternion formed by axis [0, 0, 1] and angle -90 | ||
// is the same as the quaternion formed by [0, 0, 1] and 270. | ||
// This method favors the latter. | ||
// @return {{[x,y,z], Number}} | ||
// getAxisAngle() { | ||
// const axis = []; | ||
// // const angle = quat_getAxisAngle(axis, this); | ||
// return {axis, angle}; | ||
// } | ||
// MODIFIERS | ||
// Sets a quaternion to represent the shortest rotation from one vector | ||
// to another. Both vectors are assumed to be unit length. | ||
rotationTo(vectorA, vectorB) { | ||
quat_rotationTo(this, vectorA, vectorB); | ||
return this.check(); | ||
} | ||
// Sets the specified quaternion with values corresponding to the given axes. | ||
// Each axis is a vec3 and is expected to be unit length and perpendicular | ||
// to all other specified axes. | ||
// setAxes() { | ||
// Number | ||
// } | ||
// Performs a spherical linear interpolation with two control points | ||
// sqlerp() { | ||
// Number; | ||
// } | ||
// Adds two quat's | ||
add(a) { | ||
quat_add(this, this, a); | ||
return this.check(); | ||
} | ||
// Calculates the W component of a quat from the X, Y, and Z components. | ||
// Any existing W component will be ignored. | ||
calculateW() { | ||
quat_calculateW(this, this); | ||
return this.check(); | ||
} | ||
// Calculates the conjugate of a quat If the quaternion is normalized, | ||
// this function is faster than quat_invert and produces the same result. | ||
conjugate() { | ||
quat_conjugate(this, this); | ||
return this.check(); | ||
} | ||
// Calculates the inverse of a quat | ||
invert() { | ||
quat_invert(this, this); | ||
return this.check(); | ||
} | ||
// Performs a linear interpolation between two quat's | ||
lerp(a, b, t) { | ||
if (t === undefined) { | ||
return this.lerp(this, a, b); | ||
} | ||
quat_lerp(this, a, b, t); | ||
return this.check(); | ||
} | ||
// Multiplies two quat's | ||
multiplyRight(a) { | ||
quat_multiply(this, this, a); | ||
return this.check(); | ||
} | ||
multiplyLeft(a) { | ||
quat_multiply(this, a, this); | ||
return this.check(); | ||
} | ||
// Normalize a quat | ||
normalize() { | ||
// Handle 0 case | ||
const length = this.len(); | ||
const l = length > 0 ? 1 / length : 0; | ||
this[0] = this[0] * l; | ||
this[1] = this[1] * l; | ||
this[2] = this[2] * l; | ||
this[3] = this[3] * l; | ||
// Set to [0, 0, 0, 1] if length is 0 | ||
if (length === 0) { | ||
this[3] = 1; | ||
} | ||
return this.check(); | ||
} | ||
// Rotates a quaternion by the given angle about the X axis | ||
rotateX(rad) { | ||
quat_rotateX(this, this, rad); | ||
return this.check(); | ||
} | ||
// Rotates a quaternion by the given angle about the Y axis | ||
rotateY(rad) { | ||
quat_rotateY(this, this, rad); | ||
return this.check(); | ||
} | ||
// Rotates a quaternion by the given angle about the Z axis | ||
rotateZ(rad) { | ||
quat_rotateZ(this, this, rad); | ||
return this.check(); | ||
} | ||
// Scales a quat by a scalar number | ||
scale(b) { | ||
quat_scale(this, this, b); | ||
return this.check(); | ||
} | ||
// Performs a spherical linear interpolation between two quat | ||
slerp(arg0, arg1, arg2) { | ||
let start; | ||
let target; | ||
let ratio; | ||
// eslint-disable-next-line prefer-rest-params | ||
switch (arguments.length) { | ||
case 1: // Deprecated signature ({start, target, ratio}) | ||
// eslint-disable-next-line prefer-rest-params | ||
({ | ||
start = IDENTITY_QUATERNION, | ||
target, | ||
ratio | ||
} = arg0); | ||
break; | ||
case 2: // THREE.js compatibility signature (target, ration) | ||
start = this; // eslint-disable-line | ||
target = arg0; | ||
ratio = arg1; | ||
break; | ||
default: | ||
// Default signature: (start, target, ratio) | ||
start = arg0; | ||
target = arg1; | ||
ratio = arg2; | ||
} | ||
quat_slerp(this, start, target, ratio); | ||
return this.check(); | ||
} | ||
transformVector4(vector, result = new Vector4()) { | ||
vec4_transformQuat(result, vector, this); | ||
return checkVector(result, 4); | ||
} | ||
// THREE.js Math API compatibility | ||
lengthSq() { | ||
return this.lengthSquared(); | ||
} | ||
setFromAxisAngle(axis, rad) { | ||
return this.setAxisAngle(axis, rad); | ||
} | ||
premultiply(a) { | ||
return this.multiplyLeft(a); | ||
} | ||
multiply(a) { | ||
return this.multiplyRight(a); | ||
} | ||
} | ||
//# sourceMappingURL=quaternion.js.map |
import { NumericArray } from '@math.gl/types'; | ||
import { Vector3 } from './vector3'; | ||
declare type SphericalCoordinatesOptions = { | ||
import { Vector3 } from "./vector3.js"; | ||
type SphericalCoordinatesOptions = { | ||
phi?: number; | ||
@@ -12,3 +12,3 @@ theta?: number; | ||
}; | ||
declare type FormatOptions = { | ||
type FormatOptions = { | ||
printTypes?: boolean; | ||
@@ -61,2 +61,1 @@ }; | ||
export {}; | ||
//# sourceMappingURL=spherical-coordinates.d.ts.map |
@@ -1,155 +0,146 @@ | ||
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
import { Vector3 } from "./vector3.js"; | ||
import { formatValue, equals, config } from "../lib/common.js"; | ||
import { degrees, radians, clamp } from "../lib/common.js"; | ||
// @ts-ignore gl-matrix types... | ||
import * as vec3 from "../gl-matrix/vec3.js"; | ||
// TODO - import epsilon | ||
const EPSILON = 0.000001; | ||
const EARTH_RADIUS_METERS = 6371000; | ||
/** | ||
* The poles (phi) are at the positive and negative y axis. | ||
* The equator starts at positive z. | ||
* @link https://en.wikipedia.org/wiki/Spherical_coordinate_system | ||
*/ | ||
export class SphericalCoordinates { | ||
constructor({ | ||
phi = 0, | ||
theta = 0, | ||
radius = 1, | ||
bearing, | ||
pitch, | ||
altitude, | ||
radiusScale = EARTH_RADIUS_METERS | ||
} = {}) { | ||
_defineProperty(this, "phi", void 0); | ||
_defineProperty(this, "theta", void 0); | ||
_defineProperty(this, "radius", void 0); | ||
_defineProperty(this, "radiusScale", void 0); | ||
this.phi = phi; | ||
this.theta = theta; | ||
this.radius = radius || altitude || 1; | ||
this.radiusScale = radiusScale || 1; | ||
if (bearing !== undefined) { | ||
this.bearing = bearing; | ||
// bearing: number; | ||
// pitch: number; | ||
// altitude: number; | ||
// lnglatZ coordinates | ||
// longitude: number; | ||
// latitude: number; | ||
// lng: number; | ||
// lat: number; | ||
// z: number; | ||
/** | ||
* Creates a new SphericalCoordinates object | ||
* @param options | ||
* @param [options.phi] =0 - rotation around X (latitude) | ||
* @param [options.theta] =0 - rotation around Y (longitude) | ||
* @param [options.radius] =1 - Distance from center | ||
* @param [options.bearing] | ||
* @param [options.pitch] | ||
* @param [options.altitude] | ||
* @param [options.radiusScale] =1 | ||
*/ | ||
// eslint-disable-next-line complexity | ||
constructor({ phi = 0, theta = 0, radius = 1, bearing, pitch, altitude, radiusScale = EARTH_RADIUS_METERS } = {}) { | ||
this.phi = phi; | ||
this.theta = theta; | ||
// TODO - silently accepts illegal 0 | ||
this.radius = radius || altitude || 1; // radial distance from center | ||
this.radiusScale = radiusScale || 1; // Used by lngLatZ | ||
if (bearing !== undefined) { | ||
this.bearing = bearing; // up / down towards top and bottom pole | ||
} | ||
if (pitch !== undefined) { | ||
this.pitch = pitch; // around the equator of the sphere | ||
} | ||
this.check(); | ||
} | ||
if (pitch !== undefined) { | ||
this.pitch = pitch; | ||
toString() { | ||
return this.formatString(config); | ||
} | ||
this.check(); | ||
} | ||
toString() { | ||
return this.formatString(config); | ||
} | ||
formatString({ | ||
printTypes = false | ||
}) { | ||
const f = formatValue; | ||
return "".concat(printTypes ? 'Spherical' : '', "[rho:").concat(f(this.radius), ",theta:").concat(f(this.theta), ",phi:").concat(f(this.phi), "]"); | ||
} | ||
equals(other) { | ||
return equals(this.radius, other.radius) && equals(this.theta, other.theta) && equals(this.phi, other.phi); | ||
} | ||
exactEquals(other) { | ||
return this.radius === other.radius && this.theta === other.theta && this.phi === other.phi; | ||
} | ||
get bearing() { | ||
return 180 - degrees(this.phi); | ||
} | ||
set bearing(v) { | ||
this.phi = Math.PI - radians(v); | ||
} | ||
get pitch() { | ||
return degrees(this.theta); | ||
} | ||
set pitch(v) { | ||
this.theta = radians(v); | ||
} | ||
get longitude() { | ||
return degrees(this.phi); | ||
} | ||
get latitude() { | ||
return degrees(this.theta); | ||
} | ||
get lng() { | ||
return degrees(this.phi); | ||
} | ||
get lat() { | ||
return degrees(this.theta); | ||
} | ||
get z() { | ||
return (this.radius - 1) * this.radiusScale; | ||
} | ||
set(radius, phi, theta) { | ||
this.radius = radius; | ||
this.phi = phi; | ||
this.theta = theta; | ||
return this.check(); | ||
} | ||
clone() { | ||
return new SphericalCoordinates().copy(this); | ||
} | ||
copy(other) { | ||
this.radius = other.radius; | ||
this.phi = other.phi; | ||
this.theta = other.theta; | ||
return this.check(); | ||
} | ||
fromLngLatZ([lng, lat, z]) { | ||
this.radius = 1 + z / this.radiusScale; | ||
this.phi = radians(lat); | ||
this.theta = radians(lng); | ||
return this.check(); | ||
} | ||
fromVector3(v) { | ||
this.radius = vec3.length(v); | ||
if (this.radius > 0) { | ||
this.theta = Math.atan2(v[0], v[1]); | ||
this.phi = Math.acos(clamp(v[2] / this.radius, -1, 1)); | ||
formatString({ printTypes = false }) { | ||
const f = formatValue; | ||
return `${printTypes ? 'Spherical' : ''}\ | ||
[rho:${f(this.radius)},theta:${f(this.theta)},phi:${f(this.phi)}]`; | ||
} | ||
return this.check(); | ||
} | ||
toVector3() { | ||
return new Vector3(0, 0, this.radius).rotateX({ | ||
radians: this.theta | ||
}).rotateZ({ | ||
radians: this.phi | ||
}); | ||
} | ||
makeSafe() { | ||
this.phi = Math.max(EPSILON, Math.min(Math.PI - EPSILON, this.phi)); | ||
return this; | ||
} | ||
check() { | ||
if (!Number.isFinite(this.phi) || !Number.isFinite(this.theta) || !(this.radius > 0)) { | ||
throw new Error('SphericalCoordinates: some fields set to invalid numbers'); | ||
equals(other) { | ||
return (equals(this.radius, other.radius) && | ||
equals(this.theta, other.theta) && | ||
equals(this.phi, other.phi)); | ||
} | ||
return this; | ||
} | ||
exactEquals(other) { | ||
return this.radius === other.radius && this.theta === other.theta && this.phi === other.phi; | ||
} | ||
/* eslint-disable brace-style */ | ||
// Cartographic (bearing 0 north, pitch 0 look from above) | ||
get bearing() { | ||
return 180 - degrees(this.phi); | ||
} | ||
set bearing(v) { | ||
this.phi = Math.PI - radians(v); | ||
} | ||
get pitch() { | ||
return degrees(this.theta); | ||
} | ||
set pitch(v) { | ||
this.theta = radians(v); | ||
} | ||
// get pitch() { return 90 - degrees(this.phi); } | ||
// set pitch(v) { this.phi = radians(v) + Math.PI / 2; } | ||
// get altitude() { return this.radius - 1; } // relative altitude | ||
// lnglatZ coordinates | ||
get longitude() { | ||
return degrees(this.phi); | ||
} | ||
get latitude() { | ||
return degrees(this.theta); | ||
} | ||
get lng() { | ||
return degrees(this.phi); | ||
} | ||
get lat() { | ||
return degrees(this.theta); | ||
} | ||
get z() { | ||
return (this.radius - 1) * this.radiusScale; | ||
} | ||
/* eslint-enable brace-style */ | ||
set(radius, phi, theta) { | ||
this.radius = radius; | ||
this.phi = phi; | ||
this.theta = theta; | ||
return this.check(); | ||
} | ||
clone() { | ||
return new SphericalCoordinates().copy(this); | ||
} | ||
copy(other) { | ||
this.radius = other.radius; | ||
this.phi = other.phi; | ||
this.theta = other.theta; | ||
return this.check(); | ||
} | ||
fromLngLatZ([lng, lat, z]) { | ||
this.radius = 1 + z / this.radiusScale; | ||
this.phi = radians(lat); | ||
this.theta = radians(lng); | ||
return this.check(); | ||
} | ||
fromVector3(v) { | ||
this.radius = vec3.length(v); | ||
if (this.radius > 0) { | ||
this.theta = Math.atan2(v[0], v[1]); // equator angle around y-up axis | ||
this.phi = Math.acos(clamp(v[2] / this.radius, -1, 1)); // polar angle | ||
} | ||
return this.check(); | ||
} | ||
toVector3() { | ||
return new Vector3(0, 0, this.radius) | ||
.rotateX({ radians: this.theta }) | ||
.rotateZ({ radians: this.phi }); | ||
} | ||
// restrict phi to be betwee EPS and PI-EPS | ||
makeSafe() { | ||
this.phi = Math.max(EPSILON, Math.min(Math.PI - EPSILON, this.phi)); | ||
return this; | ||
} | ||
check() { | ||
// this.makeSafe(); | ||
if (!Number.isFinite(this.phi) || !Number.isFinite(this.theta) || !(this.radius > 0)) { | ||
throw new Error('SphericalCoordinates: some fields set to invalid numbers'); | ||
} | ||
return this; | ||
} | ||
} | ||
//# sourceMappingURL=spherical-coordinates.js.map |
@@ -1,2 +0,2 @@ | ||
import { Vector } from './base/vector'; | ||
import { Vector } from "./base/vector.js"; | ||
import { NumericArray } from '@math.gl/types'; | ||
@@ -55,2 +55,1 @@ /** | ||
} | ||
//# sourceMappingURL=vector2.d.ts.map |
@@ -0,1 +1,3 @@ | ||
// Copyright (c) 2017 Uber Technologies, Inc. | ||
// MIT License | ||
import { Vector } from "./base/vector.js"; | ||
@@ -6,90 +8,104 @@ import { config, isArray } from "../lib/common.js"; | ||
import { vec2_transformMat4AsVector } from "../lib/gl-matrix-extras.js"; | ||
/** | ||
* Two-element vector class. | ||
* Subclass of Array<number> | ||
*/ | ||
export class Vector2 extends Vector { | ||
constructor(x = 0, y = 0) { | ||
super(2); | ||
if (isArray(x) && arguments.length === 1) { | ||
this.copy(x); | ||
} else { | ||
if (config.debug) { | ||
checkNumber(x); | ||
checkNumber(y); | ||
} | ||
this[0] = x; | ||
this[1] = y; | ||
// Creates a new, empty vec2 | ||
constructor(x = 0, y = 0) { | ||
// PERF NOTE: initialize elements as double precision numbers | ||
super(2); // -0, -0); | ||
if (isArray(x) && arguments.length === 1) { | ||
this.copy(x); | ||
} | ||
else { | ||
if (config.debug) { | ||
checkNumber(x); | ||
checkNumber(y); | ||
} | ||
this[0] = x; | ||
this[1] = y; | ||
} | ||
} | ||
} | ||
set(x, y) { | ||
this[0] = x; | ||
this[1] = y; | ||
return this.check(); | ||
} | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
return this.check(); | ||
} | ||
fromObject(object) { | ||
if (config.debug) { | ||
checkNumber(object.x); | ||
checkNumber(object.y); | ||
set(x, y) { | ||
this[0] = x; | ||
this[1] = y; | ||
return this.check(); | ||
} | ||
this[0] = object.x; | ||
this[1] = object.y; | ||
return this.check(); | ||
} | ||
toObject(object) { | ||
object.x = this[0]; | ||
object.y = this[1]; | ||
return object; | ||
} | ||
get ELEMENTS() { | ||
return 2; | ||
} | ||
horizontalAngle() { | ||
return Math.atan2(this.y, this.x); | ||
} | ||
verticalAngle() { | ||
return Math.atan2(this.x, this.y); | ||
} | ||
transform(matrix4) { | ||
return this.transformAsPoint(matrix4); | ||
} | ||
transformAsPoint(matrix4) { | ||
vec2_transformMat4(this, this, matrix4); | ||
return this.check(); | ||
} | ||
transformAsVector(matrix4) { | ||
vec2_transformMat4AsVector(this, this, matrix4); | ||
return this.check(); | ||
} | ||
transformByMatrix3(matrix3) { | ||
vec2_transformMat3(this, this, matrix3); | ||
return this.check(); | ||
} | ||
transformByMatrix2x3(matrix2x3) { | ||
vec2_transformMat2d(this, this, matrix2x3); | ||
return this.check(); | ||
} | ||
transformByMatrix2(matrix2) { | ||
vec2_transformMat2(this, this, matrix2); | ||
return this.check(); | ||
} | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
return this.check(); | ||
} | ||
fromObject(object) { | ||
if (config.debug) { | ||
checkNumber(object.x); | ||
checkNumber(object.y); | ||
} | ||
this[0] = object.x; | ||
this[1] = object.y; | ||
return this.check(); | ||
} | ||
toObject(object) { | ||
object.x = this[0]; | ||
object.y = this[1]; | ||
return object; | ||
} | ||
// Getters/setters | ||
get ELEMENTS() { | ||
return 2; | ||
} | ||
/** | ||
* Returns angle from x axis | ||
* @returns | ||
*/ | ||
horizontalAngle() { | ||
return Math.atan2(this.y, this.x); | ||
} | ||
/** | ||
* Returns angle from y axis | ||
* @returns | ||
*/ | ||
verticalAngle() { | ||
return Math.atan2(this.x, this.y); | ||
} | ||
// Transforms | ||
/** | ||
* Transforms as point | ||
* @param matrix4 | ||
* @returns | ||
*/ | ||
transform(matrix4) { | ||
return this.transformAsPoint(matrix4); | ||
} | ||
/** | ||
* transforms as point (4th component is implicitly 1) | ||
* @param matrix4 | ||
* @returns | ||
*/ | ||
transformAsPoint(matrix4) { | ||
vec2_transformMat4(this, this, matrix4); | ||
return this.check(); | ||
} | ||
/** | ||
* transforms as vector (4th component is implicitly 0, ignores translation. slightly faster) | ||
* @param matrix4 | ||
* @returns | ||
*/ | ||
transformAsVector(matrix4) { | ||
vec2_transformMat4AsVector(this, this, matrix4); | ||
return this.check(); | ||
} | ||
transformByMatrix3(matrix3) { | ||
vec2_transformMat3(this, this, matrix3); | ||
return this.check(); | ||
} | ||
transformByMatrix2x3(matrix2x3) { | ||
vec2_transformMat2d(this, this, matrix2x3); | ||
return this.check(); | ||
} | ||
transformByMatrix2(matrix2) { | ||
vec2_transformMat2(this, this, matrix2); | ||
return this.check(); | ||
} | ||
} | ||
//# sourceMappingURL=vector2.js.map |
import { NumericArray } from '@math.gl/types'; | ||
import { Vector } from './base/vector'; | ||
import { Vector } from "./base/vector.js"; | ||
/** | ||
@@ -56,2 +56,1 @@ * Three-element vector class. | ||
} | ||
//# sourceMappingURL=vector3.d.ts.map |
import { Vector } from "./base/vector.js"; | ||
import { config, isArray } from "../lib/common.js"; | ||
import { checkNumber } from "../lib/validators.js"; | ||
// @ts-ignore gl-matrix types | ||
import { angle as vec3_angle, cross as vec3_cross, rotateX as vec3_rotateX, rotateY as vec3_rotateY, rotateZ as vec3_rotateZ, transformMat4 as vec3_transformMat4, transformMat3 as vec3_transformMat3, transformQuat as vec3_transformQuat } from "../gl-matrix/vec3.js"; | ||
/* eslint-disable camelcase */ | ||
import { vec3_transformMat2, vec3_transformMat4AsVector } from "../lib/gl-matrix-extras.js"; | ||
const ORIGIN = [0, 0, 0]; | ||
let ZERO; | ||
/** | ||
* Three-element vector class. | ||
* Subclass of Array<number> | ||
*/ | ||
export class Vector3 extends Vector { | ||
static get ZERO() { | ||
if (!ZERO) { | ||
ZERO = new Vector3(0, 0, 0); | ||
Object.freeze(ZERO); | ||
static get ZERO() { | ||
if (!ZERO) { | ||
ZERO = new Vector3(0, 0, 0); | ||
Object.freeze(ZERO); | ||
} | ||
return ZERO; | ||
} | ||
return ZERO; | ||
} | ||
constructor(x = 0, y = 0, z = 0) { | ||
super(-0, -0, -0); | ||
if (arguments.length === 1 && isArray(x)) { | ||
this.copy(x); | ||
} else { | ||
if (config.debug) { | ||
checkNumber(x); | ||
checkNumber(y); | ||
checkNumber(z); | ||
} | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
/** | ||
* @class | ||
* @param x | ||
* @param y | ||
* @param z | ||
*/ | ||
constructor(x = 0, y = 0, z = 0) { | ||
// PERF NOTE: initialize elements as double precision numbers | ||
super(-0, -0, -0); | ||
if (arguments.length === 1 && isArray(x)) { | ||
this.copy(x); | ||
} | ||
else { | ||
// this.set(x, y, z); | ||
if (config.debug) { | ||
checkNumber(x); | ||
checkNumber(y); | ||
checkNumber(z); | ||
} | ||
// @ts-expect-error TS2412: Property '0' of type 'number | [number, number, number]' is not assignable to numeric index type 'number' | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
} | ||
} | ||
} | ||
set(x, y, z) { | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
return this.check(); | ||
} | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
return this.check(); | ||
} | ||
fromObject(object) { | ||
if (config.debug) { | ||
checkNumber(object.x); | ||
checkNumber(object.y); | ||
checkNumber(object.z); | ||
set(x, y, z) { | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
return this.check(); | ||
} | ||
this[0] = object.x; | ||
this[1] = object.y; | ||
this[2] = object.z; | ||
return this.check(); | ||
} | ||
toObject(object) { | ||
object.x = this[0]; | ||
object.y = this[1]; | ||
object.z = this[2]; | ||
return object; | ||
} | ||
get ELEMENTS() { | ||
return 3; | ||
} | ||
get z() { | ||
return this[2]; | ||
} | ||
set z(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
angle(vector) { | ||
return vec3_angle(this, vector); | ||
} | ||
cross(vector) { | ||
vec3_cross(this, this, vector); | ||
return this.check(); | ||
} | ||
rotateX({ | ||
radians, | ||
origin = ORIGIN | ||
}) { | ||
vec3_rotateX(this, this, origin, radians); | ||
return this.check(); | ||
} | ||
rotateY({ | ||
radians, | ||
origin = ORIGIN | ||
}) { | ||
vec3_rotateY(this, this, origin, radians); | ||
return this.check(); | ||
} | ||
rotateZ({ | ||
radians, | ||
origin = ORIGIN | ||
}) { | ||
vec3_rotateZ(this, this, origin, radians); | ||
return this.check(); | ||
} | ||
transform(matrix4) { | ||
return this.transformAsPoint(matrix4); | ||
} | ||
transformAsPoint(matrix4) { | ||
vec3_transformMat4(this, this, matrix4); | ||
return this.check(); | ||
} | ||
transformAsVector(matrix4) { | ||
vec3_transformMat4AsVector(this, this, matrix4); | ||
return this.check(); | ||
} | ||
transformByMatrix3(matrix3) { | ||
vec3_transformMat3(this, this, matrix3); | ||
return this.check(); | ||
} | ||
transformByMatrix2(matrix2) { | ||
vec3_transformMat2(this, this, matrix2); | ||
return this.check(); | ||
} | ||
transformByQuaternion(quaternion) { | ||
vec3_transformQuat(this, this, quaternion); | ||
return this.check(); | ||
} | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
return this.check(); | ||
} | ||
fromObject(object) { | ||
if (config.debug) { | ||
checkNumber(object.x); | ||
checkNumber(object.y); | ||
checkNumber(object.z); | ||
} | ||
this[0] = object.x; | ||
this[1] = object.y; | ||
this[2] = object.z; | ||
return this.check(); | ||
} | ||
toObject(object) { | ||
object.x = this[0]; | ||
object.y = this[1]; | ||
object.z = this[2]; | ||
return object; | ||
} | ||
// Getters/setters | ||
get ELEMENTS() { | ||
return 3; | ||
} | ||
get z() { | ||
return this[2]; | ||
} | ||
set z(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
// ACCESSORS | ||
angle(vector) { | ||
return vec3_angle(this, vector); | ||
} | ||
// MODIFIERS | ||
cross(vector) { | ||
vec3_cross(this, this, vector); | ||
return this.check(); | ||
} | ||
rotateX({ radians, origin = ORIGIN }) { | ||
vec3_rotateX(this, this, origin, radians); | ||
return this.check(); | ||
} | ||
rotateY({ radians, origin = ORIGIN }) { | ||
vec3_rotateY(this, this, origin, radians); | ||
return this.check(); | ||
} | ||
rotateZ({ radians, origin = ORIGIN }) { | ||
vec3_rotateZ(this, this, origin, radians); | ||
return this.check(); | ||
} | ||
// Transforms | ||
// transforms as point (4th component is implicitly 1) | ||
transform(matrix4) { | ||
return this.transformAsPoint(matrix4); | ||
} | ||
// transforms as point (4th component is implicitly 1) | ||
transformAsPoint(matrix4) { | ||
vec3_transformMat4(this, this, matrix4); | ||
return this.check(); | ||
} | ||
// transforms as vector (4th component is implicitly 0, ignores translation. slightly faster) | ||
transformAsVector(matrix4) { | ||
vec3_transformMat4AsVector(this, this, matrix4); | ||
return this.check(); | ||
} | ||
transformByMatrix3(matrix3) { | ||
vec3_transformMat3(this, this, matrix3); | ||
return this.check(); | ||
} | ||
transformByMatrix2(matrix2) { | ||
vec3_transformMat2(this, this, matrix2); | ||
return this.check(); | ||
} | ||
transformByQuaternion(quaternion) { | ||
vec3_transformQuat(this, this, quaternion); | ||
return this.check(); | ||
} | ||
} | ||
//# sourceMappingURL=vector3.js.map |
import { NumericArray } from '@math.gl/types'; | ||
import { Vector } from './base/vector'; | ||
import type { Matrix4 } from './matrix4'; | ||
import { Vector } from "./base/vector.js"; | ||
import type { Matrix4 } from "./matrix4.js"; | ||
/** | ||
@@ -41,2 +41,1 @@ * Four-element vector class. | ||
} | ||
//# sourceMappingURL=vector4.d.ts.map |
@@ -0,1 +1,4 @@ | ||
// Copyright (c) 2017 Uber Technologies, Inc. | ||
// MIT License | ||
/* eslint-disable camelcase */ | ||
import { transformMat4 as vec4_transformMat4, transformQuat as vec4_transformQuat } from "../gl-matrix/vec3.js"; | ||
@@ -7,117 +10,106 @@ import { vec4_transformMat2, vec4_transformMat3 } from "../lib/gl-matrix-extras.js"; | ||
let ZERO; | ||
/** | ||
* Four-element vector class. | ||
* Subclass of Array<number> | ||
*/ | ||
export class Vector4 extends Vector { | ||
static get ZERO() { | ||
if (!ZERO) { | ||
ZERO = new Vector4(0, 0, 0, 0); | ||
Object.freeze(ZERO); | ||
static get ZERO() { | ||
if (!ZERO) { | ||
ZERO = new Vector4(0, 0, 0, 0); | ||
Object.freeze(ZERO); | ||
} | ||
return ZERO; | ||
} | ||
return ZERO; | ||
} | ||
constructor(x = 0, y = 0, z = 0, w = 0) { | ||
super(-0, -0, -0, -0); | ||
if (isArray(x) && arguments.length === 1) { | ||
this.copy(x); | ||
} else { | ||
if (config.debug) { | ||
checkNumber(x); | ||
checkNumber(y); | ||
checkNumber(z); | ||
checkNumber(w); | ||
} | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
this[3] = w; | ||
constructor(x = 0, y = 0, z = 0, w = 0) { | ||
// PERF NOTE: initialize elements as double precision numbers | ||
super(-0, -0, -0, -0); | ||
if (isArray(x) && arguments.length === 1) { | ||
this.copy(x); | ||
} | ||
else { | ||
// this.set(x, y, z, w); | ||
if (config.debug) { | ||
checkNumber(x); | ||
checkNumber(y); | ||
checkNumber(z); | ||
checkNumber(w); | ||
} | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
this[3] = w; | ||
} | ||
} | ||
} | ||
set(x, y, z, w) { | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
this[3] = w; | ||
return this.check(); | ||
} | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
this[3] = array[3]; | ||
return this.check(); | ||
} | ||
fromObject(object) { | ||
if (config.debug) { | ||
checkNumber(object.x); | ||
checkNumber(object.y); | ||
checkNumber(object.z); | ||
checkNumber(object.w); | ||
set(x, y, z, w) { | ||
this[0] = x; | ||
this[1] = y; | ||
this[2] = z; | ||
this[3] = w; | ||
return this.check(); | ||
} | ||
this[0] = object.x; | ||
this[1] = object.y; | ||
this[2] = object.z; | ||
this[3] = object.w; | ||
return this; | ||
} | ||
toObject(object) { | ||
object.x = this[0]; | ||
object.y = this[1]; | ||
object.z = this[2]; | ||
object.w = this[3]; | ||
return object; | ||
} | ||
get ELEMENTS() { | ||
return 4; | ||
} | ||
get z() { | ||
return this[2]; | ||
} | ||
set z(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
get w() { | ||
return this[3]; | ||
} | ||
set w(value) { | ||
this[3] = checkNumber(value); | ||
} | ||
transform(matrix4) { | ||
vec4_transformMat4(this, this, matrix4); | ||
return this.check(); | ||
} | ||
transformByMatrix3(matrix3) { | ||
vec4_transformMat3(this, this, matrix3); | ||
return this.check(); | ||
} | ||
transformByMatrix2(matrix2) { | ||
vec4_transformMat2(this, this, matrix2); | ||
return this.check(); | ||
} | ||
transformByQuaternion(quaternion) { | ||
vec4_transformQuat(this, this, quaternion); | ||
return this.check(); | ||
} | ||
applyMatrix4(m) { | ||
m.transform(this, this); | ||
return this; | ||
} | ||
copy(array) { | ||
this[0] = array[0]; | ||
this[1] = array[1]; | ||
this[2] = array[2]; | ||
this[3] = array[3]; | ||
return this.check(); | ||
} | ||
fromObject(object) { | ||
if (config.debug) { | ||
checkNumber(object.x); | ||
checkNumber(object.y); | ||
checkNumber(object.z); | ||
checkNumber(object.w); | ||
} | ||
this[0] = object.x; | ||
this[1] = object.y; | ||
this[2] = object.z; | ||
this[3] = object.w; | ||
return this; | ||
} | ||
toObject(object) { | ||
object.x = this[0]; | ||
object.y = this[1]; | ||
object.z = this[2]; | ||
object.w = this[3]; | ||
return object; | ||
} | ||
// Getters/setters | ||
/* eslint-disable no-multi-spaces, brace-style, no-return-assign */ | ||
get ELEMENTS() { | ||
return 4; | ||
} | ||
get z() { | ||
return this[2]; | ||
} | ||
set z(value) { | ||
this[2] = checkNumber(value); | ||
} | ||
get w() { | ||
return this[3]; | ||
} | ||
set w(value) { | ||
this[3] = checkNumber(value); | ||
} | ||
transform(matrix4) { | ||
vec4_transformMat4(this, this, matrix4); | ||
return this.check(); | ||
} | ||
transformByMatrix3(matrix3) { | ||
vec4_transformMat3(this, this, matrix3); | ||
return this.check(); | ||
} | ||
transformByMatrix2(matrix2) { | ||
vec4_transformMat2(this, this, matrix2); | ||
return this.check(); | ||
} | ||
transformByQuaternion(quaternion) { | ||
vec4_transformQuat(this, this, quaternion); | ||
return this.check(); | ||
} | ||
// three.js compatibility | ||
applyMatrix4(m) { | ||
m.transform(this, this); | ||
return this; | ||
} | ||
} | ||
//# sourceMappingURL=vector4.js.map |
@@ -38,2 +38,1 @@ /** | ||
export const ANGLE_ORDER: "zyx"; | ||
//# sourceMappingURL=common.d.ts.map |
@@ -0,1 +1,8 @@ | ||
// @eslint-disable | ||
// @ts-nocheck | ||
/** | ||
* Common utilities | ||
* @module glMatrix | ||
*/ | ||
// Configuration Constants | ||
export const EPSILON = 0.000001; | ||
@@ -5,16 +12,41 @@ export let ARRAY_TYPE = typeof Float32Array !== 'undefined' ? Float32Array : Array; | ||
export const ANGLE_ORDER = 'zyx'; | ||
/** | ||
* Symmetric round | ||
* see https://www.npmjs.com/package/round-half-up-symmetric#user-content-detailed-background | ||
* | ||
* @param {Number} a value to round | ||
*/ | ||
export function round(a) { | ||
if (a >= 0) return Math.round(a); | ||
return a % 0.5 === 0 ? Math.floor(a) : Math.round(a); | ||
if (a >= 0) | ||
return Math.round(a); | ||
return a % 0.5 === 0 ? Math.floor(a) : Math.round(a); | ||
} | ||
/** | ||
* Sets the type of array used when creating new vectors and matrices | ||
* | ||
* @param {Float32ArrayConstructor | ArrayConstructor} type Array type, such as Float32Array or Array | ||
*/ | ||
export function setMatrixArrayType(type) { | ||
ARRAY_TYPE = type; | ||
ARRAY_TYPE = type; | ||
} | ||
const degree = Math.PI / 180; | ||
/** | ||
* Convert Degree To Radian | ||
* | ||
* @param {Number} a Angle in Degrees | ||
*/ | ||
export function toRadian(a) { | ||
return a * degree; | ||
return a * degree; | ||
} | ||
/** | ||
* Tests whether or not the arguments have approximately the same value, within an absolute | ||
* or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less | ||
* than or equal to 1.0, and a relative tolerance is used for larger values) | ||
* | ||
* @param {Number} a The first number to test. | ||
* @param {Number} b The second number to test. | ||
* @returns {Boolean} True if the numbers are approximately equal, false otherwise. | ||
*/ | ||
export function equals(a, b) { | ||
return Math.abs(a - b) <= EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b)); | ||
return Math.abs(a - b) <= EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b)); | ||
} | ||
//# sourceMappingURL=common.js.map |
@@ -287,2 +287,1 @@ /** | ||
export declare const sub: typeof subtract; | ||
//# sourceMappingURL=mat3.d.ts.map |
import * as glMatrix from './common.js'; | ||
/** | ||
* 3x3 Matrix | ||
* @module mat3 | ||
*/ | ||
/** | ||
* Creates a new identity mat3 | ||
* | ||
* @returns {mat3} a new 3x3 matrix | ||
*/ | ||
export function create() { | ||
const out = new glMatrix.ARRAY_TYPE(9); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
} | ||
out[0] = 1; | ||
out[4] = 1; | ||
out[8] = 1; | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(9); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
} | ||
out[0] = 1; | ||
out[4] = 1; | ||
out[8] = 1; | ||
return out; | ||
} | ||
/** | ||
* Copies the upper-left 3x3 values into the given mat3. | ||
* | ||
* @param {mat3} out the receiving 3x3 matrix | ||
* @param {ReadonlyMat4} a the source 4x4 matrix | ||
* @returns {mat3} out | ||
*/ | ||
export function fromMat4(out, a) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[4]; | ||
out[4] = a[5]; | ||
out[5] = a[6]; | ||
out[6] = a[8]; | ||
out[7] = a[9]; | ||
out[8] = a[10]; | ||
return out; | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[4]; | ||
out[4] = a[5]; | ||
out[5] = a[6]; | ||
out[6] = a[8]; | ||
out[7] = a[9]; | ||
out[8] = a[10]; | ||
return out; | ||
} | ||
/** | ||
* Creates a new mat3 initialized with values from an existing matrix | ||
* | ||
* @param {ReadonlyMat3} a matrix to clone | ||
* @returns {mat3} a new 3x3 matrix | ||
*/ | ||
export function clone(a) { | ||
const out = new glMatrix.ARRAY_TYPE(9); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[8] = a[8]; | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(9); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[8] = a[8]; | ||
return out; | ||
} | ||
/** | ||
* Copy the values from one mat3 to another | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat3} a the source matrix | ||
* @returns {mat3} out | ||
*/ | ||
export function copy(out, a) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[8] = a[8]; | ||
return out; | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[8] = a[8]; | ||
return out; | ||
} | ||
/** | ||
* Create a new mat3 with the given values | ||
* | ||
* @param {Number} m00 Component in column 0, row 0 position (index 0) | ||
* @param {Number} m01 Component in column 0, row 1 position (index 1) | ||
* @param {Number} m02 Component in column 0, row 2 position (index 2) | ||
* @param {Number} m10 Component in column 1, row 0 position (index 3) | ||
* @param {Number} m11 Component in column 1, row 1 position (index 4) | ||
* @param {Number} m12 Component in column 1, row 2 position (index 5) | ||
* @param {Number} m20 Component in column 2, row 0 position (index 6) | ||
* @param {Number} m21 Component in column 2, row 1 position (index 7) | ||
* @param {Number} m22 Component in column 2, row 2 position (index 8) | ||
* @returns {mat3} A new mat3 | ||
*/ | ||
export function fromValues(m00, m01, m02, m10, m11, m12, m20, m21, m22) { | ||
const out = new glMatrix.ARRAY_TYPE(9); | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m02; | ||
out[3] = m10; | ||
out[4] = m11; | ||
out[5] = m12; | ||
out[6] = m20; | ||
out[7] = m21; | ||
out[8] = m22; | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(9); | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m02; | ||
out[3] = m10; | ||
out[4] = m11; | ||
out[5] = m12; | ||
out[6] = m20; | ||
out[7] = m21; | ||
out[8] = m22; | ||
return out; | ||
} | ||
/** | ||
* Set the components of a mat3 to the given values | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {Number} m00 Component in column 0, row 0 position (index 0) | ||
* @param {Number} m01 Component in column 0, row 1 position (index 1) | ||
* @param {Number} m02 Component in column 0, row 2 position (index 2) | ||
* @param {Number} m10 Component in column 1, row 0 position (index 3) | ||
* @param {Number} m11 Component in column 1, row 1 position (index 4) | ||
* @param {Number} m12 Component in column 1, row 2 position (index 5) | ||
* @param {Number} m20 Component in column 2, row 0 position (index 6) | ||
* @param {Number} m21 Component in column 2, row 1 position (index 7) | ||
* @param {Number} m22 Component in column 2, row 2 position (index 8) | ||
* @returns {mat3} out | ||
*/ | ||
export function set(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) { | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m02; | ||
out[3] = m10; | ||
out[4] = m11; | ||
out[5] = m12; | ||
out[6] = m20; | ||
out[7] = m21; | ||
out[8] = m22; | ||
return out; | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m02; | ||
out[3] = m10; | ||
out[4] = m11; | ||
out[5] = m12; | ||
out[6] = m20; | ||
out[7] = m21; | ||
out[8] = m22; | ||
return out; | ||
} | ||
/** | ||
* Set a mat3 to the identity matrix | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @returns {mat3} out | ||
*/ | ||
export function identity(out) { | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 1; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 1; | ||
return out; | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 1; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 1; | ||
return out; | ||
} | ||
/** | ||
* Transpose the values of a mat3 | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat3} a the source matrix | ||
* @returns {mat3} out | ||
*/ | ||
export function transpose(out, a) { | ||
if (out === a) { | ||
// If we are transposing ourselves we can skip a few steps but have to cache some values | ||
if (out === a) { | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a12 = a[5]; | ||
out[1] = a[3]; | ||
out[2] = a[6]; | ||
out[3] = a01; | ||
out[5] = a[7]; | ||
out[6] = a02; | ||
out[7] = a12; | ||
} | ||
else { | ||
out[0] = a[0]; | ||
out[1] = a[3]; | ||
out[2] = a[6]; | ||
out[3] = a[1]; | ||
out[4] = a[4]; | ||
out[5] = a[7]; | ||
out[6] = a[2]; | ||
out[7] = a[5]; | ||
out[8] = a[8]; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Inverts a mat3 | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat3} a the source matrix | ||
* @returns {mat3} out | ||
*/ | ||
export function invert(out, a) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
out[1] = a[3]; | ||
out[2] = a[6]; | ||
out[3] = a01; | ||
out[5] = a[7]; | ||
out[6] = a02; | ||
out[7] = a12; | ||
} else { | ||
out[0] = a[0]; | ||
out[1] = a[3]; | ||
out[2] = a[6]; | ||
out[3] = a[1]; | ||
out[4] = a[4]; | ||
out[5] = a[7]; | ||
out[6] = a[2]; | ||
out[7] = a[5]; | ||
out[8] = a[8]; | ||
} | ||
return out; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
const b01 = a22 * a11 - a12 * a21; | ||
const b11 = -a22 * a10 + a12 * a20; | ||
const b21 = a21 * a10 - a11 * a20; | ||
// Calculate the determinant | ||
let det = a00 * b01 + a01 * b11 + a02 * b21; | ||
if (!det) { | ||
return null; | ||
} | ||
det = 1.0 / det; | ||
out[0] = b01 * det; | ||
out[1] = (-a22 * a01 + a02 * a21) * det; | ||
out[2] = (a12 * a01 - a02 * a11) * det; | ||
out[3] = b11 * det; | ||
out[4] = (a22 * a00 - a02 * a20) * det; | ||
out[5] = (-a12 * a00 + a02 * a10) * det; | ||
out[6] = b21 * det; | ||
out[7] = (-a21 * a00 + a01 * a20) * det; | ||
out[8] = (a11 * a00 - a01 * a10) * det; | ||
return out; | ||
} | ||
export function invert(out, a) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
const b01 = a22 * a11 - a12 * a21; | ||
const b11 = -a22 * a10 + a12 * a20; | ||
const b21 = a21 * a10 - a11 * a20; | ||
let det = a00 * b01 + a01 * b11 + a02 * b21; | ||
if (!det) { | ||
return null; | ||
} | ||
det = 1.0 / det; | ||
out[0] = b01 * det; | ||
out[1] = (-a22 * a01 + a02 * a21) * det; | ||
out[2] = (a12 * a01 - a02 * a11) * det; | ||
out[3] = b11 * det; | ||
out[4] = (a22 * a00 - a02 * a20) * det; | ||
out[5] = (-a12 * a00 + a02 * a10) * det; | ||
out[6] = b21 * det; | ||
out[7] = (-a21 * a00 + a01 * a20) * det; | ||
out[8] = (a11 * a00 - a01 * a10) * det; | ||
return out; | ||
} | ||
/** | ||
* Calculates the adjugate of a mat3 | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat3} a the source matrix | ||
* @returns {mat3} out | ||
*/ | ||
export function adjoint(out, a) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
out[0] = a11 * a22 - a12 * a21; | ||
out[1] = a02 * a21 - a01 * a22; | ||
out[2] = a01 * a12 - a02 * a11; | ||
out[3] = a12 * a20 - a10 * a22; | ||
out[4] = a00 * a22 - a02 * a20; | ||
out[5] = a02 * a10 - a00 * a12; | ||
out[6] = a10 * a21 - a11 * a20; | ||
out[7] = a01 * a20 - a00 * a21; | ||
out[8] = a00 * a11 - a01 * a10; | ||
return out; | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
out[0] = a11 * a22 - a12 * a21; | ||
out[1] = a02 * a21 - a01 * a22; | ||
out[2] = a01 * a12 - a02 * a11; | ||
out[3] = a12 * a20 - a10 * a22; | ||
out[4] = a00 * a22 - a02 * a20; | ||
out[5] = a02 * a10 - a00 * a12; | ||
out[6] = a10 * a21 - a11 * a20; | ||
out[7] = a01 * a20 - a00 * a21; | ||
out[8] = a00 * a11 - a01 * a10; | ||
return out; | ||
} | ||
/** | ||
* Calculates the determinant of a mat3 | ||
* | ||
* @param {ReadonlyMat3} a the source matrix | ||
* @returns {Number} determinant of a | ||
*/ | ||
export function determinant(a) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20); | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
return (a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20)); | ||
} | ||
/** | ||
* Multiplies two mat3's | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat3} a the first operand | ||
* @param {ReadonlyMat3} b the second operand | ||
* @returns {mat3} out | ||
*/ | ||
export function multiply(out, a, b) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
const b00 = b[0]; | ||
const b01 = b[1]; | ||
const b02 = b[2]; | ||
const b10 = b[3]; | ||
const b11 = b[4]; | ||
const b12 = b[5]; | ||
const b20 = b[6]; | ||
const b21 = b[7]; | ||
const b22 = b[8]; | ||
out[0] = b00 * a00 + b01 * a10 + b02 * a20; | ||
out[1] = b00 * a01 + b01 * a11 + b02 * a21; | ||
out[2] = b00 * a02 + b01 * a12 + b02 * a22; | ||
out[3] = b10 * a00 + b11 * a10 + b12 * a20; | ||
out[4] = b10 * a01 + b11 * a11 + b12 * a21; | ||
out[5] = b10 * a02 + b11 * a12 + b12 * a22; | ||
out[6] = b20 * a00 + b21 * a10 + b22 * a20; | ||
out[7] = b20 * a01 + b21 * a11 + b22 * a21; | ||
out[8] = b20 * a02 + b21 * a12 + b22 * a22; | ||
return out; | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
const b00 = b[0]; | ||
const b01 = b[1]; | ||
const b02 = b[2]; | ||
const b10 = b[3]; | ||
const b11 = b[4]; | ||
const b12 = b[5]; | ||
const b20 = b[6]; | ||
const b21 = b[7]; | ||
const b22 = b[8]; | ||
out[0] = b00 * a00 + b01 * a10 + b02 * a20; | ||
out[1] = b00 * a01 + b01 * a11 + b02 * a21; | ||
out[2] = b00 * a02 + b01 * a12 + b02 * a22; | ||
out[3] = b10 * a00 + b11 * a10 + b12 * a20; | ||
out[4] = b10 * a01 + b11 * a11 + b12 * a21; | ||
out[5] = b10 * a02 + b11 * a12 + b12 * a22; | ||
out[6] = b20 * a00 + b21 * a10 + b22 * a20; | ||
out[7] = b20 * a01 + b21 * a11 + b22 * a21; | ||
out[8] = b20 * a02 + b21 * a12 + b22 * a22; | ||
return out; | ||
} | ||
/** | ||
* Translate a mat3 by the given vector | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat3} a the matrix to translate | ||
* @param {ReadonlyVec2} v vector to translate by | ||
* @returns {mat3} out | ||
*/ | ||
export function translate(out, a, v) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
const x = v[0]; | ||
const y = v[1]; | ||
out[0] = a00; | ||
out[1] = a01; | ||
out[2] = a02; | ||
out[3] = a10; | ||
out[4] = a11; | ||
out[5] = a12; | ||
out[6] = x * a00 + y * a10 + a20; | ||
out[7] = x * a01 + y * a11 + a21; | ||
out[8] = x * a02 + y * a12 + a22; | ||
return out; | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
const x = v[0]; | ||
const y = v[1]; | ||
out[0] = a00; | ||
out[1] = a01; | ||
out[2] = a02; | ||
out[3] = a10; | ||
out[4] = a11; | ||
out[5] = a12; | ||
out[6] = x * a00 + y * a10 + a20; | ||
out[7] = x * a01 + y * a11 + a21; | ||
out[8] = x * a02 + y * a12 + a22; | ||
return out; | ||
} | ||
/** | ||
* Rotates a mat3 by the given angle | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat3} a the matrix to rotate | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat3} out | ||
*/ | ||
export function rotate(out, a, rad) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
out[0] = c * a00 + s * a10; | ||
out[1] = c * a01 + s * a11; | ||
out[2] = c * a02 + s * a12; | ||
out[3] = c * a10 - s * a00; | ||
out[4] = c * a11 - s * a01; | ||
out[5] = c * a12 - s * a02; | ||
out[6] = a20; | ||
out[7] = a21; | ||
out[8] = a22; | ||
return out; | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a10 = a[3]; | ||
const a11 = a[4]; | ||
const a12 = a[5]; | ||
const a20 = a[6]; | ||
const a21 = a[7]; | ||
const a22 = a[8]; | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
out[0] = c * a00 + s * a10; | ||
out[1] = c * a01 + s * a11; | ||
out[2] = c * a02 + s * a12; | ||
out[3] = c * a10 - s * a00; | ||
out[4] = c * a11 - s * a01; | ||
out[5] = c * a12 - s * a02; | ||
out[6] = a20; | ||
out[7] = a21; | ||
out[8] = a22; | ||
return out; | ||
} | ||
/** | ||
* Scales the mat3 by the dimensions in the given vec2 | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat3} a the matrix to scale | ||
* @param {ReadonlyVec2} v the vec2 to scale the matrix by | ||
* @returns {mat3} out | ||
**/ | ||
export function scale(out, a, v) { | ||
const x = v[0]; | ||
const y = v[1]; | ||
out[0] = x * a[0]; | ||
out[1] = x * a[1]; | ||
out[2] = x * a[2]; | ||
out[3] = y * a[3]; | ||
out[4] = y * a[4]; | ||
out[5] = y * a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[8] = a[8]; | ||
return out; | ||
const x = v[0]; | ||
const y = v[1]; | ||
out[0] = x * a[0]; | ||
out[1] = x * a[1]; | ||
out[2] = x * a[2]; | ||
out[3] = y * a[3]; | ||
out[4] = y * a[4]; | ||
out[5] = y * a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[8] = a[8]; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a vector translation | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat3.identity(dest); | ||
* mat3.translate(dest, dest, vec); | ||
* | ||
* @param {mat3} out mat3 receiving operation result | ||
* @param {ReadonlyVec2} v Translation vector | ||
* @returns {mat3} out | ||
*/ | ||
export function fromTranslation(out, v) { | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 1; | ||
out[5] = 0; | ||
out[6] = v[0]; | ||
out[7] = v[1]; | ||
out[8] = 1; | ||
return out; | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 1; | ||
out[5] = 0; | ||
out[6] = v[0]; | ||
out[7] = v[1]; | ||
out[8] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a given angle | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat3.identity(dest); | ||
* mat3.rotate(dest, dest, rad); | ||
* | ||
* @param {mat3} out mat3 receiving operation result | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat3} out | ||
*/ | ||
export function fromRotation(out, rad) { | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
out[0] = c; | ||
out[1] = s; | ||
out[2] = 0; | ||
out[3] = -s; | ||
out[4] = c; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 1; | ||
return out; | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
out[0] = c; | ||
out[1] = s; | ||
out[2] = 0; | ||
out[3] = -s; | ||
out[4] = c; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a vector scaling | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat3.identity(dest); | ||
* mat3.scale(dest, dest, vec); | ||
* | ||
* @param {mat3} out mat3 receiving operation result | ||
* @param {ReadonlyVec2} v Scaling vector | ||
* @returns {mat3} out | ||
*/ | ||
export function fromScaling(out, v) { | ||
out[0] = v[0]; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = v[1]; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 1; | ||
return out; | ||
out[0] = v[0]; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = v[1]; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 1; | ||
return out; | ||
} | ||
/** | ||
* Copies the values from a mat2d into a mat3 | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat2d} a the matrix to copy | ||
* @returns {mat3} out | ||
**/ | ||
export function fromMat2d(out, a) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = 0; | ||
out[3] = a[2]; | ||
out[4] = a[3]; | ||
out[5] = 0; | ||
out[6] = a[4]; | ||
out[7] = a[5]; | ||
out[8] = 1; | ||
return out; | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = 0; | ||
out[3] = a[2]; | ||
out[4] = a[3]; | ||
out[5] = 0; | ||
out[6] = a[4]; | ||
out[7] = a[5]; | ||
out[8] = 1; | ||
return out; | ||
} | ||
/** | ||
* Calculates a 3x3 matrix from the given quaternion | ||
* | ||
* @param {mat3} out mat3 receiving operation result | ||
* @param {ReadonlyQuat} q Quaternion to create matrix from | ||
* | ||
* @returns {mat3} out | ||
*/ | ||
export function fromQuat(out, q) { | ||
const x = q[0]; | ||
const y = q[1]; | ||
const z = q[2]; | ||
const w = q[3]; | ||
const x2 = x + x; | ||
const y2 = y + y; | ||
const z2 = z + z; | ||
const xx = x * x2; | ||
const yx = y * x2; | ||
const yy = y * y2; | ||
const zx = z * x2; | ||
const zy = z * y2; | ||
const zz = z * z2; | ||
const wx = w * x2; | ||
const wy = w * y2; | ||
const wz = w * z2; | ||
out[0] = 1 - yy - zz; | ||
out[3] = yx - wz; | ||
out[6] = zx + wy; | ||
out[1] = yx + wz; | ||
out[4] = 1 - xx - zz; | ||
out[7] = zy - wx; | ||
out[2] = zx - wy; | ||
out[5] = zy + wx; | ||
out[8] = 1 - xx - yy; | ||
return out; | ||
const x = q[0]; | ||
const y = q[1]; | ||
const z = q[2]; | ||
const w = q[3]; | ||
const x2 = x + x; | ||
const y2 = y + y; | ||
const z2 = z + z; | ||
const xx = x * x2; | ||
const yx = y * x2; | ||
const yy = y * y2; | ||
const zx = z * x2; | ||
const zy = z * y2; | ||
const zz = z * z2; | ||
const wx = w * x2; | ||
const wy = w * y2; | ||
const wz = w * z2; | ||
out[0] = 1 - yy - zz; | ||
out[3] = yx - wz; | ||
out[6] = zx + wy; | ||
out[1] = yx + wz; | ||
out[4] = 1 - xx - zz; | ||
out[7] = zy - wx; | ||
out[2] = zx - wy; | ||
out[5] = zy + wx; | ||
out[8] = 1 - xx - yy; | ||
return out; | ||
} | ||
/** | ||
* Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix | ||
* | ||
* @param {mat3} out mat3 receiving operation result | ||
* @param {ReadonlyMat4} a Mat4 to derive the normal matrix from | ||
* | ||
* @returns {mat3} out | ||
*/ | ||
export function normalFromMat4(out, a) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
const a30 = a[12]; | ||
const a31 = a[13]; | ||
const a32 = a[14]; | ||
const a33 = a[15]; | ||
const b00 = a00 * a11 - a01 * a10; | ||
const b01 = a00 * a12 - a02 * a10; | ||
const b02 = a00 * a13 - a03 * a10; | ||
const b03 = a01 * a12 - a02 * a11; | ||
const b04 = a01 * a13 - a03 * a11; | ||
const b05 = a02 * a13 - a03 * a12; | ||
const b06 = a20 * a31 - a21 * a30; | ||
const b07 = a20 * a32 - a22 * a30; | ||
const b08 = a20 * a33 - a23 * a30; | ||
const b09 = a21 * a32 - a22 * a31; | ||
const b10 = a21 * a33 - a23 * a31; | ||
const b11 = a22 * a33 - a23 * a32; | ||
let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; | ||
if (!det) { | ||
return null; | ||
} | ||
det = 1.0 / det; | ||
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; | ||
out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det; | ||
out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det; | ||
out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det; | ||
out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det; | ||
out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det; | ||
out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det; | ||
out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det; | ||
out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det; | ||
return out; | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
const a30 = a[12]; | ||
const a31 = a[13]; | ||
const a32 = a[14]; | ||
const a33 = a[15]; | ||
const b00 = a00 * a11 - a01 * a10; | ||
const b01 = a00 * a12 - a02 * a10; | ||
const b02 = a00 * a13 - a03 * a10; | ||
const b03 = a01 * a12 - a02 * a11; | ||
const b04 = a01 * a13 - a03 * a11; | ||
const b05 = a02 * a13 - a03 * a12; | ||
const b06 = a20 * a31 - a21 * a30; | ||
const b07 = a20 * a32 - a22 * a30; | ||
const b08 = a20 * a33 - a23 * a30; | ||
const b09 = a21 * a32 - a22 * a31; | ||
const b10 = a21 * a33 - a23 * a31; | ||
const b11 = a22 * a33 - a23 * a32; | ||
// Calculate the determinant | ||
let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; | ||
if (!det) { | ||
return null; | ||
} | ||
det = 1.0 / det; | ||
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; | ||
out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det; | ||
out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det; | ||
out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det; | ||
out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det; | ||
out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det; | ||
out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det; | ||
out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det; | ||
out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det; | ||
return out; | ||
} | ||
/** | ||
* Generates a 2D projection matrix with the given bounds | ||
* | ||
* @param {mat3} out mat3 frustum matrix will be written into | ||
* @param {number} width Width of your gl context | ||
* @param {number} height Height of gl context | ||
* @returns {mat3} out | ||
*/ | ||
export function projection(out, width, height) { | ||
out[0] = 2 / width; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = -2 / height; | ||
out[5] = 0; | ||
out[6] = -1; | ||
out[7] = 1; | ||
out[8] = 1; | ||
return out; | ||
out[0] = 2 / width; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = -2 / height; | ||
out[5] = 0; | ||
out[6] = -1; | ||
out[7] = 1; | ||
out[8] = 1; | ||
return out; | ||
} | ||
/** | ||
* Returns a string representation of a mat3 | ||
* | ||
* @param {ReadonlyMat3} a matrix to represent as a string | ||
* @returns {String} string representation of the matrix | ||
*/ | ||
export function str(a) { | ||
return "mat3(".concat(a[0], ", ").concat(a[1], ", ").concat(a[2], ", ").concat(a[3], ", ").concat(a[4], ", ").concat(a[5], ", ").concat(a[6], ", ").concat(a[7], ", ").concat(a[8], ")"); | ||
return `mat3(${a[0]}, ${a[1]}, ${a[2]}, ${a[3]}, ${a[4]}, ${a[5]}, ${a[6]}, ${a[7]}, ${a[8]})`; | ||
} | ||
/** | ||
* Returns Frobenius norm of a mat3 | ||
* | ||
* @param {ReadonlyMat3} a the matrix to calculate Frobenius norm of | ||
* @returns {Number} Frobenius norm | ||
*/ | ||
export function frob(a) { | ||
return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3] + a[4] * a[4] + a[5] * a[5] + a[6] * a[6] + a[7] * a[7] + a[8] * a[8]); | ||
return Math.sqrt(a[0] * a[0] + | ||
a[1] * a[1] + | ||
a[2] * a[2] + | ||
a[3] * a[3] + | ||
a[4] * a[4] + | ||
a[5] * a[5] + | ||
a[6] * a[6] + | ||
a[7] * a[7] + | ||
a[8] * a[8]); | ||
} | ||
/** | ||
* Adds two mat3's | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat3} a the first operand | ||
* @param {ReadonlyMat3} b the second operand | ||
* @returns {mat3} out | ||
*/ | ||
export function add(out, a, b) { | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
out[4] = a[4] + b[4]; | ||
out[5] = a[5] + b[5]; | ||
out[6] = a[6] + b[6]; | ||
out[7] = a[7] + b[7]; | ||
out[8] = a[8] + b[8]; | ||
return out; | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
out[4] = a[4] + b[4]; | ||
out[5] = a[5] + b[5]; | ||
out[6] = a[6] + b[6]; | ||
out[7] = a[7] + b[7]; | ||
out[8] = a[8] + b[8]; | ||
return out; | ||
} | ||
/** | ||
* Subtracts matrix b from matrix a | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat3} a the first operand | ||
* @param {ReadonlyMat3} b the second operand | ||
* @returns {mat3} out | ||
*/ | ||
export function subtract(out, a, b) { | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
out[3] = a[3] - b[3]; | ||
out[4] = a[4] - b[4]; | ||
out[5] = a[5] - b[5]; | ||
out[6] = a[6] - b[6]; | ||
out[7] = a[7] - b[7]; | ||
out[8] = a[8] - b[8]; | ||
return out; | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
out[3] = a[3] - b[3]; | ||
out[4] = a[4] - b[4]; | ||
out[5] = a[5] - b[5]; | ||
out[6] = a[6] - b[6]; | ||
out[7] = a[7] - b[7]; | ||
out[8] = a[8] - b[8]; | ||
return out; | ||
} | ||
/** | ||
* Multiply each element of the matrix by a scalar. | ||
* | ||
* @param {mat3} out the receiving matrix | ||
* @param {ReadonlyMat3} a the matrix to scale | ||
* @param {Number} b amount to scale the matrix's elements by | ||
* @returns {mat3} out | ||
*/ | ||
export function multiplyScalar(out, a, b) { | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
out[4] = a[4] * b; | ||
out[5] = a[5] * b; | ||
out[6] = a[6] * b; | ||
out[7] = a[7] * b; | ||
out[8] = a[8] * b; | ||
return out; | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
out[4] = a[4] * b; | ||
out[5] = a[5] * b; | ||
out[6] = a[6] * b; | ||
out[7] = a[7] * b; | ||
out[8] = a[8] * b; | ||
return out; | ||
} | ||
/** | ||
* Adds two mat3's after multiplying each element of the second operand by a scalar value. | ||
* | ||
* @param {mat3} out the receiving vector | ||
* @param {ReadonlyMat3} a the first operand | ||
* @param {ReadonlyMat3} b the second operand | ||
* @param {Number} scale the amount to scale b's elements by before adding | ||
* @returns {mat3} out | ||
*/ | ||
export function multiplyScalarAndAdd(out, a, b, scale) { | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
out[3] = a[3] + b[3] * scale; | ||
out[4] = a[4] + b[4] * scale; | ||
out[5] = a[5] + b[5] * scale; | ||
out[6] = a[6] + b[6] * scale; | ||
out[7] = a[7] + b[7] * scale; | ||
out[8] = a[8] + b[8] * scale; | ||
return out; | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
out[3] = a[3] + b[3] * scale; | ||
out[4] = a[4] + b[4] * scale; | ||
out[5] = a[5] + b[5] * scale; | ||
out[6] = a[6] + b[6] * scale; | ||
out[7] = a[7] + b[7] * scale; | ||
out[8] = a[8] + b[8] * scale; | ||
return out; | ||
} | ||
/** | ||
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) | ||
* | ||
* @param {ReadonlyMat3} a The first matrix. | ||
* @param {ReadonlyMat3} b The second matrix. | ||
* @returns {Boolean} True if the matrices are equal, false otherwise. | ||
*/ | ||
export function exactEquals(a, b) { | ||
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && a[8] === b[8]; | ||
return (a[0] === b[0] && | ||
a[1] === b[1] && | ||
a[2] === b[2] && | ||
a[3] === b[3] && | ||
a[4] === b[4] && | ||
a[5] === b[5] && | ||
a[6] === b[6] && | ||
a[7] === b[7] && | ||
a[8] === b[8]); | ||
} | ||
/** | ||
* Returns whether or not the matrices have approximately the same elements in the same position. | ||
* | ||
* @param {ReadonlyMat3} a The first matrix. | ||
* @param {ReadonlyMat3} b The second matrix. | ||
* @returns {Boolean} True if the matrices are equal, false otherwise. | ||
*/ | ||
export function equals(a, b) { | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const a2 = a[2]; | ||
const a3 = a[3]; | ||
const a4 = a[4]; | ||
const a5 = a[5]; | ||
const a6 = a[6]; | ||
const a7 = a[7]; | ||
const a8 = a[8]; | ||
const b0 = b[0]; | ||
const b1 = b[1]; | ||
const b2 = b[2]; | ||
const b3 = b[3]; | ||
const b4 = b[4]; | ||
const b5 = b[5]; | ||
const b6 = b[6]; | ||
const b7 = b[7]; | ||
const b8 = b[8]; | ||
return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && Math.abs(a8 - b8) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)); | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const a2 = a[2]; | ||
const a3 = a[3]; | ||
const a4 = a[4]; | ||
const a5 = a[5]; | ||
const a6 = a[6]; | ||
const a7 = a[7]; | ||
const a8 = a[8]; | ||
const b0 = b[0]; | ||
const b1 = b[1]; | ||
const b2 = b[2]; | ||
const b3 = b[3]; | ||
const b4 = b[4]; | ||
const b5 = b[5]; | ||
const b6 = b[6]; | ||
const b7 = b[7]; | ||
const b8 = b[8]; | ||
return (Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | ||
Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | ||
Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && | ||
Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && | ||
Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && | ||
Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && | ||
Math.abs(a6 - b6) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && | ||
Math.abs(a7 - b7) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && | ||
Math.abs(a8 - b8) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8))); | ||
} | ||
/** | ||
* Alias for {@link mat3.multiply} | ||
* @function | ||
*/ | ||
export const mul = multiply; | ||
/** | ||
* Alias for {@link mat3.subtract} | ||
* @function | ||
*/ | ||
export const sub = subtract; | ||
//# sourceMappingURL=mat3.js.map |
@@ -550,2 +550,1 @@ /** | ||
export declare const sub: typeof subtract; | ||
//# sourceMappingURL=mat4.d.ts.map |
@@ -0,6 +1,177 @@ | ||
// @eslint-disable | ||
// @ts-nocheck | ||
import * as glMatrix from './common.js'; | ||
/** | ||
* 4x4 Matrix<br>Format: column-major, when typed out it looks like row-major<br>The matrices are being post multiplied. | ||
* @module mat4 | ||
*/ | ||
/** | ||
* Creates a new identity mat4 | ||
* | ||
* @returns a new 4x4 matrix | ||
*/ | ||
export function create() { | ||
const out = new glMatrix.ARRAY_TYPE(16); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
const out = new glMatrix.ARRAY_TYPE(16); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
} | ||
out[0] = 1; | ||
out[5] = 1; | ||
out[10] = 1; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a new mat4 initialized with values from an existing matrix | ||
* | ||
* @param {ReadonlyMat4} a matrix to clone | ||
* @returns {mat4} a new 4x4 matrix | ||
*/ | ||
export function clone(a) { | ||
const out = new glMatrix.ARRAY_TYPE(16); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[8] = a[8]; | ||
out[9] = a[9]; | ||
out[10] = a[10]; | ||
out[11] = a[11]; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
return out; | ||
} | ||
/** | ||
* Copy the values from one mat4 to another | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the source matrix | ||
* @returns {mat4} out | ||
*/ | ||
export function copy(out, a) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[8] = a[8]; | ||
out[9] = a[9]; | ||
out[10] = a[10]; | ||
out[11] = a[11]; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
return out; | ||
} | ||
/** | ||
* Create a new mat4 with the given values | ||
* | ||
* @param {Number} m00 Component in column 0, row 0 position (index 0) | ||
* @param {Number} m01 Component in column 0, row 1 position (index 1) | ||
* @param {Number} m02 Component in column 0, row 2 position (index 2) | ||
* @param {Number} m03 Component in column 0, row 3 position (index 3) | ||
* @param {Number} m10 Component in column 1, row 0 position (index 4) | ||
* @param {Number} m11 Component in column 1, row 1 position (index 5) | ||
* @param {Number} m12 Component in column 1, row 2 position (index 6) | ||
* @param {Number} m13 Component in column 1, row 3 position (index 7) | ||
* @param {Number} m20 Component in column 2, row 0 position (index 8) | ||
* @param {Number} m21 Component in column 2, row 1 position (index 9) | ||
* @param {Number} m22 Component in column 2, row 2 position (index 10) | ||
* @param {Number} m23 Component in column 2, row 3 position (index 11) | ||
* @param {Number} m30 Component in column 3, row 0 position (index 12) | ||
* @param {Number} m31 Component in column 3, row 1 position (index 13) | ||
* @param {Number} m32 Component in column 3, row 2 position (index 14) | ||
* @param {Number} m33 Component in column 3, row 3 position (index 15) | ||
* @returns {mat4} A new mat4 | ||
*/ | ||
export function fromValues(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { | ||
const out = new glMatrix.ARRAY_TYPE(16); | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m02; | ||
out[3] = m03; | ||
out[4] = m10; | ||
out[5] = m11; | ||
out[6] = m12; | ||
out[7] = m13; | ||
out[8] = m20; | ||
out[9] = m21; | ||
out[10] = m22; | ||
out[11] = m23; | ||
out[12] = m30; | ||
out[13] = m31; | ||
out[14] = m32; | ||
out[15] = m33; | ||
return out; | ||
} | ||
/** | ||
* Set the components of a mat4 to the given values | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {Number} m00 Component in column 0, row 0 position (index 0) | ||
* @param {Number} m01 Component in column 0, row 1 position (index 1) | ||
* @param {Number} m02 Component in column 0, row 2 position (index 2) | ||
* @param {Number} m03 Component in column 0, row 3 position (index 3) | ||
* @param {Number} m10 Component in column 1, row 0 position (index 4) | ||
* @param {Number} m11 Component in column 1, row 1 position (index 5) | ||
* @param {Number} m12 Component in column 1, row 2 position (index 6) | ||
* @param {Number} m13 Component in column 1, row 3 position (index 7) | ||
* @param {Number} m20 Component in column 2, row 0 position (index 8) | ||
* @param {Number} m21 Component in column 2, row 1 position (index 9) | ||
* @param {Number} m22 Component in column 2, row 2 position (index 10) | ||
* @param {Number} m23 Component in column 2, row 3 position (index 11) | ||
* @param {Number} m30 Component in column 3, row 0 position (index 12) | ||
* @param {Number} m31 Component in column 3, row 1 position (index 13) | ||
* @param {Number} m32 Component in column 3, row 2 position (index 14) | ||
* @param {Number} m33 Component in column 3, row 3 position (index 15) | ||
* @returns {mat4} out | ||
*/ | ||
export function set(out, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m02; | ||
out[3] = m03; | ||
out[4] = m10; | ||
out[5] = m11; | ||
out[6] = m12; | ||
out[7] = m13; | ||
out[8] = m20; | ||
out[9] = m21; | ||
out[10] = m22; | ||
out[11] = m23; | ||
out[12] = m30; | ||
out[13] = m31; | ||
out[14] = m32; | ||
out[15] = m33; | ||
return out; | ||
} | ||
/** | ||
* Set a mat4 to the identity matrix | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @returns {mat4} out | ||
*/ | ||
export function identity(out) { | ||
out[0] = 1; | ||
out[1] = 0; | ||
@@ -10,2 +181,3 @@ out[2] = 0; | ||
out[4] = 0; | ||
out[5] = 1; | ||
out[6] = 0; | ||
@@ -15,2 +187,3 @@ out[7] = 0; | ||
out[9] = 0; | ||
out[10] = 1; | ||
out[11] = 0; | ||
@@ -20,352 +193,404 @@ out[12] = 0; | ||
out[14] = 0; | ||
} | ||
out[0] = 1; | ||
out[5] = 1; | ||
out[10] = 1; | ||
out[15] = 1; | ||
return out; | ||
out[15] = 1; | ||
return out; | ||
} | ||
export function clone(a) { | ||
const out = new glMatrix.ARRAY_TYPE(16); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[8] = a[8]; | ||
out[9] = a[9]; | ||
out[10] = a[10]; | ||
out[11] = a[11]; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
return out; | ||
/** | ||
* Transpose the values of a mat4 | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the source matrix | ||
* @returns {mat4} out | ||
*/ | ||
export function transpose(out, a) { | ||
// If we are transposing ourselves we can skip a few steps but have to cache some values | ||
if (out === a) { | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a23 = a[11]; | ||
out[1] = a[4]; | ||
out[2] = a[8]; | ||
out[3] = a[12]; | ||
out[4] = a01; | ||
out[6] = a[9]; | ||
out[7] = a[13]; | ||
out[8] = a02; | ||
out[9] = a12; | ||
out[11] = a[14]; | ||
out[12] = a03; | ||
out[13] = a13; | ||
out[14] = a23; | ||
} | ||
else { | ||
out[0] = a[0]; | ||
out[1] = a[4]; | ||
out[2] = a[8]; | ||
out[3] = a[12]; | ||
out[4] = a[1]; | ||
out[5] = a[5]; | ||
out[6] = a[9]; | ||
out[7] = a[13]; | ||
out[8] = a[2]; | ||
out[9] = a[6]; | ||
out[10] = a[10]; | ||
out[11] = a[14]; | ||
out[12] = a[3]; | ||
out[13] = a[7]; | ||
out[14] = a[11]; | ||
out[15] = a[15]; | ||
} | ||
return out; | ||
} | ||
export function copy(out, a) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[8] = a[8]; | ||
out[9] = a[9]; | ||
out[10] = a[10]; | ||
out[11] = a[11]; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
return out; | ||
} | ||
export function fromValues(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { | ||
const out = new glMatrix.ARRAY_TYPE(16); | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m02; | ||
out[3] = m03; | ||
out[4] = m10; | ||
out[5] = m11; | ||
out[6] = m12; | ||
out[7] = m13; | ||
out[8] = m20; | ||
out[9] = m21; | ||
out[10] = m22; | ||
out[11] = m23; | ||
out[12] = m30; | ||
out[13] = m31; | ||
out[14] = m32; | ||
out[15] = m33; | ||
return out; | ||
} | ||
export function set(out, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m02; | ||
out[3] = m03; | ||
out[4] = m10; | ||
out[5] = m11; | ||
out[6] = m12; | ||
out[7] = m13; | ||
out[8] = m20; | ||
out[9] = m21; | ||
out[10] = m22; | ||
out[11] = m23; | ||
out[12] = m30; | ||
out[13] = m31; | ||
out[14] = m32; | ||
out[15] = m33; | ||
return out; | ||
} | ||
export function identity(out) { | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = 1; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[10] = 1; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
} | ||
export function transpose(out, a) { | ||
if (out === a) { | ||
/** | ||
* Inverts a mat4 | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the source matrix | ||
* @returns {mat4} out | ||
*/ | ||
export function invert(out, a) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
out[1] = a[4]; | ||
out[2] = a[8]; | ||
out[3] = a[12]; | ||
out[4] = a01; | ||
out[6] = a[9]; | ||
out[7] = a[13]; | ||
out[8] = a02; | ||
out[9] = a12; | ||
out[11] = a[14]; | ||
out[12] = a03; | ||
out[13] = a13; | ||
out[14] = a23; | ||
} else { | ||
out[0] = a[0]; | ||
out[1] = a[4]; | ||
out[2] = a[8]; | ||
out[3] = a[12]; | ||
out[4] = a[1]; | ||
out[5] = a[5]; | ||
out[6] = a[9]; | ||
out[7] = a[13]; | ||
out[8] = a[2]; | ||
out[9] = a[6]; | ||
out[10] = a[10]; | ||
out[11] = a[14]; | ||
out[12] = a[3]; | ||
out[13] = a[7]; | ||
out[14] = a[11]; | ||
out[15] = a[15]; | ||
} | ||
return out; | ||
const a30 = a[12]; | ||
const a31 = a[13]; | ||
const a32 = a[14]; | ||
const a33 = a[15]; | ||
const b00 = a00 * a11 - a01 * a10; | ||
const b01 = a00 * a12 - a02 * a10; | ||
const b02 = a00 * a13 - a03 * a10; | ||
const b03 = a01 * a12 - a02 * a11; | ||
const b04 = a01 * a13 - a03 * a11; | ||
const b05 = a02 * a13 - a03 * a12; | ||
const b06 = a20 * a31 - a21 * a30; | ||
const b07 = a20 * a32 - a22 * a30; | ||
const b08 = a20 * a33 - a23 * a30; | ||
const b09 = a21 * a32 - a22 * a31; | ||
const b10 = a21 * a33 - a23 * a31; | ||
const b11 = a22 * a33 - a23 * a32; | ||
// Calculate the determinant | ||
let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; | ||
if (!det) { | ||
return null; | ||
} | ||
det = 1.0 / det; | ||
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; | ||
out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det; | ||
out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det; | ||
out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det; | ||
out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det; | ||
out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det; | ||
out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det; | ||
out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det; | ||
out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det; | ||
out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det; | ||
out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det; | ||
out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det; | ||
out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det; | ||
out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det; | ||
out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det; | ||
out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det; | ||
return out; | ||
} | ||
export function invert(out, a) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
const a30 = a[12]; | ||
const a31 = a[13]; | ||
const a32 = a[14]; | ||
const a33 = a[15]; | ||
const b00 = a00 * a11 - a01 * a10; | ||
const b01 = a00 * a12 - a02 * a10; | ||
const b02 = a00 * a13 - a03 * a10; | ||
const b03 = a01 * a12 - a02 * a11; | ||
const b04 = a01 * a13 - a03 * a11; | ||
const b05 = a02 * a13 - a03 * a12; | ||
const b06 = a20 * a31 - a21 * a30; | ||
const b07 = a20 * a32 - a22 * a30; | ||
const b08 = a20 * a33 - a23 * a30; | ||
const b09 = a21 * a32 - a22 * a31; | ||
const b10 = a21 * a33 - a23 * a31; | ||
const b11 = a22 * a33 - a23 * a32; | ||
let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; | ||
if (!det) { | ||
return null; | ||
} | ||
det = 1.0 / det; | ||
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; | ||
out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det; | ||
out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det; | ||
out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det; | ||
out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det; | ||
out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det; | ||
out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det; | ||
out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det; | ||
out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det; | ||
out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det; | ||
out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det; | ||
out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det; | ||
out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det; | ||
out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det; | ||
out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det; | ||
out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det; | ||
return out; | ||
} | ||
/** | ||
* Calculates the adjugate of a mat4 | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the source matrix | ||
* @returns {mat4} out | ||
*/ | ||
export function adjoint(out, a) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
const a30 = a[12]; | ||
const a31 = a[13]; | ||
const a32 = a[14]; | ||
const a33 = a[15]; | ||
const b00 = a00 * a11 - a01 * a10; | ||
const b01 = a00 * a12 - a02 * a10; | ||
const b02 = a00 * a13 - a03 * a10; | ||
const b03 = a01 * a12 - a02 * a11; | ||
const b04 = a01 * a13 - a03 * a11; | ||
const b05 = a02 * a13 - a03 * a12; | ||
const b06 = a20 * a31 - a21 * a30; | ||
const b07 = a20 * a32 - a22 * a30; | ||
const b08 = a20 * a33 - a23 * a30; | ||
const b09 = a21 * a32 - a22 * a31; | ||
const b10 = a21 * a33 - a23 * a31; | ||
const b11 = a22 * a33 - a23 * a32; | ||
out[0] = a11 * b11 - a12 * b10 + a13 * b09; | ||
out[1] = a02 * b10 - a01 * b11 - a03 * b09; | ||
out[2] = a31 * b05 - a32 * b04 + a33 * b03; | ||
out[3] = a22 * b04 - a21 * b05 - a23 * b03; | ||
out[4] = a12 * b08 - a10 * b11 - a13 * b07; | ||
out[5] = a00 * b11 - a02 * b08 + a03 * b07; | ||
out[6] = a32 * b02 - a30 * b05 - a33 * b01; | ||
out[7] = a20 * b05 - a22 * b02 + a23 * b01; | ||
out[8] = a10 * b10 - a11 * b08 + a13 * b06; | ||
out[9] = a01 * b08 - a00 * b10 - a03 * b06; | ||
out[10] = a30 * b04 - a31 * b02 + a33 * b00; | ||
out[11] = a21 * b02 - a20 * b04 - a23 * b00; | ||
out[12] = a11 * b07 - a10 * b09 - a12 * b06; | ||
out[13] = a00 * b09 - a01 * b07 + a02 * b06; | ||
out[14] = a31 * b01 - a30 * b03 - a32 * b00; | ||
out[15] = a20 * b03 - a21 * b01 + a22 * b00; | ||
return out; | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
const a30 = a[12]; | ||
const a31 = a[13]; | ||
const a32 = a[14]; | ||
const a33 = a[15]; | ||
const b00 = a00 * a11 - a01 * a10; | ||
const b01 = a00 * a12 - a02 * a10; | ||
const b02 = a00 * a13 - a03 * a10; | ||
const b03 = a01 * a12 - a02 * a11; | ||
const b04 = a01 * a13 - a03 * a11; | ||
const b05 = a02 * a13 - a03 * a12; | ||
const b06 = a20 * a31 - a21 * a30; | ||
const b07 = a20 * a32 - a22 * a30; | ||
const b08 = a20 * a33 - a23 * a30; | ||
const b09 = a21 * a32 - a22 * a31; | ||
const b10 = a21 * a33 - a23 * a31; | ||
const b11 = a22 * a33 - a23 * a32; | ||
out[0] = a11 * b11 - a12 * b10 + a13 * b09; | ||
out[1] = a02 * b10 - a01 * b11 - a03 * b09; | ||
out[2] = a31 * b05 - a32 * b04 + a33 * b03; | ||
out[3] = a22 * b04 - a21 * b05 - a23 * b03; | ||
out[4] = a12 * b08 - a10 * b11 - a13 * b07; | ||
out[5] = a00 * b11 - a02 * b08 + a03 * b07; | ||
out[6] = a32 * b02 - a30 * b05 - a33 * b01; | ||
out[7] = a20 * b05 - a22 * b02 + a23 * b01; | ||
out[8] = a10 * b10 - a11 * b08 + a13 * b06; | ||
out[9] = a01 * b08 - a00 * b10 - a03 * b06; | ||
out[10] = a30 * b04 - a31 * b02 + a33 * b00; | ||
out[11] = a21 * b02 - a20 * b04 - a23 * b00; | ||
out[12] = a11 * b07 - a10 * b09 - a12 * b06; | ||
out[13] = a00 * b09 - a01 * b07 + a02 * b06; | ||
out[14] = a31 * b01 - a30 * b03 - a32 * b00; | ||
out[15] = a20 * b03 - a21 * b01 + a22 * b00; | ||
return out; | ||
} | ||
/** | ||
* Calculates the determinant of a mat4 | ||
* | ||
* @param {ReadonlyMat4} a the source matrix | ||
* @returns {Number} determinant of a | ||
*/ | ||
export function determinant(a) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
const a30 = a[12]; | ||
const a31 = a[13]; | ||
const a32 = a[14]; | ||
const a33 = a[15]; | ||
const b0 = a00 * a11 - a01 * a10; | ||
const b1 = a00 * a12 - a02 * a10; | ||
const b2 = a01 * a12 - a02 * a11; | ||
const b3 = a20 * a31 - a21 * a30; | ||
const b4 = a20 * a32 - a22 * a30; | ||
const b5 = a21 * a32 - a22 * a31; | ||
const b6 = a00 * b5 - a01 * b4 + a02 * b3; | ||
const b7 = a10 * b5 - a11 * b4 + a12 * b3; | ||
const b8 = a20 * b2 - a21 * b1 + a22 * b0; | ||
const b9 = a30 * b2 - a31 * b1 + a32 * b0; | ||
return a13 * b6 - a03 * b7 + a33 * b8 - a23 * b9; | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
const a30 = a[12]; | ||
const a31 = a[13]; | ||
const a32 = a[14]; | ||
const a33 = a[15]; | ||
const b0 = a00 * a11 - a01 * a10; | ||
const b1 = a00 * a12 - a02 * a10; | ||
const b2 = a01 * a12 - a02 * a11; | ||
const b3 = a20 * a31 - a21 * a30; | ||
const b4 = a20 * a32 - a22 * a30; | ||
const b5 = a21 * a32 - a22 * a31; | ||
const b6 = a00 * b5 - a01 * b4 + a02 * b3; | ||
const b7 = a10 * b5 - a11 * b4 + a12 * b3; | ||
const b8 = a20 * b2 - a21 * b1 + a22 * b0; | ||
const b9 = a30 * b2 - a31 * b1 + a32 * b0; | ||
// Calculate the determinant | ||
return a13 * b6 - a03 * b7 + a33 * b8 - a23 * b9; | ||
} | ||
/** | ||
* Multiplies two mat4s | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the first operand | ||
* @param {ReadonlyMat4} b the second operand | ||
* @returns {mat4} out | ||
*/ | ||
export function multiply(out, a, b) { | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
const a30 = a[12]; | ||
const a31 = a[13]; | ||
const a32 = a[14]; | ||
const a33 = a[15]; | ||
let b0 = b[0]; | ||
let b1 = b[1]; | ||
let b2 = b[2]; | ||
let b3 = b[3]; | ||
out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; | ||
out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; | ||
out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; | ||
out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; | ||
b0 = b[4]; | ||
b1 = b[5]; | ||
b2 = b[6]; | ||
b3 = b[7]; | ||
out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; | ||
out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; | ||
out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; | ||
out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; | ||
b0 = b[8]; | ||
b1 = b[9]; | ||
b2 = b[10]; | ||
b3 = b[11]; | ||
out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; | ||
out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; | ||
out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; | ||
out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; | ||
b0 = b[12]; | ||
b1 = b[13]; | ||
b2 = b[14]; | ||
b3 = b[15]; | ||
out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; | ||
out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; | ||
out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; | ||
out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; | ||
return out; | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
const a30 = a[12]; | ||
const a31 = a[13]; | ||
const a32 = a[14]; | ||
const a33 = a[15]; | ||
// Cache only the current line of the second matrix | ||
let b0 = b[0]; | ||
let b1 = b[1]; | ||
let b2 = b[2]; | ||
let b3 = b[3]; | ||
out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; | ||
out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; | ||
out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; | ||
out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; | ||
b0 = b[4]; | ||
b1 = b[5]; | ||
b2 = b[6]; | ||
b3 = b[7]; | ||
out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; | ||
out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; | ||
out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; | ||
out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; | ||
b0 = b[8]; | ||
b1 = b[9]; | ||
b2 = b[10]; | ||
b3 = b[11]; | ||
out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; | ||
out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; | ||
out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; | ||
out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; | ||
b0 = b[12]; | ||
b1 = b[13]; | ||
b2 = b[14]; | ||
b3 = b[15]; | ||
out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; | ||
out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; | ||
out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; | ||
out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; | ||
return out; | ||
} | ||
/** | ||
* Translate a mat4 by the given vector | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the matrix to translate | ||
* @param {ReadonlyVec3} v vector to translate by | ||
* @returns {mat4} out | ||
*/ | ||
export function translate(out, a, v) { | ||
const x = v[0]; | ||
const y = v[1]; | ||
const z = v[2]; | ||
let a00; | ||
let a01; | ||
let a02; | ||
let a03; | ||
let a10; | ||
let a11; | ||
let a12; | ||
let a13; | ||
let a20; | ||
let a21; | ||
let a22; | ||
let a23; | ||
if (a === out) { | ||
out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; | ||
out[13] = a[1] * x + a[5] * y + a[9] * z + a[13]; | ||
out[14] = a[2] * x + a[6] * y + a[10] * z + a[14]; | ||
out[15] = a[3] * x + a[7] * y + a[11] * z + a[15]; | ||
} else { | ||
const x = v[0]; | ||
const y = v[1]; | ||
const z = v[2]; | ||
let a00; | ||
let a01; | ||
let a02; | ||
let a03; | ||
let a10; | ||
let a11; | ||
let a12; | ||
let a13; | ||
let a20; | ||
let a21; | ||
let a22; | ||
let a23; | ||
if (a === out) { | ||
out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; | ||
out[13] = a[1] * x + a[5] * y + a[9] * z + a[13]; | ||
out[14] = a[2] * x + a[6] * y + a[10] * z + a[14]; | ||
out[15] = a[3] * x + a[7] * y + a[11] * z + a[15]; | ||
} | ||
else { | ||
a00 = a[0]; | ||
a01 = a[1]; | ||
a02 = a[2]; | ||
a03 = a[3]; | ||
a10 = a[4]; | ||
a11 = a[5]; | ||
a12 = a[6]; | ||
a13 = a[7]; | ||
a20 = a[8]; | ||
a21 = a[9]; | ||
a22 = a[10]; | ||
a23 = a[11]; | ||
out[0] = a00; | ||
out[1] = a01; | ||
out[2] = a02; | ||
out[3] = a03; | ||
out[4] = a10; | ||
out[5] = a11; | ||
out[6] = a12; | ||
out[7] = a13; | ||
out[8] = a20; | ||
out[9] = a21; | ||
out[10] = a22; | ||
out[11] = a23; | ||
out[12] = a00 * x + a10 * y + a20 * z + a[12]; | ||
out[13] = a01 * x + a11 * y + a21 * z + a[13]; | ||
out[14] = a02 * x + a12 * y + a22 * z + a[14]; | ||
out[15] = a03 * x + a13 * y + a23 * z + a[15]; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Scales the mat4 by the dimensions in the given vec3 not using vectorization | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the matrix to scale | ||
* @param {ReadonlyVec3} v the vec3 to scale the matrix by | ||
* @returns {mat4} out | ||
**/ | ||
export function scale(out, a, v) { | ||
const x = v[0]; | ||
const y = v[1]; | ||
const z = v[2]; | ||
out[0] = a[0] * x; | ||
out[1] = a[1] * x; | ||
out[2] = a[2] * x; | ||
out[3] = a[3] * x; | ||
out[4] = a[4] * y; | ||
out[5] = a[5] * y; | ||
out[6] = a[6] * y; | ||
out[7] = a[7] * y; | ||
out[8] = a[8] * z; | ||
out[9] = a[9] * z; | ||
out[10] = a[10] * z; | ||
out[11] = a[11] * z; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
return out; | ||
} | ||
/** | ||
* Rotates a mat4 by the given angle around the given axis | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the matrix to rotate | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @param {ReadonlyVec3} axis the axis to rotate around | ||
* @returns {mat4} out | ||
*/ | ||
export function rotate(out, a, rad, axis) { | ||
let x = axis[0]; | ||
let y = axis[1]; | ||
let z = axis[2]; | ||
let len = Math.sqrt(x * x + y * y + z * z); | ||
let c; | ||
let s; | ||
let t; | ||
let a00; | ||
let a01; | ||
let a02; | ||
let a03; | ||
let a10; | ||
let a11; | ||
let a12; | ||
let a13; | ||
let a20; | ||
let a21; | ||
let a22; | ||
let a23; | ||
let b00; | ||
let b01; | ||
let b02; | ||
let b10; | ||
let b11; | ||
let b12; | ||
let b20; | ||
let b21; | ||
let b22; | ||
if (len < glMatrix.EPSILON) { | ||
return null; | ||
} | ||
len = 1 / len; | ||
x *= len; | ||
y *= len; | ||
z *= len; | ||
s = Math.sin(rad); | ||
c = Math.cos(rad); | ||
t = 1 - c; | ||
a00 = a[0]; | ||
@@ -383,1077 +608,1392 @@ a01 = a[1]; | ||
a23 = a[11]; | ||
out[0] = a00; | ||
out[1] = a01; | ||
out[2] = a02; | ||
out[3] = a03; | ||
out[4] = a10; | ||
out[5] = a11; | ||
out[6] = a12; | ||
out[7] = a13; | ||
out[8] = a20; | ||
out[9] = a21; | ||
out[10] = a22; | ||
out[11] = a23; | ||
out[12] = a00 * x + a10 * y + a20 * z + a[12]; | ||
out[13] = a01 * x + a11 * y + a21 * z + a[13]; | ||
out[14] = a02 * x + a12 * y + a22 * z + a[14]; | ||
out[15] = a03 * x + a13 * y + a23 * z + a[15]; | ||
} | ||
return out; | ||
// Construct the elements of the rotation matrix | ||
b00 = x * x * t + c; | ||
b01 = y * x * t + z * s; | ||
b02 = z * x * t - y * s; | ||
b10 = x * y * t - z * s; | ||
b11 = y * y * t + c; | ||
b12 = z * y * t + x * s; | ||
b20 = x * z * t + y * s; | ||
b21 = y * z * t - x * s; | ||
b22 = z * z * t + c; | ||
// Perform rotation-specific matrix multiplication | ||
out[0] = a00 * b00 + a10 * b01 + a20 * b02; | ||
out[1] = a01 * b00 + a11 * b01 + a21 * b02; | ||
out[2] = a02 * b00 + a12 * b01 + a22 * b02; | ||
out[3] = a03 * b00 + a13 * b01 + a23 * b02; | ||
out[4] = a00 * b10 + a10 * b11 + a20 * b12; | ||
out[5] = a01 * b10 + a11 * b11 + a21 * b12; | ||
out[6] = a02 * b10 + a12 * b11 + a22 * b12; | ||
out[7] = a03 * b10 + a13 * b11 + a23 * b12; | ||
out[8] = a00 * b20 + a10 * b21 + a20 * b22; | ||
out[9] = a01 * b20 + a11 * b21 + a21 * b22; | ||
out[10] = a02 * b20 + a12 * b21 + a22 * b22; | ||
out[11] = a03 * b20 + a13 * b21 + a23 * b22; | ||
if (a !== out) { | ||
// If the source and destination differ, copy the unchanged last row | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
} | ||
return out; | ||
} | ||
export function scale(out, a, v) { | ||
const x = v[0]; | ||
const y = v[1]; | ||
const z = v[2]; | ||
out[0] = a[0] * x; | ||
out[1] = a[1] * x; | ||
out[2] = a[2] * x; | ||
out[3] = a[3] * x; | ||
out[4] = a[4] * y; | ||
out[5] = a[5] * y; | ||
out[6] = a[6] * y; | ||
out[7] = a[7] * y; | ||
out[8] = a[8] * z; | ||
out[9] = a[9] * z; | ||
out[10] = a[10] * z; | ||
out[11] = a[11] * z; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
return out; | ||
} | ||
export function rotate(out, a, rad, axis) { | ||
let x = axis[0]; | ||
let y = axis[1]; | ||
let z = axis[2]; | ||
let len = Math.sqrt(x * x + y * y + z * z); | ||
let c; | ||
let s; | ||
let t; | ||
let a00; | ||
let a01; | ||
let a02; | ||
let a03; | ||
let a10; | ||
let a11; | ||
let a12; | ||
let a13; | ||
let a20; | ||
let a21; | ||
let a22; | ||
let a23; | ||
let b00; | ||
let b01; | ||
let b02; | ||
let b10; | ||
let b11; | ||
let b12; | ||
let b20; | ||
let b21; | ||
let b22; | ||
if (len < glMatrix.EPSILON) { | ||
return null; | ||
} | ||
len = 1 / len; | ||
x *= len; | ||
y *= len; | ||
z *= len; | ||
s = Math.sin(rad); | ||
c = Math.cos(rad); | ||
t = 1 - c; | ||
a00 = a[0]; | ||
a01 = a[1]; | ||
a02 = a[2]; | ||
a03 = a[3]; | ||
a10 = a[4]; | ||
a11 = a[5]; | ||
a12 = a[6]; | ||
a13 = a[7]; | ||
a20 = a[8]; | ||
a21 = a[9]; | ||
a22 = a[10]; | ||
a23 = a[11]; | ||
b00 = x * x * t + c; | ||
b01 = y * x * t + z * s; | ||
b02 = z * x * t - y * s; | ||
b10 = x * y * t - z * s; | ||
b11 = y * y * t + c; | ||
b12 = z * y * t + x * s; | ||
b20 = x * z * t + y * s; | ||
b21 = y * z * t - x * s; | ||
b22 = z * z * t + c; | ||
out[0] = a00 * b00 + a10 * b01 + a20 * b02; | ||
out[1] = a01 * b00 + a11 * b01 + a21 * b02; | ||
out[2] = a02 * b00 + a12 * b01 + a22 * b02; | ||
out[3] = a03 * b00 + a13 * b01 + a23 * b02; | ||
out[4] = a00 * b10 + a10 * b11 + a20 * b12; | ||
out[5] = a01 * b10 + a11 * b11 + a21 * b12; | ||
out[6] = a02 * b10 + a12 * b11 + a22 * b12; | ||
out[7] = a03 * b10 + a13 * b11 + a23 * b12; | ||
out[8] = a00 * b20 + a10 * b21 + a20 * b22; | ||
out[9] = a01 * b20 + a11 * b21 + a21 * b22; | ||
out[10] = a02 * b20 + a12 * b21 + a22 * b22; | ||
out[11] = a03 * b20 + a13 * b21 + a23 * b22; | ||
if (a !== out) { | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Rotates a matrix by the given angle around the X axis | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the matrix to rotate | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat4} out | ||
*/ | ||
export function rotateX(out, a, rad) { | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
if (a !== out) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
} | ||
out[4] = a10 * c + a20 * s; | ||
out[5] = a11 * c + a21 * s; | ||
out[6] = a12 * c + a22 * s; | ||
out[7] = a13 * c + a23 * s; | ||
out[8] = a20 * c - a10 * s; | ||
out[9] = a21 * c - a11 * s; | ||
out[10] = a22 * c - a12 * s; | ||
out[11] = a23 * c - a13 * s; | ||
return out; | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
if (a !== out) { | ||
// If the source and destination differ, copy the unchanged rows | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
} | ||
// Perform axis-specific matrix multiplication | ||
out[4] = a10 * c + a20 * s; | ||
out[5] = a11 * c + a21 * s; | ||
out[6] = a12 * c + a22 * s; | ||
out[7] = a13 * c + a23 * s; | ||
out[8] = a20 * c - a10 * s; | ||
out[9] = a21 * c - a11 * s; | ||
out[10] = a22 * c - a12 * s; | ||
out[11] = a23 * c - a13 * s; | ||
return out; | ||
} | ||
/** | ||
* Rotates a matrix by the given angle around the Y axis | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the matrix to rotate | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat4} out | ||
*/ | ||
export function rotateY(out, a, rad) { | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
if (a !== out) { | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
} | ||
out[0] = a00 * c - a20 * s; | ||
out[1] = a01 * c - a21 * s; | ||
out[2] = a02 * c - a22 * s; | ||
out[3] = a03 * c - a23 * s; | ||
out[8] = a00 * s + a20 * c; | ||
out[9] = a01 * s + a21 * c; | ||
out[10] = a02 * s + a22 * c; | ||
out[11] = a03 * s + a23 * c; | ||
return out; | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a20 = a[8]; | ||
const a21 = a[9]; | ||
const a22 = a[10]; | ||
const a23 = a[11]; | ||
if (a !== out) { | ||
// If the source and destination differ, copy the unchanged rows | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
} | ||
// Perform axis-specific matrix multiplication | ||
out[0] = a00 * c - a20 * s; | ||
out[1] = a01 * c - a21 * s; | ||
out[2] = a02 * c - a22 * s; | ||
out[3] = a03 * c - a23 * s; | ||
out[8] = a00 * s + a20 * c; | ||
out[9] = a01 * s + a21 * c; | ||
out[10] = a02 * s + a22 * c; | ||
out[11] = a03 * s + a23 * c; | ||
return out; | ||
} | ||
/** | ||
* Rotates a matrix by the given angle around the Z axis | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the matrix to rotate | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat4} out | ||
*/ | ||
export function rotateZ(out, a, rad) { | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
if (a !== out) { | ||
out[8] = a[8]; | ||
out[9] = a[9]; | ||
out[10] = a[10]; | ||
out[11] = a[11]; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
} | ||
out[0] = a00 * c + a10 * s; | ||
out[1] = a01 * c + a11 * s; | ||
out[2] = a02 * c + a12 * s; | ||
out[3] = a03 * c + a13 * s; | ||
out[4] = a10 * c - a00 * s; | ||
out[5] = a11 * c - a01 * s; | ||
out[6] = a12 * c - a02 * s; | ||
out[7] = a13 * c - a03 * s; | ||
return out; | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
const a00 = a[0]; | ||
const a01 = a[1]; | ||
const a02 = a[2]; | ||
const a03 = a[3]; | ||
const a10 = a[4]; | ||
const a11 = a[5]; | ||
const a12 = a[6]; | ||
const a13 = a[7]; | ||
if (a !== out) { | ||
// If the source and destination differ, copy the unchanged last row | ||
out[8] = a[8]; | ||
out[9] = a[9]; | ||
out[10] = a[10]; | ||
out[11] = a[11]; | ||
out[12] = a[12]; | ||
out[13] = a[13]; | ||
out[14] = a[14]; | ||
out[15] = a[15]; | ||
} | ||
// Perform axis-specific matrix multiplication | ||
out[0] = a00 * c + a10 * s; | ||
out[1] = a01 * c + a11 * s; | ||
out[2] = a02 * c + a12 * s; | ||
out[3] = a03 * c + a13 * s; | ||
out[4] = a10 * c - a00 * s; | ||
out[5] = a11 * c - a01 * s; | ||
out[6] = a12 * c - a02 * s; | ||
out[7] = a13 * c - a03 * s; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a vector translation | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat4.identity(dest); | ||
* mat4.translate(dest, dest, vec); | ||
* | ||
* @param {mat4} out mat4 receiving operation result | ||
* @param {ReadonlyVec3} v Translation vector | ||
* @returns {mat4} out | ||
*/ | ||
export function fromTranslation(out, v) { | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = 1; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[10] = 1; | ||
out[11] = 0; | ||
out[12] = v[0]; | ||
out[13] = v[1]; | ||
out[14] = v[2]; | ||
out[15] = 1; | ||
return out; | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = 1; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[10] = 1; | ||
out[11] = 0; | ||
out[12] = v[0]; | ||
out[13] = v[1]; | ||
out[14] = v[2]; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a vector scaling | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat4.identity(dest); | ||
* mat4.scale(dest, dest, vec); | ||
* | ||
* @param {mat4} out mat4 receiving operation result | ||
* @param {ReadonlyVec3} v Scaling vector | ||
* @returns {mat4} out | ||
*/ | ||
export function fromScaling(out, v) { | ||
out[0] = v[0]; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = v[1]; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[10] = v[2]; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
out[0] = v[0]; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = v[1]; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[10] = v[2]; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a given angle around a given axis | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat4.identity(dest); | ||
* mat4.rotate(dest, dest, rad, axis); | ||
* | ||
* @param {mat4} out mat4 receiving operation result | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @param {ReadonlyVec3} axis the axis to rotate around | ||
* @returns {mat4} out | ||
*/ | ||
export function fromRotation(out, rad, axis) { | ||
let x = axis[0]; | ||
let y = axis[1]; | ||
let z = axis[2]; | ||
let len = Math.sqrt(x * x + y * y + z * z); | ||
let c; | ||
let s; | ||
let t; | ||
if (len < glMatrix.EPSILON) { | ||
return null; | ||
} | ||
len = 1 / len; | ||
x *= len; | ||
y *= len; | ||
z *= len; | ||
s = Math.sin(rad); | ||
c = Math.cos(rad); | ||
t = 1 - c; | ||
out[0] = x * x * t + c; | ||
out[1] = y * x * t + z * s; | ||
out[2] = z * x * t - y * s; | ||
out[3] = 0; | ||
out[4] = x * y * t - z * s; | ||
out[5] = y * y * t + c; | ||
out[6] = z * y * t + x * s; | ||
out[7] = 0; | ||
out[8] = x * z * t + y * s; | ||
out[9] = y * z * t - x * s; | ||
out[10] = z * z * t + c; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
let x = axis[0]; | ||
let y = axis[1]; | ||
let z = axis[2]; | ||
let len = Math.sqrt(x * x + y * y + z * z); | ||
let c; | ||
let s; | ||
let t; | ||
if (len < glMatrix.EPSILON) { | ||
return null; | ||
} | ||
len = 1 / len; | ||
x *= len; | ||
y *= len; | ||
z *= len; | ||
s = Math.sin(rad); | ||
c = Math.cos(rad); | ||
t = 1 - c; | ||
// Perform rotation-specific matrix multiplication | ||
out[0] = x * x * t + c; | ||
out[1] = y * x * t + z * s; | ||
out[2] = z * x * t - y * s; | ||
out[3] = 0; | ||
out[4] = x * y * t - z * s; | ||
out[5] = y * y * t + c; | ||
out[6] = z * y * t + x * s; | ||
out[7] = 0; | ||
out[8] = x * z * t + y * s; | ||
out[9] = y * z * t - x * s; | ||
out[10] = z * z * t + c; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from the given angle around the X axis | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat4.identity(dest); | ||
* mat4.rotateX(dest, dest, rad); | ||
* | ||
* @param {mat4} out mat4 receiving operation result | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat4} out | ||
*/ | ||
export function fromXRotation(out, rad) { | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = c; | ||
out[6] = s; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = -s; | ||
out[10] = c; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
// Perform axis-specific matrix multiplication | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = c; | ||
out[6] = s; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = -s; | ||
out[10] = c; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from the given angle around the Y axis | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat4.identity(dest); | ||
* mat4.rotateY(dest, dest, rad); | ||
* | ||
* @param {mat4} out mat4 receiving operation result | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat4} out | ||
*/ | ||
export function fromYRotation(out, rad) { | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
out[0] = c; | ||
out[1] = 0; | ||
out[2] = -s; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = 1; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = s; | ||
out[9] = 0; | ||
out[10] = c; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
// Perform axis-specific matrix multiplication | ||
out[0] = c; | ||
out[1] = 0; | ||
out[2] = -s; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = 1; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = s; | ||
out[9] = 0; | ||
out[10] = c; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from the given angle around the Z axis | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat4.identity(dest); | ||
* mat4.rotateZ(dest, dest, rad); | ||
* | ||
* @param {mat4} out mat4 receiving operation result | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat4} out | ||
*/ | ||
export function fromZRotation(out, rad) { | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
out[0] = c; | ||
out[1] = s; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = -s; | ||
out[5] = c; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[10] = 1; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
const s = Math.sin(rad); | ||
const c = Math.cos(rad); | ||
// Perform axis-specific matrix multiplication | ||
out[0] = c; | ||
out[1] = s; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = -s; | ||
out[5] = c; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[10] = 1; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a quaternion rotation and vector translation | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat4.identity(dest); | ||
* mat4.translate(dest, vec); | ||
* let quatMat = mat4.create(); | ||
* quat4.toMat4(quat, quatMat); | ||
* mat4.multiply(dest, quatMat); | ||
* | ||
* @param {mat4} out mat4 receiving operation result | ||
* @param {quat4} q Rotation quaternion | ||
* @param {ReadonlyVec3} v Translation vector | ||
* @returns {mat4} out | ||
*/ | ||
export function fromRotationTranslation(out, q, v) { | ||
const x = q[0]; | ||
const y = q[1]; | ||
const z = q[2]; | ||
const w = q[3]; | ||
const x2 = x + x; | ||
const y2 = y + y; | ||
const z2 = z + z; | ||
const xx = x * x2; | ||
const xy = x * y2; | ||
const xz = x * z2; | ||
const yy = y * y2; | ||
const yz = y * z2; | ||
const zz = z * z2; | ||
const wx = w * x2; | ||
const wy = w * y2; | ||
const wz = w * z2; | ||
out[0] = 1 - (yy + zz); | ||
out[1] = xy + wz; | ||
out[2] = xz - wy; | ||
out[3] = 0; | ||
out[4] = xy - wz; | ||
out[5] = 1 - (xx + zz); | ||
out[6] = yz + wx; | ||
out[7] = 0; | ||
out[8] = xz + wy; | ||
out[9] = yz - wx; | ||
out[10] = 1 - (xx + yy); | ||
out[11] = 0; | ||
out[12] = v[0]; | ||
out[13] = v[1]; | ||
out[14] = v[2]; | ||
out[15] = 1; | ||
return out; | ||
// Quaternion math | ||
const x = q[0]; | ||
const y = q[1]; | ||
const z = q[2]; | ||
const w = q[3]; | ||
const x2 = x + x; | ||
const y2 = y + y; | ||
const z2 = z + z; | ||
const xx = x * x2; | ||
const xy = x * y2; | ||
const xz = x * z2; | ||
const yy = y * y2; | ||
const yz = y * z2; | ||
const zz = z * z2; | ||
const wx = w * x2; | ||
const wy = w * y2; | ||
const wz = w * z2; | ||
out[0] = 1 - (yy + zz); | ||
out[1] = xy + wz; | ||
out[2] = xz - wy; | ||
out[3] = 0; | ||
out[4] = xy - wz; | ||
out[5] = 1 - (xx + zz); | ||
out[6] = yz + wx; | ||
out[7] = 0; | ||
out[8] = xz + wy; | ||
out[9] = yz - wx; | ||
out[10] = 1 - (xx + yy); | ||
out[11] = 0; | ||
out[12] = v[0]; | ||
out[13] = v[1]; | ||
out[14] = v[2]; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a new mat4 from a dual quat. | ||
* | ||
* @param {mat4} out Matrix | ||
* @param {ReadonlyQuat2} a Dual Quaternion | ||
* @returns {mat4} mat4 receiving operation result | ||
*/ | ||
export function fromQuat2(out, a) { | ||
const translation = new glMatrix.ARRAY_TYPE(3); | ||
const bx = -a[0]; | ||
const by = -a[1]; | ||
const bz = -a[2]; | ||
const bw = a[3]; | ||
const ax = a[4]; | ||
const ay = a[5]; | ||
const az = a[6]; | ||
const aw = a[7]; | ||
const magnitude = bx * bx + by * by + bz * bz + bw * bw; | ||
if (magnitude > 0) { | ||
translation[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2 / magnitude; | ||
translation[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2 / magnitude; | ||
translation[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2 / magnitude; | ||
} else { | ||
translation[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2; | ||
translation[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2; | ||
translation[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2; | ||
} | ||
fromRotationTranslation(out, a, translation); | ||
return out; | ||
const translation = new glMatrix.ARRAY_TYPE(3); | ||
const bx = -a[0]; | ||
const by = -a[1]; | ||
const bz = -a[2]; | ||
const bw = a[3]; | ||
const ax = a[4]; | ||
const ay = a[5]; | ||
const az = a[6]; | ||
const aw = a[7]; | ||
const magnitude = bx * bx + by * by + bz * bz + bw * bw; | ||
// Only scale if it makes sense | ||
if (magnitude > 0) { | ||
translation[0] = ((ax * bw + aw * bx + ay * bz - az * by) * 2) / magnitude; | ||
translation[1] = ((ay * bw + aw * by + az * bx - ax * bz) * 2) / magnitude; | ||
translation[2] = ((az * bw + aw * bz + ax * by - ay * bx) * 2) / magnitude; | ||
} | ||
else { | ||
translation[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2; | ||
translation[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2; | ||
translation[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2; | ||
} | ||
fromRotationTranslation(out, a, translation); | ||
return out; | ||
} | ||
/** | ||
* Returns the translation vector component of a transformation | ||
* matrix. If a matrix is built with fromRotationTranslation, | ||
* the returned vector will be the same as the translation vector | ||
* originally supplied. | ||
* @param {vec3} out Vector to receive translation component | ||
* @param {ReadonlyMat4} mat Matrix to be decomposed (input) | ||
* @return {vec3} out | ||
*/ | ||
export function getTranslation(out, mat) { | ||
out[0] = mat[12]; | ||
out[1] = mat[13]; | ||
out[2] = mat[14]; | ||
return out; | ||
out[0] = mat[12]; | ||
out[1] = mat[13]; | ||
out[2] = mat[14]; | ||
return out; | ||
} | ||
/** | ||
* Returns the scaling factor component of a transformation | ||
* matrix. If a matrix is built with fromRotationTranslationScale | ||
* with a normalized Quaternion paramter, the returned vector will be | ||
* the same as the scaling vector | ||
* originally supplied. | ||
* @param {vec3} out Vector to receive scaling factor component | ||
* @param {ReadonlyMat4} mat Matrix to be decomposed (input) | ||
* @return {vec3} out | ||
*/ | ||
export function getScaling(out, mat) { | ||
const m11 = mat[0]; | ||
const m12 = mat[1]; | ||
const m13 = mat[2]; | ||
const m21 = mat[4]; | ||
const m22 = mat[5]; | ||
const m23 = mat[6]; | ||
const m31 = mat[8]; | ||
const m32 = mat[9]; | ||
const m33 = mat[10]; | ||
out[0] = Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13); | ||
out[1] = Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23); | ||
out[2] = Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33); | ||
return out; | ||
const m11 = mat[0]; | ||
const m12 = mat[1]; | ||
const m13 = mat[2]; | ||
const m21 = mat[4]; | ||
const m22 = mat[5]; | ||
const m23 = mat[6]; | ||
const m31 = mat[8]; | ||
const m32 = mat[9]; | ||
const m33 = mat[10]; | ||
out[0] = Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13); | ||
out[1] = Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23); | ||
out[2] = Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33); | ||
return out; | ||
} | ||
/** | ||
* Returns a quaternion representing the rotational component | ||
* of a transformation matrix. If a matrix is built with | ||
* fromRotationTranslation, the returned quaternion will be the | ||
* same as the quaternion originally supplied. | ||
* @param {quat} out Quaternion to receive the rotation component | ||
* @param {ReadonlyMat4} mat Matrix to be decomposed (input) | ||
* @return {quat} out | ||
*/ | ||
export function getRotation(out, mat) { | ||
const scaling = new glMatrix.ARRAY_TYPE(3); | ||
getScaling(scaling, mat); | ||
const is1 = 1 / scaling[0]; | ||
const is2 = 1 / scaling[1]; | ||
const is3 = 1 / scaling[2]; | ||
const sm11 = mat[0] * is1; | ||
const sm12 = mat[1] * is2; | ||
const sm13 = mat[2] * is3; | ||
const sm21 = mat[4] * is1; | ||
const sm22 = mat[5] * is2; | ||
const sm23 = mat[6] * is3; | ||
const sm31 = mat[8] * is1; | ||
const sm32 = mat[9] * is2; | ||
const sm33 = mat[10] * is3; | ||
const trace = sm11 + sm22 + sm33; | ||
let S = 0; | ||
if (trace > 0) { | ||
S = Math.sqrt(trace + 1.0) * 2; | ||
out[3] = 0.25 * S; | ||
out[0] = (sm23 - sm32) / S; | ||
out[1] = (sm31 - sm13) / S; | ||
out[2] = (sm12 - sm21) / S; | ||
} else if (sm11 > sm22 && sm11 > sm33) { | ||
S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2; | ||
out[3] = (sm23 - sm32) / S; | ||
out[0] = 0.25 * S; | ||
out[1] = (sm12 + sm21) / S; | ||
out[2] = (sm31 + sm13) / S; | ||
} else if (sm22 > sm33) { | ||
S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2; | ||
out[3] = (sm31 - sm13) / S; | ||
out[0] = (sm12 + sm21) / S; | ||
out[1] = 0.25 * S; | ||
out[2] = (sm23 + sm32) / S; | ||
} else { | ||
S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2; | ||
out[3] = (sm12 - sm21) / S; | ||
out[0] = (sm31 + sm13) / S; | ||
out[1] = (sm23 + sm32) / S; | ||
out[2] = 0.25 * S; | ||
} | ||
return out; | ||
const scaling = new glMatrix.ARRAY_TYPE(3); | ||
getScaling(scaling, mat); | ||
const is1 = 1 / scaling[0]; | ||
const is2 = 1 / scaling[1]; | ||
const is3 = 1 / scaling[2]; | ||
const sm11 = mat[0] * is1; | ||
const sm12 = mat[1] * is2; | ||
const sm13 = mat[2] * is3; | ||
const sm21 = mat[4] * is1; | ||
const sm22 = mat[5] * is2; | ||
const sm23 = mat[6] * is3; | ||
const sm31 = mat[8] * is1; | ||
const sm32 = mat[9] * is2; | ||
const sm33 = mat[10] * is3; | ||
const trace = sm11 + sm22 + sm33; | ||
let S = 0; | ||
if (trace > 0) { | ||
S = Math.sqrt(trace + 1.0) * 2; | ||
out[3] = 0.25 * S; | ||
out[0] = (sm23 - sm32) / S; | ||
out[1] = (sm31 - sm13) / S; | ||
out[2] = (sm12 - sm21) / S; | ||
} | ||
else if (sm11 > sm22 && sm11 > sm33) { | ||
S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2; | ||
out[3] = (sm23 - sm32) / S; | ||
out[0] = 0.25 * S; | ||
out[1] = (sm12 + sm21) / S; | ||
out[2] = (sm31 + sm13) / S; | ||
} | ||
else if (sm22 > sm33) { | ||
S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2; | ||
out[3] = (sm31 - sm13) / S; | ||
out[0] = (sm12 + sm21) / S; | ||
out[1] = 0.25 * S; | ||
out[2] = (sm23 + sm32) / S; | ||
} | ||
else { | ||
S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2; | ||
out[3] = (sm12 - sm21) / S; | ||
out[0] = (sm31 + sm13) / S; | ||
out[1] = (sm23 + sm32) / S; | ||
out[2] = 0.25 * S; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Decomposes a transformation matrix into its rotation, translation | ||
* and scale components. Returns only the rotation component | ||
* @param {quat} out_r Quaternion to receive the rotation component | ||
* @param {vec3} out_t Vector to receive the translation vector | ||
* @param {vec3} out_s Vector to receive the scaling factor | ||
* @param {ReadonlyMat4} mat Matrix to be decomposed (input) | ||
* @returns {quat} out_r | ||
*/ | ||
export function decompose(out_r, out_t, out_s, mat) { | ||
out_t[0] = mat[12]; | ||
out_t[1] = mat[13]; | ||
out_t[2] = mat[14]; | ||
const m11 = mat[0]; | ||
const m12 = mat[1]; | ||
const m13 = mat[2]; | ||
const m21 = mat[4]; | ||
const m22 = mat[5]; | ||
const m23 = mat[6]; | ||
const m31 = mat[8]; | ||
const m32 = mat[9]; | ||
const m33 = mat[10]; | ||
out_s[0] = Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13); | ||
out_s[1] = Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23); | ||
out_s[2] = Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33); | ||
const is1 = 1 / out_s[0]; | ||
const is2 = 1 / out_s[1]; | ||
const is3 = 1 / out_s[2]; | ||
const sm11 = m11 * is1; | ||
const sm12 = m12 * is2; | ||
const sm13 = m13 * is3; | ||
const sm21 = m21 * is1; | ||
const sm22 = m22 * is2; | ||
const sm23 = m23 * is3; | ||
const sm31 = m31 * is1; | ||
const sm32 = m32 * is2; | ||
const sm33 = m33 * is3; | ||
const trace = sm11 + sm22 + sm33; | ||
let S = 0; | ||
if (trace > 0) { | ||
S = Math.sqrt(trace + 1.0) * 2; | ||
out_r[3] = 0.25 * S; | ||
out_r[0] = (sm23 - sm32) / S; | ||
out_r[1] = (sm31 - sm13) / S; | ||
out_r[2] = (sm12 - sm21) / S; | ||
} else if (sm11 > sm22 && sm11 > sm33) { | ||
S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2; | ||
out_r[3] = (sm23 - sm32) / S; | ||
out_r[0] = 0.25 * S; | ||
out_r[1] = (sm12 + sm21) / S; | ||
out_r[2] = (sm31 + sm13) / S; | ||
} else if (sm22 > sm33) { | ||
S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2; | ||
out_r[3] = (sm31 - sm13) / S; | ||
out_r[0] = (sm12 + sm21) / S; | ||
out_r[1] = 0.25 * S; | ||
out_r[2] = (sm23 + sm32) / S; | ||
} else { | ||
S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2; | ||
out_r[3] = (sm12 - sm21) / S; | ||
out_r[0] = (sm31 + sm13) / S; | ||
out_r[1] = (sm23 + sm32) / S; | ||
out_r[2] = 0.25 * S; | ||
} | ||
return out_r; | ||
out_t[0] = mat[12]; | ||
out_t[1] = mat[13]; | ||
out_t[2] = mat[14]; | ||
const m11 = mat[0]; | ||
const m12 = mat[1]; | ||
const m13 = mat[2]; | ||
const m21 = mat[4]; | ||
const m22 = mat[5]; | ||
const m23 = mat[6]; | ||
const m31 = mat[8]; | ||
const m32 = mat[9]; | ||
const m33 = mat[10]; | ||
out_s[0] = Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13); | ||
out_s[1] = Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23); | ||
out_s[2] = Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33); | ||
const is1 = 1 / out_s[0]; | ||
const is2 = 1 / out_s[1]; | ||
const is3 = 1 / out_s[2]; | ||
const sm11 = m11 * is1; | ||
const sm12 = m12 * is2; | ||
const sm13 = m13 * is3; | ||
const sm21 = m21 * is1; | ||
const sm22 = m22 * is2; | ||
const sm23 = m23 * is3; | ||
const sm31 = m31 * is1; | ||
const sm32 = m32 * is2; | ||
const sm33 = m33 * is3; | ||
const trace = sm11 + sm22 + sm33; | ||
let S = 0; | ||
if (trace > 0) { | ||
S = Math.sqrt(trace + 1.0) * 2; | ||
out_r[3] = 0.25 * S; | ||
out_r[0] = (sm23 - sm32) / S; | ||
out_r[1] = (sm31 - sm13) / S; | ||
out_r[2] = (sm12 - sm21) / S; | ||
} | ||
else if (sm11 > sm22 && sm11 > sm33) { | ||
S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2; | ||
out_r[3] = (sm23 - sm32) / S; | ||
out_r[0] = 0.25 * S; | ||
out_r[1] = (sm12 + sm21) / S; | ||
out_r[2] = (sm31 + sm13) / S; | ||
} | ||
else if (sm22 > sm33) { | ||
S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2; | ||
out_r[3] = (sm31 - sm13) / S; | ||
out_r[0] = (sm12 + sm21) / S; | ||
out_r[1] = 0.25 * S; | ||
out_r[2] = (sm23 + sm32) / S; | ||
} | ||
else { | ||
S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2; | ||
out_r[3] = (sm12 - sm21) / S; | ||
out_r[0] = (sm31 + sm13) / S; | ||
out_r[1] = (sm23 + sm32) / S; | ||
out_r[2] = 0.25 * S; | ||
} | ||
return out_r; | ||
} | ||
/** | ||
* Creates a matrix from a quaternion rotation, vector translation and vector scale | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat4.identity(dest); | ||
* mat4.translate(dest, vec); | ||
* let quatMat = mat4.create(); | ||
* quat4.toMat4(quat, quatMat); | ||
* mat4.multiply(dest, quatMat); | ||
* mat4.scale(dest, scale) | ||
* | ||
* @param {mat4} out mat4 receiving operation result | ||
* @param {quat4} q Rotation quaternion | ||
* @param {ReadonlyVec3} v Translation vector | ||
* @param {ReadonlyVec3} s Scaling vector | ||
* @returns {mat4} out | ||
*/ | ||
export function fromRotationTranslationScale(out, q, v, s) { | ||
const x = q[0]; | ||
const y = q[1]; | ||
const z = q[2]; | ||
const w = q[3]; | ||
const x2 = x + x; | ||
const y2 = y + y; | ||
const z2 = z + z; | ||
const xx = x * x2; | ||
const xy = x * y2; | ||
const xz = x * z2; | ||
const yy = y * y2; | ||
const yz = y * z2; | ||
const zz = z * z2; | ||
const wx = w * x2; | ||
const wy = w * y2; | ||
const wz = w * z2; | ||
const sx = s[0]; | ||
const sy = s[1]; | ||
const sz = s[2]; | ||
out[0] = (1 - (yy + zz)) * sx; | ||
out[1] = (xy + wz) * sx; | ||
out[2] = (xz - wy) * sx; | ||
out[3] = 0; | ||
out[4] = (xy - wz) * sy; | ||
out[5] = (1 - (xx + zz)) * sy; | ||
out[6] = (yz + wx) * sy; | ||
out[7] = 0; | ||
out[8] = (xz + wy) * sz; | ||
out[9] = (yz - wx) * sz; | ||
out[10] = (1 - (xx + yy)) * sz; | ||
out[11] = 0; | ||
out[12] = v[0]; | ||
out[13] = v[1]; | ||
out[14] = v[2]; | ||
out[15] = 1; | ||
return out; | ||
// Quaternion math | ||
const x = q[0]; | ||
const y = q[1]; | ||
const z = q[2]; | ||
const w = q[3]; | ||
const x2 = x + x; | ||
const y2 = y + y; | ||
const z2 = z + z; | ||
const xx = x * x2; | ||
const xy = x * y2; | ||
const xz = x * z2; | ||
const yy = y * y2; | ||
const yz = y * z2; | ||
const zz = z * z2; | ||
const wx = w * x2; | ||
const wy = w * y2; | ||
const wz = w * z2; | ||
const sx = s[0]; | ||
const sy = s[1]; | ||
const sz = s[2]; | ||
out[0] = (1 - (yy + zz)) * sx; | ||
out[1] = (xy + wz) * sx; | ||
out[2] = (xz - wy) * sx; | ||
out[3] = 0; | ||
out[4] = (xy - wz) * sy; | ||
out[5] = (1 - (xx + zz)) * sy; | ||
out[6] = (yz + wx) * sy; | ||
out[7] = 0; | ||
out[8] = (xz + wy) * sz; | ||
out[9] = (yz - wx) * sz; | ||
out[10] = (1 - (xx + yy)) * sz; | ||
out[11] = 0; | ||
out[12] = v[0]; | ||
out[13] = v[1]; | ||
out[14] = v[2]; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat4.identity(dest); | ||
* mat4.translate(dest, vec); | ||
* mat4.translate(dest, origin); | ||
* let quatMat = mat4.create(); | ||
* quat4.toMat4(quat, quatMat); | ||
* mat4.multiply(dest, quatMat); | ||
* mat4.scale(dest, scale) | ||
* mat4.translate(dest, negativeOrigin); | ||
* | ||
* @param {mat4} out mat4 receiving operation result | ||
* @param {quat4} q Rotation quaternion | ||
* @param {ReadonlyVec3} v Translation vector | ||
* @param {ReadonlyVec3} s Scaling vector | ||
* @param {ReadonlyVec3} o The origin vector around which to scale and rotate | ||
* @returns {mat4} out | ||
*/ | ||
export function fromRotationTranslationScaleOrigin(out, q, v, s, o) { | ||
const x = q[0]; | ||
const y = q[1]; | ||
const z = q[2]; | ||
const w = q[3]; | ||
const x2 = x + x; | ||
const y2 = y + y; | ||
const z2 = z + z; | ||
const xx = x * x2; | ||
const xy = x * y2; | ||
const xz = x * z2; | ||
const yy = y * y2; | ||
const yz = y * z2; | ||
const zz = z * z2; | ||
const wx = w * x2; | ||
const wy = w * y2; | ||
const wz = w * z2; | ||
const sx = s[0]; | ||
const sy = s[1]; | ||
const sz = s[2]; | ||
const ox = o[0]; | ||
const oy = o[1]; | ||
const oz = o[2]; | ||
const out0 = (1 - (yy + zz)) * sx; | ||
const out1 = (xy + wz) * sx; | ||
const out2 = (xz - wy) * sx; | ||
const out4 = (xy - wz) * sy; | ||
const out5 = (1 - (xx + zz)) * sy; | ||
const out6 = (yz + wx) * sy; | ||
const out8 = (xz + wy) * sz; | ||
const out9 = (yz - wx) * sz; | ||
const out10 = (1 - (xx + yy)) * sz; | ||
out[0] = out0; | ||
out[1] = out1; | ||
out[2] = out2; | ||
out[3] = 0; | ||
out[4] = out4; | ||
out[5] = out5; | ||
out[6] = out6; | ||
out[7] = 0; | ||
out[8] = out8; | ||
out[9] = out9; | ||
out[10] = out10; | ||
out[11] = 0; | ||
out[12] = v[0] + ox - (out0 * ox + out4 * oy + out8 * oz); | ||
out[13] = v[1] + oy - (out1 * ox + out5 * oy + out9 * oz); | ||
out[14] = v[2] + oz - (out2 * ox + out6 * oy + out10 * oz); | ||
out[15] = 1; | ||
return out; | ||
// Quaternion math | ||
const x = q[0]; | ||
const y = q[1]; | ||
const z = q[2]; | ||
const w = q[3]; | ||
const x2 = x + x; | ||
const y2 = y + y; | ||
const z2 = z + z; | ||
const xx = x * x2; | ||
const xy = x * y2; | ||
const xz = x * z2; | ||
const yy = y * y2; | ||
const yz = y * z2; | ||
const zz = z * z2; | ||
const wx = w * x2; | ||
const wy = w * y2; | ||
const wz = w * z2; | ||
const sx = s[0]; | ||
const sy = s[1]; | ||
const sz = s[2]; | ||
const ox = o[0]; | ||
const oy = o[1]; | ||
const oz = o[2]; | ||
const out0 = (1 - (yy + zz)) * sx; | ||
const out1 = (xy + wz) * sx; | ||
const out2 = (xz - wy) * sx; | ||
const out4 = (xy - wz) * sy; | ||
const out5 = (1 - (xx + zz)) * sy; | ||
const out6 = (yz + wx) * sy; | ||
const out8 = (xz + wy) * sz; | ||
const out9 = (yz - wx) * sz; | ||
const out10 = (1 - (xx + yy)) * sz; | ||
out[0] = out0; | ||
out[1] = out1; | ||
out[2] = out2; | ||
out[3] = 0; | ||
out[4] = out4; | ||
out[5] = out5; | ||
out[6] = out6; | ||
out[7] = 0; | ||
out[8] = out8; | ||
out[9] = out9; | ||
out[10] = out10; | ||
out[11] = 0; | ||
out[12] = v[0] + ox - (out0 * ox + out4 * oy + out8 * oz); | ||
out[13] = v[1] + oy - (out1 * ox + out5 * oy + out9 * oz); | ||
out[14] = v[2] + oz - (out2 * ox + out6 * oy + out10 * oz); | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Calculates a 4x4 matrix from the given quaternion | ||
* | ||
* @param {mat4} out mat4 receiving operation result | ||
* @param {ReadonlyQuat} q Quaternion to create matrix from | ||
* | ||
* @returns {mat4} out | ||
*/ | ||
export function fromQuat(out, q) { | ||
const x = q[0]; | ||
const y = q[1]; | ||
const z = q[2]; | ||
const w = q[3]; | ||
const x2 = x + x; | ||
const y2 = y + y; | ||
const z2 = z + z; | ||
const xx = x * x2; | ||
const yx = y * x2; | ||
const yy = y * y2; | ||
const zx = z * x2; | ||
const zy = z * y2; | ||
const zz = z * z2; | ||
const wx = w * x2; | ||
const wy = w * y2; | ||
const wz = w * z2; | ||
out[0] = 1 - yy - zz; | ||
out[1] = yx + wz; | ||
out[2] = zx - wy; | ||
out[3] = 0; | ||
out[4] = yx - wz; | ||
out[5] = 1 - xx - zz; | ||
out[6] = zy + wx; | ||
out[7] = 0; | ||
out[8] = zx + wy; | ||
out[9] = zy - wx; | ||
out[10] = 1 - xx - yy; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
const x = q[0]; | ||
const y = q[1]; | ||
const z = q[2]; | ||
const w = q[3]; | ||
const x2 = x + x; | ||
const y2 = y + y; | ||
const z2 = z + z; | ||
const xx = x * x2; | ||
const yx = y * x2; | ||
const yy = y * y2; | ||
const zx = z * x2; | ||
const zy = z * y2; | ||
const zz = z * z2; | ||
const wx = w * x2; | ||
const wy = w * y2; | ||
const wz = w * z2; | ||
out[0] = 1 - yy - zz; | ||
out[1] = yx + wz; | ||
out[2] = zx - wy; | ||
out[3] = 0; | ||
out[4] = yx - wz; | ||
out[5] = 1 - xx - zz; | ||
out[6] = zy + wx; | ||
out[7] = 0; | ||
out[8] = zx + wy; | ||
out[9] = zy - wx; | ||
out[10] = 1 - xx - yy; | ||
out[11] = 0; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = 0; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Generates a frustum matrix with the given bounds | ||
* | ||
* @param {mat4} out mat4 frustum matrix will be written into | ||
* @param {Number} left Left bound of the frustum | ||
* @param {Number} right Right bound of the frustum | ||
* @param {Number} bottom Bottom bound of the frustum | ||
* @param {Number} top Top bound of the frustum | ||
* @param {Number} near Near bound of the frustum | ||
* @param {Number} far Far bound of the frustum | ||
* @returns {mat4} out | ||
*/ | ||
export function frustum(out, left, right, bottom, top, near, far) { | ||
const rl = 1 / (right - left); | ||
const tb = 1 / (top - bottom); | ||
const nf = 1 / (near - far); | ||
out[0] = near * 2 * rl; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = near * 2 * tb; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = (right + left) * rl; | ||
out[9] = (top + bottom) * tb; | ||
out[10] = (far + near) * nf; | ||
out[11] = -1; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = far * near * 2 * nf; | ||
out[15] = 0; | ||
return out; | ||
} | ||
export function perspectiveNO(out, fovy, aspect, near, far) { | ||
const f = 1.0 / Math.tan(fovy / 2); | ||
out[0] = f / aspect; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = f; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[11] = -1; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[15] = 0; | ||
if (far != null && far !== Infinity) { | ||
const rl = 1 / (right - left); | ||
const tb = 1 / (top - bottom); | ||
const nf = 1 / (near - far); | ||
out[0] = near * 2 * rl; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = near * 2 * tb; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = (right + left) * rl; | ||
out[9] = (top + bottom) * tb; | ||
out[10] = (far + near) * nf; | ||
out[14] = 2 * far * near * nf; | ||
} else { | ||
out[10] = -1; | ||
out[14] = -2 * near; | ||
} | ||
return out; | ||
out[11] = -1; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[14] = far * near * 2 * nf; | ||
out[15] = 0; | ||
return out; | ||
} | ||
/** | ||
* Generates a perspective projection matrix with the given bounds. | ||
* The near/far clip planes correspond to a normalized device coordinate Z range of [-1, 1], | ||
* which matches WebGL/OpenGL's clip volume. | ||
* Passing null/undefined/no value for far will generate infinite projection matrix. | ||
* | ||
* @param {mat4} out mat4 frustum matrix will be written into | ||
* @param {number} fovy Vertical field of view in radians | ||
* @param {number} aspect Aspect ratio. typically viewport width/height | ||
* @param {number} near Near bound of the frustum | ||
* @param {number} far Far bound of the frustum, can be null or Infinity | ||
* @returns {mat4} out | ||
*/ | ||
export function perspectiveNO(out, fovy, aspect, near, far) { | ||
const f = 1.0 / Math.tan(fovy / 2); | ||
out[0] = f / aspect; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = f; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[11] = -1; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[15] = 0; | ||
if (far != null && far !== Infinity) { | ||
const nf = 1 / (near - far); | ||
out[10] = (far + near) * nf; | ||
out[14] = 2 * far * near * nf; | ||
} | ||
else { | ||
out[10] = -1; | ||
out[14] = -2 * near; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Alias for {@link mat4.perspectiveNO} | ||
* @function | ||
*/ | ||
export const perspective = perspectiveNO; | ||
/** | ||
* Generates a perspective projection matrix suitable for WebGPU with the given bounds. | ||
* The near/far clip planes correspond to a normalized device coordinate Z range of [0, 1], | ||
* which matches WebGPU/Vulkan/DirectX/Metal's clip volume. | ||
* Passing null/undefined/no value for far will generate infinite projection matrix. | ||
* | ||
* @param {mat4} out mat4 frustum matrix will be written into | ||
* @param {number} fovy Vertical field of view in radians | ||
* @param {number} aspect Aspect ratio. typically viewport width/height | ||
* @param {number} near Near bound of the frustum | ||
* @param {number} far Far bound of the frustum, can be null or Infinity | ||
* @returns {mat4} out | ||
*/ | ||
export function perspectiveZO(out, fovy, aspect, near, far) { | ||
const f = 1.0 / Math.tan(fovy / 2); | ||
out[0] = f / aspect; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = f; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[11] = -1; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[15] = 0; | ||
if (far != null && far !== Infinity) { | ||
const nf = 1 / (near - far); | ||
out[10] = far * nf; | ||
out[14] = far * near * nf; | ||
} else { | ||
out[10] = -1; | ||
out[14] = -near; | ||
} | ||
return out; | ||
const f = 1.0 / Math.tan(fovy / 2); | ||
out[0] = f / aspect; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = f; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[11] = -1; | ||
out[12] = 0; | ||
out[13] = 0; | ||
out[15] = 0; | ||
if (far != null && far !== Infinity) { | ||
const nf = 1 / (near - far); | ||
out[10] = far * nf; | ||
out[14] = far * near * nf; | ||
} | ||
else { | ||
out[10] = -1; | ||
out[14] = -near; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Generates a perspective projection matrix with the given field of view. | ||
* This is primarily useful for generating projection matrices to be used | ||
* with the still experiemental WebVR API. | ||
* | ||
* @param {mat4} out mat4 frustum matrix will be written into | ||
* @param {Object} fov Object containing the following values: upDegrees, downDegrees, leftDegrees, rightDegrees | ||
* @param {number} near Near bound of the frustum | ||
* @param {number} far Far bound of the frustum | ||
* @returns {mat4} out | ||
*/ | ||
export function perspectiveFromFieldOfView(out, fov, near, far) { | ||
const upTan = Math.tan(fov.upDegrees * Math.PI / 180.0); | ||
const downTan = Math.tan(fov.downDegrees * Math.PI / 180.0); | ||
const leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0); | ||
const rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0); | ||
const xScale = 2.0 / (leftTan + rightTan); | ||
const yScale = 2.0 / (upTan + downTan); | ||
out[0] = xScale; | ||
out[1] = 0.0; | ||
out[2] = 0.0; | ||
out[3] = 0.0; | ||
out[4] = 0.0; | ||
out[5] = yScale; | ||
out[6] = 0.0; | ||
out[7] = 0.0; | ||
out[8] = -((leftTan - rightTan) * xScale * 0.5); | ||
out[9] = (upTan - downTan) * yScale * 0.5; | ||
out[10] = far / (near - far); | ||
out[11] = -1.0; | ||
out[12] = 0.0; | ||
out[13] = 0.0; | ||
out[14] = far * near / (near - far); | ||
out[15] = 0.0; | ||
return out; | ||
const upTan = Math.tan((fov.upDegrees * Math.PI) / 180.0); | ||
const downTan = Math.tan((fov.downDegrees * Math.PI) / 180.0); | ||
const leftTan = Math.tan((fov.leftDegrees * Math.PI) / 180.0); | ||
const rightTan = Math.tan((fov.rightDegrees * Math.PI) / 180.0); | ||
const xScale = 2.0 / (leftTan + rightTan); | ||
const yScale = 2.0 / (upTan + downTan); | ||
out[0] = xScale; | ||
out[1] = 0.0; | ||
out[2] = 0.0; | ||
out[3] = 0.0; | ||
out[4] = 0.0; | ||
out[5] = yScale; | ||
out[6] = 0.0; | ||
out[7] = 0.0; | ||
out[8] = -((leftTan - rightTan) * xScale * 0.5); | ||
out[9] = (upTan - downTan) * yScale * 0.5; | ||
out[10] = far / (near - far); | ||
out[11] = -1.0; | ||
out[12] = 0.0; | ||
out[13] = 0.0; | ||
out[14] = (far * near) / (near - far); | ||
out[15] = 0.0; | ||
return out; | ||
} | ||
/** | ||
* Generates a orthogonal projection matrix with the given bounds. | ||
* The near/far clip planes correspond to a normalized device coordinate Z range of [-1, 1], | ||
* which matches WebGL/OpenGL's clip volume. | ||
* | ||
* @param {mat4} out mat4 frustum matrix will be written into | ||
* @param {number} left Left bound of the frustum | ||
* @param {number} right Right bound of the frustum | ||
* @param {number} bottom Bottom bound of the frustum | ||
* @param {number} top Top bound of the frustum | ||
* @param {number} near Near bound of the frustum | ||
* @param {number} far Far bound of the frustum | ||
* @returns {mat4} out | ||
*/ | ||
export function orthoNO(out, left, right, bottom, top, near, far) { | ||
const lr = 1 / (left - right); | ||
const bt = 1 / (bottom - top); | ||
const nf = 1 / (near - far); | ||
out[0] = -2 * lr; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = -2 * bt; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[10] = 2 * nf; | ||
out[11] = 0; | ||
out[12] = (left + right) * lr; | ||
out[13] = (top + bottom) * bt; | ||
out[14] = (far + near) * nf; | ||
out[15] = 1; | ||
return out; | ||
const lr = 1 / (left - right); | ||
const bt = 1 / (bottom - top); | ||
const nf = 1 / (near - far); | ||
out[0] = -2 * lr; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = -2 * bt; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[10] = 2 * nf; | ||
out[11] = 0; | ||
out[12] = (left + right) * lr; | ||
out[13] = (top + bottom) * bt; | ||
out[14] = (far + near) * nf; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Alias for {@link mat4.orthoNO} | ||
* @function | ||
*/ | ||
export const ortho = orthoNO; | ||
/** | ||
* Generates a orthogonal projection matrix with the given bounds. | ||
* The near/far clip planes correspond to a normalized device coordinate Z range of [0, 1], | ||
* which matches WebGPU/Vulkan/DirectX/Metal's clip volume. | ||
* | ||
* @param {mat4} out mat4 frustum matrix will be written into | ||
* @param {number} left Left bound of the frustum | ||
* @param {number} right Right bound of the frustum | ||
* @param {number} bottom Bottom bound of the frustum | ||
* @param {number} top Top bound of the frustum | ||
* @param {number} near Near bound of the frustum | ||
* @param {number} far Far bound of the frustum | ||
* @returns {mat4} out | ||
*/ | ||
export function orthoZO(out, left, right, bottom, top, near, far) { | ||
const lr = 1 / (left - right); | ||
const bt = 1 / (bottom - top); | ||
const nf = 1 / (near - far); | ||
out[0] = -2 * lr; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = -2 * bt; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[10] = nf; | ||
out[11] = 0; | ||
out[12] = (left + right) * lr; | ||
out[13] = (top + bottom) * bt; | ||
out[14] = near * nf; | ||
out[15] = 1; | ||
return out; | ||
const lr = 1 / (left - right); | ||
const bt = 1 / (bottom - top); | ||
const nf = 1 / (near - far); | ||
out[0] = -2 * lr; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
out[4] = 0; | ||
out[5] = -2 * bt; | ||
out[6] = 0; | ||
out[7] = 0; | ||
out[8] = 0; | ||
out[9] = 0; | ||
out[10] = nf; | ||
out[11] = 0; | ||
out[12] = (left + right) * lr; | ||
out[13] = (top + bottom) * bt; | ||
out[14] = near * nf; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Generates a look-at matrix with the given eye position, focal point, and up axis. | ||
* If you want a matrix that actually makes an object look at another object, you should use targetTo instead. | ||
* | ||
* @param {mat4} out mat4 frustum matrix will be written into | ||
* @param {ReadonlyVec3} eye Position of the viewer | ||
* @param {ReadonlyVec3} center Point the viewer is looking at | ||
* @param {ReadonlyVec3} up vec3 pointing up | ||
* @returns {mat4} out | ||
*/ | ||
export function lookAt(out, eye, center, up) { | ||
let len; | ||
let x0; | ||
let x1; | ||
let x2; | ||
let y0; | ||
let y1; | ||
let y2; | ||
let z0; | ||
let z1; | ||
let z2; | ||
const eyex = eye[0]; | ||
const eyey = eye[1]; | ||
const eyez = eye[2]; | ||
const upx = up[0]; | ||
const upy = up[1]; | ||
const upz = up[2]; | ||
const centerx = center[0]; | ||
const centery = center[1]; | ||
const centerz = center[2]; | ||
if (Math.abs(eyex - centerx) < glMatrix.EPSILON && Math.abs(eyey - centery) < glMatrix.EPSILON && Math.abs(eyez - centerz) < glMatrix.EPSILON) { | ||
return identity(out); | ||
} | ||
z0 = eyex - centerx; | ||
z1 = eyey - centery; | ||
z2 = eyez - centerz; | ||
len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2); | ||
z0 *= len; | ||
z1 *= len; | ||
z2 *= len; | ||
x0 = upy * z2 - upz * z1; | ||
x1 = upz * z0 - upx * z2; | ||
x2 = upx * z1 - upy * z0; | ||
len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2); | ||
if (!len) { | ||
x0 = 0; | ||
x1 = 0; | ||
x2 = 0; | ||
} else { | ||
len = 1 / len; | ||
x0 *= len; | ||
x1 *= len; | ||
x2 *= len; | ||
} | ||
y0 = z1 * x2 - z2 * x1; | ||
y1 = z2 * x0 - z0 * x2; | ||
y2 = z0 * x1 - z1 * x0; | ||
len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2); | ||
if (!len) { | ||
y0 = 0; | ||
y1 = 0; | ||
y2 = 0; | ||
} else { | ||
len = 1 / len; | ||
y0 *= len; | ||
y1 *= len; | ||
y2 *= len; | ||
} | ||
out[0] = x0; | ||
out[1] = y0; | ||
out[2] = z0; | ||
out[3] = 0; | ||
out[4] = x1; | ||
out[5] = y1; | ||
out[6] = z1; | ||
out[7] = 0; | ||
out[8] = x2; | ||
out[9] = y2; | ||
out[10] = z2; | ||
out[11] = 0; | ||
out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez); | ||
out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez); | ||
out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez); | ||
out[15] = 1; | ||
return out; | ||
} | ||
export function targetTo(out, eye, target, up) { | ||
const eyex = eye[0]; | ||
const eyey = eye[1]; | ||
const eyez = eye[2]; | ||
const upx = up[0]; | ||
const upy = up[1]; | ||
const upz = up[2]; | ||
let z0 = eyex - target[0]; | ||
let z1 = eyey - target[1]; | ||
let z2 = eyez - target[2]; | ||
let len = z0 * z0 + z1 * z1 + z2 * z2; | ||
if (len > 0) { | ||
len = 1 / Math.sqrt(len); | ||
let len; | ||
let x0; | ||
let x1; | ||
let x2; | ||
let y0; | ||
let y1; | ||
let y2; | ||
let z0; | ||
let z1; | ||
let z2; | ||
const eyex = eye[0]; | ||
const eyey = eye[1]; | ||
const eyez = eye[2]; | ||
const upx = up[0]; | ||
const upy = up[1]; | ||
const upz = up[2]; | ||
const centerx = center[0]; | ||
const centery = center[1]; | ||
const centerz = center[2]; | ||
if (Math.abs(eyex - centerx) < glMatrix.EPSILON && | ||
Math.abs(eyey - centery) < glMatrix.EPSILON && | ||
Math.abs(eyez - centerz) < glMatrix.EPSILON) { | ||
return identity(out); | ||
} | ||
z0 = eyex - centerx; | ||
z1 = eyey - centery; | ||
z2 = eyez - centerz; | ||
len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2); | ||
z0 *= len; | ||
z1 *= len; | ||
z2 *= len; | ||
} | ||
let x0 = upy * z2 - upz * z1; | ||
let x1 = upz * z0 - upx * z2; | ||
let x2 = upx * z1 - upy * z0; | ||
len = x0 * x0 + x1 * x1 + x2 * x2; | ||
if (len > 0) { | ||
len = 1 / Math.sqrt(len); | ||
x0 *= len; | ||
x1 *= len; | ||
x2 *= len; | ||
} | ||
out[0] = x0; | ||
out[1] = x1; | ||
out[2] = x2; | ||
out[3] = 0; | ||
out[4] = z1 * x2 - z2 * x1; | ||
out[5] = z2 * x0 - z0 * x2; | ||
out[6] = z0 * x1 - z1 * x0; | ||
out[7] = 0; | ||
out[8] = z0; | ||
out[9] = z1; | ||
out[10] = z2; | ||
out[11] = 0; | ||
out[12] = eyex; | ||
out[13] = eyey; | ||
out[14] = eyez; | ||
out[15] = 1; | ||
return out; | ||
x0 = upy * z2 - upz * z1; | ||
x1 = upz * z0 - upx * z2; | ||
x2 = upx * z1 - upy * z0; | ||
len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2); | ||
if (!len) { | ||
x0 = 0; | ||
x1 = 0; | ||
x2 = 0; | ||
} | ||
else { | ||
len = 1 / len; | ||
x0 *= len; | ||
x1 *= len; | ||
x2 *= len; | ||
} | ||
y0 = z1 * x2 - z2 * x1; | ||
y1 = z2 * x0 - z0 * x2; | ||
y2 = z0 * x1 - z1 * x0; | ||
len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2); | ||
if (!len) { | ||
y0 = 0; | ||
y1 = 0; | ||
y2 = 0; | ||
} | ||
else { | ||
len = 1 / len; | ||
y0 *= len; | ||
y1 *= len; | ||
y2 *= len; | ||
} | ||
out[0] = x0; | ||
out[1] = y0; | ||
out[2] = z0; | ||
out[3] = 0; | ||
out[4] = x1; | ||
out[5] = y1; | ||
out[6] = z1; | ||
out[7] = 0; | ||
out[8] = x2; | ||
out[9] = y2; | ||
out[10] = z2; | ||
out[11] = 0; | ||
out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez); | ||
out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez); | ||
out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez); | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Generates a matrix that makes something look at something else. | ||
* | ||
* @param {mat4} out mat4 frustum matrix will be written into | ||
* @param {ReadonlyVec3} eye Position of the viewer | ||
* @param {ReadonlyVec3} center Point the viewer is looking at | ||
* @param {ReadonlyVec3} up vec3 pointing up | ||
* @returns {mat4} out | ||
*/ | ||
export function targetTo(out, eye, target, up) { | ||
const eyex = eye[0]; | ||
const eyey = eye[1]; | ||
const eyez = eye[2]; | ||
const upx = up[0]; | ||
const upy = up[1]; | ||
const upz = up[2]; | ||
let z0 = eyex - target[0]; | ||
let z1 = eyey - target[1]; | ||
let z2 = eyez - target[2]; | ||
let len = z0 * z0 + z1 * z1 + z2 * z2; | ||
if (len > 0) { | ||
len = 1 / Math.sqrt(len); | ||
z0 *= len; | ||
z1 *= len; | ||
z2 *= len; | ||
} | ||
let x0 = upy * z2 - upz * z1; | ||
let x1 = upz * z0 - upx * z2; | ||
let x2 = upx * z1 - upy * z0; | ||
len = x0 * x0 + x1 * x1 + x2 * x2; | ||
if (len > 0) { | ||
len = 1 / Math.sqrt(len); | ||
x0 *= len; | ||
x1 *= len; | ||
x2 *= len; | ||
} | ||
out[0] = x0; | ||
out[1] = x1; | ||
out[2] = x2; | ||
out[3] = 0; | ||
out[4] = z1 * x2 - z2 * x1; | ||
out[5] = z2 * x0 - z0 * x2; | ||
out[6] = z0 * x1 - z1 * x0; | ||
out[7] = 0; | ||
out[8] = z0; | ||
out[9] = z1; | ||
out[10] = z2; | ||
out[11] = 0; | ||
out[12] = eyex; | ||
out[13] = eyey; | ||
out[14] = eyez; | ||
out[15] = 1; | ||
return out; | ||
} | ||
/** | ||
* Returns a string representation of a mat4 | ||
* | ||
* @param {ReadonlyMat4} a matrix to represent as a string | ||
* @returns {String} string representation of the matrix | ||
*/ | ||
export function str(a) { | ||
return "mat4(".concat(a[0], ", ").concat(a[1], ", ").concat(a[2], ", ").concat(a[3], ", ").concat(a[4], ", ").concat(a[5], ", ").concat(a[6], ", ").concat(a[7], ", ").concat(a[8], ", ").concat(a[9], ", ").concat(a[10], ", ").concat(a[11], ", ").concat(a[12], ", ").concat(a[13], ", ").concat(a[14], ", ").concat(a[15], ")"); | ||
return `mat4(${a[0]}, ${a[1]}, ${a[2]}, ${a[3]}, ${a[4]}, ${a[5]}, ${a[6]}, ${a[7]}, ${a[8]}, ${a[9]}, ${a[10]}, ${a[11]}, ${a[12]}, ${a[13]}, ${a[14]}, ${a[15]})`; | ||
} | ||
/** | ||
* Returns Frobenius norm of a mat4 | ||
* | ||
* @param {ReadonlyMat4} a the matrix to calculate Frobenius norm of | ||
* @returns {Number} Frobenius norm | ||
*/ | ||
export function frob(a) { | ||
return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3] + a[4] * a[4] + a[5] * a[5] + a[6] * a[6] + a[7] * a[7] + a[8] * a[8] + a[9] * a[9] + a[10] * a[10] + a[11] * a[11] + a[12] * a[12] + a[13] * a[13] + a[14] * a[14] + a[15] * a[15]); | ||
return Math.sqrt(a[0] * a[0] + | ||
a[1] * a[1] + | ||
a[2] * a[2] + | ||
a[3] * a[3] + | ||
a[4] * a[4] + | ||
a[5] * a[5] + | ||
a[6] * a[6] + | ||
a[7] * a[7] + | ||
a[8] * a[8] + | ||
a[9] * a[9] + | ||
a[10] * a[10] + | ||
a[11] * a[11] + | ||
a[12] * a[12] + | ||
a[13] * a[13] + | ||
a[14] * a[14] + | ||
a[15] * a[15]); | ||
} | ||
/** | ||
* Adds two mat4's | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the first operand | ||
* @param {ReadonlyMat4} b the second operand | ||
* @returns {mat4} out | ||
*/ | ||
export function add(out, a, b) { | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
out[4] = a[4] + b[4]; | ||
out[5] = a[5] + b[5]; | ||
out[6] = a[6] + b[6]; | ||
out[7] = a[7] + b[7]; | ||
out[8] = a[8] + b[8]; | ||
out[9] = a[9] + b[9]; | ||
out[10] = a[10] + b[10]; | ||
out[11] = a[11] + b[11]; | ||
out[12] = a[12] + b[12]; | ||
out[13] = a[13] + b[13]; | ||
out[14] = a[14] + b[14]; | ||
out[15] = a[15] + b[15]; | ||
return out; | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
out[4] = a[4] + b[4]; | ||
out[5] = a[5] + b[5]; | ||
out[6] = a[6] + b[6]; | ||
out[7] = a[7] + b[7]; | ||
out[8] = a[8] + b[8]; | ||
out[9] = a[9] + b[9]; | ||
out[10] = a[10] + b[10]; | ||
out[11] = a[11] + b[11]; | ||
out[12] = a[12] + b[12]; | ||
out[13] = a[13] + b[13]; | ||
out[14] = a[14] + b[14]; | ||
out[15] = a[15] + b[15]; | ||
return out; | ||
} | ||
/** | ||
* Subtracts matrix b from matrix a | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the first operand | ||
* @param {ReadonlyMat4} b the second operand | ||
* @returns {mat4} out | ||
*/ | ||
export function subtract(out, a, b) { | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
out[3] = a[3] - b[3]; | ||
out[4] = a[4] - b[4]; | ||
out[5] = a[5] - b[5]; | ||
out[6] = a[6] - b[6]; | ||
out[7] = a[7] - b[7]; | ||
out[8] = a[8] - b[8]; | ||
out[9] = a[9] - b[9]; | ||
out[10] = a[10] - b[10]; | ||
out[11] = a[11] - b[11]; | ||
out[12] = a[12] - b[12]; | ||
out[13] = a[13] - b[13]; | ||
out[14] = a[14] - b[14]; | ||
out[15] = a[15] - b[15]; | ||
return out; | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
out[3] = a[3] - b[3]; | ||
out[4] = a[4] - b[4]; | ||
out[5] = a[5] - b[5]; | ||
out[6] = a[6] - b[6]; | ||
out[7] = a[7] - b[7]; | ||
out[8] = a[8] - b[8]; | ||
out[9] = a[9] - b[9]; | ||
out[10] = a[10] - b[10]; | ||
out[11] = a[11] - b[11]; | ||
out[12] = a[12] - b[12]; | ||
out[13] = a[13] - b[13]; | ||
out[14] = a[14] - b[14]; | ||
out[15] = a[15] - b[15]; | ||
return out; | ||
} | ||
/** | ||
* Multiply each element of the matrix by a scalar. | ||
* | ||
* @param {mat4} out the receiving matrix | ||
* @param {ReadonlyMat4} a the matrix to scale | ||
* @param {Number} b amount to scale the matrix's elements by | ||
* @returns {mat4} out | ||
*/ | ||
export function multiplyScalar(out, a, b) { | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
out[4] = a[4] * b; | ||
out[5] = a[5] * b; | ||
out[6] = a[6] * b; | ||
out[7] = a[7] * b; | ||
out[8] = a[8] * b; | ||
out[9] = a[9] * b; | ||
out[10] = a[10] * b; | ||
out[11] = a[11] * b; | ||
out[12] = a[12] * b; | ||
out[13] = a[13] * b; | ||
out[14] = a[14] * b; | ||
out[15] = a[15] * b; | ||
return out; | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
out[4] = a[4] * b; | ||
out[5] = a[5] * b; | ||
out[6] = a[6] * b; | ||
out[7] = a[7] * b; | ||
out[8] = a[8] * b; | ||
out[9] = a[9] * b; | ||
out[10] = a[10] * b; | ||
out[11] = a[11] * b; | ||
out[12] = a[12] * b; | ||
out[13] = a[13] * b; | ||
out[14] = a[14] * b; | ||
out[15] = a[15] * b; | ||
return out; | ||
} | ||
/** | ||
* Adds two mat4's after multiplying each element of the second operand by a scalar value. | ||
* | ||
* @param {mat4} out the receiving vector | ||
* @param {ReadonlyMat4} a the first operand | ||
* @param {ReadonlyMat4} b the second operand | ||
* @param {Number} scale the amount to scale b's elements by before adding | ||
* @returns {mat4} out | ||
*/ | ||
export function multiplyScalarAndAdd(out, a, b, scale) { | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
out[3] = a[3] + b[3] * scale; | ||
out[4] = a[4] + b[4] * scale; | ||
out[5] = a[5] + b[5] * scale; | ||
out[6] = a[6] + b[6] * scale; | ||
out[7] = a[7] + b[7] * scale; | ||
out[8] = a[8] + b[8] * scale; | ||
out[9] = a[9] + b[9] * scale; | ||
out[10] = a[10] + b[10] * scale; | ||
out[11] = a[11] + b[11] * scale; | ||
out[12] = a[12] + b[12] * scale; | ||
out[13] = a[13] + b[13] * scale; | ||
out[14] = a[14] + b[14] * scale; | ||
out[15] = a[15] + b[15] * scale; | ||
return out; | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
out[3] = a[3] + b[3] * scale; | ||
out[4] = a[4] + b[4] * scale; | ||
out[5] = a[5] + b[5] * scale; | ||
out[6] = a[6] + b[6] * scale; | ||
out[7] = a[7] + b[7] * scale; | ||
out[8] = a[8] + b[8] * scale; | ||
out[9] = a[9] + b[9] * scale; | ||
out[10] = a[10] + b[10] * scale; | ||
out[11] = a[11] + b[11] * scale; | ||
out[12] = a[12] + b[12] * scale; | ||
out[13] = a[13] + b[13] * scale; | ||
out[14] = a[14] + b[14] * scale; | ||
out[15] = a[15] + b[15] * scale; | ||
return out; | ||
} | ||
/** | ||
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) | ||
* | ||
* @param {ReadonlyMat4} a The first matrix. | ||
* @param {ReadonlyMat4} b The second matrix. | ||
* @returns {Boolean} True if the matrices are equal, false otherwise. | ||
*/ | ||
export function exactEquals(a, b) { | ||
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && a[8] === b[8] && a[9] === b[9] && a[10] === b[10] && a[11] === b[11] && a[12] === b[12] && a[13] === b[13] && a[14] === b[14] && a[15] === b[15]; | ||
return (a[0] === b[0] && | ||
a[1] === b[1] && | ||
a[2] === b[2] && | ||
a[3] === b[3] && | ||
a[4] === b[4] && | ||
a[5] === b[5] && | ||
a[6] === b[6] && | ||
a[7] === b[7] && | ||
a[8] === b[8] && | ||
a[9] === b[9] && | ||
a[10] === b[10] && | ||
a[11] === b[11] && | ||
a[12] === b[12] && | ||
a[13] === b[13] && | ||
a[14] === b[14] && | ||
a[15] === b[15]); | ||
} | ||
/** | ||
* Returns whether or not the matrices have approximately the same elements in the same position. | ||
* | ||
* @param {ReadonlyMat4} a The first matrix. | ||
* @param {ReadonlyMat4} b The second matrix. | ||
* @returns {Boolean} True if the matrices are equal, false otherwise. | ||
*/ | ||
export function equals(a, b) { | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const a2 = a[2]; | ||
const a3 = a[3]; | ||
const a4 = a[4]; | ||
const a5 = a[5]; | ||
const a6 = a[6]; | ||
const a7 = a[7]; | ||
const a8 = a[8]; | ||
const a9 = a[9]; | ||
const a10 = a[10]; | ||
const a11 = a[11]; | ||
const a12 = a[12]; | ||
const a13 = a[13]; | ||
const a14 = a[14]; | ||
const a15 = a[15]; | ||
const b0 = b[0]; | ||
const b1 = b[1]; | ||
const b2 = b[2]; | ||
const b3 = b[3]; | ||
const b4 = b[4]; | ||
const b5 = b[5]; | ||
const b6 = b[6]; | ||
const b7 = b[7]; | ||
const b8 = b[8]; | ||
const b9 = b[9]; | ||
const b10 = b[10]; | ||
const b11 = b[11]; | ||
const b12 = b[12]; | ||
const b13 = b[13]; | ||
const b14 = b[14]; | ||
const b15 = b[15]; | ||
return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && Math.abs(a8 - b8) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)) && Math.abs(a9 - b9) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a9), Math.abs(b9)) && Math.abs(a10 - b10) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a10), Math.abs(b10)) && Math.abs(a11 - b11) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a11), Math.abs(b11)) && Math.abs(a12 - b12) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a12), Math.abs(b12)) && Math.abs(a13 - b13) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a13), Math.abs(b13)) && Math.abs(a14 - b14) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a14), Math.abs(b14)) && Math.abs(a15 - b15) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a15), Math.abs(b15)); | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const a2 = a[2]; | ||
const a3 = a[3]; | ||
const a4 = a[4]; | ||
const a5 = a[5]; | ||
const a6 = a[6]; | ||
const a7 = a[7]; | ||
const a8 = a[8]; | ||
const a9 = a[9]; | ||
const a10 = a[10]; | ||
const a11 = a[11]; | ||
const a12 = a[12]; | ||
const a13 = a[13]; | ||
const a14 = a[14]; | ||
const a15 = a[15]; | ||
const b0 = b[0]; | ||
const b1 = b[1]; | ||
const b2 = b[2]; | ||
const b3 = b[3]; | ||
const b4 = b[4]; | ||
const b5 = b[5]; | ||
const b6 = b[6]; | ||
const b7 = b[7]; | ||
const b8 = b[8]; | ||
const b9 = b[9]; | ||
const b10 = b[10]; | ||
const b11 = b[11]; | ||
const b12 = b[12]; | ||
const b13 = b[13]; | ||
const b14 = b[14]; | ||
const b15 = b[15]; | ||
return (Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | ||
Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | ||
Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && | ||
Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && | ||
Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && | ||
Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && | ||
Math.abs(a6 - b6) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && | ||
Math.abs(a7 - b7) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && | ||
Math.abs(a8 - b8) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)) && | ||
Math.abs(a9 - b9) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a9), Math.abs(b9)) && | ||
Math.abs(a10 - b10) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a10), Math.abs(b10)) && | ||
Math.abs(a11 - b11) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a11), Math.abs(b11)) && | ||
Math.abs(a12 - b12) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a12), Math.abs(b12)) && | ||
Math.abs(a13 - b13) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a13), Math.abs(b13)) && | ||
Math.abs(a14 - b14) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a14), Math.abs(b14)) && | ||
Math.abs(a15 - b15) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a15), Math.abs(b15))); | ||
} | ||
/** | ||
* Alias for {@link mat4.multiply} | ||
* @function | ||
*/ | ||
export const mul = multiply; | ||
/** | ||
* Alias for {@link mat4.subtract} | ||
* @function | ||
*/ | ||
export const sub = subtract; | ||
//# sourceMappingURL=mat4.js.map |
@@ -357,2 +357,1 @@ import * as vec4 from './vec4.js'; | ||
export declare const setAxes: (out: any, view: any, right: any, up: any) => any; | ||
//# sourceMappingURL=quat.d.ts.map |
@@ -0,1 +1,3 @@ | ||
// @eslint-disable | ||
// @ts-nocheck | ||
import * as glMatrix from './common.js'; | ||
@@ -5,312 +7,708 @@ import * as mat3 from './mat3.js'; | ||
import * as vec4 from './vec4.js'; | ||
// const glMatrix: {EPSILON = 0.000001}; | ||
/** | ||
* Quaternion in the format XYZW | ||
* @module quat | ||
*/ | ||
/** | ||
* Creates a new identity quat | ||
* | ||
* @returns {quat} a new quaternion | ||
*/ | ||
export function create() { | ||
const out = new glMatrix.ARRAY_TYPE(4); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
const out = new glMatrix.ARRAY_TYPE(4); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
} | ||
out[3] = 1; | ||
return out; | ||
} | ||
/** | ||
* Set a quat to the identity quaternion | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @returns {quat} out | ||
*/ | ||
export function identity(out) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
} | ||
out[3] = 1; | ||
return out; | ||
out[3] = 1; | ||
return out; | ||
} | ||
export function identity(out) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
return out; | ||
} | ||
/** | ||
* Sets a quat from the given angle and rotation axis, | ||
* then returns it. | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyVec3} axis the axis around which to rotate | ||
* @param {Number} rad the angle in radians | ||
* @returns {quat} out | ||
**/ | ||
export function setAxisAngle(out, axis, rad) { | ||
rad = rad * 0.5; | ||
const s = Math.sin(rad); | ||
out[0] = s * axis[0]; | ||
out[1] = s * axis[1]; | ||
out[2] = s * axis[2]; | ||
out[3] = Math.cos(rad); | ||
return out; | ||
rad = rad * 0.5; | ||
const s = Math.sin(rad); | ||
out[0] = s * axis[0]; | ||
out[1] = s * axis[1]; | ||
out[2] = s * axis[2]; | ||
out[3] = Math.cos(rad); | ||
return out; | ||
} | ||
/** | ||
* Gets the rotation axis and angle for a given | ||
* quaternion. If a quaternion is created with | ||
* setAxisAngle, this method will return the same | ||
* values as providied in the original parameter list | ||
* OR functionally equivalent values. | ||
* Example: The quaternion formed by axis [0, 0, 1] and | ||
* angle -90 is the same as the quaternion formed by | ||
* [0, 0, 1] and 270. This method favors the latter. | ||
* @param {vec3} out_axis Vector receiving the axis of rotation | ||
* @param {ReadonlyQuat} q Quaternion to be decomposed | ||
* @return {Number} Angle, in radians, of the rotation | ||
*/ | ||
export function getAxisAngle(out_axis, q) { | ||
const rad = Math.acos(q[3]) * 2.0; | ||
const s = Math.sin(rad / 2.0); | ||
if (s > glMatrix.EPSILON) { | ||
out_axis[0] = q[0] / s; | ||
out_axis[1] = q[1] / s; | ||
out_axis[2] = q[2] / s; | ||
} else { | ||
out_axis[0] = 1; | ||
out_axis[1] = 0; | ||
out_axis[2] = 0; | ||
} | ||
return rad; | ||
const rad = Math.acos(q[3]) * 2.0; | ||
const s = Math.sin(rad / 2.0); | ||
if (s > glMatrix.EPSILON) { | ||
out_axis[0] = q[0] / s; | ||
out_axis[1] = q[1] / s; | ||
out_axis[2] = q[2] / s; | ||
} | ||
else { | ||
// If s is zero, return any axis (no rotation - axis does not matter) | ||
out_axis[0] = 1; | ||
out_axis[1] = 0; | ||
out_axis[2] = 0; | ||
} | ||
return rad; | ||
} | ||
/** | ||
* Gets the angular distance between two unit quaternions | ||
* | ||
* @param {ReadonlyQuat} a Origin unit quaternion | ||
* @param {ReadonlyQuat} b Destination unit quaternion | ||
* @return {Number} Angle, in radians, between the two quaternions | ||
*/ | ||
export function getAngle(a, b) { | ||
const dotproduct = dot(a, b); | ||
return Math.acos(2 * dotproduct * dotproduct - 1); | ||
const dotproduct = dot(a, b); | ||
return Math.acos(2 * dotproduct * dotproduct - 1); | ||
} | ||
/** | ||
* Multiplies two quat's | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a the first operand | ||
* @param {ReadonlyQuat} b the second operand | ||
* @returns {quat} out | ||
*/ | ||
export function multiply(out, a, b) { | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
const bx = b[0]; | ||
const by = b[1]; | ||
const bz = b[2]; | ||
const bw = b[3]; | ||
out[0] = ax * bw + aw * bx + ay * bz - az * by; | ||
out[1] = ay * bw + aw * by + az * bx - ax * bz; | ||
out[2] = az * bw + aw * bz + ax * by - ay * bx; | ||
out[3] = aw * bw - ax * bx - ay * by - az * bz; | ||
return out; | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
const bx = b[0]; | ||
const by = b[1]; | ||
const bz = b[2]; | ||
const bw = b[3]; | ||
out[0] = ax * bw + aw * bx + ay * bz - az * by; | ||
out[1] = ay * bw + aw * by + az * bx - ax * bz; | ||
out[2] = az * bw + aw * bz + ax * by - ay * bx; | ||
out[3] = aw * bw - ax * bx - ay * by - az * bz; | ||
return out; | ||
} | ||
/** | ||
* Rotates a quaternion by the given angle about the X axis | ||
* | ||
* @param {quat} out quat receiving operation result | ||
* @param {ReadonlyQuat} a quat to rotate | ||
* @param {number} rad angle (in radians) to rotate | ||
* @returns {quat} out | ||
*/ | ||
export function rotateX(out, a, rad) { | ||
rad *= 0.5; | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
const bx = Math.sin(rad); | ||
const bw = Math.cos(rad); | ||
out[0] = ax * bw + aw * bx; | ||
out[1] = ay * bw + az * bx; | ||
out[2] = az * bw - ay * bx; | ||
out[3] = aw * bw - ax * bx; | ||
return out; | ||
rad *= 0.5; | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
const bx = Math.sin(rad); | ||
const bw = Math.cos(rad); | ||
out[0] = ax * bw + aw * bx; | ||
out[1] = ay * bw + az * bx; | ||
out[2] = az * bw - ay * bx; | ||
out[3] = aw * bw - ax * bx; | ||
return out; | ||
} | ||
/** | ||
* Rotates a quaternion by the given angle about the Y axis | ||
* | ||
* @param {quat} out quat receiving operation result | ||
* @param {ReadonlyQuat} a quat to rotate | ||
* @param {number} rad angle (in radians) to rotate | ||
* @returns {quat} out | ||
*/ | ||
export function rotateY(out, a, rad) { | ||
rad *= 0.5; | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
const by = Math.sin(rad); | ||
const bw = Math.cos(rad); | ||
out[0] = ax * bw - az * by; | ||
out[1] = ay * bw + aw * by; | ||
out[2] = az * bw + ax * by; | ||
out[3] = aw * bw - ay * by; | ||
return out; | ||
rad *= 0.5; | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
const by = Math.sin(rad); | ||
const bw = Math.cos(rad); | ||
out[0] = ax * bw - az * by; | ||
out[1] = ay * bw + aw * by; | ||
out[2] = az * bw + ax * by; | ||
out[3] = aw * bw - ay * by; | ||
return out; | ||
} | ||
/** | ||
* Rotates a quaternion by the given angle about the Z axis | ||
* | ||
* @param {quat} out quat receiving operation result | ||
* @param {ReadonlyQuat} a quat to rotate | ||
* @param {number} rad angle (in radians) to rotate | ||
* @returns {quat} out | ||
*/ | ||
export function rotateZ(out, a, rad) { | ||
rad *= 0.5; | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
const bz = Math.sin(rad); | ||
const bw = Math.cos(rad); | ||
out[0] = ax * bw + ay * bz; | ||
out[1] = ay * bw - ax * bz; | ||
out[2] = az * bw + aw * bz; | ||
out[3] = aw * bw - az * bz; | ||
return out; | ||
rad *= 0.5; | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
const bz = Math.sin(rad); | ||
const bw = Math.cos(rad); | ||
out[0] = ax * bw + ay * bz; | ||
out[1] = ay * bw - ax * bz; | ||
out[2] = az * bw + aw * bz; | ||
out[3] = aw * bw - az * bz; | ||
return out; | ||
} | ||
/** | ||
* Calculates the W component of a quat from the X, Y, and Z components. | ||
* Assumes that quaternion is 1 unit in length. | ||
* Any existing W component will be ignored. | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a quat to calculate W component of | ||
* @returns {quat} out | ||
*/ | ||
export function calculateW(out, a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
out[0] = x; | ||
out[1] = y; | ||
out[2] = z; | ||
out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z)); | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
out[0] = x; | ||
out[1] = y; | ||
out[2] = z; | ||
out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z)); | ||
return out; | ||
} | ||
/** | ||
* Calculate the exponential of a unit quaternion. | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a quat to calculate the exponential of | ||
* @returns {quat} out | ||
*/ | ||
export function exp(out, a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
const r = Math.sqrt(x * x + y * y + z * z); | ||
const et = Math.exp(w); | ||
const s = r > 0 ? et * Math.sin(r) / r : 0; | ||
out[0] = x * s; | ||
out[1] = y * s; | ||
out[2] = z * s; | ||
out[3] = et * Math.cos(r); | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
const r = Math.sqrt(x * x + y * y + z * z); | ||
const et = Math.exp(w); | ||
const s = r > 0 ? (et * Math.sin(r)) / r : 0; | ||
out[0] = x * s; | ||
out[1] = y * s; | ||
out[2] = z * s; | ||
out[3] = et * Math.cos(r); | ||
return out; | ||
} | ||
/** | ||
* Calculate the natural logarithm of a unit quaternion. | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a quat to calculate the exponential of | ||
* @returns {quat} out | ||
*/ | ||
export function ln(out, a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
const r = Math.sqrt(x * x + y * y + z * z); | ||
const t = r > 0 ? Math.atan2(r, w) / r : 0; | ||
out[0] = x * t; | ||
out[1] = y * t; | ||
out[2] = z * t; | ||
out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w); | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
const r = Math.sqrt(x * x + y * y + z * z); | ||
const t = r > 0 ? Math.atan2(r, w) / r : 0; | ||
out[0] = x * t; | ||
out[1] = y * t; | ||
out[2] = z * t; | ||
out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w); | ||
return out; | ||
} | ||
/** | ||
* Calculate the scalar power of a unit quaternion. | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a quat to calculate the exponential of | ||
* @param {Number} b amount to scale the quaternion by | ||
* @returns {quat} out | ||
*/ | ||
export function pow(out, a, b) { | ||
ln(out, a); | ||
scale(out, out, b); | ||
exp(out, out); | ||
return out; | ||
ln(out, a); | ||
scale(out, out, b); | ||
exp(out, out); | ||
return out; | ||
} | ||
/** | ||
* Performs a spherical linear interpolation between two quat | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a the first operand | ||
* @param {ReadonlyQuat} b the second operand | ||
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs | ||
* @returns {quat} out | ||
*/ | ||
export function slerp(out, a, b, t) { | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
let bx = b[0]; | ||
let by = b[1]; | ||
let bz = b[2]; | ||
let bw = b[3]; | ||
let cosom; | ||
let omega; | ||
let scale0; | ||
let scale1; | ||
let sinom; | ||
cosom = ax * bx + ay * by + az * bz + aw * bw; | ||
if (cosom < 0.0) { | ||
cosom = -cosom; | ||
bx = -bx; | ||
by = -by; | ||
bz = -bz; | ||
bw = -bw; | ||
} | ||
if (1.0 - cosom > glMatrix.EPSILON) { | ||
omega = Math.acos(cosom); | ||
sinom = Math.sin(omega); | ||
scale0 = Math.sin((1.0 - t) * omega) / sinom; | ||
scale1 = Math.sin(t * omega) / sinom; | ||
} else { | ||
scale0 = 1.0 - t; | ||
scale1 = t; | ||
} | ||
out[0] = scale0 * ax + scale1 * bx; | ||
out[1] = scale0 * ay + scale1 * by; | ||
out[2] = scale0 * az + scale1 * bz; | ||
out[3] = scale0 * aw + scale1 * bw; | ||
return out; | ||
// benchmarks: | ||
// http://jsperf.com/quaternion-slerp-implementations | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
let bx = b[0]; | ||
let by = b[1]; | ||
let bz = b[2]; | ||
let bw = b[3]; | ||
let cosom; | ||
let omega; | ||
let scale0; | ||
let scale1; | ||
let sinom; | ||
// calc cosine | ||
cosom = ax * bx + ay * by + az * bz + aw * bw; | ||
// adjust signs (if necessary) | ||
if (cosom < 0.0) { | ||
cosom = -cosom; | ||
bx = -bx; | ||
by = -by; | ||
bz = -bz; | ||
bw = -bw; | ||
} | ||
// calculate coefficients | ||
if (1.0 - cosom > glMatrix.EPSILON) { | ||
// standard case (slerp) | ||
omega = Math.acos(cosom); | ||
sinom = Math.sin(omega); | ||
scale0 = Math.sin((1.0 - t) * omega) / sinom; | ||
scale1 = Math.sin(t * omega) / sinom; | ||
} | ||
else { | ||
// "from" and "to" quaternions are very close | ||
// ... so we can do a linear interpolation | ||
scale0 = 1.0 - t; | ||
scale1 = t; | ||
} | ||
// calculate final values | ||
out[0] = scale0 * ax + scale1 * bx; | ||
out[1] = scale0 * ay + scale1 * by; | ||
out[2] = scale0 * az + scale1 * bz; | ||
out[3] = scale0 * aw + scale1 * bw; | ||
return out; | ||
} | ||
/** | ||
* Generates a random unit quaternion | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @returns {quat} out | ||
*/ | ||
// export function random(out) { | ||
// // Implementation of http://planning.cs.uiuc.edu/node198.html | ||
// // TODO: Calling random 3 times is probably not the fastest solution | ||
// let u1 = glMatrix.RANDOM(); | ||
// let u2 = glMatrix.RANDOM(); | ||
// let u3 = glMatrix.RANDOM(); | ||
// let sqrt1MinusU1 = Math.sqrt(1 - u1); | ||
// let sqrtU1 = Math.sqrt(u1); | ||
// out[0] = sqrt1MinusU1 * Math.sin(2.0 * Math.PI * u2); | ||
// out[1] = sqrt1MinusU1 * Math.cos(2.0 * Math.PI * u2); | ||
// out[2] = sqrtU1 * Math.sin(2.0 * Math.PI * u3); | ||
// out[3] = sqrtU1 * Math.cos(2.0 * Math.PI * u3); | ||
// return out; | ||
// } | ||
/** | ||
* Calculates the inverse of a quat | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a quat to calculate inverse of | ||
* @returns {quat} out | ||
*/ | ||
export function invert(out, a) { | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const a2 = a[2]; | ||
const a3 = a[3]; | ||
const dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3; | ||
const invDot = dot ? 1.0 / dot : 0; | ||
out[0] = -a0 * invDot; | ||
out[1] = -a1 * invDot; | ||
out[2] = -a2 * invDot; | ||
out[3] = a3 * invDot; | ||
return out; | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const a2 = a[2]; | ||
const a3 = a[3]; | ||
const dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3; | ||
const invDot = dot ? 1.0 / dot : 0; | ||
// TODO: Would be faster to return [0,0,0,0] immediately if dot == 0 | ||
out[0] = -a0 * invDot; | ||
out[1] = -a1 * invDot; | ||
out[2] = -a2 * invDot; | ||
out[3] = a3 * invDot; | ||
return out; | ||
} | ||
/** | ||
* Calculates the conjugate of a quat | ||
* If the quaternion is normalized, this function is faster than quat.inverse and produces the same result. | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a quat to calculate conjugate of | ||
* @returns {quat} out | ||
*/ | ||
export function conjugate(out, a) { | ||
out[0] = -a[0]; | ||
out[1] = -a[1]; | ||
out[2] = -a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
out[0] = -a[0]; | ||
out[1] = -a[1]; | ||
out[2] = -a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
} | ||
/** | ||
* Creates a quaternion from the given 3x3 rotation matrix. | ||
* | ||
* NOTE: The resultant quaternion is not normalized, so you should be sure | ||
* to renormalize the quaternion yourself where necessary. | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyMat3} m rotation matrix | ||
* @returns {quat} out | ||
* @function | ||
*/ | ||
export function fromMat3(out, m) { | ||
const fTrace = m[0] + m[4] + m[8]; | ||
let fRoot; | ||
if (fTrace > 0.0) { | ||
fRoot = Math.sqrt(fTrace + 1.0); | ||
out[3] = 0.5 * fRoot; | ||
fRoot = 0.5 / fRoot; | ||
out[0] = (m[5] - m[7]) * fRoot; | ||
out[1] = (m[6] - m[2]) * fRoot; | ||
out[2] = (m[1] - m[3]) * fRoot; | ||
} else { | ||
let i = 0; | ||
if (m[4] > m[0]) i = 1; | ||
if (m[8] > m[i * 3 + i]) i = 2; | ||
const j = (i + 1) % 3; | ||
const k = (i + 2) % 3; | ||
fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0); | ||
out[i] = 0.5 * fRoot; | ||
fRoot = 0.5 / fRoot; | ||
out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot; | ||
out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot; | ||
out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot; | ||
} | ||
return out; | ||
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes | ||
// article "Quaternion Calculus and Fast Animation". | ||
const fTrace = m[0] + m[4] + m[8]; | ||
let fRoot; | ||
if (fTrace > 0.0) { | ||
// |w| > 1/2, may as well choose w > 1/2 | ||
fRoot = Math.sqrt(fTrace + 1.0); // 2w | ||
out[3] = 0.5 * fRoot; | ||
fRoot = 0.5 / fRoot; // 1/(4w) | ||
out[0] = (m[5] - m[7]) * fRoot; | ||
out[1] = (m[6] - m[2]) * fRoot; | ||
out[2] = (m[1] - m[3]) * fRoot; | ||
} | ||
else { | ||
// |w| <= 1/2 | ||
let i = 0; | ||
if (m[4] > m[0]) | ||
i = 1; | ||
if (m[8] > m[i * 3 + i]) | ||
i = 2; | ||
const j = (i + 1) % 3; | ||
const k = (i + 2) % 3; | ||
fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0); | ||
out[i] = 0.5 * fRoot; | ||
fRoot = 0.5 / fRoot; | ||
out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot; | ||
out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot; | ||
out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Creates a quaternion from the given euler angle x, y, z using the provided intrinsic order for the conversion. | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {Number} x Angle to rotate around X axis in degrees. | ||
* @param {Number} y Angle to rotate around Y axis in degrees. | ||
* @param {Number} z Angle to rotate around Z axis in degrees. | ||
* @param {'zyx'|'xyz'|'yxz'|'yzx'|'zxy'|'zyx'} order Intrinsic order for conversion, default is zyx. | ||
* @returns {quat} out | ||
* @function | ||
*/ | ||
// export function fromEuler(out, x, y, z, order = glMatrix.ANGLE_ORDER) { | ||
// let halfToRad = Math.PI / 360; | ||
// x *= halfToRad; | ||
// z *= halfToRad; | ||
// y *= halfToRad; | ||
// let sx = Math.sin(x); | ||
// let cx = Math.cos(x); | ||
// let sy = Math.sin(y); | ||
// let cy = Math.cos(y); | ||
// let sz = Math.sin(z); | ||
// let cz = Math.cos(z); | ||
// switch (order) { | ||
// case "xyz": | ||
// out[0] = sx * cy * cz + cx * sy * sz; | ||
// out[1] = cx * sy * cz - sx * cy * sz; | ||
// out[2] = cx * cy * sz + sx * sy * cz; | ||
// out[3] = cx * cy * cz - sx * sy * sz; | ||
// break; | ||
// case "xzy": | ||
// out[0] = sx * cy * cz - cx * sy * sz; | ||
// out[1] = cx * sy * cz - sx * cy * sz; | ||
// out[2] = cx * cy * sz + sx * sy * cz; | ||
// out[3] = cx * cy * cz + sx * sy * sz; | ||
// break; | ||
// case "yxz": | ||
// out[0] = sx * cy * cz + cx * sy * sz; | ||
// out[1] = cx * sy * cz - sx * cy * sz; | ||
// out[2] = cx * cy * sz - sx * sy * cz; | ||
// out[3] = cx * cy * cz + sx * sy * sz; | ||
// break; | ||
// case "yzx": | ||
// out[0] = sx * cy * cz + cx * sy * sz; | ||
// out[1] = cx * sy * cz + sx * cy * sz; | ||
// out[2] = cx * cy * sz - sx * sy * cz; | ||
// out[3] = cx * cy * cz - sx * sy * sz; | ||
// break; | ||
// case "zxy": | ||
// out[0] = sx * cy * cz - cx * sy * sz; | ||
// out[1] = cx * sy * cz + sx * cy * sz; | ||
// out[2] = cx * cy * sz + sx * sy * cz; | ||
// out[3] = cx * cy * cz - sx * sy * sz; | ||
// break; | ||
// case "zyx": | ||
// out[0] = sx * cy * cz - cx * sy * sz; | ||
// out[1] = cx * sy * cz + sx * cy * sz; | ||
// out[2] = cx * cy * sz - sx * sy * cz; | ||
// out[3] = cx * cy * cz + sx * sy * sz; | ||
// break; | ||
// default: | ||
// throw new Error('Unknown angle order ' + order); | ||
// } | ||
// return out; | ||
// } | ||
/** | ||
* Returns a string representation of a quaternion | ||
* | ||
* @param {ReadonlyQuat} a vector to represent as a string | ||
* @returns {String} string representation of the vector | ||
*/ | ||
export function str(a) { | ||
return "quat(".concat(a[0], ", ").concat(a[1], ", ").concat(a[2], ", ").concat(a[3], ")"); | ||
return `quat(${a[0]}, ${a[1]}, ${a[2]}, ${a[3]})`; | ||
} | ||
/** | ||
* Creates a new quat initialized with values from an existing quaternion | ||
* | ||
* @param {ReadonlyQuat} a quaternion to clone | ||
* @returns {quat} a new quaternion | ||
* @function | ||
*/ | ||
export const clone = vec4.clone; | ||
/** | ||
* Creates a new quat initialized with the given values | ||
* | ||
* @param {Number} x X component | ||
* @param {Number} y Y component | ||
* @param {Number} z Z component | ||
* @param {Number} w W component | ||
* @returns {quat} a new quaternion | ||
* @function | ||
*/ | ||
export const fromValues = vec4.fromValues; | ||
/** | ||
* Copy the values from one quat to another | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a the source quaternion | ||
* @returns {quat} out | ||
* @function | ||
*/ | ||
export const copy = vec4.copy; | ||
/** | ||
* Set the components of a quat to the given values | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {Number} x X component | ||
* @param {Number} y Y component | ||
* @param {Number} z Z component | ||
* @param {Number} w W component | ||
* @returns {quat} out | ||
* @function | ||
*/ | ||
export const set = vec4.set; | ||
/** | ||
* Adds two quat's | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a the first operand | ||
* @param {ReadonlyQuat} b the second operand | ||
* @returns {quat} out | ||
* @function | ||
*/ | ||
export const add = vec4.add; | ||
/** | ||
* Alias for {@link quat.multiply} | ||
* @function | ||
*/ | ||
export const mul = multiply; | ||
/** | ||
* Scales a quat by a scalar number | ||
* | ||
* @param {quat} out the receiving vector | ||
* @param {ReadonlyQuat} a the vector to scale | ||
* @param {Number} b amount to scale the vector by | ||
* @returns {quat} out | ||
* @function | ||
*/ | ||
export const scale = vec4.scale; | ||
/** | ||
* Calculates the dot product of two quat's | ||
* | ||
* @param {ReadonlyQuat} a the first operand | ||
* @param {ReadonlyQuat} b the second operand | ||
* @returns {Number} dot product of a and b | ||
* @function | ||
*/ | ||
export const dot = vec4.dot; | ||
/** | ||
* Performs a linear interpolation between two quat's | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a the first operand | ||
* @param {ReadonlyQuat} b the second operand | ||
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs | ||
* @returns {quat} out | ||
* @function | ||
*/ | ||
export const lerp = vec4.lerp; | ||
/** | ||
* Calculates the length of a quat | ||
* | ||
* @param {ReadonlyQuat} a vector to calculate length of | ||
* @returns {Number} length of a | ||
*/ | ||
export const length = vec4.length; | ||
/** | ||
* Alias for {@link quat.length} | ||
* @function | ||
*/ | ||
export const len = length; | ||
/** | ||
* Calculates the squared length of a quat | ||
* | ||
* @param {ReadonlyQuat} a vector to calculate squared length of | ||
* @returns {Number} squared length of a | ||
* @function | ||
*/ | ||
export const squaredLength = vec4.squaredLength; | ||
/** | ||
* Alias for {@link quat.squaredLength} | ||
* @function | ||
*/ | ||
export const sqrLen = squaredLength; | ||
/** | ||
* Normalize a quat | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a quaternion to normalize | ||
* @returns {quat} out | ||
* @function | ||
*/ | ||
export const normalize = vec4.normalize; | ||
/** | ||
* Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===) | ||
* | ||
* @param {ReadonlyQuat} a The first quaternion. | ||
* @param {ReadonlyQuat} b The second quaternion. | ||
* @returns {Boolean} True if the vectors are equal, false otherwise. | ||
*/ | ||
export const exactEquals = vec4.exactEquals; | ||
/** | ||
* Returns whether or not the quaternions point approximately to the same direction. | ||
* | ||
* Both quaternions are assumed to be unit length. | ||
* | ||
* @param {ReadonlyQuat} a The first unit quaternion. | ||
* @param {ReadonlyQuat} b The second unit quaternion. | ||
* @returns {Boolean} True if the quaternions are equal, false otherwise. | ||
*/ | ||
export function equals(a, b) { | ||
return Math.abs(vec4.dot(a, b)) >= 1 - glMatrix.EPSILON; | ||
return Math.abs(vec4.dot(a, b)) >= 1 - glMatrix.EPSILON; | ||
} | ||
export const rotationTo = function () { | ||
const tmpvec3 = vec3.create(); | ||
const xUnitVec3 = vec3.fromValues(1, 0, 0); | ||
const yUnitVec3 = vec3.fromValues(0, 1, 0); | ||
return function (out, a, b) { | ||
const dot = vec3.dot(a, b); | ||
if (dot < -0.999999) { | ||
vec3.cross(tmpvec3, xUnitVec3, a); | ||
if (vec3.len(tmpvec3) < 0.000001) vec3.cross(tmpvec3, yUnitVec3, a); | ||
vec3.normalize(tmpvec3, tmpvec3); | ||
setAxisAngle(out, tmpvec3, Math.PI); | ||
return out; | ||
} else if (dot > 0.999999) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
return out; | ||
} | ||
vec3.cross(tmpvec3, a, b); | ||
out[0] = tmpvec3[0]; | ||
out[1] = tmpvec3[1]; | ||
out[2] = tmpvec3[2]; | ||
out[3] = 1 + dot; | ||
return normalize(out, out); | ||
}; | ||
}(); | ||
export const sqlerp = function () { | ||
const temp1 = create(); | ||
const temp2 = create(); | ||
return function (out, a, b, c, d, t) { | ||
slerp(temp1, a, d, t); | ||
slerp(temp2, b, c, t); | ||
slerp(out, temp1, temp2, 2 * t * (1 - t)); | ||
return out; | ||
}; | ||
}(); | ||
export const setAxes = function () { | ||
const matr = mat3.create(); | ||
return function (out, view, right, up) { | ||
matr[0] = right[0]; | ||
matr[3] = right[1]; | ||
matr[6] = right[2]; | ||
matr[1] = up[0]; | ||
matr[4] = up[1]; | ||
matr[7] = up[2]; | ||
matr[2] = -view[0]; | ||
matr[5] = -view[1]; | ||
matr[8] = -view[2]; | ||
return normalize(out, fromMat3(out, matr)); | ||
}; | ||
}(); | ||
//# sourceMappingURL=quat.js.map | ||
/** | ||
* Sets a quaternion to represent the shortest rotation from one | ||
* vector to another. | ||
* | ||
* Both vectors are assumed to be unit length. | ||
* | ||
* @param {quat} out the receiving quaternion. | ||
* @param {ReadonlyVec3} a the initial vector | ||
* @param {ReadonlyVec3} b the destination vector | ||
* @returns {quat} out | ||
*/ | ||
export const rotationTo = (function () { | ||
const tmpvec3 = vec3.create(); | ||
const xUnitVec3 = vec3.fromValues(1, 0, 0); | ||
const yUnitVec3 = vec3.fromValues(0, 1, 0); | ||
return function (out, a, b) { | ||
const dot = vec3.dot(a, b); | ||
if (dot < -0.999999) { | ||
vec3.cross(tmpvec3, xUnitVec3, a); | ||
if (vec3.len(tmpvec3) < 0.000001) | ||
vec3.cross(tmpvec3, yUnitVec3, a); | ||
vec3.normalize(tmpvec3, tmpvec3); | ||
setAxisAngle(out, tmpvec3, Math.PI); | ||
return out; | ||
} | ||
else if (dot > 0.999999) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
return out; | ||
} | ||
vec3.cross(tmpvec3, a, b); | ||
out[0] = tmpvec3[0]; | ||
out[1] = tmpvec3[1]; | ||
out[2] = tmpvec3[2]; | ||
out[3] = 1 + dot; | ||
return normalize(out, out); | ||
}; | ||
})(); | ||
/** | ||
* Performs a spherical linear interpolation with two control points | ||
* | ||
* @param {quat} out the receiving quaternion | ||
* @param {ReadonlyQuat} a the first operand | ||
* @param {ReadonlyQuat} b the second operand | ||
* @param {ReadonlyQuat} c the third operand | ||
* @param {ReadonlyQuat} d the fourth operand | ||
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs | ||
* @returns {quat} out | ||
*/ | ||
export const sqlerp = (function () { | ||
const temp1 = create(); | ||
const temp2 = create(); | ||
return function (out, a, b, c, d, t) { | ||
slerp(temp1, a, d, t); | ||
slerp(temp2, b, c, t); | ||
slerp(out, temp1, temp2, 2 * t * (1 - t)); | ||
return out; | ||
}; | ||
})(); | ||
/** | ||
* Sets the specified quaternion with values corresponding to the given | ||
* axes. Each axis is a vec3 and is expected to be unit length and | ||
* perpendicular to all other specified axes. | ||
* | ||
* @param {ReadonlyVec3} view the vector representing the viewing direction | ||
* @param {ReadonlyVec3} right the vector representing the local "right" direction | ||
* @param {ReadonlyVec3} up the vector representing the local "up" direction | ||
* @returns {quat} out | ||
*/ | ||
export const setAxes = (function () { | ||
const matr = mat3.create(); | ||
return function (out, view, right, up) { | ||
matr[0] = right[0]; | ||
matr[3] = right[1]; | ||
matr[6] = right[2]; | ||
matr[1] = up[0]; | ||
matr[4] = up[1]; | ||
matr[7] = up[2]; | ||
matr[2] = -view[0]; | ||
matr[5] = -view[1]; | ||
matr[8] = -view[2]; | ||
return normalize(out, fromMat3(out, matr)); | ||
}; | ||
})(); |
@@ -363,2 +363,1 @@ /** | ||
export declare const forEach: (a: any, stride: any, offset: any, count: any, fn: any, arg: any) => any; | ||
//# sourceMappingURL=vec2.d.ts.map |
@@ -0,257 +1,574 @@ | ||
// @eslint-disable | ||
// @ts-nocheck | ||
import * as glMatrix from './common.js'; | ||
/** | ||
* 2 Dimensional Vector | ||
* @module vec2 | ||
*/ | ||
/** | ||
* Creates a new, empty vec2 | ||
* | ||
* @returns {NumericArray} a new 2D vector | ||
*/ | ||
export function create() { | ||
const out = new glMatrix.ARRAY_TYPE(2); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
} | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(2); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Creates a new vec2 initialized with values from an existing vector | ||
* | ||
* @param {Readonly<NumericArray>} a vector to clone | ||
* @returns {NumericArray} a new 2D vector | ||
*/ | ||
export function clone(a) { | ||
const out = new glMatrix.ARRAY_TYPE(2); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(2); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
return out; | ||
} | ||
/** | ||
* Creates a new vec2 initialized with the given values | ||
* | ||
* @param {Number} x X component | ||
* @param {Number} y Y component | ||
* @returns {NumericArray} a new 2D vector | ||
*/ | ||
export function fromValues(x, y) { | ||
const out = new glMatrix.ARRAY_TYPE(2); | ||
out[0] = x; | ||
out[1] = y; | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(2); | ||
out[0] = x; | ||
out[1] = y; | ||
return out; | ||
} | ||
/** | ||
* Copy the values from one vec2 to another | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the source vector | ||
* @returns {NumericArray} out | ||
*/ | ||
export function copy(out, a) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
return out; | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
return out; | ||
} | ||
/** | ||
* Set the components of a vec2 to the given values | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Number} x X component | ||
* @param {Number} y Y component | ||
* @returns {NumericArray} out | ||
*/ | ||
export function set(out, x, y) { | ||
out[0] = x; | ||
out[1] = y; | ||
return out; | ||
out[0] = x; | ||
out[1] = y; | ||
return out; | ||
} | ||
/** | ||
* Adds two vec2's | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @returns {NumericArray} out | ||
*/ | ||
export function add(out, a, b) { | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
return out; | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
return out; | ||
} | ||
/** | ||
* Subtracts vector b from vector a | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @returns {NumericArray} out | ||
*/ | ||
export function subtract(out, a, b) { | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
return out; | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
return out; | ||
} | ||
/** | ||
* Multiplies two vec2's | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @returns {NumericArray} out | ||
*/ | ||
export function multiply(out, a, b) { | ||
out[0] = a[0] * b[0]; | ||
out[1] = a[1] * b[1]; | ||
return out; | ||
out[0] = a[0] * b[0]; | ||
out[1] = a[1] * b[1]; | ||
return out; | ||
} | ||
/** | ||
* Divides two vec2's | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @returns {NumericArray} out | ||
*/ | ||
export function divide(out, a, b) { | ||
out[0] = a[0] / b[0]; | ||
out[1] = a[1] / b[1]; | ||
return out; | ||
out[0] = a[0] / b[0]; | ||
out[1] = a[1] / b[1]; | ||
return out; | ||
} | ||
/** | ||
* Math.ceil the components of a vec2 | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a vector to ceil | ||
* @returns {NumericArray} out | ||
*/ | ||
export function ceil(out, a) { | ||
out[0] = Math.ceil(a[0]); | ||
out[1] = Math.ceil(a[1]); | ||
return out; | ||
out[0] = Math.ceil(a[0]); | ||
out[1] = Math.ceil(a[1]); | ||
return out; | ||
} | ||
/** | ||
* Math.floor the components of a vec2 | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a vector to floor | ||
* @returns {NumericArray} out | ||
*/ | ||
export function floor(out, a) { | ||
out[0] = Math.floor(a[0]); | ||
out[1] = Math.floor(a[1]); | ||
return out; | ||
out[0] = Math.floor(a[0]); | ||
out[1] = Math.floor(a[1]); | ||
return out; | ||
} | ||
/** | ||
* Returns the minimum of two vec2's | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @returns {NumericArray} out | ||
*/ | ||
export function min(out, a, b) { | ||
out[0] = Math.min(a[0], b[0]); | ||
out[1] = Math.min(a[1], b[1]); | ||
return out; | ||
out[0] = Math.min(a[0], b[0]); | ||
out[1] = Math.min(a[1], b[1]); | ||
return out; | ||
} | ||
/** | ||
* Returns the maximum of two vec2's | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @returns {NumericArray} out | ||
*/ | ||
export function max(out, a, b) { | ||
out[0] = Math.max(a[0], b[0]); | ||
out[1] = Math.max(a[1], b[1]); | ||
return out; | ||
out[0] = Math.max(a[0], b[0]); | ||
out[1] = Math.max(a[1], b[1]); | ||
return out; | ||
} | ||
/** | ||
* symmetric round the components of a vec2 | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a vector to round | ||
* @returns {NumericArray} out | ||
*/ | ||
export function round(out, a) { | ||
out[0] = glMatrix.round(a[0]); | ||
out[1] = glMatrix.round(a[1]); | ||
return out; | ||
out[0] = glMatrix.round(a[0]); | ||
out[1] = glMatrix.round(a[1]); | ||
return out; | ||
} | ||
/** | ||
* Scales a vec2 by a scalar number | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the vector to scale | ||
* @param {Number} b amount to scale the vector by | ||
* @returns {NumericArray} out | ||
*/ | ||
export function scale(out, a, b) { | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
return out; | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
return out; | ||
} | ||
/** | ||
* Adds two vec2's after scaling the second operand by a scalar value | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @param {Number} scale the amount to scale b by before adding | ||
* @returns {NumericArray} out | ||
*/ | ||
export function scaleAndAdd(out, a, b, scale) { | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
return out; | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
return out; | ||
} | ||
/** | ||
* Calculates the euclidian distance between two vec2's | ||
* | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @returns {Number} distance between a and b | ||
*/ | ||
export function distance(a, b) { | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
return Math.sqrt(x * x + y * y); | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
return Math.sqrt(x * x + y * y); | ||
} | ||
/** | ||
* Calculates the squared euclidian distance between two vec2's | ||
* | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @returns {Number} squared distance between a and b | ||
*/ | ||
export function squaredDistance(a, b) { | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
return x * x + y * y; | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
return x * x + y * y; | ||
} | ||
/** | ||
* Calculates the length of a vec2 | ||
* | ||
* @param {Readonly<NumericArray>} a vector to calculate length of | ||
* @returns {Number} length of a | ||
*/ | ||
export function length(a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
return Math.sqrt(x * x + y * y); | ||
const x = a[0]; | ||
const y = a[1]; | ||
return Math.sqrt(x * x + y * y); | ||
} | ||
/** | ||
* Calculates the squared length of a vec2 | ||
* | ||
* @param {Readonly<NumericArray>} a vector to calculate squared length of | ||
* @returns {Number} squared length of a | ||
*/ | ||
export function squaredLength(a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
return x * x + y * y; | ||
const x = a[0]; | ||
const y = a[1]; | ||
return x * x + y * y; | ||
} | ||
/** | ||
* Negates the components of a vec2 | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a vector to negate | ||
* @returns {NumericArray} out | ||
*/ | ||
export function negate(out, a) { | ||
out[0] = -a[0]; | ||
out[1] = -a[1]; | ||
return out; | ||
out[0] = -a[0]; | ||
out[1] = -a[1]; | ||
return out; | ||
} | ||
/** | ||
* Returns the inverse of the components of a vec2 | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a vector to invert | ||
* @returns {NumericArray} out | ||
*/ | ||
export function inverse(out, a) { | ||
out[0] = 1.0 / a[0]; | ||
out[1] = 1.0 / a[1]; | ||
return out; | ||
out[0] = 1.0 / a[0]; | ||
out[1] = 1.0 / a[1]; | ||
return out; | ||
} | ||
/** | ||
* Normalize a vec2 | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a vector to normalize | ||
* @returns {NumericArray} out | ||
*/ | ||
export function normalize(out, a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
let len = x * x + y * y; | ||
if (len > 0) { | ||
len = 1 / Math.sqrt(len); | ||
} | ||
out[0] = a[0] * len; | ||
out[1] = a[1] * len; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
let len = x * x + y * y; | ||
if (len > 0) { | ||
// TODO: evaluate use of glm_invsqrt here? | ||
len = 1 / Math.sqrt(len); | ||
} | ||
out[0] = a[0] * len; | ||
out[1] = a[1] * len; | ||
return out; | ||
} | ||
/** | ||
* Calculates the dot product of two vec2's | ||
* | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @returns {Number} dot product of a and b | ||
*/ | ||
export function dot(a, b) { | ||
return a[0] * b[0] + a[1] * b[1]; | ||
return a[0] * b[0] + a[1] * b[1]; | ||
} | ||
/** | ||
* Computes the cross product of two vec2's | ||
* Note that the cross product must by definition produce a 3D vector | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @returns {vec3} out | ||
*/ | ||
export function cross(out, a, b) { | ||
const z = a[0] * b[1] - a[1] * b[0]; | ||
out[0] = out[1] = 0; | ||
out[2] = z; | ||
return out; | ||
const z = a[0] * b[1] - a[1] * b[0]; | ||
out[0] = out[1] = 0; | ||
out[2] = z; | ||
return out; | ||
} | ||
/** | ||
* Performs a linear interpolation between two vec2's | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the first operand | ||
* @param {Readonly<NumericArray>} b the second operand | ||
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs | ||
* @returns {NumericArray} out | ||
*/ | ||
export function lerp(out, a, b, t) { | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
out[0] = ax + t * (b[0] - ax); | ||
out[1] = ay + t * (b[1] - ay); | ||
return out; | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
out[0] = ax + t * (b[0] - ax); | ||
out[1] = ay + t * (b[1] - ay); | ||
return out; | ||
} | ||
/** | ||
* Generates a random vector with the given scale | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Number} [scale] Length of the resulting vector. If omitted, a unit vector will be returned | ||
* @returns {NumericArray} out | ||
*/ | ||
export function random(out, scale) { | ||
scale = scale === undefined ? 1.0 : scale; | ||
const r = glMatrix.RANDOM() * 2.0 * Math.PI; | ||
out[0] = Math.cos(r) * scale; | ||
out[1] = Math.sin(r) * scale; | ||
return out; | ||
scale = scale === undefined ? 1.0 : scale; | ||
const r = glMatrix.RANDOM() * 2.0 * Math.PI; | ||
out[0] = Math.cos(r) * scale; | ||
out[1] = Math.sin(r) * scale; | ||
return out; | ||
} | ||
/** | ||
* Transforms the vec2 with a mat2 | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the vector to transform | ||
* @param {ReadonlyMat2} m matrix to transform with | ||
* @returns {NumericArray} out | ||
*/ | ||
export function transformMat2(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[2] * y; | ||
out[1] = m[1] * x + m[3] * y; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[2] * y; | ||
out[1] = m[1] * x + m[3] * y; | ||
return out; | ||
} | ||
/** | ||
* Transforms the vec2 with a mat2d | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the vector to transform | ||
* @param {ReadonlyMat2d} m matrix to transform with | ||
* @returns {NumericArray} out | ||
*/ | ||
export function transformMat2d(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[2] * y + m[4]; | ||
out[1] = m[1] * x + m[3] * y + m[5]; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[2] * y + m[4]; | ||
out[1] = m[1] * x + m[3] * y + m[5]; | ||
return out; | ||
} | ||
/** | ||
* Transforms the vec2 with a mat3 | ||
* 3rd vector component is implicitly '1' | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the vector to transform | ||
* @param {ReadonlyMat3} m matrix to transform with | ||
* @returns {NumericArray} out | ||
*/ | ||
export function transformMat3(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[3] * y + m[6]; | ||
out[1] = m[1] * x + m[4] * y + m[7]; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[3] * y + m[6]; | ||
out[1] = m[1] * x + m[4] * y + m[7]; | ||
return out; | ||
} | ||
/** | ||
* Transforms the vec2 with a mat4 | ||
* 3rd vector component is implicitly '0' | ||
* 4th vector component is implicitly '1' | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @param {Readonly<NumericArray>} a the vector to transform | ||
* @param {ReadonlyMat4} m matrix to transform with | ||
* @returns {NumericArray} out | ||
*/ | ||
export function transformMat4(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[4] * y + m[12]; | ||
out[1] = m[1] * x + m[5] * y + m[13]; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[4] * y + m[12]; | ||
out[1] = m[1] * x + m[5] * y + m[13]; | ||
return out; | ||
} | ||
/** | ||
* Rotate a 2D vector | ||
* @param {NumericArray} out The receiving vec2 | ||
* @param {Readonly<NumericArray>} a The vec2 point to rotate | ||
* @param {Readonly<NumericArray>} b The origin of the rotation | ||
* @param {Number} rad The angle of rotation in radians | ||
* @returns {NumericArray} out | ||
*/ | ||
export function rotate(out, a, b, rad) { | ||
const p0 = a[0] - b[0]; | ||
const p1 = a[1] - b[1]; | ||
const sinC = Math.sin(rad); | ||
const cosC = Math.cos(rad); | ||
out[0] = p0 * cosC - p1 * sinC + b[0]; | ||
out[1] = p0 * sinC + p1 * cosC + b[1]; | ||
return out; | ||
// Translate point to the origin | ||
const p0 = a[0] - b[0]; | ||
const p1 = a[1] - b[1]; | ||
const sinC = Math.sin(rad); | ||
const cosC = Math.cos(rad); | ||
// perform rotation and translate to correct position | ||
out[0] = p0 * cosC - p1 * sinC + b[0]; | ||
out[1] = p0 * sinC + p1 * cosC + b[1]; | ||
return out; | ||
} | ||
/** | ||
* Get the angle between two 2D vectors | ||
* @param {Readonly<NumericArray>} a The first operand | ||
* @param {Readonly<NumericArray>} b The second operand | ||
* @returns {Number} The angle in radians | ||
*/ | ||
export function angle(a, b) { | ||
const x1 = a[0]; | ||
const y1 = a[1]; | ||
const x2 = b[0]; | ||
const y2 = b[1]; | ||
const mag = Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)); | ||
const cosine = mag && (x1 * x2 + y1 * y2) / mag; | ||
return Math.acos(Math.min(Math.max(cosine, -1), 1)); | ||
const x1 = a[0]; | ||
const y1 = a[1]; | ||
const x2 = b[0]; | ||
const y2 = b[1]; | ||
// mag is the product of the magnitudes of a and b | ||
const mag = Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)); | ||
// mag &&.. short circuits if mag == 0 | ||
const cosine = mag && (x1 * x2 + y1 * y2) / mag; | ||
// Math.min(Math.max(cosine, -1), 1) clamps the cosine between -1 and 1 | ||
return Math.acos(Math.min(Math.max(cosine, -1), 1)); | ||
} | ||
/** | ||
* Set the components of a vec2 to zero | ||
* | ||
* @param {NumericArray} out the receiving vector | ||
* @returns {NumericArray} out | ||
*/ | ||
export function zero(out) { | ||
out[0] = 0.0; | ||
out[1] = 0.0; | ||
return out; | ||
out[0] = 0.0; | ||
out[1] = 0.0; | ||
return out; | ||
} | ||
/** | ||
* Returns a string representation of a vector | ||
* | ||
* @param {Readonly<NumericArray>} a vector to represent as a string | ||
* @returns {String} string representation of the vector | ||
*/ | ||
export function str(a) { | ||
return "vec2(".concat(a[0], ", ").concat(a[1], ")"); | ||
return `vec2(${a[0]}, ${a[1]})`; | ||
} | ||
/** | ||
* Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===) | ||
* | ||
* @param {Readonly<NumericArray>} a The first vector. | ||
* @param {Readonly<NumericArray>} b The second vector. | ||
* @returns {Boolean} True if the vectors are equal, false otherwise. | ||
*/ | ||
export function exactEquals(a, b) { | ||
return a[0] === b[0] && a[1] === b[1]; | ||
return a[0] === b[0] && a[1] === b[1]; | ||
} | ||
/** | ||
* Returns whether or not the vectors have approximately the same elements in the same position. | ||
* | ||
* @param {Readonly<NumericArray>} a The first vector. | ||
* @param {Readonly<NumericArray>} b The second vector. | ||
* @returns {Boolean} True if the vectors are equal, false otherwise. | ||
*/ | ||
export function equals(a, b) { | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const b0 = b[0]; | ||
const b1 = b[1]; | ||
return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)); | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const b0 = b[0]; | ||
const b1 = b[1]; | ||
return (Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | ||
Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1))); | ||
} | ||
/** | ||
* Alias for {@link vec2.length} | ||
* @function | ||
*/ | ||
export const len = length; | ||
/** | ||
* Alias for {@link vec2.subtract} | ||
* @function | ||
*/ | ||
export const sub = subtract; | ||
/** | ||
* Alias for {@link vec2.multiply} | ||
* @function | ||
*/ | ||
export const mul = multiply; | ||
/** | ||
* Alias for {@link vec2.divide} | ||
* @function | ||
*/ | ||
export const div = divide; | ||
/** | ||
* Alias for {@link vec2.distance} | ||
* @function | ||
*/ | ||
export const dist = distance; | ||
/** | ||
* Alias for {@link vec2.squaredDistance} | ||
* @function | ||
*/ | ||
export const sqrDist = squaredDistance; | ||
/** | ||
* Alias for {@link vec2.squaredLength} | ||
* @function | ||
*/ | ||
export const sqrLen = squaredLength; | ||
export const forEach = function () { | ||
const vec = create(); | ||
return function (a, stride, offset, count, fn, arg) { | ||
let i; | ||
let l; | ||
if (!stride) { | ||
stride = 2; | ||
} | ||
if (!offset) { | ||
offset = 0; | ||
} | ||
if (count) { | ||
l = Math.min(count * stride + offset, a.length); | ||
} else { | ||
l = a.length; | ||
} | ||
for (i = offset; i < l; i += stride) { | ||
vec[0] = a[i]; | ||
vec[1] = a[i + 1]; | ||
fn(vec, vec, arg); | ||
a[i] = vec[0]; | ||
a[i + 1] = vec[1]; | ||
} | ||
return a; | ||
}; | ||
}(); | ||
//# sourceMappingURL=vec2.js.map | ||
/** | ||
* Perform some operation over an array of vec2s. | ||
* | ||
* @param {Array} a the array of vectors to iterate over | ||
* @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed | ||
* @param {Number} offset Number of elements to skip at the beginning of the array | ||
* @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array | ||
* @param {Function} fn Function to call for each vector in the array | ||
* @param {Object} [arg] additional argument to pass to fn | ||
* @returns {Array} a | ||
* @function | ||
*/ | ||
export const forEach = (function () { | ||
const vec = create(); | ||
return function (a, stride, offset, count, fn, arg) { | ||
let i; | ||
let l; | ||
if (!stride) { | ||
stride = 2; | ||
} | ||
if (!offset) { | ||
offset = 0; | ||
} | ||
if (count) { | ||
l = Math.min(count * stride + offset, a.length); | ||
} | ||
else { | ||
l = a.length; | ||
} | ||
for (i = offset; i < l; i += stride) { | ||
vec[0] = a[i]; | ||
vec[1] = a[i + 1]; | ||
fn(vec, vec, arg); | ||
a[i] = vec[0]; | ||
a[i + 1] = vec[1]; | ||
} | ||
return a; | ||
}; | ||
})(); |
@@ -406,2 +406,1 @@ /** | ||
export declare const forEach: (a: any, stride: any, offset: any, count: any, fn: any, arg: any) => any; | ||
//# sourceMappingURL=vec3.d.ts.map |
@@ -0,384 +1,752 @@ | ||
// @eslint-disable | ||
// @ts-nocheck | ||
import * as glMatrix from './common.js'; | ||
/** | ||
* 3 Dimensional Vector | ||
* @module vec3 | ||
*/ | ||
/** | ||
* Creates a new, empty vec3 | ||
* | ||
* @returns {vec3} a new 3D vector | ||
*/ | ||
export function create() { | ||
const out = new glMatrix.ARRAY_TYPE(3); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
} | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(3); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Creates a new vec3 initialized with values from an existing vector | ||
* | ||
* @param {ReadonlyVec3} a vector to clone | ||
* @returns {vec3} a new 3D vector | ||
*/ | ||
export function clone(a) { | ||
const out = new glMatrix.ARRAY_TYPE(3); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(3); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
return out; | ||
} | ||
/** | ||
* Calculates the length of a vec3 | ||
* | ||
* @param {ReadonlyVec3} a vector to calculate length of | ||
* @returns {Number} length of a | ||
*/ | ||
export function length(a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
return Math.sqrt(x * x + y * y + z * z); | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
return Math.sqrt(x * x + y * y + z * z); | ||
} | ||
/** | ||
* Creates a new vec3 initialized with the given values | ||
* | ||
* @param {Number} x X component | ||
* @param {Number} y Y component | ||
* @param {Number} z Z component | ||
* @returns {vec3} a new 3D vector | ||
*/ | ||
export function fromValues(x, y, z) { | ||
const out = new glMatrix.ARRAY_TYPE(3); | ||
out[0] = x; | ||
out[1] = y; | ||
out[2] = z; | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(3); | ||
out[0] = x; | ||
out[1] = y; | ||
out[2] = z; | ||
return out; | ||
} | ||
/** | ||
* Copy the values from one vec3 to another | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the source vector | ||
* @returns {vec3} out | ||
*/ | ||
export function copy(out, a) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
return out; | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
return out; | ||
} | ||
/** | ||
* Set the components of a vec3 to the given values | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {Number} x X component | ||
* @param {Number} y Y component | ||
* @param {Number} z Z component | ||
* @returns {vec3} out | ||
*/ | ||
export function set(out, x, y, z) { | ||
out[0] = x; | ||
out[1] = y; | ||
out[2] = z; | ||
return out; | ||
out[0] = x; | ||
out[1] = y; | ||
out[2] = z; | ||
return out; | ||
} | ||
/** | ||
* Adds two vec3's | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @returns {vec3} out | ||
*/ | ||
export function add(out, a, b) { | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
return out; | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
return out; | ||
} | ||
/** | ||
* Subtracts vector b from vector a | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @returns {vec3} out | ||
*/ | ||
export function subtract(out, a, b) { | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
return out; | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
return out; | ||
} | ||
/** | ||
* Multiplies two vec3's | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @returns {vec3} out | ||
*/ | ||
export function multiply(out, a, b) { | ||
out[0] = a[0] * b[0]; | ||
out[1] = a[1] * b[1]; | ||
out[2] = a[2] * b[2]; | ||
return out; | ||
out[0] = a[0] * b[0]; | ||
out[1] = a[1] * b[1]; | ||
out[2] = a[2] * b[2]; | ||
return out; | ||
} | ||
/** | ||
* Divides two vec3's | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @returns {vec3} out | ||
*/ | ||
export function divide(out, a, b) { | ||
out[0] = a[0] / b[0]; | ||
out[1] = a[1] / b[1]; | ||
out[2] = a[2] / b[2]; | ||
return out; | ||
out[0] = a[0] / b[0]; | ||
out[1] = a[1] / b[1]; | ||
out[2] = a[2] / b[2]; | ||
return out; | ||
} | ||
/** | ||
* Math.ceil the components of a vec3 | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a vector to ceil | ||
* @returns {vec3} out | ||
*/ | ||
export function ceil(out, a) { | ||
out[0] = Math.ceil(a[0]); | ||
out[1] = Math.ceil(a[1]); | ||
out[2] = Math.ceil(a[2]); | ||
return out; | ||
out[0] = Math.ceil(a[0]); | ||
out[1] = Math.ceil(a[1]); | ||
out[2] = Math.ceil(a[2]); | ||
return out; | ||
} | ||
/** | ||
* Math.floor the components of a vec3 | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a vector to floor | ||
* @returns {vec3} out | ||
*/ | ||
export function floor(out, a) { | ||
out[0] = Math.floor(a[0]); | ||
out[1] = Math.floor(a[1]); | ||
out[2] = Math.floor(a[2]); | ||
return out; | ||
out[0] = Math.floor(a[0]); | ||
out[1] = Math.floor(a[1]); | ||
out[2] = Math.floor(a[2]); | ||
return out; | ||
} | ||
/** | ||
* Returns the minimum of two vec3's | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @returns {vec3} out | ||
*/ | ||
export function min(out, a, b) { | ||
out[0] = Math.min(a[0], b[0]); | ||
out[1] = Math.min(a[1], b[1]); | ||
out[2] = Math.min(a[2], b[2]); | ||
return out; | ||
out[0] = Math.min(a[0], b[0]); | ||
out[1] = Math.min(a[1], b[1]); | ||
out[2] = Math.min(a[2], b[2]); | ||
return out; | ||
} | ||
/** | ||
* Returns the maximum of two vec3's | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @returns {vec3} out | ||
*/ | ||
export function max(out, a, b) { | ||
out[0] = Math.max(a[0], b[0]); | ||
out[1] = Math.max(a[1], b[1]); | ||
out[2] = Math.max(a[2], b[2]); | ||
return out; | ||
out[0] = Math.max(a[0], b[0]); | ||
out[1] = Math.max(a[1], b[1]); | ||
out[2] = Math.max(a[2], b[2]); | ||
return out; | ||
} | ||
/** | ||
* symmetric round the components of a vec3 | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a vector to round | ||
* @returns {vec3} out | ||
*/ | ||
export function round(out, a) { | ||
out[0] = glMatrix.round(a[0]); | ||
out[1] = glMatrix.round(a[1]); | ||
out[2] = glMatrix.round(a[2]); | ||
return out; | ||
out[0] = glMatrix.round(a[0]); | ||
out[1] = glMatrix.round(a[1]); | ||
out[2] = glMatrix.round(a[2]); | ||
return out; | ||
} | ||
/** | ||
* Scales a vec3 by a scalar number | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the vector to scale | ||
* @param {Number} b amount to scale the vector by | ||
* @returns {vec3} out | ||
*/ | ||
export function scale(out, a, b) { | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
return out; | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
return out; | ||
} | ||
/** | ||
* Adds two vec3's after scaling the second operand by a scalar value | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @param {Number} scale the amount to scale b by before adding | ||
* @returns {vec3} out | ||
*/ | ||
export function scaleAndAdd(out, a, b, scale) { | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
return out; | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
return out; | ||
} | ||
/** | ||
* Calculates the euclidian distance between two vec3's | ||
* | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @returns {Number} distance between a and b | ||
*/ | ||
export function distance(a, b) { | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
const z = b[2] - a[2]; | ||
return Math.sqrt(x * x + y * y + z * z); | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
const z = b[2] - a[2]; | ||
return Math.sqrt(x * x + y * y + z * z); | ||
} | ||
/** | ||
* Calculates the squared euclidian distance between two vec3's | ||
* | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @returns {Number} squared distance between a and b | ||
*/ | ||
export function squaredDistance(a, b) { | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
const z = b[2] - a[2]; | ||
return x * x + y * y + z * z; | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
const z = b[2] - a[2]; | ||
return x * x + y * y + z * z; | ||
} | ||
/** | ||
* Calculates the squared length of a vec3 | ||
* | ||
* @param {ReadonlyVec3} a vector to calculate squared length of | ||
* @returns {Number} squared length of a | ||
*/ | ||
export function squaredLength(a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
return x * x + y * y + z * z; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
return x * x + y * y + z * z; | ||
} | ||
/** | ||
* Negates the components of a vec3 | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a vector to negate | ||
* @returns {vec3} out | ||
*/ | ||
export function negate(out, a) { | ||
out[0] = -a[0]; | ||
out[1] = -a[1]; | ||
out[2] = -a[2]; | ||
return out; | ||
out[0] = -a[0]; | ||
out[1] = -a[1]; | ||
out[2] = -a[2]; | ||
return out; | ||
} | ||
/** | ||
* Returns the inverse of the components of a vec3 | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a vector to invert | ||
* @returns {vec3} out | ||
*/ | ||
export function inverse(out, a) { | ||
out[0] = 1.0 / a[0]; | ||
out[1] = 1.0 / a[1]; | ||
out[2] = 1.0 / a[2]; | ||
return out; | ||
out[0] = 1.0 / a[0]; | ||
out[1] = 1.0 / a[1]; | ||
out[2] = 1.0 / a[2]; | ||
return out; | ||
} | ||
/** | ||
* Normalize a vec3 | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a vector to normalize | ||
* @returns {vec3} out | ||
*/ | ||
export function normalize(out, a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
let len = x * x + y * y + z * z; | ||
if (len > 0) { | ||
len = 1 / Math.sqrt(len); | ||
} | ||
out[0] = a[0] * len; | ||
out[1] = a[1] * len; | ||
out[2] = a[2] * len; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
let len = x * x + y * y + z * z; | ||
if (len > 0) { | ||
// TODO: evaluate use of glm_invsqrt here? | ||
len = 1 / Math.sqrt(len); | ||
} | ||
out[0] = a[0] * len; | ||
out[1] = a[1] * len; | ||
out[2] = a[2] * len; | ||
return out; | ||
} | ||
/** | ||
* Calculates the dot product of two vec3's | ||
* | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @returns {Number} dot product of a and b | ||
*/ | ||
export function dot(a, b) { | ||
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; | ||
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; | ||
} | ||
/** | ||
* Computes the cross product of two vec3's | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @returns {vec3} out | ||
*/ | ||
export function cross(out, a, b) { | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const bx = b[0]; | ||
const by = b[1]; | ||
const bz = b[2]; | ||
out[0] = ay * bz - az * by; | ||
out[1] = az * bx - ax * bz; | ||
out[2] = ax * by - ay * bx; | ||
return out; | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const bx = b[0]; | ||
const by = b[1]; | ||
const bz = b[2]; | ||
out[0] = ay * bz - az * by; | ||
out[1] = az * bx - ax * bz; | ||
out[2] = ax * by - ay * bx; | ||
return out; | ||
} | ||
/** | ||
* Performs a linear interpolation between two vec3's | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs | ||
* @returns {vec3} out | ||
*/ | ||
export function lerp(out, a, b, t) { | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
out[0] = ax + t * (b[0] - ax); | ||
out[1] = ay + t * (b[1] - ay); | ||
out[2] = az + t * (b[2] - az); | ||
return out; | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
out[0] = ax + t * (b[0] - ax); | ||
out[1] = ay + t * (b[1] - ay); | ||
out[2] = az + t * (b[2] - az); | ||
return out; | ||
} | ||
/** | ||
* Performs a spherical linear interpolation between two vec3's | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs | ||
* @returns {vec3} out | ||
*/ | ||
export function slerp(out, a, b, t) { | ||
const angle = Math.acos(Math.min(Math.max(dot(a, b), -1), 1)); | ||
const sinTotal = Math.sin(angle); | ||
const ratioA = Math.sin((1 - t) * angle) / sinTotal; | ||
const ratioB = Math.sin(t * angle) / sinTotal; | ||
out[0] = ratioA * a[0] + ratioB * b[0]; | ||
out[1] = ratioA * a[1] + ratioB * b[1]; | ||
out[2] = ratioA * a[2] + ratioB * b[2]; | ||
return out; | ||
const angle = Math.acos(Math.min(Math.max(dot(a, b), -1), 1)); | ||
const sinTotal = Math.sin(angle); | ||
const ratioA = Math.sin((1 - t) * angle) / sinTotal; | ||
const ratioB = Math.sin(t * angle) / sinTotal; | ||
out[0] = ratioA * a[0] + ratioB * b[0]; | ||
out[1] = ratioA * a[1] + ratioB * b[1]; | ||
out[2] = ratioA * a[2] + ratioB * b[2]; | ||
return out; | ||
} | ||
/** | ||
* Performs a hermite interpolation with two control points | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @param {ReadonlyVec3} c the third operand | ||
* @param {ReadonlyVec3} d the fourth operand | ||
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs | ||
* @returns {vec3} out | ||
*/ | ||
export function hermite(out, a, b, c, d, t) { | ||
const factorTimes2 = t * t; | ||
const factor1 = factorTimes2 * (2 * t - 3) + 1; | ||
const factor2 = factorTimes2 * (t - 2) + t; | ||
const factor3 = factorTimes2 * (t - 1); | ||
const factor4 = factorTimes2 * (3 - 2 * t); | ||
out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; | ||
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; | ||
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; | ||
return out; | ||
const factorTimes2 = t * t; | ||
const factor1 = factorTimes2 * (2 * t - 3) + 1; | ||
const factor2 = factorTimes2 * (t - 2) + t; | ||
const factor3 = factorTimes2 * (t - 1); | ||
const factor4 = factorTimes2 * (3 - 2 * t); | ||
out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; | ||
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; | ||
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; | ||
return out; | ||
} | ||
/** | ||
* Performs a bezier interpolation with two control points | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the first operand | ||
* @param {ReadonlyVec3} b the second operand | ||
* @param {ReadonlyVec3} c the third operand | ||
* @param {ReadonlyVec3} d the fourth operand | ||
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs | ||
* @returns {vec3} out | ||
*/ | ||
export function bezier(out, a, b, c, d, t) { | ||
const inverseFactor = 1 - t; | ||
const inverseFactorTimesTwo = inverseFactor * inverseFactor; | ||
const factorTimes2 = t * t; | ||
const factor1 = inverseFactorTimesTwo * inverseFactor; | ||
const factor2 = 3 * t * inverseFactorTimesTwo; | ||
const factor3 = 3 * factorTimes2 * inverseFactor; | ||
const factor4 = factorTimes2 * t; | ||
out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; | ||
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; | ||
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; | ||
return out; | ||
const inverseFactor = 1 - t; | ||
const inverseFactorTimesTwo = inverseFactor * inverseFactor; | ||
const factorTimes2 = t * t; | ||
const factor1 = inverseFactorTimesTwo * inverseFactor; | ||
const factor2 = 3 * t * inverseFactorTimesTwo; | ||
const factor3 = 3 * factorTimes2 * inverseFactor; | ||
const factor4 = factorTimes2 * t; | ||
out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; | ||
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; | ||
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; | ||
return out; | ||
} | ||
/** | ||
* Generates a random vector with the given scale | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {Number} [scale] Length of the resulting vector. If omitted, a unit vector will be returned | ||
* @returns {vec3} out | ||
*/ | ||
export function random(out, scale) { | ||
scale = scale === undefined ? 1.0 : scale; | ||
const r = glMatrix.RANDOM() * 2.0 * Math.PI; | ||
const z = glMatrix.RANDOM() * 2.0 - 1.0; | ||
const zScale = Math.sqrt(1.0 - z * z) * scale; | ||
out[0] = Math.cos(r) * zScale; | ||
out[1] = Math.sin(r) * zScale; | ||
out[2] = z * scale; | ||
return out; | ||
scale = scale === undefined ? 1.0 : scale; | ||
const r = glMatrix.RANDOM() * 2.0 * Math.PI; | ||
const z = glMatrix.RANDOM() * 2.0 - 1.0; | ||
const zScale = Math.sqrt(1.0 - z * z) * scale; | ||
out[0] = Math.cos(r) * zScale; | ||
out[1] = Math.sin(r) * zScale; | ||
out[2] = z * scale; | ||
return out; | ||
} | ||
/** | ||
* Transforms the vec3 with a mat4. | ||
* 4th vector component is implicitly '1' | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the vector to transform | ||
* @param {ReadonlyMat4} m matrix to transform with | ||
* @returns {vec3} out | ||
*/ | ||
export function transformMat4(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
let w = m[3] * x + m[7] * y + m[11] * z + m[15]; | ||
w = w || 1.0; | ||
out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w; | ||
out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w; | ||
out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
let w = m[3] * x + m[7] * y + m[11] * z + m[15]; | ||
w = w || 1.0; | ||
out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w; | ||
out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w; | ||
out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w; | ||
return out; | ||
} | ||
/** | ||
* Transforms the vec3 with a mat3. | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the vector to transform | ||
* @param {ReadonlyMat3} m the 3x3 matrix to transform with | ||
* @returns {vec3} out | ||
*/ | ||
export function transformMat3(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
out[0] = x * m[0] + y * m[3] + z * m[6]; | ||
out[1] = x * m[1] + y * m[4] + z * m[7]; | ||
out[2] = x * m[2] + y * m[5] + z * m[8]; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
out[0] = x * m[0] + y * m[3] + z * m[6]; | ||
out[1] = x * m[1] + y * m[4] + z * m[7]; | ||
out[2] = x * m[2] + y * m[5] + z * m[8]; | ||
return out; | ||
} | ||
/** | ||
* Transforms the vec3 with a quat | ||
* Can also be used for dual quaternions. (Multiply it with the real part) | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @param {ReadonlyVec3} a the vector to transform | ||
* @param {ReadonlyQuat} q quaternion to transform with | ||
* @returns {vec3} out | ||
*/ | ||
export function transformQuat(out, a, q) { | ||
const qx = q[0]; | ||
const qy = q[1]; | ||
const qz = q[2]; | ||
const qw = q[3]; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
let uvx = qy * z - qz * y; | ||
let uvy = qz * x - qx * z; | ||
let uvz = qx * y - qy * x; | ||
let uuvx = qy * uvz - qz * uvy; | ||
let uuvy = qz * uvx - qx * uvz; | ||
let uuvz = qx * uvy - qy * uvx; | ||
const w2 = qw * 2; | ||
uvx *= w2; | ||
uvy *= w2; | ||
uvz *= w2; | ||
uuvx *= 2; | ||
uuvy *= 2; | ||
uuvz *= 2; | ||
out[0] = x + uvx + uuvx; | ||
out[1] = y + uvy + uuvy; | ||
out[2] = z + uvz + uuvz; | ||
return out; | ||
// benchmarks: https://jsperf.com/quaternion-transform-vec3-implementations-fixed | ||
const qx = q[0]; | ||
const qy = q[1]; | ||
const qz = q[2]; | ||
const qw = q[3]; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
// var qvec = [qx, qy, qz]; | ||
// var uv = vec3.cross([], qvec, a); | ||
let uvx = qy * z - qz * y; | ||
let uvy = qz * x - qx * z; | ||
let uvz = qx * y - qy * x; | ||
// var uuv = vec3.cross([], qvec, uv); | ||
let uuvx = qy * uvz - qz * uvy; | ||
let uuvy = qz * uvx - qx * uvz; | ||
let uuvz = qx * uvy - qy * uvx; | ||
// vec3.scale(uv, uv, 2 * w); | ||
const w2 = qw * 2; | ||
uvx *= w2; | ||
uvy *= w2; | ||
uvz *= w2; | ||
// vec3.scale(uuv, uuv, 2); | ||
uuvx *= 2; | ||
uuvy *= 2; | ||
uuvz *= 2; | ||
// return vec3.add(out, a, vec3.add(out, uv, uuv)); | ||
out[0] = x + uvx + uuvx; | ||
out[1] = y + uvy + uuvy; | ||
out[2] = z + uvz + uuvz; | ||
return out; | ||
} | ||
/** | ||
* Rotate a 3D vector around the x-axis | ||
* @param {vec3} out The receiving vec3 | ||
* @param {ReadonlyVec3} a The vec3 point to rotate | ||
* @param {ReadonlyVec3} b The origin of the rotation | ||
* @param {Number} rad The angle of rotation in radians | ||
* @returns {vec3} out | ||
*/ | ||
export function rotateX(out, a, b, rad) { | ||
const p = []; | ||
const r = []; | ||
p[0] = a[0] - b[0]; | ||
p[1] = a[1] - b[1]; | ||
p[2] = a[2] - b[2]; | ||
r[0] = p[0]; | ||
r[1] = p[1] * Math.cos(rad) - p[2] * Math.sin(rad); | ||
r[2] = p[1] * Math.sin(rad) + p[2] * Math.cos(rad); | ||
out[0] = r[0] + b[0]; | ||
out[1] = r[1] + b[1]; | ||
out[2] = r[2] + b[2]; | ||
return out; | ||
const p = []; | ||
const r = []; | ||
// Translate point to the origin | ||
p[0] = a[0] - b[0]; | ||
p[1] = a[1] - b[1]; | ||
p[2] = a[2] - b[2]; | ||
// perform rotation | ||
r[0] = p[0]; | ||
r[1] = p[1] * Math.cos(rad) - p[2] * Math.sin(rad); | ||
r[2] = p[1] * Math.sin(rad) + p[2] * Math.cos(rad); | ||
// translate to correct position | ||
out[0] = r[0] + b[0]; | ||
out[1] = r[1] + b[1]; | ||
out[2] = r[2] + b[2]; | ||
return out; | ||
} | ||
/** | ||
* Rotate a 3D vector around the y-axis | ||
* @param {vec3} out The receiving vec3 | ||
* @param {ReadonlyVec3} a The vec3 point to rotate | ||
* @param {ReadonlyVec3} b The origin of the rotation | ||
* @param {Number} rad The angle of rotation in radians | ||
* @returns {vec3} out | ||
*/ | ||
export function rotateY(out, a, b, rad) { | ||
const p = []; | ||
const r = []; | ||
p[0] = a[0] - b[0]; | ||
p[1] = a[1] - b[1]; | ||
p[2] = a[2] - b[2]; | ||
r[0] = p[2] * Math.sin(rad) + p[0] * Math.cos(rad); | ||
r[1] = p[1]; | ||
r[2] = p[2] * Math.cos(rad) - p[0] * Math.sin(rad); | ||
out[0] = r[0] + b[0]; | ||
out[1] = r[1] + b[1]; | ||
out[2] = r[2] + b[2]; | ||
return out; | ||
const p = []; | ||
const r = []; | ||
// Translate point to the origin | ||
p[0] = a[0] - b[0]; | ||
p[1] = a[1] - b[1]; | ||
p[2] = a[2] - b[2]; | ||
// perform rotation | ||
r[0] = p[2] * Math.sin(rad) + p[0] * Math.cos(rad); | ||
r[1] = p[1]; | ||
r[2] = p[2] * Math.cos(rad) - p[0] * Math.sin(rad); | ||
// translate to correct position | ||
out[0] = r[0] + b[0]; | ||
out[1] = r[1] + b[1]; | ||
out[2] = r[2] + b[2]; | ||
return out; | ||
} | ||
/** | ||
* Rotate a 3D vector around the z-axis | ||
* @param {vec3} out The receiving vec3 | ||
* @param {ReadonlyVec3} a The vec3 point to rotate | ||
* @param {ReadonlyVec3} b The origin of the rotation | ||
* @param {Number} rad The angle of rotation in radians | ||
* @returns {vec3} out | ||
*/ | ||
export function rotateZ(out, a, b, rad) { | ||
const p = []; | ||
const r = []; | ||
p[0] = a[0] - b[0]; | ||
p[1] = a[1] - b[1]; | ||
p[2] = a[2] - b[2]; | ||
r[0] = p[0] * Math.cos(rad) - p[1] * Math.sin(rad); | ||
r[1] = p[0] * Math.sin(rad) + p[1] * Math.cos(rad); | ||
r[2] = p[2]; | ||
out[0] = r[0] + b[0]; | ||
out[1] = r[1] + b[1]; | ||
out[2] = r[2] + b[2]; | ||
return out; | ||
const p = []; | ||
const r = []; | ||
// Translate point to the origin | ||
p[0] = a[0] - b[0]; | ||
p[1] = a[1] - b[1]; | ||
p[2] = a[2] - b[2]; | ||
// perform rotation | ||
r[0] = p[0] * Math.cos(rad) - p[1] * Math.sin(rad); | ||
r[1] = p[0] * Math.sin(rad) + p[1] * Math.cos(rad); | ||
r[2] = p[2]; | ||
// translate to correct position | ||
out[0] = r[0] + b[0]; | ||
out[1] = r[1] + b[1]; | ||
out[2] = r[2] + b[2]; | ||
return out; | ||
} | ||
/** | ||
* Get the angle between two 3D vectors | ||
* @param {ReadonlyVec3} a The first operand | ||
* @param {ReadonlyVec3} b The second operand | ||
* @returns {Number} The angle in radians | ||
*/ | ||
export function angle(a, b) { | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const bx = b[0]; | ||
const by = b[1]; | ||
const bz = b[2]; | ||
const mag = Math.sqrt((ax * ax + ay * ay + az * az) * (bx * bx + by * by + bz * bz)); | ||
const cosine = mag && dot(a, b) / mag; | ||
return Math.acos(Math.min(Math.max(cosine, -1), 1)); | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const bx = b[0]; | ||
const by = b[1]; | ||
const bz = b[2]; | ||
const mag = Math.sqrt((ax * ax + ay * ay + az * az) * (bx * bx + by * by + bz * bz)); | ||
const cosine = mag && dot(a, b) / mag; | ||
return Math.acos(Math.min(Math.max(cosine, -1), 1)); | ||
} | ||
/** | ||
* Set the components of a vec3 to zero | ||
* | ||
* @param {vec3} out the receiving vector | ||
* @returns {vec3} out | ||
*/ | ||
export function zero(out) { | ||
out[0] = 0.0; | ||
out[1] = 0.0; | ||
out[2] = 0.0; | ||
return out; | ||
out[0] = 0.0; | ||
out[1] = 0.0; | ||
out[2] = 0.0; | ||
return out; | ||
} | ||
/** | ||
* Returns a string representation of a vector | ||
* | ||
* @param {ReadonlyVec3} a vector to represent as a string | ||
* @returns {String} string representation of the vector | ||
*/ | ||
export function str(a) { | ||
return "vec3(".concat(a[0], ", ").concat(a[1], ", ").concat(a[2], ")"); | ||
return `vec3(${a[0]}, ${a[1]}, ${a[2]})`; | ||
} | ||
/** | ||
* Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===) | ||
* | ||
* @param {ReadonlyVec3} a The first vector. | ||
* @param {ReadonlyVec3} b The second vector. | ||
* @returns {Boolean} True if the vectors are equal, false otherwise. | ||
*/ | ||
export function exactEquals(a, b) { | ||
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2]; | ||
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2]; | ||
} | ||
/** | ||
* Returns whether or not the vectors have approximately the same elements in the same position. | ||
* | ||
* @param {ReadonlyVec3} a The first vector. | ||
* @param {ReadonlyVec3} b The second vector. | ||
* @returns {Boolean} True if the vectors are equal, false otherwise. | ||
*/ | ||
export function equals(a, b) { | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const a2 = a[2]; | ||
const b0 = b[0]; | ||
const b1 = b[1]; | ||
const b2 = b[2]; | ||
return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)); | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const a2 = a[2]; | ||
const b0 = b[0]; | ||
const b1 = b[1]; | ||
const b2 = b[2]; | ||
return (Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | ||
Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | ||
Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2))); | ||
} | ||
/** | ||
* Alias for {@link vec3.subtract} | ||
* @function | ||
*/ | ||
export const sub = subtract; | ||
/** | ||
* Alias for {@link vec3.multiply} | ||
* @function | ||
*/ | ||
export const mul = multiply; | ||
/** | ||
* Alias for {@link vec3.divide} | ||
* @function | ||
*/ | ||
export const div = divide; | ||
/** | ||
* Alias for {@link vec3.distance} | ||
* @function | ||
*/ | ||
export const dist = distance; | ||
/** | ||
* Alias for {@link vec3.squaredDistance} | ||
* @function | ||
*/ | ||
export const sqrDist = squaredDistance; | ||
/** | ||
* Alias for {@link vec3.length} | ||
* @function | ||
*/ | ||
export const len = length; | ||
/** | ||
* Alias for {@link vec3.squaredLength} | ||
* @function | ||
*/ | ||
export const sqrLen = squaredLength; | ||
export const forEach = function () { | ||
const vec = create(); | ||
return function (a, stride, offset, count, fn, arg) { | ||
let i; | ||
let l; | ||
if (!stride) { | ||
stride = 3; | ||
} | ||
if (!offset) { | ||
offset = 0; | ||
} | ||
if (count) { | ||
l = Math.min(count * stride + offset, a.length); | ||
} else { | ||
l = a.length; | ||
} | ||
for (i = offset; i < l; i += stride) { | ||
vec[0] = a[i]; | ||
vec[1] = a[i + 1]; | ||
vec[2] = a[i + 2]; | ||
fn(vec, vec, arg); | ||
a[i] = vec[0]; | ||
a[i + 1] = vec[1]; | ||
a[i + 2] = vec[2]; | ||
} | ||
return a; | ||
}; | ||
}(); | ||
//# sourceMappingURL=vec3.js.map | ||
/** | ||
* Perform some operation over an array of vec3s. | ||
* | ||
* @param {Array} a the array of vectors to iterate over | ||
* @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed | ||
* @param {Number} offset Number of elements to skip at the beginning of the array | ||
* @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array | ||
* @param {Function} fn Function to call for each vector in the array | ||
* @param {Object} [arg] additional argument to pass to fn | ||
* @returns {Array} a | ||
* @function | ||
*/ | ||
export const forEach = (function () { | ||
const vec = create(); | ||
return function (a, stride, offset, count, fn, arg) { | ||
let i; | ||
let l; | ||
if (!stride) { | ||
stride = 3; | ||
} | ||
if (!offset) { | ||
offset = 0; | ||
} | ||
if (count) { | ||
l = Math.min(count * stride + offset, a.length); | ||
} | ||
else { | ||
l = a.length; | ||
} | ||
for (i = offset; i < l; i += stride) { | ||
vec[0] = a[i]; | ||
vec[1] = a[i + 1]; | ||
vec[2] = a[i + 2]; | ||
fn(vec, vec, arg); | ||
a[i] = vec[0]; | ||
a[i + 1] = vec[1]; | ||
a[i + 2] = vec[2]; | ||
} | ||
return a; | ||
}; | ||
})(); |
@@ -330,2 +330,1 @@ /** | ||
export declare const forEach: (a: any, stride: any, offset: any, count: any, fn: any, arg: any) => any; | ||
//# sourceMappingURL=vec4.d.ts.map |
@@ -0,334 +1,620 @@ | ||
// @eslint-disable | ||
// @ts-nocheck | ||
import * as glMatrix from './common.js'; | ||
/** | ||
* 4 Dimensional Vector | ||
* @module vec4 | ||
*/ | ||
/** | ||
* Creates a new, empty vec4 | ||
* | ||
* @returns {vec4} a new 4D vector | ||
*/ | ||
export function create() { | ||
const out = new glMatrix.ARRAY_TYPE(4); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
} | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(4); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 0; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Creates a new vec4 initialized with values from an existing vector | ||
* | ||
* @param {ReadonlyVec4} a vector to clone | ||
* @returns {vec4} a new 4D vector | ||
*/ | ||
export function clone(a) { | ||
const out = new glMatrix.ARRAY_TYPE(4); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(4); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
} | ||
/** | ||
* Creates a new vec4 initialized with the given values | ||
* | ||
* @param {Number} x X component | ||
* @param {Number} y Y component | ||
* @param {Number} z Z component | ||
* @param {Number} w W component | ||
* @returns {vec4} a new 4D vector | ||
*/ | ||
export function fromValues(x, y, z, w) { | ||
const out = new glMatrix.ARRAY_TYPE(4); | ||
out[0] = x; | ||
out[1] = y; | ||
out[2] = z; | ||
out[3] = w; | ||
return out; | ||
const out = new glMatrix.ARRAY_TYPE(4); | ||
out[0] = x; | ||
out[1] = y; | ||
out[2] = z; | ||
out[3] = w; | ||
return out; | ||
} | ||
/** | ||
* Copy the values from one vec4 to another | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the source vector | ||
* @returns {vec4} out | ||
*/ | ||
export function copy(out, a) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
} | ||
/** | ||
* Set the components of a vec4 to the given values | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {Number} x X component | ||
* @param {Number} y Y component | ||
* @param {Number} z Z component | ||
* @param {Number} w W component | ||
* @returns {vec4} out | ||
*/ | ||
export function set(out, x, y, z, w) { | ||
out[0] = x; | ||
out[1] = y; | ||
out[2] = z; | ||
out[3] = w; | ||
return out; | ||
out[0] = x; | ||
out[1] = y; | ||
out[2] = z; | ||
out[3] = w; | ||
return out; | ||
} | ||
/** | ||
* Adds two vec4's | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the first operand | ||
* @param {ReadonlyVec4} b the second operand | ||
* @returns {vec4} out | ||
*/ | ||
export function add(out, a, b) { | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
return out; | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
return out; | ||
} | ||
/** | ||
* Subtracts vector b from vector a | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the first operand | ||
* @param {ReadonlyVec4} b the second operand | ||
* @returns {vec4} out | ||
*/ | ||
export function subtract(out, a, b) { | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
out[3] = a[3] - b[3]; | ||
return out; | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
out[3] = a[3] - b[3]; | ||
return out; | ||
} | ||
/** | ||
* Multiplies two vec4's | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the first operand | ||
* @param {ReadonlyVec4} b the second operand | ||
* @returns {vec4} out | ||
*/ | ||
export function multiply(out, a, b) { | ||
out[0] = a[0] * b[0]; | ||
out[1] = a[1] * b[1]; | ||
out[2] = a[2] * b[2]; | ||
out[3] = a[3] * b[3]; | ||
return out; | ||
out[0] = a[0] * b[0]; | ||
out[1] = a[1] * b[1]; | ||
out[2] = a[2] * b[2]; | ||
out[3] = a[3] * b[3]; | ||
return out; | ||
} | ||
/** | ||
* Divides two vec4's | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the first operand | ||
* @param {ReadonlyVec4} b the second operand | ||
* @returns {vec4} out | ||
*/ | ||
export function divide(out, a, b) { | ||
out[0] = a[0] / b[0]; | ||
out[1] = a[1] / b[1]; | ||
out[2] = a[2] / b[2]; | ||
out[3] = a[3] / b[3]; | ||
return out; | ||
out[0] = a[0] / b[0]; | ||
out[1] = a[1] / b[1]; | ||
out[2] = a[2] / b[2]; | ||
out[3] = a[3] / b[3]; | ||
return out; | ||
} | ||
/** | ||
* Math.ceil the components of a vec4 | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a vector to ceil | ||
* @returns {vec4} out | ||
*/ | ||
export function ceil(out, a) { | ||
out[0] = Math.ceil(a[0]); | ||
out[1] = Math.ceil(a[1]); | ||
out[2] = Math.ceil(a[2]); | ||
out[3] = Math.ceil(a[3]); | ||
return out; | ||
out[0] = Math.ceil(a[0]); | ||
out[1] = Math.ceil(a[1]); | ||
out[2] = Math.ceil(a[2]); | ||
out[3] = Math.ceil(a[3]); | ||
return out; | ||
} | ||
/** | ||
* Math.floor the components of a vec4 | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a vector to floor | ||
* @returns {vec4} out | ||
*/ | ||
export function floor(out, a) { | ||
out[0] = Math.floor(a[0]); | ||
out[1] = Math.floor(a[1]); | ||
out[2] = Math.floor(a[2]); | ||
out[3] = Math.floor(a[3]); | ||
return out; | ||
out[0] = Math.floor(a[0]); | ||
out[1] = Math.floor(a[1]); | ||
out[2] = Math.floor(a[2]); | ||
out[3] = Math.floor(a[3]); | ||
return out; | ||
} | ||
/** | ||
* Returns the minimum of two vec4's | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the first operand | ||
* @param {ReadonlyVec4} b the second operand | ||
* @returns {vec4} out | ||
*/ | ||
export function min(out, a, b) { | ||
out[0] = Math.min(a[0], b[0]); | ||
out[1] = Math.min(a[1], b[1]); | ||
out[2] = Math.min(a[2], b[2]); | ||
out[3] = Math.min(a[3], b[3]); | ||
return out; | ||
out[0] = Math.min(a[0], b[0]); | ||
out[1] = Math.min(a[1], b[1]); | ||
out[2] = Math.min(a[2], b[2]); | ||
out[3] = Math.min(a[3], b[3]); | ||
return out; | ||
} | ||
/** | ||
* Returns the maximum of two vec4's | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the first operand | ||
* @param {ReadonlyVec4} b the second operand | ||
* @returns {vec4} out | ||
*/ | ||
export function max(out, a, b) { | ||
out[0] = Math.max(a[0], b[0]); | ||
out[1] = Math.max(a[1], b[1]); | ||
out[2] = Math.max(a[2], b[2]); | ||
out[3] = Math.max(a[3], b[3]); | ||
return out; | ||
out[0] = Math.max(a[0], b[0]); | ||
out[1] = Math.max(a[1], b[1]); | ||
out[2] = Math.max(a[2], b[2]); | ||
out[3] = Math.max(a[3], b[3]); | ||
return out; | ||
} | ||
/** | ||
* symmetric round the components of a vec4 | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a vector to round | ||
* @returns {vec4} out | ||
*/ | ||
export function round(out, a) { | ||
out[0] = glMatrix.round(a[0]); | ||
out[1] = glMatrix.round(a[1]); | ||
out[2] = glMatrix.round(a[2]); | ||
out[3] = glMatrix.round(a[3]); | ||
return out; | ||
out[0] = glMatrix.round(a[0]); | ||
out[1] = glMatrix.round(a[1]); | ||
out[2] = glMatrix.round(a[2]); | ||
out[3] = glMatrix.round(a[3]); | ||
return out; | ||
} | ||
/** | ||
* Scales a vec4 by a scalar number | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the vector to scale | ||
* @param {Number} b amount to scale the vector by | ||
* @returns {vec4} out | ||
*/ | ||
export function scale(out, a, b) { | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
return out; | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
return out; | ||
} | ||
/** | ||
* Adds two vec4's after scaling the second operand by a scalar value | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the first operand | ||
* @param {ReadonlyVec4} b the second operand | ||
* @param {Number} scale the amount to scale b by before adding | ||
* @returns {vec4} out | ||
*/ | ||
export function scaleAndAdd(out, a, b, scale) { | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
out[3] = a[3] + b[3] * scale; | ||
return out; | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
out[3] = a[3] + b[3] * scale; | ||
return out; | ||
} | ||
/** | ||
* Calculates the euclidian distance between two vec4's | ||
* | ||
* @param {ReadonlyVec4} a the first operand | ||
* @param {ReadonlyVec4} b the second operand | ||
* @returns {Number} distance between a and b | ||
*/ | ||
export function distance(a, b) { | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
const z = b[2] - a[2]; | ||
const w = b[3] - a[3]; | ||
return Math.sqrt(x * x + y * y + z * z + w * w); | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
const z = b[2] - a[2]; | ||
const w = b[3] - a[3]; | ||
return Math.sqrt(x * x + y * y + z * z + w * w); | ||
} | ||
/** | ||
* Calculates the squared euclidian distance between two vec4's | ||
* | ||
* @param {ReadonlyVec4} a the first operand | ||
* @param {ReadonlyVec4} b the second operand | ||
* @returns {Number} squared distance between a and b | ||
*/ | ||
export function squaredDistance(a, b) { | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
const z = b[2] - a[2]; | ||
const w = b[3] - a[3]; | ||
return x * x + y * y + z * z + w * w; | ||
const x = b[0] - a[0]; | ||
const y = b[1] - a[1]; | ||
const z = b[2] - a[2]; | ||
const w = b[3] - a[3]; | ||
return x * x + y * y + z * z + w * w; | ||
} | ||
/** | ||
* Calculates the length of a vec4 | ||
* | ||
* @param {ReadonlyVec4} a vector to calculate length of | ||
* @returns {Number} length of a | ||
*/ | ||
export function length(a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
return Math.sqrt(x * x + y * y + z * z + w * w); | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
return Math.sqrt(x * x + y * y + z * z + w * w); | ||
} | ||
/** | ||
* Calculates the squared length of a vec4 | ||
* | ||
* @param {ReadonlyVec4} a vector to calculate squared length of | ||
* @returns {Number} squared length of a | ||
*/ | ||
export function squaredLength(a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
return x * x + y * y + z * z + w * w; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
return x * x + y * y + z * z + w * w; | ||
} | ||
/** | ||
* Negates the components of a vec4 | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a vector to negate | ||
* @returns {vec4} out | ||
*/ | ||
export function negate(out, a) { | ||
out[0] = -a[0]; | ||
out[1] = -a[1]; | ||
out[2] = -a[2]; | ||
out[3] = -a[3]; | ||
return out; | ||
out[0] = -a[0]; | ||
out[1] = -a[1]; | ||
out[2] = -a[2]; | ||
out[3] = -a[3]; | ||
return out; | ||
} | ||
/** | ||
* Returns the inverse of the components of a vec4 | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a vector to invert | ||
* @returns {vec4} out | ||
*/ | ||
export function inverse(out, a) { | ||
out[0] = 1.0 / a[0]; | ||
out[1] = 1.0 / a[1]; | ||
out[2] = 1.0 / a[2]; | ||
out[3] = 1.0 / a[3]; | ||
return out; | ||
out[0] = 1.0 / a[0]; | ||
out[1] = 1.0 / a[1]; | ||
out[2] = 1.0 / a[2]; | ||
out[3] = 1.0 / a[3]; | ||
return out; | ||
} | ||
/** | ||
* Normalize a vec4 | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a vector to normalize | ||
* @returns {vec4} out | ||
*/ | ||
export function normalize(out, a) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
let len = x * x + y * y + z * z + w * w; | ||
if (len > 0) { | ||
len = 1 / Math.sqrt(len); | ||
} | ||
out[0] = x * len; | ||
out[1] = y * len; | ||
out[2] = z * len; | ||
out[3] = w * len; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
let len = x * x + y * y + z * z + w * w; | ||
if (len > 0) { | ||
len = 1 / Math.sqrt(len); | ||
} | ||
out[0] = x * len; | ||
out[1] = y * len; | ||
out[2] = z * len; | ||
out[3] = w * len; | ||
return out; | ||
} | ||
/** | ||
* Calculates the dot product of two vec4's | ||
* | ||
* @param {ReadonlyVec4} a the first operand | ||
* @param {ReadonlyVec4} b the second operand | ||
* @returns {Number} dot product of a and b | ||
*/ | ||
export function dot(a, b) { | ||
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; | ||
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; | ||
} | ||
/** | ||
* Returns the cross-product of three vectors in a 4-dimensional space | ||
* | ||
* @param {ReadonlyVec4} result the receiving vector | ||
* @param {ReadonlyVec4} U the first vector | ||
* @param {ReadonlyVec4} V the second vector | ||
* @param {ReadonlyVec4} W the third vector | ||
* @returns {vec4} result | ||
*/ | ||
export function cross(out, u, v, w) { | ||
const A = v[0] * w[1] - v[1] * w[0]; | ||
const B = v[0] * w[2] - v[2] * w[0]; | ||
const C = v[0] * w[3] - v[3] * w[0]; | ||
const D = v[1] * w[2] - v[2] * w[1]; | ||
const E = v[1] * w[3] - v[3] * w[1]; | ||
const F = v[2] * w[3] - v[3] * w[2]; | ||
const G = u[0]; | ||
const H = u[1]; | ||
const I = u[2]; | ||
const J = u[3]; | ||
out[0] = H * F - I * E + J * D; | ||
out[1] = -(G * F) + I * C - J * B; | ||
out[2] = G * E - H * C + J * A; | ||
out[3] = -(G * D) + H * B - I * A; | ||
return out; | ||
const A = v[0] * w[1] - v[1] * w[0]; | ||
const B = v[0] * w[2] - v[2] * w[0]; | ||
const C = v[0] * w[3] - v[3] * w[0]; | ||
const D = v[1] * w[2] - v[2] * w[1]; | ||
const E = v[1] * w[3] - v[3] * w[1]; | ||
const F = v[2] * w[3] - v[3] * w[2]; | ||
const G = u[0]; | ||
const H = u[1]; | ||
const I = u[2]; | ||
const J = u[3]; | ||
out[0] = H * F - I * E + J * D; | ||
out[1] = -(G * F) + I * C - J * B; | ||
out[2] = G * E - H * C + J * A; | ||
out[3] = -(G * D) + H * B - I * A; | ||
return out; | ||
} | ||
/** | ||
* Performs a linear interpolation between two vec4's | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the first operand | ||
* @param {ReadonlyVec4} b the second operand | ||
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs | ||
* @returns {vec4} out | ||
*/ | ||
export function lerp(out, a, b, t) { | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
out[0] = ax + t * (b[0] - ax); | ||
out[1] = ay + t * (b[1] - ay); | ||
out[2] = az + t * (b[2] - az); | ||
out[3] = aw + t * (b[3] - aw); | ||
return out; | ||
const ax = a[0]; | ||
const ay = a[1]; | ||
const az = a[2]; | ||
const aw = a[3]; | ||
out[0] = ax + t * (b[0] - ax); | ||
out[1] = ay + t * (b[1] - ay); | ||
out[2] = az + t * (b[2] - az); | ||
out[3] = aw + t * (b[3] - aw); | ||
return out; | ||
} | ||
/** | ||
* Generates a random vector with the given scale | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {Number} [scale] Length of the resulting vector. If omitted, a unit vector will be returned | ||
* @returns {vec4} out | ||
*/ | ||
export function random(out, scale) { | ||
scale = scale === undefined ? 1.0 : scale; | ||
let v1; | ||
let v2; | ||
let v3; | ||
let v4; | ||
let s1; | ||
let s2; | ||
do { | ||
v1 = glMatrix.RANDOM() * 2 - 1; | ||
v2 = glMatrix.RANDOM() * 2 - 1; | ||
s1 = v1 * v1 + v2 * v2; | ||
} while (s1 >= 1); | ||
do { | ||
v3 = glMatrix.RANDOM() * 2 - 1; | ||
v4 = glMatrix.RANDOM() * 2 - 1; | ||
s2 = v3 * v3 + v4 * v4; | ||
} while (s2 >= 1); | ||
const d = Math.sqrt((1 - s1) / s2); | ||
out[0] = scale * v1; | ||
out[1] = scale * v2; | ||
out[2] = scale * v3 * d; | ||
out[3] = scale * v4 * d; | ||
return out; | ||
scale = scale === undefined ? 1.0 : scale; | ||
// Marsaglia, George. Choosing a Point from the Surface of a | ||
// Sphere. Ann. Math. Statist. 43 (1972), no. 2, 645--646. | ||
// http://projecteuclid.org/euclid.aoms/1177692644; | ||
let v1; | ||
let v2; | ||
let v3; | ||
let v4; | ||
let s1; | ||
let s2; | ||
do { | ||
v1 = glMatrix.RANDOM() * 2 - 1; | ||
v2 = glMatrix.RANDOM() * 2 - 1; | ||
s1 = v1 * v1 + v2 * v2; | ||
} while (s1 >= 1); | ||
do { | ||
v3 = glMatrix.RANDOM() * 2 - 1; | ||
v4 = glMatrix.RANDOM() * 2 - 1; | ||
s2 = v3 * v3 + v4 * v4; | ||
} while (s2 >= 1); | ||
const d = Math.sqrt((1 - s1) / s2); | ||
out[0] = scale * v1; | ||
out[1] = scale * v2; | ||
out[2] = scale * v3 * d; | ||
out[3] = scale * v4 * d; | ||
return out; | ||
} | ||
/** | ||
* Transforms the vec4 with a mat4. | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the vector to transform | ||
* @param {ReadonlyMat4} m matrix to transform with | ||
* @returns {vec4} out | ||
*/ | ||
export function transformMat4(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w; | ||
out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w; | ||
out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w; | ||
out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = a[3]; | ||
out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w; | ||
out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w; | ||
out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w; | ||
out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w; | ||
return out; | ||
} | ||
/** | ||
* Transforms the vec4 with a quat | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @param {ReadonlyVec4} a the vector to transform | ||
* @param {ReadonlyQuat} q quaternion to transform with | ||
* @returns {vec4} out | ||
*/ | ||
export function transformQuat(out, a, q) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const qx = q[0]; | ||
const qy = q[1]; | ||
const qz = q[2]; | ||
const qw = q[3]; | ||
const ix = qw * x + qy * z - qz * y; | ||
const iy = qw * y + qz * x - qx * z; | ||
const iz = qw * z + qx * y - qy * x; | ||
const iw = -qx * x - qy * y - qz * z; | ||
out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; | ||
out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; | ||
out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; | ||
out[3] = a[3]; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const qx = q[0]; | ||
const qy = q[1]; | ||
const qz = q[2]; | ||
const qw = q[3]; | ||
// calculate quat * vec | ||
const ix = qw * x + qy * z - qz * y; | ||
const iy = qw * y + qz * x - qx * z; | ||
const iz = qw * z + qx * y - qy * x; | ||
const iw = -qx * x - qy * y - qz * z; | ||
// calculate result * inverse quat | ||
out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; | ||
out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; | ||
out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; | ||
out[3] = a[3]; | ||
return out; | ||
} | ||
/** | ||
* Set the components of a vec4 to zero | ||
* | ||
* @param {vec4} out the receiving vector | ||
* @returns {vec4} out | ||
*/ | ||
export function zero(out) { | ||
out[0] = 0.0; | ||
out[1] = 0.0; | ||
out[2] = 0.0; | ||
out[3] = 0.0; | ||
return out; | ||
out[0] = 0.0; | ||
out[1] = 0.0; | ||
out[2] = 0.0; | ||
out[3] = 0.0; | ||
return out; | ||
} | ||
/** | ||
* Returns a string representation of a vector | ||
* | ||
* @param {ReadonlyVec4} a vector to represent as a string | ||
* @returns {String} string representation of the vector | ||
*/ | ||
export function str(a) { | ||
return "vec4(".concat(a[0], ", ").concat(a[1], ", ").concat(a[2], ", ").concat(a[3], ")"); | ||
return `vec4(${a[0]}, ${a[1]}, ${a[2]}, ${a[3]})`; | ||
} | ||
/** | ||
* Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===) | ||
* | ||
* @param {ReadonlyVec4} a The first vector. | ||
* @param {ReadonlyVec4} b The second vector. | ||
* @returns {Boolean} True if the vectors are equal, false otherwise. | ||
*/ | ||
export function exactEquals(a, b) { | ||
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3]; | ||
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3]; | ||
} | ||
/** | ||
* Returns whether or not the vectors have approximately the same elements in the same position. | ||
* | ||
* @param {ReadonlyVec4} a The first vector. | ||
* @param {ReadonlyVec4} b The second vector. | ||
* @returns {Boolean} True if the vectors are equal, false otherwise. | ||
*/ | ||
export function equals(a, b) { | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const a2 = a[2]; | ||
const a3 = a[3]; | ||
const b0 = b[0]; | ||
const b1 = b[1]; | ||
const b2 = b[2]; | ||
const b3 = b[3]; | ||
return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)); | ||
const a0 = a[0]; | ||
const a1 = a[1]; | ||
const a2 = a[2]; | ||
const a3 = a[3]; | ||
const b0 = b[0]; | ||
const b1 = b[1]; | ||
const b2 = b[2]; | ||
const b3 = b[3]; | ||
return (Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | ||
Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | ||
Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && | ||
Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3))); | ||
} | ||
/** | ||
* Alias for {@link vec4.subtract} | ||
* @function | ||
*/ | ||
export const sub = subtract; | ||
/** | ||
* Alias for {@link vec4.multiply} | ||
* @function | ||
*/ | ||
export const mul = multiply; | ||
/** | ||
* Alias for {@link vec4.divide} | ||
* @function | ||
*/ | ||
export const div = divide; | ||
/** | ||
* Alias for {@link vec4.distance} | ||
* @function | ||
*/ | ||
export const dist = distance; | ||
/** | ||
* Alias for {@link vec4.squaredDistance} | ||
* @function | ||
*/ | ||
export const sqrDist = squaredDistance; | ||
/** | ||
* Alias for {@link vec4.length} | ||
* @function | ||
*/ | ||
export const len = length; | ||
/** | ||
* Alias for {@link vec4.squaredLength} | ||
* @function | ||
*/ | ||
export const sqrLen = squaredLength; | ||
export const forEach = function () { | ||
const vec = create(); | ||
return function (a, stride, offset, count, fn, arg) { | ||
let i; | ||
let l; | ||
if (!stride) { | ||
stride = 4; | ||
} | ||
if (!offset) { | ||
offset = 0; | ||
} | ||
if (count) { | ||
l = Math.min(count * stride + offset, a.length); | ||
} else { | ||
l = a.length; | ||
} | ||
for (i = offset; i < l; i += stride) { | ||
vec[0] = a[i]; | ||
vec[1] = a[i + 1]; | ||
vec[2] = a[i + 2]; | ||
vec[3] = a[i + 3]; | ||
fn(vec, vec, arg); | ||
a[i] = vec[0]; | ||
a[i + 1] = vec[1]; | ||
a[i + 2] = vec[2]; | ||
a[i + 3] = vec[3]; | ||
} | ||
return a; | ||
}; | ||
}(); | ||
//# sourceMappingURL=vec4.js.map | ||
/** | ||
* Perform some operation over an array of vec4s. | ||
* | ||
* @param {Array} a the array of vectors to iterate over | ||
* @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed | ||
* @param {Number} offset Number of elements to skip at the beginning of the array | ||
* @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array | ||
* @param {Function} fn Function to call for each vector in the array | ||
* @param {Object} [arg] additional argument to pass to fn | ||
* @returns {Array} a | ||
* @function | ||
*/ | ||
export const forEach = (function () { | ||
const vec = create(); | ||
return function (a, stride, offset, count, fn, arg) { | ||
let i; | ||
let l; | ||
if (!stride) { | ||
stride = 4; | ||
} | ||
if (!offset) { | ||
offset = 0; | ||
} | ||
if (count) { | ||
l = Math.min(count * stride + offset, a.length); | ||
} | ||
else { | ||
l = a.length; | ||
} | ||
for (i = offset; i < l; i += stride) { | ||
vec[0] = a[i]; | ||
vec[1] = a[i + 1]; | ||
vec[2] = a[i + 2]; | ||
vec[3] = a[i + 3]; | ||
fn(vec, vec, arg); | ||
a[i] = vec[0]; | ||
a[i + 1] = vec[1]; | ||
a[i + 2] = vec[2]; | ||
a[i + 3] = vec[3]; | ||
} | ||
return a; | ||
}; | ||
})(); |
@@ -1,5 +0,4 @@ | ||
import * as mat2 from "./mat2.js"; | ||
import * as mat2d from "./mat2d.js"; | ||
import * as quat2 from "./quat2.js"; | ||
import * as mat2 from './mat2.js'; | ||
import * as mat2d from './mat2d.js'; | ||
import * as quat2 from './quat2.js'; | ||
export { glMatrix, mat2, mat2d, mat3, mat4, quat, quat2, vec2, vec3, vec4 }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -0,1 +1,3 @@ | ||
// @eslint-disable | ||
// @ts-nocheck | ||
import * as glMatrix from './common.js'; | ||
@@ -12,2 +14,1 @@ import * as mat2 from './mat2.js'; | ||
export { glMatrix, mat2, mat2d, mat3, mat4, quat, quat2, vec2, vec3, vec4 }; | ||
//# sourceMappingURL=index.js.map |
@@ -229,2 +229,1 @@ /** | ||
export function sub(out: mat2, a: ReadonlyMat2, b: ReadonlyMat2): mat2; | ||
//# sourceMappingURL=mat2.d.ts.map |
@@ -0,208 +1,384 @@ | ||
// @ts-nocheck | ||
/* eslint-disable */ | ||
import * as glMatrix from './common.js'; | ||
/** | ||
* 2x2 Matrix | ||
* @module mat2 | ||
*/ | ||
/** | ||
* Creates a new identity mat2 | ||
* | ||
* @returns {mat2} a new 2x2 matrix | ||
*/ | ||
export function create() { | ||
let out = new glMatrix.ARRAY_TYPE(4); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[1] = 0; | ||
out[2] = 0; | ||
} | ||
out[0] = 1; | ||
out[3] = 1; | ||
return out; | ||
let out = new glMatrix.ARRAY_TYPE(4); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[1] = 0; | ||
out[2] = 0; | ||
} | ||
out[0] = 1; | ||
out[3] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a new mat2 initialized with values from an existing matrix | ||
* | ||
* @param {ReadonlyMat2} a matrix to clone | ||
* @returns {mat2} a new 2x2 matrix | ||
*/ | ||
export function clone(a) { | ||
let out = new glMatrix.ARRAY_TYPE(4); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
let out = new glMatrix.ARRAY_TYPE(4); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
} | ||
/** | ||
* Copy the values from one mat2 to another | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @param {ReadonlyMat2} a the source matrix | ||
* @returns {mat2} out | ||
*/ | ||
export function copy(out, a) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
} | ||
/** | ||
* Set a mat2 to the identity matrix | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @returns {mat2} out | ||
*/ | ||
export function identity(out) { | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
return out; | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
return out; | ||
} | ||
/** | ||
* Create a new mat2 with the given values | ||
* | ||
* @param {Number} m00 Component in column 0, row 0 position (index 0) | ||
* @param {Number} m01 Component in column 0, row 1 position (index 1) | ||
* @param {Number} m10 Component in column 1, row 0 position (index 2) | ||
* @param {Number} m11 Component in column 1, row 1 position (index 3) | ||
* @returns {mat2} out A new 2x2 matrix | ||
*/ | ||
export function fromValues(m00, m01, m10, m11) { | ||
let out = new glMatrix.ARRAY_TYPE(4); | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m10; | ||
out[3] = m11; | ||
return out; | ||
let out = new glMatrix.ARRAY_TYPE(4); | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m10; | ||
out[3] = m11; | ||
return out; | ||
} | ||
/** | ||
* Set the components of a mat2 to the given values | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @param {Number} m00 Component in column 0, row 0 position (index 0) | ||
* @param {Number} m01 Component in column 0, row 1 position (index 1) | ||
* @param {Number} m10 Component in column 1, row 0 position (index 2) | ||
* @param {Number} m11 Component in column 1, row 1 position (index 3) | ||
* @returns {mat2} out | ||
*/ | ||
export function set(out, m00, m01, m10, m11) { | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m10; | ||
out[3] = m11; | ||
return out; | ||
out[0] = m00; | ||
out[1] = m01; | ||
out[2] = m10; | ||
out[3] = m11; | ||
return out; | ||
} | ||
/** | ||
* Transpose the values of a mat2 | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @param {ReadonlyMat2} a the source matrix | ||
* @returns {mat2} out | ||
*/ | ||
export function transpose(out, a) { | ||
if (out === a) { | ||
let a1 = a[1]; | ||
out[1] = a[2]; | ||
out[2] = a1; | ||
} else { | ||
out[0] = a[0]; | ||
out[1] = a[2]; | ||
out[2] = a[1]; | ||
out[3] = a[3]; | ||
} | ||
return out; | ||
// If we are transposing ourselves we can skip a few steps but have to cache | ||
// some values | ||
if (out === a) { | ||
let a1 = a[1]; | ||
out[1] = a[2]; | ||
out[2] = a1; | ||
} | ||
else { | ||
out[0] = a[0]; | ||
out[1] = a[2]; | ||
out[2] = a[1]; | ||
out[3] = a[3]; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Inverts a mat2 | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @param {ReadonlyMat2} a the source matrix | ||
* @returns {mat2} out | ||
*/ | ||
export function invert(out, a) { | ||
let a0 = a[0], | ||
a1 = a[1], | ||
a2 = a[2], | ||
a3 = a[3]; | ||
let det = a0 * a3 - a2 * a1; | ||
if (!det) { | ||
return null; | ||
} | ||
det = 1.0 / det; | ||
out[0] = a3 * det; | ||
out[1] = -a1 * det; | ||
out[2] = -a2 * det; | ||
out[3] = a0 * det; | ||
return out; | ||
let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; | ||
// Calculate the determinant | ||
let det = a0 * a3 - a2 * a1; | ||
if (!det) { | ||
return null; | ||
} | ||
det = 1.0 / det; | ||
out[0] = a3 * det; | ||
out[1] = -a1 * det; | ||
out[2] = -a2 * det; | ||
out[3] = a0 * det; | ||
return out; | ||
} | ||
/** | ||
* Calculates the adjugate of a mat2 | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @param {ReadonlyMat2} a the source matrix | ||
* @returns {mat2} out | ||
*/ | ||
export function adjoint(out, a) { | ||
let a0 = a[0]; | ||
out[0] = a[3]; | ||
out[1] = -a[1]; | ||
out[2] = -a[2]; | ||
out[3] = a0; | ||
return out; | ||
// Caching this value is necessary if out == a | ||
let a0 = a[0]; | ||
out[0] = a[3]; | ||
out[1] = -a[1]; | ||
out[2] = -a[2]; | ||
out[3] = a0; | ||
return out; | ||
} | ||
/** | ||
* Calculates the determinant of a mat2 | ||
* | ||
* @param {ReadonlyMat2} a the source matrix | ||
* @returns {Number} determinant of a | ||
*/ | ||
export function determinant(a) { | ||
return a[0] * a[3] - a[2] * a[1]; | ||
return a[0] * a[3] - a[2] * a[1]; | ||
} | ||
/** | ||
* Multiplies two mat2's | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @param {ReadonlyMat2} a the first operand | ||
* @param {ReadonlyMat2} b the second operand | ||
* @returns {mat2} out | ||
*/ | ||
export function multiply(out, a, b) { | ||
let a0 = a[0], | ||
a1 = a[1], | ||
a2 = a[2], | ||
a3 = a[3]; | ||
let b0 = b[0], | ||
b1 = b[1], | ||
b2 = b[2], | ||
b3 = b[3]; | ||
out[0] = a0 * b0 + a2 * b1; | ||
out[1] = a1 * b0 + a3 * b1; | ||
out[2] = a0 * b2 + a2 * b3; | ||
out[3] = a1 * b2 + a3 * b3; | ||
return out; | ||
let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; | ||
let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; | ||
out[0] = a0 * b0 + a2 * b1; | ||
out[1] = a1 * b0 + a3 * b1; | ||
out[2] = a0 * b2 + a2 * b3; | ||
out[3] = a1 * b2 + a3 * b3; | ||
return out; | ||
} | ||
/** | ||
* Rotates a mat2 by the given angle | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @param {ReadonlyMat2} a the matrix to rotate | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat2} out | ||
*/ | ||
export function rotate(out, a, rad) { | ||
let a0 = a[0], | ||
a1 = a[1], | ||
a2 = a[2], | ||
a3 = a[3]; | ||
let s = Math.sin(rad); | ||
let c = Math.cos(rad); | ||
out[0] = a0 * c + a2 * s; | ||
out[1] = a1 * c + a3 * s; | ||
out[2] = a0 * -s + a2 * c; | ||
out[3] = a1 * -s + a3 * c; | ||
return out; | ||
let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; | ||
let s = Math.sin(rad); | ||
let c = Math.cos(rad); | ||
out[0] = a0 * c + a2 * s; | ||
out[1] = a1 * c + a3 * s; | ||
out[2] = a0 * -s + a2 * c; | ||
out[3] = a1 * -s + a3 * c; | ||
return out; | ||
} | ||
/** | ||
* Scales the mat2 by the dimensions in the given vec2 | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @param {ReadonlyMat2} a the matrix to rotate | ||
* @param {ReadonlyVec2} v the vec2 to scale the matrix by | ||
* @returns {mat2} out | ||
**/ | ||
export function scale(out, a, v) { | ||
let a0 = a[0], | ||
a1 = a[1], | ||
a2 = a[2], | ||
a3 = a[3]; | ||
let v0 = v[0], | ||
v1 = v[1]; | ||
out[0] = a0 * v0; | ||
out[1] = a1 * v0; | ||
out[2] = a2 * v1; | ||
out[3] = a3 * v1; | ||
return out; | ||
let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; | ||
let v0 = v[0], v1 = v[1]; | ||
out[0] = a0 * v0; | ||
out[1] = a1 * v0; | ||
out[2] = a2 * v1; | ||
out[3] = a3 * v1; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a given angle | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat2.identity(dest); | ||
* mat2.rotate(dest, dest, rad); | ||
* | ||
* @param {mat2} out mat2 receiving operation result | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat2} out | ||
*/ | ||
export function fromRotation(out, rad) { | ||
let s = Math.sin(rad); | ||
let c = Math.cos(rad); | ||
out[0] = c; | ||
out[1] = s; | ||
out[2] = -s; | ||
out[3] = c; | ||
return out; | ||
let s = Math.sin(rad); | ||
let c = Math.cos(rad); | ||
out[0] = c; | ||
out[1] = s; | ||
out[2] = -s; | ||
out[3] = c; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a vector scaling | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat2.identity(dest); | ||
* mat2.scale(dest, dest, vec); | ||
* | ||
* @param {mat2} out mat2 receiving operation result | ||
* @param {ReadonlyVec2} v Scaling vector | ||
* @returns {mat2} out | ||
*/ | ||
export function fromScaling(out, v) { | ||
out[0] = v[0]; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = v[1]; | ||
return out; | ||
out[0] = v[0]; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = v[1]; | ||
return out; | ||
} | ||
/** | ||
* Returns a string representation of a mat2 | ||
* | ||
* @param {ReadonlyMat2} a matrix to represent as a string | ||
* @returns {String} string representation of the matrix | ||
*/ | ||
export function str(a) { | ||
return 'mat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; | ||
return 'mat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')'; | ||
} | ||
/** | ||
* Returns Frobenius norm of a mat2 | ||
* | ||
* @param {ReadonlyMat2} a the matrix to calculate Frobenius norm of | ||
* @returns {Number} Frobenius norm | ||
*/ | ||
export function frob(a) { | ||
return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3]); | ||
return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3]); | ||
} | ||
/** | ||
* Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix | ||
* @param {ReadonlyMat2} L the lower triangular matrix | ||
* @param {ReadonlyMat2} D the diagonal matrix | ||
* @param {ReadonlyMat2} U the upper triangular matrix | ||
* @param {ReadonlyMat2} a the input matrix to factorize | ||
*/ | ||
export function LDU(L, D, U, a) { | ||
L[2] = a[2] / a[0]; | ||
U[0] = a[0]; | ||
U[1] = a[1]; | ||
U[3] = a[3] - L[2] * U[1]; | ||
return [L, D, U]; | ||
L[2] = a[2] / a[0]; | ||
U[0] = a[0]; | ||
U[1] = a[1]; | ||
U[3] = a[3] - L[2] * U[1]; | ||
return [L, D, U]; | ||
} | ||
/** | ||
* Adds two mat2's | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @param {ReadonlyMat2} a the first operand | ||
* @param {ReadonlyMat2} b the second operand | ||
* @returns {mat2} out | ||
*/ | ||
export function add(out, a, b) { | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
return out; | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
return out; | ||
} | ||
/** | ||
* Subtracts matrix b from matrix a | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @param {ReadonlyMat2} a the first operand | ||
* @param {ReadonlyMat2} b the second operand | ||
* @returns {mat2} out | ||
*/ | ||
export function subtract(out, a, b) { | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
out[3] = a[3] - b[3]; | ||
return out; | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
out[3] = a[3] - b[3]; | ||
return out; | ||
} | ||
/** | ||
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) | ||
* | ||
* @param {ReadonlyMat2} a The first matrix. | ||
* @param {ReadonlyMat2} b The second matrix. | ||
* @returns {Boolean} True if the matrices are equal, false otherwise. | ||
*/ | ||
export function exactEquals(a, b) { | ||
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3]; | ||
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3]; | ||
} | ||
/** | ||
* Returns whether or not the matrices have approximately the same elements in the same position. | ||
* | ||
* @param {ReadonlyMat2} a The first matrix. | ||
* @param {ReadonlyMat2} b The second matrix. | ||
* @returns {Boolean} True if the matrices are equal, false otherwise. | ||
*/ | ||
export function equals(a, b) { | ||
let a0 = a[0], | ||
a1 = a[1], | ||
a2 = a[2], | ||
a3 = a[3]; | ||
let b0 = b[0], | ||
b1 = b[1], | ||
b2 = b[2], | ||
b3 = b[3]; | ||
return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)); | ||
let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; | ||
let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; | ||
return (Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | ||
Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | ||
Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && | ||
Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3))); | ||
} | ||
/** | ||
* Multiply each element of the matrix by a scalar. | ||
* | ||
* @param {mat2} out the receiving matrix | ||
* @param {ReadonlyMat2} a the matrix to scale | ||
* @param {Number} b amount to scale the matrix's elements by | ||
* @returns {mat2} out | ||
*/ | ||
export function multiplyScalar(out, a, b) { | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
return out; | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
return out; | ||
} | ||
/** | ||
* Adds two mat2's after multiplying each element of the second operand by a scalar value. | ||
* | ||
* @param {mat2} out the receiving vector | ||
* @param {ReadonlyMat2} a the first operand | ||
* @param {ReadonlyMat2} b the second operand | ||
* @param {Number} scale the amount to scale b's elements by before adding | ||
* @returns {mat2} out | ||
*/ | ||
export function multiplyScalarAndAdd(out, a, b, scale) { | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
out[3] = a[3] + b[3] * scale; | ||
return out; | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
out[3] = a[3] + b[3] * scale; | ||
return out; | ||
} | ||
/** | ||
* Alias for {@link mat2.multiply} | ||
* @function | ||
*/ | ||
export const mul = multiply; | ||
/** | ||
* Alias for {@link mat2.subtract} | ||
* @function | ||
*/ | ||
export const sub = subtract; | ||
//# sourceMappingURL=mat2.js.map |
@@ -244,2 +244,1 @@ /** | ||
export function sub(out: mat2d, a: ReadonlyMat2d, b: ReadonlyMat2d): mat2d; | ||
//# sourceMappingURL=mat2d.d.ts.map |
@@ -0,251 +1,426 @@ | ||
// @ts-nocheck | ||
/* eslint-disable */ | ||
import * as glMatrix from './common.js'; | ||
/** | ||
* 2x3 Matrix | ||
* @module mat2d | ||
* @description | ||
* A mat2d contains six elements defined as: | ||
* <pre> | ||
* [a, b, | ||
* c, d, | ||
* tx, ty] | ||
* </pre> | ||
* This is a short form for the 3x3 matrix: | ||
* <pre> | ||
* [a, b, 0, | ||
* c, d, 0, | ||
* tx, ty, 1] | ||
* </pre> | ||
* The last column is ignored so the array is shorter and operations are faster. | ||
*/ | ||
/** | ||
* Creates a new identity mat2d | ||
* | ||
* @returns {mat2d} a new 2x3 matrix | ||
*/ | ||
export function create() { | ||
let out = new glMatrix.ARRAY_TYPE(6); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[4] = 0; | ||
out[5] = 0; | ||
} | ||
out[0] = 1; | ||
out[3] = 1; | ||
return out; | ||
let out = new glMatrix.ARRAY_TYPE(6); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[4] = 0; | ||
out[5] = 0; | ||
} | ||
out[0] = 1; | ||
out[3] = 1; | ||
return out; | ||
} | ||
/** | ||
* Creates a new mat2d initialized with values from an existing matrix | ||
* | ||
* @param {ReadonlyMat2d} a matrix to clone | ||
* @returns {mat2d} a new 2x3 matrix | ||
*/ | ||
export function clone(a) { | ||
let out = new glMatrix.ARRAY_TYPE(6); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
return out; | ||
let out = new glMatrix.ARRAY_TYPE(6); | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
return out; | ||
} | ||
/** | ||
* Copy the values from one mat2d to another | ||
* | ||
* @param {mat2d} out the receiving matrix | ||
* @param {ReadonlyMat2d} a the source matrix | ||
* @returns {mat2d} out | ||
*/ | ||
export function copy(out, a) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
return out; | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
return out; | ||
} | ||
/** | ||
* Set a mat2d to the identity matrix | ||
* | ||
* @param {mat2d} out the receiving matrix | ||
* @returns {mat2d} out | ||
*/ | ||
export function identity(out) { | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
out[4] = 0; | ||
out[5] = 0; | ||
return out; | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
out[4] = 0; | ||
out[5] = 0; | ||
return out; | ||
} | ||
/** | ||
* Create a new mat2d with the given values | ||
* | ||
* @param {Number} a Component A (index 0) | ||
* @param {Number} b Component B (index 1) | ||
* @param {Number} c Component C (index 2) | ||
* @param {Number} d Component D (index 3) | ||
* @param {Number} tx Component TX (index 4) | ||
* @param {Number} ty Component TY (index 5) | ||
* @returns {mat2d} A new mat2d | ||
*/ | ||
export function fromValues(a, b, c, d, tx, ty) { | ||
let out = new glMatrix.ARRAY_TYPE(6); | ||
out[0] = a; | ||
out[1] = b; | ||
out[2] = c; | ||
out[3] = d; | ||
out[4] = tx; | ||
out[5] = ty; | ||
return out; | ||
let out = new glMatrix.ARRAY_TYPE(6); | ||
out[0] = a; | ||
out[1] = b; | ||
out[2] = c; | ||
out[3] = d; | ||
out[4] = tx; | ||
out[5] = ty; | ||
return out; | ||
} | ||
/** | ||
* Set the components of a mat2d to the given values | ||
* | ||
* @param {mat2d} out the receiving matrix | ||
* @param {Number} a Component A (index 0) | ||
* @param {Number} b Component B (index 1) | ||
* @param {Number} c Component C (index 2) | ||
* @param {Number} d Component D (index 3) | ||
* @param {Number} tx Component TX (index 4) | ||
* @param {Number} ty Component TY (index 5) | ||
* @returns {mat2d} out | ||
*/ | ||
export function set(out, a, b, c, d, tx, ty) { | ||
out[0] = a; | ||
out[1] = b; | ||
out[2] = c; | ||
out[3] = d; | ||
out[4] = tx; | ||
out[5] = ty; | ||
return out; | ||
out[0] = a; | ||
out[1] = b; | ||
out[2] = c; | ||
out[3] = d; | ||
out[4] = tx; | ||
out[5] = ty; | ||
return out; | ||
} | ||
/** | ||
* Inverts a mat2d | ||
* | ||
* @param {mat2d} out the receiving matrix | ||
* @param {ReadonlyMat2d} a the source matrix | ||
* @returns {mat2d} out | ||
*/ | ||
export function invert(out, a) { | ||
let aa = a[0], | ||
ab = a[1], | ||
ac = a[2], | ||
ad = a[3]; | ||
let atx = a[4], | ||
aty = a[5]; | ||
let det = aa * ad - ab * ac; | ||
if (!det) { | ||
return null; | ||
} | ||
det = 1.0 / det; | ||
out[0] = ad * det; | ||
out[1] = -ab * det; | ||
out[2] = -ac * det; | ||
out[3] = aa * det; | ||
out[4] = (ac * aty - ad * atx) * det; | ||
out[5] = (ab * atx - aa * aty) * det; | ||
return out; | ||
let aa = a[0], ab = a[1], ac = a[2], ad = a[3]; | ||
let atx = a[4], aty = a[5]; | ||
let det = aa * ad - ab * ac; | ||
if (!det) { | ||
return null; | ||
} | ||
det = 1.0 / det; | ||
out[0] = ad * det; | ||
out[1] = -ab * det; | ||
out[2] = -ac * det; | ||
out[3] = aa * det; | ||
out[4] = (ac * aty - ad * atx) * det; | ||
out[5] = (ab * atx - aa * aty) * det; | ||
return out; | ||
} | ||
/** | ||
* Calculates the determinant of a mat2d | ||
* | ||
* @param {ReadonlyMat2d} a the source matrix | ||
* @returns {Number} determinant of a | ||
*/ | ||
export function determinant(a) { | ||
return a[0] * a[3] - a[1] * a[2]; | ||
return a[0] * a[3] - a[1] * a[2]; | ||
} | ||
/** | ||
* Multiplies two mat2d's | ||
* | ||
* @param {mat2d} out the receiving matrix | ||
* @param {ReadonlyMat2d} a the first operand | ||
* @param {ReadonlyMat2d} b the second operand | ||
* @returns {mat2d} out | ||
*/ | ||
export function multiply(out, a, b) { | ||
let a0 = a[0], | ||
a1 = a[1], | ||
a2 = a[2], | ||
a3 = a[3], | ||
a4 = a[4], | ||
a5 = a[5]; | ||
let b0 = b[0], | ||
b1 = b[1], | ||
b2 = b[2], | ||
b3 = b[3], | ||
b4 = b[4], | ||
b5 = b[5]; | ||
out[0] = a0 * b0 + a2 * b1; | ||
out[1] = a1 * b0 + a3 * b1; | ||
out[2] = a0 * b2 + a2 * b3; | ||
out[3] = a1 * b2 + a3 * b3; | ||
out[4] = a0 * b4 + a2 * b5 + a4; | ||
out[5] = a1 * b4 + a3 * b5 + a5; | ||
return out; | ||
let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5]; | ||
let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5]; | ||
out[0] = a0 * b0 + a2 * b1; | ||
out[1] = a1 * b0 + a3 * b1; | ||
out[2] = a0 * b2 + a2 * b3; | ||
out[3] = a1 * b2 + a3 * b3; | ||
out[4] = a0 * b4 + a2 * b5 + a4; | ||
out[5] = a1 * b4 + a3 * b5 + a5; | ||
return out; | ||
} | ||
/** | ||
* Rotates a mat2d by the given angle | ||
* | ||
* @param {mat2d} out the receiving matrix | ||
* @param {ReadonlyMat2d} a the matrix to rotate | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat2d} out | ||
*/ | ||
export function rotate(out, a, rad) { | ||
let a0 = a[0], | ||
a1 = a[1], | ||
a2 = a[2], | ||
a3 = a[3], | ||
a4 = a[4], | ||
a5 = a[5]; | ||
let s = Math.sin(rad); | ||
let c = Math.cos(rad); | ||
out[0] = a0 * c + a2 * s; | ||
out[1] = a1 * c + a3 * s; | ||
out[2] = a0 * -s + a2 * c; | ||
out[3] = a1 * -s + a3 * c; | ||
out[4] = a4; | ||
out[5] = a5; | ||
return out; | ||
let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5]; | ||
let s = Math.sin(rad); | ||
let c = Math.cos(rad); | ||
out[0] = a0 * c + a2 * s; | ||
out[1] = a1 * c + a3 * s; | ||
out[2] = a0 * -s + a2 * c; | ||
out[3] = a1 * -s + a3 * c; | ||
out[4] = a4; | ||
out[5] = a5; | ||
return out; | ||
} | ||
/** | ||
* Scales the mat2d by the dimensions in the given vec2 | ||
* | ||
* @param {mat2d} out the receiving matrix | ||
* @param {ReadonlyMat2d} a the matrix to translate | ||
* @param {ReadonlyVec2} v the vec2 to scale the matrix by | ||
* @returns {mat2d} out | ||
**/ | ||
export function scale(out, a, v) { | ||
let a0 = a[0], | ||
a1 = a[1], | ||
a2 = a[2], | ||
a3 = a[3], | ||
a4 = a[4], | ||
a5 = a[5]; | ||
let v0 = v[0], | ||
v1 = v[1]; | ||
out[0] = a0 * v0; | ||
out[1] = a1 * v0; | ||
out[2] = a2 * v1; | ||
out[3] = a3 * v1; | ||
out[4] = a4; | ||
out[5] = a5; | ||
return out; | ||
let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5]; | ||
let v0 = v[0], v1 = v[1]; | ||
out[0] = a0 * v0; | ||
out[1] = a1 * v0; | ||
out[2] = a2 * v1; | ||
out[3] = a3 * v1; | ||
out[4] = a4; | ||
out[5] = a5; | ||
return out; | ||
} | ||
/** | ||
* Translates the mat2d by the dimensions in the given vec2 | ||
* | ||
* @param {mat2d} out the receiving matrix | ||
* @param {ReadonlyMat2d} a the matrix to translate | ||
* @param {ReadonlyVec2} v the vec2 to translate the matrix by | ||
* @returns {mat2d} out | ||
**/ | ||
export function translate(out, a, v) { | ||
let a0 = a[0], | ||
a1 = a[1], | ||
a2 = a[2], | ||
a3 = a[3], | ||
a4 = a[4], | ||
a5 = a[5]; | ||
let v0 = v[0], | ||
v1 = v[1]; | ||
out[0] = a0; | ||
out[1] = a1; | ||
out[2] = a2; | ||
out[3] = a3; | ||
out[4] = a0 * v0 + a2 * v1 + a4; | ||
out[5] = a1 * v0 + a3 * v1 + a5; | ||
return out; | ||
let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5]; | ||
let v0 = v[0], v1 = v[1]; | ||
out[0] = a0; | ||
out[1] = a1; | ||
out[2] = a2; | ||
out[3] = a3; | ||
out[4] = a0 * v0 + a2 * v1 + a4; | ||
out[5] = a1 * v0 + a3 * v1 + a5; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a given angle | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat2d.identity(dest); | ||
* mat2d.rotate(dest, dest, rad); | ||
* | ||
* @param {mat2d} out mat2d receiving operation result | ||
* @param {Number} rad the angle to rotate the matrix by | ||
* @returns {mat2d} out | ||
*/ | ||
export function fromRotation(out, rad) { | ||
let s = Math.sin(rad), | ||
c = Math.cos(rad); | ||
out[0] = c; | ||
out[1] = s; | ||
out[2] = -s; | ||
out[3] = c; | ||
out[4] = 0; | ||
out[5] = 0; | ||
return out; | ||
let s = Math.sin(rad), c = Math.cos(rad); | ||
out[0] = c; | ||
out[1] = s; | ||
out[2] = -s; | ||
out[3] = c; | ||
out[4] = 0; | ||
out[5] = 0; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a vector scaling | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat2d.identity(dest); | ||
* mat2d.scale(dest, dest, vec); | ||
* | ||
* @param {mat2d} out mat2d receiving operation result | ||
* @param {ReadonlyVec2} v Scaling vector | ||
* @returns {mat2d} out | ||
*/ | ||
export function fromScaling(out, v) { | ||
out[0] = v[0]; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = v[1]; | ||
out[4] = 0; | ||
out[5] = 0; | ||
return out; | ||
out[0] = v[0]; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = v[1]; | ||
out[4] = 0; | ||
out[5] = 0; | ||
return out; | ||
} | ||
/** | ||
* Creates a matrix from a vector translation | ||
* This is equivalent to (but much faster than): | ||
* | ||
* mat2d.identity(dest); | ||
* mat2d.translate(dest, dest, vec); | ||
* | ||
* @param {mat2d} out mat2d receiving operation result | ||
* @param {ReadonlyVec2} v Translation vector | ||
* @returns {mat2d} out | ||
*/ | ||
export function fromTranslation(out, v) { | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
out[4] = v[0]; | ||
out[5] = v[1]; | ||
return out; | ||
out[0] = 1; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
out[4] = v[0]; | ||
out[5] = v[1]; | ||
return out; | ||
} | ||
/** | ||
* Returns a string representation of a mat2d | ||
* | ||
* @param {ReadonlyMat2d} a matrix to represent as a string | ||
* @returns {String} string representation of the matrix | ||
*/ | ||
export function str(a) { | ||
return 'mat2d(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' + a[4] + ', ' + a[5] + ')'; | ||
return ('mat2d(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' + a[4] + ', ' + a[5] + ')'); | ||
} | ||
/** | ||
* Returns Frobenius norm of a mat2d | ||
* | ||
* @param {ReadonlyMat2d} a the matrix to calculate Frobenius norm of | ||
* @returns {Number} Frobenius norm | ||
*/ | ||
export function frob(a) { | ||
return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3] + a[4] * a[4] + a[5] * a[5] + 1); | ||
return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3] + a[4] * a[4] + a[5] * a[5] + 1); | ||
} | ||
/** | ||
* Adds two mat2d's | ||
* | ||
* @param {mat2d} out the receiving matrix | ||
* @param {ReadonlyMat2d} a the first operand | ||
* @param {ReadonlyMat2d} b the second operand | ||
* @returns {mat2d} out | ||
*/ | ||
export function add(out, a, b) { | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
out[4] = a[4] + b[4]; | ||
out[5] = a[5] + b[5]; | ||
return out; | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
out[4] = a[4] + b[4]; | ||
out[5] = a[5] + b[5]; | ||
return out; | ||
} | ||
/** | ||
* Subtracts matrix b from matrix a | ||
* | ||
* @param {mat2d} out the receiving matrix | ||
* @param {ReadonlyMat2d} a the first operand | ||
* @param {ReadonlyMat2d} b the second operand | ||
* @returns {mat2d} out | ||
*/ | ||
export function subtract(out, a, b) { | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
out[3] = a[3] - b[3]; | ||
out[4] = a[4] - b[4]; | ||
out[5] = a[5] - b[5]; | ||
return out; | ||
out[0] = a[0] - b[0]; | ||
out[1] = a[1] - b[1]; | ||
out[2] = a[2] - b[2]; | ||
out[3] = a[3] - b[3]; | ||
out[4] = a[4] - b[4]; | ||
out[5] = a[5] - b[5]; | ||
return out; | ||
} | ||
/** | ||
* Multiply each element of the matrix by a scalar. | ||
* | ||
* @param {mat2d} out the receiving matrix | ||
* @param {ReadonlyMat2d} a the matrix to scale | ||
* @param {Number} b amount to scale the matrix's elements by | ||
* @returns {mat2d} out | ||
*/ | ||
export function multiplyScalar(out, a, b) { | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
out[4] = a[4] * b; | ||
out[5] = a[5] * b; | ||
return out; | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
out[4] = a[4] * b; | ||
out[5] = a[5] * b; | ||
return out; | ||
} | ||
/** | ||
* Adds two mat2d's after multiplying each element of the second operand by a scalar value. | ||
* | ||
* @param {mat2d} out the receiving vector | ||
* @param {ReadonlyMat2d} a the first operand | ||
* @param {ReadonlyMat2d} b the second operand | ||
* @param {Number} scale the amount to scale b's elements by before adding | ||
* @returns {mat2d} out | ||
*/ | ||
export function multiplyScalarAndAdd(out, a, b, scale) { | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
out[3] = a[3] + b[3] * scale; | ||
out[4] = a[4] + b[4] * scale; | ||
out[5] = a[5] + b[5] * scale; | ||
return out; | ||
out[0] = a[0] + b[0] * scale; | ||
out[1] = a[1] + b[1] * scale; | ||
out[2] = a[2] + b[2] * scale; | ||
out[3] = a[3] + b[3] * scale; | ||
out[4] = a[4] + b[4] * scale; | ||
out[5] = a[5] + b[5] * scale; | ||
return out; | ||
} | ||
/** | ||
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===) | ||
* | ||
* @param {ReadonlyMat2d} a The first matrix. | ||
* @param {ReadonlyMat2d} b The second matrix. | ||
* @returns {Boolean} True if the matrices are equal, false otherwise. | ||
*/ | ||
export function exactEquals(a, b) { | ||
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5]; | ||
return (a[0] === b[0] && | ||
a[1] === b[1] && | ||
a[2] === b[2] && | ||
a[3] === b[3] && | ||
a[4] === b[4] && | ||
a[5] === b[5]); | ||
} | ||
/** | ||
* Returns whether or not the matrices have approximately the same elements in the same position. | ||
* | ||
* @param {ReadonlyMat2d} a The first matrix. | ||
* @param {ReadonlyMat2d} b The second matrix. | ||
* @returns {Boolean} True if the matrices are equal, false otherwise. | ||
*/ | ||
export function equals(a, b) { | ||
let a0 = a[0], | ||
a1 = a[1], | ||
a2 = a[2], | ||
a3 = a[3], | ||
a4 = a[4], | ||
a5 = a[5]; | ||
let b0 = b[0], | ||
b1 = b[1], | ||
b2 = b[2], | ||
b3 = b[3], | ||
b4 = b[4], | ||
b5 = b[5]; | ||
return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)); | ||
let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5]; | ||
let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5]; | ||
return (Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | ||
Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | ||
Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && | ||
Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && | ||
Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && | ||
Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5))); | ||
} | ||
/** | ||
* Alias for {@link mat2d.multiply} | ||
* @function | ||
*/ | ||
export const mul = multiply; | ||
/** | ||
* Alias for {@link mat2d.subtract} | ||
* @function | ||
*/ | ||
export const sub = subtract; | ||
//# sourceMappingURL=mat2d.js.map |
@@ -352,2 +352,1 @@ /** | ||
export declare function equals(a: any, b: any): boolean; | ||
//# sourceMappingURL=quat2.d.ts.map |
@@ -0,482 +1,720 @@ | ||
// @ts-nocheck | ||
/* eslint-disable */ | ||
import * as glMatrix from './common.js'; | ||
import * as quat from './quat.js'; | ||
import * as mat4 from './mat4.js'; | ||
/** | ||
* Dual Quaternion<br> | ||
* Format: [real, dual]<br> | ||
* Quaternion format: XYZW<br> | ||
* Make sure to have normalized dual quaternions, otherwise the functions may not work as intended.<br> | ||
* @module quat2 | ||
*/ | ||
/** | ||
* Creates a new identity dual quat | ||
* | ||
* @returns {quat2} a new dual quaternion [real -> rotation, dual -> translation] | ||
*/ | ||
export function create() { | ||
let dq = new glMatrix.ARRAY_TYPE(8); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
dq[0] = 0; | ||
dq[1] = 0; | ||
dq[2] = 0; | ||
dq[4] = 0; | ||
dq[5] = 0; | ||
dq[6] = 0; | ||
dq[7] = 0; | ||
} | ||
dq[3] = 1; | ||
return dq; | ||
let dq = new glMatrix.ARRAY_TYPE(8); | ||
if (glMatrix.ARRAY_TYPE != Float32Array) { | ||
dq[0] = 0; | ||
dq[1] = 0; | ||
dq[2] = 0; | ||
dq[4] = 0; | ||
dq[5] = 0; | ||
dq[6] = 0; | ||
dq[7] = 0; | ||
} | ||
dq[3] = 1; | ||
return dq; | ||
} | ||
/** | ||
* Creates a new quat initialized with values from an existing quaternion | ||
* | ||
* @param {ReadonlyQuat2} a dual quaternion to clone | ||
* @returns {quat2} new dual quaternion | ||
* @function | ||
*/ | ||
export function clone(a) { | ||
let dq = new glMatrix.ARRAY_TYPE(8); | ||
dq[0] = a[0]; | ||
dq[1] = a[1]; | ||
dq[2] = a[2]; | ||
dq[3] = a[3]; | ||
dq[4] = a[4]; | ||
dq[5] = a[5]; | ||
dq[6] = a[6]; | ||
dq[7] = a[7]; | ||
return dq; | ||
let dq = new glMatrix.ARRAY_TYPE(8); | ||
dq[0] = a[0]; | ||
dq[1] = a[1]; | ||
dq[2] = a[2]; | ||
dq[3] = a[3]; | ||
dq[4] = a[4]; | ||
dq[5] = a[5]; | ||
dq[6] = a[6]; | ||
dq[7] = a[7]; | ||
return dq; | ||
} | ||
/** | ||
* Creates a new dual quat initialized with the given values | ||
* | ||
* @param {Number} x1 X component | ||
* @param {Number} y1 Y component | ||
* @param {Number} z1 Z component | ||
* @param {Number} w1 W component | ||
* @param {Number} x2 X component | ||
* @param {Number} y2 Y component | ||
* @param {Number} z2 Z component | ||
* @param {Number} w2 W component | ||
* @returns {quat2} new dual quaternion | ||
* @function | ||
*/ | ||
export function fromValues(x1, y1, z1, w1, x2, y2, z2, w2) { | ||
let dq = new glMatrix.ARRAY_TYPE(8); | ||
dq[0] = x1; | ||
dq[1] = y1; | ||
dq[2] = z1; | ||
dq[3] = w1; | ||
dq[4] = x2; | ||
dq[5] = y2; | ||
dq[6] = z2; | ||
dq[7] = w2; | ||
return dq; | ||
let dq = new glMatrix.ARRAY_TYPE(8); | ||
dq[0] = x1; | ||
dq[1] = y1; | ||
dq[2] = z1; | ||
dq[3] = w1; | ||
dq[4] = x2; | ||
dq[5] = y2; | ||
dq[6] = z2; | ||
dq[7] = w2; | ||
return dq; | ||
} | ||
/** | ||
* Creates a new dual quat from the given values (quat and translation) | ||
* | ||
* @param {Number} x1 X component | ||
* @param {Number} y1 Y component | ||
* @param {Number} z1 Z component | ||
* @param {Number} w1 W component | ||
* @param {Number} x2 X component (translation) | ||
* @param {Number} y2 Y component (translation) | ||
* @param {Number} z2 Z component (translation) | ||
* @returns {quat2} new dual quaternion | ||
* @function | ||
*/ | ||
export function fromRotationTranslationValues(x1, y1, z1, w1, x2, y2, z2) { | ||
let dq = new glMatrix.ARRAY_TYPE(8); | ||
dq[0] = x1; | ||
dq[1] = y1; | ||
dq[2] = z1; | ||
dq[3] = w1; | ||
let ax = x2 * 0.5, | ||
ay = y2 * 0.5, | ||
az = z2 * 0.5; | ||
dq[4] = ax * w1 + ay * z1 - az * y1; | ||
dq[5] = ay * w1 + az * x1 - ax * z1; | ||
dq[6] = az * w1 + ax * y1 - ay * x1; | ||
dq[7] = -ax * x1 - ay * y1 - az * z1; | ||
return dq; | ||
let dq = new glMatrix.ARRAY_TYPE(8); | ||
dq[0] = x1; | ||
dq[1] = y1; | ||
dq[2] = z1; | ||
dq[3] = w1; | ||
let ax = x2 * 0.5, ay = y2 * 0.5, az = z2 * 0.5; | ||
dq[4] = ax * w1 + ay * z1 - az * y1; | ||
dq[5] = ay * w1 + az * x1 - ax * z1; | ||
dq[6] = az * w1 + ax * y1 - ay * x1; | ||
dq[7] = -ax * x1 - ay * y1 - az * z1; | ||
return dq; | ||
} | ||
/** | ||
* Creates a dual quat from a quaternion and a translation | ||
* | ||
* @param {ReadonlyQuat2} dual quaternion receiving operation result | ||
* @param {ReadonlyQuat} q a normalized quaternion | ||
* @param {ReadonlyVec3} t translation vector | ||
* @returns {quat2} dual quaternion receiving operation result | ||
* @function | ||
*/ | ||
export function fromRotationTranslation(out, q, t) { | ||
let ax = t[0] * 0.5, | ||
ay = t[1] * 0.5, | ||
az = t[2] * 0.5, | ||
bx = q[0], | ||
by = q[1], | ||
bz = q[2], | ||
bw = q[3]; | ||
out[0] = bx; | ||
out[1] = by; | ||
out[2] = bz; | ||
out[3] = bw; | ||
out[4] = ax * bw + ay * bz - az * by; | ||
out[5] = ay * bw + az * bx - ax * bz; | ||
out[6] = az * bw + ax * by - ay * bx; | ||
out[7] = -ax * bx - ay * by - az * bz; | ||
return out; | ||
let ax = t[0] * 0.5, ay = t[1] * 0.5, az = t[2] * 0.5, bx = q[0], by = q[1], bz = q[2], bw = q[3]; | ||
out[0] = bx; | ||
out[1] = by; | ||
out[2] = bz; | ||
out[3] = bw; | ||
out[4] = ax * bw + ay * bz - az * by; | ||
out[5] = ay * bw + az * bx - ax * bz; | ||
out[6] = az * bw + ax * by - ay * bx; | ||
out[7] = -ax * bx - ay * by - az * bz; | ||
return out; | ||
} | ||
/** | ||
* Creates a dual quat from a translation | ||
* | ||
* @param {ReadonlyQuat2} dual quaternion receiving operation result | ||
* @param {ReadonlyVec3} t translation vector | ||
* @returns {quat2} dual quaternion receiving operation result | ||
* @function | ||
*/ | ||
export function fromTranslation(out, t) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
out[4] = t[0] * 0.5; | ||
out[5] = t[1] * 0.5; | ||
out[6] = t[2] * 0.5; | ||
out[7] = 0; | ||
return out; | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
out[4] = t[0] * 0.5; | ||
out[5] = t[1] * 0.5; | ||
out[6] = t[2] * 0.5; | ||
out[7] = 0; | ||
return out; | ||
} | ||
/** | ||
* Creates a dual quat from a quaternion | ||
* | ||
* @param {ReadonlyQuat2} dual quaternion receiving operation result | ||
* @param {ReadonlyQuat} q the quaternion | ||
* @returns {quat2} dual quaternion receiving operation result | ||
* @function | ||
*/ | ||
export function fromRotation(out, q) { | ||
out[0] = q[0]; | ||
out[1] = q[1]; | ||
out[2] = q[2]; | ||
out[3] = q[3]; | ||
out[4] = 0; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
return out; | ||
out[0] = q[0]; | ||
out[1] = q[1]; | ||
out[2] = q[2]; | ||
out[3] = q[3]; | ||
out[4] = 0; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
return out; | ||
} | ||
/** | ||
* Creates a new dual quat from a matrix (4x4) | ||
* | ||
* @param {quat2} out the dual quaternion | ||
* @param {ReadonlyMat4} a the matrix | ||
* @returns {quat2} dual quat receiving operation result | ||
* @function | ||
*/ | ||
export function fromMat4(out, a) { | ||
let outer = quat.create(); | ||
mat4.getRotation(outer, a); | ||
let t = new glMatrix.ARRAY_TYPE(3); | ||
mat4.getTranslation(t, a); | ||
fromRotationTranslation(out, outer, t); | ||
return out; | ||
//TODO Optimize this | ||
let outer = quat.create(); | ||
mat4.getRotation(outer, a); | ||
let t = new glMatrix.ARRAY_TYPE(3); | ||
mat4.getTranslation(t, a); | ||
fromRotationTranslation(out, outer, t); | ||
return out; | ||
} | ||
/** | ||
* Copy the values from one dual quat to another | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat2} a the source dual quaternion | ||
* @returns {quat2} out | ||
* @function | ||
*/ | ||
export function copy(out, a) { | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
return out; | ||
out[0] = a[0]; | ||
out[1] = a[1]; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
out[4] = a[4]; | ||
out[5] = a[5]; | ||
out[6] = a[6]; | ||
out[7] = a[7]; | ||
return out; | ||
} | ||
/** | ||
* Set a dual quat to the identity dual quaternion | ||
* | ||
* @param {quat2} out the receiving quaternion | ||
* @returns {quat2} out | ||
*/ | ||
export function identity(out) { | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
out[4] = 0; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
return out; | ||
out[0] = 0; | ||
out[1] = 0; | ||
out[2] = 0; | ||
out[3] = 1; | ||
out[4] = 0; | ||
out[5] = 0; | ||
out[6] = 0; | ||
out[7] = 0; | ||
return out; | ||
} | ||
/** | ||
* Set the components of a dual quat to the given values | ||
* | ||
* @param {quat2} out the receiving quaternion | ||
* @param {Number} x1 X component | ||
* @param {Number} y1 Y component | ||
* @param {Number} z1 Z component | ||
* @param {Number} w1 W component | ||
* @param {Number} x2 X component | ||
* @param {Number} y2 Y component | ||
* @param {Number} z2 Z component | ||
* @param {Number} w2 W component | ||
* @returns {quat2} out | ||
* @function | ||
*/ | ||
export function set(out, x1, y1, z1, w1, x2, y2, z2, w2) { | ||
out[0] = x1; | ||
out[1] = y1; | ||
out[2] = z1; | ||
out[3] = w1; | ||
out[4] = x2; | ||
out[5] = y2; | ||
out[6] = z2; | ||
out[7] = w2; | ||
return out; | ||
out[0] = x1; | ||
out[1] = y1; | ||
out[2] = z1; | ||
out[3] = w1; | ||
out[4] = x2; | ||
out[5] = y2; | ||
out[6] = z2; | ||
out[7] = w2; | ||
return out; | ||
} | ||
/** | ||
* Gets the real part of a dual quat | ||
* @param {quat} out real part | ||
* @param {ReadonlyQuat2} a Dual Quaternion | ||
* @return {quat} real part | ||
*/ | ||
export const getReal = quat.copy; | ||
/** | ||
* Gets the dual part of a dual quat | ||
* @param {quat} out dual part | ||
* @param {ReadonlyQuat2} a Dual Quaternion | ||
* @return {quat} dual part | ||
*/ | ||
export function getDual(out, a) { | ||
out[0] = a[4]; | ||
out[1] = a[5]; | ||
out[2] = a[6]; | ||
out[3] = a[7]; | ||
return out; | ||
out[0] = a[4]; | ||
out[1] = a[5]; | ||
out[2] = a[6]; | ||
out[3] = a[7]; | ||
return out; | ||
} | ||
/** | ||
* Set the real component of a dual quat to the given quaternion | ||
* | ||
* @param {quat2} out the receiving quaternion | ||
* @param {ReadonlyQuat} q a quaternion representing the real part | ||
* @returns {quat2} out | ||
* @function | ||
*/ | ||
export const setReal = quat.copy; | ||
/** | ||
* Set the dual component of a dual quat to the given quaternion | ||
* | ||
* @param {quat2} out the receiving quaternion | ||
* @param {ReadonlyQuat} q a quaternion representing the dual part | ||
* @returns {quat2} out | ||
* @function | ||
*/ | ||
export function setDual(out, q) { | ||
out[4] = q[0]; | ||
out[5] = q[1]; | ||
out[6] = q[2]; | ||
out[7] = q[3]; | ||
return out; | ||
out[4] = q[0]; | ||
out[5] = q[1]; | ||
out[6] = q[2]; | ||
out[7] = q[3]; | ||
return out; | ||
} | ||
/** | ||
* Gets the translation of a normalized dual quat | ||
* @param {vec3} out translation | ||
* @param {ReadonlyQuat2} a Dual Quaternion to be decomposed | ||
* @return {vec3} translation | ||
*/ | ||
export function getTranslation(out, a) { | ||
let ax = a[4], | ||
ay = a[5], | ||
az = a[6], | ||
aw = a[7], | ||
bx = -a[0], | ||
by = -a[1], | ||
bz = -a[2], | ||
bw = a[3]; | ||
out[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2; | ||
out[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2; | ||
out[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2; | ||
return out; | ||
let ax = a[4], ay = a[5], az = a[6], aw = a[7], bx = -a[0], by = -a[1], bz = -a[2], bw = a[3]; | ||
out[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2; | ||
out[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2; | ||
out[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2; | ||
return out; | ||
} | ||
/** | ||
* Translates a dual quat by the given vector | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat2} a the dual quaternion to translate | ||
* @param {ReadonlyVec3} v vector to translate by | ||
* @returns {quat2} out | ||
*/ | ||
export function translate(out, a, v) { | ||
let ax1 = a[0], | ||
ay1 = a[1], | ||
az1 = a[2], | ||
aw1 = a[3], | ||
bx1 = v[0] * 0.5, | ||
by1 = v[1] * 0.5, | ||
bz1 = v[2] * 0.5, | ||
ax2 = a[4], | ||
ay2 = a[5], | ||
az2 = a[6], | ||
aw2 = a[7]; | ||
out[0] = ax1; | ||
out[1] = ay1; | ||
out[2] = az1; | ||
out[3] = aw1; | ||
out[4] = aw1 * bx1 + ay1 * bz1 - az1 * by1 + ax2; | ||
out[5] = aw1 * by1 + az1 * bx1 - ax1 * bz1 + ay2; | ||
out[6] = aw1 * bz1 + ax1 * by1 - ay1 * bx1 + az2; | ||
out[7] = -ax1 * bx1 - ay1 * by1 - az1 * bz1 + aw2; | ||
return out; | ||
let ax1 = a[0], ay1 = a[1], az1 = a[2], aw1 = a[3], bx1 = v[0] * 0.5, by1 = v[1] * 0.5, bz1 = v[2] * 0.5, ax2 = a[4], ay2 = a[5], az2 = a[6], aw2 = a[7]; | ||
out[0] = ax1; | ||
out[1] = ay1; | ||
out[2] = az1; | ||
out[3] = aw1; | ||
out[4] = aw1 * bx1 + ay1 * bz1 - az1 * by1 + ax2; | ||
out[5] = aw1 * by1 + az1 * bx1 - ax1 * bz1 + ay2; | ||
out[6] = aw1 * bz1 + ax1 * by1 - ay1 * bx1 + az2; | ||
out[7] = -ax1 * bx1 - ay1 * by1 - az1 * bz1 + aw2; | ||
return out; | ||
} | ||
/** | ||
* Rotates a dual quat around the X axis | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat2} a the dual quaternion to rotate | ||
* @param {number} rad how far should the rotation be | ||
* @returns {quat2} out | ||
*/ | ||
export function rotateX(out, a, rad) { | ||
let bx = -a[0], | ||
by = -a[1], | ||
bz = -a[2], | ||
bw = a[3], | ||
ax = a[4], | ||
ay = a[5], | ||
az = a[6], | ||
aw = a[7], | ||
ax1 = ax * bw + aw * bx + ay * bz - az * by, | ||
ay1 = ay * bw + aw * by + az * bx - ax * bz, | ||
az1 = az * bw + aw * bz + ax * by - ay * bx, | ||
aw1 = aw * bw - ax * bx - ay * by - az * bz; | ||
quat.rotateX(out, a, rad); | ||
bx = out[0]; | ||
by = out[1]; | ||
bz = out[2]; | ||
bw = out[3]; | ||
out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by; | ||
out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz; | ||
out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx; | ||
out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz; | ||
return out; | ||
let bx = -a[0], by = -a[1], bz = -a[2], bw = a[3], ax = a[4], ay = a[5], az = a[6], aw = a[7], ax1 = ax * bw + aw * bx + ay * bz - az * by, ay1 = ay * bw + aw * by + az * bx - ax * bz, az1 = az * bw + aw * bz + ax * by - ay * bx, aw1 = aw * bw - ax * bx - ay * by - az * bz; | ||
quat.rotateX(out, a, rad); | ||
bx = out[0]; | ||
by = out[1]; | ||
bz = out[2]; | ||
bw = out[3]; | ||
out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by; | ||
out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz; | ||
out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx; | ||
out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz; | ||
return out; | ||
} | ||
/** | ||
* Rotates a dual quat around the Y axis | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat2} a the dual quaternion to rotate | ||
* @param {number} rad how far should the rotation be | ||
* @returns {quat2} out | ||
*/ | ||
export function rotateY(out, a, rad) { | ||
let bx = -a[0], | ||
by = -a[1], | ||
bz = -a[2], | ||
bw = a[3], | ||
ax = a[4], | ||
ay = a[5], | ||
az = a[6], | ||
aw = a[7], | ||
ax1 = ax * bw + aw * bx + ay * bz - az * by, | ||
ay1 = ay * bw + aw * by + az * bx - ax * bz, | ||
az1 = az * bw + aw * bz + ax * by - ay * bx, | ||
aw1 = aw * bw - ax * bx - ay * by - az * bz; | ||
quat.rotateY(out, a, rad); | ||
bx = out[0]; | ||
by = out[1]; | ||
bz = out[2]; | ||
bw = out[3]; | ||
out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by; | ||
out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz; | ||
out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx; | ||
out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz; | ||
return out; | ||
let bx = -a[0], by = -a[1], bz = -a[2], bw = a[3], ax = a[4], ay = a[5], az = a[6], aw = a[7], ax1 = ax * bw + aw * bx + ay * bz - az * by, ay1 = ay * bw + aw * by + az * bx - ax * bz, az1 = az * bw + aw * bz + ax * by - ay * bx, aw1 = aw * bw - ax * bx - ay * by - az * bz; | ||
quat.rotateY(out, a, rad); | ||
bx = out[0]; | ||
by = out[1]; | ||
bz = out[2]; | ||
bw = out[3]; | ||
out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by; | ||
out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz; | ||
out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx; | ||
out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz; | ||
return out; | ||
} | ||
/** | ||
* Rotates a dual quat around the Z axis | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat2} a the dual quaternion to rotate | ||
* @param {number} rad how far should the rotation be | ||
* @returns {quat2} out | ||
*/ | ||
export function rotateZ(out, a, rad) { | ||
let bx = -a[0], | ||
by = -a[1], | ||
bz = -a[2], | ||
bw = a[3], | ||
ax = a[4], | ||
ay = a[5], | ||
az = a[6], | ||
aw = a[7], | ||
ax1 = ax * bw + aw * bx + ay * bz - az * by, | ||
ay1 = ay * bw + aw * by + az * bx - ax * bz, | ||
az1 = az * bw + aw * bz + ax * by - ay * bx, | ||
aw1 = aw * bw - ax * bx - ay * by - az * bz; | ||
quat.rotateZ(out, a, rad); | ||
bx = out[0]; | ||
by = out[1]; | ||
bz = out[2]; | ||
bw = out[3]; | ||
out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by; | ||
out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz; | ||
out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx; | ||
out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz; | ||
return out; | ||
let bx = -a[0], by = -a[1], bz = -a[2], bw = a[3], ax = a[4], ay = a[5], az = a[6], aw = a[7], ax1 = ax * bw + aw * bx + ay * bz - az * by, ay1 = ay * bw + aw * by + az * bx - ax * bz, az1 = az * bw + aw * bz + ax * by - ay * bx, aw1 = aw * bw - ax * bx - ay * by - az * bz; | ||
quat.rotateZ(out, a, rad); | ||
bx = out[0]; | ||
by = out[1]; | ||
bz = out[2]; | ||
bw = out[3]; | ||
out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by; | ||
out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz; | ||
out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx; | ||
out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz; | ||
return out; | ||
} | ||
/** | ||
* Rotates a dual quat by a given quaternion (a * q) | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat2} a the dual quaternion to rotate | ||
* @param {ReadonlyQuat} q quaternion to rotate by | ||
* @returns {quat2} out | ||
*/ | ||
export function rotateByQuatAppend(out, a, q) { | ||
let qx = q[0], | ||
qy = q[1], | ||
qz = q[2], | ||
qw = q[3], | ||
ax = a[0], | ||
ay = a[1], | ||
az = a[2], | ||
aw = a[3]; | ||
out[0] = ax * qw + aw * qx + ay * qz - az * qy; | ||
out[1] = ay * qw + aw * qy + az * qx - ax * qz; | ||
out[2] = az * qw + aw * qz + ax * qy - ay * qx; | ||
out[3] = aw * qw - ax * qx - ay * qy - az * qz; | ||
ax = a[4]; | ||
ay = a[5]; | ||
az = a[6]; | ||
aw = a[7]; | ||
out[4] = ax * qw + aw * qx + ay * qz - az * qy; | ||
out[5] = ay * qw + aw * qy + az * qx - ax * qz; | ||
out[6] = az * qw + aw * qz + ax * qy - ay * qx; | ||
out[7] = aw * qw - ax * qx - ay * qy - az * qz; | ||
return out; | ||
let qx = q[0], qy = q[1], qz = q[2], qw = q[3], ax = a[0], ay = a[1], az = a[2], aw = a[3]; | ||
out[0] = ax * qw + aw * qx + ay * qz - az * qy; | ||
out[1] = ay * qw + aw * qy + az * qx - ax * qz; | ||
out[2] = az * qw + aw * qz + ax * qy - ay * qx; | ||
out[3] = aw * qw - ax * qx - ay * qy - az * qz; | ||
ax = a[4]; | ||
ay = a[5]; | ||
az = a[6]; | ||
aw = a[7]; | ||
out[4] = ax * qw + aw * qx + ay * qz - az * qy; | ||
out[5] = ay * qw + aw * qy + az * qx - ax * qz; | ||
out[6] = az * qw + aw * qz + ax * qy - ay * qx; | ||
out[7] = aw * qw - ax * qx - ay * qy - az * qz; | ||
return out; | ||
} | ||
/** | ||
* Rotates a dual quat by a given quaternion (q * a) | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat} q quaternion to rotate by | ||
* @param {ReadonlyQuat2} a the dual quaternion to rotate | ||
* @returns {quat2} out | ||
*/ | ||
export function rotateByQuatPrepend(out, q, a) { | ||
let qx = q[0], | ||
qy = q[1], | ||
qz = q[2], | ||
qw = q[3], | ||
bx = a[0], | ||
by = a[1], | ||
bz = a[2], | ||
bw = a[3]; | ||
out[0] = qx * bw + qw * bx + qy * bz - qz * by; | ||
out[1] = qy * bw + qw * by + qz * bx - qx * bz; | ||
out[2] = qz * bw + qw * bz + qx * by - qy * bx; | ||
out[3] = qw * bw - qx * bx - qy * by - qz * bz; | ||
bx = a[4]; | ||
by = a[5]; | ||
bz = a[6]; | ||
bw = a[7]; | ||
out[4] = qx * bw + qw * bx + qy * bz - qz * by; | ||
out[5] = qy * bw + qw * by + qz * bx - qx * bz; | ||
out[6] = qz * bw + qw * bz + qx * by - qy * bx; | ||
out[7] = qw * bw - qx * bx - qy * by - qz * bz; | ||
return out; | ||
let qx = q[0], qy = q[1], qz = q[2], qw = q[3], bx = a[0], by = a[1], bz = a[2], bw = a[3]; | ||
out[0] = qx * bw + qw * bx + qy * bz - qz * by; | ||
out[1] = qy * bw + qw * by + qz * bx - qx * bz; | ||
out[2] = qz * bw + qw * bz + qx * by - qy * bx; | ||
out[3] = qw * bw - qx * bx - qy * by - qz * bz; | ||
bx = a[4]; | ||
by = a[5]; | ||
bz = a[6]; | ||
bw = a[7]; | ||
out[4] = qx * bw + qw * bx + qy * bz - qz * by; | ||
out[5] = qy * bw + qw * by + qz * bx - qx * bz; | ||
out[6] = qz * bw + qw * bz + qx * by - qy * bx; | ||
out[7] = qw * bw - qx * bx - qy * by - qz * bz; | ||
return out; | ||
} | ||
/** | ||
* Rotates a dual quat around a given axis. Does the normalisation automatically | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat2} a the dual quaternion to rotate | ||
* @param {ReadonlyVec3} axis the axis to rotate around | ||
* @param {Number} rad how far the rotation should be | ||
* @returns {quat2} out | ||
*/ | ||
export function rotateAroundAxis(out, a, axis, rad) { | ||
if (Math.abs(rad) < glMatrix.EPSILON) { | ||
return copy(out, a); | ||
} | ||
let axisLength = Math.sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]); | ||
rad = rad * 0.5; | ||
let s = Math.sin(rad); | ||
let bx = s * axis[0] / axisLength; | ||
let by = s * axis[1] / axisLength; | ||
let bz = s * axis[2] / axisLength; | ||
let bw = Math.cos(rad); | ||
let ax1 = a[0], | ||
ay1 = a[1], | ||
az1 = a[2], | ||
aw1 = a[3]; | ||
out[0] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by; | ||
out[1] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz; | ||
out[2] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx; | ||
out[3] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz; | ||
let ax = a[4], | ||
ay = a[5], | ||
az = a[6], | ||
aw = a[7]; | ||
out[4] = ax * bw + aw * bx + ay * bz - az * by; | ||
out[5] = ay * bw + aw * by + az * bx - ax * bz; | ||
out[6] = az * bw + aw * bz + ax * by - ay * bx; | ||
out[7] = aw * bw - ax * bx - ay * by - az * bz; | ||
return out; | ||
//Special case for rad = 0 | ||
if (Math.abs(rad) < glMatrix.EPSILON) { | ||
return copy(out, a); | ||
} | ||
let axisLength = Math.sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]); | ||
rad = rad * 0.5; | ||
let s = Math.sin(rad); | ||
let bx = (s * axis[0]) / axisLength; | ||
let by = (s * axis[1]) / axisLength; | ||
let bz = (s * axis[2]) / axisLength; | ||
let bw = Math.cos(rad); | ||
let ax1 = a[0], ay1 = a[1], az1 = a[2], aw1 = a[3]; | ||
out[0] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by; | ||
out[1] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz; | ||
out[2] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx; | ||
out[3] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz; | ||
let ax = a[4], ay = a[5], az = a[6], aw = a[7]; | ||
out[4] = ax * bw + aw * bx + ay * bz - az * by; | ||
out[5] = ay * bw + aw * by + az * bx - ax * bz; | ||
out[6] = az * bw + aw * bz + ax * by - ay * bx; | ||
out[7] = aw * bw - ax * bx - ay * by - az * bz; | ||
return out; | ||
} | ||
/** | ||
* Adds two dual quat's | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat2} a the first operand | ||
* @param {ReadonlyQuat2} b the second operand | ||
* @returns {quat2} out | ||
* @function | ||
*/ | ||
export function add(out, a, b) { | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
out[4] = a[4] + b[4]; | ||
out[5] = a[5] + b[5]; | ||
out[6] = a[6] + b[6]; | ||
out[7] = a[7] + b[7]; | ||
return out; | ||
out[0] = a[0] + b[0]; | ||
out[1] = a[1] + b[1]; | ||
out[2] = a[2] + b[2]; | ||
out[3] = a[3] + b[3]; | ||
out[4] = a[4] + b[4]; | ||
out[5] = a[5] + b[5]; | ||
out[6] = a[6] + b[6]; | ||
out[7] = a[7] + b[7]; | ||
return out; | ||
} | ||
/** | ||
* Multiplies two dual quat's | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat2} a the first operand | ||
* @param {ReadonlyQuat2} b the second operand | ||
* @returns {quat2} out | ||
*/ | ||
export function multiply(out, a, b) { | ||
let ax0 = a[0], | ||
ay0 = a[1], | ||
az0 = a[2], | ||
aw0 = a[3], | ||
bx1 = b[4], | ||
by1 = b[5], | ||
bz1 = b[6], | ||
bw1 = b[7], | ||
ax1 = a[4], | ||
ay1 = a[5], | ||
az1 = a[6], | ||
aw1 = a[7], | ||
bx0 = b[0], | ||
by0 = b[1], | ||
bz0 = b[2], | ||
bw0 = b[3]; | ||
out[0] = ax0 * bw0 + aw0 * bx0 + ay0 * bz0 - az0 * by0; | ||
out[1] = ay0 * bw0 + aw0 * by0 + az0 * bx0 - ax0 * bz0; | ||
out[2] = az0 * bw0 + aw0 * bz0 + ax0 * by0 - ay0 * bx0; | ||
out[3] = aw0 * bw0 - ax0 * bx0 - ay0 * by0 - az0 * bz0; | ||
out[4] = ax0 * bw1 + aw0 * bx1 + ay0 * bz1 - az0 * by1 + ax1 * bw0 + aw1 * bx0 + ay1 * bz0 - az1 * by0; | ||
out[5] = ay0 * bw1 + aw0 * by1 + az0 * bx1 - ax0 * bz1 + ay1 * bw0 + aw1 * by0 + az1 * bx0 - ax1 * bz0; | ||
out[6] = az0 * bw1 + aw0 * bz1 + ax0 * by1 - ay0 * bx1 + az1 * bw0 + aw1 * bz0 + ax1 * by0 - ay1 * bx0; | ||
out[7] = aw0 * bw1 - ax0 * bx1 - ay0 * by1 - az0 * bz1 + aw1 * bw0 - ax1 * bx0 - ay1 * by0 - az1 * bz0; | ||
return out; | ||
let ax0 = a[0], ay0 = a[1], az0 = a[2], aw0 = a[3], bx1 = b[4], by1 = b[5], bz1 = b[6], bw1 = b[7], ax1 = a[4], ay1 = a[5], az1 = a[6], aw1 = a[7], bx0 = b[0], by0 = b[1], bz0 = b[2], bw0 = b[3]; | ||
out[0] = ax0 * bw0 + aw0 * bx0 + ay0 * bz0 - az0 * by0; | ||
out[1] = ay0 * bw0 + aw0 * by0 + az0 * bx0 - ax0 * bz0; | ||
out[2] = az0 * bw0 + aw0 * bz0 + ax0 * by0 - ay0 * bx0; | ||
out[3] = aw0 * bw0 - ax0 * bx0 - ay0 * by0 - az0 * bz0; | ||
out[4] = | ||
ax0 * bw1 + aw0 * bx1 + ay0 * bz1 - az0 * by1 + ax1 * bw0 + aw1 * bx0 + ay1 * bz0 - az1 * by0; | ||
out[5] = | ||
ay0 * bw1 + aw0 * by1 + az0 * bx1 - ax0 * bz1 + ay1 * bw0 + aw1 * by0 + az1 * bx0 - ax1 * bz0; | ||
out[6] = | ||
az0 * bw1 + aw0 * bz1 + ax0 * by1 - ay0 * bx1 + az1 * bw0 + aw1 * bz0 + ax1 * by0 - ay1 * bx0; | ||
out[7] = | ||
aw0 * bw1 - ax0 * bx1 - ay0 * by1 - az0 * bz1 + aw1 * bw0 - ax1 * bx0 - ay1 * by0 - az1 * bz0; | ||
return out; | ||
} | ||
/** | ||
* Alias for {@link quat2.multiply} | ||
* @function | ||
*/ | ||
export const mul = multiply; | ||
/** | ||
* Scales a dual quat by a scalar number | ||
* | ||
* @param {quat2} out the receiving dual quat | ||
* @param {ReadonlyQuat2} a the dual quat to scale | ||
* @param {Number} b amount to scale the dual quat by | ||
* @returns {quat2} out | ||
* @function | ||
*/ | ||
export function scale(out, a, b) { | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
out[4] = a[4] * b; | ||
out[5] = a[5] * b; | ||
out[6] = a[6] * b; | ||
out[7] = a[7] * b; | ||
return out; | ||
out[0] = a[0] * b; | ||
out[1] = a[1] * b; | ||
out[2] = a[2] * b; | ||
out[3] = a[3] * b; | ||
out[4] = a[4] * b; | ||
out[5] = a[5] * b; | ||
out[6] = a[6] * b; | ||
out[7] = a[7] * b; | ||
return out; | ||
} | ||
/** | ||
* Calculates the dot product of two dual quat's (The dot product of the real parts) | ||
* | ||
* @param {ReadonlyQuat2} a the first operand | ||
* @param {ReadonlyQuat2} b the second operand | ||
* @returns {Number} dot product of a and b | ||
* @function | ||
*/ | ||
export const dot = quat.dot; | ||
/** | ||
* Performs a linear interpolation between two dual quats's | ||
* NOTE: The resulting dual quaternions won't always be normalized (The error is most noticeable when t = 0.5) | ||
* | ||
* @param {quat2} out the receiving dual quat | ||
* @param {ReadonlyQuat2} a the first operand | ||
* @param {ReadonlyQuat2} b the second operand | ||
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs | ||
* @returns {quat2} out | ||
*/ | ||
export function lerp(out, a, b, t) { | ||
let mt = 1 - t; | ||
if (dot(a, b) < 0) t = -t; | ||
out[0] = a[0] * mt + b[0] * t; | ||
out[1] = a[1] * mt + b[1] * t; | ||
out[2] = a[2] * mt + b[2] * t; | ||
out[3] = a[3] * mt + b[3] * t; | ||
out[4] = a[4] * mt + b[4] * t; | ||
out[5] = a[5] * mt + b[5] * t; | ||
out[6] = a[6] * mt + b[6] * t; | ||
out[7] = a[7] * mt + b[7] * t; | ||
return out; | ||
let mt = 1 - t; | ||
if (dot(a, b) < 0) | ||
t = -t; | ||
out[0] = a[0] * mt + b[0] * t; | ||
out[1] = a[1] * mt + b[1] * t; | ||
out[2] = a[2] * mt + b[2] * t; | ||
out[3] = a[3] * mt + b[3] * t; | ||
out[4] = a[4] * mt + b[4] * t; | ||
out[5] = a[5] * mt + b[5] * t; | ||
out[6] = a[6] * mt + b[6] * t; | ||
out[7] = a[7] * mt + b[7] * t; | ||
return out; | ||
} | ||
/** | ||
* Calculates the inverse of a dual quat. If they are normalized, conjugate is cheaper | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat2} a dual quat to calculate inverse of | ||
* @returns {quat2} out | ||
*/ | ||
export function invert(out, a) { | ||
let sqlen = squaredLength(a); | ||
out[0] = -a[0] / sqlen; | ||
out[1] = -a[1] / sqlen; | ||
out[2] = -a[2] / sqlen; | ||
out[3] = a[3] / sqlen; | ||
out[4] = -a[4] / sqlen; | ||
out[5] = -a[5] / sqlen; | ||
out[6] = -a[6] / sqlen; | ||
out[7] = a[7] / sqlen; | ||
return out; | ||
let sqlen = squaredLength(a); | ||
out[0] = -a[0] / sqlen; | ||
out[1] = -a[1] / sqlen; | ||
out[2] = -a[2] / sqlen; | ||
out[3] = a[3] / sqlen; | ||
out[4] = -a[4] / sqlen; | ||
out[5] = -a[5] / sqlen; | ||
out[6] = -a[6] / sqlen; | ||
out[7] = a[7] / sqlen; | ||
return out; | ||
} | ||
/** | ||
* Calculates the conjugate of a dual quat | ||
* If the dual quaternion is normalized, this function is faster than quat2.inverse and produces the same result. | ||
* | ||
* @param {quat2} out the receiving quaternion | ||
* @param {ReadonlyQuat2} a quat to calculate conjugate of | ||
* @returns {quat2} out | ||
*/ | ||
export function conjugate(out, a) { | ||
out[0] = -a[0]; | ||
out[1] = -a[1]; | ||
out[2] = -a[2]; | ||
out[3] = a[3]; | ||
out[4] = -a[4]; | ||
out[5] = -a[5]; | ||
out[6] = -a[6]; | ||
out[7] = a[7]; | ||
return out; | ||
out[0] = -a[0]; | ||
out[1] = -a[1]; | ||
out[2] = -a[2]; | ||
out[3] = a[3]; | ||
out[4] = -a[4]; | ||
out[5] = -a[5]; | ||
out[6] = -a[6]; | ||
out[7] = a[7]; | ||
return out; | ||
} | ||
/** | ||
* Calculates the length of a dual quat | ||
* | ||
* @param {ReadonlyQuat2} a dual quat to calculate length of | ||
* @returns {Number} length of a | ||
* @function | ||
*/ | ||
export const length = quat.length; | ||
/** | ||
* Alias for {@link quat2.length} | ||
* @function | ||
*/ | ||
export const len = length; | ||
/** | ||
* Calculates the squared length of a dual quat | ||
* | ||
* @param {ReadonlyQuat2} a dual quat to calculate squared length of | ||
* @returns {Number} squared length of a | ||
* @function | ||
*/ | ||
export const squaredLength = quat.squaredLength; | ||
/** | ||
* Alias for {@link quat2.squaredLength} | ||
* @function | ||
*/ | ||
export const sqrLen = squaredLength; | ||
/** | ||
* Normalize a dual quat | ||
* | ||
* @param {quat2} out the receiving dual quaternion | ||
* @param {ReadonlyQuat2} a dual quaternion to normalize | ||
* @returns {quat2} out | ||
* @function | ||
*/ | ||
export function normalize(out, a) { | ||
let magnitude = squaredLength(a); | ||
if (magnitude > 0) { | ||
magnitude = Math.sqrt(magnitude); | ||
let a0 = a[0] / magnitude; | ||
let a1 = a[1] / magnitude; | ||
let a2 = a[2] / magnitude; | ||
let a3 = a[3] / magnitude; | ||
let b0 = a[4]; | ||
let b1 = a[5]; | ||
let b2 = a[6]; | ||
let b3 = a[7]; | ||
let a_dot_b = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3; | ||
out[0] = a0; | ||
out[1] = a1; | ||
out[2] = a2; | ||
out[3] = a3; | ||
out[4] = (b0 - a0 * a_dot_b) / magnitude; | ||
out[5] = (b1 - a1 * a_dot_b) / magnitude; | ||
out[6] = (b2 - a2 * a_dot_b) / magnitude; | ||
out[7] = (b3 - a3 * a_dot_b) / magnitude; | ||
} | ||
return out; | ||
let magnitude = squaredLength(a); | ||
if (magnitude > 0) { | ||
magnitude = Math.sqrt(magnitude); | ||
let a0 = a[0] / magnitude; | ||
let a1 = a[1] / magnitude; | ||
let a2 = a[2] / magnitude; | ||
let a3 = a[3] / magnitude; | ||
let b0 = a[4]; | ||
let b1 = a[5]; | ||
let b2 = a[6]; | ||
let b3 = a[7]; | ||
let a_dot_b = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3; | ||
out[0] = a0; | ||
out[1] = a1; | ||
out[2] = a2; | ||
out[3] = a3; | ||
out[4] = (b0 - a0 * a_dot_b) / magnitude; | ||
out[5] = (b1 - a1 * a_dot_b) / magnitude; | ||
out[6] = (b2 - a2 * a_dot_b) / magnitude; | ||
out[7] = (b3 - a3 * a_dot_b) / magnitude; | ||
} | ||
return out; | ||
} | ||
/** | ||
* Returns a string representation of a dual quaternion | ||
* | ||
* @param {ReadonlyQuat2} a dual quaternion to represent as a string | ||
* @returns {String} string representation of the dual quat | ||
*/ | ||
export function str(a) { | ||
return 'quat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' + a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ')'; | ||
return ('quat2(' + | ||
a[0] + | ||
', ' + | ||
a[1] + | ||
', ' + | ||
a[2] + | ||
', ' + | ||
a[3] + | ||
', ' + | ||
a[4] + | ||
', ' + | ||
a[5] + | ||
', ' + | ||
a[6] + | ||
', ' + | ||
a[7] + | ||
')'); | ||
} | ||
/** | ||
* Returns whether or not the dual quaternions have exactly the same elements in the same position (when compared with ===) | ||
* | ||
* @param {ReadonlyQuat2} a the first dual quaternion. | ||
* @param {ReadonlyQuat2} b the second dual quaternion. | ||
* @returns {Boolean} true if the dual quaternions are equal, false otherwise. | ||
*/ | ||
export function exactEquals(a, b) { | ||
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7]; | ||
return (a[0] === b[0] && | ||
a[1] === b[1] && | ||
a[2] === b[2] && | ||
a[3] === b[3] && | ||
a[4] === b[4] && | ||
a[5] === b[5] && | ||
a[6] === b[6] && | ||
a[7] === b[7]); | ||
} | ||
/** | ||
* Returns whether or not the dual quaternions have approximately the same elements in the same position. | ||
* | ||
* @param {ReadonlyQuat2} a the first dual quat. | ||
* @param {ReadonlyQuat2} b the second dual quat. | ||
* @returns {Boolean} true if the dual quats are equal, false otherwise. | ||
*/ | ||
export function equals(a, b) { | ||
let a0 = a[0], | ||
a1 = a[1], | ||
a2 = a[2], | ||
a3 = a[3], | ||
a4 = a[4], | ||
a5 = a[5], | ||
a6 = a[6], | ||
a7 = a[7]; | ||
let b0 = b[0], | ||
b1 = b[1], | ||
b2 = b[2], | ||
b3 = b[3], | ||
b4 = b[4], | ||
b5 = b[5], | ||
b6 = b[6], | ||
b7 = b[7]; | ||
return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)); | ||
let a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5], a6 = a[6], a7 = a[7]; | ||
let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5], b6 = b[6], b7 = b[7]; | ||
return (Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && | ||
Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && | ||
Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && | ||
Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && | ||
Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && | ||
Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && | ||
Math.abs(a6 - b6) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && | ||
Math.abs(a7 - b7) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7))); | ||
} | ||
//# sourceMappingURL=quat2.js.map |
export type { TypedArray, NumericArray } from '@math.gl/types'; | ||
export type { isTypedArray, isNumericArray } from '@math.gl/types'; | ||
export { Vector2 } from './classes/vector2'; | ||
export { Vector3 } from './classes/vector3'; | ||
export { Vector4 } from './classes/vector4'; | ||
export { Matrix3 } from './classes/matrix3'; | ||
export { Matrix4 } from './classes/matrix4'; | ||
export { Quaternion } from './classes/quaternion'; | ||
export { SphericalCoordinates } from './classes/spherical-coordinates'; | ||
export { Pose } from './classes/pose'; | ||
export { Euler } from './classes/euler'; | ||
export * as _MathUtils from './lib/math-utils'; | ||
export { assert } from './lib/assert'; | ||
export { config, configure, formatValue, isArray, clone, equals, exactEquals, toRadians, toDegrees, radians, degrees, sin, cos, tan, asin, acos, atan, clamp, lerp, withEpsilon } from './lib/common'; | ||
export { SphericalCoordinates as _SphericalCoordinates } from './classes/spherical-coordinates'; | ||
export { Pose as _Pose } from './classes/pose'; | ||
export { Euler as _Euler } from './classes/euler'; | ||
export { Vector2 } from "./classes/vector2.js"; | ||
export { Vector3 } from "./classes/vector3.js"; | ||
export { Vector4 } from "./classes/vector4.js"; | ||
export { Matrix3 } from "./classes/matrix3.js"; | ||
export { Matrix4 } from "./classes/matrix4.js"; | ||
export { Quaternion } from "./classes/quaternion.js"; | ||
export { SphericalCoordinates } from "./classes/spherical-coordinates.js"; | ||
export { Pose } from "./classes/pose.js"; | ||
export { Euler } from "./classes/euler.js"; | ||
export * as _MathUtils from "./lib/math-utils.js"; | ||
export { assert } from "./lib/assert.js"; | ||
export { config, configure, formatValue, isArray, clone, equals, exactEquals, toRadians, toDegrees, radians, degrees, sin, cos, tan, asin, acos, atan, clamp, lerp, withEpsilon } from "./lib/common.js"; | ||
export { SphericalCoordinates as _SphericalCoordinates } from "./classes/spherical-coordinates.js"; | ||
export { Pose as _Pose } from "./classes/pose.js"; | ||
export { Euler as _Euler } from "./classes/euler.js"; | ||
/** @deprecated Use Matrix3 */ | ||
export * as mat3 from './gl-matrix/mat3'; | ||
export * as mat3 from "./gl-matrix/mat3.js"; | ||
/** @deprecated Use Matrix4 */ | ||
export * as mat4 from './gl-matrix/mat4'; | ||
export * as mat4 from "./gl-matrix/mat4.js"; | ||
/** @deprecated Use Quaterinion */ | ||
export * as quat from './gl-matrix/quat'; | ||
export * as quat from "./gl-matrix/quat.js"; | ||
/** @deprecated UseVector */ | ||
export * as vec2 from './gl-matrix/vec2'; | ||
export * as vec2 from "./gl-matrix/vec2.js"; | ||
/** @deprecated Use Vector3 */ | ||
export * as vec3 from './gl-matrix/vec3'; | ||
export * as vec3 from "./gl-matrix/vec3.js"; | ||
/** @deprecated Use Vector4 */ | ||
export * as vec4 from './gl-matrix/vec4'; | ||
//# sourceMappingURL=index.d.ts.map | ||
export * as vec4 from "./gl-matrix/vec4.js"; |
@@ -0,1 +1,3 @@ | ||
// luma.gl, MIT license | ||
// classes | ||
export { Vector2 } from "./classes/vector2.js"; | ||
@@ -7,24 +9,29 @@ export { Vector3 } from "./classes/vector3.js"; | ||
export { Quaternion } from "./classes/quaternion.js"; | ||
// experimental | ||
export { SphericalCoordinates } from "./classes/spherical-coordinates.js"; | ||
export { Pose } from "./classes/pose.js"; | ||
export { Euler } from "./classes/euler.js"; | ||
import * as _MathUtils from "./lib/math-utils.js"; | ||
export { _MathUtils }; | ||
export * as _MathUtils from "./lib/math-utils.js"; | ||
// lib | ||
export { assert } from "./lib/assert.js"; | ||
export { config, configure, formatValue, isArray, clone, equals, exactEquals, toRadians, toDegrees, radians, degrees, sin, cos, tan, asin, acos, atan, clamp, lerp, withEpsilon } from "./lib/common.js"; | ||
export { | ||
// math.gl global utility methods | ||
config, configure, formatValue, isArray, clone, equals, exactEquals, toRadians, toDegrees, | ||
// math.gl "GLSL"-style functions | ||
radians, degrees, sin, cos, tan, asin, acos, atan, clamp, lerp, withEpsilon } from "./lib/common.js"; | ||
// DEPRECATED | ||
export { SphericalCoordinates as _SphericalCoordinates } from "./classes/spherical-coordinates.js"; | ||
export { Pose as _Pose } from "./classes/pose.js"; | ||
export { Euler as _Euler } from "./classes/euler.js"; | ||
import * as _mat from "./gl-matrix/mat3.js"; | ||
export { _mat as mat3 }; | ||
import * as _mat2 from "./gl-matrix/mat4.js"; | ||
export { _mat2 as mat4 }; | ||
import * as _quat from "./gl-matrix/quat.js"; | ||
export { _quat as quat }; | ||
import * as _vec from "./gl-matrix/vec2.js"; | ||
export { _vec as vec2 }; | ||
import * as _vec2 from "./gl-matrix/vec3.js"; | ||
export { _vec2 as vec3 }; | ||
import * as _vec3 from "./gl-matrix/vec4.js"; | ||
export { _vec3 as vec4 }; | ||
//# sourceMappingURL=index.js.map | ||
/** @deprecated Use Matrix3 */ | ||
export * as mat3 from "./gl-matrix/mat3.js"; | ||
/** @deprecated Use Matrix4 */ | ||
export * as mat4 from "./gl-matrix/mat4.js"; | ||
/** @deprecated Use Quaterinion */ | ||
export * as quat from "./gl-matrix/quat.js"; | ||
/** @deprecated UseVector */ | ||
export * as vec2 from "./gl-matrix/vec2.js"; | ||
/** @deprecated Use Vector3 */ | ||
export * as vec3 from "./gl-matrix/vec3.js"; | ||
/** @deprecated Use Vector4 */ | ||
export * as vec4 from "./gl-matrix/vec4.js"; |
export declare function assert(condition: unknown, message?: string): void; | ||
//# sourceMappingURL=assert.d.ts.map |
export function assert(condition, message) { | ||
if (!condition) { | ||
throw new Error("math.gl assertion ".concat(message)); | ||
} | ||
if (!condition) { | ||
throw new Error(`math.gl assertion ${message}`); | ||
} | ||
} | ||
//# sourceMappingURL=assert.js.map |
import type { NumericArray } from '@math.gl/types'; | ||
import type { MathArray } from '../classes/base/math-array'; | ||
export declare type ConfigurationOptions = { | ||
import type { MathArray } from "../classes/base/math-array.js"; | ||
export type ConfigurationOptions = { | ||
EPSILON: number; | ||
@@ -100,2 +100,1 @@ debug?: boolean; | ||
export declare function withEpsilon<T>(epsilon: number, func: () => T): T; | ||
//# sourceMappingURL=common.d.ts.map |
@@ -1,184 +0,213 @@ | ||
const RADIANS_TO_DEGREES = 1 / Math.PI * 180; | ||
const DEGREES_TO_RADIANS = 1 / 180 * Math.PI; | ||
// math.gl, MIT license | ||
const RADIANS_TO_DEGREES = (1 / Math.PI) * 180; | ||
const DEGREES_TO_RADIANS = (1 / 180) * Math.PI; | ||
const DEFAULT_CONFIG = { | ||
EPSILON: 1e-12, | ||
debug: false, | ||
precision: 4, | ||
printTypes: false, | ||
printDegrees: false, | ||
printRowMajor: true, | ||
_cartographicRadians: false | ||
EPSILON: 1e-12, | ||
debug: false, | ||
precision: 4, | ||
printTypes: false, | ||
printDegrees: false, | ||
printRowMajor: true, | ||
_cartographicRadians: false | ||
}; | ||
globalThis.mathgl = globalThis.mathgl || { | ||
config: { ...DEFAULT_CONFIG | ||
} | ||
}; | ||
// Configuration is truly global as of v3.6 to ensure single config even if multiple copies of math.gl | ||
// Multiple copies of config can be quite tricky to debug... | ||
globalThis.mathgl = globalThis.mathgl || { config: { ...DEFAULT_CONFIG } }; | ||
export const config = globalThis.mathgl.config; | ||
export function configure(options) { | ||
Object.assign(config, options); | ||
return config; | ||
// Only copy existing keys | ||
Object.assign(config, options); | ||
return config; | ||
} | ||
export function formatValue(value, { | ||
precision = config.precision | ||
} = {}) { | ||
value = round(value); | ||
return "".concat(parseFloat(value.toPrecision(precision))); | ||
/** | ||
* Formats a value into a string | ||
* @param value | ||
* @param param1 | ||
* @returns | ||
*/ | ||
export function formatValue(value, { precision = config.precision } = {}) { | ||
value = round(value); | ||
// get rid of trailing zeros | ||
return `${parseFloat(value.toPrecision(precision))}`; | ||
} | ||
/** | ||
* Check if value is an "array" | ||
* Returns `true` if value is either an array or a typed array | ||
* Note: returns `false` for `ArrayBuffer` and `DataView` instances | ||
* @note isTypedArray and isNumericArray are often more useful in TypeScript | ||
*/ | ||
export function isArray(value) { | ||
return Array.isArray(value) || ArrayBuffer.isView(value) && !(value instanceof DataView); | ||
return Array.isArray(value) || (ArrayBuffer.isView(value) && !(value instanceof DataView)); | ||
} | ||
export function clone(array) { | ||
return 'clone' in array ? array.clone() : array.slice(); | ||
return 'clone' in array ? array.clone() : array.slice(); | ||
} | ||
export function toRadians(degrees) { | ||
return radians(degrees); | ||
return radians(degrees); | ||
} | ||
export function toDegrees(radians) { | ||
return degrees(radians); | ||
return degrees(radians); | ||
} | ||
export function radians(degrees, result) { | ||
return map(degrees, degrees => degrees * DEGREES_TO_RADIANS, result); | ||
return map(degrees, (degrees) => degrees * DEGREES_TO_RADIANS, result); | ||
} | ||
export function degrees(radians, result) { | ||
return map(radians, radians => radians * RADIANS_TO_DEGREES, result); | ||
return map(radians, (radians) => radians * RADIANS_TO_DEGREES, result); | ||
} | ||
/** | ||
* "GLSL equivalent" of `Math.sin`: Works on single values and vectors | ||
* @deprecated | ||
*/ | ||
export function sin(radians, result) { | ||
return map(radians, angle => Math.sin(angle), result); | ||
return map(radians, (angle) => Math.sin(angle), result); | ||
} | ||
/** | ||
* "GLSL equivalent" of `Math.cos`: Works on single values and vectors | ||
* @deprecated | ||
*/ | ||
export function cos(radians, result) { | ||
return map(radians, angle => Math.cos(angle), result); | ||
return map(radians, (angle) => Math.cos(angle), result); | ||
} | ||
/** | ||
* "GLSL equivalent" of `Math.tan`: Works on single values and vectors | ||
* @deprecated | ||
*/ | ||
export function tan(radians, result) { | ||
return map(radians, angle => Math.tan(angle), result); | ||
return map(radians, (angle) => Math.tan(angle), result); | ||
} | ||
/** | ||
* "GLSL equivalent" of `Math.asin`: Works on single values and vectors | ||
* @deprecated | ||
*/ | ||
export function asin(radians, result) { | ||
return map(radians, angle => Math.asin(angle), result); | ||
return map(radians, (angle) => Math.asin(angle), result); | ||
} | ||
/** | ||
* "GLSL equivalent" of `Math.acos`: Works on single values and vectors | ||
* @deprecated | ||
*/ | ||
export function acos(radians, result) { | ||
return map(radians, angle => Math.acos(angle), result); | ||
return map(radians, (angle) => Math.acos(angle), result); | ||
} | ||
/** | ||
* "GLSL equivalent" of `Math.atan`: Works on single values and vectors | ||
* @deprecated | ||
*/ | ||
export function atan(radians, result) { | ||
return map(radians, angle => Math.atan(angle), result); | ||
return map(radians, (angle) => Math.atan(angle), result); | ||
} | ||
export function clamp(value, min, max) { | ||
return map(value, value => Math.max(min, Math.min(max, value))); | ||
return map(value, (value) => Math.max(min, Math.min(max, value))); | ||
} | ||
export function lerp(a, b, t) { | ||
if (isArray(a)) { | ||
return a.map((ai, i) => lerp(ai, b[i], t)); | ||
} | ||
return t * b + (1 - t) * a; | ||
if (isArray(a)) { | ||
return a.map((ai, i) => lerp(ai, b[i], t)); | ||
} | ||
return t * b + (1 - t) * a; | ||
} | ||
/* eslint-disable */ | ||
/** | ||
* Compares any two math objects, using `equals` method if available. | ||
* @param a | ||
* @param b | ||
* @param epsilon | ||
* @returns | ||
*/ | ||
export function equals(a, b, epsilon) { | ||
const oldEpsilon = config.EPSILON; | ||
if (epsilon) { | ||
config.EPSILON = epsilon; | ||
} | ||
try { | ||
if (a === b) { | ||
return true; | ||
const oldEpsilon = config.EPSILON; | ||
if (epsilon) { | ||
config.EPSILON = epsilon; | ||
} | ||
if (isArray(a) && isArray(b)) { | ||
if (a.length !== b.length) { | ||
try { | ||
if (a === b) { | ||
return true; | ||
} | ||
if (isArray(a) && isArray(b)) { | ||
if (a.length !== b.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < a.length; ++i) { | ||
// eslint-disable-next-line max-depth | ||
if (!equals(a[i], b[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (a && a.equals) { | ||
return a.equals(b); | ||
} | ||
if (b && b.equals) { | ||
return b.equals(a); | ||
} | ||
if (typeof a === 'number' && typeof b === 'number') { | ||
return Math.abs(a - b) <= config.EPSILON * Math.max(1, Math.abs(a), Math.abs(b)); | ||
} | ||
return false; | ||
} | ||
for (let i = 0; i < a.length; ++i) { | ||
if (!equals(a[i], b[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
if (a && a.equals) { | ||
return a.equals(b); | ||
finally { | ||
config.EPSILON = oldEpsilon; | ||
} | ||
if (b && b.equals) { | ||
return b.equals(a); | ||
} | ||
if (typeof a === 'number' && typeof b === 'number') { | ||
return Math.abs(a - b) <= config.EPSILON * Math.max(1, Math.abs(a), Math.abs(b)); | ||
} | ||
return false; | ||
} finally { | ||
config.EPSILON = oldEpsilon; | ||
} | ||
} | ||
export function exactEquals(a, b) { | ||
if (a === b) { | ||
return true; | ||
} | ||
if (a && typeof a === 'object' && b && typeof b === 'object') { | ||
if (a.constructor !== b.constructor) { | ||
return false; | ||
if (a === b) { | ||
return true; | ||
} | ||
if (a.exactEquals) { | ||
return a.exactEquals(b); | ||
if (a && typeof a === 'object' && b && typeof b === 'object') { | ||
if (a.constructor !== b.constructor) { | ||
return false; | ||
} | ||
if (a.exactEquals) { | ||
return a.exactEquals(b); | ||
} | ||
} | ||
} | ||
if (isArray(a) && isArray(b)) { | ||
if (a.length !== b.length) { | ||
return false; | ||
if (isArray(a) && isArray(b)) { | ||
if (a.length !== b.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < a.length; ++i) { | ||
if (!exactEquals(a[i], b[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
for (let i = 0; i < a.length; ++i) { | ||
if (!exactEquals(a[i], b[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
return false; | ||
} | ||
/* eslint-enable */ | ||
export function withEpsilon(epsilon, func) { | ||
const oldPrecision = config.EPSILON; | ||
config.EPSILON = epsilon; | ||
let value; | ||
try { | ||
value = func(); | ||
} finally { | ||
config.EPSILON = oldPrecision; | ||
} | ||
return value; | ||
const oldPrecision = config.EPSILON; | ||
config.EPSILON = epsilon; | ||
let value; | ||
try { | ||
value = func(); | ||
} | ||
finally { | ||
config.EPSILON = oldPrecision; | ||
} | ||
return value; | ||
} | ||
// HELPERS | ||
function round(value) { | ||
return Math.round(value / config.EPSILON) * config.EPSILON; | ||
return Math.round(value / config.EPSILON) * config.EPSILON; | ||
} | ||
// If the array has a clone function, calls it, otherwise returns a copy | ||
function duplicateArray(array) { | ||
return array.clone ? array.clone() : new Array(array.length); | ||
// @ts-expect-error We check for math.gl class methods | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
return array.clone ? array.clone() : new Array(array.length); | ||
} | ||
// If the argument value is an array, applies the func element wise, | ||
// otherwise applies func to the argument value | ||
function map(value, func, result) { | ||
if (isArray(value)) { | ||
const array = value; | ||
result = result || duplicateArray(array); | ||
for (let i = 0; i < result.length && i < array.length; ++i) { | ||
const val = typeof value === 'number' ? value : value[i]; | ||
result[i] = func(val, i, result); | ||
if (isArray(value)) { | ||
const array = value; | ||
result = result || duplicateArray(array); | ||
for (let i = 0; i < result.length && i < array.length; ++i) { | ||
const val = typeof value === 'number' ? value : value[i]; | ||
result[i] = func(val, i, result); | ||
} | ||
return result; | ||
} | ||
return result; | ||
} | ||
return func(value); | ||
return func(value); | ||
} | ||
//# sourceMappingURL=common.js.map |
@@ -7,2 +7,1 @@ import { NumericArray } from '@math.gl/types'; | ||
export declare function vec4_transformMat3<T extends NumericArray>(out: T, a: Readonly<NumericArray>, m: Readonly<NumericArray>): T; | ||
//# sourceMappingURL=gl-matrix-extras.d.ts.map |
@@ -0,46 +1,49 @@ | ||
// vec2 additions | ||
export function vec2_transformMat4AsVector(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const w = m[3] * x + m[7] * y || 1.0; | ||
out[0] = (m[0] * x + m[4] * y) / w; | ||
out[1] = (m[1] * x + m[5] * y) / w; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const w = m[3] * x + m[7] * y || 1.0; | ||
out[0] = (m[0] * x + m[4] * y) / w; | ||
out[1] = (m[1] * x + m[5] * y) / w; | ||
return out; | ||
} | ||
// vec3 additions | ||
// Transform as vector, only uses 3x3 minor matrix | ||
export function vec3_transformMat4AsVector(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = m[3] * x + m[7] * y + m[11] * z || 1.0; | ||
out[0] = (m[0] * x + m[4] * y + m[8] * z) / w; | ||
out[1] = (m[1] * x + m[5] * y + m[9] * z) / w; | ||
out[2] = (m[2] * x + m[6] * y + m[10] * z) / w; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
const w = m[3] * x + m[7] * y + m[11] * z || 1.0; | ||
out[0] = (m[0] * x + m[4] * y + m[8] * z) / w; | ||
out[1] = (m[1] * x + m[5] * y + m[9] * z) / w; | ||
out[2] = (m[2] * x + m[6] * y + m[10] * z) / w; | ||
return out; | ||
} | ||
export function vec3_transformMat2(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[2] * y; | ||
out[1] = m[1] * x + m[3] * y; | ||
out[2] = a[2]; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[2] * y; | ||
out[1] = m[1] * x + m[3] * y; | ||
out[2] = a[2]; | ||
return out; | ||
} | ||
// vec4 additions | ||
export function vec4_transformMat2(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[2] * y; | ||
out[1] = m[1] * x + m[3] * y; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
out[0] = m[0] * x + m[2] * y; | ||
out[1] = m[1] * x + m[3] * y; | ||
out[2] = a[2]; | ||
out[3] = a[3]; | ||
return out; | ||
} | ||
export function vec4_transformMat3(out, a, m) { | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
out[0] = m[0] * x + m[3] * y + m[6] * z; | ||
out[1] = m[1] * x + m[4] * y + m[7] * z; | ||
out[2] = m[2] * x + m[5] * y + m[8] * z; | ||
out[3] = a[3]; | ||
return out; | ||
const x = a[0]; | ||
const y = a[1]; | ||
const z = a[2]; | ||
out[0] = m[0] * x + m[3] * y + m[6] * z; | ||
out[1] = m[1] * x + m[4] * y + m[7] * z; | ||
out[2] = m[2] * x + m[5] * y + m[8] * z; | ||
out[3] = a[3]; | ||
return out; | ||
} | ||
//# sourceMappingURL=gl-matrix-extras.js.map |
@@ -25,2 +25,1 @@ export declare const EPSILON1 = 0.1; | ||
export declare const TWO_PI: number; | ||
//# sourceMappingURL=math-utils.d.ts.map |
@@ -0,1 +1,3 @@ | ||
// NOTE: Added to make Cesium-derived test cases work | ||
// TODO: Determine if/how to keep | ||
export const EPSILON1 = 1e-1; | ||
@@ -25,2 +27,1 @@ export const EPSILON2 = 1e-2; | ||
export const TWO_PI = Math.PI * 2; | ||
//# sourceMappingURL=math-utils.js.map |
@@ -6,2 +6,1 @@ import { NumberArray } from '@math.gl/types'; | ||
export declare function deprecated(method: string, version: string): void; | ||
//# sourceMappingURL=validators.d.ts.map |
import { config } from "./common.js"; | ||
export function validateVector(v, length) { | ||
if (v.length !== length) { | ||
return false; | ||
} | ||
for (let i = 0; i < v.length; ++i) { | ||
if (!Number.isFinite(v[i])) { | ||
return false; | ||
if (v.length !== length) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
// Could be arguments "array" (v.every not availasble) | ||
for (let i = 0; i < v.length; ++i) { | ||
if (!Number.isFinite(v[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
export function checkNumber(value) { | ||
if (!Number.isFinite(value)) { | ||
throw new Error("Invalid number ".concat(JSON.stringify(value))); | ||
} | ||
return value; | ||
if (!Number.isFinite(value)) { | ||
throw new Error(`Invalid number ${JSON.stringify(value)}`); | ||
} | ||
return value; | ||
} | ||
export function checkVector(v, length, callerName = '') { | ||
if (config.debug && !validateVector(v, length)) { | ||
throw new Error("math.gl: ".concat(callerName, " some fields set to invalid numbers'")); | ||
} | ||
return v; | ||
if (config.debug && !validateVector(v, length)) { | ||
throw new Error(`math.gl: ${callerName} some fields set to invalid numbers'`); | ||
} | ||
return v; | ||
} | ||
const map = {}; | ||
export function deprecated(method, version) { | ||
if (!map[method]) { | ||
map[method] = true; | ||
console.warn("".concat(method, " has been removed in version ").concat(version, ", see upgrade guide for more information")); | ||
} | ||
if (!map[method]) { | ||
map[method] = true; | ||
// eslint-disable-next-line | ||
console.warn(`${method} has been removed in version ${version}, see upgrade guide for more information`); | ||
} | ||
} | ||
//# sourceMappingURL=validators.js.map |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"keywords": [ | ||
@@ -36,5 +36,5 @@ "webgl", | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs", | ||
"types": "./dist/index.d.ts" | ||
"require": "./dist/index.cjs" | ||
} | ||
@@ -50,6 +50,5 @@ }, | ||
"dependencies": { | ||
"@babel/runtime": "^7.12.0", | ||
"@math.gl/types": "4.0.0" | ||
"@math.gl/types": "4.0.1" | ||
}, | ||
"gitHead": "b7af99a25965af5112307552084fbaf5d4d53b7d" | ||
"gitHead": "33f369ba3a259f79acc3fa8181190c9da8841648" | ||
} |
@@ -195,3 +195,3 @@ // Copyright (c) 2017 Uber Technologies, Inc. | ||
rotate(radians: number): NumericArray { | ||
rotate(radians: number): this { | ||
mat3_rotate(this, this, radians); | ||
@@ -198,0 +198,0 @@ return this.check(); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
30616
1249837
94
+ Added@math.gl/types@4.0.1(transitive)
- Removed@babel/runtime@^7.12.0
- Removed@babel/runtime@7.25.6(transitive)
- Removed@math.gl/types@4.0.0(transitive)
- Removedregenerator-runtime@0.14.1(transitive)
Updated@math.gl/types@4.0.1