Skip to content

Commit

Permalink
Add trace function for matrix types (AcademySoftwareFoundation#280)
Browse files Browse the repository at this point in the history
* Add trace function for matrix types

Signed-off-by: Xiao Zhai <[email protected]>

* Add tests for the matrix trace function

Signed-off-by: Xiao Zhai <[email protected]>

* Fix issue in extractQuat introduced by incorrect use of matrix trace

Signed-off-by: Xiao Zhai <[email protected]>

Signed-off-by: Xiao Zhai <[email protected]>
  • Loading branch information
zhai-xiao authored and cary-ilm committed Feb 27, 2023
1 parent e7dcea2 commit 6d5e2d8
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Imath/ImathMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Matrix22
/// Determinant
IMATH_HOSTDEVICE constexpr T determinant() const IMATH_NOEXCEPT;

/// Trace
IMATH_HOSTDEVICE constexpr T trace() const IMATH_NOEXCEPT;

/// Set matrix to rotation by r (in radians)
/// @return const referenced to this
template <class S> IMATH_HOSTDEVICE const Matrix22& setRotation (S r) IMATH_NOEXCEPT;
Expand Down Expand Up @@ -573,6 +576,9 @@ template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Matrix33
/// Determinant
IMATH_HOSTDEVICE constexpr T determinant() const IMATH_NOEXCEPT;

/// Trace
IMATH_HOSTDEVICE constexpr T trace() const IMATH_NOEXCEPT;

/// Set matrix to rotation by r (in radians)
/// @return const referenced to this
template <class S> IMATH_HOSTDEVICE const Matrix33& setRotation (S r) IMATH_NOEXCEPT;
Expand Down Expand Up @@ -947,6 +953,9 @@ template <class T> class IMATH_EXPORT_TEMPLATE_TYPE Matrix44
/// Determinant
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T determinant() const IMATH_NOEXCEPT;

/// Trace
IMATH_HOSTDEVICE constexpr T trace() const IMATH_NOEXCEPT;

/// Set matrix to rotation by XYZ euler angles (in radians)
/// @return const referenced to this
template <class S> IMATH_HOSTDEVICE const Matrix44& setEulerAngles (const Vec3<S>& r) IMATH_NOEXCEPT;
Expand Down Expand Up @@ -1638,6 +1647,13 @@ Matrix22<T>::determinant() const IMATH_NOEXCEPT
return x[0][0] * x[1][1] - x[1][0] * x[0][1];
}

template <class T>
IMATH_HOSTDEVICE constexpr inline T
Matrix22<T>::trace () const IMATH_NOEXCEPT
{
return x[0][0] + x[1][1];
}

template <class T>
template <class S>
IMATH_HOSTDEVICE inline const Matrix22<T>&
Expand Down Expand Up @@ -2764,6 +2780,13 @@ Matrix33<T>::determinant() const IMATH_NOEXCEPT
x[0][2] * (x[1][0] * x[2][1] - x[1][1] * x[2][0]);
}

template <class T>
IMATH_HOSTDEVICE constexpr inline T
Matrix33<T>::trace () const IMATH_NOEXCEPT
{
return x[0][0] + x[1][1] + x[2][2];
}

template <class T>
template <class S>
IMATH_HOSTDEVICE inline const Matrix33<T>&
Expand Down Expand Up @@ -4202,6 +4225,13 @@ Matrix44<T>::determinant() const IMATH_NOEXCEPT
return sum;
}

template <class T>
IMATH_HOSTDEVICE constexpr inline T
Matrix44<T>::trace () const IMATH_NOEXCEPT
{
return x[0][0] + x[1][1] + x[2][2] + x[3][3];
}

template <class T>
template <class S>
IMATH_HOSTDEVICE inline const Matrix44<T>&
Expand Down
110 changes: 110 additions & 0 deletions src/ImathTest/testMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,116 @@ testMatrix()
u.baseTypeEpsilon());
}

// Trace
{
cout << "2x2 trace" << endl;

IMATH_INTERNAL_NAMESPACE::Rand32 random;

IMATH_INTERNAL_NAMESPACE::M22f u;
float trace = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
const float randomNum = random.nextf ();
u[i][j] = randomNum;
if (i == j) { trace += randomNum; }
}
}

assert (fabsf (u.trace () - trace) <= u.baseTypeEpsilon ());
}
{
IMATH_INTERNAL_NAMESPACE::Rand32 random;

IMATH_INTERNAL_NAMESPACE::M22d u;
double trace = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
const double randomNum = random.nextf ();
u[i][j] = randomNum;
if (i == j) { trace += randomNum; }
}
}

assert (fabsf (u.trace () - trace) <= u.baseTypeEpsilon ());
}
{
cout << "3x3 trace" << endl;

IMATH_INTERNAL_NAMESPACE::Rand32 random;

IMATH_INTERNAL_NAMESPACE::M33f u;
float trace = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
const float randomNum = random.nextf ();
u[i][j] = randomNum;
if (i == j) { trace += randomNum; }
}
}

assert (fabsf (u.trace () - trace) <= u.baseTypeEpsilon ());
}
{
IMATH_INTERNAL_NAMESPACE::Rand32 random;

IMATH_INTERNAL_NAMESPACE::M33d u;
double trace = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
const double randomNum = random.nextf ();
u[i][j] = randomNum;
if (i == j) { trace += randomNum; }
}
}

assert (fabsf (u.trace () - trace) <= u.baseTypeEpsilon ());
}
{
cout << "4x4 trace" << endl;

IMATH_INTERNAL_NAMESPACE::Rand32 random;

IMATH_INTERNAL_NAMESPACE::M44f u;
float trace = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
const float randomNum = random.nextf ();
u[i][j] = randomNum;
if (i == j) { trace += randomNum; }
}
}

assert (fabsf (u.trace () - trace) <= u.baseTypeEpsilon ());
}
{
IMATH_INTERNAL_NAMESPACE::Rand32 random;

IMATH_INTERNAL_NAMESPACE::M44d u;
double trace = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
const double randomNum = random.nextf ();
u[i][j] = randomNum;
if (i == j) { trace += randomNum; }
}
}

assert (fabsf (u.trace () - trace) <= u.baseTypeEpsilon ());
}

// Matrix minors
{
cout << "4x4 matrix minors" << endl;
Expand Down

0 comments on commit 6d5e2d8

Please sign in to comment.