Skip to content

Commit

Permalink
STYLE: Deref input and output pointer everywhere in "ImageSamplers"
Browse files Browse the repository at this point in the history
Follow-up to commit 0ef6d0d "STYLE: `Deref` sampler input and output at the start of GenerateData()"
  • Loading branch information
N-Dekker committed Dec 5, 2023
1 parent 0ef6d0d commit e375051
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 55 deletions.
29 changes: 15 additions & 14 deletions Common/ImageSamplers/itkImageFullSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkImageFullSampler.h"

#include "itkImageRegionConstIteratorWithIndex.h"
#include "elxDeref.h"

namespace itk
{
Expand All @@ -41,13 +42,13 @@ ImageFullSampler<TInputImage>::GenerateData()
}

/** Get handles to the input image, output sample container, and the mask. */
InputImageConstPointer inputImage = this->GetInput();
typename ImageSampleContainerType::Pointer sampleContainer = this->GetOutput();
typename MaskType::ConstPointer mask = this->GetMask();
const InputImageType & inputImage = elastix::Deref(this->GetInput());
ImageSampleContainerType & sampleContainer = elastix::Deref(this->GetOutput());
typename MaskType::ConstPointer mask = this->GetMask();

// Take capacity from the output container, and clear it.
std::vector<ImageSampleType> sampleVector;
sampleContainer->swap(sampleVector);
sampleContainer.swap(sampleVector);
sampleVector.clear();

const auto croppedInputImageRegion = this->GetCroppedInputImageRegion();
Expand Down Expand Up @@ -78,15 +79,15 @@ ImageFullSampler<TInputImage>::GenerateData()
}

/** Simply loop over the image and store all samples in the container. */
for (InputImageIterator iter(inputImage, croppedInputImageRegion); !iter.IsAtEnd(); ++iter)
for (InputImageIterator iter(&inputImage, croppedInputImageRegion); !iter.IsAtEnd(); ++iter)
{
ImageSampleType tempSample;

/** Get sampled index */
InputImageIndexType index = iter.GetIndex();

/** Translate index to point */
inputImage->TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);
inputImage.TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);

/** Get sampled image value */
tempSample.m_ImageValue = iter.Get();
Expand All @@ -101,15 +102,15 @@ ImageFullSampler<TInputImage>::GenerateData()
mask->UpdateSource();

/** Loop over the image and check if the points falls within the mask. */
for (InputImageIterator iter(inputImage, croppedInputImageRegion); !iter.IsAtEnd(); ++iter)
for (InputImageIterator iter(&inputImage, croppedInputImageRegion); !iter.IsAtEnd(); ++iter)
{
ImageSampleType tempSample;

/** Get sampled index. */
InputImageIndexType index = iter.GetIndex();

/** Translate index to point. */
inputImage->TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);
inputImage.TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);

if (mask->IsInsideInWorldSpace(tempSample.m_ImageCoordinates))
{
Expand All @@ -124,7 +125,7 @@ ImageFullSampler<TInputImage>::GenerateData()
} // end else (if mask exists)

// Move the samples from the vector into the output container.
sampleContainer->swap(sampleVector);
sampleContainer.swap(sampleVector);

} // end GenerateData()

Expand All @@ -139,7 +140,7 @@ ImageFullSampler<TInputImage>::ThreadedGenerateData(const InputImageRegionType &
ThreadIdType threadId)
{
/** Get handles to the input image, mask and the output. */
InputImageConstPointer inputImage = this->GetInput();
const InputImageType & inputImage = elastix::Deref(this->GetInput());
typename MaskType::ConstPointer mask = this->GetMask();
ImageSampleContainerPointer & sampleContainerThisThread // & ???
= this->m_ThreaderSampleContainer[threadId];
Expand Down Expand Up @@ -177,15 +178,15 @@ ImageFullSampler<TInputImage>::ThreadedGenerateData(const InputImageRegionType &
}

/** Simply loop over the image and store all samples in the container. */
for (InputImageIterator iter(inputImage, inputRegionForThread); !iter.IsAtEnd(); ++iter)
for (InputImageIterator iter(&inputImage, inputRegionForThread); !iter.IsAtEnd(); ++iter)
{
ImageSampleType tempSample;

/** Get sampled index */
InputImageIndexType index = iter.GetIndex();

/** Translate index to point */
inputImage->TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);
inputImage.TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);

