Skip to content

Commit

Permalink
Add trace function for matrix types (#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 Nov 16, 2022
1 parent 5d1792d commit 0436624
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 @@ -292,6 +292,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>
Expand Down Expand Up @@ -678,6 +681,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>
Expand Down Expand Up @@ -1162,6 +1168,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>
Expand Down Expand Up @@ -1915,6 +1924,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 @@ -3086,6 +3102,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 @@ -4592,6 +4615,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 @@ -640,6 +640,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 0436624

Please sign in to comment.