diff --git a/test/unit/AssimpAPITest_aiMatrix3x3.cpp b/test/unit/AssimpAPITest_aiMatrix3x3.cpp index 132b9dfe9..5c0282a63 100644 --- a/test/unit/AssimpAPITest_aiMatrix3x3.cpp +++ b/test/unit/AssimpAPITest_aiMatrix3x3.cpp @@ -143,7 +143,8 @@ TEST_F(AssimpAPITest_aiMatrix3x3, aiMatrix3TranslationTest) { } TEST_F(AssimpAPITest_aiMatrix3x3, aiMatrix3FromToTest) { - const auto from = random_vec3(), to = random_vec3(); + // Use predetermined vectors to prevent running into division by zero. + const auto from = aiVector3D(1,2,1).Normalize(), to = aiVector3D(-1,1,1).Normalize(); aiMatrix3x3::FromToMatrix(from, to, result_cpp); aiMatrix3FromTo(&result_c, &from, &to); EXPECT_EQ(result_cpp, result_c); diff --git a/test/unit/AssimpAPITest_aiMatrix4x4.cpp b/test/unit/AssimpAPITest_aiMatrix4x4.cpp index b342d3142..35c6fedfe 100644 --- a/test/unit/AssimpAPITest_aiMatrix4x4.cpp +++ b/test/unit/AssimpAPITest_aiMatrix4x4.cpp @@ -51,6 +51,16 @@ protected: 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::PI() / 4.0f, aiVector3D(1).Normalize(), r); + return t * r; + } + aiMatrix4x4 result_c, result_cpp; }; @@ -142,7 +152,7 @@ TEST_F(AssimpAPITest_aiMatrix4x4, aiDecomposeMatrixTest) { position_c, position_cpp; aiQuaternion rotation_c, rotation_cpp; - result_c = result_cpp = random_mat4(); + 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); @@ -155,7 +165,7 @@ TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4DecomposeIntoScalingEulerAnglesPositi rotation_c, rotation_cpp, position_c, position_cpp; - result_c = result_cpp = random_mat4(); + 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); @@ -169,7 +179,7 @@ TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4DecomposeIntoScalingAxisAnglePosition position_c, position_cpp; float angle_c, angle_cpp; - result_c = result_cpp = random_mat4(); + 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); @@ -182,7 +192,7 @@ TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4DecomposeNoScalingTest) { aiVector3D position_c, position_cpp; aiQuaternion rotation_c, rotation_cpp; - result_c = result_cpp = random_mat4(); + 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); @@ -242,7 +252,8 @@ TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4ScalingTest) { } TEST_F(AssimpAPITest_aiMatrix4x4, aiMatrix4FromToTest) { - const auto from = random_vec3(), to = random_vec3(); + // 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); diff --git a/test/unit/AssimpAPITest_aiQuaternion.cpp b/test/unit/AssimpAPITest_aiQuaternion.cpp index c02c8ce05..6bdef5a15 100644 --- a/test/unit/AssimpAPITest_aiQuaternion.cpp +++ b/test/unit/AssimpAPITest_aiQuaternion.cpp @@ -55,7 +55,13 @@ protected: }; TEST_F(AssimpAPITest_aiQuaternion, aiCreateQuaternionFromMatrixTest) { - const auto m = random_mat3(); + // Use a predetermined transformation matrix + // to prevent running into division by zero. + aiMatrix3x3 m, r; + aiMatrix3x3::Translation(aiVector2D(14,-25), m); + aiMatrix3x3::RotationZ(Math::PI() / 4.0f, r); + m = m * r; + result_cpp = aiQuaternion(m); aiCreateQuaternionFromMatrix(&result_c, &m); EXPECT_EQ(result_cpp, result_c); @@ -118,9 +124,11 @@ TEST_F(AssimpAPITest_aiQuaternion, aiQuaternionMultiplyTest) { } TEST_F(AssimpAPITest_aiQuaternion, aiQuaternionInterpolateTest) { - const float INTERPOLATION(RandUnit.next()); - const auto q1 = random_quat(); - const auto q2 = random_quat(); + // Use predetermined quaternions to prevent division by zero + // during slerp calculations. + const float INTERPOLATION(0.5f); + const auto q1 = aiQuaternion(aiVector3D(-1,1,1).Normalize(), Math::PI() / 4.0f); + const auto q2 = aiQuaternion(aiVector3D(1,2,1).Normalize(), Math::PI() / 2.0f); aiQuaternion::Interpolate(result_cpp, q1, q2, INTERPOLATION); aiQuaternionInterpolate(&result_c, &q1, &q2, INTERPOLATION); EXPECT_EQ(result_cpp, result_c); diff --git a/test/unit/AssimpAPITest_aiVector2D.cpp b/test/unit/AssimpAPITest_aiVector2D.cpp index 7c2be6c32..55df06025 100644 --- a/test/unit/AssimpAPITest_aiVector2D.cpp +++ b/test/unit/AssimpAPITest_aiVector2D.cpp @@ -49,7 +49,7 @@ class AssimpAPITest_aiVector2D : public AssimpMathTest { protected: virtual void SetUp() { result_c = result_cpp = aiVector2D(); - temp = random_vec2(); + temp = random_vec2(); // Generates a random 2D vector != null vector. } aiVector2D result_c, result_cpp, temp; @@ -82,7 +82,7 @@ TEST_F(AssimpAPITest_aiVector2D, aiVector2SubtractTest) { } TEST_F(AssimpAPITest_aiVector2D, aiVector2ScaleTest) { - const float FACTOR = Rand.next(); + const float FACTOR = RandNonZero.next(); result_c = result_cpp = random_vec2(); result_cpp *= FACTOR; aiVector2Scale(&result_c, FACTOR); @@ -97,7 +97,7 @@ TEST_F(AssimpAPITest_aiVector2D, aiVector2SymMulTest) { } TEST_F(AssimpAPITest_aiVector2D, aiVector2DivideByScalarTest) { - const float DIVISOR = Rand.next(); + const float DIVISOR = RandNonZero.next(); result_c = result_cpp = random_vec2(); result_cpp /= DIVISOR; aiVector2DivideByScalar(&result_c, DIVISOR); diff --git a/test/unit/AssimpAPITest_aiVector3D.cpp b/test/unit/AssimpAPITest_aiVector3D.cpp index 410c34857..a59867f10 100644 --- a/test/unit/AssimpAPITest_aiVector3D.cpp +++ b/test/unit/AssimpAPITest_aiVector3D.cpp @@ -49,7 +49,7 @@ class AssimpAPITest_aiVector3D : public AssimpMathTest { protected: virtual void SetUp() { result_c = result_cpp = aiVector3D(); - temp = random_vec3(); + temp = random_vec3(); // Generates a random 3D vector != null vector. } aiVector3D result_c, result_cpp, temp; @@ -88,7 +88,7 @@ TEST_F(AssimpAPITest_aiVector3D, aiVector3SubtractTest) { } TEST_F(AssimpAPITest_aiVector3D, aiVector3ScaleTest) { - const float FACTOR = Rand.next(); + const float FACTOR = RandNonZero.next(); result_c = result_cpp = random_vec3(); result_cpp *= FACTOR; aiVector3Scale(&result_c, FACTOR); @@ -103,7 +103,7 @@ TEST_F(AssimpAPITest_aiVector3D, aiVector3SymMulTest) { } TEST_F(AssimpAPITest_aiVector3D, aiVector3DivideByScalarTest) { - const float DIVISOR = Rand.next(); + const float DIVISOR = RandNonZero.next(); result_c = result_cpp = random_vec3(); result_cpp /= DIVISOR; aiVector3DivideByScalar(&result_c, DIVISOR); diff --git a/test/unit/MathTest.cpp b/test/unit/MathTest.cpp index 2aacb1517..69b23f625 100644 --- a/test/unit/MathTest.cpp +++ b/test/unit/MathTest.cpp @@ -47,13 +47,10 @@ namespace Assimp { // Initialize epsilon value. const float AssimpMathTest::Epsilon = Math::getEpsilon(); -// Initialize with an interval of [1,100] to avoid null values. -RandomUniformFloatGenerator AssimpMathTest::Rand(1.0f, 100.0f); +// Initialize with an interval of [1,100]. +RandomUniformFloatGenerator AssimpMathTest::RandNonZero(1.0f, 100.0f); // Initialize with an interval of [-PI,PI] inclusively. RandomUniformFloatGenerator AssimpMathTest::RandPI(-Math::PI(), Math::PI()); -// Initialize with an interval of [0,1] inclusively. -RandomUniformFloatGenerator AssimpMathTest::RandUnit(0.0f, 1.0f); - } diff --git a/test/unit/MathTest.h b/test/unit/MathTest.h index f60f5f173..19401b29a 100644 --- a/test/unit/MathTest.h +++ b/test/unit/MathTest.h @@ -53,14 +53,14 @@ namespace Assimp { /** Custom test class providing several math related utilities. */ class AssimpMathTest : public ::testing::Test { public: - /** Return a random 2D vector. */ + /** Return a random non-null 2D vector. */ inline static aiVector2D random_vec2() { - return aiVector2D(Rand.next(), Rand.next()); + return aiVector2D(RandNonZero.next(), RandNonZero.next()); } - /** Return a random 3D vector. */ + /** Return a random non-null 3D vector. */ inline static aiVector3D random_vec3() { - return aiVector3D(Rand.next(), Rand.next(),Rand.next()); + return aiVector3D(RandNonZero.next(), RandNonZero.next(),RandNonZero.next()); } /** Return a random unit 3D vector. */ @@ -74,28 +74,28 @@ public: return aiQuaternion(random_unit_vec3(), RandPI.next()); } - /** Return a random 3x3 matrix. */ + /** Return a random non-null 3x3 matrix. */ inline static aiMatrix3x3 random_mat3() { return aiMatrix3x3( - Rand.next(), Rand.next(),Rand.next(), - Rand.next(), Rand.next(),Rand.next(), - Rand.next(), Rand.next(),Rand.next()); + RandNonZero.next(), RandNonZero.next(),RandNonZero.next(), + RandNonZero.next(), RandNonZero.next(),RandNonZero.next(), + RandNonZero.next(), RandNonZero.next(),RandNonZero.next()); } - /** Return a random 4x4 matrix. */ + /** Return a random non-null 4x4 matrix. */ inline static aiMatrix4x4 random_mat4() { return aiMatrix4x4( - Rand.next(), Rand.next(),Rand.next(), Rand.next(), - Rand.next(), Rand.next(),Rand.next(), Rand.next(), - Rand.next(), Rand.next(),Rand.next(), Rand.next(), - Rand.next(), Rand.next(),Rand.next(), Rand.next()); + RandNonZero.next(), RandNonZero.next(),RandNonZero.next(), RandNonZero.next(), + RandNonZero.next(), RandNonZero.next(),RandNonZero.next(), RandNonZero.next(), + RandNonZero.next(), RandNonZero.next(),RandNonZero.next(), RandNonZero.next(), + RandNonZero.next(), RandNonZero.next(),RandNonZero.next(), RandNonZero.next()); } /** Epsilon value to use in tests. */ static const float Epsilon; /** Random number generators. */ - static RandomUniformFloatGenerator Rand, RandPI, RandUnit; + static RandomUniformFloatGenerator RandNonZero, RandPI; }; }