From 219be2e755d26458fd67a7ac0ea4cb02c0644165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Foucault?= Date: Fri, 10 Mar 2023 10:07:34 +0100 Subject: [PATCH] BLI; Math: Remove `Normalized` template parameter for some conversion Always expect normalized matrix input as per API design. --- source/blender/blenlib/BLI_math_matrix.hh | 102 +++++++----------- source/blender/blenlib/BLI_math_quaternion.hh | 8 +- source/blender/blenlib/intern/math_matrix.cc | 4 +- .../blenlib/tests/BLI_math_matrix_test.cc | 5 +- .../spreadsheet_data_source_geometry.cc | 2 +- 5 files changed, 47 insertions(+), 74 deletions(-) diff --git a/source/blender/blenlib/BLI_math_matrix.hh b/source/blender/blenlib/BLI_math_matrix.hh index a881f2642f5..26922d0f65e 100644 --- a/source/blender/blenlib/BLI_math_matrix.hh +++ b/source/blender/blenlib/BLI_math_matrix.hh @@ -256,13 +256,13 @@ template * Extract euler rotation from transform matrix. * \return the rotation with the smallest values from the potential candidates. */ -template +template [[nodiscard]] inline detail::EulerXYZ to_euler(const MatBase &mat); -template +template [[nodiscard]] inline detail::EulerXYZ to_euler(const MatBase &mat); -template +template [[nodiscard]] inline detail::Euler3 to_euler(const MatBase &mat, EulerOrder order); -template +template [[nodiscard]] inline detail::Euler3 to_euler(const MatBase &mat, EulerOrder order); /** @@ -272,25 +272,25 @@ template * \return the rotation with the smallest values from the potential candidates. * \note this correspond to the C API "to_compatible" functions. */ -template +template [[nodiscard]] inline detail::EulerXYZ to_nearest_euler(const MatBase &mat, const detail::EulerXYZ &reference); -template +template [[nodiscard]] inline detail::EulerXYZ to_nearest_euler(const MatBase &mat, const detail::EulerXYZ &reference); -template +template [[nodiscard]] inline detail::Euler3 to_nearest_euler(const MatBase &mat, const detail::Euler3 &reference); -template +template [[nodiscard]] inline detail::Euler3 to_nearest_euler(const MatBase &mat, const detail::Euler3 &reference); /** * Extract quaternion rotation from transform matrix. */ -template +template [[nodiscard]] inline detail::Quaternion to_quaternion(const MatBase &mat); -template +template [[nodiscard]] inline detail::Quaternion to_quaternion(const MatBase &mat); /** @@ -1076,61 +1076,44 @@ extern template MatBase from_rotation(const math::AxisAngleCartesia } // namespace detail -template +template [[nodiscard]] inline detail::Euler3 to_euler(const MatBase &mat, EulerOrder order) { detail::Euler3 eul1(order), eul2(order); - if constexpr (Normalized) { - detail::normalized_to_eul2(mat, eul1, eul2); - } - else { - detail::normalized_to_eul2(normalize(mat), eul1, eul2); - } + detail::normalized_to_eul2(mat, eul1, eul2); /* Return best, which is just the one with lowest values in it. */ return (length_manhattan(VecBase(eul1)) > length_manhattan(VecBase(eul2))) ? eul2 : eul1; } -template -[[nodiscard]] inline detail::EulerXYZ to_euler(const MatBase &mat) +template [[nodiscard]] inline detail::EulerXYZ to_euler(const MatBase &mat) { detail::EulerXYZ eul1, eul2; - if constexpr (Normalized) { - detail::normalized_to_eul2(mat, eul1, eul2); - } - else { - detail::normalized_to_eul2(normalize(mat), eul1, eul2); - } + detail::normalized_to_eul2(mat, eul1, eul2); /* Return best, which is just the one with lowest values in it. */ return (length_manhattan(VecBase(eul1)) > length_manhattan(VecBase(eul2))) ? eul2 : eul1; } -template +template [[nodiscard]] inline detail::Euler3 to_euler(const MatBase &mat, EulerOrder order) { /* TODO(fclem): Avoid the copy with 3x3 ref. */ - return to_euler(MatBase(mat), order); + return to_euler(MatBase(mat), order); } -template -[[nodiscard]] inline detail::EulerXYZ to_euler(const MatBase &mat) +template [[nodiscard]] inline detail::EulerXYZ to_euler(const MatBase &mat) { /* TODO(fclem): Avoid the copy with 3x3 ref. */ - return to_euler(MatBase(mat)); + return to_euler(MatBase(mat)); } -template +template [[nodiscard]] inline detail::Euler3 to_nearest_euler(const MatBase &mat, const detail::Euler3 &reference) { detail::Euler3 eul1(reference.order()), eul2(reference.order()); - if constexpr (Normalized) { - detail::normalized_to_eul2(mat, eul1, eul2); - } - else { - detail::normalized_to_eul2(normalize(mat), eul1, eul2); - } + detail::normalized_to_eul2(mat, eul1, eul2); eul1 = eul1.wrapped_around(reference); eul2 = eul2.wrapped_around(reference); /* Return best, which is just the one with lowest values it in. */ @@ -1140,17 +1123,12 @@ template eul1; } -template +template [[nodiscard]] inline detail::EulerXYZ to_nearest_euler(const MatBase &mat, const detail::EulerXYZ &reference) { detail::EulerXYZ eul1, eul2; - if constexpr (Normalized) { - detail::normalized_to_eul2(mat, eul1, eul2); - } - else { - detail::normalized_to_eul2(normalize(mat), eul1, eul2); - } + detail::normalized_to_eul2(mat, eul1, eul2); eul1 = eul1.wrapped_around(reference); eul2 = eul2.wrapped_around(reference); /* Return best, which is just the one with lowest values it in. */ @@ -1160,39 +1138,33 @@ template eul1; } -template +template [[nodiscard]] inline detail::Euler3 to_nearest_euler(const MatBase &mat, const detail::Euler3 &reference) { /* TODO(fclem): Avoid the copy with 3x3 ref. */ - return to_euler(MatBase(mat), reference); + return to_euler(MatBase(mat), reference); } -template +template [[nodiscard]] inline detail::EulerXYZ to_nearest_euler(const MatBase &mat, const detail::EulerXYZ &reference) { /* TODO(fclem): Avoid the copy with 3x3 ref. */ - return to_euler(MatBase(mat), reference); + return to_euler(MatBase(mat), reference); } -template +template [[nodiscard]] inline detail::Quaternion to_quaternion(const MatBase &mat) { - using namespace math; - if constexpr (Normalized) { - return detail::normalized_to_quat_with_checks(mat); - } - else { - return detail::normalized_to_quat_with_checks(normalize(mat)); - } + return detail::normalized_to_quat_with_checks(mat); } -template +template [[nodiscard]] inline detail::Quaternion to_quaternion(const MatBase &mat) { /* TODO(fclem): Avoid the copy with 3x3 ref. */ - return to_quaternion(MatBase(mat)); + return to_quaternion(MatBase(mat)); } template @@ -1222,22 +1194,22 @@ template /* Implementation details. Use `to_euler` and `to_quaternion` instead. */ namespace detail { -template +template inline void to_rotation(const MatBase &mat, detail::Quaternion &r_rotation) { - r_rotation = to_quaternion(mat); + r_rotation = to_quaternion(mat); } -template +template inline void to_rotation(const MatBase &mat, detail::EulerXYZ &r_rotation) { - r_rotation = to_euler(mat); + r_rotation = to_euler(mat); } -template +template inline void to_rotation(const MatBase &mat, detail::Euler3 &r_rotation) { - r_rotation = to_euler(mat, r_rotation.order()); + r_rotation = to_euler(mat, r_rotation.order()); } } // namespace detail @@ -1254,7 +1226,7 @@ inline void to_rot_scale(const MatBase &mat, r_scale = -r_scale; } } - detail::to_rotation(normalized_mat, r_rotation); + detail::to_rotation(normalized_mat, r_rotation); } template diff --git a/source/blender/blenlib/BLI_math_quaternion.hh b/source/blender/blenlib/BLI_math_quaternion.hh index b2cb6a34ec3..217819d5a6d 100644 --- a/source/blender/blenlib/BLI_math_quaternion.hh +++ b/source/blender/blenlib/BLI_math_quaternion.hh @@ -586,7 +586,7 @@ template const Mat4T baseinv = invert(basemat); /* Extra orthogonalize, to avoid flipping with stretched bones. */ - detail::Quaternion basequat = to_quaternion(orthogonalize(baseRS, Axis::Y)); + detail::Quaternion basequat = to_quaternion(normalize(orthogonalize(baseRS, Axis::Y))); Mat4T baseR = from_rotation(basequat); baseR.location() = baseRS.location(); @@ -603,7 +603,7 @@ template } /* Non-dual part. */ - const detail::Quaternion q = to_quaternion(R); + const detail::Quaternion q = to_quaternion(normalize(R)); /* Dual part. */ const Vec3T &t = R.location().xyz(); @@ -663,7 +663,7 @@ template detail::EulerXYZ to_euler(const detail::Quaternion &q using Mat3T = MatBase; BLI_assert(is_unit_scale(quat)); Mat3T unit_mat = from_rotation(quat); - return to_euler(unit_mat); + return to_euler(unit_mat); } template @@ -672,7 +672,7 @@ detail::Euler3 to_euler(const detail::Quaternion &quat, EulerOrder order) using Mat3T = MatBase; BLI_assert(is_unit_scale(quat)); Mat3T unit_mat = from_rotation(quat); - return to_euler(unit_mat, order); + return to_euler(unit_mat, order); } /** \} */ diff --git a/source/blender/blenlib/intern/math_matrix.cc b/source/blender/blenlib/intern/math_matrix.cc index 1cea339533c..aad6498b62b 100644 --- a/source/blender/blenlib/intern/math_matrix.cc +++ b/source/blender/blenlib/intern/math_matrix.cc @@ -339,8 +339,8 @@ MatBase interpolate(const MatBase &A, const MatBase & P_B = -P_B; } - detail::Quaternion quat_A = math::to_quaternion(U_A); - detail::Quaternion quat_B = math::to_quaternion(U_B); + detail::Quaternion quat_A = math::to_quaternion(normalize(U_A)); + detail::Quaternion quat_B = math::to_quaternion(normalize(U_B)); detail::Quaternion quat = math::interpolate(quat_A, quat_B, t); Mat3T U = from_rotation(quat); diff --git a/source/blender/blenlib/tests/BLI_math_matrix_test.cc b/source/blender/blenlib/tests/BLI_math_matrix_test.cc index 3eefd8fecb8..2e08472ed44 100644 --- a/source/blender/blenlib/tests/BLI_math_matrix_test.cc +++ b/source/blender/blenlib/tests/BLI_math_matrix_test.cc @@ -347,8 +347,6 @@ TEST(math_matrix, MatrixMethods) float3 expect_scale = float3(3, 2, 2); float3 expect_location = float3(0, 1, 0); - EXPECT_V3_NEAR(float3(to_euler(m)), float3(expect_eul), 0.0002f); - EXPECT_V4_NEAR(float4(to_quaternion(m)), float4(expect_qt), 0.0002f); EXPECT_EQ(to_scale(m), expect_scale); float4 expect_sz = {3, 2, 2, M_SQRT2}; @@ -360,6 +358,9 @@ TEST(math_matrix, MatrixMethods) float4x4 m2 = normalize(m); EXPECT_TRUE(is_unit_scale(m2)); + EXPECT_V3_NEAR(float3(to_euler(m1)), float3(expect_eul), 0.0002f); + EXPECT_V4_NEAR(float4(to_quaternion(m1)), float4(expect_qt), 0.0002f); + EulerXYZ eul; Quaternion qt; float3 scale; diff --git a/source/blender/editors/space_spreadsheet/spreadsheet_data_source_geometry.cc b/source/blender/editors/space_spreadsheet/spreadsheet_data_source_geometry.cc index 2b7d981319c..b95efed52a3 100644 --- a/source/blender/editors/space_spreadsheet/spreadsheet_data_source_geometry.cc +++ b/source/blender/editors/space_spreadsheet/spreadsheet_data_source_geometry.cc @@ -268,7 +268,7 @@ std::unique_ptr GeometryDataSource::get_column_values( if (STREQ(column_id.name, "Rotation")) { return std::make_unique( column_id.name, VArray::ForFunc(domain_num, [transforms](int64_t index) { - return float3(math::to_euler(transforms[index])); + return float3(math::to_euler(math::normalize(transforms[index]))); })); } if (STREQ(column_id.name, "Scale")) {