Skip to content

Commit

Permalink
Remove Degrees from getRotationAboutX/Y
Browse files Browse the repository at this point in the history
These methods are now using the same pattern as getRotationAboutZ, i.e.
accepting a templated unit class which can be either math::Degrees or
math::Radians.
  • Loading branch information
Matthew Mott committed Mar 21, 2021
1 parent 5c81357 commit af810ea
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
30 changes: 12 additions & 18 deletions libs/math/Matrix4.h
Expand Up @@ -101,13 +101,19 @@ class Matrix4
*/
static Matrix4 getRotationQuantised(const Quaternion& quaternion);

/// Construct a rotation matrix about the X axis for an angle in degrees
static Matrix4 getRotationAboutXDegrees(double angle);
/// Construct a rotation matrix about the X axis for the given angle
template<typename Unit_T> static Matrix4 getRotationAboutX(Unit_T angle)
{
double radians = angle.asRadians();
return getRotationAboutXForSinCos(sin(radians), cos(radians));
}

/**
* Constructs a pure-rotation matrix about the y axis from an angle in degrees.
*/
static Matrix4 getRotationAboutYDegrees(double angle);
/// Construct a rotation matrix about the Y axis for the given angle
template<typename Unit_T> static Matrix4 getRotationAboutY(Unit_T angle)
{
double radians = angle.asRadians();
return getRotationAboutYForSinCos(sin(radians), cos(radians));
}

/// Construct a rotation matrix about the Z axis for a given angle
template<typename Unit_T> static Matrix4 getRotationAboutZ(Unit_T angle)
Expand Down Expand Up @@ -540,18 +546,6 @@ inline Matrix4 Matrix4::getPremultipliedBy(const Matrix4& other) const
return other.getMultipliedBy(*this);
}

inline Matrix4 Matrix4::getRotationAboutXDegrees(double angle)
{
double radians = degrees_to_radians(angle);
return getRotationAboutXForSinCos(sin(radians), cos(radians));
}

inline Matrix4 Matrix4::getRotationAboutYDegrees(double angle)
{
double radians = degrees_to_radians(angle);
return getRotationAboutYForSinCos(sin(radians), cos(radians));
}

inline Matrix4 Matrix4::getProjectionForFrustum(double left, double right, double bottom, double top, double nearval, double farval)
{
return Matrix4::byColumns(
Expand Down
16 changes: 12 additions & 4 deletions radiantcore/selection/algorithm/Transformation.cpp
Expand Up @@ -540,11 +540,19 @@ void rotateSelectionAboutAxis(axis_t axis, float deg)
switch(axis)
{
case 0:
rotateSelected(Quaternion::createForMatrix(Matrix4::getRotationAboutXDegrees(deg)));
break;
rotateSelected(
Quaternion::createForMatrix(
Matrix4::getRotationAboutX(math::Degrees(deg))
)
);
break;
case 1:
rotateSelected(Quaternion::createForMatrix(Matrix4::getRotationAboutYDegrees(deg)));
break;
rotateSelected(
Quaternion::createForMatrix(
Matrix4::getRotationAboutY(math::Degrees(deg))
)
);
break;
case 2:
rotateSelected(
Quaternion::createForMatrix(
Expand Down
24 changes: 12 additions & 12 deletions test/math/Matrix4.cpp
Expand Up @@ -137,12 +137,12 @@ TEST(MathTest, ConvertDegreesAndRadians)

TEST(MathTest, MatrixRotationAboutXDegrees)
{
double angle = 30.0;
double cosAngle = cos(degrees_to_radians(angle));
double sinAngle = sin(degrees_to_radians(angle));
math::Degrees angle(30.0);
double cosAngle = cos(angle.asRadians());
double sinAngle = sin(angle.asRadians());

// Test X rotation
auto xRot = Matrix4::getRotationAboutXDegrees(angle);
auto xRot = Matrix4::getRotationAboutX(angle);
expectNear(xRot, Matrix4::byRows(1, 0, 0, 0,
0, cosAngle, -sinAngle, 0,
0, sinAngle, cosAngle, 0,
Expand All @@ -151,12 +151,12 @@ TEST(MathTest, MatrixRotationAboutXDegrees)

TEST(MathTest, MatrixRotationAboutYDegrees)
{
double angle = 30.0;
double cosAngle = cos(degrees_to_radians(angle));
double sinAngle = sin(degrees_to_radians(angle));
math::Degrees angle(45.0);
double cosAngle = cos(angle.asRadians());
double sinAngle = sin(angle.asRadians());

// Test Y rotation
auto yRot = Matrix4::getRotationAboutYDegrees(angle);
auto yRot = Matrix4::getRotationAboutY(angle);
expectNear(yRot, Matrix4::byRows(cosAngle, 0, sinAngle, 0,
0, 1, 0, 0,
-sinAngle, 0, cosAngle, 0,
Expand All @@ -165,12 +165,12 @@ TEST(MathTest, MatrixRotationAboutYDegrees)

TEST(MathTest, MatrixRotationAboutZDegrees)
{
double angle = 30.0;
double cosAngle = cos(degrees_to_radians(angle));
double sinAngle = sin(degrees_to_radians(angle));
math::Degrees angle(60.0);
double cosAngle = cos(angle.asRadians());
double sinAngle = sin(angle.asRadians());

// Test Z rotation
auto zRot = Matrix4::getRotationAboutZ(math::Degrees(angle));
auto zRot = Matrix4::getRotationAboutZ(angle);
expectNear(zRot, Matrix4::byRows(cosAngle, -sinAngle, 0, 0,
sinAngle, cosAngle, 0, 0,
0, 0, 1, 0,
Expand Down

0 comments on commit af810ea

Please sign in to comment.