Skip to content

Commit

Permalink
STYLE: Combine code MatrixOffsetTransformBase constructors
Browse files Browse the repository at this point in the history
Replaced separate (`protected`) default-constructor and converting
constructor (from `unsigned int paramDims`) by a singe `explicit`
constructor with default argument (still `protected` anyway).

Used C++11 in-class data member initializers.

Added extra GTest unit test to check `MatrixOffsetTransformBase::New()`.
  • Loading branch information
N-Dekker authored and dzenanz committed May 18, 2021
1 parent 68d73ba commit e6f5f96
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
15 changes: 7 additions & 8 deletions Modules/Core/Transform/include/itkMatrixOffsetTransformBase.h
Expand Up @@ -503,8 +503,7 @@ class ITK_TEMPLATE_EXPORT MatrixOffsetTransformBase
* omitted, then the MatrixOffsetTransformBase is initialized to an identity
* transformation in the appropriate number of dimensions. */
MatrixOffsetTransformBase(const MatrixType & matrix, const OutputVectorType & offset);
MatrixOffsetTransformBase(unsigned int paramDims);
MatrixOffsetTransformBase();
explicit MatrixOffsetTransformBase(unsigned int paramDims = ParametersDimension);

/** Destroy an MatrixOffsetTransformBase object */
~MatrixOffsetTransformBase() override = default;
Expand Down Expand Up @@ -577,13 +576,13 @@ class ITK_TEMPLATE_EXPORT MatrixOffsetTransformBase
itkGetConstMacro(Singular, bool);

private:
MatrixType m_Matrix; // Matrix of the transformation
OutputVectorType m_Offset; // Offset of the transformation
mutable InverseMatrixType m_InverseMatrix; // Inverse of the matrix
mutable bool m_Singular; // Is m_Inverse singular?
MatrixType m_Matrix{ MatrixType::GetIdentity() }; // Matrix of the transformation
OutputVectorType m_Offset{}; // Offset of the transformation
mutable InverseMatrixType m_InverseMatrix{ InverseMatrixType::GetIdentity() }; // Inverse of the matrix
mutable bool m_Singular{ false }; // Is m_Inverse singular?

InputPointType m_Center;
OutputVectorType m_Translation;
InputPointType m_Center{};
OutputVectorType m_Translation{};

/** To avoid recomputation of the inverse if not needed */
TimeStamp m_MatrixMTime;
Expand Down
34 changes: 3 additions & 31 deletions Modules/Core/Transform/include/itkMatrixOffsetTransformBase.hxx
Expand Up @@ -27,35 +27,12 @@
namespace itk
{

template <typename TParametersValueType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
MatrixOffsetTransformBase<TParametersValueType, NInputDimensions, NOutputDimensions>::MatrixOffsetTransformBase()
: Superclass(ParametersDimension)
{
m_Matrix.SetIdentity();
m_MatrixMTime.Modified();
m_Offset.Fill(0);
m_Center.Fill(0);
m_Translation.Fill(0);
m_Singular = false;
m_InverseMatrix.SetIdentity();
m_InverseMatrixMTime = m_MatrixMTime;
this->m_FixedParameters.SetSize(NInputDimensions);
this->m_FixedParameters.Fill(0.0);
}


template <typename TParametersValueType, unsigned int NInputDimensions, unsigned int NOutputDimensions>
MatrixOffsetTransformBase<TParametersValueType, NInputDimensions, NOutputDimensions>::MatrixOffsetTransformBase(
unsigned int paramDims)
: Superclass(paramDims)
{
m_Matrix.SetIdentity();
m_MatrixMTime.Modified();
m_Offset.Fill(0);
m_Center.Fill(0);
m_Translation.Fill(0);
m_Singular = false;
m_InverseMatrix.SetIdentity();
m_InverseMatrixMTime = m_MatrixMTime;
this->m_FixedParameters.SetSize(NInputDimensions);
this->m_FixedParameters.Fill(0.0);
Expand All @@ -66,16 +43,11 @@ template <typename TParametersValueType, unsigned int NInputDimensions, unsigned
MatrixOffsetTransformBase<TParametersValueType, NInputDimensions, NOutputDimensions>::MatrixOffsetTransformBase(
const MatrixType & matrix,
const OutputVectorType & offset)
: m_Matrix(matrix)
, m_Offset(offset)
{
m_Matrix = matrix;
m_MatrixMTime.Modified();
m_Offset = offset;
m_Center.Fill(0);
m_Translation.Fill(0);
for (unsigned int i = 0; i < NOutputDimensions; ++i)
{
m_Translation[i] = offset[i];
}
std::copy_n(offset.begin(), NOutputDimensions, m_Translation.begin());
this->ComputeMatrixParameters();
}

Expand Down
25 changes: 25 additions & 0 deletions Modules/Core/Transform/test/itkMatrixOffsetTransformBaseGTest.cxx
Expand Up @@ -24,6 +24,23 @@

namespace
{

template <typename TParametersValueType, unsigned NDimensions>
void
Check_New_MatrixOffsetTransformBase()
{
const auto transformBase = itk::MatrixOffsetTransformBase<TParametersValueType, NDimensions, NDimensions>::New();

EXPECT_TRUE(transformBase->GetMatrix().GetVnlMatrix().is_identity());

const auto zeroFilledFixedArray = itk::FixedArray<double, NDimensions>::Filled(0.0);

EXPECT_EQ(zeroFilledFixedArray, transformBase->GetOffset());
EXPECT_EQ(zeroFilledFixedArray, transformBase->GetCenter());
EXPECT_EQ(zeroFilledFixedArray, transformBase->GetTranslation());
}


template <unsigned int NDimensions>
void
Assert_SetFixedParameters_throws_when_size_is_less_than_NDimensions()
Expand All @@ -42,6 +59,14 @@ Assert_SetFixedParameters_throws_when_size_is_less_than_NDimensions()
} // namespace


// Checks the object created by MatrixOffsetTransformBase::New().
TEST(MatrixOffsetTransformBase, CheckNew)
{
Check_New_MatrixOffsetTransformBase<float, 2>();
Check_New_MatrixOffsetTransformBase<double, 3>();
}


TEST(MatrixOffsetTransformBase, SetFixedParametersThrowsWhenSizeIsLessThanNDimensions)
{
Assert_SetFixedParameters_throws_when_size_is_less_than_NDimensions<2>();
Expand Down

0 comments on commit e6f5f96

Please sign in to comment.