Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Add convenience methods to quaternion and matrix4 #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions modules/core/src/classes/euler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,23 +285,7 @@ export class Euler extends MathArray {

// TODO - move to Quaternion
getQuaternion(): Quaternion {
const q = new Quaternion();
switch (this[3]) {
case RotationOrder.XYZ:
return q.rotateX(this[0]).rotateY(this[1]).rotateZ(this[2]);
case RotationOrder.YXZ:
return q.rotateY(this[0]).rotateX(this[1]).rotateZ(this[2]);
case RotationOrder.ZXY:
return q.rotateZ(this[0]).rotateX(this[1]).rotateY(this[2]);
case RotationOrder.ZYX:
return q.rotateZ(this[0]).rotateY(this[1]).rotateX(this[2]);
case RotationOrder.YZX:
return q.rotateY(this[0]).rotateZ(this[1]).rotateX(this[2]);
case RotationOrder.XZY:
return q.rotateX(this[0]).rotateZ(this[1]).rotateY(this[2]);
default:
throw new Error(ERR_UNKNOWN_ORDER);
}
return new Quaternion().fromEuler(this);
}

// INTERNAL METHODS
Expand Down
28 changes: 28 additions & 0 deletions modules/core/src/classes/matrix4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,34 @@ export class Matrix4 extends Matrix {
return this.check();
}

/**
* Calculates a 4x4 matrix from the given matrix3
* @param matrix3
* @returns self
*/
fromMatrix3(matrix3: Readonly<NumericArray>): this {
this[0] = matrix3[0];
this[1] = matrix3[1];
this[2] = matrix3[2];
this[3] = 0;

this[4] = matrix3[3];
this[5] = matrix3[4];
this[6] = matrix3[5];
this[7] = 0;

this[8] = matrix3[6];
this[9] = matrix3[7];
this[10] = matrix3[8];
this[11] = 0;

this[12] = 0;
this[13] = 0;
this[14] = 0;
this[15] = 1;
return this.check();
}

/**
* Generates a frustum matrix with the given bounds
* @param view.left - Left bound of the frustum
Expand Down
46 changes: 36 additions & 10 deletions modules/core/src/classes/quaternion.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) 2017 Uber Technologies, Inc.
// MIT License
import {NumericArray} from '@math.gl/types';
import {MathArray} from './base/math-array';
import {checkNumber, checkVector} from '../lib/validators';
import {Vector4} from './vector4';
import { NumericArray } from '@math.gl/types';
import { MathArray } from './base/math-array';
import { checkNumber, checkVector } from '../lib/validators';
import { Vector4 } from './vector4';
// @ts-ignore gl-matrix types...
import {
fromMat3 as quat_fromMat3,
Expand All @@ -27,7 +27,8 @@ import {
slerp as quat_slerp
} from '../gl-matrix/quat';
// @ts-ignore gl-matrix types...
import {transformQuat as vec4_transformQuat} from '../gl-matrix/vec4';
import { transformQuat as vec4_transformQuat } from '../gl-matrix/vec4';
import { Euler } from './euler';

const IDENTITY_QUATERNION = [0, 0, 0, 1] as const;

Expand Down Expand Up @@ -59,7 +60,7 @@ export class Quaternion extends MathArray {
return this.check();
}

fromObject(object: {x: number; y: number; z: number; w: number}): this {
fromObject(object: { x: number; y: number; z: number; w: number }): this {
this[0] = object.x;
this[1] = object.y;
this[2] = object.z;
Expand All @@ -79,6 +80,31 @@ export class Quaternion extends MathArray {
return this.check();
}

/**
* Creates a quaternion from the given Euler.
* @param euler
* @returns
*/
fromEuler(euler: Euler): this {
this.identity();
switch (euler.order) {
case Euler.XYZ:
return this.rotateX(euler[0]).rotateY(euler[1]).rotateZ(euler[2]);
case Euler.YXZ:
return this.rotateY(euler[0]).rotateX(euler[1]).rotateZ(euler[2]);
case Euler.ZXY:
return this.rotateZ(euler[0]).rotateX(euler[1]).rotateY(euler[2]);
case Euler.ZYX:
return this.rotateZ(euler[0]).rotateY(euler[1]).rotateX(euler[2]);
case Euler.YZX:
return this.rotateY(euler[0]).rotateZ(euler[1]).rotateX(euler[2]);
case Euler.XZY:
return this.rotateX(euler[0]).rotateZ(euler[1]).rotateY(euler[2]);
default:
throw new Error('Unknown Euler angle order');
}
}

fromAxisRotation(axis: Readonly<NumericArray>, rad: number): this {
quat_setAxisAngle(this, axis, rad);
return this.check();
Expand Down Expand Up @@ -280,10 +306,10 @@ export class Quaternion extends MathArray {
arg0:
| Readonly<NumericArray>
| {
start: Readonly<NumericArray>;
target: Readonly<NumericArray>;
ratio: number;
},
start: Readonly<NumericArray>;
target: Readonly<NumericArray>;
ratio: number;
},
arg1?: Readonly<NumericArray> | number,
arg2?: number
): this {
Expand Down
Loading