mirror of
https://github.com/pissang/claygl.git
synced 2026-02-01 17:27:08 +00:00
wip(type): add types to vector and matrix
This commit is contained in:
parent
578bc776ed
commit
b0a1575c35
@ -26,7 +26,7 @@
|
||||
|
||||
import { Mat2Array, Vec2Array } from './common';
|
||||
|
||||
export type { Vec2Array };
|
||||
export type { Mat2Array };
|
||||
/**
|
||||
* Creates a new identity mat2
|
||||
*
|
||||
|
||||
@ -1,286 +1,227 @@
|
||||
// @ts-nocheck
|
||||
import mat2 from '../glmatrix/mat2';
|
||||
import * as mat2 from '../glmatrix/mat2';
|
||||
import { matrixOrVectorClassToString } from './util';
|
||||
import type Vector2 from './Vector2';
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @alias clay.Matrix2
|
||||
*/
|
||||
const Matrix2 = function () {
|
||||
class Matrix2 {
|
||||
/**
|
||||
* Storage of Matrix2
|
||||
* @name array
|
||||
* @type {Float32Array}
|
||||
* @memberOf clay.Matrix2#
|
||||
*/
|
||||
this.array = mat2.create();
|
||||
|
||||
/**
|
||||
* @name _dirty
|
||||
* @type {boolean}
|
||||
* @memberOf clay.Matrix2#
|
||||
*/
|
||||
this._dirty = true;
|
||||
};
|
||||
|
||||
Matrix2.prototype = {
|
||||
constructor: Matrix2,
|
||||
array = mat2.create();
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Set components from array
|
||||
* @param {Float32Array|number[]} arr
|
||||
* @param arr
|
||||
*/
|
||||
setArray: function (arr) {
|
||||
setArray(arr: mat2.Mat2Array) {
|
||||
for (let i = 0; i < this.array.length; i++) {
|
||||
this.array[i] = arr[i];
|
||||
}
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
/**
|
||||
* Clone a new Matrix2
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
clone: function () {
|
||||
clone() {
|
||||
return new Matrix2().copy(this);
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy from b
|
||||
* @param {clay.Matrix2} b
|
||||
* @return {clay.Matrix2}
|
||||
* @param b
|
||||
*/
|
||||
copy: function (b) {
|
||||
copy(b: Matrix2) {
|
||||
mat2.copy(this.array, b.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the adjugate of self, in-place
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
adjoint: function () {
|
||||
adjoint() {
|
||||
mat2.adjoint(this.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate matrix determinant
|
||||
* @return {number}
|
||||
*/
|
||||
determinant: function () {
|
||||
determinant() {
|
||||
return mat2.determinant(this.array);
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Set to a identity matrix
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
identity: function () {
|
||||
identity() {
|
||||
mat2.identity(this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Invert self
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
invert: function () {
|
||||
invert() {
|
||||
mat2.invert(this.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for mutiply
|
||||
* @param {clay.Matrix2} b
|
||||
* @return {clay.Matrix2}
|
||||
* @param b
|
||||
*/
|
||||
mul: function (b) {
|
||||
mul(b: Matrix2) {
|
||||
mat2.mul(this.array, this.array, b.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for multiplyLeft
|
||||
* @param {clay.Matrix2} a
|
||||
* @return {clay.Matrix2}
|
||||
* @param a
|
||||
*/
|
||||
mulLeft: function (a) {
|
||||
mulLeft(a: Matrix2) {
|
||||
mat2.mul(this.array, a.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply self and b
|
||||
* @param {clay.Matrix2} b
|
||||
* @return {clay.Matrix2}
|
||||
* @param b
|
||||
*/
|
||||
multiply: function (b) {
|
||||
multiply(b: Matrix2) {
|
||||
mat2.multiply(this.array, this.array, b.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply a and self, a is on the left
|
||||
* @param {clay.Matrix2} a
|
||||
* @return {clay.Matrix2}
|
||||
* @param a
|
||||
*/
|
||||
multiplyLeft: function (a) {
|
||||
multiplyLeft(a: Matrix2) {
|
||||
mat2.multiply(this.array, a.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate self by a given radian
|
||||
* @param {number} rad
|
||||
* @return {clay.Matrix2}
|
||||
* @param {number} rad
|
||||
*/
|
||||
rotate: function (rad) {
|
||||
rotate(rad: number) {
|
||||
mat2.rotate(this.array, this.array, rad);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale self by s
|
||||
* @param {clay.Vector2} s
|
||||
* @return {clay.Matrix2}
|
||||
* @param s
|
||||
*/
|
||||
scale: function (v) {
|
||||
scale(v: Vector2) {
|
||||
mat2.scale(this.array, this.array, v.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
/**
|
||||
* Transpose self, in-place.
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
transpose: function () {
|
||||
transpose() {
|
||||
mat2.transpose(this.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
toString: function () {
|
||||
return '[' + Array.prototype.join.call(this.array, ',') + ']';
|
||||
},
|
||||
toString() {
|
||||
return matrixOrVectorClassToString(this, 2);
|
||||
}
|
||||
|
||||
toArray: function () {
|
||||
toArray() {
|
||||
return Array.prototype.slice.call(this.array);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Matrix2} out
|
||||
* @param {Matrix2} a
|
||||
* @return {Matrix2}
|
||||
*/
|
||||
Matrix2.adjoint = function (out, a) {
|
||||
mat2.adjoint(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static adjoint(out: Matrix2, a: Matrix2) {
|
||||
mat2.adjoint(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2} out
|
||||
* @param {clay.Matrix2} a
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
Matrix2.copy = function (out, a) {
|
||||
mat2.copy(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static copy(out: Matrix2, a: Matrix2) {
|
||||
mat2.copy(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2} a
|
||||
* @return {number}
|
||||
*/
|
||||
Matrix2.determinant = function (a) {
|
||||
return mat2.determinant(a.array);
|
||||
};
|
||||
/**
|
||||
* @param a
|
||||
*/
|
||||
static determinant(a: Matrix2) {
|
||||
return mat2.determinant(a.array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2} out
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
Matrix2.identity = function (out) {
|
||||
mat2.identity(out.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
*/
|
||||
static identity(out: Matrix2) {
|
||||
mat2.identity(out.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2} out
|
||||
* @param {clay.Matrix2} a
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
Matrix2.invert = function (out, a) {
|
||||
mat2.invert(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static invert(out: Matrix2, a: Matrix2) {
|
||||
mat2.invert(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2} out
|
||||
* @param {clay.Matrix2} a
|
||||
* @param {clay.Matrix2} b
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
Matrix2.mul = function (out, a, b) {
|
||||
mat2.mul(out.array, a.array, b.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
static mul(out: Matrix2, a: Matrix2, b: Matrix2) {
|
||||
mat2.mul(out.array, a.array, b.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @function
|
||||
* @param {clay.Matrix2} out
|
||||
* @param {clay.Matrix2} a
|
||||
* @param {clay.Matrix2} b
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
Matrix2.multiply = Matrix2.mul;
|
||||
/**
|
||||
* @function
|
||||
* @param out
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
static multiply = Matrix2.mul;
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2} out
|
||||
* @param {clay.Matrix2} a
|
||||
* @param {number} rad
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
Matrix2.rotate = function (out, a, rad) {
|
||||
mat2.rotate(out.array, a.array, rad);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
* @param {number} rad
|
||||
*/
|
||||
static rotate(out: Matrix2, a: Matrix2, rad: number) {
|
||||
mat2.rotate(out.array, a.array, rad);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2} out
|
||||
* @param {clay.Matrix2} a
|
||||
* @param {clay.Vector2} v
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
Matrix2.scale = function (out, a, v) {
|
||||
mat2.scale(out.array, a.array, v.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param {Matrix2} out
|
||||
* @param {Matrix2} a
|
||||
* @return {Matrix2}
|
||||
*/
|
||||
Matrix2.transpose = function (out, a) {
|
||||
mat2.transpose(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
* @param v
|
||||
*/
|
||||
static scale(out: Matrix2, a: Matrix2, v: Vector2) {
|
||||
mat2.scale(out.array, a.array, v.array);
|
||||
return out;
|
||||
}
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static transpose(out: Matrix2, a: Matrix2) {
|
||||
mat2.transpose(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
export default Matrix2;
|
||||
|
||||
@ -1,269 +1,210 @@
|
||||
// @ts-nocheck
|
||||
import mat2d from '../glmatrix/mat2d';
|
||||
import * as mat2d from '../glmatrix/mat2d';
|
||||
import { matrixOrVectorClassToString } from './util';
|
||||
import type Vector2 from './Vector2';
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @alias clay.Matrix2d
|
||||
*/
|
||||
const Matrix2d = function () {
|
||||
/**
|
||||
* Storage of Matrix2d
|
||||
* @name array
|
||||
* @type {Float32Array}
|
||||
* @memberOf clay.Matrix2d#
|
||||
*/
|
||||
this.array = mat2d.create();
|
||||
|
||||
/**
|
||||
* @name _dirty
|
||||
* @type {boolean}
|
||||
* @memberOf clay.Matrix2d#
|
||||
*/
|
||||
this._dirty = true;
|
||||
};
|
||||
|
||||
Matrix2d.prototype = {
|
||||
constructor: Matrix2d,
|
||||
class Matrix2d {
|
||||
array = mat2d.create();
|
||||
|
||||
/**
|
||||
* Set components from array
|
||||
* @param {Float32Array|number[]} arr
|
||||
* @param arr
|
||||
*/
|
||||
setArray: function (arr) {
|
||||
setArray(arr: mat2d.Mat2dArray) {
|
||||
for (let i = 0; i < this.array.length; i++) {
|
||||
this.array[i] = arr[i];
|
||||
}
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
/**
|
||||
* Clone a new Matrix2d
|
||||
* @return {clay.Matrix2d}
|
||||
*/
|
||||
clone: function () {
|
||||
clone() {
|
||||
return new Matrix2d().copy(this);
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy from b
|
||||
* @param {clay.Matrix2d} b
|
||||
* @return {clay.Matrix2d}
|
||||
* @param b
|
||||
*/
|
||||
copy: function (b) {
|
||||
copy(b: Matrix2d) {
|
||||
mat2d.copy(this.array, b.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate matrix determinant
|
||||
* @return {number}
|
||||
*/
|
||||
determinant: function () {
|
||||
determinant() {
|
||||
return mat2d.determinant(this.array);
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Set to a identity matrix
|
||||
* @return {clay.Matrix2d}
|
||||
*/
|
||||
identity: function () {
|
||||
identity() {
|
||||
mat2d.identity(this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Invert self
|
||||
* @return {clay.Matrix2d}
|
||||
*/
|
||||
invert: function () {
|
||||
invert() {
|
||||
mat2d.invert(this.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for mutiply
|
||||
* @param {clay.Matrix2d} b
|
||||
* @return {clay.Matrix2d}
|
||||
* @param b
|
||||
*/
|
||||
mul: function (b) {
|
||||
mul(b: Matrix2d) {
|
||||
mat2d.mul(this.array, this.array, b.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for multiplyLeft
|
||||
* @param {clay.Matrix2d} a
|
||||
* @return {clay.Matrix2d}
|
||||
* @param a
|
||||
*/
|
||||
mulLeft: function (b) {
|
||||
mulLeft(b: Matrix2d) {
|
||||
mat2d.mul(this.array, b.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply self and b
|
||||
* @param {clay.Matrix2d} b
|
||||
* @return {clay.Matrix2d}
|
||||
* @param b
|
||||
*/
|
||||
multiply: function (b) {
|
||||
multiply(b: Matrix2d) {
|
||||
mat2d.multiply(this.array, this.array, b.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply a and self, a is on the left
|
||||
* @param {clay.Matrix2d} a
|
||||
* @return {clay.Matrix2d}
|
||||
* @param a
|
||||
*/
|
||||
multiplyLeft: function (b) {
|
||||
multiplyLeft(b: Matrix2d) {
|
||||
mat2d.multiply(this.array, b.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate self by a given radian
|
||||
* @param {number} rad
|
||||
* @return {clay.Matrix2d}
|
||||
* @param {number} rad
|
||||
*/
|
||||
rotate: function (rad) {
|
||||
rotate(rad: number) {
|
||||
mat2d.rotate(this.array, this.array, rad);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale self by s
|
||||
* @param {clay.Vector2} s
|
||||
* @return {clay.Matrix2d}
|
||||
* @param {clay.Vector2} s
|
||||
*/
|
||||
scale: function (s) {
|
||||
scale(s: Vector2) {
|
||||
mat2d.scale(this.array, this.array, s.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate self by v
|
||||
* @param {clay.Vector2} v
|
||||
* @return {clay.Matrix2d}
|
||||
* @param {clay.Vector2} v
|
||||
*/
|
||||
translate: function (v) {
|
||||
translate(v: Vector2) {
|
||||
mat2d.translate(this.array, this.array, v.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
toString: function () {
|
||||
return '[' + Array.prototype.join.call(this.array, ',') + ']';
|
||||
},
|
||||
toString() {
|
||||
return matrixOrVectorClassToString(this, 3);
|
||||
}
|
||||
|
||||
toArray: function () {
|
||||
toArray() {
|
||||
return Array.prototype.slice.call(this.array);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2d} out
|
||||
* @param {clay.Matrix2d} a
|
||||
* @return {clay.Matrix2d}
|
||||
*/
|
||||
Matrix2d.copy = function (out, a) {
|
||||
mat2d.copy(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static copy(out: Matrix2d, a: Matrix2d) {
|
||||
mat2d.copy(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2d} a
|
||||
* @return {number}
|
||||
*/
|
||||
Matrix2d.determinant = function (a) {
|
||||
return mat2d.determinant(a.array);
|
||||
};
|
||||
/**
|
||||
* @param a
|
||||
*/
|
||||
static determinant(a: Matrix2d) {
|
||||
return mat2d.determinant(a.array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2d} out
|
||||
* @return {clay.Matrix2d}
|
||||
*/
|
||||
Matrix2d.identity = function (out) {
|
||||
mat2d.identity(out.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
*/
|
||||
static identity(out: Matrix2d) {
|
||||
mat2d.identity(out.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2d} out
|
||||
* @param {clay.Matrix2d} a
|
||||
* @return {clay.Matrix2d}
|
||||
*/
|
||||
Matrix2d.invert = function (out, a) {
|
||||
mat2d.invert(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static invert(out: Matrix2d, a: Matrix2d) {
|
||||
mat2d.invert(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2d} out
|
||||
* @param {clay.Matrix2d} a
|
||||
* @param {clay.Matrix2d} b
|
||||
* @return {clay.Matrix2d}
|
||||
*/
|
||||
Matrix2d.mul = function (out, a, b) {
|
||||
mat2d.mul(out.array, a.array, b.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
static mul(out: Matrix2d, a: Matrix2d, b: Matrix2d) {
|
||||
mat2d.mul(out.array, a.array, b.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @function
|
||||
* @param {clay.Matrix2d} out
|
||||
* @param {clay.Matrix2d} a
|
||||
* @param {clay.Matrix2d} b
|
||||
* @return {clay.Matrix2d}
|
||||
*/
|
||||
Matrix2d.multiply = Matrix2d.mul;
|
||||
/**
|
||||
* @function
|
||||
* @param out
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
static multiply = Matrix2d.mul;
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2d} out
|
||||
* @param {clay.Matrix2d} a
|
||||
* @param {number} rad
|
||||
* @return {clay.Matrix2d}
|
||||
*/
|
||||
Matrix2d.rotate = function (out, a, rad) {
|
||||
mat2d.rotate(out.array, a.array, rad);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
* @param {number} rad
|
||||
*/
|
||||
static rotate(out: Matrix2d, a: Matrix2d, rad: number) {
|
||||
mat2d.rotate(out.array, a.array, rad);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2d} out
|
||||
* @param {clay.Matrix2d} a
|
||||
* @param {clay.Vector2} v
|
||||
* @return {clay.Matrix2d}
|
||||
*/
|
||||
Matrix2d.scale = function (out, a, v) {
|
||||
mat2d.scale(out.array, a.array, v.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
* @param {clay.Vector2} v
|
||||
*/
|
||||
static scale(out: Matrix2d, a: Matrix2d, v: Vector2) {
|
||||
mat2d.scale(out.array, a.array, v.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix2d} out
|
||||
* @param {clay.Matrix2d} a
|
||||
* @param {clay.Vector2} v
|
||||
* @return {clay.Matrix2d}
|
||||
*/
|
||||
Matrix2d.translate = function (out, a, v) {
|
||||
mat2d.translate(out.array, a.array, v.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
* @param {clay.Vector2} v
|
||||
*/
|
||||
static translate(out: Matrix2d, a: Matrix2d, v: Vector2) {
|
||||
mat2d.translate(out.array, a.array, v.array);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
export default Matrix2d;
|
||||
|
||||
@ -1,395 +1,321 @@
|
||||
// @ts-nocheck
|
||||
import mat3 from '../glmatrix/mat3';
|
||||
import * as mat3 from '../glmatrix/mat3';
|
||||
import type Matrix2d from './Matrix2d';
|
||||
import type Matrix4 from './Matrix4';
|
||||
import type Quaternion from './Quaternion';
|
||||
import { matrixOrVectorClassToString } from './util';
|
||||
import type Vector2 from './Vector2';
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @alias clay.Matrix3
|
||||
*/
|
||||
const Matrix3 = function () {
|
||||
class Matrix3 {
|
||||
/**
|
||||
* Storage of Matrix3
|
||||
* @name array
|
||||
* @type {Float32Array}
|
||||
* @memberOf clay.Matrix3#
|
||||
*/
|
||||
this.array = mat3.create();
|
||||
|
||||
/**
|
||||
* @name _dirty
|
||||
* @type {boolean}
|
||||
* @memberOf clay.Matrix3#
|
||||
*/
|
||||
this._dirty = true;
|
||||
};
|
||||
|
||||
Matrix3.prototype = {
|
||||
constructor: Matrix3,
|
||||
array = mat3.create();
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Set components from array
|
||||
* @param {Float32Array|number[]} arr
|
||||
*/
|
||||
setArray: function (arr) {
|
||||
setArray(arr: mat3.Mat3Array) {
|
||||
for (let i = 0; i < this.array.length; i++) {
|
||||
this.array[i] = arr[i];
|
||||
}
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
/**
|
||||
* Calculate the adjugate of self, in-place
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
adjoint: function () {
|
||||
adjoint() {
|
||||
mat3.adjoint(this.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a new Matrix3
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
clone: function () {
|
||||
clone() {
|
||||
return new Matrix3().copy(this);
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy from b
|
||||
* @param {clay.Matrix3} b
|
||||
* @return {clay.Matrix3}
|
||||
* @param b
|
||||
*/
|
||||
copy: function (b) {
|
||||
copy(b: Matrix3) {
|
||||
mat3.copy(this.array, b.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate matrix determinant
|
||||
* @return {number}
|
||||
*/
|
||||
determinant: function () {
|
||||
determinant() {
|
||||
return mat3.determinant(this.array);
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the values from Matrix2d a
|
||||
* @param {clay.Matrix2d} a
|
||||
* @return {clay.Matrix3}
|
||||
* @param a
|
||||
*/
|
||||
fromMat2d: function (a) {
|
||||
fromMat2d(a: Matrix2d) {
|
||||
mat3.fromMat2d(this.array, a.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the upper-left 3x3 values of Matrix4
|
||||
* @param {clay.Matrix4} a
|
||||
* @return {clay.Matrix3}
|
||||
* @param a
|
||||
*/
|
||||
fromMat4: function (a) {
|
||||
fromMat4(a: Matrix4) {
|
||||
mat3.fromMat4(this.array, a.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a rotation matrix from the given quaternion
|
||||
* @param {clay.Quaternion} q
|
||||
* @return {clay.Matrix3}
|
||||
* @param q
|
||||
*/
|
||||
fromQuat: function (q) {
|
||||
fromQuat(q: Quaternion) {
|
||||
mat3.fromQuat(this.array, q.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Set to a identity matrix
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
identity: function () {
|
||||
identity() {
|
||||
mat3.identity(this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Invert self
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
invert: function () {
|
||||
invert() {
|
||||
mat3.invert(this.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for mutiply
|
||||
* @param {clay.Matrix3} b
|
||||
* @return {clay.Matrix3}
|
||||
* @param b
|
||||
*/
|
||||
mul: function (b) {
|
||||
mul(b: Matrix3) {
|
||||
mat3.mul(this.array, this.array, b.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for multiplyLeft
|
||||
* @param {clay.Matrix3} a
|
||||
* @return {clay.Matrix3}
|
||||
* @param a
|
||||
*/
|
||||
mulLeft: function (a) {
|
||||
mulLeft(a: Matrix3) {
|
||||
mat3.mul(this.array, a.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply self and b
|
||||
* @param {clay.Matrix3} b
|
||||
* @return {clay.Matrix3}
|
||||
* @param b
|
||||
*/
|
||||
multiply: function (b) {
|
||||
multiply(b: Matrix3) {
|
||||
mat3.multiply(this.array, this.array, b.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply a and self, a is on the left
|
||||
* @param {clay.Matrix3} a
|
||||
* @return {clay.Matrix3}
|
||||
* @param a
|
||||
*/
|
||||
multiplyLeft: function (a) {
|
||||
multiplyLeft(a: Matrix3) {
|
||||
mat3.multiply(this.array, a.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate self by a given radian
|
||||
* @param {number} rad
|
||||
* @return {clay.Matrix3}
|
||||
* @param rad
|
||||
*/
|
||||
rotate: function (rad) {
|
||||
rotate(rad: number) {
|
||||
mat3.rotate(this.array, this.array, rad);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale self by s
|
||||
* @param {clay.Vector2} s
|
||||
* @return {clay.Matrix3}
|
||||
* @param v
|
||||
*/
|
||||
scale: function (v) {
|
||||
scale(v: Vector2) {
|
||||
mat3.scale(this.array, this.array, v.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate self by v
|
||||
* @param {clay.Vector2} v
|
||||
* @return {clay.Matrix3}
|
||||
* @param v
|
||||
*/
|
||||
translate: function (v) {
|
||||
translate(v: Vector2) {
|
||||
mat3.translate(this.array, this.array, v.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
/**
|
||||
* Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
|
||||
* @param {clay.Matrix4} a
|
||||
* @param a
|
||||
*/
|
||||
normalFromMat4: function (a) {
|
||||
normalFromMat4(a: Matrix4) {
|
||||
mat3.normalFromMat4(this.array, a.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Transpose self, in-place.
|
||||
* @return {clay.Matrix2}
|
||||
*/
|
||||
transpose: function () {
|
||||
transpose() {
|
||||
mat3.transpose(this.array, this.array);
|
||||
this._dirty = true;
|
||||
return this;
|
||||
},
|
||||
}
|
||||
|
||||
toString: function () {
|
||||
return '[' + Array.prototype.join.call(this.array, ',') + ']';
|
||||
},
|
||||
toString() {
|
||||
return matrixOrVectorClassToString(this, 3);
|
||||
}
|
||||
|
||||
toArray: function () {
|
||||
toArray() {
|
||||
return Array.prototype.slice.call(this.array);
|
||||
}
|
||||
};
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix3} a
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.adjoint = function (out, a) {
|
||||
mat3.adjoint(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix3} a
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.copy = function (out, a) {
|
||||
mat3.copy(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static adjoint(out: Matrix3, a: Matrix3) {
|
||||
mat3.adjoint(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} a
|
||||
* @return {number}
|
||||
*/
|
||||
Matrix3.determinant = function (a) {
|
||||
return mat3.determinant(a.array);
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static copy(out: Matrix3, a: Matrix3) {
|
||||
mat3.copy(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.identity = function (out) {
|
||||
mat3.identity(out.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param a
|
||||
*/
|
||||
static determinant(a: Matrix3) {
|
||||
return mat3.determinant(a.array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix3} a
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.invert = function (out, a) {
|
||||
mat3.invert(out.array, a.array);
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
*/
|
||||
static identity(out: Matrix3) {
|
||||
mat3.identity(out.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix3} a
|
||||
* @param {clay.Matrix3} b
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.mul = function (out, a, b) {
|
||||
mat3.mul(out.array, a.array, b.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static invert(out: Matrix3, a: Matrix3) {
|
||||
mat3.invert(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @function
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix3} a
|
||||
* @param {clay.Matrix3} b
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.multiply = Matrix3.mul;
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
static mul(out: Matrix3, a: Matrix3, b: Matrix3) {
|
||||
mat3.mul(out.array, a.array, b.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix2d} a
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.fromMat2d = function (out, a) {
|
||||
mat3.fromMat2d(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @function
|
||||
* @param out
|
||||
* @param a
|
||||
* @param b
|
||||
*/
|
||||
static multiply = Matrix3.mul;
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix4} a
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.fromMat4 = function (out, a) {
|
||||
mat3.fromMat4(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static fromMat2d(out: Matrix3, a: Matrix2d) {
|
||||
mat3.fromMat2d(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Quaternion} a
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.fromQuat = function (out, q) {
|
||||
mat3.fromQuat(out.array, q.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static fromMat4(out: Matrix3, a: Matrix4) {
|
||||
mat3.fromMat4(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix4} a
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.normalFromMat4 = function (out, a) {
|
||||
mat3.normalFromMat4(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static fromQuat(out: Matrix3, q: Quaternion) {
|
||||
mat3.fromQuat(out.array, q.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix3} a
|
||||
* @param {number} rad
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.rotate = function (out, a, rad) {
|
||||
mat3.rotate(out.array, a.array, rad);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static normalFromMat4(out: Matrix3, a: Matrix4) {
|
||||
mat3.normalFromMat4(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix3} a
|
||||
* @param {clay.Vector2} v
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.scale = function (out, a, v) {
|
||||
mat3.scale(out.array, a.array, v.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
* @param rad
|
||||
*/
|
||||
static rotate(out: Matrix3, a: Matrix3, rad: number) {
|
||||
mat3.rotate(out.array, a.array, rad);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix3} a
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.transpose = function (out, a) {
|
||||
mat3.transpose(out.array, a.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
* @param v
|
||||
*/
|
||||
static scale(out: Matrix3, a: Matrix3, v: Vector2) {
|
||||
mat3.scale(out.array, a.array, v.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {clay.Matrix3} out
|
||||
* @param {clay.Matrix3} a
|
||||
* @param {clay.Vector2} v
|
||||
* @return {clay.Matrix3}
|
||||
*/
|
||||
Matrix3.translate = function (out, a, v) {
|
||||
mat3.translate(out.array, a.array, v.array);
|
||||
out._dirty = true;
|
||||
return out;
|
||||
};
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
*/
|
||||
static transpose(out: Matrix3, a: Matrix3) {
|
||||
mat3.transpose(out.array, a.array);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param out
|
||||
* @param a
|
||||
* @param v
|
||||
*/
|
||||
static translate(out: Matrix3, a: Matrix3, v: Vector2) {
|
||||
mat3.translate(out.array, a.array, v.array);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
export default Matrix3;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
1310
src/math/Vector3.ts
1310
src/math/Vector3.ts
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,11 +1,8 @@
|
||||
// @ts-nocheck
|
||||
const mathUtil = {};
|
||||
|
||||
mathUtil.isPowerOfTwo = function (value) {
|
||||
export function isPowerOfTwo(value: number) {
|
||||
return (value & (value - 1)) === 0;
|
||||
};
|
||||
}
|
||||
|
||||
mathUtil.nextPowerOfTwo = function (value) {
|
||||
export function nextPowerOfTwo(value: number) {
|
||||
value--;
|
||||
value |= value >> 1;
|
||||
value |= value >> 2;
|
||||
@ -15,10 +12,27 @@ mathUtil.nextPowerOfTwo = function (value) {
|
||||
value++;
|
||||
|
||||
return value;
|
||||
};
|
||||
}
|
||||
|
||||
mathUtil.nearestPowerOfTwo = function (value) {
|
||||
export function nearestPowerOfTwo(value: number) {
|
||||
return Math.pow(2, Math.round(Math.log(value) / Math.LN2));
|
||||
};
|
||||
}
|
||||
|
||||
export default mathUtil;
|
||||
export function formatMatrixString(array: number[], cols: number) {
|
||||
let str = '';
|
||||
for (let i = 0; i < Math.ceil(array.length / cols); i++) {
|
||||
str += array.slice(i * cols, (i + 1) * cols).join('\t') + '\n';
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
export function matrixOrVectorClassToString(obj: { array: number[] }, cols: number) {
|
||||
let str = '';
|
||||
const array = obj.array;
|
||||
const Clz = obj.constructor;
|
||||
const className = (Clz && Clz.name) || '';
|
||||
for (let i = 0; i < Math.ceil(array.length / cols); i++) {
|
||||
str += array.slice(i * cols, (i + 1) * cols).join('\t') + '\n';
|
||||
}
|
||||
return className + '[\n' + str + '\n]';
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user