Skip to content

Commit ab2e2e9

Browse files
committed
ENH: Move FillImageRegion to elxCoreMainGTestUtilities.h
Aims to support both ITK 5.1 and ITK 5.2, by allowing `ImageRegionRange` to be either in `namespace itk` or in `namespace itk::Experimental`.
1 parent 7677baf commit ab2e2e9

File tree

5 files changed

+88
-33
lines changed

5 files changed

+88
-33
lines changed

Core/Main/GTesting/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_executable(ElastixLibGTest
2+
elxCoreMainGTestUtilities.h
23
ElastixFilterGTest.cxx
34
ElastixLibGTest.cxx
45
itkElastixRegistrationMethodGTest.cxx

Core/Main/GTesting/ElastixFilterGTest.cxx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919

2020
// First include the header file to be tested:
2121
#include <elxElastixFilter.h>
22+
23+
#include "elxCoreMainGTestUtilities.h"
24+
25+
// ITK header file:
2226
#include <itkImage.h>
23-
#include <itkImageRegionRange.h>
2427

2528
// GoogleTest header file:
2629
#include <gtest/gtest.h>
@@ -40,7 +43,6 @@ GTEST_TEST(ElastixFilter, Translation)
4043
using SizeType = itk::Size<ImageDimension>;
4144
using IndexType = itk::Index<ImageDimension>;
4245
using OffsetType = itk::Offset<ImageDimension>;
43-
using RegionRangeType = itk::Experimental::ImageRegionRange<ImageType>;
4446

4547
const OffsetType translationOffset{ { 1, -2 } };
4648
const auto regionSize = SizeType::Filled(2);
@@ -50,16 +52,12 @@ GTEST_TEST(ElastixFilter, Translation)
5052
const auto fixedImage = ImageType::New();
5153
fixedImage->SetRegions(imageSize);
5254
fixedImage->Allocate(true);
53-
const RegionType fixedImageRegion{ fixedImageRegionIndex, regionSize };
54-
const RegionRangeType fixedImageRegionRange{ *fixedImage, fixedImageRegion };
55-
std::fill(std::begin(fixedImageRegionRange), std::end(fixedImageRegionRange), 1.0f);
55+
elx::CoreMainGTestUtilities::FillImageRegion(*fixedImage, fixedImageRegionIndex, regionSize);
5656

5757
const auto movingImage = ImageType::New();
5858
movingImage->SetRegions(imageSize);
5959
movingImage->Allocate(true);
60-
const RegionType movingImageRegion{ fixedImageRegionIndex + translationOffset, regionSize };
61-
const RegionRangeType movingImageRegionRange{ *movingImage, movingImageRegion };
62-
std::fill(std::begin(movingImageRegionRange), std::end(movingImageRegionRange), 1.0f);
60+
elx::CoreMainGTestUtilities::FillImageRegion(*movingImage, fixedImageRegionIndex + translationOffset, regionSize);
6361

6462
const auto parameterObject = elastix::ParameterObject::New();
6563

Core/Main/GTesting/ElastixLibGTest.cxx

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
// First include the header file to be tested:
2121
#include "elastixlib.h"
2222

23+
#include "elxCoreMainGTestUtilities.h"
24+
2325
// ITK header files:
2426
#include <itkImage.h>
25-
#include <itkImageRegionRange.h>
2627

2728
// GoogleTest header file:
2829
#include <gtest/gtest.h>
@@ -122,17 +123,6 @@ ExpectRoundedTransformParametersEqualOffset(const elastix::ELASTIX & elas
122123
}
123124

124125

125-
template <typename TPixel, unsigned int VImageDimension>
126-
void
127-
FillImageRegion(itk::Image<TPixel, VImageDimension> & image,
128-
const itk::Index<VImageDimension> & regionIndex,
129-
const itk::Size<VImageDimension> & regionSize)
130-
{
131-
using ImageRegionRangeType = itk::Experimental::ImageRegionRange<itk::Image<TPixel, VImageDimension>>;
132-
const ImageRegionRangeType imageRegionRange{ image, itk::ImageRegion<VImageDimension>{ regionIndex, regionSize } };
133-
std::fill(std::begin(imageRegionRange), std::end(imageRegionRange), 1);
134-
}
135-
136126
template <unsigned VImageDimension>
137127
std::map<std::string, std::vector<std::string>>
138128
CreateParameterMap(std::initializer_list<std::pair<std::string, std::string>> initializerList)
@@ -170,7 +160,8 @@ Expect_TransformParameters_are_zero_when_fixed_image_is_moving_image()
170160
const auto image = ImageType::New();
171161
image->SetRegions(SizeType::Filled(imageSizeValue));
172162
image->Allocate(true);
173-
FillImageRegion(*image, itk::Index<VImageDimension>::Filled(1), SizeType::Filled(regionSizeValue));
163+
elx::CoreMainGTestUtilities::FillImageRegion(
164+
*image, itk::Index<VImageDimension>::Filled(1), SizeType::Filled(regionSizeValue));
174165

175166
elastix::ELASTIX elastixObject;
176167
ASSERT_EQ(elastixObject.RegisterImages(image, image, parameterMap, ".", false, false), 0);
@@ -258,12 +249,12 @@ GTEST_TEST(ElastixLib, ExampleFromManualRunningElastix)
258249
const auto fixed_image = ITKImageType::New();
259250
fixed_image->SetRegions(imageSize);
260251
fixed_image->Allocate(true);
261-
FillImageRegion(*fixed_image, fixedImageRegionIndex, regionSize);
252+
elx::CoreMainGTestUtilities::FillImageRegion(*fixed_image, fixedImageRegionIndex, regionSize);
262253

263254
const auto moving_image = ITKImageType::New();
264255
moving_image->SetRegions(imageSize);
265256
moving_image->Allocate(true);
266-
FillImageRegion(*moving_image, fixedImageRegionIndex + translationOffset, regionSize);
257+
elx::CoreMainGTestUtilities::FillImageRegion(*moving_image, fixedImageRegionIndex + translationOffset, regionSize);
267258

268259
const std::string output_directory(".");
269260
const bool write_log_file{ false };
@@ -360,12 +351,12 @@ GTEST_TEST(ElastixLib, Translation3D)
360351
const auto fixedImage = ImageType::New();
361352
fixedImage->SetRegions(imageSize);
362353
fixedImage->Allocate(true);
363-
FillImageRegion(*fixedImage, fixedImageRegionIndex, regionSize);
354+
elx::CoreMainGTestUtilities::FillImageRegion(*fixedImage, fixedImageRegionIndex, regionSize);
364355

365356
const auto movingImage = ImageType::New();
366357
movingImage->SetRegions(imageSize);
367358
movingImage->Allocate(true);
368-
FillImageRegion(*movingImage, fixedImageRegionIndex + translationOffset, regionSize);
359+
elx::CoreMainGTestUtilities::FillImageRegion(*movingImage, fixedImageRegionIndex + translationOffset, regionSize);
369360

370361
elastix::ELASTIX elastixObject;
371362

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
19+
#ifndef elxCoreMainGTestUtilities_h
20+
#define elxCoreMainGTestUtilities_h
21+
22+
#include <itkImage.h>
23+
#include <itkImageRegionRange.h>
24+
#include <itkIndex.h>
25+
#include <itkSize.h>
26+
27+
#include <algorithm> // For fill.
28+
#include <iterator> // For begin and end.
29+
30+
namespace itk
31+
{
32+
namespace Experimental
33+
{
34+
// Workaround to allow using things that may be either in itk or in itk::Experimental.
35+
}
36+
} // namespace itk
37+
38+
39+
namespace elastix
40+
{
41+
namespace CoreMainGTestUtilities
42+
{
43+
44+
/// Fills the specified image region with pixel values 1.
45+
template <typename TPixel, unsigned int VImageDimension>
46+
void
47+
FillImageRegion(itk::Image<TPixel, VImageDimension> & image,
48+
const itk::Index<VImageDimension> & regionIndex,
49+
const itk::Size<VImageDimension> & regionSize)
50+
{
51+
// ImageRegionRange is to be moved from namespace itk::Experimental
52+
// to namespace itk with ITK version 5.2.
53+
using namespace itk;
54+
using namespace itk::Experimental;
55+
56+
const ImageRegionRange<Image<TPixel, VImageDimension>> imageRegionRange{
57+
image, ImageRegion<VImageDimension>{ regionIndex, regionSize }
58+
};
59+
std::fill(std::begin(imageRegionRange), std::end(imageRegionRange), 1);
60+
}
61+
62+
63+
} // namespace CoreMainGTestUtilities
64+
} // namespace elastix
65+
66+
67+
#endif

Core/Main/GTesting/itkElastixRegistrationMethodGTest.cxx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919

2020
// First include the header file to be tested:
2121
#include <itkElastixRegistrationMethod.h>
22+
23+
#include "elxCoreMainGTestUtilities.h"
24+
25+
// ITK header file:
2226
#include <itkImage.h>
23-
#include <itkImageRegionRange.h>
2427

2528
// GoogleTest header file:
2629
#include <gtest/gtest.h>
@@ -40,7 +43,6 @@ GTEST_TEST(itkElastixRegistrationMethod, Translation)
4043
using SizeType = itk::Size<ImageDimension>;
4144
using IndexType = itk::Index<ImageDimension>;
4245
using OffsetType = itk::Offset<ImageDimension>;
43-
using RegionRangeType = itk::Experimental::ImageRegionRange<ImageType>;
4446

4547
const OffsetType translationOffset{ { 1, -2 } };
4648
const auto regionSize = SizeType::Filled(2);
@@ -50,16 +52,12 @@ GTEST_TEST(itkElastixRegistrationMethod, Translation)
5052
const auto fixedImage = ImageType::New();
5153
fixedImage->SetRegions(imageSize);
5254
fixedImage->Allocate(true);
53-
const RegionType fixedImageRegion{ fixedImageRegionIndex, regionSize };
54-
const RegionRangeType fixedImageRegionRange{ *fixedImage, fixedImageRegion };
55-
std::fill(std::begin(fixedImageRegionRange), std::end(fixedImageRegionRange), 1.0f);
55+
elx::CoreMainGTestUtilities::FillImageRegion(*fixedImage, fixedImageRegionIndex, regionSize);
5656

5757
const auto movingImage = ImageType::New();
5858
movingImage->SetRegions(imageSize);
5959
movingImage->Allocate(true);
60-
const RegionType movingImageRegion{ fixedImageRegionIndex + translationOffset, regionSize };
61-
const RegionRangeType movingImageRegionRange{ *movingImage, movingImageRegion };
62-
std::fill(std::begin(movingImageRegionRange), std::end(movingImageRegionRange), 1.0f);
60+
elx::CoreMainGTestUtilities::FillImageRegion(*movingImage, fixedImageRegionIndex + translationOffset, regionSize);
6361

6462
const auto parameterObject = elastix::ParameterObject::New();
6563

0 commit comments

Comments
 (0)