ITK  6.0.0
Insight Toolkit
itkImageRegion.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
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 * https://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 *
20 * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21 *
22 * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23 *
24 * For complete copyright, license and disclaimer of warranty information
25 * please refer to the NOTICE file at the top of the ITK source tree.
26 *
27 *=========================================================================*/
28#ifndef itkImageRegion_h
29#define itkImageRegion_h
30
31#include "itkRegion.h"
32
33#include "itkSize.h"
34#include "itkContinuousIndex.h"
35#include "itkMath.h"
36#include <type_traits> // For conditional and integral_constant.
37#include <utility> // For tuple_element and tuple_size.
38
39// Macro added to each `ImageRegion` member function that overrides a virtual member function of `Region`, when legacy
40// support is enabled. Without legacy support, `ImageRegion` will no longer inherit from `Region`, so then those
41// `ImageRegion` member functions will no longer override.
42#ifdef ITK_LEGACY_REMOVE
43# define itkRegionOverrideMacro // nothing
44#else
45# define itkRegionOverrideMacro override
46#endif
47
48namespace itk
49{
50// Forward declaration of ImageBase so it can be declared a friend
51// (needed for PrintSelf mechanism)
52template <unsigned int VImageDimension>
53class ITK_TEMPLATE_EXPORT ImageBase;
54
79template <unsigned int VImageDimension>
80class ITK_TEMPLATE_EXPORT ImageRegion final
81#ifndef ITK_LEGACY_REMOVE
82 // This inheritance is only there when legacy support is enabled.
83 : public Region
84#endif
85{
86public:
88 using Self = ImageRegion;
89
90#ifndef ITK_LEGACY_REMOVE
91 using Superclass = Region;
92#endif
93
95 const char *
97 {
98 return "ImageRegion";
99 }
100
102 static constexpr unsigned int ImageDimension = VImageDimension;
103
106 static constexpr unsigned int SliceDimension = ImageDimension - (ImageDimension > 1);
107
109 static constexpr unsigned int
110 GetImageDimension()
111 {
112 return ImageDimension;
113 }
114
120 using IndexValueArrayType = IndexValueType[ImageDimension];
121 using OffsetTableType = OffsetValueType[ImageDimension + 1];
122
125 using SizeValueType = typename SizeType::SizeValueType;
126
129
132 GetRegionType() const itkRegionOverrideMacro
133 {
135 }
136
138 void
139 Print(std::ostream & os, Indent indent = 0) const itkRegionOverrideMacro;
140
144 ImageRegion() noexcept = default;
145
149
152 ImageRegion(const Self &) noexcept = default;
153
157 ImageRegion(const IndexType & index, const SizeType & size) noexcept
158 : // Note: Use parentheses instead of curly braces to initialize data members,
159 // to avoid AppleClang 6.0.0.6000056 compile errors, "no viable conversion..."
160 m_Index(index)
161 , m_Size(size)
162 {}
163
168 ImageRegion(const SizeType & size) noexcept
169 : m_Size(size)
170 {
171 // Note: m_Index is initialized by its C++11 default member initializer.
172 }
173
177 operator=(const Self &) noexcept = default;
178
180 void
181 SetIndex(const IndexType & index)
182 {
183 m_Index = index;
184 }
185
187 const IndexType &
188 GetIndex() const
189 {
190 return m_Index;
191 }
193 GetModifiableIndex()
194 {
195 return m_Index;
196 }
201 void
202 SetSize(const SizeType & size)
203 {
204 m_Size = size;
205 }
206
208 const SizeType &
209 GetSize() const
210 {
211 return m_Size;
212 }
214 GetModifiableSize()
215 {
216 return m_Size;
217 }
222 void
223 SetSize(unsigned int i, SizeValueType sze)
224 {
225 m_Size[i] = sze;
226 }
228 GetSize(unsigned int i) const
229 {
230 return m_Size[i];
231 }
236 void
237 SetIndex(unsigned int i, IndexValueType sze)
238 {
239 m_Index[i] = sze;
240 }
242 GetIndex(unsigned int i) const
243 {
244 return m_Index[i];
245 }
250 GetUpperIndex() const;
251
253 void
254 SetUpperIndex(const IndexType & idx);
255
257 void
258 ComputeOffsetTable(OffsetTableType offsetTable) const;
259
261 bool
262 operator==(const Self & region) const noexcept
263 {
264 return (m_Index == region.m_Index) && (m_Size == region.m_Size);
265 }
267 ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
268
270 bool
271 IsInside(const IndexType & index) const
272 {
273 for (unsigned int i = 0; i < ImageDimension; ++i)
274 {
275 if (index[i] < m_Index[i] || index[i] >= m_Index[i] + static_cast<IndexValueType>(m_Size[i]))
276 {
277 return false;
278 }
279 }
280 return true;
281 }
288 template <typename TCoordRepType>
289 bool
290 IsInside(const ContinuousIndex<TCoordRepType, VImageDimension> & index) const
291 {
292 constexpr TCoordRepType half = 0.5;
293 for (unsigned int i = 0; i < ImageDimension; ++i)
294 {
295 // Use negation of tests so that index[i]==NaN leads to returning false.
296 if (!(index[i] >= m_Index[i] - half && index[i] <= (m_Index[i] + static_cast<IndexValueType>(m_Size[i])) - half))
297 {
298 return false;
299 }
300 }
301 return true;
302 }
309 bool
310 IsInside(const Self & otherRegion) const
311 {
312 const auto & otherIndex = otherRegion.m_Index;
313 const auto & otherSize = otherRegion.m_Size;
316 for (unsigned int i = 0; i < ImageDimension; ++i)
317 {
318 if (otherIndex[i] < m_Index[i] || otherSize[i] == 0 ||
319 otherIndex[i] + static_cast<IndexValueType>(otherSize[i]) >
320 m_Index[i] + static_cast<IndexValueType>(m_Size[i]))
321 {
322 return false;
323 }
324 }
325 return true;
326 }
327
331 GetNumberOfPixels() const;
332
336 void
337 PadByRadius(OffsetValueType radius);
338
339 void
340 PadByRadius(const IndexValueArrayType radius);
341
342 void
343 PadByRadius(const SizeType & radius);
344
349 bool
350 ShrinkByRadius(OffsetValueType radius);
351
352 bool
353 ShrinkByRadius(const IndexValueArrayType radius);
354
355 bool
356 ShrinkByRadius(const SizeType & radius);
357
362 bool
363 Crop(const Self & region);
364
369 Slice(const unsigned int dim) const;
370
373 template <size_t VTupleIndex>
374 [[nodiscard]] auto &
375 get()
376 {
377 if constexpr (VTupleIndex == 0)
378 {
379 return m_Index;
380 }
381 else
382 {
383 static_assert(VTupleIndex == 1);
384 return m_Size;
385 }
386 }
390 template <size_t VTupleIndex>
391 [[nodiscard]] const auto &
392 get() const
393 {
394 if constexpr (VTupleIndex == 0)
395 {
396 return m_Index;
397 }
398 else
399 {
400 static_assert(VTupleIndex == 1);
401 return m_Size;
402 }
403 }
406protected:
411 void
412 PrintSelf(std::ostream & os, Indent indent) const itkRegionOverrideMacro;
413
414private:
415 IndexType m_Index = { { 0 } };
416 SizeType m_Size = { { 0 } };
417
419 friend class ImageBase<VImageDimension>;
420};
421
422
423// Deduction guide to avoid compiler warnings (-wctad-maybe-unsupported) when using class template argument deduction.
424template <unsigned int VImageDimension>
425ImageRegion(const Index<VImageDimension> &, const Size<VImageDimension> &)->ImageRegion<VImageDimension>;
426
427
428template <unsigned int VImageDimension>
429std::ostream &
430operator<<(std::ostream & os, const ImageRegion<VImageDimension> & region);
431} // end namespace itk
432
433
434namespace std
435{
436#if defined(__clang__) && defined(__apple_build_version__) && (__clang_major__ <= 10)
437# pragma clang diagnostic push
438// Old AppleClang 10.0.0 (Xcode 10.1, newest on macOS 10.13) produced some unimportant warnings, like:
439// "warning: 'tuple_size' defined as a struct template here but previously declared as a class template"
440# pragma clang diagnostic ignored "-Wmismatched-tags"
441#endif
442
443// NOLINTBEGIN(cert-dcl58-cpp)
444// Locally suppressed the following warning from Clang-Tidy (LLVM 17.0.1), as it appears undeserved.
445// > warning: modification of 'std' namespace can result in undefined behavior [cert-dcl58-cpp]
446
454template <unsigned int VImageDimension>
455struct tuple_size<itk::ImageRegion<VImageDimension>> : integral_constant<size_t, 2>
456{};
457
459template <size_t VTupleIndex, unsigned int VImageDimension>
460struct tuple_element<VTupleIndex, itk::ImageRegion<VImageDimension>>
461 : conditional<VTupleIndex == 0, itk::Index<VImageDimension>, itk::Size<VImageDimension>>
462{
463 static_assert(VTupleIndex < tuple_size_v<itk::ImageRegion<VImageDimension>>);
464};
465
466// NOLINTEND(cert-dcl58-cpp)
467
468#if defined(__clang__) && defined(__apple_build_version__) && (__clang_major__ <= 10)
469# pragma clang diagnostic pop
470#endif
471} // namespace std
472
473#undef itkRegionOverrideMacro
474
475#ifndef ITK_MANUAL_INSTANTIATION
476# include "itkImageRegion.hxx"
477#endif
478
479#endif
A templated class holding a point in n-Dimensional image space.
An image region represents a structured region of data.
typename IndexType::OffsetType OffsetType
typename IndexType::IndexValueType IndexValueType
OffsetValueType[ImageDimension+1] OffsetTableType
IndexValueType[ImageDimension] IndexValueArrayType
typename OffsetType::OffsetValueType OffsetValueType
typename SizeType::SizeValueType SizeValueType
Control indentation during Print() invocation.
Definition: itkIndent.h:50
A region represents some portion or piece of data.
Definition: itkRegion.h:66
const char * GetNameOfClass() const override
BinaryGeneratorImageFilter< TInputImage1, TInputImage2, TOutputImage > Superclass
#define itkRegionOverrideMacro
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
ImageRegion(const Index< VImageDimension > &, const Size< VImageDimension > &) -> ImageRegion< VImageDimension >
long IndexValueType
Definition: itkIntTypes.h:93
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:543
class ITK_TEMPLATE_EXPORT ImageBase
unsigned long SizeValueType
Definition: itkIntTypes.h:86
long OffsetValueType
Definition: itkIntTypes.h:97
STL namespace.