Files
cocos4/cocos/core/geometry/aabb.ts
T
Yun Hsiao Wu 32598c25cb switch to eslint (#7727)
* switch to eslint

* eslint: extends airbnb
2020-11-09 10:19:12 +08:00

259 lines
7.4 KiB
TypeScript

/**
* @packageDocumentation
* @module geometry
*/
import { Mat3, Mat4, Quat, Vec3 } from '../../core/math';
import enums from './enums';
import { IVec3Like } from '../math/type-define';
import sphere from './sphere';
const _v3_tmp = new Vec3();
const _v3_tmp2 = new Vec3();
const _v3_tmp3 = new Vec3();
const _v3_tmp4 = new Vec3();
const _m3_tmp = new Mat3();
// https://zeuxcg.org/2010/10/17/aabb-from-obb-with-component-wise-abs/
const transform_extent_m4 = (out: Vec3, extent: Vec3, m4: Mat4) => {
_m3_tmp.m00 = Math.abs(m4.m00); _m3_tmp.m01 = Math.abs(m4.m01); _m3_tmp.m02 = Math.abs(m4.m02);
_m3_tmp.m03 = Math.abs(m4.m04); _m3_tmp.m04 = Math.abs(m4.m05); _m3_tmp.m05 = Math.abs(m4.m06);
_m3_tmp.m06 = Math.abs(m4.m08); _m3_tmp.m07 = Math.abs(m4.m09); _m3_tmp.m08 = Math.abs(m4.m10);
Vec3.transformMat3(out, extent, _m3_tmp);
};
/**
* @en
* Basic Geometry: Axis-aligned bounding box, using center and half extents structure.
* @zh
* 基础几何 轴对齐包围盒,使用中心点和半长宽高的结构。
*/
export default class aabb {
/**
* @en
* create a new aabb
* @zh
* 创建一个新的 aabb 实例。
* @param px - aabb 的原点的 X 坐标。
* @param py - aabb 的原点的 Y 坐标。
* @param pz - aabb 的原点的 Z 坐标。
* @param hw - aabb 宽度的一半。
* @param hh - aabb 高度的一半。
* @param hl - aabb 长度的一半。
* @returns 返回新创建的 aabb 实例。
*/
public static create (px?: number, py?: number, pz?: number, hw?: number, hh?: number, hl?: number) {
return new aabb(px, py, pz, hw, hh, hl);
}
/**
* @en
* clone a new aabb
* @zh
* 克隆一个 aabb。
* @param a - 克隆的目标。
* @returns 克隆出的 aabb。
*/
public static clone (a: aabb) {
return new aabb(a.center.x, a.center.y, a.center.z,
a.halfExtents.x, a.halfExtents.y, a.halfExtents.z);
}
/**
* @en
* copy the values from one aabb to another
* @zh
* 将从一个 aabb 的值复制到另一个 aabb。
* @param {aabb} out 接受操作的 aabb。
* @param {aabb} a 被复制的 aabb。
* @return {aabb} out 接受操作的 aabb。
*/
public static copy (out: aabb, a: aabb): aabb {
Vec3.copy(out.center, a.center);
Vec3.copy(out.halfExtents, a.halfExtents);
return out;
}
/**
* @en
* create a new aabb from two corner points
* @zh
* 从两个点创建一个新的 aabb。
* @param out - 接受操作的 aabb。
* @param minPos - aabb 的最小点。
* @param maxPos - aabb 的最大点。
* @returns {aabb} out 接受操作的 aabb。
*/
public static fromPoints (out: aabb, minPos: IVec3Like, maxPos: IVec3Like): aabb {
Vec3.add(_v3_tmp, maxPos, minPos);
Vec3.subtract(_v3_tmp2, maxPos, minPos);
Vec3.multiplyScalar(out.center, _v3_tmp, 0.5);
Vec3.multiplyScalar(out.halfExtents, _v3_tmp2, 0.5);
return out;
}
/**
* @en
* Set the components of a aabb to the given values
* @zh
* 将 aabb 的属性设置为给定的值。
* @param {aabb} out 接受操作的 aabb。
* @param px - aabb 的原点的 X 坐标。
* @param py - aabb 的原点的 Y 坐标。
* @param pz - aabb 的原点的 Z 坐标。
* @param hw - aabb 宽度的一半。
* @param hh - aabb 高度的一半。
* @param hl - aabb 长度度的一半。
* @return {aabb} out 接受操作的 aabb。
*/
public static set (out: aabb, px: number, py: number, pz: number, hw: number, hh: number, hl: number): aabb {
Vec3.set(out.center, px, py, pz);
Vec3.set(out.halfExtents, hw, hh, hl);
return out;
}
/**
* @en
* Merge tow aabb.
* @zh
* 合并两个 aabb 到 out。
* @param out 接受操作的 aabb。
* @param a 输入的 aabb。
* @param b 输入的 aabb。
* @returns {aabb} out 接受操作的 aabb。
*/
public static merge (out: aabb, a: aabb, b: aabb): aabb {
Vec3.subtract(_v3_tmp, a.center, a.halfExtents);
Vec3.subtract(_v3_tmp2, b.center, b.halfExtents);
Vec3.add(_v3_tmp3, a.center, a.halfExtents);
Vec3.add(_v3_tmp4, b.center, b.halfExtents);
Vec3.max(_v3_tmp4, _v3_tmp3, _v3_tmp4);
Vec3.min(_v3_tmp3, _v3_tmp, _v3_tmp2);
return aabb.fromPoints(out, _v3_tmp3, _v3_tmp4);
}
/**
* @en
* aabb to sphere
* @zh
* 包围盒转包围球
* @param out 接受操作的 sphere。
* @param a 输入的 aabb。
*/
public static toBoundingSphere (out: sphere, a: aabb) {
a.getBoundary(_v3_tmp, _v3_tmp2);
// Initialize sphere
out.center.set(_v3_tmp);
out.radius = 0.0;
// Calculate sphere
Vec3.subtract(_v3_tmp3, _v3_tmp2, out.center);
const dist = _v3_tmp3.length();
const half = dist * 0.5;
out.radius += half;
Vec3.multiplyScalar(_v3_tmp3, _v3_tmp3, half / dist);
Vec3.add(out.center, out.center, _v3_tmp3);
return out;
}
/**
* @en
* Transform this aabb.
* @zh
* 变换一个 aabb 到 out 中。
* @param out 接受操作的 aabb。
* @param a 输入的源 aabb。
* @param matrix 矩阵。
* @returns {aabb} out 接受操作的 aabb。
*/
public static transform (out: aabb, a: aabb, matrix: Mat4): aabb {
Vec3.transformMat4(out.center, a.center, matrix);
transform_extent_m4(out.halfExtents, a.halfExtents, matrix);
return out;
}
/**
* @zh
* 本地坐标的中心点。
*/
public center: Vec3;
/**
* @zh
* 长宽高的一半。
*/
public halfExtents: Vec3;
/**
* @en
* Gets the type of the shape.
* @zh
* 获取形状的类型。
*/
get type () {
return this._type;
}
protected readonly _type: number;
constructor (px = 0, py = 0, pz = 0, hw = 1, hh = 1, hl = 1) {
this._type = enums.SHAPE_AABB;
this.center = new Vec3(px, py, pz);
this.halfExtents = new Vec3(hw, hh, hl);
}
/**
* @en
* Get the bounding points of this shape
* @zh
* 获取 aabb 的最小点和最大点。
* @param {Vec3} minPos 最小点。
* @param {Vec3} maxPos 最大点。
*/
public getBoundary (minPos: IVec3Like, maxPos: IVec3Like) {
Vec3.subtract(minPos, this.center, this.halfExtents);
Vec3.add(maxPos, this.center, this.halfExtents);
}
/**
* @en
* Transform this shape
* @zh
* 将 out 根据这个 aabb 的数据进行变换。
* @param m 变换的矩阵。
* @param pos 变换的位置部分。
* @param rot 变换的旋转部分。
* @param scale 变换的缩放部分。
* @param out 变换的目标。
*/
public transform (m: Mat4, pos: Vec3 | null, rot: Quat | null, scale: Vec3 | null, out: aabb) {
Vec3.transformMat4(out.center, this.center, m);
transform_extent_m4(out.halfExtents, this.halfExtents, m);
}
/**
* @zh
* 获得克隆。
* @returns {aabb}
*/
public clone (): aabb {
return aabb.clone(this);
}
/**
* @zh
* 拷贝对象。
* @param a 拷贝的目标。
* @returns {aabb}
*/
public copy (a: aabb): aabb {
return aabb.copy(this, a);
}
}