Skip to content

Commit

Permalink
Add transform
Browse files Browse the repository at this point in the history
  • Loading branch information
G43riko committed Aug 19, 2021
1 parent fd43d50 commit 2bda0a5
Show file tree
Hide file tree
Showing 20 changed files with 877 additions and 43 deletions.
10 changes: 6 additions & 4 deletions src/components/g-map.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
export class GMap<T, S> extends Map<T, S> {
export class GMap<T, S extends string | number | Record<string | number, unknown>> extends Map<T, S> {
public get(key: T, defaultValue?: S): S | undefined {
return super.get(key) || defaultValue;
}

public getOrCreate(key: T, defaultValue: S): S | undefined {
public getOrCreate(key: T, defaultValue: S | (() => S)): S | undefined {
const result = super.get(key);
if (result) {
return result;
}
this.set(key, defaultValue);

return defaultValue;
const newValue = typeof defaultValue === "function" ? defaultValue() : defaultValue;
this.set(key, newValue);

return newValue;
}
}
2 changes: 2 additions & 0 deletions src/enums/keys.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum Keys {
DELETE = "Delete",
CONTROL = "ControlLeft",
SHIFT = "ShiftLeft",
TAB = "Tab",
PAGE_UP = "PageUp",
PAGE_DOWN = "PageDown",
ESCAPE = "Escape",
Expand All @@ -25,6 +26,7 @@ export enum Keys {
C = "KeyC",
O = "KeyO",
H = "KeyH",
Q = "KeyQ",
L = "KeyL",

DIGIT_1 = "Digit1",
Expand Down
32 changes: 31 additions & 1 deletion src/math/mat4.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// /* eslint-disable */
/**
* https://github.com/mrdoob/three.js/blob/dev/src/math/Matrix4.js
* https://github.com/BennyQBD/3DEngineCpp/blob/master/src/core/math3d.h
* https://glmatrix.net/docs/mat4.js.html
* https://github.com/fynnfluegge/oreon-engine/blob/master/oreonengine/oe-core/src/main/java/org/oreon/core/math/Matrix4f.java
*/
import { Quaternion } from "./quaternion";
import { SimpleVector3 } from "./simple-vector3";
import { ReadonlySimpleVector3, SimpleVector3 } from "./simple-vector3";
import { Vector3 } from "./vector3";

/**
Expand Down Expand Up @@ -273,6 +274,35 @@ export class SimpleMat4 {

return result;
}

public transformVector(a: SimpleVector3): SimpleVector3 {
const m = this.data;
const x = a.x;
const y = a.y;
const z = a.z;
let w = m[3] * x + m[7] * y + m[11] * z + m[15];
w = w || 1;
a.x = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
a.y = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
a.z = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;

return a;
}

public getTransformedVector(a: ReadonlySimpleVector3): SimpleVector3 {
const m = this.data;
const x = a.x;
const y = a.y;
const z = a.z;
let w = m[3] * x + m[7] * y + m[11] * z + m[15];
w = w || 1;

return {
x: (m[0] * x + m[4] * y + m[8] * z + m[12]) / w,
y: (m[1] * x + m[5] * y + m[9] * z + m[13]) / w,
z: (m[2] * x + m[6] * y + m[10] * z + m[14]) / w,
};
}
}

export class Mat4 extends SimpleMat4 {
Expand Down
7 changes: 7 additions & 0 deletions src/math/simple-vector2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ export interface SimpleVector2 {
}

export type ReadonlySimpleVector2 = Readonly<SimpleVector2>;

export const SimpleVector2 = Object.freeze({
UP : Object.freeze({x: 0, y: 1}) as ReadonlySimpleVector2,
RIGHT: Object.freeze({x: 1, y: 0}) as ReadonlySimpleVector2,
ONE : Object.freeze({x: 1, y: 1}) as ReadonlySimpleVector2,
ZERO : Object.freeze({x: 0, y: 0}) as ReadonlySimpleVector2,
});
7 changes: 7 additions & 0 deletions src/math/simple-vector3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ export interface SimpleVector3 extends SimpleVector2 {
}

export type ReadonlySimpleVector3 = Readonly<SimpleVector3>;

export const SimpleVector3 = Object.freeze({
UP : Object.freeze({x: 0, y: 1, z: 0}) as ReadonlySimpleVector3,
RIGHT: Object.freeze({x: 1, y: 1, z: 0}) as ReadonlySimpleVector3,
ONE : Object.freeze({x: 1, y: 1, z: 1}) as ReadonlySimpleVector3,
ZERO : Object.freeze({x: 0, y: 0, z: 0}) as ReadonlySimpleVector3,
});
46 changes: 24 additions & 22 deletions src/math/vector2.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Range } from "../models/range";
import { SimpleVector2 } from "./simple-vector2";
import { ReadonlySimpleVector2, SimpleVector2 } from "./simple-vector2";

export class Vector2 implements SimpleVector2 {
public constructor(public x = 0,
public y = 0) {
public constructor(
public x = 0,
public y = 0,
) {
}

public static get ZERO(): Vector2 {
Expand Down Expand Up @@ -46,29 +48,29 @@ export class Vector2 implements SimpleVector2 {
return Vector2.size(this);
}

public static equals(vecA: SimpleVector2, vecB: SimpleVector2): boolean {
public static equals(vecA: ReadonlySimpleVector2, vecB: ReadonlySimpleVector2): boolean {
if (vecA === vecB) {
return true;
}

return vecA.x === vecB.x && vecA.y === vecB.y;
}

public static sub(vecA: SimpleVector2, vecB: SimpleVector2, result = new Vector2()): Vector2 {
public static sub(vecA: ReadonlySimpleVector2, vecB: ReadonlySimpleVector2, result = new Vector2()): Vector2 {
return result.setData(vecA.x - vecB.x, vecA.y - vecB.y);
}

public static dot(vecA: SimpleVector2, vecB: SimpleVector2): number {
public static dot(vecA: ReadonlySimpleVector2, vecB: ReadonlySimpleVector2): number {
return vecA.x * vecB.x + vecA.y * vecB.y;
}

public static lerp<T extends SimpleVector2>(start: SimpleVector2, end: SimpleVector2, ratio: number): SimpleVector2 {
public static lerp(start: ReadonlySimpleVector2, end: ReadonlySimpleVector2, ratio: number): Vector2 {
const dir = Vector2.sub(end, start);

return Vector2.mulNum(dir, ratio, dir).add(start);
}

public static getAbs(vec: SimpleVector2, result = new Vector2()): Vector2 {
public static getAbs(vec: ReadonlySimpleVector2, result = new Vector2()): Vector2 {
return result.setData(Math.abs(vec.x), Math.abs(vec.y));
}

Expand All @@ -83,7 +85,7 @@ export class Vector2 implements SimpleVector2 {
) <= cutOff;
}

public static createOutlineRange(points: readonly SimpleVector2[]): Range<SimpleVector2> {
public static createOutlineRange(points: readonly ReadonlySimpleVector2[]): Range<SimpleVector2> {
const min = {
x: Infinity,
y: Infinity,
Expand Down Expand Up @@ -125,27 +127,27 @@ export class Vector2 implements SimpleVector2 {
return item && !isNaN(item.x) && !isNaN(item.y);
}

public static sum(vecA: SimpleVector2, vecB: SimpleVector2, result = new Vector2()): Vector2 {
public static sum(vecA: ReadonlySimpleVector2, vecB: ReadonlySimpleVector2, result = new Vector2()): Vector2 {
return result.setData(vecA.x + vecB.x, vecA.y + vecB.y);
}

public static min(vecA: SimpleVector2, vecB: SimpleVector2, result = new Vector2()): Vector2 {
public static min(vecA: ReadonlySimpleVector2, vecB: ReadonlySimpleVector2, result = new Vector2()): Vector2 {
return result.setData(Math.min(vecA.x, vecB.x), Math.min(vecA.y, vecB.y));
}

public static max(vecA: SimpleVector2, vecB: SimpleVector2, result = new Vector2()): Vector2 {
public static max(vecA: ReadonlySimpleVector2, vecB: ReadonlySimpleVector2, result = new Vector2()): Vector2 {
return result.setData(Math.max(vecA.x, vecB.x), Math.max(vecA.y, vecB.y));
}

public static dist(vecA: SimpleVector2, vecB: SimpleVector2): number {
public static dist(vecA: ReadonlySimpleVector2, vecB: ReadonlySimpleVector2): number {
return Math.sqrt(Math.pow(vecA.x - vecB.x, 2) + Math.pow(vecA.y - vecB.y, 2));
}

public static size(vec: SimpleVector2): number {
public static size(vec: ReadonlySimpleVector2): number {
return Math.sqrt(vec.x * vec.x + vec.y * vec.y);
}

public static fromVec(vec: SimpleVector2): Vector2 {
public static fromVec(vec: ReadonlySimpleVector2): Vector2 {
return new Vector2(vec.x, vec.y);
}

Expand Down Expand Up @@ -178,15 +180,15 @@ export class Vector2 implements SimpleVector2 {
return result;
}

public static mulNum(vecA: SimpleVector2, val: number, result = new Vector2()): Vector2 {
public static mulNum(vecA: ReadonlySimpleVector2, val: number, result = new Vector2()): Vector2 {
return result.setData(vecA.x * val, vecA.y * val);
}

public static addNum(vecA: SimpleVector2, val: number, result = new Vector2()): Vector2 {
public static addNum(vecA: ReadonlySimpleVector2, val: number, result = new Vector2()): Vector2 {
return result.setData(vecA.x + val, vecA.y + val);
}

public mul(value: SimpleVector2 | number): this {
public mul(value: ReadonlySimpleVector2 | number): this {
if (typeof value === "number") {
this.x *= value;
this.y *= value;
Expand All @@ -199,7 +201,7 @@ export class Vector2 implements SimpleVector2 {
return this;
}

public add(value: SimpleVector2 | number): this {
public add(value: ReadonlySimpleVector2 | number): this {
if (typeof value === "number") {
this.x += value;
this.y += value;
Expand All @@ -212,7 +214,7 @@ export class Vector2 implements SimpleVector2 {
return this;
}

public sub(value: SimpleVector2 | number): this {
public sub(value: ReadonlySimpleVector2 | number): this {
if (typeof value === "number") {
this.x -= value;
this.y -= value;
Expand All @@ -225,7 +227,7 @@ export class Vector2 implements SimpleVector2 {
return this;
}

public div(value: SimpleVector2 | number): this {
public div(value: ReadonlySimpleVector2 | number): this {
if (typeof value === "number") {
this.x /= value;
this.y /= value;
Expand All @@ -245,7 +247,7 @@ export class Vector2 implements SimpleVector2 {
return this;
}

public set(vec: SimpleVector2): this {
public set(vec: ReadonlySimpleVector2): this {
this.x = vec.x;
this.y = vec.y;

Expand Down
8 changes: 5 additions & 3 deletions src/math/vector3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { ReadonlySimpleVector3, SimpleVector3 } from "./simple-vector3";
import { Vector2 } from "./vector2";

export class Vector3 implements SimpleVector3 {
public constructor(public x = 0,
public y = 0,
public z = 0) {
public constructor(
public x = 0,
public y = 0,
public z = 0,
) {
}

public static get UP(): Vector3 {
Expand Down
28 changes: 15 additions & 13 deletions src/math/vector4.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { SimpleVector4 } from "./simple-vector4";
import { ReadonlySimpleVector4, SimpleVector4 } from "./simple-vector4";

export class Vector4 implements SimpleVector4 {
public constructor(public x = 0,
public y = 0,
public z = 0,
public w = 0) {
public constructor(
public x = 0,
public y = 0,
public z = 0,
public w = 0,
) {
}

public static get ZERO(): Vector4 {
Expand All @@ -31,15 +33,15 @@ export class Vector4 implements SimpleVector4 {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
}

public static equals(vecA: SimpleVector4, vecB: SimpleVector4): boolean {
public static equals(vecA: ReadonlySimpleVector4, vecB: ReadonlySimpleVector4): boolean {
if (vecA === vecB) {
return true;
}

return vecA.x === vecB.x && vecA.y === vecB.y && vecA.z === vecB.z && vecA.w === vecB.w;
}

public static min(vecA: SimpleVector4, vecB: SimpleVector4): Vector4 {
public static min(vecA: ReadonlySimpleVector4, vecB: ReadonlySimpleVector4): Vector4 {
return new Vector4(
Math.min(vecA.x, vecB.x),
Math.min(vecA.y, vecB.y),
Expand All @@ -48,7 +50,7 @@ export class Vector4 implements SimpleVector4 {
);
}

public static max(vecA: SimpleVector4, vecB: SimpleVector4): Vector4 {
public static max(vecA: ReadonlySimpleVector4, vecB: ReadonlySimpleVector4): Vector4 {
return new Vector4(
Math.max(vecA.x, vecB.x),
Math.max(vecA.y, vecB.y),
Expand All @@ -57,7 +59,7 @@ export class Vector4 implements SimpleVector4 {
);
}

public static dist(vecA: SimpleVector4, vecB: SimpleVector4): number {
public static dist(vecA: ReadonlySimpleVector4, vecB: ReadonlySimpleVector4): number {
return Math.sqrt(
Math.pow(vecA.x - vecB.x, 2) +
Math.pow(vecA.y - vecB.y, 2) +
Expand Down Expand Up @@ -102,7 +104,7 @@ export class Vector4 implements SimpleVector4 {
return this;
}

public mul(value: SimpleVector4 | number): this {
public mul(value: ReadonlySimpleVector4 | number): this {
if (typeof value === "number") {
this.x *= value;
this.y *= value;
Expand All @@ -118,7 +120,7 @@ export class Vector4 implements SimpleVector4 {
return this;
}

public add(vec: SimpleVector4): this {
public add(vec: ReadonlySimpleVector4): this {
this.x += vec.x;
this.y += vec.y;
this.z += vec.z;
Expand All @@ -127,7 +129,7 @@ export class Vector4 implements SimpleVector4 {
return this;
}

public sub(vec: SimpleVector4): this {
public sub(vec: ReadonlySimpleVector4): this {
this.x -= vec.x;
this.y -= vec.y;
this.z -= vec.z;
Expand All @@ -145,7 +147,7 @@ export class Vector4 implements SimpleVector4 {
return this;
}

public set(vec: SimpleVector4): this {
public set(vec: ReadonlySimpleVector4): this {
this.x = vec.x;
this.y = vec.y;
this.z = vec.z;
Expand Down
Loading

0 comments on commit 2bda0a5

Please sign in to comment.