Skip to content

Commit

Permalink
Uses Float3 to represent euler angles
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeblanc committed Mar 29, 2024
1 parent 442b563 commit bbab4fb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
12 changes: 5 additions & 7 deletions include/ozz/base/maths/quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ struct OZZ_BASE_DLL Quaternion {
// Returns a normalized quaternion initialized from an Euler representation.
// Euler angles are ordered Heading, Elevation and Bank, or Yaw, Pitch and
// Roll.
static OZZ_INLINE Quaternion FromEuler(float _yaw, float _pitch,
float _roll);
static OZZ_INLINE Quaternion FromEuler(const Float3& _ypr);

// Returns the quaternion that will rotate vector _from into vector _to,
// around their plan perpendicular axis.The input vectors don't need to be
Expand Down Expand Up @@ -202,15 +201,14 @@ OZZ_INLINE Float4 ToAxisAngle(const Quaternion& _q) {
}
}

OZZ_INLINE Quaternion Quaternion::FromEuler(float _yaw, float _pitch,
float _roll) {
const float half_yaw = _yaw * .5f;
OZZ_INLINE Quaternion Quaternion::FromEuler(const Float3& ypr) {
const float half_yaw = ypr.x * .5f;
const float c1 = std::cos(half_yaw);
const float s1 = std::sin(half_yaw);
const float half_pitch = _pitch * .5f;
const float half_pitch = ypr.y * .5f;
const float c2 = std::cos(half_pitch);
const float s2 = std::sin(half_pitch);
const float half_roll = _roll * .5f;
const float half_roll = ypr.z * .5f;
const float c3 = std::cos(half_roll);
const float s3 = std::sin(half_roll);
const float c1c2 = c1 * c2;
Expand Down
3 changes: 1 addition & 2 deletions samples/framework/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ bool OnRawSkeletonJointGui(
if (euler_modified) {
modified = true;
ozz::math::Float3 euler_rad = euler * ozz::math::kDegreeToRadian;
rotation = ozz::math::Quaternion::FromEuler(euler_rad.x, euler_rad.y,
euler_rad.z);
rotation = ozz::math::Quaternion::FromEuler(euler_rad);
}

// Scale (must be uniform and not 0)
Expand Down
10 changes: 5 additions & 5 deletions src/animation/offline/motion_extractor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ bool MotionExtractor::operator()(const RawAnimation& _input,
_motion_rotation->keyframes.clear();
for (const auto& joint_key : input_track.rotations) {
// Decompose rotation to take expected components only.
const auto decomp = ToEuler(joint_key.value * Conjugate(ref.rotation));
const auto motion_q =
math::Quaternion::FromEuler(rotation_settings.y ? decomp.x : 0,
rotation_settings.x ? decomp.y : 0,
rotation_settings.z ? decomp.z : 0);
const math::Float3 mask{1.f * rotation_settings.y, // Yaw
1.f * rotation_settings.x, // Pitch
1.f * rotation_settings.z}; // Roll
const auto euler = ToEuler(joint_key.value * Conjugate(ref.rotation));
const auto motion_q = math::Quaternion::FromEuler(euler * mask);
_motion_rotation->keyframes.push_back(
{ozz::animation::offline::RawTrackInterpolation::kLinear,
joint_key.time / _input.duration, motion_q});
Expand Down
16 changes: 8 additions & 8 deletions test/animation/offline/animation_optimizer_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,19 +340,19 @@ TEST(Optimize, AnimationOptimizer) {
// Rotations on track 0.
{
RawAnimation::RotationKey key = {
0.f, ozz::math::Quaternion::FromEuler(0.f, 0.f, 0.f)};
0.f, ozz::math::Quaternion::FromEuler({0.f, 0.f, 0.f})};
input.tracks[0].rotations.push_back(key);
}
{ // Include error
const float angle_error = 2.5e-3f; // creates an arc of .1m at 40m.
RawAnimation::RotationKey key = {
.1f, ozz::math::Quaternion::FromEuler(ozz::math::kPi_4 + angle_error,
0.f, 0.f)};
.1f, ozz::math::Quaternion::FromEuler(
{ozz::math::kPi_4 + angle_error, 0.f, 0.f})};
input.tracks[0].rotations.push_back(key);
}
{
RawAnimation::RotationKey key = {
.2f, ozz::math::Quaternion::FromEuler(ozz::math::kPi_2, 0.f, 0.f)};
.2f, ozz::math::Quaternion::FromEuler({ozz::math::kPi_2, 0.f, 0.f})};
input.tracks[0].rotations.push_back(key);
}

Expand Down Expand Up @@ -484,19 +484,19 @@ TEST(OptimizeOverride, AnimationOptimizer) {
// Rotations on track 0.
{
RawAnimation::RotationKey key = {
0.f, ozz::math::Quaternion::FromEuler(0.f, 0.f, 0.f)};
0.f, ozz::math::Quaternion::FromEuler({0.f, 0.f, 0.f})};
input.tracks[1].rotations.push_back(key);
}
{ // Includes an error that
const float angle_error = 1e-3f; // creates an arc of 1mm at 1m.
RawAnimation::RotationKey key = {
.1f, ozz::math::Quaternion::FromEuler(ozz::math::kPi_4 + angle_error,
0.f, 0.f)};
.1f, ozz::math::Quaternion::FromEuler(
{ozz::math::kPi_4 + angle_error, 0.f, 0.f})};
input.tracks[1].rotations.push_back(key);
}
{
RawAnimation::RotationKey key = {
.2f, ozz::math::Quaternion::FromEuler(ozz::math::kPi_2, 0.f, 0.f)};
.2f, ozz::math::Quaternion::FromEuler({ozz::math::kPi_2, 0.f, 0.f})};
input.tracks[1].rotations.push_back(key);
}

Expand Down
35 changes: 19 additions & 16 deletions test/base/maths/quaternion_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,38 +113,39 @@ TEST(QuaternionAxisCosAngle, ozz_math) {

TEST(QuaternionQuaternionEuler, ozz_math) {
// Identity
EXPECT_QUATERNION_EQ(Quaternion::FromEuler(0.f, 0.f, 0.f), 0.f, 0.f, 0.f,
EXPECT_QUATERNION_EQ(Quaternion::FromEuler({0.f, 0.f, 0.f}), 0.f, 0.f, 0.f,
1.f);
EXPECT_FLOAT3_EQ(ToEuler(Quaternion::identity()), 0.f, 0.f, 0.f);

// Yaw
const Quaternion yaw = Quaternion::FromEuler(ozz::math::kPi_2, 0.f, 0.f);
const Quaternion yaw = Quaternion::FromEuler({ozz::math::kPi_2, 0.f, 0.f});
EXPECT_QUATERNION_EQ(yaw, 0.f, .70710677f, 0.f, .70710677f);
EXPECT_FLOAT3_EQ(ToEuler(Quaternion(0.f, .70710677f, 0.f, .70710677f)),
ozz::math::kPi_2, 0.f, 0.f);

// Pitch
const Quaternion pitch = Quaternion::FromEuler(0.f, ozz::math::kPi_2, 0.f);
const Quaternion pitch = Quaternion::FromEuler({0.f, ozz::math::kPi_2, 0.f});
EXPECT_QUATERNION_EQ(pitch, 0.f, 0.f, .70710677f, .70710677f);
EXPECT_FLOAT3_EQ(ToEuler(Quaternion(0.f, 0.f, .70710677f, .70710677f)), 0.f,
ozz::math::kPi_2, 0.f);

// Roll
const Quaternion roll = Quaternion::FromEuler(0.f, 0.f, ozz::math::kPi_2);
const Quaternion roll = Quaternion::FromEuler({0.f, 0.f, ozz::math::kPi_2});
EXPECT_QUATERNION_EQ(roll, .70710677f, 0.f, 0.f, .70710677f);
EXPECT_FLOAT3_EQ(ToEuler(Quaternion(.70710677f, 0.f, 0.f, .70710677f)), 0.f,
0.f, ozz::math::kPi_2);

// All
const Quaternion all = yaw * pitch * roll;
EXPECT_QUATERNION_EQ(Quaternion::FromEuler(ozz::math::kPi_2, ozz::math::kPi_2,
ozz::math::kPi_2),
all.x, all.y, all.z, all.w);
EXPECT_QUATERNION_EQ(
Quaternion::FromEuler(
{ozz::math::kPi_2, ozz::math::kPi_2, ozz::math::kPi_2}),
all.x, all.y, all.z, all.w);

// Any rotation
EXPECT_QUATERNION_EQ(
Quaternion::FromEuler(ozz::math::kPi / 4.f, -ozz::math::kPi / 6.f,
ozz::math::kPi_2),
Quaternion::FromEuler(
{ozz::math::kPi / 4.f, -ozz::math::kPi / 6.f, ozz::math::kPi_2}),
.56098551f, .092295974f, -0.43045932f, .70105737f);
EXPECT_FLOAT3_EQ(
ToEuler(Quaternion(.56098551f, .092295974f, -0.43045932f, .70105737f)),
Expand Down Expand Up @@ -319,14 +320,16 @@ TEST(QuaternionCompare, ozz_math) {
EXPECT_TRUE(Compare(Quaternion::identity(), Quaternion::identity(),
std::cos(.5f * 0.f)));
EXPECT_TRUE(Compare(Quaternion::identity(),
Quaternion::FromEuler(0.f, 0.f, ozz::math::kPi / 100.f),
std::cos(.5f * ozz::math::kPi / 50.f)));
EXPECT_TRUE(Compare(Quaternion::identity(),
-Quaternion::FromEuler(0.f, 0.f, ozz::math::kPi / 100.f),
Quaternion::FromEuler({0.f, 0.f, ozz::math::kPi / 100.f}),
std::cos(.5f * ozz::math::kPi / 50.f)));
EXPECT_FALSE(Compare(Quaternion::identity(),
Quaternion::FromEuler(0.f, 0.f, ozz::math::kPi / 100.f),
std::cos(.5f * ozz::math::kPi / 200.f)));
EXPECT_TRUE(
Compare(Quaternion::identity(),
-Quaternion::FromEuler({0.f, 0.f, ozz::math::kPi / 100.f}),
std::cos(.5f * ozz::math::kPi / 50.f)));
EXPECT_FALSE(
Compare(Quaternion::identity(),
Quaternion::FromEuler({0.f, 0.f, ozz::math::kPi / 100.f}),
std::cos(.5f * ozz::math::kPi / 200.f)));
}

TEST(QuaternionArithmetic, ozz_math) {
Expand Down

0 comments on commit bbab4fb

Please sign in to comment.