ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkImageIORegion.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkImageIORegion_h
00019 #define __itkImageIORegion_h
00020 
00021 #include <algorithm>
00022 #include "itkIntTypes.h"
00023 #include "itkObjectFactory.h"
00024 #include "itkImageRegion.h"
00025 
00026 namespace itk
00027 {
00052 class ITK_EXPORT ImageIORegion:public Region
00053 {
00054 public:
00056   typedef ImageIORegion Self;
00057   typedef Region        Superclass;
00058 
00061   typedef ::itk::SizeValueType    SizeValueType;
00062   typedef ::itk::IndexValueType   IndexValueType;
00063   typedef ::itk::OffsetValueType  OffsetValueType;
00064 
00066   typedef std::vector< IndexValueType > IndexType;
00067 
00069   typedef std::vector< SizeValueType > SizeType;
00070 
00072   typedef Superclass::RegionType RegionType;
00073 
00075   itkTypeMacro(ImageIORegion, Region);
00076 
00078   unsigned int GetImageDimension() const;
00079 
00083   unsigned int GetRegionDimension() const;
00084 
00086   virtual RegionType GetRegionType() const;
00087 
00090   ImageIORegion(unsigned int dimension);
00091 
00094   ImageIORegion();
00095 
00098   virtual ~ImageIORegion();
00099 
00102   ImageIORegion(const Self & region);
00103 
00106   void operator=(const Self & region);
00107 
00109   void SetIndex(const IndexType & index);
00110 
00112   const IndexType & GetIndex() const;
00113 
00116   void SetSize(const SizeType & size);
00117 
00119   const SizeType & GetSize() const;
00120 
00124   SizeValueType GetSize(unsigned long i) const;
00125 
00126   IndexValueType GetIndex(unsigned long i) const;
00127 
00128   void SetSize(const unsigned long i, SizeValueType size);
00129 
00130   void SetIndex(const unsigned long i, IndexValueType idx);
00131 
00133   bool operator==(const Self & region) const;
00134 
00136   bool operator!=(const Self & region) const;
00137 
00139   bool IsInside(const IndexType & index) const;
00140 
00142   bool IsInside(const Self & region) const;
00143 
00146   SizeValueType GetNumberOfPixels(void) const;
00147 
00148 protected:
00153   virtual void PrintSelf(std::ostream & os, Indent indent) const;
00154 
00155 private:
00156   unsigned int m_ImageDimension;
00157   IndexType    m_Index;
00158   SizeType     m_Size;
00159 };
00160 
00161 // Declare operator<<
00162 extern ITK_EXPORT std::ostream & operator<<(std::ostream & os, const ImageIORegion & region);
00163 
00170 template< unsigned int VDimension >
00171 class ImageIORegionAdaptor
00172 {
00173 public:
00174   typedef ImageRegion< VDimension > ImageRegionType;
00175   typedef ImageIORegion             ImageIORegionType;
00176 
00177   typedef typename ImageRegionType::SizeType  ImageSizeType;
00178   typedef typename ImageRegionType::IndexType ImageIndexType;
00179 
00180   static void Convert(const ImageRegionType & inImageRegion,
00181                       ImageIORegionType & outIORegion,
00182                       const ImageIndexType & largestRegionIndex)
00183   {
00184     //
00185     // The ImageRegion and ImageIORegion objects may have different dimensions.
00186     // Here we only copy the common dimensions between the two. If the
00187     // ImageRegion
00188     // has more dimensions than the ImageIORegion, then the defaults of the
00189     // ImageRegion
00190     // will take care of the remaining codimension. If the ImageRegion has less
00191     // dimensions
00192     // than the ImageIORegion, then the remaining IO dimensions are simply
00193     // ignored.
00194     //
00195     const unsigned int ioDimension = outIORegion.GetImageDimension();
00196     const unsigned int imageDimension = VDimension;
00197 
00198     unsigned int minDimension = ( ioDimension > imageDimension ) ? imageDimension : ioDimension;
00199 
00200     ImageSizeType  size  = inImageRegion.GetSize();
00201     ImageIndexType index = inImageRegion.GetIndex();
00202 
00203     for ( unsigned int i = 0; i < minDimension; i++ )
00204       {
00205       outIORegion.SetSize(i, size[i]);
00206       outIORegion.SetIndex(i, index[i] - largestRegionIndex[i]);
00207       }
00208 
00209     //
00210     // Fill in the remaining codimension (if any) with default values
00211     //
00212     for ( unsigned int k = minDimension; k < ioDimension; k++ )
00213       {
00214       outIORegion.SetSize(k, 1);    // Note that default size in IO is 1 not 0
00215       outIORegion.SetIndex(k, 0);
00216       }
00217   }
00218 
00219   static void Convert(const ImageIORegionType & inIORegion,
00220                       ImageRegionType & outImageRegion,
00221                       const ImageIndexType & largestRegionIndex)
00222   {
00223     ImageSizeType  size;
00224     ImageIndexType index;
00225 
00226     size.Fill(1);  // initialize with default values
00227     index.Fill(0);
00228 
00229     //
00230     // The ImageRegion and ImageIORegion objects may have different dimensions.
00231     // Here we only copy the common dimensions between the two. If the
00232     // ImageRegion
00233     // has more dimensions than the ImageIORegion, then the defaults of the
00234     // ImageRegion
00235     // will take care of the remaining codimension. If the ImageRegion has less
00236     // dimensions
00237     // than the ImageIORegion, then the remaining IO dimensions are simply
00238     // ignored.
00239     //
00240     const unsigned int ioDimension = inIORegion.GetImageDimension();
00241     const unsigned int imageDimension = VDimension;
00242 
00243     unsigned int minDimension = ( ioDimension > imageDimension ) ? imageDimension : ioDimension;
00244 
00245     for ( unsigned int i = 0; i < minDimension; i++ )
00246       {
00247       size[i]  = inIORegion.GetSize(i);
00248       index[i] = inIORegion.GetIndex(i) + largestRegionIndex[i];
00249       }
00250 
00251     outImageRegion.SetSize(size);
00252     outImageRegion.SetIndex(index);
00253   }
00254 };
00255 } // end namespace itk
00256 
00257 #endif
00258