Skip to content

Commit

Permalink
feat(mat3): mat3 with vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Aug 4, 2020
1 parent 10f23e5 commit 8840843
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import { operatorCalc } from './operator';
import { Vector, Victor } from './vector';
import { Point, IPoint } from './point';
import { Quaternion, IQuaternion } from './quaternion';
import { IMat3 } from './mat3';

export {
Vector, Victor, Victor as IVector, vector, victor, victor as ivector, FORWARD, LEFT, UP
Vector, Victor, Victor as IVector, vector, victor, victor as ivector, FORWARD, LEFT, UP, RIGHT
} from './vector';
export {
Point, IPoint, point, ipoint
} from './point';
export {
Quaternion, IQuaternion, IDENTITY, quaternion, iquaternion
Quaternion, IQuaternion, IDENTITY, quaternion, iquaternion, fromOrientation
} from './quaternion';
export {
Degree, IDegree, degree, idegree
} from './degree';
export {
Color, IColor, color, icolor
} from './color';
export { IMat3 } from './mat3';

/**
* @typedef {Vector & number} IndexVectorType
* @typedef {Victor & number} IndexVictorType
* @typedef {Point & number} IndexPointType
* @typedef {IPoint & number} IndexIPointType
* @typedef {Vector & number} IndexVector
* @typedef {Victor & number} IndexVictor
* @typedef {Point & number} IndexPoint
* @typedef {IPoint & number} IndexIPoint
* @typedef {Quaternion & number} IndexQuaternion
* @typedef {IQuaternion & number} IndexIQuaternion
* @typedef {IMat3 & number} IndexIMat3
*/

/**
* @param {() => number} alg
* @return {IndexVectorType | IndexVictorType | IndexPointType | IndexIPointType | number}
* @return {IndexVector | IndexVictor | IndexPoint | IndexIPoint | IndexQuaternion | IndexIQuaternion | IndexIMat3 | number}
*/
export function calc(alg) {
return operatorCalc(alg);
Expand Down
69 changes: 69 additions & 0 deletions src/mat3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { defineMatrixLength, cachedValueOf } from './operator';

const AXES = Symbol('data');

class AMat3 {
constructor(...columns) {
this[AXES] = columns;
}

get [0]() {
return this[AXES][0];
}

set [0](_) {
throw new Error('set [0] not implemented');
}

get [1]() {
return this[AXES][1];
}

set [1](_) {
throw new Error('set [1] not implemented');
}

get [2]() {
return this[AXES][2];
}

set [2](_) {
throw new Error('set [2] not implemented');
}

multiplyMat(_) {
throw new Error('multiplyMat() not implemented');
}

// https://stackoverflow.com/questions/24593939/matrix-multiplication-with-vector-in-glsl#answer-24594497
multiplyVec({ x, y, z }) {
const [column0, column1, column2] = this[AXES];
return new column0.constructor(
column0.x * x + column1.x * y + column2.x * z,
column0.y * x + column1.y * y + column2.y * z,
column0.z * x + column1.z * y + column2.z * z
);
}

multiply(other) {
if (other instanceof AMat3) {
return this.multiplyMat(other);
}
const { x, y, z } = other;
if (x === undefined || y === undefined || z === undefined) {
throw new Error(`multiply only works with mat3 and vec3, not supported ${other}`);
}
return this.multiplyVec(other);
}

[Symbol.iterator]() {
return this[AXES].values();
}
}

cachedValueOf(AMat3);
defineMatrixLength(AMat3);

export class IMat3 extends AMat3 {

}
1 change: 1 addition & 0 deletions src/utils/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export function isArray(arr) {
return Array.isArray(arr) || ArrayBuffer.isView(arr);
}

// http://schteppe.github.io/cannon.js/docs/files/src_math_Quaternion.js.html
export function multQuatVec(quat, vec) {
const {
x, y, z
Expand Down

0 comments on commit 8840843

Please sign in to comment.