18#ifndef elxGTestUtilities_h
19#define elxGTestUtilities_h
28#include <itkInterpolateImageFunction.h>
29#include <itkOptimizerParameters.h>
31#include <itkSingleValuedCostFunction.h>
33#include <itkSmartPointer.h>
37#include <gtest/gtest.h>
47namespace GTestUtilities
51template <
typename TMap>
55 const auto endOfMap2 = map2.end();
57 for (
const auto & keyValuePair : map1)
59 EXPECT_EQ(map2.find(keyValuePair.first), endOfMap2);
65template <
typename TMap>
70 for (
const auto & keyValuePair : map2)
72 map1.insert(keyValuePair);
80template <
typename TElastix>
81itk::SmartPointer<TElastix>
84 using FixedImageType =
typename TElastix::FixedImageType;
85 using MovingImageType =
typename TElastix::MovingImageType;
87 const auto elastixObject = TElastix::New();
89 elastixObject->SetConfiguration(elx::Configuration::New());
91 const auto fixedImageContainer = elx::ElastixBase::DataObjectContainerType::New();
92 fixedImageContainer->push_back(FixedImageType::New());
93 elastixObject->SetFixedImageContainer(fixedImageContainer);
95 const auto movingImageContainer = elx::ElastixBase::DataObjectContainerType::New();
96 movingImageContainer->push_back(MovingImageType::New());
97 elastixObject->SetMovingImageContainer(movingImageContainer);
108 assert(minValue < maxValue);
109 assert((maxValue - minValue) <= DBL_MAX);
113 std::mt19937 randomNumberEngine;
115 std::generate_n(parameters.begin(), numberOfParameters, [&randomNumberEngine, minValue, maxValue] {
116 return std::uniform_real_distribution<>{ minValue, maxValue }(randomNumberEngine);
123template <
typename TFixedImage,
typename TMovingImage>
127 const TFixedImage & fixedImage,
128 const TMovingImage & movingImage,
131 itk::InterpolateImageFunction<TMovingImage> & interpolator,
132 const typename TFixedImage::RegionType & fixedImageRegion)
141 metric.SetMovingImage(&movingImage);
142 metric.SetFixedImage(&fixedImage);
144 metric.SetInterpolator(&interpolator);
145 metric.SetFixedImageRegion(fixedImageRegion);
160 static constexpr auto quiet_NaN = std::numeric_limits<double>::quiet_NaN();
162 ValueAndDerivative valueAndDerivative{ quiet_NaN, itk::Array<double>(optimizerParameters.size(), quiet_NaN) };
163 costFunction.GetValueAndDerivative(optimizerParameters, valueAndDerivative.value, valueAndDerivative.derivative);
164 return valueAndDerivative;
An extension of the ITK ImageToImageMetric. It is the intended base class for all elastix metrics.
void Initialize() override
virtual void SetTransform(AdvancedTransformType *arg)
virtual void SetImageSampler(ImageSamplerType *_arg)
This class is a base class for any image sampler.
itk::OptimizerParameters< double > GeneratePseudoRandomParameters(const unsigned numberOfParameters, const double minValue, const double maxValue=1.0)
void InitializeMetric(itk::AdvancedImageToImageMetric< TFixedImage, TMovingImage > &metric, const TFixedImage &fixedImage, const TMovingImage &movingImage, itk::ImageSamplerBase< TFixedImage > &imageSampler, itk::AdvancedTransform< double, TFixedImage::ImageDimension, TMovingImage::ImageDimension > &advancedTransform, itk::InterpolateImageFunction< TMovingImage > &interpolator, const typename TFixedImage::RegionType &fixedImageRegion)
Does set up and initialize the specified advanced metric.
void ExpectAllKeysUnique(const TMap &map1, const TMap &map2)
Expect that all keys of both specified maps are unique.
TMap MakeMergedMap(TMap map1, const TMap &map2)
Makes a map by merging its two arguments together.
itk::SmartPointer< TElastix > CreateDefaultElastixObject()
class ITK_TEMPLATE_EXPORT OptimizerParameters
Represents the value and derivative retrieved from a metric (cost function).
static ValueAndDerivative FromCostFunction(const itk::SingleValuedCostFunction &costFunction, const itk::OptimizerParameters< double > &optimizerParameters)
itk::Array< double > derivative