* tslint

* fix node decl errors

* fix decl error
This commit is contained in:
Leslie Leigh
2019-03-11 23:52:17 +08:00
committed by Weiliang Wang
parent 7b806d34dc
commit 4e062977bc
14 changed files with 367 additions and 401 deletions
+4 -4
View File
@@ -349,7 +349,7 @@ function refreshScene () {
}
}
function adjustWidgetToAllowMovingInEditor (oldPos: Vec3) {
function adjustWidgetToAllowMovingInEditor (this: WidgetComponent, oldPos: Vec3) {
if (!CC_EDITOR) {
return;
}
@@ -358,7 +358,7 @@ function adjustWidgetToAllowMovingInEditor (oldPos: Vec3) {
return;
}
const self = this as WidgetComponent;
const self = this;
const newPos = self.node.getPosition();
const delta = newPos.sub(oldPos);
@@ -396,7 +396,7 @@ function adjustWidgetToAllowMovingInEditor (oldPos: Vec3) {
}
}
function adjustWidgetToAllowResizingInEditor (oldSize: Size) {
function adjustWidgetToAllowResizingInEditor (this: WidgetComponent, oldSize: Size) {
if (!CC_EDITOR) {
return;
}
@@ -405,7 +405,7 @@ function adjustWidgetToAllowResizingInEditor (oldSize: Size) {
return;
}
const self = this as WidgetComponent;
const self = this;
const newSize = self.node.getContentSize();
const delta = new Vec3(newSize.width - oldSize.width, newSize.height - oldSize.height, 0);
+1 -1
View File
@@ -13,7 +13,7 @@ class Empty { protected constructor () {}; }
export function EventTargetFactory<Base extends Constructor<{}>> (b?: Base) {
let base = b;
if (!base) {
base = <Base>Empty;
base = Empty as Base;
}
class EventTarget extends base {
private _callbacksInvoker = new CallbacksInvoker();
+39 -40
View File
@@ -1,9 +1,10 @@
import { IWritableArrayLike } from './fwd-decls';
import { EPSILON } from './utils';
/**
* Represents a color with red(r), green(g), blue(b) component of that color.
*/
class color3 {
export default class color3 {
/**
* Creates a color, with components specified separately, or a black color if not specified
@@ -23,7 +24,7 @@ class color3 {
* @param a - Color to clone.
* @return The newly created color.
*/
public static clone (a) {
public static clone (a: color3) {
return new color3(a.r, a.g, a.b);
}
@@ -34,7 +35,7 @@ class color3 {
* @param a - The specified color.
* @return out.
*/
public static copy (out, a) {
public static copy<Out extends color3> (out: Out, a: color3) {
out.r = a.r;
out.g = a.g;
out.b = a.b;
@@ -50,7 +51,7 @@ class color3 {
* @param b - Value assigned to b component.
* @return out.
*/
public static set (out, r, g, b) {
public static set<Out extends color3> (out: Out, r: number, g: number, b: number) {
out.r = r;
out.g = g;
out.b = b;
@@ -65,7 +66,7 @@ class color3 {
* @return out.
* @function
*/
public static fromHex (out, hex) {
public static fromHex<Out extends color3> (out: Out, hex: number) {
const r = ((hex >> 16)) / 255.0;
const g = ((hex >> 8) & 0xff) / 255.0;
const b = ((hex) & 0xff) / 255.0;
@@ -84,7 +85,7 @@ class color3 {
* @param b - The second operand.
* @return out.
*/
public static add (out, a, b) {
public static add<Out extends color3> (out: Out, a: color3, b: color3) {
out.r = a.r + b.r;
out.g = a.g + b.g;
out.b = a.b + b.b;
@@ -99,7 +100,7 @@ class color3 {
* @param b - The b.
* @return out.
*/
public static subtract (out, a, b) {
public static subtract<Out extends color3> (out: Out, a: color3, b: color3) {
out.r = a.r - b.r;
out.g = a.g - b.g;
out.b = a.b - b.b;
@@ -109,7 +110,7 @@ class color3 {
/**
* Alias of {@link color3.subtract}.
*/
public static sub (out, a, b) {
public static sub<Out extends color3> (out: Out, a: color3, b: color3) {
return color3.subtract(out, a, b);
}
@@ -121,7 +122,7 @@ class color3 {
* @param b - The second operand.
* @return out.
*/
public static multiply (out, a, b) {
public static multiply<Out extends color3> (out: Out, a: color3, b: color3) {
out.r = a.r * b.r;
out.g = a.g * b.g;
out.b = a.b * b.b;
@@ -131,7 +132,7 @@ class color3 {
/**
* Alias of {@link color3.multiply}.
*/
public static mul (out, a, b) {
public static mul<Out extends color3> (out: Out, a: color3, b: color3) {
return color3.multiply(out, a, b);
}
@@ -143,7 +144,7 @@ class color3 {
* @param b - The second operand.
* @return out.
*/
public static divide (out, a, b) {
public static divide<Out extends color3> (out: Out, a: color3, b: color3) {
out.r = a.r / b.r;
out.g = a.g / b.g;
out.b = a.b / b.b;
@@ -153,7 +154,7 @@ class color3 {
/**
* Alias of {@link color3.divide}.
*/
public static div (out, a, b) {
public static div<Out extends color3> (out: Out, a: color3, b: color3) {
return color3.divide(out, a, b);
}
@@ -165,7 +166,7 @@ class color3 {
* @param b - The scale number.
* @return out.
*/
public static scale (out, a, b) {
public static scale<Out extends color3> (out: Out, a: color3, b: number) {
out.r = a.r * b;
out.g = a.g * b;
out.b = a.b * b;
@@ -181,10 +182,8 @@ class color3 {
* @param t - The interpolation coefficient.
* @return out.
*/
public static lerp (out, a, b, t) {
const ar = a.r,
ag = a.g,
ab = a.b;
public static lerp<Out extends color3> (out: Out, a: color3, b: color3, t: number) {
const { r: ar, g: ag, b: ab } = a;
out.r = ar + t * (b.r - ar);
out.g = ag + t * (b.g - ag);
out.b = ab + t * (b.b - ab);
@@ -197,7 +196,7 @@ class color3 {
* @param a - The color.
* @return - String representation of this color.
*/
public static str (a) {
public static str (a: color3) {
return `color3(${a.r}, ${a.g}, ${a.b})`;
}
@@ -208,7 +207,7 @@ class color3 {
* @param a - The color.
* @return out.
*/
public static array (out, a) {
public static array<Out extends IWritableArrayLike<number>> (out: Out, a: color3) {
const scale = a instanceof cc.Color ? 1 / 255 : 1;
out[0] = a.r * scale;
out[1] = a.g * scale;
@@ -224,7 +223,7 @@ class color3 {
* @param b - The second color.
* @return True if the colors are equal, false otherwise.
*/
public static exactEquals (a, b) {
public static exactEquals (a: color3, b: color3) {
return a.r === b.r && a.g === b.g && a.b === b.b;
}
@@ -235,9 +234,9 @@ class color3 {
* @param b - The second color.
* @return True if the colors are approximately equal, false otherwise.
*/
public static equals (a, b) {
const a0 = a.r, a1 = a.g, a2 = a.b;
const b0 = b.r, b1 = b.g, b2 = b.b;
public static equals (a: color3, b: color3) {
const { r: a0, g: a1, b: a2} = a;
const { r: b0, g: b1, b: b2} = b;
return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)));
@@ -249,9 +248,25 @@ class color3 {
* @param a - The color.
* @return - The color's hexadecimal formal.
*/
public static hex (a) {
public static hex (a: color3) {
return (a.r * 255) << 16 | (a.g * 255) << 8 | (a.b * 255);
}
/**
* The r component.
*/
public r: number;
/**
* The g component.
*/
public g: number;
/**
* The b component.
*/
public b: number;
/**
* Creates a color, with components specified separately.
*
@@ -260,24 +275,8 @@ class color3 {
* @param b - Value assigned to b component.
*/
constructor (r = 1, g = 1, b = 1) {
/**
* The r component.
* @type {number}
* */
this.r = r;
/**
* The g component.
* @type {number}
* */
this.g = g;
/**
* The b component.
* @type {number}
* */
this.b = b;
}
}
export default color3;
+1
View File
@@ -1,3 +1,4 @@
import { IWritableArrayLike } from './fwd-decls';
import { EPSILON } from './utils';
// tslint:disable: one-variable-per-declaration
+1 -74
View File
@@ -1,77 +1,4 @@
declare interface IVec2Like {
x: number;
y: number;
}
declare interface IVec3Like {
x: number;
y: number;
z: number;
}
declare interface IVec4Like {
x: number;
y: number;
z: number;
w: number;
}
declare interface IQuatLike {
x: number;
y: number;
z: number;
w: number;
}
declare interface IMat2Like {
m00: number;
m01: number;
m02: number;
m03: number;
}
declare interface IMat23Like {
m00: number;
m01: number;
m02: number;
m03: number;
m04: number;
m05: number;
}
declare interface IMat3Like {
m00: number;
m01: number;
m02: number;
m03: number;
m04: number;
m05: number;
m06: number;
m07: number;
m08: number;
}
declare interface IMat4Like {
m00: number;
m01: number;
m02: number;
m03: number;
m04: number;
m05: number;
m06: number;
m07: number;
m08: number;
m09: number;
m10: number;
m11: number;
m12: number;
m13: number;
m14: number;
m15: number;
}
declare interface IWritableArrayLike<T> {
export interface IWritableArrayLike<T> {
readonly length: number;
[index: number]: T;
}
+66 -62
View File
@@ -1,9 +1,12 @@
import { IWritableArrayLike } from './fwd-decls';
import { EPSILON } from './utils';
import vec2 from './vec2';
/**
* Mathematical 2x2 matrix.
*/
class mat2 {
// tslint:disable-next-line:class-name
export default class mat2 {
/**
* Creates a matrix, with elements specified separately.
@@ -14,7 +17,7 @@ class mat2 {
* @param m03 - Value assigned to element at column 1 row 1.
* @return The newly created matrix.
*/
public static create(m00 = 1, m01 = 0, m02 = 0, m03 = 1) {
public static create (m00 = 1, m01 = 0, m02 = 0, m03 = 1) {
return new mat2(m00, m01, m02, m03);
}
@@ -24,7 +27,7 @@ class mat2 {
* @param a - Matrix to clone.
* @return The newly created matrix.
*/
public static clone(a) {
public static clone (a: mat2) {
return new mat2(a.m00, a.m01, a.m02, a.m03);
}
@@ -35,7 +38,7 @@ class mat2 {
* @param a - The specified matrix.
* @return out.
*/
public static copy(out, a) {
public static copy<Out extends mat2> (out: Out, a: mat2) {
out.m00 = a.m00;
out.m01 = a.m01;
out.m02 = a.m02;
@@ -49,7 +52,7 @@ class mat2 {
* @param out - Matrix to modified.
* @return out.
*/
public static identity(out) {
public static identity<Out extends mat2> (out: Out) {
out.m00 = 1;
out.m01 = 0;
out.m02 = 0;
@@ -67,7 +70,7 @@ class mat2 {
* @param m11 - Value assigned to element at column 1 row 1.
* @return out.
*/
public static set(out, m00, m01, m10, m11) {
public static set<Out extends mat2> (out: Out, m00: number, m01: number, m10: number, m11: number) {
out.m00 = m00;
out.m01 = m01;
out.m02 = m10;
@@ -82,7 +85,7 @@ class mat2 {
* @param a - Matrix to transpose.
* @return out.
*/
public static transpose(out, a) {
public static transpose<Out extends mat2> (out: Out, a: mat2) {
// If we are transposing ourselves we can skip a few steps but have to cache some values
if (out === a) {
const a1 = a.m01;
@@ -105,8 +108,8 @@ class mat2 {
* @param a - Matrix to invert.
* @return out.
*/
public static invert(out, a) {
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03;
public static invert<Out extends mat2> (out: Out, a: mat2) {
const { m00: a0, m01: a1, m02: a2, m03: a3 } = a;
// Calculate the determinant
let det = a0 * a3 - a2 * a1;
@@ -131,7 +134,7 @@ class mat2 {
* @param a - Matrix to calculate.
* @return out.
*/
public static adjoint(out, a) {
public static adjoint<Out extends mat2> (out: Out, a: mat2) {
// Caching this value is nessecary if out == a
const a0 = a.m00;
out.m00 = a.m03;
@@ -148,7 +151,7 @@ class mat2 {
* @param a - Matrix to calculate.
* @return Determinant of a.
*/
public static determinant(a) {
public static determinant (a: mat2) {
return a.m00 * a.m03 - a.m02 * a.m01;
}
@@ -160,9 +163,9 @@ class mat2 {
* @param b - The second operand.
* @return out.
*/
public static multiply(out, a, b) {
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03;
const b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03;
public static multiply<Out extends mat2> (out: Out, a: mat2, b: mat2) {
const { m00: a0, m01: a1, m02: a2, m03: a3 } = a;
const { m00: b0, m01: b1, m02: b2, m03: b3 } = b;
out.m00 = a0 * b0 + a2 * b1;
out.m01 = a1 * b0 + a3 * b1;
out.m02 = a0 * b2 + a2 * b3;
@@ -173,7 +176,7 @@ class mat2 {
/**
* Alias of {@link mat2.multiply}.
*/
public static mul(out, a, b) {
public static mul<Out extends mat2> (out: Out, a: mat2, b: mat2) {
return mat2.multiply(out, a, b);
}
@@ -185,10 +188,10 @@ class mat2 {
* @param rad - The rotation angle.
* @return out
*/
public static rotate(out, a, rad) {
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03,
s = Math.sin(rad),
c = Math.cos(rad);
public static rotate<Out extends mat2> (out: Out, a: mat2, rad: number) {
const { m00: a0, m01: a1, m02: a2, m03: a3 } = a;
const s = Math.sin(rad);
const c = Math.cos(rad);
out.m00 = a0 * c + a2 * s;
out.m01 = a1 * c + a3 * s;
out.m02 = a0 * -s + a2 * c;
@@ -203,10 +206,11 @@ class mat2 {
* @param a - Matrix to scale.
* @param v - The scale vector.
* @return out
**/
public static scale(out, a, v) {
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03,
v0 = v.x, v1 = v.y;
*/
public static scale<Out extends mat2> (out: Out, a: mat2, v: vec2) {
const { m00: a0, m01: a1, m02: a2, m03: a3 } = a;
const v0 = v.x;
const v1 = v.y;
out.m00 = a0 * v0;
out.m01 = a1 * v0;
out.m02 = a2 * v1;
@@ -225,9 +229,9 @@ class mat2 {
* @param rad - The rotation angle.
* @return out.
*/
public static fromRotation(out, rad) {
const s = Math.sin(rad),
c = Math.cos(rad);
public static fromRotation<Out extends mat2> (out: Out, rad: number) {
const s = Math.sin(rad);
const c = Math.cos(rad);
out.m00 = c;
out.m01 = s;
out.m02 = -s;
@@ -246,7 +250,7 @@ class mat2 {
* @param v - Scale vector.
* @return out.
*/
public static fromScaling(out, v) {
public static fromScaling<Out extends mat2> (out: Out, v: vec2) {
out.m00 = v.x;
out.m01 = 0;
out.m02 = 0;
@@ -260,7 +264,7 @@ class mat2 {
* @param a - The matrix.
* @return String representation of this matrix.
*/
public static str(a) {
public static str (a: mat2) {
return `mat2(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03})`;
}
@@ -271,7 +275,7 @@ class mat2 {
* @param m - The matrix.
* @return out.
*/
public static array(out, m, ofs = 0) {
public static array<Out extends IWritableArrayLike<number>> (out: Out, m: mat2, ofs = 0) {
out[ofs + 0] = m.m00;
out[ofs + 1] = m.m01;
out[ofs + 2] = m.m02;
@@ -286,7 +290,7 @@ class mat2 {
* @param a - Matrix to calculate Frobenius norm of.
* @return - The frobenius norm.
*/
public static frob(a) {
public static frob (a: mat2) {
return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2)));
}
@@ -297,7 +301,7 @@ class mat2 {
* @param U - The upper triangular matrix.
* @param a - The input matrix to factorize.
*/
public static LDU(L, D, U, a) {
public static LDU (L: mat2, D: mat2, U: mat2, a: mat2) {
L.m02 = a.m02 / a.m00;
U.m00 = a.m00;
U.m01 = a.m01;
@@ -312,7 +316,7 @@ class mat2 {
* @param b - The second operand.
* @return out.
*/
public static add(out, a, b) {
public static add<Out extends mat2> (out: Out, a: mat2, b: mat2) {
out.m00 = a.m00 + b.m00;
out.m01 = a.m01 + b.m01;
out.m02 = a.m02 + b.m02;
@@ -328,7 +332,7 @@ class mat2 {
* @param b - The second operand.
* @return out.
*/
public static subtract(out, a, b) {
public static subtract<Out extends mat2> (out: Out, a: mat2, b: mat2) {
out.m00 = a.m00 - b.m00;
out.m01 = a.m01 - b.m01;
out.m02 = a.m02 - b.m02;
@@ -339,7 +343,7 @@ class mat2 {
/**
* Alias of {@link mat2.subtract}.
*/
public static sub(out, a, b) {
public static sub<Out extends mat2> (out: Out, a: mat2, b: mat2) {
return mat2.subtract(out, a, b);
}
@@ -350,7 +354,7 @@ class mat2 {
* @param b - The second matrix.
* @return True if the matrices are equal, false otherwise.
*/
public static exactEquals(a, b) {
public static exactEquals (a: mat2, b: mat2) {
return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && a.m03 === b.m03;
}
@@ -361,9 +365,9 @@ class mat2 {
* @param b - The second matrix.
* @return True if the matrices are equal, false otherwise.
*/
public static equals(a, b) {
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03;
const b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03;
public static equals (a: mat2, b: mat2) {
const { m00: a0, m01: a1, m02: a2, m03: a3 } = a;
const { m00: b0, m01: b1, m02: b2, m03: b3 } = b;
return (
Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
@@ -380,7 +384,7 @@ class mat2 {
* @param b - The scale number.
* @return out.
*/
public static multiplyScalar(out, a, b) {
public static multiplyScalar<Out extends mat2> (out: Out, a: mat2, b: number) {
out.m00 = a.m00 * b;
out.m01 = a.m01 * b;
out.m02 = a.m02 * b;
@@ -397,13 +401,34 @@ class mat2 {
* @param scale - The scale number.
* @return out.
*/
public static multiplyScalarAndAdd(out, a, b, scale) {
public static multiplyScalarAndAdd<Out extends mat2> (out: Out, a: mat2, b: mat2, scale: number) {
out.m00 = a.m00 + (b.m00 * scale);
out.m01 = a.m01 + (b.m01 * scale);
out.m02 = a.m02 + (b.m02 * scale);
out.m03 = a.m03 + (b.m03 * scale);
return out;
}
/**
* The element at column 0 row 0.
*/
public m00: number;
/**
* The element at column 0 row 1.
*/
public m01: number;
/**
* The element at column 1 row 0.
*/
public m02: number;
/**
* The element at column 1 row 1.
*/
public m03: number;
/**
* Creates a matrix, with elements specified separately.
*
@@ -412,31 +437,10 @@ class mat2 {
* @param m02 - Value assigned to element at column 1 row 0.
* @param m03 - Value assigned to element at column 1 row 1.
*/
constructor(m00 = 1, m01 = 0, m02 = 0, m03 = 1) {
/**
* The element at column 0 row 0.
* @type {number}
* */
constructor (m00 = 1, m01 = 0, m02 = 0, m03 = 1) {
this.m00 = m00;
/**
* The element at column 0 row 1.
* @type {number}
* */
this.m01 = m01;
/**
* The element at column 1 row 0.
* @type {number}
* */
this.m02 = m02;
/**
* The element at column 1 row 1.
* @type {number}
* */
this.m03 = m03;
}
}
export default mat2;
+81 -73
View File
@@ -1,4 +1,6 @@
import { IWritableArrayLike } from './fwd-decls';
import { EPSILON } from './utils';
import vec2 from './vec2';
/**
* Mathematical 2x3 matrix.
@@ -16,6 +18,7 @@ import { EPSILON } from './utils';
* </pre>
* The last row is ignored so the array is shorter and operations are faster.
*/
// tslint:disable-next-line:class-name
class mat23 {
/**
@@ -29,7 +32,7 @@ class mat23 {
* @param m05 - Value assigned to element ty.
* @return The newly created matrix.
*/
public static create(m00 = 1, m01 = 0, m02 = 0, m03 = 1, m04 = 0, m05 = 0) {
public static create (m00 = 1, m01 = 0, m02 = 0, m03 = 1, m04 = 0, m05 = 0) {
return new mat23(m00, m01, m02, m03, m04, m05);
}
@@ -39,7 +42,7 @@ class mat23 {
* @param a - Matrix to clone.
* @return The newly created matrix.
*/
public static clone(a) {
public static clone (a: mat23) {
return new mat23(
a.m00, a.m01,
a.m02, a.m03,
@@ -54,7 +57,7 @@ class mat23 {
* @param a - The specified matrix.
* @return out.
*/
public static copy(out, a) {
public static copy<Out extends mat23> (out: Out, a: mat23) {
out.m00 = a.m00;
out.m01 = a.m01;
out.m02 = a.m02;
@@ -70,7 +73,7 @@ class mat23 {
* @param out - Matrix to modified.
* @return out.
*/
public static identity(out) {
public static identity<Out extends mat23> (out: Out) {
out.m00 = 1;
out.m01 = 0;
out.m02 = 0;
@@ -92,7 +95,7 @@ class mat23 {
* @param ty - Value assigned to element ty.
* @return out.
*/
public static set(out, a, b, c, d, tx, ty) {
public static set<Out extends mat23> (out: Out, a: number, b: number, c: number, d: number, tx: number, ty: number) {
out.m00 = a;
out.m01 = b;
out.m02 = c;
@@ -109,9 +112,8 @@ class mat23 {
* @param a - Matrix to invert.
* @return out.
*/
public static invert(out, a) {
const aa = a.m00, ab = a.m01, ac = a.m02, ad = a.m03,
atx = a.m04, aty = a.m05;
public static invert<Out extends mat23> (out: Out, a: mat23) {
const { m00: aa, m01: ab, m02: ac, m03: ad, m04: atx, m05: aty } = a;
let det = aa * ad - ab * ac;
if (!det) {
@@ -134,7 +136,7 @@ class mat23 {
* @param a - Matrix to calculate.
* @return Determinant of a.
*/
public static determinant(a) {
public static determinant (a: mat23) {
return a.m00 * a.m03 - a.m01 * a.m02;
}
@@ -146,9 +148,9 @@ class mat23 {
* @param b - The second operand.
* @return out.
*/
public static multiply(out, a, b) {
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05;
public static multiply<Out extends mat23> (out: Out, a: mat23, b: mat23) {
const { m00: a0, m01: a1, m02: a2, m03: a3, m04: a4, m05: a5 } = a;
const { m00: b0, m01: b1, m02: b2, m03: b3, m04: b4, m05: b5 } = b;
out.m00 = a0 * b0 + a2 * b1;
out.m01 = a1 * b0 + a3 * b1;
out.m02 = a0 * b2 + a2 * b3;
@@ -161,7 +163,7 @@ class mat23 {
/**
* Alias of {@link mat23.multiply}.
*/
public static mul(out, a, b) {
public static mul<Out extends mat23> (out: Out, a: mat23, b: mat23) {
return mat23.multiply(out, a, b);
}
@@ -173,10 +175,10 @@ class mat23 {
* @param rad - The rotation angle.
* @return out
*/
public static rotate(out, a, rad) {
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
s = Math.sin(rad),
c = Math.cos(rad);
public static rotate<Out extends mat23> (out: Out, a: mat23, rad: number) {
const { m00: a0, m01: a1, m02: a2, m03: a3, m04: a4, m05: a5 } = a;
const s = Math.sin(rad);
const c = Math.cos(rad);
out.m00 = a0 * c + a2 * s;
out.m01 = a1 * c + a3 * s;
out.m02 = a0 * -s + a2 * c;
@@ -193,10 +195,11 @@ class mat23 {
* @param a - Matrix to multiply.
* @param v - The scale vector.
* @return out
**/
public static scale(out, a, v) {
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
v0 = v.x, v1 = v.y;
*/
public static scale<Out extends mat23> (out: Out, a: mat23, v: vec2) {
const { m00: a0, m01: a1, m02: a2, m03: a3, m04: a4, m05: a5 } = a;
const v0 = v.x;
const v1 = v.y;
out.m00 = a0 * v0;
out.m01 = a1 * v0;
out.m02 = a2 * v1;
@@ -214,9 +217,10 @@ class mat23 {
* @param v - The translation offset.
* @return out.
*/
public static translate(out, a, v) {
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05,
v0 = v.x, v1 = v.y;
public static translate<Out extends mat23> (out: Out, a: mat23, v: vec2) {
const { m00: a0, m01: a1, m02: a2, m03: a3, m04: a4, m05: a5 } = a;
const v0 = v.x;
const v1 = v.y;
out.m00 = a0;
out.m01 = a1;
out.m02 = a2;
@@ -237,8 +241,9 @@ class mat23 {
* @param rad - The rotation angle.
* @return out.
*/
public static fromRotation(out, rad) {
const s = Math.sin(rad), c = Math.cos(rad);
public static fromRotation<Out extends mat23> (out: Out, rad: number) {
const s = Math.sin(rad);
const c = Math.cos(rad);
out.m00 = c;
out.m01 = s;
out.m02 = -s;
@@ -259,7 +264,7 @@ class mat23 {
* @param v - Scale vector.
* @return out.
*/
public static fromScaling(out, v) {
public static fromScaling<Out extends mat23> (out: Out, v: mat23) {
out.m00 = v.m00;
out.m01 = 0;
out.m02 = 0;
@@ -280,7 +285,7 @@ class mat23 {
* @param v - The translation offset.
* @return out.
*/
public static fromTranslation(out, v) {
public static fromTranslation<Out extends mat23> (out: Out, v: vec2) {
out.m00 = 1;
out.m01 = 0;
out.m02 = 0;
@@ -308,8 +313,9 @@ class mat23 {
* @param s - Scale vector.
* @return out.
*/
public static fromRTS(out, r, t, s) {
const sr = Math.sin(r), cr = Math.cos(r);
public static fromRTS<Out extends mat23> (out: Out, r: number, t: vec2, s: vec2) {
const sr = Math.sin(r);
const cr = Math.cos(r);
out.m00 = cr * s.x;
out.m01 = sr * s.x;
out.m02 = -sr * s.y;
@@ -325,7 +331,7 @@ class mat23 {
* @param a - The matrix.
* @return String representation of this matrix.
*/
public static str(a) {
public static str (a: mat23) {
return `mat23(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03}, ${a.m04}, ${a.m05})`;
}
@@ -336,7 +342,7 @@ class mat23 {
* @param m - The matrix.
* @return out.
*/
public static array(out, m) {
public static array<Out extends IWritableArrayLike<number>> (out: Out, m: mat23) {
out[0] = m.m00;
out[1] = m.m01;
out[2] = m.m02;
@@ -354,7 +360,7 @@ class mat23 {
* @param m
* @return
*/
public static array4x4(out, m) {
public static array4x4<Out extends IWritableArrayLike<number>> (out: Out, m: mat23) {
out[0] = m.m00;
out[1] = m.m01;
out[2] = 0;
@@ -381,7 +387,7 @@ class mat23 {
* @param a - Matrix to calculate Frobenius norm of.
* @return - The frobenius norm.
*/
public static frob(a) {
public static frob (a: mat23) {
return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2) + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + 1));
}
@@ -393,7 +399,7 @@ class mat23 {
* @param b - The second operand.
* @return out.
*/
public static add(out, a, b) {
public static add<Out extends mat23> (out: Out, a: mat23, b: mat23) {
out.m00 = a.m00 + b.m00;
out.m01 = a.m01 + b.m01;
out.m02 = a.m02 + b.m02;
@@ -411,7 +417,7 @@ class mat23 {
* @param b - The second operand.
* @return out.
*/
public static subtract(out, a, b) {
public static subtract<Out extends mat23> (out: Out, a: mat23, b: mat23) {
out.m00 = a.m00 - b.m00;
out.m01 = a.m01 - b.m01;
out.m02 = a.m02 - b.m02;
@@ -424,7 +430,7 @@ class mat23 {
/**
* Alias of {@link mat23.subtract}.
*/
public static sub(out, a, b) {
public static sub<Out extends mat23> (out: Out, a: mat23, b: mat23) {
return mat23.subtract(out, a, b);
}
@@ -436,7 +442,7 @@ class mat23 {
* @param b - The scale number.
* @return out.
*/
public static multiplyScalar(out, a, b) {
public static multiplyScalar<Out extends mat23> (out: Out, a: mat23, b: number) {
out.m00 = a.m00 * b;
out.m01 = a.m01 * b;
out.m02 = a.m02 * b;
@@ -455,7 +461,7 @@ class mat23 {
* @param scale - The scale number.
* @return out.
*/
public static multiplyScalarAndAdd(out, a, b, scale) {
public static multiplyScalarAndAdd<Out extends mat23> (out: Out, a: mat23, b: mat23, scale: number) {
out.m00 = a.m00 + (b.m00 * scale);
out.m01 = a.m01 + (b.m01 * scale);
out.m02 = a.m02 + (b.m02 * scale);
@@ -472,7 +478,7 @@ class mat23 {
* @param b - The second matrix.
* @return True if the matrices are equal, false otherwise.
*/
public static exactEquals(a, b) {
public static exactEquals (a: mat23, b: mat23) {
return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && a.m03 === b.m03 && a.m04 === b.m04 && a.m05 === b.m05;
}
@@ -483,9 +489,9 @@ class mat23 {
* @param b - The second matrix.
* @return True if the matrices are equal, false otherwise.
*/
public static equals(a, b) {
const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05;
const b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05;
public static equals (a: mat23, b: mat23) {
const { m00: a0, m01: a1, m02: a2, m03: a3, m04: a4, m05: a5 } = a;
const { m00: b0, m01: b1, m02: b2, m03: b3, m04: b4, m05: b5 } = b;
return (
Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
@@ -495,6 +501,37 @@ class mat23 {
Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5))
);
}
/**
* The element a.
*/
public m00: number;
/**
* The element b.
*/
public m01: number;
/**
* The element c.
*/
public m02: number;
/**
* The element d.
*/
public m03: number;
/**
* The element tx.
*/
public m04: number;
/**
* The element ty.
*/
public m05: number;
/**
* Creates a matrix, with elements specified separately.
*
@@ -505,41 +542,12 @@ class mat23 {
* @param m04 - Value assigned to element tx.
* @param m05 - Value assigned to element ty.
*/
constructor(m00 = 1, m01 = 0, m02 = 0, m03 = 1, m04 = 0, m05 = 0) {
/**
* The element a.
* @type {number}
* */
constructor (m00 = 1, m01 = 0, m02 = 0, m03 = 1, m04 = 0, m05 = 0) {
this.m00 = m00;
/**
* The element b.
* @type {number}
* */
this.m01 = m01;
/**
* The element c.
* @type {number}
* */
this.m02 = m02;
/**
* The element d.
* @type {number}
* */
this.m03 = m03;
/**
* The element tx.
* @type {number}
* */
this.m04 = m04;
/**
* The element ty.
* @type {number}
* */
this.m05 = m05;
}
}
+1
View File
@@ -29,6 +29,7 @@ import vec3 from './vec3';
* This is not unique to glMatrix, either, as OpenGL developers have long been confused by the
* apparent lack of consistency between the memory layout and the documentation.
*/
// tslint:disable-next-line:class-name
class mat3 {
/**
+1 -1
View File
@@ -2,7 +2,6 @@ import { EPSILON } from './utils';
// tslint:disable: one-variable-per-declaration
// tslint:disable: max-line-length
// tslint:disable: class-name
/**
* Mathematical 4x4 matrix.
@@ -30,6 +29,7 @@ import { EPSILON } from './utils';
* This is not unique to glMatrix, either, as OpenGL developers have long been confused by the
* apparent lack of consistency between the memory layout and the documentation.
*/
// tslint:disable-next-line:class-name
class mat4 {
/**
+51 -51
View File
@@ -1,5 +1,4 @@
/// <reference path="./fwd-decls" />
import { IWritableArrayLike } from './fwd-decls';
import mat3 from './mat3';
import { toDegree } from './utils';
import vec3 from './vec3';
@@ -13,6 +12,7 @@ const halfToRad = 0.5 * Math.PI / 180.0;
* A quaternion is a hypercomplex number represented by w + xi + yj + zk, where
* x, y, z and w are real numbers(called here its components), and i, j, and k are the fundamental quaternion units.
*/
// tslint:disable-next-line:class-name
export default class quat {
public static IDENTITY = new quat();
@@ -25,7 +25,7 @@ export default class quat {
* @param w - Value assigned to w component.
* @return The newly created quaternion.
*/
public static create(x = 0, y = 0, z = 0, w = 1) {
public static create (x = 0, y = 0, z = 0, w = 1) {
return new quat(x, y, z, w);
}
@@ -35,7 +35,7 @@ export default class quat {
* @param a - Quaternion to clone.
* @return The newly created quaternion.
*/
public static clone(a: quat) {
public static clone (a: quat) {
return new quat(a.x, a.y, a.z, a.w);
}
@@ -46,7 +46,7 @@ export default class quat {
* @param a - The specified quaternion.
* @return out.
*/
public static copy<Out extends IQuatLike>(out: Out, a: quat) {
public static copy<Out extends quat> (out: Out, a: quat) {
return vec4.copy(out, a);
}
@@ -60,7 +60,7 @@ export default class quat {
* @param w - Value set to w component.
* @return out.
*/
public static set<Out extends IQuatLike>(out: Out, x: number, y: number, z: number, w: number) {
public static set<Out extends quat> (out: Out, x: number, y: number, z: number, w: number) {
out.x = x;
out.y = y;
out.z = z;
@@ -74,7 +74,7 @@ export default class quat {
* @param out - Quaternion to set.
* @return out.
*/
public static identity<Out extends IQuatLike> (out: Out) {
public static identity<Out extends quat> (out: Out) {
out.x = 0;
out.y = 0;
out.z = 0;
@@ -93,7 +93,7 @@ export default class quat {
* @param b - The destination vector.
* @return out.
*/
public static rotationTo<Out extends IQuatLike>(out: Out, a: vec3, b: vec3) {
public static rotationTo<Out extends quat> (out: Out, a: vec3, b: vec3) {
const dot = vec3.dot(a, b);
if (dot < -0.999999) {
vec3.cross(tmpVec3, tmpXUnitVec3, a);
@@ -132,7 +132,7 @@ export default class quat {
* @param {quat} q - Quaternion to be decomposed.
* @return - Angle, in radians, of the rotation.
*/
public static getAxisAngle<Out extends IVec3Like>(outAxis: Out, q: quat) {
public static getAxisAngle<Out extends vec3> (outAxis: Out, q: quat) {
const rad = Math.acos(q.w) * 2.0;
const s = Math.sin(rad / 2.0);
if (s !== 0.0) {
@@ -156,7 +156,7 @@ export default class quat {
* @param b - The second operand.
* @return out.
*/
public static multiply<Out extends IQuatLike>(out: Out, a: quat, b: quat) {
public static multiply<Out extends quat> (out: Out, a: quat, b: quat) {
const { x: ax, y: ay, z: az, w: aw } = a;
const { x: bx, y: by, z: bz, w: bw } = b;
@@ -170,7 +170,7 @@ export default class quat {
/**
* Alias of {@link quat.multiply}.
*/
public static mul<Out extends IQuatLike>(out: Out, a: quat, b: quat) {
public static mul<Out extends quat> (out: Out, a: quat, b: quat) {
return quat.multiply(out, a, b);
}
@@ -182,7 +182,7 @@ export default class quat {
* @param b - The scale number.
* @return out.
*/
public static scale<Out extends IQuatLike>(out: Out, a: quat, b: number) {
public static scale<Out extends quat> (out: Out, a: quat, b: number) {
out.x = a.x * b;
out.y = a.y * b;
out.z = a.z * b;
@@ -198,7 +198,7 @@ export default class quat {
* @param rad - Angle (in radians) to rotate.
* @return out.
*/
public static rotateX<Out extends IQuatLike>(out: Out, a: quat, rad: number) {
public static rotateX<Out extends quat> (out: Out, a: quat, rad: number) {
rad *= 0.5;
const { x: ax, y: ay, z: az, w: aw } = a;
@@ -220,7 +220,7 @@ export default class quat {
* @param rad - Angle (in radians) to rotate.
* @return out.
*/
public static rotateY<Out extends IQuatLike>(out: Out, a: quat, rad: number) {
public static rotateY<Out extends quat> (out: Out, a: quat, rad: number) {
rad *= 0.5;
const { x: ax, y: ay, z: az, w: aw } = a;
@@ -242,7 +242,7 @@ export default class quat {
* @param rad - Angle (in radians) to rotate.
* @return out.
*/
public static rotateZ<Out extends IQuatLike>(out: Out, a: quat, rad: number) {
public static rotateZ<Out extends quat> (out: Out, a: quat, rad: number) {
rad *= 0.5;
const { x: ax, y: ay, z: az, w: aw } = a;
@@ -265,7 +265,7 @@ export default class quat {
* @param rad - Angle (in radians) to rotate.
* @return out.
*/
public static rotateAround<Out extends IQuatLike>(out: Out, rot: quat, axis: IVec3Like, rad: number) {
public static rotateAround<Out extends quat> (out: Out, rot: quat, axis: vec3, rad: number) {
// get inv-axis (local to rot)
quat.invert(tmpQuat1, rot);
vec3.transformQuat(tmpVec3, axis, tmpQuat1);
@@ -284,7 +284,7 @@ export default class quat {
* @param rad - Angle (in radians) to rotate.
* @return out.
*/
public static rotateAroundLocal<Out extends IQuatLike>(out: Out, rot: quat, axis: IVec3Like, rad: number) {
public static rotateAroundLocal<Out extends quat> (out: Out, rot: quat, axis: vec3, rad: number) {
quat.fromAxisAngle(tmpQuat1, axis, rad);
quat.mul(out, rot, tmpQuat1);
return out;
@@ -299,7 +299,7 @@ export default class quat {
* @param a - Quaternion to calculate W.
* @return out.
*/
public static calculateW<Out extends IQuatLike>(out: Out, a: quat) {
public static calculateW<Out extends quat> (out: Out, a: quat) {
const { x, y, z } = a;
out.x = x;
@@ -316,7 +316,7 @@ export default class quat {
* @param b - The second operand.
* @return - The dot product of a and b.
*/
public static dot(a: quat, b: quat) {
public static dot (a: quat, b: quat) {
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
@@ -329,7 +329,7 @@ export default class quat {
* @param t - The interpolation coefficient.
* @return out.
*/
public static lerp<Out extends IQuatLike>(out: Out, a: quat, b: quat, t: number) {
public static lerp<Out extends quat> (out: Out, a: quat, b: quat, t: number) {
const { x: ax, y: ay, z: az, w: aw } = a;
out.x = ax + t * (b.x - ax);
out.y = ay + t * (b.y - ay);
@@ -347,7 +347,7 @@ export default class quat {
* @param t - The interpolation coefficient.
* @return out.
*/
public static slerp<Out extends IQuatLike>(out: Out, a: quat, b: quat, t: number) {
public static slerp<Out extends quat> (out: Out, a: quat, b: quat, t: number) {
// benchmarks:
// http://jsperf.com/quaternion-slerp-implementations
@@ -400,7 +400,7 @@ export default class quat {
* @param t - The interpolation coefficient.
* @return out
*/
public static sqlerp<Out extends IQuatLike>(out: Out, a: quat, b: quat, c: quat, d: quat, t: number) {
public static sqlerp<Out extends quat> (out: Out, a: quat, b: quat, c: quat, d: quat, t: number) {
quat.slerp(tmpQuat1, a, d, t);
quat.slerp(tmpQuat2, b, c, t);
quat.slerp(out, tmpQuat1, tmpQuat2, 2 * t * (1 - t));
@@ -414,7 +414,7 @@ export default class quat {
* @param a - Quaternion to calculate inverse of.
* @return out.
*/
public static invert<Out extends IQuatLike>(out: Out, a: quat) {
public static invert<Out extends quat> (out: Out, a: quat) {
const { x: a0, y: a1, z: a2, w: a3 } = a;
const dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
const invDot = dot ? 1.0 / dot : 0;
@@ -436,7 +436,7 @@ export default class quat {
* @param a - Quaternion to calculate conjugate of.
* @return out.
*/
public static conjugate<Out extends IQuatLike>(out: Out, a: quat) {
public static conjugate<Out extends quat> (out: Out, a: quat) {
out.x = -a.x;
out.y = -a.y;
out.z = -a.z;
@@ -450,7 +450,7 @@ export default class quat {
* @param a - The quaternion.
* @return Length of the quaternion.
*/
public static magnitude(a: quat) {
public static magnitude (a: quat) {
const { x, y, z, w } = a;
return Math.sqrt(x * x + y * y + z * z + w * w);
}
@@ -458,7 +458,7 @@ export default class quat {
/**
* Alias of {@link quat.magnitude}.
*/
public static mag(a: quat) {
public static mag (a: quat) {
return quat.magnitude(a);
}
@@ -468,7 +468,7 @@ export default class quat {
* @param a - The quaternion.
* @return Squared length of the quaternion.
*/
public static squaredMagnitude(a: quat) {
public static squaredMagnitude (a: quat) {
const { x, y, z, w } = a;
return x * x + y * y + z * z + w * w;
}
@@ -476,7 +476,7 @@ export default class quat {
/**
* Alias of {@link quat.squaredMagnitude}
*/
public static sqrMag(a: quat) {
public static sqrMag (a: quat) {
return quat.squaredMagnitude(a);
}
@@ -488,7 +488,7 @@ export default class quat {
* @return out.
* @function
*/
public static normalize<Out extends IQuatLike>(out: Out, a: quat) {
public static normalize<Out extends quat> (out: Out, a: quat) {
const { x, y, z, w } = a;
let len = x * x + y * y + z * z + w * w;
if (len > 0) {
@@ -512,7 +512,7 @@ export default class quat {
* @param zAxis - Vector representing the viewing direction.
* @return out.
*/
public static fromAxes<Out extends IQuatLike>(out: Out, xAxis: IVec3Like, yAxis: IVec3Like, zAxis: IVec3Like) {
public static fromAxes<Out extends quat> (out: Out, xAxis: vec3, yAxis: vec3, zAxis: vec3) {
mat3.set(tmpMat3,
xAxis.x, xAxis.y, xAxis.z,
yAxis.x, yAxis.y, yAxis.z,
@@ -530,7 +530,7 @@ export default class quat {
*
* @return out.
*/
public static fromViewUp<Out extends IQuatLike>(out: Out, view: IVec3Like, up: IVec3Like) {
public static fromViewUp<Out extends quat> (out: Out, view: vec3, up: vec3) {
mat3.fromViewUp(tmpMat3, view, up);
return quat.normalize(out, quat.fromMat3(out, tmpMat3));
}
@@ -544,7 +544,7 @@ export default class quat {
* @param rad - The angle in radians.
* @return out.
*/
public static fromAxisAngle<Out extends IQuatLike>(out: Out, axis: IVec3Like, rad: number) {
public static fromAxisAngle<Out extends quat> (out: Out, axis: vec3, rad: number) {
rad = rad * 0.5;
const s = Math.sin(rad);
out.x = s * axis.x;
@@ -565,7 +565,7 @@ export default class quat {
* @return out.
* @function
*/
public static fromMat3<Out extends IQuatLike>(out: Out, m: IMat3Like) {
public static fromMat3<Out extends quat> (out: Out, m: mat3) {
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
const {
@@ -622,7 +622,7 @@ export default class quat {
* @return out.
* @function
*/
public static fromEuler<Out extends IQuatLike>(out: Out, x: number, y: number, z: number) {
public static fromEuler<Out extends quat> (out: Out, x: number, y: number, z: number) {
x *= halfToRad;
y *= halfToRad;
z *= halfToRad;
@@ -649,9 +649,9 @@ export default class quat {
* @param q - The quaternion.
* @function
*/
public static toAxisX(out: vec3, q: quat) {
let fy = 2.0 * q.y;
let fz = 2.0 * q.z;
public static toAxisX (out: vec3, q: quat) {
const fy = 2.0 * q.y;
const fz = 2.0 * q.z;
out.x = 1.0 - fy * q.y - fz * q.z;
out.y = fy * q.x + fz * q.w;
out.z = fz * q.x + fy * q.w;
@@ -664,10 +664,10 @@ export default class quat {
* @param q - The quaternion.
* @function
*/
public static toAxisY(out: vec3, q: quat) {
let fx = 2.0 * q.x;
let fy = 2.0 * q.y;
let fz = 2.0 * q.z;
public static toAxisY (out: vec3, q: quat) {
const fx = 2.0 * q.x;
const fy = 2.0 * q.y;
const fz = 2.0 * q.z;
out.x = fy * q.x - fz * q.w;
out.y = 1.0 - fx * q.x - fz * q.z;
out.z = fz * q.y + fx * q.w;
@@ -680,10 +680,10 @@ export default class quat {
* @param q - The quaternion.
* @function
*/
public static toAxisZ(out: vec3, q: quat) {
let fx = 2.0 * q.x;
let fy = 2.0 * q.y;
let fz = 2.0 * q.z;
public static toAxisZ (out: vec3, q: quat) {
const fx = 2.0 * q.x;
const fy = 2.0 * q.y;
const fz = 2.0 * q.z;
out.x = fz * q.x - fy * q.w;
out.y = fz * q.y - fx * q.w;
out.z = 1.0 - fx * q.x - fy * q.y;
@@ -696,7 +696,7 @@ export default class quat {
* @param q - the quaternion to be converted
* @return out.
*/
public static toEuler<Out extends IVec3Like>(out: Out, q: quat) {
public static toEuler<Out extends vec3> (out: Out, q: quat) {
const { x, y, z, w } = q;
let heading: number = NaN;
let attitude: number = NaN;
@@ -735,7 +735,7 @@ export default class quat {
* @param a - The quaternion.
* @return - String representation of this quaternion.
*/
public static str(a: quat) {
public static str (a: quat) {
return `quat(${a.x}, ${a.y}, ${a.z}, ${a.w})`;
}
@@ -746,7 +746,7 @@ export default class quat {
* @param q - The quaternion.
* @return out.
*/
public static array<Out extends IWritableArrayLike<number>>(out: Out, q: quat) {
public static array<Out extends IWritableArrayLike<number>> (out: Out, q: quat) {
out[0] = q.x;
out[1] = q.y;
out[2] = q.z;
@@ -762,7 +762,7 @@ export default class quat {
* @param b - The second quaternion.
* @return True if the quaternions are equal, false otherwise.
*/
public static exactEquals(a: quat, b: quat) {
public static exactEquals (a: quat, b: quat) {
return vec4.exactEquals(a, b);
}
@@ -773,7 +773,7 @@ export default class quat {
* @param b The second quaternion.
* @return True if the quaternions are approximately equal, false otherwise.
*/
public static equals(a: quat, b: quat) {
public static equals (a: quat, b: quat) {
return vec4.equals(a, b);
}
@@ -805,7 +805,7 @@ export default class quat {
* @param z - Value assigned to z component.
* @param w - Value assigned to w component.
*/
constructor(x = 0, y = 0, z = 0, w = 1) {
constructor (x = 0, y = 0, z = 0, w = 1) {
this.x = x;
this.y = y;
this.z = z;
+35 -30
View File
@@ -1,12 +1,17 @@
/// <reference path="./fwd-decls" />
import { IWritableArrayLike } from './fwd-decls';
import mat2 from './mat2';
import mat23 from './mat23';
import mat3 from './mat3';
import mat4 from './mat4';
import { EPSILON, random } from './utils';
import vec3 from './vec3';
/**
* Mathematical 2-dimensional vector.
*
* x, y is alias of the first, second component of vector, respectively.
*/
// tslint:disable-next-line:class-name
export default class vec2 {
public static ZERO = new vec2(0, 0);
public static ONE = new vec2(1, 1);
@@ -28,7 +33,7 @@ export default class vec2 {
*
* @return The newly created vector.
*/
public static zero<Out extends IVec2Like> (out: Out) {
public static zero<Out extends vec2> (out: Out) {
out.x = 0;
out.y = 0;
return out;
@@ -51,7 +56,7 @@ export default class vec2 {
* @param a - The specified vector.
* @return out.
*/
public static copy<Out extends IVec2Like> (out: Out, a: vec2) {
public static copy<Out extends vec2> (out: Out, a: vec2) {
out.x = a.x;
out.y = a.y;
return out;
@@ -65,7 +70,7 @@ export default class vec2 {
* @param y - Value set to y component.
* @return out.
*/
public static set<Out extends IVec2Like> (out: Out, x: number, y: number) {
public static set<Out extends vec2> (out: Out, x: number, y: number) {
out.x = x;
out.y = y;
return out;
@@ -81,7 +86,7 @@ export default class vec2 {
* @param b - The second operand.
* @return out.
*/
public static add<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
public static add<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = a.x + b.x;
out.y = a.y + b.y;
return out;
@@ -97,7 +102,7 @@ export default class vec2 {
* @param b - The second operand.
* @return out.
*/
public static subtract<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
public static subtract<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = a.x - b.x;
out.y = a.y - b.y;
return out;
@@ -106,7 +111,7 @@ export default class vec2 {
/**
* Alias of {@link vec2.subtract}.
*/
public static sub<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
public static sub<Out extends vec2> (out: Out, a: vec2, b: vec2) {
return vec2.subtract(out, a, b);
}
@@ -120,7 +125,7 @@ export default class vec2 {
* @param b - The second operand.
* @return out.
*/
public static multiply<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
public static multiply<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = a.x * b.x;
out.y = a.y * b.y;
return out;
@@ -129,7 +134,7 @@ export default class vec2 {
/**
* Alias of {@link vec2.multiply}.
*/
public static mul<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
public static mul<Out extends vec2> (out: Out, a: vec2, b: vec2) {
return vec2.multiply(out, a, b);
}
@@ -143,7 +148,7 @@ export default class vec2 {
* @param b - The second operand.
* @return out.
*/
public static divide<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
public static divide<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = a.x / b.x;
out.y = a.y / b.y;
return out;
@@ -152,7 +157,7 @@ export default class vec2 {
/**
* Alias of {@link vec2.divide}.
*/
public static div<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
public static div<Out extends vec2> (out: Out, a: vec2, b: vec2) {
return vec2.divide(out, a, b);
}
@@ -165,7 +170,7 @@ export default class vec2 {
* @param a - Vector to perform operation.
* @return out.
*/
public static ceil<Out extends IVec2Like> (out: Out, a: vec2) {
public static ceil<Out extends vec2> (out: Out, a: vec2) {
out.x = Math.ceil(a.x);
out.y = Math.ceil(a.y);
return out;
@@ -180,7 +185,7 @@ export default class vec2 {
* @param a - Vector to perform operation.
* @return out.
*/
public static floor<Out extends IVec2Like> (out: Out, a: vec2) {
public static floor<Out extends vec2> (out: Out, a: vec2) {
out.x = Math.floor(a.x);
out.y = Math.floor(a.y);
return out;
@@ -196,7 +201,7 @@ export default class vec2 {
* @param b - The second operand.
* @return out.
*/
public static min<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
public static min<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = Math.min(a.x, b.x);
out.y = Math.min(a.y, b.y);
return out;
@@ -212,7 +217,7 @@ export default class vec2 {
* @param b - The second operand.
* @return out.
*/
public static max<Out extends IVec2Like> (out: Out, a: vec2, b: vec2) {
public static max<Out extends vec2> (out: Out, a: vec2, b: vec2) {
out.x = Math.max(a.x, b.x);
out.y = Math.max(a.y, b.y);
return out;
@@ -227,7 +232,7 @@ export default class vec2 {
* @param a - Vector to perform operation.
* @return out.
*/
public static round<Out extends IVec2Like> (out: Out, a: vec2) {
public static round<Out extends vec2> (out: Out, a: vec2) {
out.x = Math.round(a.x);
out.y = Math.round(a.y);
return out;
@@ -241,7 +246,7 @@ export default class vec2 {
* @param b - The scale number.
* @return out.
*/
public static scale<Out extends IVec2Like> (out: Out, a: vec2, b: number) {
public static scale<Out extends vec2> (out: Out, a: vec2, b: number) {
out.x = a.x * b;
out.y = a.y * b;
return out;
@@ -256,7 +261,7 @@ export default class vec2 {
* @param scale - The scale number before adding.
* @return out.
*/
public static scaleAndAdd<Out extends IVec2Like> (out: Out, a: vec2, b: vec2, scale: number) {
public static scaleAndAdd<Out extends vec2> (out: Out, a: vec2, b: vec2, scale: number) {
out.x = a.x + (b.x * scale);
out.y = a.y + (b.y * scale);
return out;
@@ -345,7 +350,7 @@ export default class vec2 {
* @param a - Vector to negate.
* @return out.
*/
public static negate<Out extends IVec2Like> (out: Out, a: vec2) {
public static negate<Out extends vec2> (out: Out, a: vec2) {
out.x = -a.x;
out.y = -a.y;
return out;
@@ -358,7 +363,7 @@ export default class vec2 {
* @param a - Vector to invert.
* @return out.
*/
public static inverse<Out extends IVec2Like> (out: Out, a: vec2) {
public static inverse<Out extends vec2> (out: Out, a: vec2) {
out.x = 1.0 / a.x;
out.y = 1.0 / a.y;
return out;
@@ -371,7 +376,7 @@ export default class vec2 {
* @param a - Vector to invert.
* @return out.
*/
public static inverseSafe<Out extends IVec2Like> (out: Out, a: vec2) {
public static inverseSafe<Out extends vec2> (out: Out, a: vec2) {
const { x, y } = a;
if (Math.abs(x) < EPSILON) {
@@ -396,7 +401,7 @@ export default class vec2 {
* @param a - Vector to normalize.
* @return out.
*/
public static normalize<Out extends IVec2Like> (out: Out, a: vec2) {
public static normalize<Out extends vec2> (out: Out, a: vec2) {
const { x, y } = a;
let len = x * x + y * y;
if (len > 0) {
@@ -428,7 +433,7 @@ export default class vec2 {
* @param b - The second operand.
* @return out.
*/
public static cross<Out extends IVec3Like> (out: Out, a: vec2, b: vec2) {
public static cross<Out extends vec3> (out: Out, a: vec2, b: vec2) {
const z = a.x * b.y - a.y * b.x;
out.x = out.y = 0;
out.z = z;
@@ -444,7 +449,7 @@ export default class vec2 {
* @param t - The interpolation coefficient.
* @return out.
*/
public static lerp<Out extends IVec2Like> (out: Out, a: vec2, b: vec2, t: number) {
public static lerp<Out extends vec2> (out: Out, a: vec2, b: vec2, t: number) {
const { x: ax, y: ay } = a;
out.x = ax + t * (b.x - ax);
out.y = ay + t * (b.y - ay);
@@ -458,7 +463,7 @@ export default class vec2 {
* @param [scale] Length of the resulting vector. If ommitted, a unit length vector will be returned.
* @return out.
*/
public static random<Out extends IVec2Like> (out: Out, scale: number) {
public static random<Out extends vec2> (out: Out, scale: number) {
scale = scale || 1.0;
const r = random() * 2.0 * Math.PI;
out.x = Math.cos(r) * scale;
@@ -474,7 +479,7 @@ export default class vec2 {
* @param m - The matrix.
* @return out.
*/
public static transformMat2<Out extends IVec2Like> (out: Out, a: vec2, m: IMat2Like) {
public static transformMat2<Out extends vec2> (out: Out, a: vec2, m: mat2) {
const { x, y } = a;
out.x = m.m00 * x + m.m02 * y;
out.y = m.m01 * x + m.m03 * y;
@@ -489,7 +494,7 @@ export default class vec2 {
* @param m - The matrix.
* @return out.
*/
public static transformMat23<Out extends IVec2Like> (out: Out, a: vec2, m: IMat23Like) {
public static transformMat23<Out extends vec2> (out: Out, a: vec2, m: mat23) {
const { x, y } = a;
out.x = m.m00 * x + m.m02 * y + m.m04;
out.y = m.m01 * x + m.m03 * y + m.m05;
@@ -504,7 +509,7 @@ export default class vec2 {
* @param m - The matrix.
* @return out.
*/
public static transformMat3<Out extends IVec2Like> (out: Out, a: vec2, m: IMat3Like) {
public static transformMat3<Out extends vec2> (out: Out, a: vec2, m: mat3) {
const { x, y } = a;
out.x = m.m00 * x + m.m03 * y + m.m06;
out.y = m.m01 * x + m.m04 * y + m.m07;
@@ -521,7 +526,7 @@ export default class vec2 {
* @param m - The matrix.
* @return out.
*/
public static transformMat4<Out extends IVec2Like> (out: Out, a: vec2, m: IMat4Like) {
public static transformMat4<Out extends vec2> (out: Out, a: vec2, m: mat4) {
const { x, y } = a;
out.x = m.m00 * x + m.m04 * y + m.m12;
out.y = m.m01 * x + m.m05 * y + m.m13;
+40 -37
View File
@@ -1,5 +1,7 @@
/// <reference path="./fwd-decls" />
import { IWritableArrayLike } from './fwd-decls';
import mat3 from './mat3';
import mat4 from './mat4';
import quat from './quat';
import { EPSILON, random } from './utils';
/**
@@ -7,6 +9,7 @@ import { EPSILON, random } from './utils';
*
* x, y, z is alias of the first, second, third component of vector, respectively.
*/
// tslint:disable-next-line:class-name
export default class vec3 {
public static UNIT_X = new vec3(1, 0, 0);
public static UNIT_Y = new vec3(0, 1, 0);
@@ -32,7 +35,7 @@ export default class vec3 {
*
* @return The newly created vector.
*/
public static zero<Out extends IVec3Like> (out: Out) {
public static zero<Out extends vec3> (out: Out) {
out.x = 0;
out.y = 0;
out.z = 0;
@@ -56,7 +59,7 @@ export default class vec3 {
* @param a - The specified vector.
* @return out.
*/
public static copy<Out extends IVec3Like> (out: Out, a: vec3) {
public static copy<Out extends vec3> (out: Out, a: vec3) {
out.x = a.x;
out.y = a.y;
out.z = a.z;
@@ -72,7 +75,7 @@ export default class vec3 {
* @param z - Value set to z component.
* @return out.
*/
public static set<Out extends IVec3Like> (out: Out, x: number, y: number, z: number) {
public static set<Out extends vec3> (out: Out, x: number, y: number, z: number) {
out.x = x;
out.y = y;
out.z = z;
@@ -89,7 +92,7 @@ export default class vec3 {
* @param b - The second operand.
* @return out.
*/
public static add<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
public static add<Out extends vec3> (out: Out, a: vec3, b: vec3) {
out.x = a.x + b.x;
out.y = a.y + b.y;
out.z = a.z + b.z;
@@ -106,7 +109,7 @@ export default class vec3 {
* @param b - The second operand.
* @return out.
*/
public static subtract<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
public static subtract<Out extends vec3> (out: Out, a: vec3, b: vec3) {
out.x = a.x - b.x;
out.y = a.y - b.y;
out.z = a.z - b.z;
@@ -116,7 +119,7 @@ export default class vec3 {
/**
* Alias of {@link vec3.subtract}.
*/
public static sub<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
public static sub<Out extends vec3> (out: Out, a: vec3, b: vec3) {
return vec3.subtract(out, a, b);
}
@@ -130,7 +133,7 @@ export default class vec3 {
* @param b - The second operand.
* @return out.
*/
public static multiply<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
public static multiply<Out extends vec3> (out: Out, a: vec3, b: vec3) {
out.x = a.x * b.x;
out.y = a.y * b.y;
out.z = a.z * b.z;
@@ -140,7 +143,7 @@ export default class vec3 {
/**
* Alias of {@link vec3.multiply}.
*/
public static mul<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
public static mul<Out extends vec3> (out: Out, a: vec3, b: vec3) {
return vec3.multiply(out, a, b);
}
@@ -154,7 +157,7 @@ export default class vec3 {
* @param b - The second operand.
* @return out.
*/
public static divide<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
public static divide<Out extends vec3> (out: Out, a: vec3, b: vec3) {
out.x = a.x / b.x;
out.y = a.y / b.y;
out.z = a.z / b.z;
@@ -164,7 +167,7 @@ export default class vec3 {
/**
* Alias of {@link vec3.divide}.
*/
public static div<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
public static div<Out extends vec3> (out: Out, a: vec3, b: vec3) {
return vec3.divide(out, a, b);
}
@@ -177,7 +180,7 @@ export default class vec3 {
* @param a - Vector to perform operation.
* @return out.
*/
public static ceil<Out extends IVec3Like> (out: Out, a: vec3) {
public static ceil<Out extends vec3> (out: Out, a: vec3) {
out.x = Math.ceil(a.x);
out.y = Math.ceil(a.y);
out.z = Math.ceil(a.z);
@@ -193,7 +196,7 @@ export default class vec3 {
* @param a - Vector to perform operation.
* @return out.
*/
public static floor<Out extends IVec3Like> (out: Out, a: vec3) {
public static floor<Out extends vec3> (out: Out, a: vec3) {
out.x = Math.floor(a.x);
out.y = Math.floor(a.y);
out.z = Math.floor(a.z);
@@ -210,7 +213,7 @@ export default class vec3 {
* @param b - The second operand.
* @return out.
*/
public static min<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
public static min<Out extends vec3> (out: Out, a: vec3, b: vec3) {
out.x = Math.min(a.x, b.x);
out.y = Math.min(a.y, b.y);
out.z = Math.min(a.z, b.z);
@@ -227,7 +230,7 @@ export default class vec3 {
* @param b - The second operand.
* @return out.
*/
public static max<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
public static max<Out extends vec3> (out: Out, a: vec3, b: vec3) {
out.x = Math.max(a.x, b.x);
out.y = Math.max(a.y, b.y);
out.z = Math.max(a.z, b.z);
@@ -243,7 +246,7 @@ export default class vec3 {
* @param a - Vector to perform operation.
* @return out.
*/
public static round<Out extends IVec3Like> (out: Out, a: vec3) {
public static round<Out extends vec3> (out: Out, a: vec3) {
out.x = Math.round(a.x);
out.y = Math.round(a.y);
out.z = Math.round(a.z);
@@ -258,7 +261,7 @@ export default class vec3 {
* @param b - The scale number.
* @return out.
*/
public static scale<Out extends IVec3Like> (out: Out, a: vec3, b: number) {
public static scale<Out extends vec3> (out: Out, a: vec3, b: number) {
out.x = a.x * b;
out.y = a.y * b;
out.z = a.z * b;
@@ -274,7 +277,7 @@ export default class vec3 {
* @param scale - The scale number before adding.
* @return out.
*/
public static scaleAndAdd<Out extends IVec3Like> (out: Out, a: vec3, b: vec3, scale: number) {
public static scaleAndAdd<Out extends vec3> (out: Out, a: vec3, b: vec3, scale: number) {
out.x = a.x + b.x * scale;
out.y = a.y + b.y * scale;
out.z = a.z + b.z * scale;
@@ -366,7 +369,7 @@ export default class vec3 {
* @param a - Vector to negate.
* @return out.
*/
public static negate<Out extends IVec3Like> (out: Out, a: vec3) {
public static negate<Out extends vec3> (out: Out, a: vec3) {
out.x = -a.x;
out.y = -a.y;
out.z = -a.z;
@@ -380,7 +383,7 @@ export default class vec3 {
* @param a - Vector to invert.
* @return out.
*/
public static inverse<Out extends IVec3Like> (out: Out, a: vec3) {
public static inverse<Out extends vec3> (out: Out, a: vec3) {
out.x = 1.0 / a.x;
out.y = 1.0 / a.y;
out.z = 1.0 / a.z;
@@ -394,7 +397,7 @@ export default class vec3 {
* @param a - Vector to invert.
* @return out.
*/
public static inverseSafe<Out extends IVec3Like> (out: Out, a: vec3) {
public static inverseSafe<Out extends vec3> (out: Out, a: vec3) {
const { x, y, z } = a;
if (Math.abs(x) < EPSILON) {
@@ -425,7 +428,7 @@ export default class vec3 {
* @param a - Vector to normalize.
* @return out.
*/
public static normalize<Out extends IVec3Like> (out: Out, a: vec3) {
public static normalize<Out extends vec3> (out: Out, a: vec3) {
const { x, y, z } = a;
let len = x * x + y * y + z * z;
@@ -458,7 +461,7 @@ export default class vec3 {
* @param b - The second operand.
* @return out.
*/
public static cross<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
public static cross<Out extends vec3> (out: Out, a: vec3, b: vec3) {
const { x: ax, y: ay, z: az } = a;
const { x: bx, y: by, z: bz } = b;
@@ -477,7 +480,7 @@ export default class vec3 {
* @param t - The interpolation coefficient.
* @return out.
*/
public static lerp<Out extends IVec3Like> (out: Out, a: vec3, b: vec3, t: number) {
public static lerp<Out extends vec3> (out: Out, a: vec3, b: vec3, t: number) {
const { x: ax, y: ay, z: az } = a;
out.x = ax + t * (b.x - ax);
out.y = ay + t * (b.y - ay);
@@ -496,7 +499,7 @@ export default class vec3 {
* @param t - The interpolation coefficient.
* @return out.
*/
public static hermite<Out extends IVec3Like> (
public static hermite<Out extends vec3> (
out: Out, a: vec3, b: vec3, c: vec3, d: vec3, t: number) {
const factorTimes2 = t * t;
const factor1 = factorTimes2 * (2 * t - 3) + 1;
@@ -522,7 +525,7 @@ export default class vec3 {
* @param t - The interpolation coefficient.
* @return out.
*/
public static bezier<Out extends IVec3Like> (
public static bezier<Out extends vec3> (
out: Out, a: vec3, b: vec3, c: vec3, d: vec3, t: number) {
const inverseFactor = 1 - t;
const inverseFactorTimesTwo = inverseFactor * inverseFactor;
@@ -546,7 +549,7 @@ export default class vec3 {
* @param [scale] Length of the resulting vector. If ommitted, a unit length vector will be returned.
* @return out.
*/
public static random<Out extends IVec3Like> (out: Out, scale: number) {
public static random<Out extends vec3> (out: Out, scale: number) {
scale = scale || 1.0;
const phi = random() * 2.0 * Math.PI;
@@ -567,7 +570,7 @@ export default class vec3 {
* @param {mat4} m - The matrix.
* @return out.
*/
public static transformMat4<Out extends IVec3Like> (out: Out, a: vec3, m: IMat4Like) {
public static transformMat4<Out extends vec3> (out: Out, a: vec3, m: mat4) {
const { x, y, z } = a;
let rhw = m.m03 * x + m.m07 * y + m.m11 * z + m.m15;
rhw = rhw ? 1 / rhw : 1;
@@ -586,7 +589,7 @@ export default class vec3 {
* @param {mat4} m - The matrix.
* @return out.
*/
public static transformMat4Normal<Out extends IVec3Like> (out: Out, a: vec3, m: IMat4Like) {
public static transformMat4Normal<Out extends vec3> (out: Out, a: vec3, m: mat4) {
const { x, y, z } = a;
let rhw = m.m03 * x + m.m07 * y + m.m11 * z;
rhw = rhw ? 1 / rhw : 1;
@@ -604,7 +607,7 @@ export default class vec3 {
* @param {mat3} m - The matrix.
* @return out.
*/
public static transformMat3<Out extends IVec3Like> (out: Out, a: vec3, m: IMat3Like) {
public static transformMat3<Out extends vec3> (out: Out, a: vec3, m: mat3) {
const { x, y, z } = a;
out.x = x * m.m00 + y * m.m03 + z * m.m06;
out.y = x * m.m01 + y * m.m04 + z * m.m07;
@@ -620,7 +623,7 @@ export default class vec3 {
* @param {quat} q - The quaternion.
* @return out.
*/
public static transformQuat<Out extends IVec3Like> (out: Out, a: vec3, q: IQuatLike) {
public static transformQuat<Out extends vec3> (out: Out, a: vec3, q: quat) {
// benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
const { x, y, z } = a;
@@ -647,7 +650,7 @@ export default class vec3 {
* @param c - The angle of rotation.
* @return out.
*/
public static rotateX<Out extends IVec3Like> (out: Out, a: vec3, b: vec3, c: number) {
public static rotateX<Out extends vec3> (out: Out, a: vec3, b: vec3, c: number) {
// Translate point to the origin
const px = a.x - b.x;
const py = a.y - b.y;
@@ -674,7 +677,7 @@ export default class vec3 {
* @param c - The angle of rotation.
* @return out.
*/
public static rotateY<Out extends IVec3Like> (out: Out, a: vec3, b: vec3, c: number) {
public static rotateY<Out extends vec3> (out: Out, a: vec3, b: vec3, c: number) {
// Translate point to the origin
const px = a.x - b.x;
const py = a.y - b.y;
@@ -701,7 +704,7 @@ export default class vec3 {
* @param c - The angle of rotation.
* @return out.
*/
public static rotateZ<Out extends IVec3Like> (out: Out, a: vec3, b: vec3, c: number) {
public static rotateZ<Out extends vec3> (out: Out, a: vec3, b: vec3, c: number) {
// Translate point to the origin
const px = a.x - b.x;
const py = a.y - b.y;
@@ -801,7 +804,7 @@ export default class vec3 {
* @param a The vector.
* @param n The plane's normal.
*/
public static projectOnPlane<Out extends IVec3Like> (out: Out, a: vec3, n: vec3) {
public static projectOnPlane<Out extends vec3> (out: Out, a: vec3, n: vec3) {
return vec3.sub(out, a, vec3.project(out, a, n));
}
@@ -811,7 +814,7 @@ export default class vec3 {
* @param a The vector to project.
* @param b The vector onto which the projection performs.
*/
public static project<Out extends IVec3Like> (out: Out, a: vec3, b: vec3) {
public static project<Out extends vec3> (out: Out, a: vec3, b: vec3) {
const sqrLen = vec3.squaredMagnitude(b);
if (sqrLen < 0.000001) {
return vec3.set(out, 0, 0, 0);
+28 -26
View File
@@ -1,5 +1,6 @@
/// <reference path="./fwd-decls" />
import { IWritableArrayLike } from './fwd-decls';
import mat4 from './mat4';
import quat from './quat';
import { EPSILON, random } from './utils';
/**
@@ -7,6 +8,7 @@ import { EPSILON, random } from './utils';
*
* x, y, z, w is alias of the first, second, third, fourth component of vector, respectively.
*/
// tslint:disable-next-line:class-name
export default class vec4 {
public static ZERO = new vec4(0, 0, 0, 0);
public static ONE = new vec4(1, 1, 1, 1);
@@ -55,7 +57,7 @@ export default class vec4 {
* @param a - The specified vector.
* @return out.
*/
public static copy<Out extends IVec4Like> (out: Out, a: vec4) {
public static copy<Out extends vec4> (out: Out, a: vec4) {
out.x = a.x;
out.y = a.y;
out.z = a.z;
@@ -73,7 +75,7 @@ export default class vec4 {
* @param w - Value set to w component.
* @return out.
*/
public static set<Out extends IVec4Like> (out: Out, x: number, y: number, z: number, w: number) {
public static set<Out extends vec4> (out: Out, x: number, y: number, z: number, w: number) {
out.x = x;
out.y = y;
out.z = z;
@@ -91,7 +93,7 @@ export default class vec4 {
* @param b - The second operand.
* @return out.
*/
public static add<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
public static add<Out extends vec4> (out: Out, a: vec4, b: vec4) {
out.x = a.x + b.x;
out.y = a.y + b.y;
out.z = a.z + b.z;
@@ -109,7 +111,7 @@ export default class vec4 {
* @param b - The second operand.
* @return out.
*/
public static subtract<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
public static subtract<Out extends vec4> (out: Out, a: vec4, b: vec4) {
out.x = a.x - b.x;
out.y = a.y - b.y;
out.z = a.z - b.z;
@@ -120,7 +122,7 @@ export default class vec4 {
/**
* Alias of {@link vec4.subtract}.
*/
public static sub<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
public static sub<Out extends vec4> (out: Out, a: vec4, b: vec4) {
return vec4.subtract(out, a, b);
}
@@ -134,7 +136,7 @@ export default class vec4 {
* @param b - The second operand.
* @return out.
*/
public static multiply<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
public static multiply<Out extends vec4> (out: Out, a: vec4, b: vec4) {
out.x = a.x * b.x;
out.y = a.y * b.y;
out.z = a.z * b.z;
@@ -145,7 +147,7 @@ export default class vec4 {
/**
* Alias of {@link vec4.multiply}.
*/
public static mul<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
public static mul<Out extends vec4> (out: Out, a: vec4, b: vec4) {
return vec4.multiply(out, a, b);
}
@@ -159,7 +161,7 @@ export default class vec4 {
* @param b - The second operand.
* @return out.
*/
public static divide<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
public static divide<Out extends vec4> (out: Out, a: vec4, b: vec4) {
out.x = a.x / b.x;
out.y = a.y / b.y;
out.z = a.z / b.z;
@@ -170,7 +172,7 @@ export default class vec4 {
/**
* Alias of {@link vec4.divide}.
*/
public static div<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
public static div<Out extends vec4> (out: Out, a: vec4, b: vec4) {
return vec4.divide(out, a, b);
}
@@ -183,7 +185,7 @@ export default class vec4 {
* @param a - Vector to perform operation.
* @return out.
*/
public static ceil<Out extends IVec4Like> (out: Out, a: vec4) {
public static ceil<Out extends vec4> (out: Out, a: vec4) {
out.x = Math.ceil(a.x);
out.y = Math.ceil(a.y);
out.z = Math.ceil(a.z);
@@ -200,7 +202,7 @@ export default class vec4 {
* @param a - Vector to perform operation.
* @return out.
*/
public static floor<Out extends IVec4Like> (out: Out, a: vec4) {
public static floor<Out extends vec4> (out: Out, a: vec4) {
out.x = Math.floor(a.x);
out.y = Math.floor(a.y);
out.z = Math.floor(a.z);
@@ -218,7 +220,7 @@ export default class vec4 {
* @param b - The second operand.
* @return out.
*/
public static min<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
public static min<Out extends vec4> (out: Out, a: vec4, b: vec4) {
out.x = Math.min(a.x, b.x);
out.y = Math.min(a.y, b.y);
out.z = Math.min(a.z, b.z);
@@ -236,7 +238,7 @@ export default class vec4 {
* @param b - The second operand.
* @return out.
*/
public static max<Out extends IVec4Like> (out: Out, a: vec4, b: vec4) {
public static max<Out extends vec4> (out: Out, a: vec4, b: vec4) {
out.x = Math.max(a.x, b.x);
out.y = Math.max(a.y, b.y);
out.z = Math.max(a.z, b.z);
@@ -253,7 +255,7 @@ export default class vec4 {
* @param a - Vector to perform operation.
* @return out.
*/
public static round<Out extends IVec4Like> (out: Out, a: vec4) {
public static round<Out extends vec4> (out: Out, a: vec4) {
out.x = Math.round(a.x);
out.y = Math.round(a.y);
out.z = Math.round(a.z);
@@ -269,7 +271,7 @@ export default class vec4 {
* @param b - The scale number.
* @return out.
*/
public static scale<Out extends IVec4Like> (out: Out, a: vec4, b: number) {
public static scale<Out extends vec4> (out: Out, a: vec4, b: number) {
out.x = a.x * b;
out.y = a.y * b;
out.z = a.z * b;
@@ -286,7 +288,7 @@ export default class vec4 {
* @param scale - The scale number before adding.
* @return out.
*/
public static scaleAndAdd<Out extends IVec4Like> (out: Out, a: vec4, b: vec4, scale: number) {
public static scaleAndAdd<Out extends vec4> (out: Out, a: vec4, b: vec4, scale: number) {
out.x = a.x + (b.x * scale);
out.y = a.y + (b.y * scale);
out.z = a.z + (b.z * scale);
@@ -381,7 +383,7 @@ export default class vec4 {
* @param a - Vector to negate.
* @return out.
*/
public static negate<Out extends IVec4Like> (out: Out, a: vec4) {
public static negate<Out extends vec4> (out: Out, a: vec4) {
out.x = -a.x;
out.y = -a.y;
out.z = -a.z;
@@ -396,7 +398,7 @@ export default class vec4 {
* @param a - Vector to invert.
* @return out.
*/
public static inverse<Out extends IVec4Like> (out: Out, a: vec4) {
public static inverse<Out extends vec4> (out: Out, a: vec4) {
out.x = 1.0 / a.x;
out.y = 1.0 / a.y;
out.z = 1.0 / a.z;
@@ -411,7 +413,7 @@ export default class vec4 {
* @param a - Vector to invert.
* @return out.
*/
public static inverseSafe<Out extends IVec4Like> (out: Out, a: vec4) {
public static inverseSafe<Out extends vec4> (out: Out, a: vec4) {
const { x, y, z, w } = a;
if (Math.abs(x) < EPSILON) {
@@ -448,7 +450,7 @@ export default class vec4 {
* @param a - Vector to normalize.
* @return out.
*/
public static normalize<Out extends IVec4Like> (out: Out, a: vec4) {
public static normalize<Out extends vec4> (out: Out, a: vec4) {
const { x, y, z, w } = a;
let len = x * x + y * y + z * z + w * w;
if (len > 0) {
@@ -481,7 +483,7 @@ export default class vec4 {
* @param t - The interpolation coefficient.
* @return out.
*/
public static lerp<Out extends IVec4Like> (out: Out, a: vec4, b: vec4, t: number) {
public static lerp<Out extends vec4> (out: Out, a: vec4, b: vec4, t: number) {
const { x: ax, y: ay, z: az, w: aw } = a;
out.x = ax + t * (b.x - ax);
out.y = ay + t * (b.y - ay);
@@ -497,7 +499,7 @@ export default class vec4 {
* @param [scale] Length of the resulting vector. If ommitted, a unit length vector will be returned.
* @return out.
*/
public static random<Out extends IVec4Like> (out: Out, scale: number) {
public static random<Out extends vec4> (out: Out, scale: number) {
scale = scale || 1.0;
const phi = random() * 2.0 * Math.PI;
@@ -518,7 +520,7 @@ export default class vec4 {
* @param m - The matrix.
* @return out.
*/
public static transformMat4<Out extends IVec4Like> (out: Out, a: vec4, m: IMat4Like) {
public static transformMat4<Out extends vec4> (out: Out, a: vec4, m: mat4) {
const { x, y, z, w } = a;
out.x = m.m00 * x + m.m04 * y + m.m08 * z + m.m12 * w;
out.y = m.m01 * x + m.m05 * y + m.m09 * z + m.m13 * w;
@@ -535,7 +537,7 @@ export default class vec4 {
* @param q - The quaternion.
* @return out.
*/
public static transformQuat<Out extends IVec4Like> (out: Out, a: vec4, q: IQuatLike) {
public static transformQuat<Out extends vec4> (out: Out, a: vec4, q: quat) {
const { x, y, z } = a;
const { x: qx, y: qy, z: qz, w: qw } = q;
+18 -2
View File
@@ -536,12 +536,16 @@ class Node extends BaseNode {
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
}
public getWorldPosition<Out extends vec3 = Vec3> (out: Out): Out;
public getWorldPosition (): Vec3;
/**
* get world position
* @param out - the receiving vector
* @returns the resulting vector
*/
public getWorldPosition (out?: Vec3): Vec3 {
public getWorldPosition<Out extends vec3 = Vec3> (out?: Out) {
this.updateWorldTransform();
if (out) {
return vec3.copy(out, this._pos);
@@ -611,6 +615,10 @@ class Node extends BaseNode {
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
}
public getWorldRotation<Out extends quat = Quat> (out: Out): Out;
public getWorldRotation (): Quat;
/**
* get world rotation
* @param out - the receiving quaternion
@@ -662,6 +670,10 @@ class Node extends BaseNode {
this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART);
}
public getWorldScale<Out extends vec3 = Vec3> (out: Out): Out;
public getWorldScale (): Vec3;
/**
* get world scale
* @param out - the receiving vector
@@ -676,12 +688,16 @@ class Node extends BaseNode {
}
}
public getWorldMatrix<Out extends mat4> (out?: Out): Out;
public getWorldMatrix (): Mat4;
/**
* get the matrix that transforms a point from local space into world space
* @param out - the receiving matrix
* @returns the - resulting matrix
*/
public getWorldMatrix (out?: Mat4): Mat4 {
public getWorldMatrix (out?: Mat4) {
this.updateWorldTransformFull();
if (out) {
return mat4.copy(out, this._mat);