/* Copyright (c) 2018 Xiamen Yaji Software Co., Ltd. http://www.cocos.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated engine source code (the "Software"), a limited, worldwide, royalty-free, non-assignable, revocable and non-exclusive license to use Cocos Creator solely to develop games on your target platforms. You shall not use Cocos Creator software for developing other software or tools that's used for developing games. You are not granted to publish, distribute, sublicense, and/or sell copies of Cocos Creator. The software or tools in this License Agreement are licensed, not sold. Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * @category core/math */ import CCClass from '../data/class'; import { Mat3 } from './mat3'; import { IQuatLike, IVec3Like } from './type-define'; import { EPSILON, toDegree } from './utils'; import { ValueType } from '../value-types/value-type'; import { Vec3 } from './vec3'; let _x: number = 0.0; let _y: number = 0.0; let _z: number = 0.0; let _w: number = 0.0; /** * 四元数。 */ export class Quat extends ValueType { public static IDENTITY = Object.freeze(new Quat()); /** * @zh 获得指定四元数的拷贝 */ public static clone (a: Out) { return new Quat(a.x, a.y, a.z, a.w); } /** * @zh 复制目标四元数 */ public static copy (out: Out, a: QuatLike) { out.x = a.x; out.y = a.y; out.z = a.z; out.w = a.w; return out; } /** * @zh 设置四元数值 */ public static set (out: Out, x: number, y: number, z: number, w: number) { out.x = x; out.y = y; out.z = z; out.w = w; return out; } /** * @zh 将目标赋值为单位四元数 */ public static identity (out: Out) { out.x = 0; out.y = 0; out.z = 0; out.w = 1; return out; } /** * @zh 设置四元数为两向量间的最短路径旋转,默认两向量都已归一化 */ public static rotationTo (out: Out, a: VecLike, b: VecLike) { const dot = Vec3.dot(a, b); if (dot < -0.999999) { Vec3.cross(v3_1, Vec3.UNIT_X, a); if (v3_1.length() < 0.000001) { Vec3.cross(v3_1, Vec3.UNIT_Y, a); } Vec3.normalize(v3_1, v3_1); Quat.fromAxisAngle(out, v3_1, Math.PI); return out; } else if (dot > 0.999999) { out.x = 0; out.y = 0; out.z = 0; out.w = 1; return out; } else { Vec3.cross(v3_1, a, b); out.x = v3_1.x; out.y = v3_1.y; out.z = v3_1.z; out.w = 1 + dot; return Quat.normalize(out, out); } } /** * @zh 获取四元数的旋转轴和旋转弧度 * @param outAxis 旋转轴输出 * @param q 源四元数 * @return 旋转弧度 */ public static getAxisAngle (outAxis: VecLike, q: Out) { const rad = Math.acos(q.w) * 2.0; const s = Math.sin(rad / 2.0); if (s !== 0.0) { outAxis.x = q.x / s; outAxis.y = q.y / s; outAxis.z = q.z / s; } else { // If s is zero, return any axis (no rotation - axis does not matter) outAxis.x = 1; outAxis.y = 0; outAxis.z = 0; } return rad; } /** * @zh 四元数乘法 */ public static multiply (out: Out, a: QuatLike_1, b: QuatLike_2) { _x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y; _y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z; _z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x; _w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z; out.x = _x; out.y = _y; out.z = _z; out.w = _w; return out; } /** * @zh 四元数标量乘法 */ public static multiplyScalar (out: Out, a: Out, b: number) { out.x = a.x * b; out.y = a.y * b; out.z = a.z * b; out.w = a.w * b; return out; } /** * @zh 四元数乘加:A + B * scale */ public static scaleAndAdd (out: Out, a: Out, b: Out, scale: number) { out.x = a.x + b.x * scale; out.y = a.y + b.y * scale; out.z = a.z + b.z * scale; out.w = a.w + b.w * scale; return out; } /** * @zh 绕 X 轴旋转指定四元数 * @param rad 旋转弧度 */ public static rotateX (out: Out, a: Out, rad: number) { rad *= 0.5; const bx = Math.sin(rad); const bw = Math.cos(rad); out.x = a.x * bw + a.w * bx; out.y = a.y * bw + a.z * bx; out.z = a.z * bw - a.y * bx; out.w = a.w * bw - a.x * bx; return out; } /** * @zh 绕 Y 轴旋转指定四元数 * @param rad 旋转弧度 */ public static rotateY (out: Out, a: Out, rad: number) { rad *= 0.5; const by = Math.sin(rad); const bw = Math.cos(rad); out.x = a.x * bw - a.z * by; out.y = a.y * bw + a.w * by; out.z = a.z * bw + a.x * by; out.w = a.w * bw - a.y * by; return out; } /** * @zh 绕 Z 轴旋转指定四元数 * @param rad 旋转弧度 */ public static rotateZ (out: Out, a: Out, rad: number) { rad *= 0.5; const bz = Math.sin(rad); const bw = Math.cos(rad); out.x = a.x * bw + a.y * bz; out.y = a.y * bw - a.x * bz; out.z = a.z * bw + a.w * bz; out.w = a.w * bw - a.z * bz; return out; } /** * @zh 绕世界空间下指定轴旋转四元数 * @param axis 旋转轴 * @param rad 旋转弧度 */ public static rotateAround (out: Out, rot: Out, axis: VecLike, rad: number) { // get inv-axis (local to rot) Quat.invert(qt_1, rot); Vec3.transformQuat(v3_1, axis, qt_1); // rotate by inv-axis Quat.fromAxisAngle(qt_1, v3_1, rad); Quat.multiply(out, rot, qt_1); return out; } /** * @zh 绕本地空间下指定轴旋转四元数 * @param axis 旋转轴 * @param rad 旋转弧度 */ public static rotateAroundLocal (out: Out, rot: Out, axis: VecLike, rad: number) { Quat.fromAxisAngle(qt_1, axis, rad); Quat.multiply(out, rot, qt_1); return out; } /** * @zh 根据 xyz 分量计算 w 分量,默认已归一化 */ public static calculateW (out: Out, a: Out) { out.x = a.x; out.y = a.y; out.z = a.z; out.w = Math.sqrt(Math.abs(1.0 - a.x * a.x - a.y * a.y - a.z * a.z)); return out; } /** * @zh 四元数点积(数量积) */ public static dot (a: Out, b: Out) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; } /** * @zh 逐元素线性插值: A + t * (B - A) */ public static lerp (out: Out, a: Out, b: Out, t: number) { out.x = a.x + t * (b.x - a.x); out.y = a.y + t * (b.y - a.y); out.z = a.z + t * (b.z - a.z); out.w = a.w + t * (b.w - a.w); return out; } /** * @zh 四元数球面插值 */ public static slerp (out: Out, a: QuatLike_1, b: QuatLike_2, t: number) { // benchmarks: // http://jsperf.com/quaternion-slerp-implementations let scale0 = 0; let scale1 = 0; // calc cosine let cosom = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; // adjust signs (if necessary) if (cosom < 0.0) { cosom = -cosom; b.x = -b.x; b.y = -b.y; b.z = -b.z; b.w = -b.w; } // calculate coefficients if ((1.0 - cosom) > 0.000001) { // standard case (slerp) const omega = Math.acos(cosom); const sinom = Math.sin(omega); scale0 = Math.sin((1.0 - t) * omega) / sinom; scale1 = Math.sin(t * omega) / sinom; } else { // "from" and "to" quaternions are very close // ... so we can do a linear interpolation scale0 = 1.0 - t; scale1 = t; } // calculate final values out.x = scale0 * a.x + scale1 * b.x; out.y = scale0 * a.y + scale1 * b.y; out.z = scale0 * a.z + scale1 * b.z; out.w = scale0 * a.w + scale1 * b.w; return out; } /** * @zh 带两个控制点的四元数球面插值 */ public static sqlerp (out: Out, a: Out, b: Out, c: Out, d: Out, t: number) { Quat.slerp(qt_1, a, d, t); Quat.slerp(qt_2, b, c, t); Quat.slerp(out, qt_1, qt_2, 2 * t * (1 - t)); return out; } /** * @zh 四元数求逆 */ public static invert (out: Out, a: QuatLike) { const dot = a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w; const invDot = dot ? 1.0 / dot : 0; // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0 out.x = -a.x * invDot; out.y = -a.y * invDot; out.z = -a.z * invDot; out.w = a.w * invDot; return out; } /** * @zh 求共轭四元数,对单位四元数与求逆等价,但更高效 */ public static conjugate (out: Out, a: Out) { out.x = -a.x; out.y = -a.y; out.z = -a.z; out.w = a.w; return out; } /** * @zh 求四元数长度 */ public static len (a: Out) { return Math.sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w); } /** * @zh 求四元数长度平方 */ public static lengthSqr (a: Out) { return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w; } /** * @zh 归一化四元数 */ public static normalize (out: Out, a: Out) { let len = a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w; if (len > 0) { len = 1 / Math.sqrt(len); out.x = a.x * len; out.y = a.y * len; out.z = a.z * len; out.w = a.w * len; } return out; } /** * @zh 根据本地坐标轴朝向计算四元数,默认三向量都已归一化且相互垂直 */ public static fromAxes (out: Out, xAxis: VecLike, yAxis: VecLike, zAxis: VecLike) { Mat3.set(m3_1, xAxis.x, xAxis.y, xAxis.z, yAxis.x, yAxis.y, yAxis.z, zAxis.x, zAxis.y, zAxis.z, ); return Quat.normalize(out, Quat.fromMat3(out, m3_1)); } /** * @zh 根据视口的前方向和上方向计算四元数 * @param view 视口面向的前方向,必须归一化 * @param up 视口的上方向,必须归一化,默认为 (0, 1, 0) */ public static fromViewUp (out: Out, view: VecLike, up?: Vec3) { Mat3.fromViewUp(m3_1, view, up); return Quat.normalize(out, Quat.fromMat3(out, m3_1)); } /** * @zh 根据旋转轴和旋转弧度计算四元数 */ public static fromAxisAngle (out: Out, axis: VecLike, rad: number) { rad = rad * 0.5; const s = Math.sin(rad); out.x = s * axis.x; out.y = s * axis.y; out.z = s * axis.z; out.w = Math.cos(rad); return out; } /** * @zh 根据三维矩阵信息计算四元数,默认输入矩阵不含有缩放信息 */ public static fromMat3 (out: Out, m: Mat3) { const { m00: m00, m03: m01, m06: m02, m01: m10, m04: m11, m07: m12, m02: m20, m05: m21, m08: m22, } = m; const trace = m00 + m11 + m22; if (trace > 0) { const s = 0.5 / Math.sqrt(trace + 1.0); out.w = 0.25 / s; out.x = (m21 - m12) * s; out.y = (m02 - m20) * s; out.z = (m10 - m01) * s; } else if ((m00 > m11) && (m00 > m22)) { const s = 2.0 * Math.sqrt(1.0 + m00 - m11 - m22); out.w = (m21 - m12) / s; out.x = 0.25 * s; out.y = (m01 + m10) / s; out.z = (m02 + m20) / s; } else if (m11 > m22) { const s = 2.0 * Math.sqrt(1.0 + m11 - m00 - m22); out.w = (m02 - m20) / s; out.x = (m01 + m10) / s; out.y = 0.25 * s; out.z = (m12 + m21) / s; } else { const s = 2.0 * Math.sqrt(1.0 + m22 - m00 - m11); out.w = (m10 - m01) / s; out.x = (m02 + m20) / s; out.y = (m12 + m21) / s; out.z = 0.25 * s; } return out; } /** * @zh 根据欧拉角信息计算四元数 */ public static fromEuler (out: Out, x: number, y: number, z: number) { x *= halfToRad; y *= halfToRad; z *= halfToRad; const sx = Math.sin(x); const cx = Math.cos(x); const sy = Math.sin(y); const cy = Math.cos(y); const sz = Math.sin(z); const cz = Math.cos(z); out.x = sx * cy * cz + cx * sy * sz; out.y = cx * sy * cz + sx * cy * sz; out.z = cx * cy * sz - sx * sy * cz; out.w = cx * cy * cz - sx * sy * sz; return out; } /** * @zh 返回定义此四元数的坐标系 X 轴向量 */ public static toAxisX (out: VecLike, q: Out) { const fy = 2.0 * q.y; 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; return out; } /** * @zh 返回定义此四元数的坐标系 Y 轴向量 */ public static toAxisY (out: VecLike, q: Out) { const fx = 2.0 * q.x; const fy = 2.0 * q.y; const fz = 2.0 * q.z; out.x = fy * q.x - fz * q.w; out.y = 1.0 - fx * q.x - fz * q.z; out.z = fz * q.y + fx * q.w; return out; } /** * @zh 返回定义此四元数的坐标系 Z 轴向量 */ public static toAxisZ (out: VecLike, q: Out) { 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.y = fz * q.y - fx * q.w; out.z = 1.0 - fx * q.x - fy * q.y; return out; } /** * @zh 根据四元数计算欧拉角,返回角度在 [-180, 180] 区间内 */ public static toEuler (out: VecLike, q: Out) { const { x, y, z, w } = q; let heading: number = NaN; let attitude: number = NaN; let bank: number = NaN; const test = x * y + z * w; if (test > 0.499999) { // singularity at north pole heading = 2 * Math.atan2(x, w); attitude = Math.PI / 2; bank = 0; } if (test < -0.499999) { // singularity at south pole heading = -2 * Math.atan2(x, w); attitude = - Math.PI / 2; bank = 0; } if (isNaN(heading)) { const sqx = x * x; const sqy = y * y; const sqz = z * z; heading = Math.atan2(2 * y * w - 2 * x * z, 1 - 2 * sqy - 2 * sqz); // heading attitude = Math.asin(2 * test); // attitude bank = Math.atan2(2 * x * w - 2 * y * z, 1 - 2 * sqx - 2 * sqz); // bank } // ranged [-180, 180] out.y = toDegree(heading); out.z = toDegree(attitude); out.x = toDegree(bank); return out; } /** * @zh 四元数转数组 * @param ofs 数组内的起始偏移量 */ public static array (out: IWritableArrayLike, q: Out, ofs = 0) { out[ofs + 0] = q.x; out[ofs + 1] = q.y; out[ofs + 2] = q.z; out[ofs + 3] = q.w; return out; } /** * @zh 四元数等价判断 */ public static strictEquals (a: Out, b: Out) { return a.x === b.x && a.y === b.y && a.z === b.z && a.w === b.w; } /** * @zh 排除浮点数误差的四元数近似等价判断 */ public static equals (a: Out, b: Out, epsilon = EPSILON) { return (Math.abs(a.x - b.x) <= epsilon * Math.max(1.0, Math.abs(a.x), Math.abs(b.x)) && Math.abs(a.y - b.y) <= epsilon * Math.max(1.0, Math.abs(a.y), Math.abs(b.y)) && Math.abs(a.z - b.z) <= epsilon * Math.max(1.0, Math.abs(a.z), Math.abs(b.z)) && Math.abs(a.w - b.w) <= epsilon * Math.max(1.0, Math.abs(a.w), Math.abs(b.w))); } /** * x 分量。 */ public x: number; /** * y 分量。 */ public y: number; /** * z 分量。 */ public z: number; /** * w 分量。 */ public w: number; constructor (other: Quat); constructor (x?: number, y?: number, z?: number, w?: number); constructor (x?: number | Quat, y?: number, z?: number, w?: number) { super(); if (x && typeof x === 'object') { this.x = x.x; this.y = x.y; this.z = x.z; this.w = x.w; } else { this.x = x || 0; this.y = y || 0; this.z = z || 0; this.w = w || 1; } } /** * 克隆当前四元数。 */ public clone () { return new Quat(this.x, this.y, this.z, this.w); } /** * 设置当前四元数使其与指定四元数相等。 * @param other 相比较的四元数。 * @returns `this` */ public set (other: Quat); /** * 设置当前四元数指定元素值。 * @param x 四元数 x 元素值 * @param y 四元数 y 元素值 * @param z 四元数 z 元素值 * @param w 四元数 w 元素值 * @returns `this` */ public set (x?: number, y?: number, z?: number, w?: number); public set (x?: number | Quat, y?: number, z?: number, w?: number) { if (x && typeof x === 'object') { this.x = x.x; this.y = x.y; this.z = x.z; this.w = x.w; } else { this.x = x || 0; this.y = y || 0; this.z = z || 0; this.w = w || 1; } return this; } /** * 判断当前向量是否在误差范围内与指定向量相等。 * @param other 相比较的向量。 * @param epsilon 允许的误差,应为非负数。 * @returns 当两向量的各分量都在指定的误差范围内分别相等时,返回 `true`;否则返回 `false`。 */ public equals (other: Quat, epsilon = EPSILON) { return (Math.abs(this.x - other.x) <= epsilon * Math.max(1.0, Math.abs(this.x), Math.abs(other.x)) && Math.abs(this.y - other.y) <= epsilon * Math.max(1.0, Math.abs(this.y), Math.abs(other.y)) && Math.abs(this.z - other.z) <= epsilon * Math.max(1.0, Math.abs(this.z), Math.abs(other.z)) && Math.abs(this.w - other.w) <= epsilon * Math.max(1.0, Math.abs(this.w), Math.abs(other.w))); } /** * 判断当前四元数是否与指定四元数相等。 * @param other 相比较的四元数。 * @returns 两四元数的各分量都相等时返回 `true`;否则返回 `false`。 */ public strictEquals (other: Quat) { return other && this.x === other.x && this.y === other.y && this.z === other.z && this.w === other.w; } /** * 将当前四元数转化为欧拉角(x-y-z)并赋值给出口向量。 * @param out 出口向量。 */ public getEulerAngles (out: Vec3) { const { x, y, z, w } = this; let heading: number = NaN; let attitude: number = NaN; let bank: number = NaN; const test = x * y + z * w; if (test > 0.499999) { // singularity at north pole heading = 2 * Math.atan2(x, w); attitude = Math.PI / 2; bank = 0; } if (test < -0.499999) { // singularity at south pole heading = -2 * Math.atan2(x, w); attitude = - Math.PI / 2; bank = 0; } if (isNaN(heading)) { const sqx = x * x; const sqy = y * y; const sqz = z * z; heading = Math.atan2(2 * y * w - 2 * x * z, 1 - 2 * sqy - 2 * sqz); // heading attitude = Math.asin(2 * test); // attitude bank = Math.atan2(2 * x * w - 2 * y * z, 1 - 2 * sqx - 2 * sqz); // bank } // ranged [-180, 180] out.y = toDegree(heading); out.z = toDegree(attitude); out.x = toDegree(bank); return out; } /** * 根据指定的插值比率,从当前四元数到目标四元数之间做插值。 * @param to 目标四元数。 * @param ratio 插值比率,范围为 [0,1]。 */ public lerp (to: Quat, ratio: number) { let scale0 = 0; let scale1 = 0; // calc cosine let cosom = this.x * to.x + this.y * to.y + this.z * to.z + this.w * to.w; // adjust signs (if necessary) if (cosom < 0.0) { cosom = -cosom; to.x = -to.x; to.y = -to.y; to.z = -to.z; to.w = -to.w; } // calculate coefficients if ((1.0 - cosom) > 0.000001) { // standard case (slerp) const omega = Math.acos(cosom); const sinom = Math.sin(omega); scale0 = Math.sin((1.0 - ratio) * omega) / sinom; scale1 = Math.sin(ratio * omega) / sinom; } else { // "from" and "to" quaternions are very close // ... so we can do a linear interpolation scale0 = 1.0 - ratio; scale1 = ratio; } // calculate final values this.x = scale0 * this.x + scale1 * to.x; this.y = scale0 * this.y + scale1 * to.y; this.z = scale0 * this.z + scale1 * to.z; this.w = scale0 * this.w + scale1 * to.w; return this; } /** * @zh 求四元数长度 */ public length () { return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w); } /** * @zh 求四元数长度平方 */ public lengthSqr () { return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; } } const qt_1 = new Quat(); const qt_2 = new Quat(); const v3_1 = new Vec3(); const m3_1 = new Mat3(); const halfToRad = 0.5 * Math.PI / 180.0; CCClass.fastDefine('cc.Quat', Quat, { x: 0, y: 0, z: 0, w: 1 }); cc.Quat = Quat; export function quat (other: Quat): Quat; export function quat (x?: number, y?: number, z?: number, w?: number): Quat; export function quat (x: number | Quat = 0, y: number = 0, z: number = 0, w: number = 1) { return new Quat(x as any, y, z, w); } cc.quat = quat;