-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optimize math class size and dependencies
- Loading branch information
Showing
20 changed files
with
703 additions
and
703 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.