Math tune cpp (#14526)

* fix problem, add missing functions for vec4

* add unit test for math lib, review geom lib, fix comment, clean code

* revert changes on native math lib

* fix typo, cancel deprecates

* fix csm layer frustum usage

* tune document

* restore native changes

* stage math

* correct frustum

* add more unit test

* fix mistakes introduced in the last commit

* add missing changes

* update

* update deprecated information, rename interfaces

* fix operator priority issue

* fix math library related issues
This commit is contained in:
Yiwen
2023-03-12 15:58:07 +08:00
committed by GitHub
parent 82949a7c99
commit b6abb65171
16 changed files with 411 additions and 215 deletions
+14 -18
View File
@@ -49,9 +49,9 @@ const _temp_v3 = new Vec3();
*/
export class Frustum {
/**
* @en Creates an orthogonal frustum.
* @en Creates an orthographic frustum.
* @zh 创建一个正交视锥体。
* @param out @en The result orthogonal frustum. @zh 输出的正交视锥体。
* @param out @en The result orthographic frustum. @zh 输出的正交视锥体。
* @param width @en The width of the frustum. @zh 正交视锥体的宽度。
* @param height @en The height of the frustum. @zh 正交视锥体的高度。
* @param near @en The near plane of the frustum. @zh 正交视锥体的近平面值。
@@ -59,7 +59,7 @@ export class Frustum {
* @param transform @en The transform matrix of the frustum. @zh 正交视锥体的变换矩阵。
* @returns @en The result frustum, same as the `out` parameter. @zh 存储结果的视锥体,与 `out` 参数为同一个对象。
*/
public static createOrthogonal (out: Frustum, width: number, height: number, near: number, far: number, transform: Mat4) {
public static createOrthographic (out: Frustum, width: number, height: number, near: number, far: number, transform: Mat4) {
const halfWidth = width / 2;
const halfHeight = height / 2;
Vec3.set(_temp_v3, halfWidth, halfHeight, -near);
@@ -83,9 +83,9 @@ export class Frustum {
}
/**
* @en Creates an orthogonal frustum.
* @en Creates an orthographic frustum.
* @zh 创建一个正交视锥体。
* @param out @en The result orthogonal frustum. @zh 输出的正交视锥体。
* @param out @en The result orthographic frustum. @zh 输出的正交视锥体。
* @param width @en The width of the frustum. @zh 正交视锥体的宽度。
* @param height @en The height of the frustum. @zh 正交视锥体的高度。
* @param near @en The near plane of the frustum. @zh 正交视锥体的近平面值。
@@ -93,10 +93,10 @@ export class Frustum {
* @param transform @en The transform matrix of the frustum. @zh 正交视锥体的变换矩阵。
* @returns @en The result frustum, same as the `out` parameter. @zh 存储结果的视锥体,与 `out` 参数为同一个对象。
*
* @deprecated since v3.8.0, please use [[createOrthogonal]] instead.
* @deprecated since v3.8.0, please use [[createOrthographic]] instead.
*/
public static createOrtho (out: Frustum, width: number, height: number, near: number, far: number, transform: Mat4) {
return Frustum.createOrthogonal(out, width, height, near, far, transform);
return Frustum.createOrthographic(out, width, height, near, far, transform);
}
/**
@@ -146,7 +146,7 @@ export class Frustum {
* @param aabb @en The AABB to create the result frustum. @zh 用于创建视锥体 AABB。
* @returns @en The result frustum, same as the `out` parameter. @zh 存储结果的视锥体,与 `out` 参数为同一个对象。
*
* @deprecated since v3.8.0, please use [[createOrthogonal]] instead.
* @deprecated since v3.8.0, please use [[createOrthographic]] instead.
*/
public static createFromAABB (out: Frustum, aabb: AABB | Readonly<AABB>): Frustum {
const vec3_min = new Vec3(); const vec3_max = new Vec3();
@@ -162,10 +162,6 @@ export class Frustum {
out.vertices[6].set(vec3_min.x, vec3_min.y, -vec3_max.z);
out.vertices[7].set(vec3_max.x, vec3_min.y, -vec3_max.z);
if (out._type !== enums.SHAPE_FRUSTUM_ACCURATE) {
return out;
}
out.updatePlanes();
return out;
@@ -234,6 +230,8 @@ export class Frustum {
* Sets whether to use accurate intersection testing function on this frustum.
* @zh
* 设置是否在此截锥体上使用精确的相交测试函数。
*
* @deprecated since v3.8.0 no need to set accurate flag since it doesn't affect the calculation at all.
*/
set accurate (b: boolean) {
this._type = b ? enums.SHAPE_FRUSTUM_ACCURATE : enums.SHAPE_FRUSTUM;
@@ -312,8 +310,6 @@ export class Frustum {
Vec3.set(this.planes[5].n, m.m03 - m.m02, m.m07 - m.m06, m.m11 - m.m10);
this.planes[5].d = -(m.m15 - m.m14);
if (this._type !== enums.SHAPE_FRUSTUM_ACCURATE) { return; }
// normalize planes
for (let i = 0; i < 6; i++) {
const pl = this.planes[i];
@@ -336,9 +332,6 @@ export class Frustum {
* @param mat @en The transform matrix. @zh 变换矩阵。
*/
public transform (mat: Mat4) {
if (this._type !== enums.SHAPE_FRUSTUM_ACCURATE) {
return;
}
for (let i = 0; i < 8; i++) {
Vec3.transformMat4(this.vertices[i], this.vertices[i], mat);
}
@@ -350,10 +343,13 @@ export class Frustum {
* @zh 置空此视锥体,所有顶点将被赋值为 0。
*/
public zero () {
// reset to initial state
for (let i = 0; i < 8; i++) {
this.vertices[i].set(0.0, 0.0, 0.0);
}
this.updatePlanes();
for (let i = 0; i < 6; i++) {
Plane.set(this.planes[i], 0, 0, 0, 0);
}
}
/**
-17
View File
@@ -1813,23 +1813,6 @@ export class Mat4 extends ValueType {
* @en Set the matrix with values of all elements
* @zh 设置当前矩阵指定元素值。
*
* @param m00 Component in column 0, row 0 position (index 0)
* @param m01 Component in column 0, row 1 position (index 1)
* @param m02 Component in column 0, row 2 position (index 2)
* @param m03 Component in column 0, row 3 position (index 3)
* @param m10 Component in column 1, row 0 position (index 4)
* @param m11 Component in column 1, row 1 position (index 5)
* @param m12 Component in column 1, row 2 position (index 6)
* @param m13 Component in column 1, row 3 position (index 7)
* @param m20 Component in column 2, row 0 position (index 8)
* @param m21 Component in column 2, row 1 position (index 9)
* @param m22 Component in column 2, row 2 position (index 10)
* @param m23 Component in column 2, row 3 position (index 11)
* @param m30 Component in column 3, row 0 position (index 12)
* @param m31 Component in column 3, row 1 position (index 13)
* @param m32 Component in column 3, row 2 position (index 14)
* @param m33 Component in column 3, row 3 position (index 15)
* @return this
*/
public set(
+47 -48
View File
@@ -46,6 +46,14 @@ void Frustum::createOrtho(Frustum *out, float width,
float near,
float far,
const Mat4 &transform) {
createOrthographic(out, width, height, near, far, transform);
}
void Frustum::createOrthographic(Frustum *out, float width,
float height,
float near,
float far,
const Mat4 &transform) {
auto halfWidth = width / 2.0F;
auto halfHeight = height / 2.0F;
Vec3::transformMat4({halfWidth, halfHeight, -near}, transform, &out->vertices[0]);
@@ -57,12 +65,7 @@ void Frustum::createOrtho(Frustum *out, float width,
Vec3::transformMat4({-halfWidth, -halfHeight, -far}, transform, &out->vertices[6]);
Vec3::transformMat4({halfWidth, -halfHeight, -far}, transform, &out->vertices[7]);
Plane::fromPoints(out->planes[0], out->vertices[1], out->vertices[6], out->vertices[5]);
Plane::fromPoints(out->planes[1], out->vertices[3], out->vertices[4], out->vertices[7]);
Plane::fromPoints(out->planes[2], out->vertices[6], out->vertices[3], out->vertices[7]);
Plane::fromPoints(out->planes[3], out->vertices[0], out->vertices[5], out->vertices[4]);
Plane::fromPoints(out->planes[4], out->vertices[2], out->vertices[0], out->vertices[3]);
Plane::fromPoints(out->planes[5], out->vertices[7], out->vertices[5], out->vertices[6]);
out->updatePlanes();
}
Frustum *Frustum::createFromAABB(Frustum *out, const AABB &aabb) {
@@ -70,24 +73,42 @@ Frustum *Frustum::createFromAABB(Frustum *out, const AABB &aabb) {
Vec3 maxPos;
aabb.getBoundary(&minPos, &maxPos);
out->vertices[0].set(minPos.x, maxPos.y, minPos.z);
out->vertices[1].set(maxPos.x, maxPos.y, minPos.z);
out->vertices[2].set(maxPos.x, minPos.y, minPos.z);
out->vertices[3].set(minPos.x, minPos.y, minPos.z);
out->vertices[4].set(minPos.x, maxPos.y, maxPos.z);
out->vertices[5].set(maxPos.x, maxPos.y, maxPos.z);
out->vertices[6].set(maxPos.x, minPos.y, maxPos.z);
out->vertices[7].set(minPos.x, minPos.y, maxPos.z);
if (out->getType() != ShapeEnum::SHAPE_FRUSTUM_ACCURATE) {
return out;
}
out->vertices[0].set(maxPos.x, maxPos.y, -minPos.z);
out->vertices[1].set(minPos.x, maxPos.y, -minPos.z);
out->vertices[2].set(minPos.x, minPos.y, -minPos.z);
out->vertices[3].set(maxPos.x, minPos.y, -minPos.z);
out->vertices[4].set(maxPos.x, maxPos.y, -maxPos.z);
out->vertices[5].set(minPos.x, maxPos.y, -maxPos.z);
out->vertices[6].set(minPos.x, minPos.y, -maxPos.z);
out->vertices[7].set(maxPos.x, minPos.y, -maxPos.z);
out->updatePlanes();
return out;
}
void Frustum::createPerspective(Frustum *out, float fov,
float aspect,
float near,
float far,
const Mat4 &transform) {
const float h = tanf(fov * 0.5F);
const float w = h * aspect;
const Vec3 nearTemp(near * w, near * h, near);
const Vec3 farTemp(far * w, far * h, far);
out->vertices[0].transformMat4(Vec3(nearTemp.x, nearTemp.y, -nearTemp.z), transform);
out->vertices[1].transformMat4(Vec3(-nearTemp.x, nearTemp.y, -nearTemp.z), transform);
out->vertices[2].transformMat4(Vec3(-nearTemp.x, -nearTemp.y, -nearTemp.z), transform);
out->vertices[3].transformMat4(Vec3(nearTemp.x, -nearTemp.y, -nearTemp.z), transform);
out->vertices[4].transformMat4(Vec3(farTemp.x, farTemp.y, -farTemp.z), transform);
out->vertices[5].transformMat4(Vec3(-farTemp.x, farTemp.y, -farTemp.z), transform);
out->vertices[6].transformMat4(Vec3(-farTemp.x, -farTemp.y, -farTemp.z), transform);
out->vertices[7].transformMat4(Vec3(farTemp.x, -farTemp.y, -farTemp.z), transform);
out->updatePlanes();
}
void Frustum::update(const Mat4 &m, const Mat4 &inv) {
// left plane
planes[0]->n.set(m.m[3] + m.m[0], m.m[7] + m.m[4], m.m[11] + m.m[8]);
@@ -108,10 +129,6 @@ void Frustum::update(const Mat4 &m, const Mat4 &inv) {
planes[5]->n.set(m.m[3] - m.m[2], m.m[7] - m.m[6], m.m[11] - m.m[10]);
planes[5]->d = -(m.m[15] - m.m[14]);
if (getType() != ShapeEnum::SHAPE_FRUSTUM_ACCURATE) {
return;
}
for (Plane *plane : planes) {
float invDist = 1 / plane->n.length();
plane->n *= invDist;
@@ -125,40 +142,22 @@ void Frustum::update(const Mat4 &m, const Mat4 &inv) {
}
void Frustum::transform(const Mat4 &mat) {
if (getType() != ShapeEnum::SHAPE_FRUSTUM_ACCURATE) {
return;
}
for (auto i = 0; i < 8; i++) {
vertices[i].transformMat4(vertices[i], mat);
}
Plane::fromPoints(planes[0], vertices[1], vertices[6], vertices[5]);
Plane::fromPoints(planes[1], vertices[3], vertices[4], vertices[7]);
Plane::fromPoints(planes[2], vertices[6], vertices[3], vertices[7]);
Plane::fromPoints(planes[3], vertices[0], vertices[5], vertices[4]);
Plane::fromPoints(planes[4], vertices[2], vertices[0], vertices[3]);
Plane::fromPoints(planes[5], vertices[7], vertices[5], vertices[6]);
updatePlanes();
}
void Frustum::createOrthographic(float width, float height, float near, float far, const Mat4 &transform) {
createOrthographic(this, width, height, near, far, transform);
}
void Frustum::createOrtho(const float width, const float height, const float near, const float far, const Mat4 &transform) {
createOrtho(this, width, height, near, far, transform);
createOrthographic(width, height, near, far, transform);
}
void Frustum::split(float start, float end, float aspect, float fov, const Mat4 &transform) {
const float h = tanf(fov * 0.5F);
const float w = h * aspect;
const Vec3 nearTemp(start * w, start * h, start);
const Vec3 farTemp(end * w, end * h, end);
vertices[0].transformMat4(Vec3(nearTemp.x, nearTemp.y, nearTemp.z), transform);
vertices[1].transformMat4(Vec3(-nearTemp.x, nearTemp.y, nearTemp.z), transform);
vertices[2].transformMat4(Vec3(-nearTemp.x, -nearTemp.y, nearTemp.z), transform);
vertices[3].transformMat4(Vec3(nearTemp.x, -nearTemp.y, nearTemp.z), transform);
vertices[4].transformMat4(Vec3(farTemp.x, farTemp.y, farTemp.z), transform);
vertices[5].transformMat4(Vec3(-farTemp.x, farTemp.y, farTemp.z), transform);
vertices[6].transformMat4(Vec3(-farTemp.x, -farTemp.y, farTemp.z), transform);
vertices[7].transformMat4(Vec3(farTemp.x, -farTemp.y, farTemp.z), transform);
updatePlanes();
void Frustum::split(float near, float far, float aspect, float fov, const Mat4 &transform) {
createPerspective(this, fov, aspect, near, far, transform);
}
void Frustum::updatePlanes() {
+60 -3
View File
@@ -42,25 +42,65 @@ public:
* Create a ortho frustum.
* @zh
* 创建一个正交视锥体。
* @param out @en The result orthogonal frustum. @zh 输出的正交视锥体。
* @param out @en The result orthographic frustum. @zh 输出的正交视锥体。
* @param width @en The width of the frustum. @zh 正交视锥体的宽度。
* @param height @en The height of the frustum. @zh 正交视锥体的高度。
* @param near @en The near plane of the frustum. @zh 正交视锥体的近平面值。
* @param far @en The far plane of the frustum. @zh 正交视锥体的远平面值。
* @param transform @en The transform matrix of the frustum. @zh 正交视锥体的变换矩阵。
* @return @en The out object @zh 返回正交视锥体.
*
* @deprecated since 3.8.0, please use [createOrthographic] instead of it.
*/
static void createOrtho(Frustum *out, float width,
float height,
float near,
float far,
const Mat4 &transform);
/**
* @en
* Create a ortho frustum.
* @zh
* 创建一个正交视锥体。
* @param out @en The result orthographic frustum. @zh 输出的正交视锥体。
* @param width @en The width of the frustum. @zh 正交视锥体的宽度。
* @param height @en The height of the frustum. @zh 正交视锥体的高度。
* @param near @en The near plane of the frustum. @zh 正交视锥体的近平面值。
* @param far @en The far plane of the frustum. @zh 正交视锥体的远平面值。
* @param transform @en The transform matrix of the frustum. @zh 正交视锥体的变换矩阵。
*/
static void createOrthographic(Frustum *out, float width,
float height,
float near,
float far,
const Mat4 &transform);
/**
* @en
* Create a perspective frustum.
* @zh
* 创建一个透视视锥体。
* @param out @en The result perspective frustum. @zh 输出的透视视锥体。
* @param fov @en The field of view of the frustum. @zh 视锥体的视野。
* @param aspect @en The aspect ratio of the frustum. @zh 视锥体的宽高比。
* @param near @en The near plane of the frustum. @zh 视锥体的近平面值。
* @param far @en The far plane of the frustum. @zh 视锥体的远平面值。
* @param transform @en The transform matrix of the frustum. @zh 视锥体的变换矩阵。
*/
static void createPerspective(Frustum *out, float fov,
float aspect,
float near,
float far,
const Mat4 &transform);
/**
* @en Create a frustum from an AABB box.
* @zh 从 AABB 包围盒中创建一个视锥体。
* @param out @en The result frustum @zh 输出的视锥体对象。
* @param aabb @en The AABB bounding box of the frustum @zh AABB 包围盒。
* @return @en The out object @zh 返回视锥体.
*
* @deprecated since 3.8.0, please use [createOrthographic] instead of it.
*/
static Frustum *createFromAABB(Frustum *out, const AABB &aabb);
@@ -125,7 +165,22 @@ public:
void transform(const Mat4 &);
void createOrtho(float width, float height, float near, float far, const Mat4 &transform);
void split(float start, float end, float aspect, float fov, const Mat4 &transform);
void createOrthographic(float width, float height, float near, float far, const Mat4 &transform);
/**
* @en
* Set as a perspective frustum.
* @zh
* 设置为一个透视视锥体。
* @param near @en The near plane of the frustum. @zh 视锥体的近平面值。
* @param far @en The far plane of the frustum. @zh 视锥体的远平面值。
* @param fov @en The field of view of the frustum. @zh 视锥体的视野。
* @param aspect @en The aspect ratio of the frustum. @zh 视锥体的宽高比。
* @param transform @en The transform matrix of the frustum. @zh 视锥体的变换矩阵。
*
* @deprecated since 3.8.0, please use [createPerspective] instead of it.
*/
void split(float near, float far, float aspect, float fov, const Mat4 &transform);
void updatePlanes();
void update(const Mat4 &m, const Mat4 &inv);
@@ -134,6 +189,8 @@ public:
* Set whether to use accurate intersection testing function on this frustum.
* @zh
* 设置是否在此截锥体上使用精确的相交测试函数。
*
* @deprecated since v3.8.0 no need to set accurate flag since it doesn't affect the calculation at all.
*/
inline void setAccurate(bool accurate) {
setType(accurate ? ShapeEnum::SHAPE_FRUSTUM_ACCURATE : ShapeEnum::SHAPE_FRUSTUM);
+52 -59
View File
@@ -26,6 +26,7 @@
#include <cstring>
#include "base/Log.h"
#include "base/std/container/array.h"
#include "math/Math.h"
#include "math/MathUtil.h"
#include "math/Quaternion.h"
@@ -227,25 +228,6 @@ void Mat4::createBillboardHelper(const Vec3 &objectPosition, const Vec3 &cameraP
}
}
// void Mat4::createReflection(const Plane& plane, Mat4* dst)
// {
// Vec3 normal(plane.getNormal());
// float k = -2.0f * plane.getDistance();
// dst->setIdentity();
// dst->m[0] -= 2.0f * normal.x * normal.x;
// dst->m[5] -= 2.0f * normal.y * normal.y;
// dst->m[10] -= 2.0f * normal.z * normal.z;
// dst->m[1] = dst->m[4] = -2.0f * normal.x * normal.y;
// dst->m[2] = dst->m[8] = -2.0f * normal.x * normal.z;
// dst->m[6] = dst->m[9] = -2.0f * normal.y * normal.z;
// dst->m[3] = k * normal.x;
// dst->m[7] = k * normal.y;
// dst->m[11] = k * normal.z;
// }
void Mat4::createScale(const Vec3 &scale, Mat4 *dst) {
CC_ASSERT(dst);
@@ -313,18 +295,18 @@ void Mat4::createRotation(const Vec3 &axis, float angle, Mat4 *dst) {
// Make sure the input axis is normalized.
float n = x * x + y * y + z * z;
if (n != 1.0F) {
// Not normalized.
n = std::sqrt(n);
// Prevent divide too close to zero.
if (n > 0.000001F) {
n = 1.0F / n;
x *= n;
y *= n;
z *= n;
}
n = std::sqrt(n);
// do nothing for invalid axis
if (n < math::EPSILON) {
return;
}
n = 1.0F / n;
x *= n;
y *= n;
z *= n;
float c = std::cos(angle);
float s = std::sin(angle);
@@ -802,28 +784,29 @@ bool Mat4::inverse() {
}
// Support the case where m == dst.
det = 1.0F / det;
Mat4 inverse;
inverse.m[0] = m[5] * b5 - m[6] * b4 + m[7] * b3;
inverse.m[1] = -m[1] * b5 + m[2] * b4 - m[3] * b3;
inverse.m[2] = m[13] * a5 - m[14] * a4 + m[15] * a3;
inverse.m[3] = -m[9] * a5 + m[10] * a4 - m[11] * a3;
inverse.m[0] = (m[5] * b5 - m[6] * b4 + m[7] * b3) * det;
inverse.m[1] = (-m[1] * b5 + m[2] * b4 - m[3] * b3) * det;
inverse.m[2] = (m[13] * a5 - m[14] * a4 + m[15] * a3) * det;
inverse.m[3] = (-m[9] * a5 + m[10] * a4 - m[11] * a3) * det;
inverse.m[4] = -m[4] * b5 + m[6] * b2 - m[7] * b1;
inverse.m[5] = m[0] * b5 - m[2] * b2 + m[3] * b1;
inverse.m[6] = -m[12] * a5 + m[14] * a2 - m[15] * a1;
inverse.m[7] = m[8] * a5 - m[10] * a2 + m[11] * a1;
inverse.m[4] = (-m[4] * b5 + m[6] * b2 - m[7] * b1) * det;
inverse.m[5] = (m[0] * b5 - m[2] * b2 + m[3] * b1) * det;
inverse.m[6] = (-m[12] * a5 + m[14] * a2 - m[15] * a1) * det;
inverse.m[7] = (m[8] * a5 - m[10] * a2 + m[11] * a1) * det;
inverse.m[8] = m[4] * b4 - m[5] * b2 + m[7] * b0;
inverse.m[9] = -m[0] * b4 + m[1] * b2 - m[3] * b0;
inverse.m[10] = m[12] * a4 - m[13] * a2 + m[15] * a0;
inverse.m[11] = -m[8] * a4 + m[9] * a2 - m[11] * a0;
inverse.m[8] = (m[4] * b4 - m[5] * b2 + m[7] * b0) * det;
inverse.m[9] = (-m[0] * b4 + m[1] * b2 - m[3] * b0) * det;
inverse.m[10] = (m[12] * a4 - m[13] * a2 + m[15] * a0) * det;
inverse.m[11] = (-m[8] * a4 + m[9] * a2 - m[11] * a0) * det;
inverse.m[12] = -m[4] * b3 + m[5] * b1 - m[6] * b0;
inverse.m[13] = m[0] * b3 - m[1] * b1 + m[2] * b0;
inverse.m[14] = -m[12] * a3 + m[13] * a1 - m[14] * a0;
inverse.m[15] = m[8] * a3 - m[9] * a1 + m[10] * a0;
inverse.m[12] = (-m[4] * b3 + m[5] * b1 - m[6] * b0) * det;
inverse.m[13] = (m[0] * b3 - m[1] * b1 + m[2] * b0) * det;
inverse.m[14] = (-m[12] * a3 + m[13] * a1 - m[14] * a0) * det;
inverse.m[15] = (m[8] * a3 - m[9] * a1 + m[10] * a0) * det;
multiply(inverse, 1.0F / det, this);
*this = inverse;
return true;
}
@@ -957,20 +940,23 @@ void Mat4::set(float m00, float m01, float m02, float m03,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33) {
m[0] = m00;
m[1] = m10;
m[2] = m20;
m[3] = m30;
m[4] = m01;
m[1] = m01;
m[2] = m02;
m[3] = m03;
m[4] = m10;
m[5] = m11;
m[6] = m21;
m[7] = m31;
m[8] = m02;
m[9] = m12;
m[6] = m12;
m[7] = m13;
m[8] = m20;
m[9] = m21;
m[10] = m22;
m[11] = m32;
m[12] = m03;
m[13] = m13;
m[14] = m23;
m[11] = m23;
m[12] = m30;
m[13] = m31;
m[14] = m32;
m[15] = m33;
}
@@ -1051,7 +1037,14 @@ Mat4 Mat4::getTransposed() const {
}
bool Mat4::approxEquals(const Mat4 &v, float precision /* = CC_FLOAT_CMP_PRECISION */) const {
return math::isEqualF(m[0], v.m[0], precision) && math::isEqualF(m[1], v.m[1], precision) && math::isEqualF(m[2], v.m[2], precision) && math::isEqualF(m[3], v.m[3], precision) && math::isEqualF(m[4], v.m[4], precision) && math::isEqualF(m[5], v.m[5], precision) && math::isEqualF(m[6], v.m[6], precision) && math::isEqualF(m[7], v.m[7], precision) && math::isEqualF(m[8], v.m[8], precision) && math::isEqualF(m[9], v.m[9], precision) && math::isEqualF(m[10], v.m[10], precision) && math::isEqualF(m[11], v.m[11], precision) && math::isEqualF(m[12], v.m[12], precision) && math::isEqualF(m[13], v.m[13], precision) && math::isEqualF(m[14], v.m[14], precision) && math::isEqualF(m[15], v.m[15], precision);
return math::isEqualF(m[0], v.m[0], precision) && math::isEqualF(m[1], v.m[1], precision) &&
math::isEqualF(m[2], v.m[2], precision) && math::isEqualF(m[3], v.m[3], precision) &&
math::isEqualF(m[4], v.m[4], precision) && math::isEqualF(m[5], v.m[5], precision) &&
math::isEqualF(m[6], v.m[6], precision) && math::isEqualF(m[7], v.m[7], precision) &&
math::isEqualF(m[8], v.m[8], precision) && math::isEqualF(m[9], v.m[9], precision) &&
math::isEqualF(m[10], v.m[10], precision) && math::isEqualF(m[11], v.m[11], precision) &&
math::isEqualF(m[12], v.m[12], precision) && math::isEqualF(m[13], v.m[13], precision) &&
math::isEqualF(m[14], v.m[14], precision) && math::isEqualF(m[15], v.m[15], precision);
}
const Mat4 Mat4::IDENTITY = Mat4(
+35 -43
View File
@@ -117,22 +117,22 @@ public:
/**
* Constructs a matrix initialized to the specified value.
*
* @param m00 The first element of the first row.
* @param m01 The second element of the first row.
* @param m02 The third element of the first row.
* @param m03 The fourth element of the first row.
* @param m10 The first element of the second row.
* @param m11 The second element of the second row.
* @param m12 The third element of the second row.
* @param m13 The fourth element of the second row.
* @param m20 The first element of the third row.
* @param m21 The second element of the third row.
* @param m22 The third element of the third row.
* @param m23 The fourth element of the third row.
* @param m30 The first element of the fourth row.
* @param m31 The second element of the fourth row.
* @param m32 The third element of the fourth row.
* @param m33 The fourth element of the fourth row.
* @param m00 Component in column 0, row 0 position (index 0)
* @param m01 Component in column 0, row 1 position (index 1)
* @param m02 Component in column 0, row 2 position (index 2)
* @param m03 Component in column 0, row 3 position (index 3)
* @param m10 Component in column 1, row 0 position (index 4)
* @param m11 Component in column 1, row 1 position (index 5)
* @param m12 Component in column 1, row 2 position (index 6)
* @param m13 Component in column 1, row 3 position (index 7)
* @param m20 Component in column 2, row 0 position (index 8)
* @param m21 Component in column 2, row 1 position (index 9)
* @param m22 Component in column 2, row 2 position (index 10)
* @param m23 Component in column 2, row 3 position (index 11)
* @param m30 Component in column 3, row 0 position (index 12)
* @param m31 Component in column 3, row 1 position (index 13)
* @param m32 Component in column 3, row 2 position (index 14)
* @param m33 Component in column 3, row 3 position (index 15)
*/
Mat4(float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
@@ -215,17 +215,6 @@ public:
bool isFieldOfViewY = false, float minClipZ = -1, float projectionSignY = 1,
int orientation = 0, Mat4 *dst = nullptr);
/**
* Creates an orthographic projection matrix.
*
* @param width The width of the view.
* @param height The height of the view.
* @param zNearPlane The minimum z-value of the view volume.
* @param zFarPlane The maximum z-value of the view volume.
* @param dst A matrix to store the result in.
*/
static void createOrthographic(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane, Mat4 *dst);
/**
* Creates an orthographic projection matrix.
*
@@ -254,8 +243,11 @@ public:
* @param zFarPlane The maximum z-value of the view volume.
* @param dst A matrix to store the result in.
*/
static void createOrthographic(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane, Mat4 *dst);
static void createOrthographicOffCenter(float left, float right, float bottom, float top,
float zNearPlane, float zFarPlane, Mat4 *dst);
static void createOrthographicOffCenter(float left, float right, float bottom, float top,
float zNearPlane, float zFarPlane, float minClipZ,
float projectionSignY, int orientation, Mat4 *dst);
@@ -732,22 +724,22 @@ public:
/**
* Sets the values of this matrix.
*
* @param m00 The first element of the first row.
* @param m01 The second element of the first row.
* @param m02 The third element of the first row.
* @param m03 The fourth element of the first row.
* @param m10 The first element of the second row.
* @param m11 The second element of the second row.
* @param m12 The third element of the second row.
* @param m13 The fourth element of the second row.
* @param m20 The first element of the third row.
* @param m21 The second element of the third row.
* @param m22 The third element of the third row.
* @param m23 The fourth element of the third row.
* @param m30 The first element of the fourth row.
* @param m31 The second element of the fourth row.
* @param m32 The third element of the fourth row.
* @param m33 The fourth element of the fourth row.
* @param m00 Component in column 0, row 0 position (index 0)
* @param m01 Component in column 0, row 1 position (index 1)
* @param m02 Component in column 0, row 2 position (index 2)
* @param m03 Component in column 0, row 3 position (index 3)
* @param m10 Component in column 1, row 0 position (index 4)
* @param m11 Component in column 1, row 1 position (index 5)
* @param m12 Component in column 1, row 2 position (index 6)
* @param m13 Component in column 1, row 3 position (index 7)
* @param m20 Component in column 2, row 0 position (index 8)
* @param m21 Component in column 2, row 1 position (index 9)
* @param m22 Component in column 2, row 2 position (index 10)
* @param m23 Component in column 2, row 3 position (index 11)
* @param m30 Component in column 3, row 0 position (index 12)
* @param m31 Component in column 3, row 1 position (index 13)
* @param m32 Component in column 3, row 2 position (index 14)
* @param m33 Component in column 3, row 3 position (index 15)
*/
void set(float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
+7 -5
View File
@@ -66,15 +66,17 @@ inline bool isPowerOfTwo(T n) {
}
inline bool isEqualF(float lhs, float rhs, float precision = 0.000001F) {
return (std::fabs(lhs - rhs) <= precision);
const bool hasInf = std::isinf(lhs) || std::isinf(rhs);
return !hasInf && (std::fabs(lhs - rhs) <= (std::fmax(std::fmax(std::fabs(lhs), std::fabs(rhs)), 1.0F) * precision));
}
inline bool isNotEqualF(float lhs, float rhs, float precision = 0.000001F) {
return (std::fabs(lhs - rhs) > precision);
inline bool isNotEqualF(float lhs, float rhs, float precision = 0.000001F) { // same as !isEqualF
const bool hasInf = std::isinf(lhs) || std::isinf(rhs);
return hasInf || (std::fabs(lhs - rhs) > (std::fmax(std::fmax(std::fabs(lhs), std::fabs(rhs)), 1.0F) * precision));
}
inline bool isNotZeroF(float v, float precision = 0.000001F) {
return (std::fabs(v) > precision);
inline bool isNotZeroF(float v, float precision = 0.000001F) { // same as isNotEqualF(v, 0.0F)
return std::isinf(v) || (std::fabs(v) > (std::fmax(std::fabs(v), 1.0F) * precision));
}
} // namespace math
+2 -2
View File
@@ -182,14 +182,14 @@ inline void MathUtilC::transposeMatrix(const float* m, float* dst)
m[2], m[6], m[10], m[14],
m[3], m[7], m[11], m[15]
};
memcpy(dst, t, MATRIX_SIZE);
}
memcpy(dst, t, MATRIX_SIZE);}
inline void MathUtilC::transformVec4(const float* m, float x, float y, float z, float w, float* dst)
{
dst[0] = x * m[0] + y * m[4] + z * m[8] + w * m[12];
dst[1] = x * m[1] + y * m[5] + z * m[9] + w * m[13];
dst[2] = x * m[2] + y * m[6] + z * m[10] + w * m[14];
dst[3] = x * m[3] + y * m[7] + z * m[11] + w * m[15];
}
inline void MathUtilC::transformVec4(const float* m, const float* v, float* dst)
+2 -1
View File
@@ -49,7 +49,7 @@ constexpr auto HALF_TO_RAD = 0.5 * D2R;
template <typename F>
bool equals(F a, F b) {
static_assert(std::is_floating_point<F>::value, "number expected");
return std::fabs(a - b) <= EPSILON * std::max(1.0, std::fabs(a), std::fabs(b));
return std::fabs(a - b) <= EPSILON * std::max(1.0F, std::max(std::fabs(a), std::fabs(b)));
}
/**
@@ -215,6 +215,7 @@ auto pseudoRandomRangeInt(In seed, In min, In max) {
*/
template <typename T>
auto nextPow2(T val) {
// ref: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
--val;
val = (val >> 1) | val;
val = (val >> 2) | val;
+20 -4
View File
@@ -78,11 +78,12 @@ bool Vec4::isOne() const {
}
float Vec4::angle(const Vec4 &v1, const Vec4 &v2) {
float dx = v1.w * v2.x - v1.x * v2.w - v1.y * v2.z + v1.z * v2.y;
float dy = v1.w * v2.y - v1.y * v2.w - v1.z * v2.x + v1.x * v2.z;
float dz = v1.w * v2.z - v1.z * v2.w - v1.x * v2.y + v1.y * v2.x;
const float dx = (v1.y * v2.z - v1.z * v2.y);
const float dy = (v1.z * v2.x - v1.x * v2.z);
const float dz = (v1.x * v2.y - v1.y * v2.x);
const float dotVal = (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
return std::atan2(std::sqrt(dx * dx + dy * dy + dz * dz) + MATH_FLOAT_SMALL, dot(v1, v2));
return std::atan2(std::sqrt(dx * dx + dy * dy + dz * dz) + MATH_FLOAT_SMALL, dotVal);
}
void Vec4::add(const Vec4 &v) {
@@ -219,6 +220,21 @@ void Vec4::negate() {
w = -w;
}
/**
* The inverse value of this vector.
*
* This method set each component to its inverse value, zero
* will become infinity.
*/
void Vec4::inverse(const Vec4 &v, Vec4 *dst) {
CC_ASSERT(dst);
dst->x = 1.0F / v.x;
dst->y = 1.0F / v.y;
dst->z = 1.0F / v.z;
dst->w = 1.0F / v.w;
}
void Vec4::normalize() {
float n = x * x + y * y + z * z + w * w;
// Already normalized.
+8
View File
@@ -256,6 +256,14 @@ public:
*/
void negate();
/**
* The inverse value of this vector.
*
* This method set each component to its inverse value, zero
* will become infinity.
*/
static void inverse(const Vec4 &v, Vec4 *dst);
/**
* Normalizes this vector.
*
@@ -283,9 +283,6 @@ Mat4 CSMLayers::getCameraWorldMatrix(const scene::Camera *camera) {
Mat4 out;
Mat4::fromRT(rotation, position, &out);
out.m[8] *= -1.0F;
out.m[9] *= -1.0F;
out.m[10] *= -1.0F;
return out;
}
@@ -0,0 +1,48 @@
/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
****************************************************************************/
#include <math.h>
#include <vector>
#include "cocos/math/Color.h"
#include "cocos/math/Math.h"
#include "gtest/gtest.h"
#include "utils.h"
TEST(mathColorTest, test) {
logLabel = "test the Color set function";
cc::Color color;
uint32_t val = 0x12345678;
color.set(val);
EXPECT_EQ(color.a, 0x12);
EXPECT_EQ(color.b, 0x34);
EXPECT_EQ(color.g, 0x56);
EXPECT_EQ(color.r, 0x78);
logLabel = "test the color to Vec4 function";
color.set(0xff, 0xff, 0x00, 0xff);
cc::Vec4 vec4 = color.toVec4();
EXPECT_EQ(vec4.x, 1.0F);
EXPECT_EQ(vec4.y, 1.0F);
EXPECT_EQ(vec4.z, 0.0F);
EXPECT_EQ(vec4.w, 1.0F);
}
+24 -7
View File
@@ -120,6 +120,11 @@ TEST(mathMat4Test, test5) {
cc::Vec3 transVec(1, 1, 2);
cc::Mat4::fromRT(rotVec, transVec, &outMat);
ExpectEq(outMat.m[5] == -3 && outMat.m[9] == -2 && outMat.m[14] == 2, true);
// fromRTS
logLabel = "test the mat4 fromRTS function";
cc::Vec3 scaleVec(1, 1, 1);
cc::Mat4::fromRTS(rotVec, transVec, scaleVec, &outMat);
ExpectEq(outMat.m[5] == -3 && outMat.m[9] == -2 && outMat.m[14] == 2, true);
// decompose
logLabel = "test the mat4 decompose function";
outMat.decompose(&scale, &rot, &transVec);
@@ -186,12 +191,19 @@ TEST(mathMat4Test, test5) {
ExpectEq(isInverse == true, true);
// getInversed
logLabel = "test the mat4 getInversed function";
cc::Mat4 inversed;
inversed.m[1] = 10;
inversed.m[2] = 13;
inversed.m[4] = 2;
cc::Mat4 inversed(
1, 1, 1, 3,
5, 4, 1, 7,
9, 3, 2, 1,
4, 4, 3, 1);
cc::Mat4 inversedMat = inversed.getInversed();
ExpectEq(IsEqualF(inversedMat.m[6], -1.36842108), true);
auto inversedMat2 = cc::Mat4(
4.0F / 165, -1.0F / 55, 28.0F / 165, -19.0F / 165,
-41.0F / 55, 17.0F / 55, -12.0F / 55, 16.0F / 55,
13.0F / 15, -2.0F / 5, 1.0F / 15, 2.0F / 15,
47.0F / 165, 2.0F / 55, -1.0F / 165, -17.0F / 165);
ExpectEq(inversedMat.approxEquals(inversedMat2), true);
// identity
logLabel = "test the mat4 isIdentity function";
ExpectEq(outMat.isIdentity(), false);
@@ -257,9 +269,14 @@ TEST(mathMat4Test, test5) {
ExpectEq(translate.m[12] = 2 && translate.m[13] == 6, true);
// transpose
logLabel = "test the mat4 transpose function";
cc::Mat4 matTranspose(11, 21, 31, 2, 12, 22, 32, 4, 13, 23, 33, 5, 0, 0, 0, 1);
cc::Mat4 matTranspose(
11, 21, 31, 2,
12, 22, 32, 4,
13, 23, 33, 5,
0, 0, 0, 1
);
matTranspose.transpose();
ExpectEq(matTranspose.m[1] == 21 && matTranspose.m[4] == 12 && matTranspose.m[7] == 4, true);
ExpectEq(matTranspose.m[1] == 12 && matTranspose.m[4] == 21 && matTranspose.m[7] == 0, true);
// approxEqual
logLabel = "test the mat4 approx equal function";
+72 -5
View File
@@ -21,13 +21,14 @@ 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.
****************************************************************************/
#include "gtest/gtest.h"
#include "cocos/math/Vec2.h"
#include "cocos/math/Math.h"
#include "cocos/math/MathUtil.h"
#include "utils.h"
#include <math.h>
#include <vector>
#include "cocos/math/Math.h"
#include "cocos/math/MathUtil.h"
#include "cocos/math/Utils.h"
#include "cocos/math/Vec2.h"
#include "gtest/gtest.h"
#include "utils.h"
TEST(mathUtilsTest, test9) {
// smooth
@@ -41,4 +42,70 @@ TEST(mathUtilsTest, test9) {
logLabel = "test the MathUtil lerp function";
float res = cc::MathUtil::lerp(2, 15, 0.8);
ExpectEq(IsEqualF(res, 12.3999996), true);
// equals
logLabel = "test the MathUtil equals function";
bool res1 = cc::mathutils::equals(2.0F, 2.0000001F);
ExpectEq(res1, true);
// approx
logLabel = "test the MathUtil approx function";
bool res2 = cc::mathutils::approx(2.0F, 2.0000001F);
ExpectEq(res2, true);
bool res3 = cc::mathutils::approx(2.0F, 2.0000001F, 0.000001F);
ExpectEq(res3, true);
// clamp
logLabel = "test the MathUtil clamp function";
float res4 = cc::mathutils::clamp(2.0F, 1.0F, 3.0F);
ExpectEq(IsEqualF(res4, 2.0F), true);
float res5 = cc::mathutils::clamp01(2.0F);
ExpectEq(IsEqualF(res5, 1.0F), true);
// lerp
logLabel = "test the MathUtil lerp function";
float res6 = cc::mathutils::lerp(2.0F, 3.0F, 0.5F);
ExpectEq(IsEqualF(res6, 2.5F), true);
// toRadian
logLabel = "test the MathUtil toRadian function";
float res7 = cc::mathutils::toRadian(180.0F);
ExpectEq(cc::mathutils::equals(res7, 3.14159274F), true);
// toDegree
logLabel = "test the MathUtil toDegree function";
float res8 = cc::mathutils::toDegree(3.14159274F);
ExpectEq(cc::mathutils::equals(res8, 180.0F), true);
// nextPow2
logLabel = "test the MathUtil nextPow2 function";
ExpectEq(IsEqualF(cc::mathutils::nextPow2(0), 0), true);
ExpectEq(IsEqualF(cc::mathutils::nextPow2(1), 1), true);
ExpectEq(IsEqualF(cc::mathutils::nextPow2(2), 2), true);
ExpectEq(IsEqualF(cc::mathutils::nextPow2(3), 4), true);
ExpectEq(IsEqualF(cc::mathutils::nextPow2(4), 4), true);
ExpectEq(IsEqualF(cc::mathutils::nextPow2(5), 8), true);
ExpectEq(IsEqualF(cc::mathutils::nextPow2(31), 32), true);
// repeat
logLabel = "test the MathUtil repeat function";
ExpectEq(IsEqualF(cc::mathutils::repeat(0.0F, 1.0F), 0.0F), true);
ExpectEq(IsEqualF(cc::mathutils::repeat(1.0F, 1.0F), 0.0F), true);
ExpectEq(IsEqualF(cc::mathutils::repeat(2.0F, 1.0F), 0.0F), true);
ExpectEq(IsEqualF(cc::mathutils::repeat(3.0F, 1.0F), 0.0F), true);
// pingPong
logLabel = "test the MathUtil pingPong function";
ExpectEq(IsEqualF(cc::mathutils::pingPong(0.0F, 1.0F), 0.0F), true);
ExpectEq(IsEqualF(cc::mathutils::pingPong(1.0F, 1.0F), 1.0F), true);
ExpectEq(IsEqualF(cc::mathutils::pingPong(2.0F, 1.0F), 0.0F), true);
ExpectEq(IsEqualF(cc::mathutils::pingPong(0.5F, 1.0F), 0.5F), true);
ExpectEq(IsEqualF(cc::mathutils::pingPong(.5F, .5F), 0.5F), true);
// inverseLerp
logLabel = "test the MathUtil inverseLerp function";
ExpectEq(IsEqualF(cc::mathutils::inverseLerp(0.0F, 1.0F, 0.0F), 0.0F), true);
ExpectEq(IsEqualF(cc::mathutils::inverseLerp(0.0F, 1.0F, 1.0F), 1.0F), true);
ExpectEq(IsEqualF(cc::mathutils::inverseLerp(0.0F, 1.0F, 0.5F), 0.5F), true);
// absMaxComponent
logLabel = "test the MathUtil absMaxComponent function";
ExpectEq(IsEqualF(cc::mathutils::absMaxComponent(cc::Vec3(1, 2, 3)), 3), true);
ExpectEq(IsEqualF(cc::mathutils::absMaxComponent(cc::Vec3(1, 2, -3)), -3), true);
ExpectEq(IsEqualF(cc::mathutils::absMaxComponent(cc::Vec3(1, -2, 3)), 3), true);
// absMax
logLabel = "test the MathUtil absMax function";
ExpectEq(IsEqualF(cc::mathutils::absMax(1.0F, 3.0F), 3.0F), true);
ExpectEq(IsEqualF(cc::mathutils::absMax(-1.0F, 3.0F), 3.0F), true);
ExpectEq(IsEqualF(cc::mathutils::absMax(1.0F, -3.0F), -3.0F), true);
}
@@ -81,6 +81,21 @@ TEST(mathVec4Test, test3) {
logLabel = "test the vec4 length function";
vec4.set(8, 6, 0, 0);
ExpectEq(IsEqualF(vec4.length(), 10), true);
// negate
logLabel = "test the vec4 negate function";
vec4.set(8, 6, 0, 0);
vec4.negate();
ExpectEq(vec4.approxEquals(cc::Vec4(-8, -6, 0, 0)), true);
// inverse
logLabel = "test the vec4 inverse function";
vec4.set(8, 6, 1, 1);
cc::Vec4::inverse(vec4, &vec4);
ExpectEq(vec4.approxEquals(cc::Vec4(0.125F, 0.16666667F, 1.0F, 1.0F)), true);
// scale
logLabel = "test the vec4 scale function";
vec4.set(8, 6, 0, 0);
vec4.scale(2);
ExpectEq(vec4.approxEquals(cc::Vec4(16, 12, 0, 0)), true);
// normalize
logLabel = "test whether vec4 is normalized";
vec4.normalize();
@@ -101,6 +116,11 @@ TEST(mathVec4Test, test3) {
ExpectEq(vec4 == cc::Vec4(3, 3, 0, 0), true);
cc::Vec4::subtract(cc::Vec4(4, 5, 0, 1), cc::Vec4(1, 2, 0, 1), &vec4);
ExpectEq(vec4 == cc::Vec4(3, 3, 0, 0), true);
// lerp
logLabel = "test whether vec4 lerp function";
vec4.set(4, 5, 0, 1);
cc::Vec4::lerp(vec4, cc::Vec4(1, 2, 0, 1), 0.5F, &vec4);
ExpectEq(vec4 == cc::Vec4(2.5F, 3.5F, 0, 1), true);
// fromColor
logLabel = "test whether vec3 fromColor function";
vec4 = cc::Vec4::fromColor(255);