mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 08:42:43 +08:00
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:
@@ -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);
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user