Orfeo Toolbox  4.0
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 "vnl/vnl_math.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 ImageBase;
43 
68 template< unsigned int VImageDimension >
69 class ImageRegion:public Region
70 {
71 public:
73  typedef ImageRegion Self;
74  typedef Region Superclass;
75 
77  itkTypeMacro(ImageRegion, Region);
78 
80  itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
81 
84  itkStaticConstMacro( SliceDimension, unsigned int,
85  ( ImageDimension - ( ImageDimension > 1 ) ) );
86 
88  static unsigned int GetImageDimension()
89  { return ImageDimension; }
90 
98 
102 
105 
107  virtual typename Superclass::RegionType GetRegionType() const
109 
112  ImageRegion();
113 
116  virtual ~ImageRegion();
117 
120  ImageRegion(const Self & region):Region(region), m_Index(region.m_Index), m_Size(region.m_Size) {}
121 
124  ImageRegion(const IndexType & index, const SizeType & size)
125  { m_Index = index; m_Size = size; }
126 
130  ImageRegion(const SizeType & size)
131  { m_Size = size; m_Index.Fill(0); }
132 
135  void operator=(const Self & region)
136  { m_Index = region.m_Index;m_Size = region.m_Size; }
137 
139  void SetIndex(const IndexType & index)
140  { m_Index = index; }
141 
143  const IndexType & GetIndex() const { return m_Index; }
145 
148  void SetSize(const SizeType & size)
149  { m_Size = size; }
150 
152  const SizeType & GetSize() const { return m_Size; }
154 
157  void SetSize(unsigned int i, SizeValueType sze)
158  { m_Size[i] = sze; }
159  SizeValueType GetSize(unsigned int i) const
160  { return m_Size[i]; }
161 
164  void SetIndex(unsigned int i, IndexValueType sze)
165  { m_Index[i] = sze; }
166  IndexValueType GetIndex(unsigned int i) const
167  { return m_Index[i]; }
168 
170  IndexType GetUpperIndex() const;
171 
173  void SetUpperIndex( const IndexType & idx );
174 
176  void ComputeOffsetTable(OffsetTableType offsetTable) const;
177 
179  bool
180  operator==(const Self & region) const
181  {
182  bool same = ( m_Index == region.m_Index );
183  same = same && ( m_Size == region.m_Size );
184  return same;
185  }
186 
188  bool
189  operator!=(const Self & region) const
190  {
191  bool same = ( m_Index == region.m_Index );
192  same = same && ( m_Size == region.m_Size );
193  return !same;
194  }
195 
197  bool
198  IsInside(const IndexType & index) const
199  {
200  for ( unsigned int i = 0; i < ImageDimension; i++ )
201  {
202  if ( index[i] < m_Index[i] )
203  {
204  return false;
205  }
206  if ( index[i] >= ( m_Index[i] + static_cast< IndexValueType >( m_Size[i] ) ) )
207  {
208  return false;
209  }
210  }
211  return true;
212  }
213 
218  template< typename TCoordRepType >
219  bool
221  {
222  for ( unsigned int i = 0; i < ImageDimension; i++ )
223  {
224  if ( Math::RoundHalfIntegerUp< IndexValueType >(index[i]) < static_cast< IndexValueType >( m_Index[i] ) )
225  {
226  return false;
227  }
228  // bound is the last valid pixel location
229  const TCoordRepType bound = static_cast< TCoordRepType >(
230  m_Index[i] + m_Size[i] - 0.5 );
231 
232  /* Note for NaN: test using negation of a positive test in order
233  * to always evaluate to true (and thus return false) when index[i]
234  * is NaN. The cast above to integer via RoundHalfIntegerUp will cast
235  * NaN into a platform-dependent value (large negative, -1 or large
236  * positive, empirically). Thus this test here is relied on
237  * to 'catch' NaN's. */
238  if ( ! (index[i] <= bound) )
239  {
240  return false;
241  }
242  }
243  return true;
244  }
245 
250  bool
251  IsInside(const Self & region) const
252  {
253  IndexType beginCorner = region.GetIndex();
254 
255  if ( !this->IsInside(beginCorner) )
256  {
257  return false;
258  }
259  IndexType endCorner;
260  SizeType size = region.GetSize();
261  for ( unsigned int i = 0; i < ImageDimension; i++ )
262  {
263  endCorner[i] = beginCorner[i] + static_cast< OffsetValueType >( size[i] ) - 1;
264  }
265  if ( !this->IsInside(endCorner) )
266  {
267  return false;
268  }
269  return true;
270  }
271 
275 
279  void PadByRadius(OffsetValueType radius);
280 
281  void PadByRadius(const IndexValueArrayType radius);
282 
283  void PadByRadius(const SizeType & radius);
284 
289  bool ShrinkByRadius(OffsetValueType radius);
290 
291  bool ShrinkByRadius(const IndexValueArrayType radius);
292 
293  bool ShrinkByRadius(const SizeType & radius);
294 
299  bool Crop(const Self & region);
300 
304  SliceRegion Slice(const unsigned int dim) const;
305 
306 protected:
311  virtual void PrintSelf(std::ostream & os, Indent indent) const;
312 
313 private:
316 
318  friend class ImageBase< VImageDimension >;
319 };
320 
321 template< unsigned int VImageDimension >
322 std::ostream & operator<<(std::ostream & os, const ImageRegion< VImageDimension > & region);
323 } // end namespace itk
324 
325 #ifndef ITK_MANUAL_INSTANTIATION
326 #include "itkImageRegion.hxx"
327 #endif
328 
329 #endif

Generated at Sat Mar 8 2014 14:51:53 for Orfeo Toolbox with doxygen 1.8.3.1