/** Get sampled image value */
tempSample.m_ImageValue = iter.Get();
Expand All @@ -200,15 +201,15 @@ ImageFullSampler<TInputImage>::ThreadedGenerateData(const InputImageRegionType &
mask->UpdateSource();

/** Loop over the image and check if the points falls within the mask. */
for (InputImageIterator iter(inputImage, inputRegionForThread); !iter.IsAtEnd(); ++iter)
for (InputImageIterator iter(&inputImage, inputRegionForThread); !iter.IsAtEnd(); ++iter)
{
ImageSampleType tempSample;

/** Get sampled index. */
InputImageIndexType index = iter.GetIndex();

/** Translate index to point. */
inputImage->TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);
inputImage.TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);

if (mask->IsInsideInWorldSpace(tempSample.m_ImageCoordinates))
{
Expand Down
19 changes: 10 additions & 9 deletions Common/ImageSamplers/itkImageGridSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkImageGridSampler.h"

#include "itkImageRegionConstIteratorWithIndex.h"
#include "elxDeref.h"

#include <algorithm> // For accumulate.
#include <cassert>
Expand Down Expand Up @@ -54,13 +55,13 @@ void
ImageGridSampler<TInputImage>::GenerateData()
{
/** Get handles to the input image, output sample container, and the mask. */
InputImageConstPointer inputImage = this->GetInput();
typename ImageSampleContainerType::Pointer sampleContainer = this->GetOutput();
typename MaskType::ConstPointer mask = this->GetMask();
const InputImageType & inputImage = elastix::Deref(this->GetInput());
ImageSampleContainerType & sampleContainer = elastix::Deref(this->GetOutput());
typename MaskType::ConstPointer mask = this->GetMask();

// Take capacity from the output container, and clear it.
std::vector<ImageSampleType> sampleVector;
sampleContainer->swap(sampleVector);
sampleContainer.swap(sampleVector);
sampleVector.clear();

/** Take into account the possibility of a smaller bounding box around the mask */
Expand Down Expand Up @@ -106,10 +107,10 @@ ImageGridSampler<TInputImage>::GenerateData()
ImageSampleType tempSample;

// Get sampled fixed image value.
tempSample.m_ImageValue = inputImage->GetPixel(index);
tempSample.m_ImageValue = inputImage.GetPixel(index);

// Translate index to point.
inputImage->TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);
inputImage.TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);

// Store sample in container.
sampleVector.push_back(tempSample);
Expand Down Expand Up @@ -143,12 +144,12 @@ ImageGridSampler<TInputImage>::GenerateData()
ImageSampleType tempSample;

// Translate index to point.
inputImage->TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);
inputImage.TransformIndexToPhysicalPoint(index, tempSample.m_ImageCoordinates);

if (mask->IsInsideInWorldSpace(tempSample.m_ImageCoordinates))
{
// Get sampled fixed image value.
tempSample.m_ImageValue = inputImage->GetPixel(index);
tempSample.m_ImageValue = inputImage.GetPixel(index);

// Store sample in container.
sampleVector.push_back(tempSample);
Expand All @@ -167,7 +168,7 @@ ImageGridSampler<TInputImage>::GenerateData()
} // else (if mask exists)

// Move the samples from the vector into the output container.
sampleContainer->swap(sampleVector);
sampleContainer.swap(sampleVector);

} // end GenerateData()

Expand Down
29 changes: 15 additions & 14 deletions Common/ImageSamplers/itkImageSamplerBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkImageSamplerBase_hxx

#include "itkImageSamplerBase.h"
#include "elxDeref.h"

namespace itk
{
Expand Down Expand Up @@ -164,18 +165,18 @@ ImageSamplerBase<TInputImage>::GenerateInputRequestedRegion()
itkExceptionMacro("ERROR: Input image not set");
}

/** Get a pointer to the input image. */
InputImagePointer inputImage = const_cast<InputImageType *>(this->GetInput());
/** Get a non-const reference to the input image. */
auto & inputImage = const_cast<InputImageType &>(elastix::Deref(this->GetInput()));

