Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ BSplineInterpolateImageFunction<TImageType, TCoordinate, TCoefficientType>::SetS
// this->SetPoles();
m_MaxNumberInterpolationPoints = Math::UnsignedPower(SplineOrder + 1, ImageDimension);
this->GeneratePointsToIndex();
this->Modified();
}

template <typename TImageType, typename TCoordinate, typename TCoefficientType>
Expand Down
99 changes: 99 additions & 0 deletions Modules/Filtering/ImageGrid/test/itkResampleImageFilterGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "itkResampleImageFilter.h"

#include "itkAffineTransform.h"
#include "itkBSplineInterpolateImageFunction.h"
#include "itkCastImageFilter.h"
#include "itkGaussianInterpolateImageFunction.h"
#include "itkImage.h"
Expand All @@ -33,6 +34,7 @@
// Standard C++ header files:
#include <limits>
#include <random>
#include <vector>


namespace
Expand Down Expand Up @@ -241,3 +243,100 @@ TEST(ResampleImageFilter, StreamingMatchesNonStreamedWithGaussianInterpolator)
}
EXPECT_EQ(itU.IsAtEnd(), itS.IsAtEnd());
}


namespace
{
using BSplineImageType = itk::Image<float, 2>;
using BSplineInterpolatorType = itk::BSplineInterpolateImageFunction<BSplineImageType>;
using BSplineResamplerType = itk::ResampleImageFilter<BSplineImageType, BSplineImageType>;

constexpr itk::SizeValueType kBSplineInputSize{ 8 };
constexpr itk::SizeValueType kBSplineOutputSize{ 6 };
// Off the input grid, so that the spline order changes every output pixel.
constexpr double kBSplineOutputOrigin{ 1.25 };
constexpr float kBSplineMaximumPixelValue{ 100.0f };

BSplineImageType::Pointer
MakeRandomBSplineInput()
{
const auto input = BSplineImageType::New();
input->SetRegions(BSplineImageType::SizeType::Filled(kBSplineInputSize));
input->Allocate();
std::mt19937 randomNumberEngine{};
std::uniform_real_distribution<float> distribution(0.0f, kBSplineMaximumPixelValue);
for (itk::ImageRegionIterator<BSplineImageType> it(input, input->GetBufferedRegion()); !it.IsAtEnd(); ++it)
{
it.Set(distribution(randomNumberEngine));
}
return input;
}

BSplineResamplerType::Pointer
MakeResampler(const BSplineImageType * input, const unsigned int splineOrder)
{
const auto interpolator = BSplineInterpolatorType::New();
interpolator->SetSplineOrder(splineOrder);
const auto resampler = BSplineResamplerType::New();
resampler->SetInput(input);
resampler->SetInterpolator(interpolator);
resampler->SetSize(BSplineImageType::SizeType::Filled(kBSplineOutputSize));
resampler->SetOutputOrigin(itk::MakePoint(kBSplineOutputOrigin, kBSplineOutputOrigin));
return resampler;
}

std::vector<float>
PixelsOf(const BSplineImageType * image)
{
return std::vector<float>(image->GetBufferPointer(),
image->GetBufferPointer() + image->GetBufferedRegion().GetNumberOfPixels());
}

BSplineInterpolatorType *
BSplineInterpolatorOf(BSplineResamplerType & resampler)
{
return dynamic_cast<BSplineInterpolatorType *>(resampler.GetModifiableInterpolator());
}
} // namespace


// Changing the B-spline order after an Update() must make the next Update() use the new order.
TEST(ResampleImageFilter, ReexecutesWhenBSplineOrderChanges)
{
const auto input = MakeRandomBSplineInput();
const auto resampler = MakeResampler(input, 1);
resampler->Update();
const std::vector<float> linearPixels = PixelsOf(resampler->GetOutput());

BSplineInterpolatorType * const interpolator = BSplineInterpolatorOf(*resampler);
ASSERT_NE(interpolator, nullptr);
const itk::ModifiedTimeType timeBefore = interpolator->GetMTime();
interpolator->SetSplineOrder(3);
EXPECT_GT(interpolator->GetMTime(), timeBefore);
resampler->Update();

const auto cubicResampler = MakeResampler(input, 3);
cubicResampler->Update();
const std::vector<float> cubicPixels = PixelsOf(cubicResampler->GetOutput());

EXPECT_NE(linearPixels, cubicPixels);
EXPECT_EQ(PixelsOf(resampler->GetOutput()), cubicPixels);
}


// Setting the B-spline order it already has must not mark the interpolator or the filter out of date.
TEST(ResampleImageFilter, KeepsUpToDateWhenBSplineOrderIsUnchanged)
{
const auto input = MakeRandomBSplineInput();
const auto resampler = MakeResampler(input, 3);
resampler->Update();

BSplineInterpolatorType * const interpolator = BSplineInterpolatorOf(*resampler);
ASSERT_NE(interpolator, nullptr);
const itk::ModifiedTimeType interpolatorTime = interpolator->GetMTime();
const itk::ModifiedTimeType outputTime = resampler->GetOutput()->GetMTime();
interpolator->SetSplineOrder(3);
EXPECT_EQ(interpolator->GetMTime(), interpolatorTime);
resampler->Update();
EXPECT_EQ(resampler->GetOutput()->GetMTime(), outputTime);
}
Loading