Skip to content

Commit

Permalink
feat: optimize math class size and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jun 21, 2020
1 parent cf08778 commit ca74267
Show file tree
Hide file tree
Showing 20 changed files with 703 additions and 703 deletions.
15 changes: 11 additions & 4 deletions src/examples/gettingstarted/4_lambertCube/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { fetchImage } from "../../../lib/io/loaders/Image";
import { ShaderMaterial } from "../../../lib/materials/ShaderMaterial";
import { Euler } from "../../../lib/math/Euler";
import { Matrix4 } from "../../../lib/math/Matrix4";
import {
makeMatrix4Perspective,
makeMatrix4RotationFromEuler,
makeMatrix4Translation,
} from "../../../lib/math/Matrix4.Functions";
import { Vector3 } from "../../../lib/math/Vector3";
import { BufferGeometry } from "../../../lib/renderers/webgl2/buffers/BufferGeometry";
import { DepthTestFunc, DepthTestState } from "../../../lib/renderers/webgl2/DepthTestState";
Expand All @@ -23,11 +28,10 @@ async function init(): Promise<null> {
document.body.appendChild(canvasFramebuffer.canvas);

const program = new Program(context, material);
const texImage2D = new TexImage2D(context, texture);
const uniforms = {
localToWorld: new Matrix4(),
worldToView: new Matrix4().makeTranslation(new Vector3(0, 0, -1)),
viewToScreen: new Matrix4().makePerspective(-0.25, 0.25, 0.25, -0.25, 0.1, 4.0),
worldToView: makeMatrix4Translation(new Matrix4(), new Vector3(0, 0, -1)),
viewToScreen: makeMatrix4Perspective(new Matrix4(), -0.25, 0.25, 0.25, -0.25, 0.1, 4.0),
viewLightPosition: new Vector3(0, 0, 0),
map: new TexImage2D(context, texture),
};
Expand All @@ -38,7 +42,10 @@ async function init(): Promise<null> {
requestAnimationFrame(animate);

const now = Date.now();
uniforms.localToWorld.makeRotationFromEuler(new Euler(now * 0.001, now * 0.0033, now * 0.00077));
uniforms.localToWorld = makeMatrix4RotationFromEuler(
uniforms.localToWorld,
new Euler(now * 0.001, now * 0.0033, now * 0.00077),
);
canvasFramebuffer.renderBufferGeometry(program, uniforms, bufferGeometry, depthTestState);
}

Expand Down
14 changes: 11 additions & 3 deletions src/examples/gettingstarted/5_reflectiveCube/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { fetchImage } from "../../../lib/io/loaders/Image";
import { ShaderMaterial } from "../../../lib/materials/ShaderMaterial";
import { Euler } from "../../../lib/math/Euler";
import { Matrix4 } from "../../../lib/math/Matrix4";
import {
makeMatrix4Perspective,
makeMatrix4RotationFromEuler,
makeMatrix4Translation,
} from "../../../lib/math/Matrix4.Functions";
import { Vector3 } from "../../../lib/math/Vector3";
import { BufferGeometry } from "../../../lib/renderers/webgl2/buffers/BufferGeometry";
import { DepthTestFunc, DepthTestState } from "../../../lib/renderers/webgl2/DepthTestState";
Expand Down Expand Up @@ -32,8 +37,8 @@ async function init(): Promise<null> {
const program = new Program(context, material);
const uniforms = {
localToWorld: new Matrix4(),
worldToView: new Matrix4().makeTranslation(new Vector3(0, 0, -1)),
viewToScreen: new Matrix4().makePerspective(-0.25, 0.25, 0.25, -0.25, 0.1, 4.0),
worldToView: makeMatrix4Translation(new Matrix4(), new Vector3(0, 0, -1)),
viewToScreen: makeMatrix4Perspective(new Matrix4(), -0.25, 0.25, 0.25, -0.25, 0.1, 4.0),
cubeMap: new TexImage2D(context, cubeTexture),
};
const bufferGeometry = new BufferGeometry(context, geometry);
Expand All @@ -43,7 +48,10 @@ async function init(): Promise<null> {
requestAnimationFrame(animate);

const now = Date.now();
uniforms.localToWorld.makeRotationFromEuler(new Euler(now * 0.0001, now * 0.00033, now * 0.000077));
uniforms.localToWorld = makeMatrix4RotationFromEuler(
uniforms.localToWorld,
new Euler(now * 0.0001, now * 0.00033, now * 0.000077),
);
canvasFramebuffer.renderBufferGeometry(program, uniforms, bufferGeometry, depthTestState);
}

Expand Down
38 changes: 38 additions & 0 deletions src/lib/math/Box2.Functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Box2 } from "./Box2";
import { Vector2 } from "./Vector2";

export function makeBox2FromVector2s(b: Box2, points: Vector2[]): Box2 {
b.makeEmpty();

points.forEach((point) => {
expandBox2ByVector2(b, point);
});

return b;
}

export function expandBox2ByVector2(b: Box2, point: Vector2): Box2 {
b.min.min(point);
b.max.max(point);

return b;
}

export function box2ContainsVector2(b: Box2, point: Vector2): boolean {
return point.x < b.min.x || point.x > b.max.x || point.y < b.min.y || point.y > b.max.y ? false : true;
}

export function box2ContainsBox2(b: Box2, otherBox: Box2): boolean {
return (
b.min.x <= otherBox.min.x && otherBox.max.x <= b.max.x && b.min.y <= otherBox.min.y && otherBox.max.y <= b.max.y
);
}

export function clampVector2ToBox2(b: Box2, point: Vector2): Vector2 {
return new Vector2().copy(point).clamp(b.min, b.max);
}

export function distanceBox2ToVector2(b: Box2, point: Vector2): number {
const clampedPoint = new Vector2().copy(point).clamp(b.min, b.max);
return clampedPoint.sub(point).length();
}
34 changes: 0 additions & 34 deletions src/lib/math/Box2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ export class Box2 implements ICloneable<Box2>, IEquatable<Box2>, IHashable {
return this;
}

setFromPoints(points: Vector2[]): this {
this.makeEmpty();

points.forEach((point) => {
this.expandByPoint(point);
});

return this;
}

clone(): Box2 {
return new Box2().copy(this);
}
Expand All @@ -92,30 +82,6 @@ export class Box2 implements ICloneable<Box2>, IEquatable<Box2>, IHashable {
return this.max.x < this.min.x || this.max.y < this.min.y;
}

expandByPoint(point: Vector2): this {
this.min.min(point);
this.max.max(point);

return this;
}

containsPoint(point: Vector2): boolean {
return point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y ? false : true;
}

containsBox(box: Box2): boolean {
return this.min.x <= box.min.x && box.max.x <= this.max.x && this.min.y <= box.min.y && box.max.y <= this.max.y;
}

clampPoint(point: Vector2): Vector2 {
return new Vector2().copy(point).clamp(this.min, this.max);
}

distanceToPoint(point: Vector2): number {
const clampedPoint = new Vector2().copy(point).clamp(this.min, this.max);
return clampedPoint.sub(point).length();
}

intersect(box: Box2): this {
this.min.max(box.min);
this.max.min(box.max);
Expand Down
112 changes: 112 additions & 0 deletions src/lib/math/Euler.Functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Euler, EulerOrder } from "./Euler";
import { clamp } from "./Functions";
import { Matrix4 } from "./Matrix4";
import { makeMatrix4RotationFromQuaternion } from "./Matrix4.Functions";
import { Quaternion } from "./Quaternion";

export function makeEulerFromRotationMatrix4(e: Euler, m: Matrix4, order: EulerOrder = EulerOrder.Default): Euler {
// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)

const te = m.elements;
const m11 = te[0],
m12 = te[4],
m13 = te[8];
const m21 = te[1],
m22 = te[5],
m23 = te[9];
const m31 = te[2],
m32 = te[6],
m33 = te[10];

let x = 0,
y = 0,
z = 0;

switch (order) {
case EulerOrder.XYZ:
y = Math.asin(clamp(m13, -1, 1));

if (Math.abs(m13) < 0.9999999) {
x = Math.atan2(-m23, m33);
z = Math.atan2(-m12, m11);
} else {
x = Math.atan2(m32, m22);
z = 0;
}

break;

case EulerOrder.YXZ:
x = Math.asin(-clamp(m23, -1, 1));

if (Math.abs(m23) < 0.9999999) {
y = Math.atan2(m13, m33);
z = Math.atan2(m21, m22);
} else {
y = Math.atan2(-m31, m11);
z = 0;
}

break;

case EulerOrder.ZXY:
x = Math.asin(clamp(m32, -1, 1));

if (Math.abs(m32) < 0.9999999) {
y = Math.atan2(-m31, m33);
z = Math.atan2(-m12, m22);
} else {
y = 0;
z = Math.atan2(m21, m11);
}

break;

case EulerOrder.ZYX:
y = Math.asin(-clamp(m31, -1, 1));

if (Math.abs(m31) < 0.9999999) {
x = Math.atan2(m32, m33);
z = Math.atan2(m21, m11);
} else {
x = 0;
z = Math.atan2(-m12, m22);
}

break;

case EulerOrder.YZX:
z = Math.asin(clamp(m21, -1, 1));

if (Math.abs(m21) < 0.9999999) {
x = Math.atan2(-m23, m22);
y = Math.atan2(-m31, m11);
} else {
x = 0;
y = Math.atan2(m13, m33);
}

break;

case EulerOrder.XZY:
z = Math.asin(-clamp(m12, -1, 1));

if (Math.abs(m12) < 0.9999999) {
x = Math.atan2(m32, m22);
y = Math.atan2(m13, m11);
} else {
x = Math.atan2(-m23, m33);
y = 0;
}

break;
}

return e.set(x, y, z, order);
}

export function makeEulerFromQuaternion(e: Euler, q: Quaternion, order: EulerOrder): Euler {
const m = makeMatrix4RotationFromQuaternion(new Matrix4(), q);

return makeEulerFromRotationMatrix4(e, m, order);
}
113 changes: 0 additions & 113 deletions src/lib/math/Euler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { hashFloat4 } from "../core/hash";
import { IPrimitive } from "./IPrimitive";
import { Matrix4 } from "./Matrix4";
import { Quaternion } from "./Quaternion";

export enum EulerOrder {
XYZ,
Expand Down Expand Up @@ -42,117 +40,6 @@ export class Euler implements IPrimitive<Euler> {
return this;
}

setFromRotationMatrix4(m: Matrix4, order: EulerOrder = EulerOrder.Default): this {
const clamp = (value: number, min: number, max: number): number => Math.min(Math.max(value, min), max);

// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)

const te = m.elements;
const m11 = te[0],
m12 = te[4],
m13 = te[8];
const m21 = te[1],
m22 = te[5],
m23 = te[9];
const m31 = te[2],
m32 = te[6],
m33 = te[10];

let x = 0,
y = 0,
z = 0;

switch (order) {
case EulerOrder.XYZ:
y = Math.asin(clamp(m13, -1, 1));

if (Math.abs(m13) < 0.9999999) {
x = Math.atan2(-m23, m33);
z = Math.atan2(-m12, m11);
} else {
x = Math.atan2(m32, m22);
z = 0;
}

break;

case EulerOrder.YXZ:
x = Math.asin(-clamp(m23, -1, 1));

if (Math.abs(m23) < 0.9999999) {
y = Math.atan2(m13, m33);
z = Math.atan2(m21, m22);
} else {
y = Math.atan2(-m31, m11);
z = 0;
}

break;

case EulerOrder.ZXY:
x = Math.asin(clamp(m32, -1, 1));

if (Math.abs(m32) < 0.9999999) {
y = Math.atan2(-m31, m33);
z = Math.atan2(-m12, m22);
} else {
y = 0;
z = Math.atan2(m21, m11);
}

break;

case EulerOrder.ZYX:
y = Math.asin(-clamp(m31, -1, 1));

if (Math.abs(m31) < 0.9999999) {
x = Math.atan2(m32, m33);
z = Math.atan2(m21, m11);
} else {
x = 0;
z = Math.atan2(-m12, m22);
}

break;

case EulerOrder.YZX:
z = Math.asin(clamp(m21, -1, 1));

if (Math.abs(m21) < 0.9999999) {
x = Math.atan2(-m23, m22);
y = Math.atan2(-m31, m11);
} else {
x = 0;
y = Math.atan2(m13, m33);
}

break;

case EulerOrder.XZY:
z = Math.asin(-clamp(m12, -1, 1));

if (Math.abs(m12) < 0.9999999) {
x = Math.atan2(m32, m22);
y = Math.atan2(m13, m11);
} else {
x = Math.atan2(-m23, m33);
y = 0;
}

break;
}

this.set(x, y, z, order);

return this;
}

setFromQuaternion(q: Quaternion, order = this.order): this {
const m = new Matrix4().makeRotationFromQuaternion(q);

return this.setFromRotationMatrix4(m, order);
}

equals(e: Euler): boolean {
return e.x === this.x && e.y === this.y && e.z === this.z && e.order === this.order;
}
Expand Down
Loading

0 comments on commit ca74267

Please sign in to comment.