Loading...
Searching...
No Matches
elxGTestUtilities.h
Go to the documentation of this file.
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef elxGTestUtilities_h
#define elxGTestUtilities_h
#include <elxConfiguration.h>
#include <elxElastixBase.h>
#include <itkAdvancedImageToImageMetric.h>
#include <itkAdvancedTransform.h>
#include <itkImageSamplerBase.h>
// ITK header files:
#include <itkInterpolateImageFunction.h>
#include <itkOptimizerParameters.h>
#include <itkPoint.h>
#include <itkSingleValuedCostFunction.h>
#include <itkSize.h>
#include <itkSmartPointer.h>
#include <itkVector.h>
// GoogleTest header file:
#include <gtest/gtest.h>
#include <algorithm> // For generate_n.
#include <cassert>
#include <cfloat> // For DBL_MAX.
#include <limits>
#include <random>
namespace elastix
{
namespace GTestUtilities
{
template <typename TMap>
void
ExpectAllKeysUnique(const TMap & map1, const TMap & map2)
{
const auto endOfMap2 = map2.end();
for (const auto & keyValuePair : map1)
{
EXPECT_EQ(map2.find(keyValuePair.first), endOfMap2);
}
}
template <typename TMap>
TMap
MakeMergedMap(TMap map1, const TMap & map2)
{
// Note: This for-loop should be equivalent to C++17 `map1.merge(TMap{map2});`
for (const auto & keyValuePair : map2)
{
map1.insert(keyValuePair);
}
return map1;
}
template <typename TElastix>
itk::SmartPointer<TElastix>
CreateDefaultElastixObject()
{
using FixedImageType = typename TElastix::FixedImageType;
using MovingImageType = typename TElastix::MovingImageType;
const auto elastixObject = TElastix::New();
elastixObject->SetConfiguration(elx::Configuration::New());
const auto fixedImageContainer = elx::ElastixBase::DataObjectContainerType::New();
fixedImageContainer->push_back(FixedImageType::New());
elastixObject->SetFixedImageContainer(fixedImageContainer);
const auto movingImageContainer = elx::ElastixBase::DataObjectContainerType::New();
movingImageContainer->push_back(MovingImageType::New());
elastixObject->SetMovingImageContainer(movingImageContainer);
return elastixObject;
}
inline itk::OptimizerParameters<double>
GeneratePseudoRandomParameters(const unsigned numberOfParameters, const double minValue, const double maxValue = 1.0)
{
assert(minValue < maxValue);
assert((maxValue - minValue) <= DBL_MAX);
itk::OptimizerParameters<double> parameters(numberOfParameters);
std::mt19937 randomNumberEngine;
std::generate_n(parameters.begin(), numberOfParameters, [&randomNumberEngine, minValue, maxValue] {
return std::uniform_real_distribution<>{ minValue, maxValue }(randomNumberEngine);
});
return parameters;
}
template <typename TFixedImage, typename TMovingImage>
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)
{
// In elastix, this member function is just called by elx::MetricBase::SetAdvancedMetricImageSampler, at
// https://github.com/SuperElastix/elastix/blob/5.1.0/Core/ComponentBaseClasses/elxMetricBase.hxx#L313
metric.SetImageSampler(&imageSampler);
// Similar to the six member function calls in `MultiResolutionImageRegistrationMethod2::Initialize()` "Setup the
// metric", at
// https://github.com/SuperElastix/elastix/blob/5.1.0/Common/itkMultiResolutionImageRegistrationMethod2.hxx#L118-L124
metric.SetMovingImage(&movingImage);
metric.SetFixedImage(&fixedImage);
metric.SetTransform(&advancedTransform);
metric.SetInterpolator(&interpolator);
metric.SetFixedImageRegion(fixedImageRegion);
metric.Initialize();
}
struct ValueAndDerivative
{
double value;
itk::Array<double> derivative;
static ValueAndDerivative
FromCostFunction(const itk::SingleValuedCostFunction & costFunction,
const itk::OptimizerParameters<double> & optimizerParameters)
{
static constexpr auto quiet_NaN = std::numeric_limits<double>::quiet_NaN();
ValueAndDerivative valueAndDerivative{ quiet_NaN, itk::Array<double>(optimizerParameters.size(), quiet_NaN) };
costFunction.GetValueAndDerivative(optimizerParameters, valueAndDerivative.value, valueAndDerivative.derivative);
return valueAndDerivative;
}
};
} // namespace GTestUtilities
} // namespace elastix
#endif
elastix::Configuration::New
static Pointer New()
itk::AdvancedImageToImageMetric
An extension of the ITK ImageToImageMetric. It is the intended base class for all elastix metrics.
Definition itkAdvancedImageToImageMetric.h:83
itk::AdvancedImageToImageMetric::Initialize
void Initialize() override
itk::AdvancedImageToImageMetric::SetTransform
virtual void SetTransform(AdvancedTransformType *arg)
Definition itkAdvancedImageToImageMetric.h:206
itk::AdvancedImageToImageMetric::SetImageSampler
virtual void SetImageSampler(ImageSamplerType *_arg)
itk::AdvancedTransform
Transform maps points, vectors and covariant vectors from an input space to an output space.
Definition itkAdvancedTransform.h:84
itk::ImageSamplerBase
This class is a base class for any image sampler.
Definition itkImageSamplerBase.h:44
elxConfiguration.h
elxElastixBase.h
itkAdvancedImageToImageMetric.h
itkAdvancedTransform.h
itkImageSamplerBase.h
elastix::GTestUtilities
Definition elxGTestUtilities.h:48
elastix::GTestUtilities::GeneratePseudoRandomParameters
itk::OptimizerParameters< double > GeneratePseudoRandomParameters(const unsigned numberOfParameters, const double minValue, const double maxValue=1.0)
Definition elxGTestUtilities.h:106
elastix::GTestUtilities::InitializeMetric
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.
Definition elxGTestUtilities.h:125
elastix::GTestUtilities::ExpectAllKeysUnique
void ExpectAllKeysUnique(const TMap &map1, const TMap &map2)
Expect that all keys of both specified maps are unique.
Definition elxGTestUtilities.h:53
elastix::GTestUtilities::MakeMergedMap
TMap MakeMergedMap(TMap map1, const TMap &map2)
Makes a map by merging its two arguments together.
Definition elxGTestUtilities.h:67
elastix::GTestUtilities::CreateDefaultElastixObject
itk::SmartPointer< TElastix > CreateDefaultElastixObject()
Definition elxGTestUtilities.h:82
elastix
Definition elxDefaultConstruct.h:25
itk::OptimizerParameters
class ITK_TEMPLATE_EXPORT OptimizerParameters
Definition elxConversion.h:33
elastix::GTestUtilities::ValueAndDerivative
Represents the value and derivative retrieved from a metric (cost function).
Definition elxGTestUtilities.h:152
elastix::GTestUtilities::ValueAndDerivative::FromCostFunction
static ValueAndDerivative FromCostFunction(const itk::SingleValuedCostFunction &costFunction, const itk::OptimizerParameters< double > &optimizerParameters)
Definition elxGTestUtilities.h:157
elastix::GTestUtilities::ValueAndDerivative::value
double value
Definition elxGTestUtilities.h:153
elastix::GTestUtilities::ValueAndDerivative::derivative
itk::Array< double > derivative
Definition elxGTestUtilities.h:154

Generated on 1768596610 for elastix by 1.15.0 |