123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- /*
- ---------------------------------------------------------------------------
- Open Asset Import Library (assimp)
- ---------------------------------------------------------------------------
- Copyright (c) 2006-2025, assimp team
- All rights reserved.
- Redistribution and use of this software in source and binary forms,
- with or without modification, are permitted provided that the following
- conditions are met:
- * Redistributions of source code must retain the above
- copyright notice, this list of conditions and the
- following disclaimer.
- * Redistributions in binary form must reproduce the above
- copyright notice, this list of conditions and the
- following disclaimer in the documentation and/or other
- materials provided with the distribution.
- * Neither the name of the assimp team, nor the names of its
- contributors may be used to endorse or promote products
- derived from this software without specific prior
- written permission of the assimp team.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ---------------------------------------------------------------------------
- */
- #include "UnitTestPCH.h"
- #include "MathTest.h"
- #include <assimp/MathFunctions.h>
- #include <array>
- using namespace Assimp;
- class AssimpAPITest_aiMatrix4x4 : public AssimpMathTest {
- protected:
- void SetUp() override {
- result_c = result_cpp = aiMatrix4x4();
- }
- /* Generates a predetermined transformation matrix to use
- for the aiDecompose functions to prevent running into
- division by zero. */
- aiMatrix4x4 get_predetermined_transformation_matrix_for_decomposition() const {
- aiMatrix4x4 t, r;
- aiMatrix4x4::Translation(aiVector3D(14,-25,-8), t);
- aiMatrix4x4::Rotation(Math::aiPi<float>() / 4.0f, aiVector3D(1).Normalize(), r);
- return t * r;
- }
- aiMatrix4x4 result_c, result_cpp;
- };
- TEST_F(AssimpAPITest_aiMatrix4x4, isIdendityTest) {
- aiMatrix4x4 m = aiMatrix4x4(1 + Math::getEpsilon<ai_real>(), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
- const bool result = m.IsIdentity(Math::getEpsilon<ai_real>());
- EXPECT_TRUE(result);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiIdentityMatrix4Test) {
- // Force a non-identity matrix.
- result_c = aiMatrix4x4(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
- aiIdentityMatrix4(&result_c);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4FromMatrix3Test) {
- aiMatrix3x3 m = random_mat3();
- result_cpp = aiMatrix4x4(m);
- aiMatrix4FromMatrix3(&result_c, &m);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4FromScalingQuaternionPositionTest) {
- const aiVector3D s = random_vec3();
- const aiQuaternion q = random_quat();
- const aiVector3D t = random_vec3();
- result_cpp = aiMatrix4x4(s, q, t);
- aiMatrix4FromScalingQuaternionPosition(&result_c, &s, &q, &t);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4AddTest) {
- const aiMatrix4x4 temp = random_mat4();
- result_c = result_cpp = random_mat4();
- result_cpp = result_cpp + temp;
- aiMatrix4Add(&result_c, &temp);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4AreEqualTest) {
- result_c = result_cpp = random_mat4();
- EXPECT_EQ(result_cpp == result_c,
- (bool)aiMatrix4AreEqual(&result_cpp, &result_c));
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4AreEqualEpsilonTest) {
- result_c = result_cpp = random_mat4();
- EXPECT_EQ(result_cpp.Equal(result_c, Epsilon),
- (bool)aiMatrix4AreEqualEpsilon(&result_cpp, &result_c, Epsilon));
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMultiplyMatrix4Test) {
- const auto m = random_mat4();
- result_c = result_cpp = random_mat4();
- result_cpp *= m;
- aiMultiplyMatrix4(&result_c, &m);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiTransposeMatrix4Test) {
- result_c = result_cpp = random_mat4();
- result_cpp.Transpose();
- aiTransposeMatrix4(&result_c);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4InverseTest) {
- // Use a predetermined matrix to prevent arbitrary
- // cases where it could have a null determinant.
- result_c = result_cpp = aiMatrix4x4(
- 6, 10, 15, 3,
- 14, 2, 12, 8,
- 9, 13, 5, 16,
- 4, 7, 11, 1);
- result_cpp.Inverse();
- aiMatrix4Inverse(&result_c);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4DeterminantTest) {
- result_c = result_cpp = random_mat4();
- EXPECT_EQ(result_cpp.Determinant(),
- aiMatrix4Determinant(&result_c));
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4IsIdentityTest) {
- EXPECT_EQ(result_cpp.IsIdentity(),
- (bool)aiMatrix4IsIdentity(&result_c));
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiDecomposeMatrixTest) {
- aiVector3D scaling_c, scaling_cpp,
- position_c, position_cpp;
- aiQuaternion rotation_c, rotation_cpp;
- result_c = result_cpp = get_predetermined_transformation_matrix_for_decomposition();
- result_cpp.Decompose(scaling_cpp, rotation_cpp, position_cpp);
- aiDecomposeMatrix(&result_c, &scaling_c, &rotation_c, &position_c);
- EXPECT_EQ(scaling_cpp, scaling_c);
- EXPECT_EQ(position_cpp, position_c);
- EXPECT_EQ(rotation_cpp, rotation_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4DecomposeIntoScalingEulerAnglesPositionTest) {
- aiVector3D scaling_c, scaling_cpp,
- rotation_c, rotation_cpp,
- position_c, position_cpp;
- result_c = result_cpp = get_predetermined_transformation_matrix_for_decomposition();
- result_cpp.Decompose(scaling_cpp, rotation_cpp, position_cpp);
- aiMatrix4DecomposeIntoScalingEulerAnglesPosition(&result_c, &scaling_c, &rotation_c, &position_c);
- EXPECT_EQ(scaling_cpp, scaling_c);
- EXPECT_EQ(position_cpp, position_c);
- EXPECT_EQ(rotation_cpp, rotation_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4DecomposeIntoScalingAxisAnglePositionTest) {
- aiVector3D scaling_c, scaling_cpp,
- axis_c, axis_cpp,
- position_c, position_cpp;
- ai_real angle_c, angle_cpp;
- result_c = result_cpp = get_predetermined_transformation_matrix_for_decomposition();
- result_cpp.Decompose(scaling_cpp, axis_cpp, angle_cpp, position_cpp);
- aiMatrix4DecomposeIntoScalingAxisAnglePosition(&result_c, &scaling_c, &axis_c, &angle_c, &position_c);
- EXPECT_EQ(scaling_cpp, scaling_c);
- EXPECT_EQ(axis_cpp, axis_c);
- EXPECT_EQ(angle_cpp, angle_c);
- EXPECT_EQ(position_cpp, position_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4DecomposeNoScalingTest) {
- aiVector3D position_c, position_cpp;
- aiQuaternion rotation_c, rotation_cpp;
- result_c = result_cpp = get_predetermined_transformation_matrix_for_decomposition();
- result_cpp.DecomposeNoScaling(rotation_cpp, position_cpp);
- aiMatrix4DecomposeNoScaling(&result_c, &rotation_c, &position_c);
- EXPECT_EQ(position_cpp, position_c);
- EXPECT_EQ(rotation_cpp, rotation_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4FromEulerAnglesTest) {
- const float x(RandPI.next()),
- y(RandPI.next()),
- z(RandPI.next());
- result_cpp.FromEulerAnglesXYZ(x, y, z);
- aiMatrix4FromEulerAngles(&result_c, x, y, z);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4RotationXTest) {
- const float angle(RandPI.next());
- aiMatrix4x4::RotationX(angle, result_cpp);
- aiMatrix4RotationX(&result_c, angle);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4RotationYTest) {
- const float angle(RandPI.next());
- aiMatrix4x4::RotationY(angle, result_cpp);
- aiMatrix4RotationY(&result_c, angle);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4RotationZTest) {
- const float angle(RandPI.next());
- aiMatrix4x4::RotationZ(angle, result_cpp);
- aiMatrix4RotationZ(&result_c, angle);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4FromRotationAroundAxisTest) {
- const float angle(RandPI.next());
- const auto axis = random_unit_vec3();
- aiMatrix4x4::Rotation(angle, axis, result_cpp);
- aiMatrix4FromRotationAroundAxis(&result_c, &axis, angle);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4TranslationTest) {
- const auto axis = random_vec3();
- aiMatrix4x4::Translation(axis, result_cpp);
- aiMatrix4Translation(&result_c, &axis);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4ScalingTest) {
- const auto scaling = random_vec3();
- aiMatrix4x4::Scaling(scaling, result_cpp);
- aiMatrix4Scaling(&result_c, &scaling);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4FromToTest) {
- // Use predetermined vectors to prevent running into division by zero.
- const auto from = aiVector3D(1,2,1).Normalize(), to = aiVector3D(-1,1,1).Normalize();
- aiMatrix4x4::FromToMatrix(from, to, result_cpp);
- aiMatrix4FromTo(&result_c, &from, &to);
- EXPECT_EQ(result_cpp, result_c);
- }
- TEST_F(AssimpAPITest_aiMatrix4x4, operatorTest) {
- std::array<ai_real, 16> value = { 1, 2, 3, 4, 5, 6, 7, 8,
- 9, 10, 11, 12, 13, 14, 15, 16 };
- result_cpp = aiMatrix4x4( value[0], value[1], value[2], value[3],
- value[4], value[5], value[6], value[7],
- value[8], value[9], value[10], value[11],
- value[12], value[13], value[14], value[15] );
- size_t idx=0;
- for (unsigned int i = 0; i < 4; ++i) {
- for (unsigned int j = 0; j < 4; ++j) {
- ai_real curValue = result_cpp[i][j];
- EXPECT_EQ(curValue, value[idx]);
- idx++;
- }
- }
- }
|