cmtkRegion.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 2010 SRI International
00004 //
00005 //  Copyright 2010 Torsten Rohlfing
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2490 $
00026 //
00027 //  $LastChangedDate: 2010-10-21 14:32:20 -0700 (Thu, 21 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkRegion_h_included_
00034 #define __cmtkRegion_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <Base/cmtkFixedVector.h>
00039 
00040 #include <System/cmtkSmartPtr.h>
00041 #include <System/cmtkSmartConstPtr.h>
00042 
00043 #include <fstream>
00044 
00045 namespace
00046 cmtk
00047 {
00049 template<size_t NDIM,typename T=int>
00050 class Region
00051 {
00052 public:
00054   typedef Region<NDIM,T> Self;
00055 
00057   typedef FixedVector<NDIM,T> IndexType;
00058 
00060   typedef SmartPointer<Self> SmartPtr;
00061 
00063   typedef SmartConstPointer<Self> SmartConstPtr;
00064 
00066   Region() {}
00067 
00069   Region( const typename Self::IndexType& fromIndex, const typename Self::IndexType& toIndex )
00070   {
00071     this->m_RegionFrom = fromIndex;
00072     this->m_RegionTo = toIndex;
00073   }
00074   
00076   typename Self::IndexType& From()
00077   {
00078     return this->m_RegionFrom;
00079   }
00080 
00082   const typename Self::IndexType& From() const
00083   {
00084     return this->m_RegionFrom;
00085   }
00086 
00088   typename Self::IndexType& To()
00089   {
00090     return this->m_RegionTo;
00091   }
00092 
00094   const typename Self::IndexType& To() const
00095   {
00096     return this->m_RegionTo;
00097   }
00098 
00100   T Size() const
00101   {
00102     T size = (this->m_RegionTo[0]-this->m_RegionFrom[0]);
00103     for ( size_t i = 1; i < NDIM; ++i )
00104       size *= (this->m_RegionTo[i]-this->m_RegionFrom[i]);
00105     return size;
00106   }
00107 
00109   const typename Self::IndexType begin() const
00110   {
00111     return this->m_RegionFrom;
00112   }
00113   
00115   const typename Self::IndexType end() const
00116   {
00117     typename Self::IndexType e = this->m_RegionFrom;
00118     ++e[NDIM-1];
00119     return e;
00120   }
00121   
00122 private:
00124   typename Self::IndexType m_RegionFrom;
00125 
00127   typename Self::IndexType m_RegionTo;
00128 };
00129 
00131 template<size_t NDIM,typename T>
00132 std::ofstream& operator<<( std::ofstream& stream, const Region<NDIM,T>& region )
00133 {
00134   return stream << region.From() << region.To();
00135 }
00136 
00138 template<size_t NDIM,typename T>
00139 std::ifstream& operator>>( std::ifstream& stream, Region<NDIM,T>& region )
00140 {
00141   return stream >> region.From() >> region.To();
00142 }
00143 
00144 } // namespace cmtk
00145 
00146 #endif // #ifndef __cmtkRegion_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines