Skip to content

Commit

Permalink
Merge pull request #4402 from N-Dekker/Remove-redundant-GetInverseMat…
Browse files Browse the repository at this point in the history
…rix-calls
  • Loading branch information
dzenanz committed Jan 19, 2024
2 parents 4e59823 + f8075a7 commit 5cf4a72
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ CenteredSimilarity2DTransform<TParametersValueType>::GetInverse(Self * inverse)
}

inverse->SetFixedParameters(this->GetFixedParameters());
this->GetInverseMatrix();
const auto & inverseMatrix = this->GetInverseMatrix();
if (this->GetSingular())
{
return false;
}
inverse->SetCenter(this->GetCenter()); // inverse have the same center
inverse->SetScale(1.0 / this->GetScale());
inverse->SetAngle(-this->GetAngle());
inverse->SetTranslation(-(this->GetInverseMatrix() * this->GetTranslation()));
inverse->SetTranslation(-(inverseMatrix * this->GetTranslation()));
return true;
}

Expand Down
35 changes: 25 additions & 10 deletions Modules/Core/Transform/include/itkMatrixOffsetTransformBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ MatrixOffsetTransformBase<TParametersValueType, VInputDimension, VOutputDimensio
os << indent << "Translation: " << m_Translation << std::endl;

os << indent << "Inverse: " << std::endl;

const auto & inverseMatrix = this->GetInverseMatrix();

for (i = 0; i < VInputDimension; ++i)
{
os << indent.GetNextIndent();
for (j = 0; j < VOutputDimension; ++j)
{
os << this->GetInverseMatrix()[i][j] << ' ';
os << inverseMatrix[i][j] << ' ';
}
os << std::endl;
}
Expand Down Expand Up @@ -199,13 +202,14 @@ MatrixOffsetTransformBase<TParametersValueType, VInputDimension, VOutputDimensio
{
OutputCovariantVectorType result; // Converted vector

const auto & inverseMatrix = this->GetInverseMatrix();

for (unsigned int i = 0; i < VOutputDimension; ++i)
{
result[i] = NumericTraits<ScalarType>::ZeroValue();
for (unsigned int j = 0; j < VInputDimension; ++j)
{
result[i] += this->GetInverseMatrix()[j][i] * vec[j]; // Inverse
// transposed
result[i] += inverseMatrix[j][i] * vec[j]; // Inverse transposed
}
}
return result;
Expand All @@ -222,14 +226,17 @@ MatrixOffsetTransformBase<TParametersValueType, VInputDimension, VOutputDimensio

vnl_vector<TParametersValueType> vnl_vect(vectorDim);
vnl_matrix<TParametersValueType> vnl_mat(vectorDim, vect.Size(), 0.0);

const auto & inverseMatrix = this->GetInverseMatrix();

for (unsigned int i = 0; i < vectorDim; ++i)
{
vnl_vect[i] = vect[i];
for (unsigned int j = 0; j < vectorDim; ++j)
{
if ((i < VInputDimension) && (j < VInputDimension))
{
vnl_mat(i, j) = this->GetInverseMatrix()(j, i);
vnl_mat(i, j) = inverseMatrix(j, i);
}
else if (i == j)
{
Expand Down Expand Up @@ -258,11 +265,14 @@ MatrixOffsetTransformBase<TParametersValueType, VInputDimension, VOutputDimensio

JacobianType jacobian;
jacobian.SetSize(InverseMatrixType::RowDimensions, InverseMatrixType::ColumnDimensions);

const auto & inverseMatrix = this->GetInverseMatrix();

for (unsigned int i = 0; i < InverseMatrixType::RowDimensions; ++i)
{
for (unsigned int j = 0; j < InverseMatrixType::ColumnDimensions; ++j)
{
jacobian(i, j) = this->GetInverseMatrix()(i, j);
jacobian(i, j) = inverseMatrix(i, j);
}
}

Expand Down Expand Up @@ -321,12 +331,15 @@ MatrixOffsetTransformBase<TParametersValueType, VInputDimension, VOutputDimensio
}
}


const auto & inverseMatrix = this->GetInverseMatrix();

for (unsigned int i = 0; i < VInputDimension; ++i)
{
for (unsigned int j = 0; j < VOutputDimension; ++j)
{
jacobian(j, i) = this->GetMatrix()(j, i);
invJacobian(i, j) = this->GetInverseMatrix()(i, j);
invJacobian(i, j) = inverseMatrix(i, j);
}
}

Expand Down Expand Up @@ -365,12 +378,14 @@ MatrixOffsetTransformBase<TParametersValueType, VInputDimension, VOutputDimensio
}
}

const auto & inverseMatrix = this->GetInverseMatrix();

for (unsigned int i = 0; i < VInputDimension; ++i)
{
for (unsigned int j = 0; j < VOutputDimension; ++j)
{
jacobian(j, i) = this->GetMatrix()(j, i);
invJacobian(i, j) = this->GetInverseMatrix()(i, j);
invJacobian(i, j) = inverseMatrix(i, j);
}
}

Expand Down Expand Up @@ -425,15 +440,15 @@ MatrixOffsetTransformBase<TParametersValueType, VInputDimension, VOutputDimensio
}

inverse->SetFixedParameters(this->GetFixedParameters());
this->GetInverseMatrix();
const auto & inverseMatrix = this->GetInverseMatrix();
if (m_Singular)
{
return false;
}

inverse->m_Matrix = this->GetInverseMatrix();
inverse->m_Matrix = inverseMatrix;
inverse->m_InverseMatrix = m_Matrix;
inverse->m_Offset = -(this->GetInverseMatrix() * m_Offset);
inverse->m_Offset = -(inverseMatrix * m_Offset);
inverse->ComputeTranslation();
inverse->ComputeMatrixParameters();

Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Transform/include/itkSimilarity2DTransform.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,15 @@ Similarity2DTransform<TParametersValueType>::GetInverse(Self * inverse) const
}

inverse->SetFixedParameters(this->GetFixedParameters());
this->GetInverseMatrix();
const auto & inverseMatrix = this->GetInverseMatrix();
if (this->GetSingular())
{
return false;
}
inverse->SetCenter(this->GetCenter()); // inverse have the same center
inverse->SetScale(1.0 / this->GetScale());
inverse->SetAngle(-this->GetAngle());
inverse->SetTranslation(-(this->GetInverseMatrix() * this->GetTranslation()));
inverse->SetTranslation(-(inverseMatrix * this->GetTranslation()));

return true;
}
Expand Down

0 comments on commit 5cf4a72

Please sign in to comment.