ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkImageIORegion.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 #ifndef __itkImageIORegion_h
19 #define __itkImageIORegion_h
20 
21 #include <algorithm>
22 #include "itkIntTypes.h"
23 #include "itkObjectFactory.h"
24 #include "itkImageRegion.h"
25 
26 namespace itk
27 {
52 class ITK_EXPORT ImageIORegion:public Region
53 {
54 public:
57  typedef Region Superclass;
58 
64 
66  typedef std::vector< IndexValueType > IndexType;
67 
69  typedef std::vector< SizeValueType > SizeType;
70 
73 
75  itkTypeMacro(ImageIORegion, Region);
76 
78  unsigned int GetImageDimension() const;
79 
83  unsigned int GetRegionDimension() const;
84 
86  virtual RegionType GetRegionType() const;
87 
90  ImageIORegion(unsigned int dimension);
91 
94  ImageIORegion();
95 
98  virtual ~ImageIORegion();
99 
102  ImageIORegion(const Self & region);
103 
106  void operator=(const Self & region);
107 
109  void SetIndex(const IndexType & index);
110 
112  const IndexType & GetIndex() const;
113 
116  void SetSize(const SizeType & size);
117 
119  const SizeType & GetSize() const;
120 
124  SizeValueType GetSize(unsigned long i) const;
125 
126  IndexValueType GetIndex(unsigned long i) const;
127 
128  void SetSize(const unsigned long i, SizeValueType size);
129 
130  void SetIndex(const unsigned long i, IndexValueType idx);
131 
133  bool operator==(const Self & region) const;
134 
136  bool operator!=(const Self & region) const;
137 
139  bool IsInside(const IndexType & index) const;
140 
142  bool IsInside(const Self & region) const;
143 
146  SizeValueType GetNumberOfPixels(void) const;
147 
148 protected:
153  virtual void PrintSelf(std::ostream & os, Indent indent) const;
154 
155 private:
156  unsigned int m_ImageDimension;
159 };
160 
161 // Declare operator<<
162 extern ITK_EXPORT std::ostream & operator<<(std::ostream & os, const ImageIORegion & region);
163 
170 template< unsigned int VDimension >
172 {
173 public:
176 
179 
180  static void Convert(const ImageRegionType & inImageRegion,
181  ImageIORegionType & outIORegion,
182  const ImageIndexType & largestRegionIndex)
183  {
184  //
185  // The ImageRegion and ImageIORegion objects may have different dimensions.
186  // Here we only copy the common dimensions between the two. If the
187  // ImageRegion
188  // has more dimensions than the ImageIORegion, then the defaults of the
189  // ImageRegion
190  // will take care of the remaining codimension. If the ImageRegion has less
191  // dimensions
192  // than the ImageIORegion, then the remaining IO dimensions are simply
193  // ignored.
194  //
195  const unsigned int ioDimension = outIORegion.GetImageDimension();
196  const unsigned int imageDimension = VDimension;
197 
198  unsigned int minDimension = ( ioDimension > imageDimension ) ? imageDimension : ioDimension;
199 
200  ImageSizeType size = inImageRegion.GetSize();
201  ImageIndexType index = inImageRegion.GetIndex();
202 
203  for ( unsigned int i = 0; i < minDimension; i++ )
204  {
205  outIORegion.SetSize(i, size[i]);
206  outIORegion.SetIndex(i, index[i] - largestRegionIndex[i]);
207  }
208 
209  //
210  // Fill in the remaining codimension (if any) with default values
211  //
212  for ( unsigned int k = minDimension; k < ioDimension; k++ )
213  {
214  outIORegion.SetSize(k, 1); // Note that default size in IO is 1 not 0
215  outIORegion.SetIndex(k, 0);
216  }
217  }
218 
219  static void Convert(const ImageIORegionType & inIORegion,
220  ImageRegionType & outImageRegion,
221  const ImageIndexType & largestRegionIndex)
222  {
223  ImageSizeType size;
224  ImageIndexType index;
225 
226  size.Fill(1); // initialize with default values
227  index.Fill(0);
228 
229  //
230  // The ImageRegion and ImageIORegion objects may have different dimensions.
231  // Here we only copy the common dimensions between the two. If the
232  // ImageRegion
233  // has more dimensions than the ImageIORegion, then the defaults of the
234  // ImageRegion
235  // will take care of the remaining codimension. If the ImageRegion has less
236  // dimensions
237  // than the ImageIORegion, then the remaining IO dimensions are simply
238  // ignored.
239  //
240  const unsigned int ioDimension = inIORegion.GetImageDimension();
241  const unsigned int imageDimension = VDimension;
242 
243  unsigned int minDimension = ( ioDimension > imageDimension ) ? imageDimension : ioDimension;
244 
245  for ( unsigned int i = 0; i < minDimension; i++ )
246  {
247  size[i] = inIORegion.GetSize(i);
248  index[i] = inIORegion.GetIndex(i) + largestRegionIndex[i];
249  }
250 
251  outImageRegion.SetSize(size);
252  outImageRegion.SetIndex(index);
253  }
254 };
255 } // end namespace itk
256 
257 #endif
258