Skip to content

Commit

Permalink
fix(math): reusable isNumber check
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Aug 20, 2020
1 parent fb343db commit 54befda
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
16 changes: 9 additions & 7 deletions src/adapter/playcanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import {
operatorCalc, cachedValueOf, defineVectorLength, cachedFactory, cachedFunction, defineMatrixLength
} from '../operator';
import { multiplyMat3Vec, multiplyMat3Mat3, multiplyVecMat3 } from '../utils/math';
import {
multiplyMat3Vec, multiplyMat3Mat3, multiplyVecMat3, isNumber
} from '../utils/math';

function fallbackWindow() {
return {
Expand Down Expand Up @@ -99,13 +101,13 @@ export function hijackPlayCanvas(pc) {
}
});
pc.quat = cachedFunction((x, y, z, w) => {
if (typeof x === 'number') {
if (isNumber(x)) {
return new Quat(x, y, z, w);
}
if (!x) {
return new Quat();
}
if (typeof y === 'number') {
if (isNumber(y)) {
return new Quat().setFromAxisAngle(x, y * RAD_TO_DEG);
}
return new Quat().setFromMat4(new AMat4().setLookAt(ZERO, x, y || UP));
Expand All @@ -122,7 +124,7 @@ export function hijackPlayCanvas(pc) {
};

Quat.prototype.multiply = function (other, y, z, w) {
if (other && typeof other.w === 'number') {
if (other && (isNumber(other.w))) {
return this.multiplyQuaternion(other);
}
return this.multiplyQuaternion(pc.quat(other, y, z, w));
Expand Down Expand Up @@ -175,7 +177,7 @@ export function hijackPlayCanvas(pc) {
});

AMat3.prototype.multiply = function (other) {
if (other && typeof other.w === 'number') {
if (other && (isNumber(other.z))) {
return multiplyMat3Vec(other);
}
return multiplyMat3Mat3(this, other);
Expand Down Expand Up @@ -294,7 +296,7 @@ export function hijackPlayCanvas(pc) {
constructor(...axes) {
super();
const [first] = axes;
if (typeof first === 'number' || (first && first.constructor === Number)) {
if (isNumber(first)) {
for (let i = 0; i < 9; i += 1) {
this.data[i] = first;
}
Expand All @@ -310,7 +312,7 @@ export function hijackPlayCanvas(pc) {
constructor(...axes) {
super();
const [first] = axes;
if (typeof first === 'number' || (first && first.constructor === Number)) {
if (isNumber(first)) {
for (let i = 0; i < 16; i += 1) {
this.data[i] = first;
}
Expand Down
4 changes: 2 additions & 2 deletions src/color.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import { isArray } from './utils/math';
import { isArray, isNumber } from './utils/math';
import {
cachedFunction, cachedMethod, cachedValueOf, defineVectorLength, operatorCalc
} from './operator';
Expand Down Expand Up @@ -28,7 +28,7 @@ class AColor {
});
} else if (isArray(x)) {
this[AXES] = [...x];
} else if (x && typeof x.x === 'number') {
} else if (x && isNumber(x.x)) {
this[AXES] = [x.x || 0, x.y || 0, x.z || 0, x.w || 0];
} else {
this[AXES] = [x || 0, y || 0, z || 0, w || 0];
Expand Down
4 changes: 2 additions & 2 deletions src/degree.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import { normRad } from './utils/math';
import { normRad, isNumber } from './utils/math';
import { convertToCSSVars } from './utils/css';

const ANGLE = Symbol('angle rad');
Expand Down Expand Up @@ -101,5 +101,5 @@ export function idegree(angle) {
}

export function isAngle(angle) {
return typeof angle === 'number' || angle instanceof ADegree;
return isNumber(angle) || angle instanceof ADegree;
}
4 changes: 2 additions & 2 deletions src/point.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import { isArray, normRad } from './utils/math';
import { isArray, normRad, isNumber } from './utils/math';
import {
cachedFunction, cachedGetter, cachedMethod, cachedValueOf, defineVectorLength, operatorCalc
} from './operator';
Expand Down Expand Up @@ -37,7 +37,7 @@ class APoint {
});
} else if (isArray(x)) {
this[AXES] = [...x];
} else if (x && typeof x.x === 'number') {
} else if (x && isNumber(x.x)) {
this[AXES] = [x.x || 0, x.y || 0];
} else {
this[AXES] = [x || 0, y || 0];
Expand Down
20 changes: 16 additions & 4 deletions src/quaternion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import {
FORWARD, LEFT, RIGHT, UP, Vector, Victor
} from './vector';
import { isArray, multQuatVec } from './utils/math';
import { isArray, multQuatVec, isNumber } from './utils/math';
import {
cachedFunction, defineMatrixLength, cachedValueOf, operatorCalc as calc
} from './operator';
Expand Down Expand Up @@ -97,7 +97,7 @@ function axisAngle(axis, angle) {
}

function getQuat(x, y, z, w) {
if (typeof x === 'number') {
if (isNumber(x)) {
return [x, y, z, w];
}
if (isArray(x)) {
Expand All @@ -113,7 +113,7 @@ function getQuat(x, y, z, w) {
}

function from(x, y, z, w) {
if (x && typeof x.w === 'number') {
if (x && isNumber(x.w)) {
return getQuat(x.x, x.y, x.z, x.w);
}
return getQuat(x, y, z, w) || [0, 0, 0, 1];
Expand All @@ -133,7 +133,7 @@ class AQuaternion {
}

multiply(other, y, z, w) {
if (typeof other.w === 'number') {
if (isNumber(other.w)) {
return this.multiplyQuaternion(other);
}
const o = getQuat(other, y, z, w);
Expand Down Expand Up @@ -199,6 +199,18 @@ class AQuaternion {
return this.multiplyVector(UP);
}

get [0]() {
return this.left;
}

get [1]() {
return this.dir;
}

get [2]() {
return this.up;
}

/**
*
* @returns {number}
Expand Down
7 changes: 7 additions & 0 deletions src/utils/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ export function multiplyVecMat3(vecLeft, [column0, column1, column2]) {
vecLeft.dot(column2)
);
}

export function isNumber(nr) {
if (typeof nr === 'number' || (nr && nr.constructor === Number)) {
return true;
}
return false;
}
4 changes: 2 additions & 2 deletions src/vector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-nocheck
import {
acos, isArray, multQuatVec, normRad, multiplyVecMat3
acos, isArray, multQuatVec, normRad, multiplyVecMat3, isNumber
} from './utils/math';
import {
cachedFunction, cachedGetter, cachedMethod, cachedValueOf, defineVectorLength, operatorCalc
Expand Down Expand Up @@ -36,7 +36,7 @@ class AVector {
});
} else if (isArray(x)) {
this[AXES] = [...x];
} else if (x && typeof x.x === 'number') {
} else if (x && isNumber(x.x)) {
this[AXES] = [x.x || 0, x.y || 0, x.z || 0];
} else {
this[AXES] = [x || 0, y || 0, z || 0];
Expand Down

0 comments on commit 54befda

Please sign in to comment.