Skip to content
Open
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
2 changes: 2 additions & 0 deletions Common/GTesting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(ELX_COMMON_GTEST_SOURCES
elxTransformIOGTest.cxx
itkAdvancedImageToImageMetricGTest.cxx
itkAdvancedMeanSquaresImageToImageMetricGTest.cxx
itkCombinationImageToImageMetricGTest.cxx
itkComputeImageExtremaFilterGTest.cxx
itkCorrespondingPointsEuclideanDistancePointMetricGTest.cxx
itkGridScheduleComputerGTest.cxx
Expand Down Expand Up @@ -60,6 +61,7 @@ endif()

target_include_directories(CommonGTest PRIVATE
${elastix_SOURCE_DIR}/Components/Metrics
${elastix_SOURCE_DIR}/Components/Registrations
${elastix_SOURCE_DIR}/Components/ResampleInterpolators
${elastix_SOURCE_DIR}/Components/Resamplers
${elastix_SOURCE_DIR}/Components/Transforms
Expand Down
107 changes: 107 additions & 0 deletions Common/GTesting/itkCombinationImageToImageMetricGTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*=========================================================================
*
* 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.
*
*=========================================================================*/

// First include the header file to be tested:
#include "MultiMetricMultiResolutionRegistration/itkCombinationImageToImageMetric.h"

#include "AdvancedMeanSquares/itkAdvancedMeanSquaresImageToImageMetric.h"
#include "itkAdvancedTranslationTransform.h"
#include "itkImageFullSampler.h"
#include "GTesting/elxCoreMainGTestUtilities.h"
#include "elxGTestUtilities.h"
#include "elxDefaultConstruct.h"
#include <itkImage.h>
#include <itkNearestNeighborInterpolateImageFunction.h>
#include <gtest/gtest.h>

// The template to be tested.
using itk::CombinationImageToImageMetric;

using elx::CoreMainGTestUtilities::CreateImageFilledWithSequenceOfNaturalNumbers;
using elx::CoreMainGTestUtilities::minimumImageSizeValue;
using elx::GTestUtilities::InitializeMetric;
using elx::GTestUtilities::ValueAndDerivative;


// Tests that Initialize() preserves the number of work units of a sub-metric, and that the combination metric then
// yields the same value and derivative as the sub-metric by itself. A sub-metric allocates its per-work-unit data
// during its own Initialize(), so its number of work units should not be changed afterwards.
GTEST_TEST(CombinationImageToImageMetric, InitializePreservesNumberOfWorkUnitsOfSubMetric)
{
static constexpr auto imageDimension = 2U;
using PixelType = float;
using ImageType = itk::Image<PixelType, imageDimension>;
using SizeType = itk::Size<imageDimension>;
using SubMetricType = itk::AdvancedMeanSquaresImageToImageMetric<ImageType, ImageType>;

const auto fixedImage =
CreateImageFilledWithSequenceOfNaturalNumbers<PixelType>(SizeType::Filled(minimumImageSizeValue));
const auto movingImage =
CreateImageFilledWithSequenceOfNaturalNumbers<PixelType>(SizeType::Filled(minimumImageSizeValue + 1));

const itk::ThreadIdType defaultNumberOfWorkUnits =
elx::DefaultConstruct<CombinationImageToImageMetric<ImageType, ImageType>>{}.GetNumberOfWorkUnits();

// Test both less and more work units than the default number of the combination metric.
for (const itk::ThreadIdType numberOfWorkUnits : { itk::ThreadIdType{ 1 }, defaultNumberOfWorkUnits + 1 })
{
elx::DefaultConstruct<itk::AdvancedTranslationTransform<double, imageDimension>> transform{};
elx::DefaultConstruct<itk::NearestNeighborInterpolateImageFunction<ImageType>> interpolator{};

const auto createSubMetric = [numberOfWorkUnits] {
const auto subMetric = SubMetricType::New();
subMetric->SetUseMultiThread(true);
subMetric->SetNumberOfWorkUnits(numberOfWorkUnits);
return subMetric;
};

elx::DefaultConstruct<itk::ImageFullSampler<ImageType>> imageSampler{};
const auto subMetric = createSubMetric();
subMetric->SetImageSampler(&imageSampler);

elx::DefaultConstruct<CombinationImageToImageMetric<ImageType, ImageType>> combinationMetric{};
combinationMetric.SetNumberOfMetrics(1);
combinationMetric.SetMetric(subMetric, 0);
combinationMetric.SetMetricWeight(1.0, 0);
combinationMetric.SetUseAllMetrics();
combinationMetric.SetFixedImage(fixedImage);
combinationMetric.SetMovingImage(movingImage);
combinationMetric.SetTransform(&transform);
combinationMetric.SetInterpolator(&interpolator);
combinationMetric.SetFixedImageRegion(fixedImage->GetBufferedRegion());
combinationMetric.Initialize();

EXPECT_EQ(subMetric->GetNumberOfWorkUnits(), numberOfWorkUnits);

elx::DefaultConstruct<itk::ImageFullSampler<ImageType>> expectedImageSampler{};
const auto expectedMetric = createSubMetric();
InitializeMetric(*expectedMetric,
*fixedImage,
*movingImage,
expectedImageSampler,
transform,
interpolator,
fixedImage->GetBufferedRegion());

const auto expected = ValueAndDerivative::FromCostFunction(*expectedMetric, transform.GetParameters());
const auto actual = ValueAndDerivative::FromCostFunction(combinationMetric, transform.GetParameters());

EXPECT_EQ(actual.value, expected.value);
EXPECT_EQ(actual.derivative, expected.derivative);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ CombinationImageToImageMetric<TFixedImage, TMovingImage>::Initialize()
{
// The NumberOfThreadsPerMetric is changed after Initialize() so we save it before and then
// set it on.
unsigned nrOfThreadsPerMetric = this->GetNumberOfWorkUnits();
const ThreadIdType nrOfThreadsPerMetric = testPtr1->GetNumberOfWorkUnits();
testPtr1->Initialize();
testPtr1->SetNumberOfWorkUnits(nrOfThreadsPerMetric);
}
Expand Down
Loading