/** Get and set the region. */
if (this->GetInputImageRegion().GetNumberOfPixels() != 0)
{
InputImageRegionType inputRequestedRegion = this->GetInputImageRegion();

/** Crop the input requested region at the input's largest possible region. */
if (inputRequestedRegion.Crop(inputImage->GetLargestPossibleRegion()))
if (inputRequestedRegion.Crop(inputImage.GetLargestPossibleRegion()))
{
inputImage->SetRequestedRegion(inputRequestedRegion);
inputImage.SetRequestedRegion(inputRequestedRegion);
}
else
{
Expand All @@ -184,25 +185,25 @@ ImageSamplerBase<TInputImage>::GenerateInputRequestedRegion()
*/

/** Store what we tried to request (prior to trying to crop). */
inputImage->SetRequestedRegion(inputRequestedRegion);
inputImage.SetRequestedRegion(inputRequestedRegion);

/** Build an exception. */
InvalidRequestedRegionError e(__FILE__, __LINE__);
e.SetLocation(ITK_LOCATION);
e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
e.SetDataObject(inputImage);
e.SetDataObject(&inputImage);
throw e;
}
}
else
{
inputImage->SetRequestedRegion(inputImage->GetLargestPossibleRegion());
this->SetInputImageRegion(inputImage->GetLargestPossibleRegion());
inputImage.SetRequestedRegion(inputImage.GetLargestPossibleRegion());
this->SetInputImageRegion(inputImage.GetLargestPossibleRegion());
}

/** Crop the region of the inputImage to the bounding box of the mask. */
this->CropInputImageRegion();
inputImage->SetRequestedRegion(this->m_CroppedInputImageRegion);
inputImage.SetRequestedRegion(this->m_CroppedInputImageRegion);

} // end GenerateInputRequestedRegion()

Expand Down Expand Up @@ -398,15 +399,15 @@ ImageSamplerBase<TInputImage>::AfterThreadedGenerateData()
}

/** Get handle to the output sample container. */
typename ImageSampleContainerType::Pointer sampleContainer = this->GetOutput();
sampleContainer->clear();
sampleContainer->reserve(this->m_NumberOfSamples);
ImageSampleContainerType & sampleContainer = elastix::Deref(this->GetOutput());
sampleContainer.clear();
sampleContainer.reserve(this->m_NumberOfSamples);

/** Combine the results of all threads. */
for (std::size_t i = 0; i < this->GetNumberOfWorkUnits(); ++i)
{
sampleContainer->insert(
sampleContainer->end(), this->m_ThreaderSampleContainer[i]->begin(), this->m_ThreaderSampleContainer[i]->end());
sampleContainer.insert(
sampleContainer.end(), this->m_ThreaderSampleContainer[i]->begin(), this->m_ThreaderSampleContainer[i]->end());
}

} // end AfterThreadedGenerateData()
Expand Down
9 changes: 5 additions & 4 deletions Common/ImageSamplers/itkImageToVectorContainerFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkImageToVectorContainerFilter.h"

#include "itkMath.h"
#include "elxDeref.h"

