Skip to content

Commit

Permalink
STYLE: MultiTransform iterate over m_TransformQueue by range-based for
Browse files Browse the repository at this point in the history
Simplified the implementation of `MultiTransform` member functions, by using
range-based `for` loops to iterate over its `m_TransformQueue` data member.
  • Loading branch information
N-Dekker authored and hjmjohnson committed Jun 20, 2023
1 parent 9a2379f commit da9ecbb
Showing 1 changed file with 23 additions and 40 deletions.
63 changes: 23 additions & 40 deletions Modules/Core/Transform/include/itkMultiTransform.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,15 @@ MultiTransform<TParametersValueType, VDimension, VSubDimensions>::GetParameters(
/* Resize destructively. But if it's already this size, nothing is done so
* it's efficient. */
this->m_Parameters.SetSize(this->GetNumberOfParameters());
NumberOfParametersType offset{};
TransformQueueType transforms = this->GetTransformQueue();
typename TransformQueueType::const_iterator it;
it = transforms.begin();
NumberOfParametersType offset{};

do
for (const TransformType * const transform : m_TransformQueue)
{
const ParametersType & subParameters = (*it)->GetParameters();
const ParametersType & subParameters = transform->GetParameters();
/* use vnl_vector data_block() to get data ptr */
std::copy_n(subParameters.data_block(), subParameters.Size(), &(this->m_Parameters.data_block())[offset]);
offset += subParameters.Size();
++it;
} while (it != transforms.end());
}

return this->m_Parameters;
}
Expand All @@ -109,11 +105,9 @@ MultiTransform<TParametersValueType, VDimension, VSubDimensions>::SetParameters(
<< this->GetNumberOfParameters() << '.');
}

TransformQueueType transforms = this->GetTransformQueue();
NumberOfParametersType offset{};
auto it = transforms.begin();

do
for (TransformType * const transform : m_TransformQueue)
{
/* If inputParams is same object as m_Parameters, we just pass
* each sub-transforms own m_Parameters in. This is needed to
Expand All @@ -122,17 +116,16 @@ MultiTransform<TParametersValueType, VDimension, VSubDimensions>::SetParameters(
* parameters to update member variable states. A hack. */
if (&inputParameters == &this->m_Parameters)
{
(*it)->SetParameters((*it)->GetParameters());
transform->SetParameters(transform->GetParameters());
}
else
{
const size_t parameterSize = (*it)->GetParameters().Size();
(*it)->CopyInParameters(&(inputParameters.data_block())[offset],
&(inputParameters.data_block())[offset] + parameterSize);
const size_t parameterSize = transform->GetParameters().Size();
transform->CopyInParameters(&(inputParameters.data_block())[offset],
&(inputParameters.data_block())[offset] + parameterSize);
offset += static_cast<NumberOfParametersType>(parameterSize);
}
++it;
} while (it != transforms.end());
}
}


Expand All @@ -145,20 +138,16 @@ MultiTransform<TParametersValueType, VDimension, VSubDimensions>::GetFixedParame
* it's efficient. */
this->m_FixedParameters.SetSize(this->GetNumberOfFixedParameters());

NumberOfParametersType offset{};
typename TransformQueueType::const_iterator it;
TransformQueueType transforms = this->GetTransformQueue();
it = transforms.begin();
NumberOfParametersType offset{};

do
for (const TransformType * const transform : m_TransformQueue)
{
const FixedParametersType & subFixedParameters = (*it)->GetFixedParameters();
const FixedParametersType & subFixedParameters = transform->GetFixedParameters();
/* use vnl_vector data_block() to get data ptr */
std::copy_n(
subFixedParameters.data_block(), subFixedParameters.Size(), &(this->m_FixedParameters.data_block())[offset]);
offset += subFixedParameters.Size();
++it;
} while (it != transforms.end());
}

return this->m_FixedParameters;
}
Expand All @@ -178,21 +167,18 @@ MultiTransform<TParametersValueType, VDimension, VSubDimensions>::SetFixedParame

/* Assumes input params are concatenation of the parameters of the
* sub transforms. */
TransformQueueType transforms = this->GetTransformQueue();
NumberOfParametersType offset{};

/* Why is this done? Seems unnecessary. */
this->m_FixedParameters = inputParameters;

auto it = transforms.begin();
do
for (TransformType * const transform : m_TransformQueue)
{
const size_t fixedParameterSize = (*it)->GetFixedParameters().Size();
(*it)->CopyInFixedParameters(&(this->m_FixedParameters.data_block())[offset],
&(this->m_FixedParameters.data_block())[offset] + fixedParameterSize);
const size_t fixedParameterSize = transform->GetFixedParameters().Size();
transform->CopyInFixedParameters(&(this->m_FixedParameters.data_block())[offset],
&(this->m_FixedParameters.data_block())[offset] + fixedParameterSize);
offset += static_cast<NumberOfParametersType>(fixedParameterSize);
++it;
} while (it != transforms.end());
}
}


Expand Down Expand Up @@ -318,15 +304,13 @@ template <typename TParametersValueType, unsigned int VDimension, unsigned int V
bool
MultiTransform<TParametersValueType, VDimension, VSubDimensions>::GetInverse(Self * inverse) const
{
typename TransformQueueType::const_iterator it;

// NOTE: MultiTransform delegates to
// individual transform for setting FixedParameters
// inverse->SetFixedParameters( this->GetFixedParameters() );
inverse->ClearTransformQueue();
for (it = this->m_TransformQueue.begin(); it != this->m_TransformQueue.end(); ++it)
for (const TransformType * const transform : m_TransformQueue)
{
TransformTypePointer inverseTransform = ((*it)->GetInverseTransform()).GetPointer();
TransformTypePointer inverseTransform = (transform->GetInverseTransform()).GetPointer();
if (!inverseTransform)
{
inverse->ClearTransformQueue();
Expand All @@ -350,11 +334,10 @@ MultiTransform<TParametersValueType, VDimension, VSubDimensions>::PrintSelf(std:
Superclass::PrintSelf(os, indent);

os << indent << "TransformQueue: " << std::endl;
typename TransformQueueType::const_iterator cit;
for (cit = this->m_TransformQueue.begin(); cit != this->m_TransformQueue.end(); ++cit)
for (const TransformType * const transform : m_TransformQueue)
{
os << indent << ">>>>>>>>>" << std::endl;
(*cit)->Print(os, indent);
transform->Print(os, indent);
}
}

Expand Down

0 comments on commit da9ecbb

Please sign in to comment.