File: itkImageRegionGTest.cxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (240 lines) | stat: -rw-r--r-- 8,989 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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
 *
 *         https://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 "itkImageRegion.h"
#include "itkIndexRange.h"
#include <gtest/gtest.h>
#include <limits>
#include <type_traits> // For remove_const_t and remove_reference_t.


namespace
{
template <unsigned int VDimension>
constexpr bool
CheckTrivialCopyabilityOfImageRegion()
{
  constexpr bool isImageRegionTriviallyCopyable{ std::is_trivially_copyable_v<itk::ImageRegion<VDimension>> };

#ifdef ITK_FUTURE_LEGACY_REMOVE
  static_assert(isImageRegionTriviallyCopyable, "In the future, ImageRegion<VDimension> should be trivially copyable.");
  return isImageRegionTriviallyCopyable;
#else
  static_assert(!isImageRegionTriviallyCopyable, "ImageRegion<VDimension> should *not* be trivially copyable.");
  return !isImageRegionTriviallyCopyable;
#endif
}


// Checks that `ImageRegion` supports class template argument deduction (CTAD).
template <unsigned int VDimension>
constexpr bool
CheckClassTemplateArgumentDeduction()
{
  using ExpectedType = itk::ImageRegion<VDimension>;

  static_assert(
    std::is_same_v<decltype(itk::ImageRegion(itk::Index<VDimension>{}, itk::Size<VDimension>{})), ExpectedType>,
    "The `ImageRegion(Index, Size)` constructor should support CTAD!");
  static_assert(std::is_same_v<decltype(itk::ImageRegion(itk::Size<VDimension>{})), ExpectedType>,
                "The `ImageRegion(Size)` constructor should support CTAD!");
  return true;
}


} // namespace

static_assert(CheckTrivialCopyabilityOfImageRegion<2>() && CheckTrivialCopyabilityOfImageRegion<3>(),
              "ImageRegion<VDimension> should be trivially copyable when legacy support is removed.");

static_assert(CheckClassTemplateArgumentDeduction<2>() && CheckClassTemplateArgumentDeduction<3>());

// Tests that a zero-sized region is not considered to be inside of another region.
TEST(ImageRegion, ZeroSizedRegionIsNotInside)
{
  using RegionType = itk::ImageRegion<2>;
  using IndexType = RegionType::IndexType;
  using SizeType = RegionType::SizeType;

  const RegionType region(SizeType::Filled(2));

  for (const auto indexValue : { -1, 0, 1 })
  {
    const RegionType zeroSizedRegion{ IndexType::Filled(indexValue), SizeType{ { 0 } } };

    EXPECT_FALSE(region.IsInside(zeroSizedRegion));
  }
}


// Tests that regions of size 1 are considered to be inside of a region, if and only if ("iff") their index is inside of
// this region.
TEST(ImageRegion, OneSizedRegionIsInsideIffItsIndexIsInside)
{
  const auto check = [](const auto & region) {
    using RegionType = std::remove_const_t<std::remove_reference_t<decltype(region)>>;
    using SizeType = typename RegionType::SizeType;

    auto paddedRegion = region;
    paddedRegion.PadByRadius(1);

    for (const auto & index : itk::ImageRegionIndexRange<RegionType::ImageDimension>(paddedRegion))
    {
      const RegionType oneSizedRegion{ index, SizeType::Filled(1) };

      // The one-sized region is inside this region if and only if its index is inside this region.
      EXPECT_EQ(region.IsInside(oneSizedRegion), region.IsInside(index));
    }
  };

  // Check for a 2D and a 3D image region.
  check(itk::ImageRegion<2>(itk::Size<2>::Filled(3)));
  check(itk::ImageRegion<3>(itk::MakeIndex(-1, 0, 1), itk::MakeSize(2, 3, 4)));
}


