Skip to content

Commit

Permalink
BUG: Fix additional segmentation faults with empty Composite
Browse files Browse the repository at this point in the history
When the CompositeTransform contains no transform is should behave
like an identity transform. This patches address loop iteration issues
that would cause a segmentation fault due to invalid indexing of
transforms when there composite was empty.
  • Loading branch information
blowekamp committed Jun 8, 2020
1 parent ecad7ff commit 7212dc7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 120 deletions.
162 changes: 42 additions & 120 deletions Modules/Core/Transform/include/itkCompositeTransform.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,11 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformPoint(const Inpu
{

/* Apply in reverse queue order. */
typename TransformQueueType::const_iterator it(this->m_TransformQueue.end());
const typename TransformQueueType::const_iterator beginit(this->m_TransformQueue.begin());
OutputPointType outputPoint(inputPoint);
do
OutputPointType outputPoint(inputPoint);
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputPoint = (*it)->TransformPoint(outputPoint);
} while (it != beginit);
}
return outputPoint;
}

Expand All @@ -90,15 +87,12 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformVector(const Inp
{
OutputVectorType outputVector(inputVector);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
/* Apply in reverse queue order. */
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputVector = (*it)->TransformVector(outputVector);
} while (it != this->m_TransformQueue.begin());
}

return outputVector;
}
Expand All @@ -112,16 +106,12 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformVector(const Inp
OutputVectorType outputVector(inputVector);
OutputPointType outputPoint(inputPoint);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputVector = (*it)->TransformVector(outputVector, outputPoint);
outputPoint = (*it)->TransformPoint(outputPoint);
} while (it != this->m_TransformQueue.begin());
}

return outputVector;
}
Expand All @@ -135,16 +125,12 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformVector(const Inp
OutputVnlVectorType outputVector(inputVector);
OutputPointType outputPoint(inputPoint);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputVector = (*it)->TransformVector(outputVector, outputPoint);
outputPoint = (*it)->TransformPoint(outputPoint);
} while (it != this->m_TransformQueue.begin());
}

return outputVector;
}
Expand All @@ -156,15 +142,11 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformVector(const Inp
{
OutputVnlVectorType outputVector(inputVector);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputVector = (*it)->TransformVector(outputVector);
} while (it != this->m_TransformQueue.begin());
}

return outputVector;
}
Expand All @@ -176,15 +158,11 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformVector(const Inp
{
OutputVectorPixelType outputVector(inputVector);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputVector = (*it)->TransformVector(outputVector);
} while (it != this->m_TransformQueue.begin());
}

return outputVector;
}
Expand All @@ -198,16 +176,12 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformVector(const Inp
OutputVectorPixelType outputVector(inputVector);
OutputPointType outputPoint(inputPoint);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputVector = (*it)->TransformVector(outputVector, outputPoint);
outputPoint = (*it)->TransformPoint(outputPoint);
} while (it != this->m_TransformQueue.begin());
}

return outputVector;
}
Expand All @@ -220,15 +194,11 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformCovariantVector(
{
OutputCovariantVectorType outputVector(inputVector);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputVector = (*it)->TransformCovariantVector(outputVector);
} while (it != this->m_TransformQueue.begin());
}

return outputVector;
}
Expand All @@ -243,16 +213,12 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformCovariantVector(
OutputCovariantVectorType outputVector(inputVector);
OutputPointType outputPoint(inputPoint);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputVector = (*it)->TransformCovariantVector(outputVector, outputPoint);
outputPoint = (*it)->TransformPoint(outputPoint);
} while (it != this->m_TransformQueue.begin());
}

return outputVector;
}
Expand All @@ -265,15 +231,11 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformCovariantVector(
{
OutputVectorPixelType outputVector(inputVector);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputVector = (*it)->TransformCovariantVector(outputVector);
} while (it != this->m_TransformQueue.begin());
}

