Implement Matrix4x4::Equals

This commit is contained in:
2024-05-27 13:35:11 -04:00
parent aa8bc4d1c4
commit 52547bca9d

View File

@@ -409,19 +409,9 @@ namespace J3ML::LinearAlgebra {
Vector3 GetScale() const;
Matrix4x4 Scale(const Vector3&);
float &At(int row, int col);
float At(int x, int y) const;
template <typename T>
void Swap(T &a, T &b)
{
T temp = std::move(a);
a = std::move(b);
b = std::move(temp);
}
void SwapColumns(int col1, int col2);
@@ -434,7 +424,7 @@ namespace J3ML::LinearAlgebra {
void ScaleRow3(int row, float scalar);
void ScaleColumn(int col, float scalar);
void ScaleColumn3(int col, float scalar);
/// Algorithm from Eric Lengyel's Mathematics for 3D Game Programming & Computer Graphics, 2nd Ed.
/// Reduces this matrix to its row-echelon form.
void Pivot();
/// Tests if this matrix does not contain any NaNs or infs.
@@ -487,12 +477,9 @@ namespace J3ML::LinearAlgebra {
Vector4 Transform(float tx, float ty, float tz, float tw) const;
Vector4 Transform(const Vector4& rhs) const;
Matrix4x4 Translate(const Vector3& rhs) const;
static Matrix4x4 FromTranslation(const Vector3& rhs);
Vector4 operator[](int row);
Matrix4x4 operator-() const;
@@ -531,6 +518,17 @@ namespace J3ML::LinearAlgebra {
@note This function assumes that this matrix does not contain projection (the fourth row of this matrix is [0 0 0 1]). */
Vector3 ExtractScale() const;
/// Removes the scaling performed by this matrix. That is, decomposes this matrix M into a form M = M' * S, where
/// M' has unitary column vectors and S is a diagonal matrix. Then replaces this matrix with M'.
/// @note This function assumes that this matrix does not contain projection (the fourth row of this matrix is [0 0 0 1]).
/// @note This function assumes that this matrix has orthogonal basis vectors (row and column vector sets are orthogonal).
/// @note This function does not remove reflection (-1 scale along some axis).
void RemoveScale()
{
float tx = Row3(0).Normalize();
float ty = Row3(1).Normalize();
}
/// Decomposes this matrix to translate, rotate, and scale parts.
/** This function decomposes this matrix M to a form M = T * R * S, where T is a translation matrix, R is a rotation matrix, and S is a scale matrix
@@ -541,27 +539,9 @@ namespace J3ML::LinearAlgebra {
after rotation and scaling.
@param rotate [out] This object receives the rotation part of this transform.
@param scale [out] This vector receives the scaling along the local (before transformation by R) X, Y, and Z axes performed by this matrix. */
void Decompose(Vector3& translate, Quaternion& rotate, Vector3& scale) const
{
assert(this->IsColOrthogonal3());
Matrix3x3 r;
Decompose(translate, r, scale);
rotate = Quaternion(r);
/// Test that composing back yields the original Matrix4x4.
assert(Matrix4x4::FromTRS(translate, rotate, scale).Equals(*this, 0.1f));
}
void Decompose(Vector3& translate, Quaternion& rotate, Vector3& scale) const;
void Decompose(Vector3& translate, Matrix3x3& rotate, Vector3& scale) const;
void Decompose(Vector3& translate, Matrix4x4& rotate, Vector3& scale) const
{
assert(this->IsColOrthogonal3());
Matrix3x3 r;
Decompose(translate, r, scale);
rotate.SetRotatePart(r);
rotate.SetTranslatePart(0,0,0);
}
void Decompose(Vector3& translate, Matrix4x4& rotate, Vector3& scale) const;
/// Returns true if this matrix only contains uniform scaling, compared to the given epsilon.
/// @note If the matrix does not really do any scaling, this function returns true (scaling uniformly by a factor of 1).
@@ -666,6 +646,10 @@ namespace J3ML::LinearAlgebra {
/// Returns the sum of the diagonal elements of this matrix.
float Trace() const;
/// Returns true if this Matrix4x4 is equal to the given Matrix4x4, up to given per-element epsilon.
bool Equals(const Matrix4x4& other, float epsilon = 1e-3f) const;
protected:
float elems[4][4];