// Tests that Crop returns false and does not change anything, when it cannot crop. That is when one of the regions is
// zero-sized, or when the region to be cropped and the crop region don't overlap.
TEST(ImageRegion, CropReturnsFalseWithoutChangingAnythingWhenItCannotCrop)
{
  const auto check = [](const auto & originalRegion, const auto & cropRegion) {
    auto regionToBeCropped = originalRegion;

    EXPECT_FALSE(regionToBeCropped.Crop(cropRegion));
    EXPECT_EQ(regionToBeCropped, originalRegion);
  };

  using RegionType = itk::ImageRegion<2>;
  using IndexType = RegionType::IndexType;
  using SizeType = RegionType::SizeType;

  for (const auto indexValue : { std::numeric_limits<itk::IndexValueType>::min(),
                                 itk::IndexValueType{ -1 },
                                 itk::IndexValueType{},
                                 itk::IndexValueType{ 1 } })
  {
    const RegionType zeroSizedRegion{ IndexType::Filled(indexValue), SizeType{} };
    const RegionType nonZeroSizedRegion{ IndexType::Filled(indexValue), SizeType::Filled(1) };

    // Cannot crop when the region to be cropped and/or the crop region is zero-sized.
    check(zeroSizedRegion, zeroSizedRegion);
    check(zeroSizedRegion, nonZeroSizedRegion);
    check(nonZeroSizedRegion, zeroSizedRegion);
  }

  for (const auto indexValue : { std::numeric_limits<itk::IndexValueType>::min(),
                                 itk::IndexValueType{ -1 },
                                 itk::IndexValueType{},
                                 itk::IndexValueType{ 1 } })
  {
    for (const itk::SizeValueType sizeValue : { 1, 2 })
    {
      const RegionType region1{ IndexType::Filled(indexValue), SizeType::Filled(sizeValue) };
      const RegionType region2{ IndexType::Filled(indexValue + static_cast<itk::IndexValueType>(sizeValue)),
                                SizeType::Filled(sizeValue) };

      // Cannot crop when the region to be cropped and the crop region do not overlap.
      check(region1, region2);
      check(region2, region1);
    }
  }
}


// Tests that Crop returns true, and does not change the region to be cropped, when it is equal to the crop region
// (assuming that both regions are not zero-sized).
TEST(ImageRegion, CropDoesNotChangeRegionToBeCroppedWhenCropRegionIsEqual)
{
  using RegionType = itk::ImageRegion<2>;
  using IndexType = RegionType::IndexType;
  using SizeType = RegionType::SizeType;

  for (const auto indexValue : { std::numeric_limits<itk::IndexValueType>::min(),
                                 itk::IndexValueType{ -1 },
                                 itk::IndexValueType{},
                                 itk::IndexValueType{ 1 } })
  {
    for (const itk::SizeValueType sizeValue : { 1, 2 })
    {
      const RegionType region{ IndexType::Filled(indexValue), SizeType::Filled(sizeValue) };
      auto             regionToBeCropped = region;
      EXPECT_TRUE(regionToBeCropped.Crop(region));
      EXPECT_EQ(regionToBeCropped, region);
    }
  }
}


// Tests cropping a larger to a smaller region, and vice versa.
TEST(ImageRegion, CropLargerToSmallerRegionAndViceVersa)
{
  using RegionType = itk::ImageRegion<2>;
  using IndexType = RegionType::IndexType;
  using SizeType = RegionType::SizeType;

  for (const auto indexValue : { std::numeric_limits<itk::IndexValueType>::min(),
                                 itk::IndexValueType{ -1 },
                                 itk::IndexValueType{},
                                 itk::IndexValueType{ 1 } })
  {
    for (const itk::SizeValueType sizeValue : { 1, 2 })
    {
      const RegionType smallerRegion{ IndexType::Filled(indexValue), SizeType::Filled(sizeValue) };
      const RegionType largerRegion{ smallerRegion.GetIndex(), SizeType::Filled(sizeValue + 1) };

      {
        auto regionToBeCropped = largerRegion;

        EXPECT_TRUE(regionToBeCropped.Crop(smallerRegion));
        EXPECT_EQ(regionToBeCropped, smallerRegion);
      }
      {
        auto regionToBeCropped = smallerRegion;

        EXPECT_TRUE(regionToBeCropped.Crop(largerRegion));
        EXPECT_EQ(regionToBeCropped, smallerRegion);
      }
    }
  }
}


// Tests C++ structured binding of an ImageRegion.
TEST(ImageRegion, SupportsStructuredBinding)
{
  using RegionType = itk::ImageRegion<2>;
  using IndexType = RegionType::IndexType;
  using SizeType = RegionType::SizeType;

  RegionType region{};
  auto && [index, size] = region;

  static_assert(std::is_same_v<decltype(index), IndexType>);
  static_assert(std::is_same_v<decltype(size), SizeType>);
  EXPECT_EQ(&index, &(region.GetIndex()));
  EXPECT_EQ(&size, &(region.GetSize()));

  const RegionType constRegion{};
  auto && [constIndex, constSize] = constRegion;

  static_assert(std::is_same_v<decltype(constIndex), const IndexType>);
  static_assert(std::is_same_v<decltype(constSize), const SizeType>);
  EXPECT_EQ(&constIndex, &(constRegion.GetIndex()));
  EXPECT_EQ(&constSize, &(constRegion.GetSize()));
}