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

ENH: Suppress "Bad Rotation Matrix" warnings #1697

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Changes from all commits
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
45 changes: 43 additions & 2 deletions ImageRegistration/itkANTSCenteredAffine2DTransform.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define _itkANTSCenteredAffine2DTransform_hxx

// #include "itkRigid2DTransform.h"
#include "itkMath.h"
#include "vnl/algo/vnl_qr.h"

namespace itk
Expand Down Expand Up @@ -133,9 +134,49 @@ ANTSCenteredAffine2DTransform<TScalarType>::ComputeMatrixParameters(void)

this->ComputeMatrix();

if (static_cast<double>(this->GetMatrix()[1][0]) - static_cast<double>(std::sin(m_Angle)) > 0.000001)
if (this->GetDebug())
{
itkWarningMacro("Bad Rotation Matrix " << this->GetMatrix());
// check that the matrix computed from the parameters contains a valid rotation of m_Angle
const TMatrix U{ this->GetMatrix().GetVnlMatrix() };
vnl_qr<ScalarType> myqr_updated(U.as_matrix());

// Note switch of Q and R from vnl names here!
R = myqr_updated.Q(); // Q() is the rotation
Q = myqr_updated.R(); // R() is the upper triangluar

TMatrix dq_updated(itk::NumericTraits<TScalarType>::ZeroValue());
for (unsigned i = 0; i < 2; i++)
{
dq_updated(i, i) = (Q(i, i) >= 0) ? 1 : -1;
}

R = R * dq_updated;
Q = dq_updated * Q;

double angleCheck = static_cast<double>(std::acos(R[0][0]));

if (R[1][0] < itk::NumericTraits<TScalarType>::ZeroValue())
{
angleCheck = -angleCheck;
}

double tolerance = 1e-4;

if (std::abs(angleCheck - static_cast<double>(m_Angle)) > tolerance)
{
itkWarningMacro("Bad rotation in affine transform matrix " << this->GetMatrix() << std::endl
<< "Angle = " << angleCheck << std::endl
<< "acos(R[0][0]) = " << std::acos(R[0][0]) << std::endl
<< "cos(Angle) = " << std::cos(angleCheck) << std::endl
<< "R[0][0] = " << R[0][0]);
}
if (std::abs(static_cast<double>(R[1][0]) - static_cast<double>(std::sin(angleCheck))) > tolerance)
{
itkWarningMacro("Bad rotation in affine transform matrix " << this->GetMatrix() << std::endl
<< "Angle = " << angleCheck << std::endl
<< "sin(Angle) = " << std::sin(angleCheck) << std::endl
<< "R[1][0] = " << R[1][0]);
}
}
}

Expand Down