Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/tests/BLI_math_rotation_test.cc
| /* SPDX-License-Identifier: Apache-2.0 */ | /* SPDX-License-Identifier: Apache-2.0 */ | ||||
| #include "testing/testing.h" | #include "testing/testing.h" | ||||
| #include "BLI_math_base.h" | #include "BLI_math_base.h" | ||||
| #include "BLI_math_matrix.h" | #include "BLI_math_matrix.h" | ||||
| #include "BLI_math_rotation.h" | #include "BLI_math_rotation.h" | ||||
| #include "BLI_math_rotation.hh" | |||||
| #include "BLI_math_rotation_legacy.hh" | #include "BLI_math_rotation_legacy.hh" | ||||
| #include "BLI_math_vector.hh" | #include "BLI_math_vector.hh" | ||||
| #include "BLI_vector.hh" | #include "BLI_vector.hh" | ||||
| #include <cmath> | #include <cmath> | ||||
| /* Test that quaternion converts to itself via matrix. */ | /* Test that quaternion converts to itself via matrix. */ | ||||
| ▲ Show 20 Lines • Show All 250 Lines • ▼ Show 20 Lines | for (int range = 1; range <= 64; range++) { | ||||
| test_sin_cos_from_fraction_symmetry(range); | test_sin_cos_from_fraction_symmetry(range); | ||||
| } | } | ||||
| } | } | ||||
| /** \} */ | /** \} */ | ||||
| namespace blender::math::tests { | namespace blender::math::tests { | ||||
| TEST(math_rotation, DefaultConstructor) | |||||
| { | |||||
| Quaternion quat{}; | |||||
| EXPECT_EQ(quat.x, 0.0f); | |||||
| EXPECT_EQ(quat.y, 0.0f); | |||||
| EXPECT_EQ(quat.z, 0.0f); | |||||
| EXPECT_EQ(quat.w, 0.0f); | |||||
| EulerXYZ eul{}; | |||||
| EXPECT_EQ(eul.x, 0.0f); | |||||
| EXPECT_EQ(eul.y, 0.0f); | |||||
| EXPECT_EQ(eul.z, 0.0f); | |||||
| } | |||||
| TEST(math_rotation, RotateDirectionAroundAxis) | TEST(math_rotation, RotateDirectionAroundAxis) | ||||
| { | { | ||||
| const float3 a = rotate_direction_around_axis({1, 0, 0}, {0, 0, 1}, M_PI_2); | const float3 a = rotate_direction_around_axis({1, 0, 0}, {0, 0, 1}, M_PI_2); | ||||
| EXPECT_NEAR(a.x, 0.0f, FLT_EPSILON); | EXPECT_NEAR(a.x, 0.0f, FLT_EPSILON); | ||||
| EXPECT_NEAR(a.y, 1.0f, FLT_EPSILON); | EXPECT_NEAR(a.y, 1.0f, FLT_EPSILON); | ||||
| EXPECT_NEAR(a.z, 0.0f, FLT_EPSILON); | EXPECT_NEAR(a.z, 0.0f, FLT_EPSILON); | ||||
| const float3 b = rotate_direction_around_axis({1, 0, 0}, {0, 0, 1}, M_PI); | const float3 b = rotate_direction_around_axis({1, 0, 0}, {0, 0, 1}, M_PI); | ||||
| EXPECT_NEAR(b.x, -1.0f, FLT_EPSILON); | EXPECT_NEAR(b.x, -1.0f, FLT_EPSILON); | ||||
| EXPECT_NEAR(b.y, 0.0f, FLT_EPSILON); | EXPECT_NEAR(b.y, 0.0f, FLT_EPSILON); | ||||
| EXPECT_NEAR(b.z, 0.0f, FLT_EPSILON); | EXPECT_NEAR(b.z, 0.0f, FLT_EPSILON); | ||||
| const float3 c = rotate_direction_around_axis({0, 0, 1}, {0, 0, 1}, 0.0f); | const float3 c = rotate_direction_around_axis({0, 0, 1}, {0, 0, 1}, 0.0f); | ||||
| EXPECT_NEAR(c.x, 0.0f, FLT_EPSILON); | EXPECT_NEAR(c.x, 0.0f, FLT_EPSILON); | ||||
| EXPECT_NEAR(c.y, 0.0f, FLT_EPSILON); | EXPECT_NEAR(c.y, 0.0f, FLT_EPSILON); | ||||
| EXPECT_NEAR(c.z, 1.0f, FLT_EPSILON); | EXPECT_NEAR(c.z, 1.0f, FLT_EPSILON); | ||||
| } | } | ||||
| TEST(math_rotation, AxisAngleConstructors) | |||||
| { | |||||
| AxisAngle a({0.0f, 0.0f, 2.0f}, M_PI_2); | |||||
| EXPECT_V3_NEAR(a.axis(), float3(0, 0, 1), 1e-4); | |||||
| EXPECT_NEAR(a.angle(), M_PI_2, 1e-4); | |||||
| AxisAngleNormalized b({0.0f, 0.0f, 1.0f}, M_PI_2); | |||||
| EXPECT_V3_NEAR(b.axis(), float3(0, 0, 1), 1e-4); | |||||
| EXPECT_NEAR(b.angle(), M_PI_2, 1e-4); | |||||
| AxisAngle c({1.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}); | |||||
| EXPECT_V3_NEAR(c.axis(), float3(0, 0, 1), 1e-4); | |||||
| EXPECT_NEAR(c.angle(), M_PI_2, 1e-4); | |||||
| AxisAngle d({1.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}); | |||||
| EXPECT_V3_NEAR(d.axis(), float3(0, 0, -1), 1e-4); | |||||
| EXPECT_NEAR(d.angle(), M_PI_2, 1e-4); | |||||
| } | |||||
| TEST(math_rotation, TypeConversion) | |||||
| { | |||||
| EulerXYZ euler(0, 0, M_PI_2); | |||||
| Quaternion quat(M_SQRT1_2, 0.0f, 0.0f, M_SQRT1_2); | |||||
| AxisAngle axis_angle({0.0f, 0.0f, 2.0f}, M_PI_2); | |||||
| EXPECT_V4_NEAR(float4(Quaternion(euler)), float4(quat), 1e-4); | |||||
| EXPECT_V3_NEAR(AxisAngle(euler).axis(), axis_angle.axis(), 1e-4); | |||||
| EXPECT_NEAR(AxisAngle(euler).angle(), axis_angle.angle(), 1e-4); | |||||
| EXPECT_V3_NEAR(float3(EulerXYZ(quat)), float3(euler), 1e-4); | |||||
| EXPECT_V3_NEAR(AxisAngle(quat).axis(), axis_angle.axis(), 1e-4); | |||||
| EXPECT_NEAR(AxisAngle(quat).angle(), axis_angle.angle(), 1e-4); | |||||
| EXPECT_V3_NEAR(float3(EulerXYZ(axis_angle)), float3(euler), 1e-4); | |||||
| EXPECT_V4_NEAR(float4(Quaternion(axis_angle)), float4(quat), 1e-4); | |||||
| } | |||||
| } // namespace blender::math::tests | } // namespace blender::math::tests | ||||