namespace itk
{
Expand Down Expand Up @@ -139,21 +140,21 @@ ImageToVectorContainerFilter<TInputImage, TOutputVectorContainer>::SplitRequeste
InputImageRegionType & splitRegion)
{
// Get the input pointer
const InputImageType * inputPtr = this->GetInput();
const typename TInputImage::SizeType & requestedRegionSize = inputPtr->GetRequestedRegion().GetSize();
const InputImageType & inputImage = elastix::Deref(this->GetInput());
const typename TInputImage::SizeType & requestedRegionSize = inputImage.GetRequestedRegion().GetSize();
// \todo: requested region -> this->GetCroppedInputImageRegion()

int splitAxis;
typename TInputImage::IndexType splitIndex;
typename TInputImage::SizeType splitSize;

// Initialize the splitRegion to the output requested region
splitRegion = inputPtr->GetRequestedRegion();
splitRegion = inputImage.GetRequestedRegion();
splitIndex = splitRegion.GetIndex();
splitSize = splitRegion.GetSize();

// split on the outermost dimension available
splitAxis = inputPtr->GetImageDimension() - 1;
splitAxis = inputImage.GetImageDimension() - 1;
while (requestedRegionSize[splitAxis] == 1)
{
--splitAxis;
Expand Down
29 changes: 15 additions & 14 deletions Common/ImageSamplers/itkMultiInputImageRandomCoordinateSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkMultiInputImageRandomCoordinateSampler.h"
#include <vnl/vnl_inverse.h>
#include "itkConfigure.h"
#include "elxDeref.h"

namespace itk
{
Expand All @@ -41,32 +42,32 @@ MultiInputImageRandomCoordinateSampler<TInputImage>::GenerateData()
}

/** Get handles to the input image, output sample container, and mask. */
InputImageConstPointer inputImage = this->GetInput();
typename ImageSampleContainerType::Pointer sampleContainer = this->GetOutput();
typename MaskType::ConstPointer mask = this->GetMask();
typename InterpolatorType::Pointer interpolator = this->GetModifiableInterpolator();
const InputImageType & inputImage = elastix::Deref(this->GetInput());
ImageSampleContainerType & sampleContainer = elastix::Deref(this->GetOutput());
typename MaskType::ConstPointer mask = this->GetMask();
typename InterpolatorType::Pointer interpolator = this->GetModifiableInterpolator();

/** Set up the interpolator. */
interpolator->SetInputImage(inputImage);
interpolator->SetInputImage(&inputImage);

/** Get the intersection of all sample regions. */
InputImageContinuousIndexType smallestContIndex;
InputImageContinuousIndexType largestContIndex;
this->GenerateSampleRegion(smallestContIndex, largestContIndex);

/** Reserve memory for the output. */
sampleContainer->Reserve(this->GetNumberOfSamples());
sampleContainer.Reserve(this->GetNumberOfSamples());

/** Setup an iterator over the output, which is of ImageSampleContainerType. */
typename ImageSampleContainerType::Iterator iter;
typename ImageSampleContainerType::ConstIterator end = sampleContainer->End();
typename ImageSampleContainerType::ConstIterator end = sampleContainer.End();

InputImageContinuousIndexType sampleContIndex;
/** Fill the sample container. */
if (mask.IsNull())
{
/** Start looping over the sample container. */
for (iter = sampleContainer->Begin(); iter != end; ++iter)
for (iter = sampleContainer.Begin(); iter != end; ++iter)
{
/** Make a reference to the current sample in the container. */
InputImagePointType & samplePoint = iter->Value().m_ImageCoordinates;
Expand All @@ -76,7 +77,7 @@ MultiInputImageRandomCoordinateSampler<TInputImage>::GenerateData()
this->GenerateRandomCoordinate(smallestContIndex, largestContIndex, sampleContIndex);

/** Convert to point */
inputImage->TransformContinuousIndexToPhysicalPoint(sampleContIndex, samplePoint);
inputImage.TransformContinuousIndexToPhysicalPoint(sampleContIndex, samplePoint);

/** Compute the value at the contindex. */
sampleValue = static_cast<ImageSampleValueType>(this->m_Interpolator->EvaluateAtContinuousIndex(sampleContIndex));
Expand All @@ -95,7 +96,7 @@ MultiInputImageRandomCoordinateSampler<TInputImage>::GenerateData()
unsigned long maximumNumberOfSamplesToTry = 10 * this->GetNumberOfSamples();

/** Start looping over the sample container. */
for (iter = sampleContainer->Begin(); iter != end; ++iter)
for (iter = sampleContainer.Begin(); iter != end; ++iter)
{
/** Make a reference to the current sample in the container. */
InputImagePointType & samplePoint = iter->Value().m_ImageCoordinates;
Expand All @@ -109,17 +110,17 @@ MultiInputImageRandomCoordinateSampler<TInputImage>::GenerateData()
if (numberOfSamplesTried > maximumNumberOfSamplesToTry)
{
/** Squeeze the sample container to the size that is still valid. */
typename ImageSampleContainerType::iterator stlnow = sampleContainer->begin();
typename ImageSampleContainerType::iterator stlend = sampleContainer->end();
typename ImageSampleContainerType::iterator stlnow = sampleContainer.begin();
typename ImageSampleContainerType::iterator stlend = sampleContainer.end();
stlnow += iter.Index();
sampleContainer->erase(stlnow, stlend);
sampleContainer.erase(stlnow, stlend);
itkExceptionMacro(
<< "Could not find enough image samples within reasonable time. Probably the mask is too small");
}

/** Generate a point in the input image region. */
this->GenerateRandomCoordinate(smallestContIndex, largestContIndex, sampleContIndex);
inputImage->TransformContinuousIndexToPhysicalPoint(sampleContIndex, samplePoint);
inputImage.TransformContinuousIndexToPhysicalPoint(sampleContIndex, samplePoint);
} while (!this->IsInsideAllMasks(samplePoint));

/** Compute the value at the contindex. */
Expand Down

0 comments on commit e375051

Please sign in to comment.