diff options
author | Hans Goudey <h.goudey@me.com> | 2022-04-15 17:54:15 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-04-15 17:54:45 +0300 |
commit | 47d961a4b1c14b0cee2817de226ee356e711e146 (patch) | |
tree | 4c057b00b7eb82144e4acf12196b9bf33481e9a5 /source/blender/blenlib | |
parent | e96a809a68ef40225d5eddfd790644538e171a8d (diff) |
Fix: Apply tilt in curves data-block normals calculation
The ported normal calculation from ceed37fc5cbb466a0 neglected to
use the tilt attribute to rotate the normals around the tangents.
This commit adds that behavior back, adding a new math header file
to avoid duplicating the rotation function for normalized axes.
Differential Revision: https://developer.blender.org/D14655
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_math_rotation.hh | 18 | ||||
-rw-r--r-- | source/blender/blenlib/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_rotation.cc | 26 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_math_rotation_test.cc | 22 |
4 files changed, 68 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_math_rotation.hh b/source/blender/blenlib/BLI_math_rotation.hh new file mode 100644 index 00000000000..e8b746b34df --- /dev/null +++ b/source/blender/blenlib/BLI_math_rotation.hh @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma once + +/** \file + * \ingroup bli + */ + +#include "BLI_math_vec_types.hh" + +namespace blender::math { + +/** + * Rotate the unit-length \a direction around the unit-length \a axis by the \a angle. + */ +float3 rotate_direction_around_axis(const float3 &direction, const float3 &axis, float angle); + +} // namespace blender::math diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index 99e07264276..e8a3851e082 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -94,6 +94,7 @@ set(SRC intern/math_interp.c intern/math_matrix.c intern/math_rotation.c + intern/math_rotation.cc intern/math_solvers.c intern/math_statistics.c intern/math_time.c @@ -251,6 +252,7 @@ set(SRC BLI_math_matrix.h BLI_math_mpq.hh BLI_math_rotation.h + BLI_math_rotation.hh BLI_math_solvers.h BLI_math_statistics.h BLI_math_time.h diff --git a/source/blender/blenlib/intern/math_rotation.cc b/source/blender/blenlib/intern/math_rotation.cc new file mode 100644 index 00000000000..74300d55954 --- /dev/null +++ b/source/blender/blenlib/intern/math_rotation.cc @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup bli + */ + +#include "BLI_math_base.h" +#include "BLI_math_rotation.hh" +#include "BLI_math_vector.h" +#include "BLI_math_vector.hh" + +namespace blender::math { + +float3 rotate_direction_around_axis(const float3 &direction, const float3 &axis, const float angle) +{ + BLI_ASSERT_UNIT_V3(direction); + BLI_ASSERT_UNIT_V3(axis); + + const float3 axis_scaled = axis * math::dot(direction, axis); + const float3 diff = direction - axis_scaled; + const float3 cross = math::cross(axis, diff); + + return axis_scaled + diff * std::cos(angle) + cross * std::sin(angle); +} + +} // namespace blender::math diff --git a/source/blender/blenlib/tests/BLI_math_rotation_test.cc b/source/blender/blenlib/tests/BLI_math_rotation_test.cc index a10e441cfbe..a283118bea2 100644 --- a/source/blender/blenlib/tests/BLI_math_rotation_test.cc +++ b/source/blender/blenlib/tests/BLI_math_rotation_test.cc @@ -4,6 +4,8 @@ #include "BLI_math_base.h" #include "BLI_math_rotation.h" +#include "BLI_math_rotation.hh" +#include "BLI_math_vector.hh" #include <cmath> @@ -147,3 +149,23 @@ TEST(math_rotation, quat_split_swing_and_twist_negative) EXPECT_V4_NEAR(swing, expected_swing, FLT_EPSILON); EXPECT_V4_NEAR(twist, expected_twist, FLT_EPSILON); } + +namespace blender::math::tests { + +TEST(math_rotation, RotateDirectionAroundAxis) +{ + 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.y, 1.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); + EXPECT_NEAR(b.x, -1.0f, FLT_EPSILON); + EXPECT_NEAR(b.y, 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); + EXPECT_NEAR(c.x, 0.0f, FLT_EPSILON); + EXPECT_NEAR(c.y, 0.0f, FLT_EPSILON); + EXPECT_NEAR(c.z, 1.0f, FLT_EPSILON); +} + +} // namespace blender::math::tests |