Skip to content

Commit

Permalink
Add unit tests for Quaternion::createForX/Y/Z
Browse files Browse the repository at this point in the history
Confirm that the generated Quaternion contains the values derived from the
half-angle, and is equal to that returned from createForMatrix() with the
equivalent rotation matrix.
  • Loading branch information
Matthew Mott committed Mar 21, 2021
1 parent af810ea commit 4ce21a8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
11 changes: 9 additions & 2 deletions libs/math/Quaternion.h
Expand Up @@ -42,7 +42,7 @@ class Quaternion :
*/
static Quaternion createForUnitVectors(const Vector3& from, const Vector3& to);

/**
/**
* Constructs a rotation quaternion for the given euler angles.
* Each component of the given vector refers to an angle (in degrees)
* of one of the x-/y-/z-axis.
Expand All @@ -54,7 +54,7 @@ class Quaternion :
*/
static Quaternion createForAxisAngle(const Vector3& axis, double angle);

/**
/**
* Constructs a rotation quaternion about a given axis.
*/
static Quaternion createForX(double angle);
Expand Down Expand Up @@ -227,3 +227,10 @@ inline Vector3 Quaternion::transformPoint(const Vector3& point) const

const double c_half_sqrt2 = 0.70710678118654752440084436210485;
const float c_half_sqrt2f = static_cast<float>(c_half_sqrt2);

/// Stream insertion for Quaternion
inline std::ostream& operator<< (std::ostream& s, const Quaternion& q)
{
return s << "Quaternion(x=" << q.x() << ", y=" << q.y() << ", z=" << q.z()
<< ", w=" << q.w() << ")";
}
43 changes: 43 additions & 0 deletions test/math/Quaternion.cpp
@@ -1,10 +1,53 @@
#include "gtest/gtest.h"

#include "math/Quaternion.h"
#include "math/Matrix4.h"

namespace test
{

// Fuzzy equality assertion for Quaternions
void expectNear(const Quaternion& q1, const Quaternion& q2)
{
EXPECT_DOUBLE_EQ(q1.x(), q2.x());
EXPECT_DOUBLE_EQ(q1.y(), q2.y());
EXPECT_DOUBLE_EQ(q1.z(), q2.z());
EXPECT_DOUBLE_EQ(q1.w(), q2.w());
}

TEST(MathTest, QuaternionForXRotation)
{
math::Degrees angle(45);
Quaternion xRot = Quaternion::createForX(angle.asRadians());

expectNear(xRot, Quaternion(sin(angle.asRadians() / 2), 0, 0,
cos(angle.asRadians() / 2)));
expectNear(xRot,
Quaternion::createForMatrix(Matrix4::getRotationAboutX(angle)));
}

TEST(MathTest, QuaternionForYRotation)
{
math::Degrees angle(60);
Quaternion yRot = Quaternion::createForY(angle.asRadians());

expectNear(yRot, Quaternion(0, sin(angle.asRadians() / 2), 0,
cos(angle.asRadians() / 2)));
expectNear(yRot,
Quaternion::createForMatrix(Matrix4::getRotationAboutY(angle)));
}

TEST(MathTest, QuaternionForZRotation)
{
math::Degrees angle(75);
Quaternion zRot = Quaternion::createForZ(angle.asRadians());

expectNear(zRot, Quaternion(0, 0, sin(angle.asRadians() / 2),
cos(angle.asRadians() / 2)));
expectNear(zRot,
Quaternion::createForMatrix(Matrix4::getRotationAboutZ(angle)));
}

TEST(MathTest, QuaternionMultiplication)
{
Quaternion q1(3, 5, 7, 11);
Expand Down

0 comments on commit 4ce21a8

Please sign in to comment.