Skip to content

Commit

Permalink
fix ts math library (cocos#14385)
Browse files Browse the repository at this point in the history
* fix ts math library
  • Loading branch information
stanleyljl authored Feb 27, 2023
1 parent de98057 commit 04e9f5e
Show file tree
Hide file tree
Showing 14 changed files with 729 additions and 103 deletions.
6 changes: 4 additions & 2 deletions cocos/core/geometry/plane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ const temp_vec4 = legacyCC.v4();
/**
* @en
* Basic Geometry: Plane.
* Plane Equation: a*x + b*y + c*z - d = 0.
* @zh
* 基础几何:平面。
* 平面方程: a*x + b*y + c*z - d = 0。
*/

export class Plane {
Expand Down Expand Up @@ -223,9 +225,9 @@ export class Plane {
public transform (mat: Mat4): void {
Mat4.invert(temp_mat, mat);
Mat4.transpose(temp_mat, temp_mat);
Vec4.set(temp_vec4, this.n.x, this.n.y, this.n.z, this.d);
Vec4.set(temp_vec4, this.n.x, this.n.y, this.n.z, -this.d);
Vec4.transformMat4(temp_vec4, temp_vec4, temp_mat);
Vec3.set(this.n, temp_vec4.x, temp_vec4.y, temp_vec4.z);
this.d = temp_vec4.w;
this.d = -temp_vec4.w;
}
}
2 changes: 1 addition & 1 deletion cocos/core/geometry/sphere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export class Sphere {

/**
* @en
* Transforms this sphere by a 4x4 matrix and a quternion, stores the result to the `out` parameter.
* Transforms this sphere by a 4x4 matrix and a quaternion, stores the result to the `out` parameter.
* @zh
* 使用一个 4x4 矩阵和一个四元数变换此球体,并将结果存储在 `out` 参数中。
* @param m @en The 4x4 transform matrix. @zh 4x4 变换矩阵。
Expand Down
39 changes: 29 additions & 10 deletions cocos/core/math/affine-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export class AffineTransform {

/**
* @en Concatenate a transform matrix to another. The results are reflected in the out `AffineTransform`.
* @zh 将两个矩阵相乘的结果赋值给输出矩阵。
* First apply t1, then t2: out * v = t2 * (t1 * v).
* @zh 将两个矩阵相乘的结果赋值给输出矩阵,先应用t1再应用t2: out * v = t2 * (t1 * v)。
* @param out Out object to store the concat result
* @param t1 The first transform object.
* @param t2 The transform object to concatenate.
Expand Down Expand Up @@ -198,24 +199,42 @@ export class AffineTransform {
* @param rect The rect object to apply transform.
* @param anAffineTransform transform matrix.
*/
public static transformObb (out_bl: Vec2, out_tl: Vec2, out_tr: Vec2, out_br: Vec2, rect: Rect, anAffineTransform: AffineTransform) {
public static transformObb (out_bl: Vec2, out_tl: Vec2, out_tr: Vec2, out_br: Vec2, rect: Rect,
anAffineTransform: AffineTransform, flipY = true) {
const tx = anAffineTransform.a * rect.x + anAffineTransform.c * rect.y + anAffineTransform.tx;
const ty = anAffineTransform.b * rect.x + anAffineTransform.d * rect.y + anAffineTransform.ty;
const xa = anAffineTransform.a * rect.width;
const xb = anAffineTransform.b * rect.width;
const yc = anAffineTransform.c * rect.height;
const yd = anAffineTransform.d * rect.height;

out_tl.x = tx;
out_tl.y = ty;
out_tr.x = xa + tx;
out_tr.y = xb + ty;
out_bl.x = yc + tx;
out_bl.y = yd + ty;
out_br.x = xa + yc + tx;
out_br.y = xb + yd + ty;
if (flipY) {
out_tl.x = tx;
out_tl.y = ty;
out_tr.x = xa + tx;
out_tr.y = xb + ty;
out_bl.x = yc + tx;
out_bl.y = yd + ty;
out_br.x = xa + yc + tx;
out_br.y = xb + yd + ty;
} else {
out_bl.x = tx;
out_bl.y = ty;
out_br.x = xa + tx;
out_br.y = xb + ty;
out_tl.x = yc + tx;
out_tl.y = yd + ty;
out_tr.x = xa + yc + tx;
out_tr.y = xb + yd + ty;
}
}

/**
* matrix layout
* |a c tx|
* |b d ty|
* |0 0 1 |
*/
public declare a: number;
public declare b: number;
public declare c: number;
Expand Down
58 changes: 36 additions & 22 deletions cocos/core/math/mat3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ export class Mat3 extends ValueType {
public static set <Out extends IMat3Like> (
out: Out,
m00: number, m01: number, m02: number,
m10: number, m11: number, m12: number,
m20: number, m21: number, m22: number,
m03: number, m04: number, m05: number,
m06: number, m07: number, m08: number,
) {
out.m00 = m00; out.m01 = m01; out.m02 = m02;
out.m03 = m10; out.m04 = m11; out.m05 = m12;
out.m06 = m20; out.m07 = m21; out.m08 = m22;
out.m03 = m03; out.m04 = m04; out.m05 = m05;
out.m06 = m06; out.m07 = m07; out.m08 = m08;
return out;
}

Expand Down Expand Up @@ -180,8 +180,8 @@ export class Mat3 extends ValueType {
}

/**
* @en Multiply two matrices explicitly and save the results to out matrix
* @zh 矩阵乘法
* @en Multiply two matrices explicitly and save the results to out matrix: a * b
* @zh 矩阵乘法:a * b
*/
public static multiply <Out extends IMat3Like> (out: Out, a: Out, b: Out) {
const a00 = a.m00; const a01 = a.m01; const a02 = a.m02;
Expand All @@ -207,8 +207,8 @@ export class Mat3 extends ValueType {
}

/**
* @en Take the first third order of the fourth order matrix and multiply by the third order matrix
* @zh 取四阶矩阵的前三阶,与三阶矩阵相乘
* @en Take the first third order of the fourth order matrix and multiply by the third order matrix: a * b
* @zh 取四阶矩阵的前三阶,与三阶矩阵相乘:a * b
*/
public static multiplyMat4 <Out extends IMat3Like> (out: Out, a: Out, b: IMat4Like) {
const a00 = a.m00; const a01 = a.m01; const a02 = a.m02;
Expand All @@ -234,10 +234,21 @@ export class Mat3 extends ValueType {
}

/**
* @en Multiply a matrix with a translation vector given by a translation offset.
* @zh 在给定矩阵变换基础上加入变换
* @en Multiply a matrix with a translation vector given by a translation offset, first translate, then transform:a * T(v).
* @zh 在给定矩阵变换基础上加入位移变换,先位移,再变换,即a * T(v)。
*/
/**
* @deprecated since v3.8.0, the function name is misleading, please use translate instead.
*/
public static transform <Out extends IMat3Like, VecLike extends IVec3Like> (out: Out, a: Out, v: VecLike) {
this.translate(out, a, v);
}

/**
* @en Multiply a matrix with a translation vector given by a translation offset, first translate, then transform:a * T(v).
* @zh 在给定矩阵变换基础上加入位移变换,先位移,再变换,即a * T(v)。
*/
public static translate <Out extends IMat3Like, VecLike extends IVec3Like> (out: Out, a: Out, v: VecLike) {
const a00 = a.m00; const a01 = a.m01; const a02 = a.m02;
const a10 = a.m03; const a11 = a.m04; const a12 = a.m05;
const a20 = a.m06; const a21 = a.m07; const a22 = a.m08;
Expand All @@ -258,8 +269,8 @@ export class Mat3 extends ValueType {
}

/**
* @en Multiply a matrix with a scale matrix given by a scale vector and save the results to out matrix
* @zh 在给定矩阵变换基础上加入新缩放变换
* @en Multiply a matrix with a scale matrix given by a scale vector and save the results to out matrix, first scale, then transform:a * S(v).
* @zh 在给定矩阵变换基础上加入新缩放变换,先缩放,再变换,即a * S(v)。
*/
public static scale <Out extends IMat3Like, VecLike extends IVec3Like> (out: Out, a: Out, v: VecLike) {
const x = v.x; const y = v.y;
Expand All @@ -279,9 +290,9 @@ export class Mat3 extends ValueType {
}

/**
* @en Rotates the transform by the given angle and save the results into the out matrix
* @zh 在给定矩阵变换基础上加入新旋转变换
* @param rad radius of rotation
* @en Rotates the transform by the given angle and save the results into the out matrix, first rotate, then transform:a * R(rad).
* @zh 在给定矩阵变换基础上加入新旋转变换,先旋转,再变换,即a * R(rad)。
* @param rad radian of rotation
*/
public static rotate <Out extends IMat3Like> (out: Out, a: Out, rad: number) {
const a00 = a.m00; const a01 = a.m01; const a02 = a.m02;
Expand Down Expand Up @@ -445,6 +456,9 @@ export class Mat3 extends ValueType {
return out;
}

/**
* @deprecated since v3.8.0, this function is too complicated, and should be split into several functions.
*/
/**
* @en Calculates the upper-left 3x3 matrix of a 4x4 matrix's inverse transpose
* @zh 计算指定四维矩阵的逆转置三维矩阵
Expand Down Expand Up @@ -625,8 +639,8 @@ export class Mat3 extends ValueType {

/**
* @en Convert Matrix to euler angle, resulting angle y, z in the range of [-PI, PI],
* x in the range of [-PI/2, PI/2], the rotation order is YXZ.
* @zh 将矩阵转换成欧拉角, 返回角度 y,z 在 [-PI, PI] 区间内, x 在 [-PI/2, PI/2] 区间内,旋转顺序为 YXZ.
* x in the range of [-PI/2, PI/2], the rotation order is YXZ, first rotate around Y, then around X, and finally around Z.
* @zh 将矩阵转换成欧拉角, 返回角度 y,z 在 [-PI, PI] 区间内, x 在 [-PI/2, PI/2] 区间内,旋转顺序为 YXZ,即先绕Y旋转,再绕X,最后绕Z旋转。
*/
public static toEuler (matrix: Mat3, v: Vec3): boolean {
//a[col][row]
Expand Down Expand Up @@ -991,8 +1005,8 @@ export class Mat3 extends ValueType {
}

/**
* @en Multiply the current matrix with a scale matrix given by a scale vector.
* @zh 将当前矩阵左乘缩放矩阵的结果赋值给当前矩阵,缩放矩阵由各个轴的缩放给出。
* @en Multiply the current matrix with a scale matrix given by a scale vector, that is M * S(vec).
* @zh 将当前矩阵左乘缩放矩阵的结果赋值给当前矩阵,缩放矩阵由各个轴的缩放给出,即M * S(vec)
* @param vec vector to scale by
*/
public scale (vec: Vec3) {
Expand All @@ -1013,9 +1027,9 @@ export class Mat3 extends ValueType {
}

/**
* @en Rotates the current matrix by the given angle.
* @zh 将当前矩阵左乘旋转矩阵的结果赋值给当前矩阵,旋转矩阵由旋转轴和旋转角度给出。
* @param rad radius of rotation
* @en Rotates the current matrix by the given angle, that is M * R(rad).
* @zh 将当前矩阵左乘旋转矩阵的结果赋值给当前矩阵,旋转矩阵由旋转轴和旋转角度给出,即M * R(rad)
* @param rad radian of rotation
*/
public rotate (rad: number) {
const a00 = this.m00; const a01 = this.m01; const a02 = this.m02;
Expand Down
36 changes: 19 additions & 17 deletions cocos/core/math/quat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class Quat extends ValueType {
* @zh 获取四元数的旋转轴和旋转弧度
* @param outAxis output axis
* @param q input quaternion
* @return radius of rotation
* @return radian of rotation
*/
public static getAxisAngle<Out extends IQuatLike, VecLike extends IVec3Like> (outAxis: VecLike, q: Out) {
const rad = Math.acos(q.w) * 2.0;
Expand All @@ -135,8 +135,8 @@ export class Quat extends ValueType {
}

/**
* @en Quaternion multiplication and save the results to out quaternion
* @zh 四元数乘法
* @en Quaternion multiplication and save the results to out quaternion, that is a * b.
* @zh 四元数乘法,即a * b。
*/
public static multiply<Out extends IQuatLike, QuatLike_1 extends IQuatLike, QuatLike_2 extends IQuatLike> (out: Out, a: QuatLike_1, b: QuatLike_2) {
const x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;
Expand Down Expand Up @@ -177,7 +177,7 @@ export class Quat extends ValueType {
/**
* @en Sets the out quaternion to represent a radian rotation around x axis
* @zh 绕 X 轴旋转指定四元数
* @param rad radius of rotation
* @param rad radian of rotation
*/
public static rotateX<Out extends IQuatLike> (out: Out, a: Out, rad: number) {
rad *= 0.5;
Expand All @@ -196,7 +196,7 @@ export class Quat extends ValueType {
/**
* @en Sets the out quaternion to represent a radian rotation around y axis
* @zh 绕 Y 轴旋转指定四元数
* @param rad radius of rotation
* @param rad radian of rotation
*/
public static rotateY<Out extends IQuatLike> (out: Out, a: Out, rad: number) {
rad *= 0.5;
Expand All @@ -215,7 +215,7 @@ export class Quat extends ValueType {
/**
* @en Sets the out quaternion to represent a radian rotation around z axis
* @zh 绕 Z 轴旋转指定四元数
* @param rad radius of rotation
* @param rad radian of rotation
*/
public static rotateZ<Out extends IQuatLike> (out: Out, a: Out, rad: number) {
rad *= 0.5;
Expand All @@ -235,7 +235,7 @@ export class Quat extends ValueType {
* @en Sets the out quaternion to represent a radian rotation around a given rotation axis in world space
* @zh 绕世界空间下指定轴旋转四元数
* @param axis axis of rotation, normalized by default
* @param rad radius of rotation
* @param rad radian of rotation
*/
public static rotateAround<Out extends IQuatLike, VecLike extends IVec3Like> (out: Out, rot: Out, axis: VecLike, rad: number) {
// get inv-axis (local to rot)
Expand All @@ -251,7 +251,7 @@ export class Quat extends ValueType {
* @en Sets the out quaternion to represent a radian rotation around a given rotation axis in local space
* @zh 绕本地空间下指定轴旋转四元数
* @param axis axis of rotation
* @param rad radius of rotation
* @param rad radian of rotation
*/
public static rotateAroundLocal<Out extends IQuatLike, VecLike extends IVec3Like> (out: Out, rot: Out, axis: VecLike, rad: number) {
Quat.fromAxisAngle(qt_1, axis, rad);
Expand Down Expand Up @@ -457,7 +457,7 @@ export class Quat extends ValueType {
* @en Calculates the quaternion with the three-dimensional transform matrix, considering no scale included in the matrix
* @zh 根据三维矩阵信息计算四元数,默认输入矩阵不含有缩放信息
*/
public static fromMat3<Out extends IQuatLike> (out: Out, m: Mat3) {
public static fromMat3<Out extends IQuatLike> (out: Out, m: Mat3) {
const {
m00, m03: m01, m06: m02,
m01: m10, m04: m11, m07: m12,
Expand Down Expand Up @@ -500,8 +500,8 @@ export class Quat extends ValueType {
}

/**
* @en Calculates the quaternion with Euler angles, the rotation order is YZX
* @zh 根据欧拉角信息计算四元数,旋转顺序为 YZX
* @en Calculates the quaternion with Euler angles, the rotation order is YZX, first rotate around Y, then around Z, and finally around X.
* @zh 根据欧拉角信息计算四元数,旋转顺序为 YZX,即先绕Y旋转,再绕Z,最后绕X旋转。
*/
public static fromEuler<Out extends IQuatLike> (out: Out, x: number, y: number, z: number) {
x *= halfToRad;
Expand Down Expand Up @@ -547,7 +547,7 @@ export class Quat extends ValueType {
const fz = 2.0 * q.z;
out.x = 1.0 - fy * q.y - fz * q.z;
out.y = fy * q.x + fz * q.w;
out.z = fz * q.x + fy * q.w;
out.z = fz * q.x - fy * q.w;

return out;
}
Expand Down Expand Up @@ -575,16 +575,17 @@ export class Quat extends ValueType {
const fx = 2.0 * q.x;
const fy = 2.0 * q.y;
const fz = 2.0 * q.z;
out.x = fz * q.x - fy * q.w;
out.x = fz * q.x + fy * q.w;
out.y = fz * q.y - fx * q.w;
out.z = 1.0 - fx * q.x - fy * q.y;

return out;
}

/**
* @en Converts the quaternion to angles, result angle x, y in the range of [-180, 180], z in the range of [-90, 90] interval, the rotation order is YZX
* @zh 根据四元数计算欧拉角,返回角度 x, y 在 [-180, 180] 区间内, z 默认在 [-90, 90] 区间内,旋转顺序为 YZX
* @en Converts the quaternion to angles, result angle x, y in the range of [-180, 180], z in the range of [-90, 90] interval,
* the rotation order is YZX, first rotate around Y, then around Z, and finally around X
* @zh 根据四元数计算欧拉角,返回角度 x, y 在 [-180, 180] 区间内, z 默认在 [-90, 90] 区间内,旋转顺序为 YZX,即先绕Y旋转,再绕Z,最后绕X旋转。
* @param outerZ change z value range to [-180, -90] U [90, 180]
*/
public static toEuler (out: IVec3Like, q: IQuatLike, outerZ?: boolean) {
Expand Down Expand Up @@ -619,8 +620,9 @@ export class Quat extends ValueType {
}

/**
* @en Converts the quaternion to euler angles, result angle y, z in the range of [-180, 180], x in the range of [-90, 90], the rotation order is YXZ
* @zh 根据四元数计算欧拉角,返回角度 yz 在 [-180, 180], x 在 [-90, 90],旋转顺序为 YXZ
* @en Converts the quaternion to euler angles, result angle y, z in the range of [-180, 180], x in the range of [-90, 90],
* the rotation order is YXZ, first rotate around Y, then around X, and finally around Z.
* @zh 根据四元数计算欧拉角,返回角度 yz 在 [-180, 180], x 在 [-90, 90],旋转顺序为 YXZ,即先绕Y旋转,再绕X,最后绕Z旋转。
*/
public static toEulerInYXZOrder (out: Vec3, q: IQuatLike) {
Mat3.fromQuat(m3_1, q);
Expand Down
Loading

0 comments on commit 04e9f5e

Please sign in to comment.