Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trace function for matrix types #280

Merged
merged 3 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 3 additions & 4 deletions src/Imath/ImathMatrixAlgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -779,18 +779,17 @@ extractQuat (const Matrix44<T>& mat)
{
Matrix44<T> rot;

T tr, s;
T s;
T q[4];
int i, j, k;
Quat<T> quat;

int nxt[3] = {1, 2, 0};
tr = mat[0][0] + mat[1][1] + mat[2][2];
zhai-xiao marked this conversation as resolved.
Show resolved Hide resolved

// check the diagonal
if (tr > 0.0)
if (mat.trace () > 0.0)
{
s = std::sqrt (tr + T (1.0));
s = std::sqrt (mat.trace () + T (1.0));
quat.r = s / T (2.0);
s = T (0.5) / s;

Expand Down