Update return type for viewport screen functions (#5803)

* update return type for viewport screen functions

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* add tests for AZ::Matrix3x4::CreateFromMatrix4x4 and add TransformPoint to Matrix3x4

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* update NDC -> Ndc

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* updates following review feedback

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* updates and improvements following PR feedback

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* add forward declaration of Matrix3x4 type

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>

* update where forward declarations are defined for Matrix3x4

Signed-off-by: Tom Hulton-Harrop <82228511+hultonha@users.noreply.github.com>
This commit is contained in:
Tom Hulton-Harrop
2021-11-24 09:17:43 +00:00
committed by GitHub
parent b196473465
commit 5bd751531d
20 changed files with 146 additions and 74 deletions
@@ -351,7 +351,8 @@ namespace AZ
->Method("CreateFromMatrix3x3AndTranslation", &Matrix3x4::CreateFromMatrix3x3AndTranslation)
->Method("CreateScale", &Matrix3x4::CreateScale)
->Method("CreateDiagonal", &Matrix3x4::CreateDiagonal)
->Method("CreateTranslation", &Matrix3x4::CreateTranslation);
->Method("CreateTranslation", &Matrix3x4::CreateTranslation)
->Method("UnsafeCreateFromMatrix4x4", &Matrix3x4::UnsafeCreateFromMatrix4x4);
}
}
@@ -90,6 +90,9 @@ namespace AZ
//! Constructs from a Matrix3x3 and a translation.
static Matrix3x4 CreateFromMatrix3x3AndTranslation(const Matrix3x3& matrix3x3, const Vector3& translation);
//! Constructs from a Matrix4x4.
static Matrix3x4 UnsafeCreateFromMatrix4x4(const Matrix4x4& matrix4x4);
//! Constructs from a Transform.
static Matrix3x4 CreateFromTransform(const Transform& transform);
@@ -227,7 +230,7 @@ namespace AZ
Matrix3x4& operator+=(const Matrix3x4& rhs);
//! @}
//! Operator for matrix-matrix substraction.
//! Operator for matrix-matrix subtraction.
//! @{
[[nodiscard]] Matrix3x4 operator-(const Matrix3x4& rhs) const;
Matrix3x4& operator-=(const Matrix3x4& rhs);
@@ -266,6 +269,9 @@ namespace AZ
//! Post-multiplies the matrix by a vector, using only the 3x3 part of the matrix.
[[nodiscard]] Vector3 TransformVector(const Vector3& rhs) const;
//! Post-multiplies the matrix by a point, using the rotation and translation part of the matrix.
[[nodiscard]] Vector3 TransformPoint(const Vector3& rhs) const;
//! Gets the result of transposing the 3x3 part of the matrix, setting the translation part to zero.
[[nodiscard]] Matrix3x4 GetTranspose() const;
@@ -203,6 +203,16 @@ namespace AZ
}
AZ_MATH_INLINE Matrix3x4 Matrix3x4::UnsafeCreateFromMatrix4x4(const Matrix4x4& matrix4x4)
{
Matrix3x4 result;
result.SetRow(0, matrix4x4.GetRow(0));
result.SetRow(1, matrix4x4.GetRow(1));
result.SetRow(2, matrix4x4.GetRow(2));
return result;
}
AZ_MATH_INLINE Matrix3x4 Matrix3x4::CreateScale(const Vector3& scale)
{
return CreateDiagonal(scale);
@@ -609,6 +619,12 @@ namespace AZ
}
AZ_MATH_INLINE Vector3 Matrix3x4::TransformPoint(const Vector3& rhs) const
{
return Multiply3x3(rhs) + GetTranslation();
}
AZ_MATH_INLINE Matrix3x4 Matrix3x4::GetTranspose() const
{
Matrix3x4 result;
@@ -33,6 +33,15 @@ namespace MathTestData
AZ::Matrix3x3::CreateScale(AZ::Vector3(0.7f, 1.3f, 0.9f))
};
static const AZ::Matrix4x4 Matrix4x4s[] = {
AZ::Matrix4x4::CreateIdentity(),
AZ::Matrix4x4::CreateFromQuaternionAndTranslation(AZ::Quaternion(-0.46f, 0.26f, -0.22f, 0.82f), AZ::Vector3(1.0f, 5.0f, 10.0f)),
AZ::Matrix4x4::CreateFromTransform(AZ::Transform::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateScale(AZ::Vector3(1.0f, 2.0f, 3.0f)), AZ::Vector3(2.0f, 4.0f, 6.0f))),
AZ::Matrix4x4::CreateScale(AZ::Vector3(5.0f, 10.0f, 15.0f)),
AZ::Matrix4x4::CreateRotationZ(AZ::DegToRad(45.0f))
};
using AxisPair = AZStd::pair<AZ::Constants::Axis, AZ::Vector3>;
static const AxisPair Axes[] = {
{ AZ::Constants::Axis::XPositive, AZ::Vector3::CreateAxisX(1.0f) },
@@ -10,6 +10,7 @@
#include <AzCore/Math/Matrix3x4.h>
#include <AzCore/Math/Matrix3x3.h>
#include <AzCore/Math/Quaternion.h>
#include <AzCore/Math/VectorConversions.h>
#include <AZTestShared/Math/MathTestHelpers.h>
#include "MathTestData.h"
@@ -392,6 +393,32 @@ namespace UnitTest
INSTANTIATE_TEST_CASE_P(MATH_Matrix3x4, Matrix3x4CreateFromMatrix3x3Fixture, ::testing::ValuesIn(MathTestData::Matrix3x3s));
using Matrix3x4CreateFromMatrix4x4Fixture = ::testing::TestWithParam<AZ::Matrix4x4>;
TEST_P(Matrix3x4CreateFromMatrix4x4Fixture, UnsafeCreateFromMatrix4x4)
{
const AZ::Matrix4x4 matrix4x4 = GetParam();
const AZ::Matrix3x4 matrix3x4 = AZ::Matrix3x4::UnsafeCreateFromMatrix4x4(matrix4x4);
EXPECT_THAT(matrix3x4.GetTranslation(), IsClose(matrix4x4.GetTranslation()));
const AZ::Vector3 vector(2.3f, -0.6, 1.8f);
EXPECT_THAT(matrix3x4.TransformVector(vector), IsClose((matrix4x4 * AZ::Vector3ToVector4(vector, 0.0f)).GetAsVector3()));
const AZ::Vector3 point(12.3f, -5.6, 7.3f);
EXPECT_THAT(matrix3x4.TransformPoint(point), IsClose((matrix4x4 * AZ::Vector3ToVector4(point, 1.0f)).GetAsVector3()));
}
INSTANTIATE_TEST_CASE_P(MATH_Matrix3x4, Matrix3x4CreateFromMatrix4x4Fixture, ::testing::ValuesIn(MathTestData::Matrix4x4s));
TEST(MATH_Matrix3x4, TransformPoint)
{
const AZ::Matrix3x4 matrix3x4 = AZ::Matrix3x4::CreateFromMatrix3x3AndTranslation(
AZ::Matrix3x3::CreateRotationY(AZ::DegToRad(90.0f)), AZ::Vector3(5.0f, 0.0f, 0.0f));
const AZ::Vector3 result = matrix3x4.TransformPoint(AZ::Vector3(1.0f, 0.0f, 0.0f));
const AZ::Vector3 expected = AZ::Vector3(5.0f, 0.0f, -1.0f);
EXPECT_THAT(result, expected);
}
TEST(MATH_Matrix3x4, CreateScale)
{
const AZ::Vector3 scale(1.7f, 0.3f, 2.4f);