go home Home | Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
elxGTestUtilities.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright UMC Utrecht and contributors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef elxGTestUtilities_h
19#define elxGTestUtilities_h
20
21#include <elxConfiguration.h>
22#include <elxElastixBase.h>
25#include <itkImageSamplerBase.h>
26
27// ITK header files:
28#include <itkInterpolateImageFunction.h>
29#include <itkOptimizerParameters.h>
30#include <itkPoint.h>
31#include <itkSingleValuedCostFunction.h>
32#include <itkSize.h>
33#include <itkSmartPointer.h>
34#include <itkVector.h>
35
36// GoogleTest header file:
37#include <gtest/gtest.h>
38
39#include <algorithm> // For generate_n.
40#include <cassert>
41#include <cfloat> // For DBL_MAX.
42#include <limits>
43#include <random>
44
45namespace elastix
46{
47namespace GTestUtilities
48{
49
51template <typename TMap>
52void
53ExpectAllKeysUnique(const TMap & map1, const TMap & map2)
54{
55 const auto endOfMap2 = map2.end();
56
57 for (const auto & keyValuePair : map1)
58 {
59 EXPECT_EQ(map2.find(keyValuePair.first), endOfMap2);
60 }
61}
62
63
65template <typename TMap>
66TMap
67MakeMergedMap(TMap map1, const TMap & map2)
68{
69 // Note: This for-loop should be equivalent to C++17 `map1.merge(TMap{map2});`
70 for (const auto & keyValuePair : map2)
71 {
72 map1.insert(keyValuePair);
73 }
74 return map1;
75}
76
77
80template <typename TElastix>
81itk::SmartPointer<TElastix>
83{
84 using FixedImageType = typename TElastix::FixedImageType;
85 using MovingImageType = typename TElastix::MovingImageType;
86
87 const auto elastixObject = TElastix::New();
88
89 elastixObject->SetConfiguration(elx::Configuration::New());
90
91 const auto fixedImageContainer = elx::ElastixBase::DataObjectContainerType::New();
92 fixedImageContainer->push_back(FixedImageType::New());
93 elastixObject->SetFixedImageContainer(fixedImageContainer);
94
95 const auto movingImageContainer = elx::ElastixBase::DataObjectContainerType::New();
96 movingImageContainer->push_back(MovingImageType::New());
97 elastixObject->SetMovingImageContainer(movingImageContainer);
98
99 return elastixObject;
100}
101
102
106GeneratePseudoRandomParameters(const unsigned numberOfParameters, const double minValue, const double maxValue = 1.0)
107{
108 assert(minValue < maxValue);
109 assert((maxValue - minValue) <= DBL_MAX);
110
111 itk::OptimizerParameters<double> parameters(numberOfParameters);
112
113 std::mt19937 randomNumberEngine;
114
115 std::generate_n(parameters.begin(), numberOfParameters, [&randomNumberEngine, minValue, maxValue] {
116 return std::uniform_real_distribution<>{ minValue, maxValue }(randomNumberEngine);
117 });
118 return parameters;
119}
120
121
123template <typename TFixedImage, typename TMovingImage>
124void
127 const TFixedImage & fixedImage,
128 const TMovingImage & movingImage,
131 itk::InterpolateImageFunction<TMovingImage> & interpolator,
132 const typename TFixedImage::RegionType & fixedImageRegion)
133{
134 // In elastix, this member function is just called by elx::MetricBase::SetAdvancedMetricImageSampler, at
135 // https://github.com/SuperElastix/elastix/blob/5.1.0/Core/ComponentBaseClasses/elxMetricBase.hxx#L313
136 metric.SetImageSampler(&imageSampler);
137
138 // Similar to the six member function calls in `MultiResolutionImageRegistrationMethod2::Initialize()` "Setup the
139 // metric", at
140 // https://github.com/SuperElastix/elastix/blob/5.1.0/Common/itkMultiResolutionImageRegistrationMethod2.hxx#L118-L124
141 metric.SetMovingImage(&movingImage);
142 metric.SetFixedImage(&fixedImage);
143 metric.SetTransform(&advancedTransform);
144 metric.SetInterpolator(&interpolator);
145 metric.SetFixedImageRegion(fixedImageRegion);
146 metric.Initialize();
147}
148
149
152{
153 double value;
154 itk::Array<double> derivative;
155
156 static ValueAndDerivative
157 FromCostFunction(const itk::SingleValuedCostFunction & costFunction,
158 const itk::OptimizerParameters<double> & optimizerParameters)
159 {
160 static constexpr auto quiet_NaN = std::numeric_limits<double>::quiet_NaN();
161
162 ValueAndDerivative valueAndDerivative{ quiet_NaN, itk::Array<double>(optimizerParameters.size(), quiet_NaN) };
163 costFunction.GetValueAndDerivative(optimizerParameters, valueAndDerivative.value, valueAndDerivative.derivative);
164 return valueAndDerivative;
165 }
166};
167
168} // namespace GTestUtilities
169} // namespace elastix
170
171
172#endif
An extension of the ITK ImageToImageMetric. It is the intended base class for all elastix metrics.
virtual void SetTransform(AdvancedTransformType *arg)
virtual void SetImageSampler(ImageSamplerType *_arg)
Transform maps points, vectors and covariant vectors from an input space to an output space.
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)


Generated on 2024-07-17 for elastix by doxygen 1.11.0 (9b424b03c9833626cd435af22a444888fbbb192d) elastix logo