ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkImageRegion.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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  *
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 
37 namespace itk
38 {
39 // Forward declaration of ImageBase so it can be declared a friend
40 // (needed for PrintSelf mechanism)
41 template< unsigned int VImageDimension >
42 class ITK_TEMPLATE_EXPORT ImageBase;
43 
68 template< unsigned int VImageDimension >
69 class ITK_TEMPLATE_EXPORT ImageRegion final: public Region
70 {
71 public:
73  using Self = ImageRegion;
74  using Superclass = Region;
75 
77  itkTypeMacro(ImageRegion, Region);
78 
80  static constexpr unsigned int ImageDimension = VImageDimension;
81 
84  static constexpr unsigned int SliceDimension = ImageDimension - ( ImageDimension > 1 );
85 
87  static unsigned int GetImageDimension()
88  { return ImageDimension; }
89 
95  typedef IndexValueType IndexValueArrayType[ImageDimension];
96  typedef OffsetValueType OffsetTableType[ImageDimension+1];
97 
101 
104 
106  typename Superclass::RegionType GetRegionType() const override
107  { return Superclass::ITK_STRUCTURED_REGION; }
108 
112  ImageRegion() ITK_NOEXCEPT = default;
113 
116  ~ImageRegion() override = default;
117 
120  ImageRegion(const Self & ) ITK_NOEXCEPT = default;
121 
124  ImageRegion(const IndexType & index, const SizeType & size) ITK_NOEXCEPT
125  :
126  // Note: Use parentheses instead of curly braces to initialize data members,
127  // to avoid AppleClang 6.0.0.6000056 compile errors, "no viable conversion..."
128  m_Index(index),
129  m_Size(size)
130  {
131  }
132 
136  ImageRegion(const SizeType & size) ITK_NOEXCEPT
137  :
138  m_Size(size)
139  {
140  // Note: m_Index is initialized by its C++11 default member initializer.
141  }
142 
145  Self& operator=(const Self & ) ITK_NOEXCEPT = default;
146 
148  void SetIndex(const IndexType & index)
149  { m_Index = index; }
150 
152  const IndexType & GetIndex() const { return m_Index; }
153  IndexType & GetModifiableIndex() { return m_Index; }
155 
158  void SetSize(const SizeType & size)
159  { m_Size = size; }
160 
162  const SizeType & GetSize() const { return m_Size; }
163  SizeType & GetModifiableSize() { return m_Size; }
165 
168  void SetSize(unsigned int i, SizeValueType sze)
169  { m_Size[i] = sze; }
170  SizeValueType GetSize(unsigned int i) const
171  { return m_Size[i]; }
173 
176  void SetIndex(unsigned int i, IndexValueType sze)
177  { m_Index[i] = sze; }
178  IndexValueType GetIndex(unsigned int i) const
179  { return m_Index[i]; }
181 
183  IndexType GetUpperIndex() const;
184 
186  void SetUpperIndex( const IndexType & idx );
187 
189  void ComputeOffsetTable(OffsetTableType offsetTable) const;
190 
192  bool
193  operator==(const Self & region) const ITK_NOEXCEPT
194  {
195  return (m_Index == region.m_Index) && (m_Size == region.m_Size);
196  }
197 
199  bool
200  operator!=(const Self & region) const ITK_NOEXCEPT
201  {
202  return !(*this == region);
203  }
204 
206  bool
207  IsInside(const IndexType & index) const
208  {
209  for ( unsigned int i = 0; i < ImageDimension; i++ )
210  {
211  if ( index[i] < m_Index[i] )
212  {
213  return false;
214  }
215  if ( index[i] >= ( m_Index[i] + static_cast< IndexValueType >( m_Size[i] ) ) )
216  {
217  return false;
218  }
219  }
220  return true;
221  }
223 
228  template< typename TCoordRepType >
229  bool
231  {
232  for ( unsigned int i = 0; i < ImageDimension; i++ )
233  {
234  if ( Math::RoundHalfIntegerUp< IndexValueType >(index[i]) < static_cast< IndexValueType >( m_Index[i] ) )
235  {
236  return false;
237  }
238  // bound is the last valid pixel location
239  const auto bound = static_cast< TCoordRepType >(
240  m_Index[i] + m_Size[i] - 0.5 );
242 
243  /* Note for NaN: test using negation of a positive test in order
244  * to always evaluate to true (and thus return false) when index[i]
245  * is NaN. The cast above to integer via RoundHalfIntegerUp will cast
246  * NaN into a platform-dependent value (large negative, -1 or large
247  * positive, empirically). Thus this test here is relied on
248  * to 'catch' NaN's. */
249  if ( ! (index[i] <= bound) )
250  {
251  return false;
252  }
253  }
254  return true;
255  }
256 
261  bool
262  IsInside(const Self & region) const
263  {
264  IndexType beginCorner = region.GetIndex();
265 
266  if ( !this->IsInside(beginCorner) )
267  {
268  return false;
269  }
270  IndexType endCorner;
271  const SizeType & size = region.GetSize();
272  for ( unsigned int i = 0; i < ImageDimension; i++ )
273  {
274  endCorner[i] = beginCorner[i] + static_cast< OffsetValueType >( size[i] ) - 1;
275  }
276  if ( !this->IsInside(endCorner) )
277  {
278  return false;
279  }
280  return true;
281  }
282 
285  SizeValueType GetNumberOfPixels() const;
286 
290  void PadByRadius(OffsetValueType radius);
291 
292  void PadByRadius(const IndexValueArrayType radius);
293 
294  void PadByRadius(const SizeType & radius);
295 
300  bool ShrinkByRadius(OffsetValueType radius);
301 
302  bool ShrinkByRadius(const IndexValueArrayType radius);
303 
304  bool ShrinkByRadius(const SizeType & radius);
305 
310  bool Crop(const Self & region);
311 
315  SliceRegion Slice(const unsigned int dim) const;
316 
317 protected:
322  void PrintSelf(std::ostream & os, Indent indent) const override;
323 
324 private:
325  IndexType m_Index = {{0}};
326  SizeType m_Size = {{0}};
327 
329  friend class ImageBase< VImageDimension >;
330 };
331 
332 template< unsigned int VImageDimension >
333 std::ostream & operator<<(std::ostream & os, const ImageRegion< VImageDimension > & region);
334 } // end namespace itk
335 
336 #ifndef ITK_MANUAL_INSTANTIATION
337 #include "itkImageRegion.hxx"
338 #endif
339 
340 #endif
void SetSize(const SizeType &size)
typename IndexType::OffsetType OffsetType
bool operator==(const Self &region) const noexcept
IndexValueType GetIndex(unsigned int i) const
const IndexType & GetIndex() const
unsigned long SizeValueType
Definition: itkIntTypes.h:83
bool IsInside(const IndexType &index) const
An image region represents a structured region of data.
typename IndexType::IndexValueType IndexValueType
static unsigned int GetImageDimension()
Superclass::RegionType GetRegionType() const override
SizeValueType GetSize(unsigned int i) const
ImageRegion(const SizeType &size) noexcept
IndexType & GetModifiableIndex()
SizeType & GetModifiableSize()
signed long IndexValueType
Definition: itkIntTypes.h:90
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image...
Definition: itkOffset.h:67
void SetSize(unsigned int i, SizeValueType sze)
const SizeType & GetSize() const
A region represents some portion or piece of data.
Definition: itkRegion.h:64
typename OffsetType::OffsetValueType OffsetValueType
class ITK_TEMPLATE_EXPORT ImageBase
bool IsInside(const Self &region) const
Base class for templated image classes.
Definition: itkImageBase.h:105
A templated class holding a point in n-Dimensional image space.
Control indentation during Print() invocation.
Definition: itkIndent.h:49
bool operator!=(const Self &region) const noexcept
void SetIndex(unsigned int i, IndexValueType sze)
typename SizeType::SizeValueType SizeValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
bool IsInside(const ContinuousIndex< TCoordRepType, VImageDimension > &index) const