fix cpp math library (#14458)

* fix cpp math library
This commit is contained in:
李吉林
2023-03-03 17:33:33 +08:00
committed by GitHub
parent 010245457d
commit f4e8b1e03a
32 changed files with 407 additions and 723 deletions
+14 -11
View File
@@ -38,31 +38,31 @@ TEST(mathMat3Test, test4) {
ExpectEq(mat3.m[0] == 1 && mat3.m[4] == 1 && mat3.m[8] == 1, true);
// transpose
logLabel = "test the mat3 transpose function";
mat3.set(11, 21, 31, 12, 22, 32, 13, 23, 33);
mat3.set(11, 12, 13, 21, 22, 23, 31, 32, 33);
mat3.transpose();
ExpectEq(mat3.m[1] == 21 && mat3.m[4] == 22 && mat3.m[7] == 23, true);
cc::Mat3 matTemp(11, 21, 31, 12, 22, 32, 13, 23, 33);
cc::Mat3 matTemp(11, 12, 13, 21, 22, 23, 31, 32, 33);
cc::Mat3::transpose(matTemp, &mat3);
ExpectEq(mat3.m[1] == 21 && mat3.m[4] == 22 && mat3.m[7] == 23, true);
// inverse
logLabel = "test the mat3 inverse function";
mat3.set(0, 2, 0, 3, 0, 0, 2, 0, 1);
mat3.set(0, 3, 2, 2, 0, 0, 0, 0, 1);
mat3.inverse();
ExpectEq(mat3.m[1] == 0.5f && mat3.m[8] == 1, true);
// adjoint
logLabel = "test the mat3 adjoint function";
cc::Mat3 matAdj(0, 2, 0, 3, 0, 0, 2, 0, 1);
cc::Mat3 matAdj(0, 3, 2, 2, 0, 0, 0, 0, 1);
cc::Mat3::adjoint(matAdj, &mat3);
ExpectEq(mat3.m[1] == -3 && mat3.m[3] == -2 && mat3.m[8] == -6, true);
// determinant
logLabel = "test the mat3 determinant function";
mat3.set(1, 0, 3, 2, 1, 0, 4, 0, 2);
mat3.set(1, 2, 4, 0, 1, 0, 3, 0, 2);
float det = mat3.determinant();
ExpectEq(det == -10, true);
// multiply
logLabel = "test the mat3 multiply function";
cc::Mat3 lm(2, 3, 1, 4, 7, 3, 8, 2, 0);
cc::Mat3 rm(5, 2, 6, 8, 1, 0, 6, 5, 4);
cc::Mat3 lm(2, 4, 8, 3, 7, 2, 1, 3, 0);
cc::Mat3 rm(5, 8, 6, 2, 1, 5, 6, 0, 4);
cc::Mat3::multiply(lm, rm, &mat3);
ExpectEq(mat3.m[0] == 40 && mat3.m[4] == 30 && mat3.m[8] == 48, true);
// translate
@@ -105,13 +105,16 @@ TEST(mathMat3Test, test4) {
// fromQuat
logLabel = "test the mat3 fromQuat function";
cc::Mat3 fromQuat;
cc::Quaternion quat(0, 0, 3, 1);
cc::Quaternion quat;
cc::Quaternion::createFromAxisAngle(cc::Vec3(0, 0, 1), M_PI / 2, &quat);
cc::Mat3::fromQuat(quat, &fromQuat);
ExpectEq(copyMat.m[0] == 3 && copyMat.m[3] == 6 && copyMat.m[4] == 7, true);
cc::Vec3 vec3;
vec3.transformMat3(cc::Vec3(1, 1, 0), fromQuat);
ExpectEq(vec3.approxEquals(cc::Vec3(-1, 1, 0)), true);
// add
logLabel = "test the mat3 add function";
cc::Mat3 lAdd(9, 0, 0, 3, 8, 0, 9, 0, 0);
cc::Mat3 rAdd(1, 0, 0, 5, 2, 0, 1, 0, 0);
cc::Mat3 lAdd(9, 3, 9, 0, 8, 0, 0, 0, 0);
cc::Mat3 rAdd(1, 5, 1, 0, 2, 0, 0, 0, 0);
cc::Mat3 addOut;
cc::Mat3::add(lAdd, rAdd, &addOut);
ExpectEq(addOut.m[0] == 10 && addOut.m[2] == 10 && addOut.m[4] == 10, true);
@@ -101,12 +101,31 @@ TEST(mathQuaternionTest, test6) {
ExpectEq(IsEqualF(7.5, quat.x) && IsEqualF(0.5, quat.z), true);
// slerp
logLabel = "test the quaternion slerp function";
q1.set(1, 2, 10, 1);
q2.set(30, 10, 20, 1);
cc::Quaternion::slerp(q1, q2, 0, &quat);
ExpectEq(IsEqualF(1, quat.x) && IsEqualF(10, quat.z), true);
q1.setIdentity();
q2.set(sin(M_PI / 4), 0, 0, cos(M_PI / 4));
cc::Quaternion::slerp(q1, q2, 0.5f, &quat);
ExpectEq(quat.approxEquals(cc::Quaternion(sin(M_PI / 8), 0, 0, cos(M_PI / 8))), true);
logLabel = "test the quaternion approx equal function";
cc::Quaternion a{0.123456F, 1.234567F, 2.345678F, 3.345679F};
cc::Quaternion b{0.123455F, 1.234568F, 2.345679F, 3.345678F};
ExpectEq(a.approxEquals(b), true);
// fromEuler
logLabel = "test the quaternion fromEuler function";
cc::Quaternion::fromEuler(0, 0, 60, &q1);
ExpectEq(q1.approxEquals(cc::Quaternion(0, 0, sin(M_PI / 6), cos(M_PI / 6))), true);
// toEuler
logLabel = "test the quaternion toEuler function";
cc::Vec3 v;
cc::Quaternion::toEuler(q1, false, &v);
ExpectEq(v == cc::Vec3(0, 0, 60), true);
// fromMat3
logLabel = "test the quaternion fromMat3 function";
cc::Mat3 m;
cc::Mat3::fromRotation(M_PI / 3, &m);
cc::Quaternion::fromMat3(m, &q1);
ExpectEq(q1.approxEquals(cc::Quaternion(0, 0, sin(M_PI / 6), cos(M_PI / 6))), true);
// createFromAngleZ
logLabel = "test the quaternion createFromAngleZ function";
cc::Quaternion::createFromAngleZ(60, &q1);
ExpectEq(q1.approxEquals(cc::Quaternion(0, 0, sin(M_PI / 6), cos(M_PI / 6))), true);
}
+18 -2
View File
@@ -51,6 +51,10 @@ TEST(mathVec3Test, test2) {
ExpectEq(IsEqualF(vec3.angle(cc::Vec3(2, 0, 0), cc::Vec3(-2, 0, 0)), M_PI), true);
ExpectEq(IsEqualF(vec3.angle(cc::Vec3(2, 0, 0), cc::Vec3(0, 2, 0)), M_PI * 0.5f), true);
ExpectEq(IsEqualF(vec3.angle(cc::Vec3(2, 0, 0), cc::Vec3(0, -2, 0)), M_PI * 0.5f), true);
// transformInverseRTS
logLabel = "test the vec3 transformInverseRTS function";
cc::Vec3::transformInverseRTS(cc::Vec3(-2, 4, 7), cc::Quaternion(0, 0, sin(M_PI / 4), cos(M_PI / 4)), cc::Vec3(1, 2, 3), cc::Vec3(2, 3, 4), &vec3);
ExpectEq(vec3.approxEquals(cc::Vec3(1, 1, 1)), true);
// add
logLabel = "test the vec3 add function";
vec3.set(3.9, 1.3, 0);
@@ -95,11 +99,23 @@ TEST(mathVec3Test, test2) {
matTranslate.m[14] = 2;
vec3.transformMat4(cc::Vec3(1, 1, 2), matTranslate);
ExpectEq(vec3 == cc::Vec3(3, 2, 4), true);
// transformMat4Normal
logLabel = "test the vec3 transformMat4Normal function";
cc::Mat4 matTransfromNormal;
matTransfromNormal.m[0] = 2;
matTransfromNormal.m[5] = 4;
matTransfromNormal.m[10] = 8;
matTransfromNormal.m[12] = 2;
matTransfromNormal.m[13] = 1;
matTransfromNormal.m[14] = 2;
cc::Vec3::transformMat4Normal(cc::Vec3(1, 1, 2), matTransfromNormal, &vec3);
ExpectEq(vec3 == cc::Vec3(2, 4, 16), true);
// transformQuat
logLabel = "test the vec3 transformQuat function";
cc::Quaternion quaternion(10, 0, 0, 0);
vec3.set(3, 2, 4);
cc::Quaternion quaternion(sin(M_PI / 4), 0, 0, cos(M_PI / 4));
vec3.transformQuat(quaternion);
ExpectEq(vec3 == cc::Vec3(300, -200, -400), true);
ExpectEq(vec3.approxEquals(cc::Vec3(3, -4, 2)), true);
// distance
logLabel = "test the vec3 distance function";
vec3.set(7, 2, 0);
@@ -1,44 +0,0 @@
/****************************************************************************
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/Math.h"
#include "cocos/math/Vec2.h"
#include "cocos/math/Vertex.h"
#include "gtest/gtest.h"
#include "utils.h"
TEST(mathVertexTest, test8) {
// ccVertexLineToPolygon
logLabel = "test the vertex ccVertexLineToPolygon function";
std::vector<cc::Vec2> points{4};
points[0] = {1, 1};
points[1] = {10, 10};
points[2] = {0, -10};
points[3] = {20, 70};
std::vector<cc::Vec2> outP{8};
cc::ccVertexLineToPolygon(points, 1, 0, 4, &outP);
ExpectEq(IsEqualF(outP[5].x, -0.468942523) && IsEqualF(outP[5].y, -9.82652664), true);
// ccVertexLineIntersect
logLabel = "test the vertex ccVertexLineIntersect function";
float res;
bool isFind = cc::ccVertexLineIntersect(5, 5, 1, 10, 12, 20, 12, 40, &res);
ExpectEq(isFind, true);
}