return outputVector;
}
Expand All @@ -288,16 +250,12 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformCovariantVector(
OutputVectorPixelType outputVector(inputVector);
OutputPointType outputPoint(inputPoint);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputVector = (*it)->TransformCovariantVector(outputVector, outputPoint);
outputPoint = (*it)->TransformPoint(outputPoint);
} while (it != this->m_TransformQueue.begin());
}

return outputVector;
}
Expand All @@ -312,16 +270,12 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformDiffusionTensor3
OutputDiffusionTensor3DType outputTensor(inputTensor);
OutputPointType outputPoint(inputPoint);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputTensor = (*it)->TransformDiffusionTensor3D(outputTensor, outputPoint);
outputPoint = (*it)->TransformPoint(outputPoint);
} while (it != this->m_TransformQueue.begin());
}

return outputTensor;
}
Expand All @@ -336,16 +290,12 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformDiffusionTensor3
OutputVectorPixelType outputTensor(inputTensor);
OutputPointType outputPoint(inputPoint);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputTensor = (*it)->TransformDiffusionTensor3D(outputTensor, outputPoint);
outputPoint = (*it)->TransformPoint(outputPoint);
} while (it != this->m_TransformQueue.begin());
}

return outputTensor;
}
Expand All @@ -358,15 +308,11 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformDiffusionTensor3
{
OutputDiffusionTensor3DType outputTensor(inputTensor);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputTensor = (*it)->TransformDiffusionTensor3D(outputTensor);
} while (it != this->m_TransformQueue.begin());
}

return outputTensor;
}
Expand All @@ -379,15 +325,11 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformDiffusionTensor3
{
OutputVectorPixelType outputTensor(inputTensor);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputTensor = (*it)->TransformDiffusionTensor3D(outputTensor);
} while (it != this->m_TransformQueue.begin());
}

return outputTensor;
}
Expand All @@ -402,16 +344,12 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformSymmetricSecondR
OutputSymmetricSecondRankTensorType outputTensor(inputTensor);
OutputPointType outputPoint(inputPoint);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputTensor = (*it)->TransformSymmetricSecondRankTensor(outputTensor, outputPoint);
outputPoint = (*it)->TransformPoint(outputPoint);
} while (it != this->m_TransformQueue.begin());
}

return outputTensor;
}
Expand All @@ -426,16 +364,12 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformSymmetricSecondR
OutputVectorPixelType outputTensor(inputTensor);
OutputPointType outputPoint(inputPoint);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputTensor = (*it)->TransformSymmetricSecondRankTensor(outputTensor, outputPoint);
outputPoint = (*it)->TransformPoint(outputPoint);
} while (it != this->m_TransformQueue.begin());
}

return outputTensor;
}
Expand All @@ -448,15 +382,11 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformSymmetricSecondR
{
OutputSymmetricSecondRankTensorType outputTensor(inputTensor);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputTensor = (*it)->TransformSymmetricSecondRankTensor(outputTensor);
} while (it != this->m_TransformQueue.begin());
}

return outputTensor;
}
Expand All @@ -469,15 +399,11 @@ CompositeTransform<TParametersValueType, NDimensions>::TransformSymmetricSecondR
{
OutputVectorPixelType outputTensor(inputTensor);

typename TransformQueueType::const_iterator it;
/* Apply in reverse queue order. */
it = this->m_TransformQueue.end();

do
for (auto it = this->m_TransformQueue.rbegin(); it != this->m_TransformQueue.rend(); ++it)
{
it--;
outputTensor = (*it)->TransformSymmetricSecondRankTensor(outputTensor);
} while (it != this->m_TransformQueue.begin());
}

return outputTensor;
}
Expand Down Expand Up @@ -702,17 +628,13 @@ CompositeTransform<TParametersValueType, NDimensions>::GetParameters() const

NumberOfParametersType offset = NumericTraits<NumberOfParametersType>::ZeroValue();

auto it = transforms.end();

do
for (auto it = transforms.rbegin(); it != transforms.rend(); ++it)
{
it--;
const ParametersType & subParameters = (*it)->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();

} while (it != transforms.begin());
}
}

return this->m_Parameters;
Expand Down
Loading

0 comments on commit 7212dc7

Please sign in to comment.